ioctl.c 15 KB

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