ioctl.c 15 KB

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