ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include <linux/capability.h>
  2. #include <linux/blkdev.h>
  3. #include <linux/export.h>
  4. #include <linux/gfp.h>
  5. #include <linux/blkpg.h>
  6. #include <linux/hdreg.h>
  7. #include <linux/backing-dev.h>
  8. #include <linux/fs.h>
  9. #include <linux/blktrace_api.h>
  10. #include <asm/uaccess.h>
  11. #ifdef CONFIG_BLOCK_SUPPORT_STLOG
  12. #include <linux/stlog.h>
  13. #else
  14. #define ST_LOG(fmt,...)
  15. #endif
  16. static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
  17. {
  18. struct block_device *bdevp;
  19. struct gendisk *disk;
  20. struct hd_struct *part, *lpart;
  21. struct blkpg_ioctl_arg a;
  22. struct blkpg_partition p;
  23. struct disk_part_iter piter;
  24. long long start, length;
  25. int partno;
  26. if (!capable(CAP_SYS_ADMIN))
  27. return -EACCES;
  28. if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
  29. return -EFAULT;
  30. if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
  31. return -EFAULT;
  32. disk = bdev->bd_disk;
  33. if (bdev != bdev->bd_contains)
  34. return -EINVAL;
  35. partno = p.pno;
  36. if (partno <= 0)
  37. return -EINVAL;
  38. switch (a.op) {
  39. case BLKPG_ADD_PARTITION:
  40. start = p.start >> 9;
  41. length = p.length >> 9;
  42. /* check for fit in a hd_struct */
  43. if (sizeof(sector_t) == sizeof(long) &&
  44. sizeof(long long) > sizeof(long)) {
  45. long pstart = start, plength = length;
  46. if (pstart != start || plength != length
  47. || pstart < 0 || plength < 0 || partno > 65535)
  48. return -EINVAL;
  49. }
  50. mutex_lock(&bdev->bd_mutex);
  51. /* overlap? */
  52. disk_part_iter_init(&piter, disk,
  53. DISK_PITER_INCL_EMPTY);
  54. while ((part = disk_part_iter_next(&piter))) {
  55. if (!(start + length <= part->start_sect ||
  56. start >= part->start_sect + part->nr_sects)) {
  57. disk_part_iter_exit(&piter);
  58. mutex_unlock(&bdev->bd_mutex);
  59. return -EBUSY;
  60. }
  61. }
  62. disk_part_iter_exit(&piter);
  63. /* all seems OK */
  64. part = add_partition(disk, partno, start, length,
  65. ADDPART_FLAG_NONE, NULL);
  66. mutex_unlock(&bdev->bd_mutex);
  67. return IS_ERR(part) ? PTR_ERR(part) : 0;
  68. case BLKPG_DEL_PARTITION:
  69. part = disk_get_part(disk, partno);
  70. if (!part)
  71. return -ENXIO;
  72. bdevp = bdget(part_devt(part));
  73. disk_put_part(part);
  74. if (!bdevp)
  75. return -ENOMEM;
  76. mutex_lock(&bdevp->bd_mutex);
  77. if (bdevp->bd_openers) {
  78. mutex_unlock(&bdevp->bd_mutex);
  79. bdput(bdevp);
  80. return -EBUSY;
  81. }
  82. /* all seems OK */
  83. fsync_bdev(bdevp);
  84. invalidate_bdev(bdevp);
  85. mutex_lock_nested(&bdev->bd_mutex, 1);
  86. delete_partition(disk, partno);
  87. mutex_unlock(&bdev->bd_mutex);
  88. mutex_unlock(&bdevp->bd_mutex);
  89. bdput(bdevp);
  90. return 0;
  91. case BLKPG_RESIZE_PARTITION:
  92. start = p.start >> 9;
  93. /* new length of partition in bytes */
  94. length = p.length >> 9;
  95. /* check for fit in a hd_struct */
  96. if (sizeof(sector_t) == sizeof(long) &&
  97. sizeof(long long) > sizeof(long)) {
  98. long pstart = start, plength = length;
  99. if (pstart != start || plength != length
  100. || pstart < 0 || plength < 0)
  101. return -EINVAL;
  102. }
  103. part = disk_get_part(disk, partno);
  104. if (!part)
  105. return -ENXIO;
  106. bdevp = bdget(part_devt(part));
  107. if (!bdevp) {
  108. disk_put_part(part);
  109. return -ENOMEM;
  110. }
  111. mutex_lock(&bdevp->bd_mutex);
  112. mutex_lock_nested(&bdev->bd_mutex, 1);
  113. if (start != part->start_sect) {
  114. mutex_unlock(&bdevp->bd_mutex);
  115. mutex_unlock(&bdev->bd_mutex);
  116. bdput(bdevp);
  117. disk_put_part(part);
  118. return -EINVAL;
  119. }
  120. /* overlap? */
  121. disk_part_iter_init(&piter, disk,
  122. DISK_PITER_INCL_EMPTY);
  123. while ((lpart = disk_part_iter_next(&piter))) {
  124. if (lpart->partno != partno &&
  125. !(start + length <= lpart->start_sect ||
  126. start >= lpart->start_sect + lpart->nr_sects)
  127. ) {
  128. disk_part_iter_exit(&piter);
  129. mutex_unlock(&bdevp->bd_mutex);
  130. mutex_unlock(&bdev->bd_mutex);
  131. bdput(bdevp);
  132. disk_put_part(part);
  133. return -EBUSY;
  134. }
  135. }
  136. disk_part_iter_exit(&piter);
  137. part_nr_sects_write(part, (sector_t)length);
  138. i_size_write(bdevp->bd_inode, p.length);
  139. mutex_unlock(&bdevp->bd_mutex);
  140. mutex_unlock(&bdev->bd_mutex);
  141. bdput(bdevp);
  142. disk_put_part(part);
  143. return 0;
  144. default:
  145. return -EINVAL;
  146. }
  147. }
  148. static int blkdev_reread_part(struct block_device *bdev)
  149. {
  150. struct gendisk *disk = bdev->bd_disk;
  151. int res;
  152. if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
  153. return -EINVAL;
  154. if (!capable(CAP_SYS_ADMIN))
  155. return -EACCES;
  156. if (!mutex_trylock(&bdev->bd_mutex))
  157. return -EBUSY;
  158. res = rescan_partitions(disk, bdev);
  159. mutex_unlock(&bdev->bd_mutex);
  160. return res;
  161. }
  162. static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
  163. uint64_t len, int secure)
  164. {
  165. unsigned long flags = 0;
  166. if (start & 511)
  167. return -EINVAL;
  168. if (len & 511)
  169. return -EINVAL;
  170. start >>= 9;
  171. len >>= 9;
  172. if (start + len > (i_size_read(bdev->bd_inode) >> 9))
  173. return -EINVAL;
  174. if (secure)
  175. flags |= BLKDEV_DISCARD_SECURE;
  176. ST_LOG("%s %d:%d %lu %lu",secure?"SECDIS":"DIS",MAJOR(bdev->bd_dev),MINOR(bdev->bd_dev),start,len);
  177. return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
  178. }
  179. static int blk_ioctl_sanitize(struct block_device *bdev)
  180. {
  181. return blkdev_issue_sanitize(bdev, GFP_KERNEL);
  182. }
  183. static int blk_ioctl_zeroout(struct block_device *bdev, uint64_t start,
  184. uint64_t len)
  185. {
  186. if (start & 511)
  187. return -EINVAL;
  188. if (len & 511)
  189. return -EINVAL;
  190. start >>= 9;
  191. len >>= 9;
  192. if (start + len > (i_size_read(bdev->bd_inode) >> 9))
  193. return -EINVAL;
  194. return blkdev_issue_zeroout(bdev, start, len, GFP_KERNEL);
  195. }
  196. static int put_ushort(unsigned long arg, unsigned short val)
  197. {
  198. return put_user(val, (unsigned short __user *)arg);
  199. }
  200. static int put_int(unsigned long arg, int val)
  201. {
  202. return put_user(val, (int __user *)arg);
  203. }
  204. static int put_uint(unsigned long arg, unsigned int val)
  205. {
  206. return put_user(val, (unsigned int __user *)arg);
  207. }
  208. static int put_long(unsigned long arg, long val)
  209. {
  210. return put_user(val, (long __user *)arg);
  211. }
  212. static int put_ulong(unsigned long arg, unsigned long val)
  213. {
  214. return put_user(val, (unsigned long __user *)arg);
  215. }
  216. static int put_u64(unsigned long arg, u64 val)
  217. {
  218. return put_user(val, (u64 __user *)arg);
  219. }
  220. int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
  221. unsigned cmd, unsigned long arg)
  222. {
  223. struct gendisk *disk = bdev->bd_disk;
  224. if (disk->fops->ioctl)
  225. return disk->fops->ioctl(bdev, mode, cmd, arg);
  226. return -ENOTTY;
  227. }
  228. /*
  229. * For the record: _GPL here is only because somebody decided to slap it
  230. * on the previous export. Sheer idiocy, since it wasn't copyrightable
  231. * at all and could be open-coded without any exports by anybody who cares.
  232. */
  233. EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
  234. /*
  235. * Is it an unrecognized ioctl? The correct returns are either
  236. * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
  237. * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
  238. * code before returning.
  239. *
  240. * Confused drivers sometimes return EINVAL, which is wrong. It
  241. * means "I understood the ioctl command, but the parameters to
  242. * it were wrong".
  243. *
  244. * We should aim to just fix the broken drivers, the EINVAL case
  245. * should go away.
  246. */
  247. static inline int is_unrecognized_ioctl(int ret)
  248. {
  249. return ret == -EINVAL ||
  250. ret == -ENOTTY ||
  251. ret == -ENOIOCTLCMD;
  252. }
  253. /*
  254. * always keep this in sync with compat_blkdev_ioctl()
  255. */
  256. int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
  257. unsigned long arg)
  258. {
  259. struct gendisk *disk = bdev->bd_disk;
  260. struct backing_dev_info *bdi;
  261. loff_t size;
  262. int ret, n;
  263. switch(cmd) {
  264. case BLKFLSBUF:
  265. if (!capable(CAP_SYS_ADMIN))
  266. return -EACCES;
  267. ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  268. if (!is_unrecognized_ioctl(ret))
  269. return ret;
  270. fsync_bdev(bdev);
  271. invalidate_bdev(bdev);
  272. return 0;
  273. case BLKROSET:
  274. ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  275. if (!is_unrecognized_ioctl(ret))
  276. return ret;
  277. if (!capable(CAP_SYS_ADMIN))
  278. return -EACCES;
  279. if (get_user(n, (int __user *)(arg)))
  280. return -EFAULT;
  281. set_device_ro(bdev, n);
  282. return 0;
  283. case BLKSANITIZE:
  284. ret = blk_ioctl_sanitize(bdev);
  285. break;
  286. case BLKDISCARD:
  287. case BLKSECDISCARD: {
  288. uint64_t range[2];
  289. if (!(mode & FMODE_WRITE))
  290. return -EBADF;
  291. if (copy_from_user(range, (void __user *)arg, sizeof(range)))
  292. return -EFAULT;
  293. return blk_ioctl_discard(bdev, range[0], range[1],
  294. cmd == BLKSECDISCARD);
  295. }
  296. case BLKZEROOUT: {
  297. uint64_t range[2];
  298. if (!(mode & FMODE_WRITE))
  299. return -EBADF;
  300. if (copy_from_user(range, (void __user *)arg, sizeof(range)))
  301. return -EFAULT;
  302. return blk_ioctl_zeroout(bdev, range[0], range[1]);
  303. }
  304. case HDIO_GETGEO: {
  305. struct hd_geometry geo;
  306. if (!arg)
  307. return -EINVAL;
  308. if (!disk->fops->getgeo)
  309. return -ENOTTY;
  310. /*
  311. * We need to set the startsect first, the driver may
  312. * want to override it.
  313. */
  314. memset(&geo, 0, sizeof(geo));
  315. geo.start = get_start_sect(bdev);
  316. ret = disk->fops->getgeo(bdev, &geo);
  317. if (ret)
  318. return ret;
  319. if (copy_to_user((struct hd_geometry __user *)arg, &geo,
  320. sizeof(geo)))
  321. return -EFAULT;
  322. return 0;
  323. }
  324. case BLKRAGET:
  325. case BLKFRAGET:
  326. if (!arg)
  327. return -EINVAL;
  328. bdi = blk_get_backing_dev_info(bdev);
  329. if (bdi == NULL)
  330. return -ENOTTY;
  331. return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
  332. case BLKROGET:
  333. return put_int(arg, bdev_read_only(bdev) != 0);
  334. case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
  335. return put_int(arg, block_size(bdev));
  336. case BLKSSZGET: /* get block device logical block size */
  337. return put_int(arg, bdev_logical_block_size(bdev));
  338. case BLKPBSZGET: /* get block device physical block size */
  339. return put_uint(arg, bdev_physical_block_size(bdev));
  340. case BLKIOMIN:
  341. return put_uint(arg, bdev_io_min(bdev));
  342. case BLKIOOPT:
  343. return put_uint(arg, bdev_io_opt(bdev));
  344. case BLKALIGNOFF:
  345. return put_int(arg, bdev_alignment_offset(bdev));
  346. case BLKDISCARDZEROES:
  347. return put_uint(arg, bdev_discard_zeroes_data(bdev));
  348. case BLKSECTGET:
  349. return put_ushort(arg, queue_max_sectors(bdev_get_queue(bdev)));
  350. case BLKROTATIONAL:
  351. return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
  352. case BLKRASET:
  353. case BLKFRASET:
  354. if(!capable(CAP_SYS_ADMIN))
  355. return -EACCES;
  356. bdi = blk_get_backing_dev_info(bdev);
  357. if (bdi == NULL)
  358. return -ENOTTY;
  359. bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
  360. return 0;
  361. case BLKBSZSET:
  362. /* set the logical block size */
  363. if (!capable(CAP_SYS_ADMIN))
  364. return -EACCES;
  365. if (!arg)
  366. return -EINVAL;
  367. if (get_user(n, (int __user *) arg))
  368. return -EFAULT;
  369. if (!(mode & FMODE_EXCL)) {
  370. bdgrab(bdev);
  371. if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
  372. return -EBUSY;
  373. }
  374. ret = set_blocksize(bdev, n);
  375. if (!(mode & FMODE_EXCL))
  376. blkdev_put(bdev, mode | FMODE_EXCL);
  377. return ret;
  378. case BLKPG:
  379. ret = blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
  380. break;
  381. case BLKRRPART:
  382. ret = blkdev_reread_part(bdev);
  383. break;
  384. case BLKGETSIZE:
  385. size = i_size_read(bdev->bd_inode);
  386. if ((size >> 9) > ~0UL)
  387. return -EFBIG;
  388. return put_ulong(arg, size >> 9);
  389. case BLKGETSIZE64:
  390. return put_u64(arg, i_size_read(bdev->bd_inode));
  391. case BLKTRACESTART:
  392. case BLKTRACESTOP:
  393. case BLKTRACESETUP:
  394. case BLKTRACETEARDOWN:
  395. ret = blk_trace_ioctl(bdev, cmd, (char __user *) arg);
  396. break;
  397. default:
  398. ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  399. }
  400. return ret;
  401. }
  402. EXPORT_SYMBOL_GPL(blkdev_ioctl);