ioctl.c 12 KB

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