ioctl.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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;
  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)
  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. default:
  92. return -EINVAL;
  93. }
  94. }
  95. static int blkdev_reread_part(struct block_device *bdev)
  96. {
  97. struct gendisk *disk = bdev->bd_disk;
  98. int res;
  99. if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
  100. return -EINVAL;
  101. if (!capable(CAP_SYS_ADMIN))
  102. return -EACCES;
  103. if (!mutex_trylock(&bdev->bd_mutex))
  104. return -EBUSY;
  105. res = rescan_partitions(disk, bdev);
  106. mutex_unlock(&bdev->bd_mutex);
  107. return res;
  108. }
  109. static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
  110. uint64_t len, int secure)
  111. {
  112. unsigned long flags = 0;
  113. if (start & 511)
  114. return -EINVAL;
  115. if (len & 511)
  116. return -EINVAL;
  117. start >>= 9;
  118. len >>= 9;
  119. if (start + len > (i_size_read(bdev->bd_inode) >> 9))
  120. return -EINVAL;
  121. if (secure)
  122. flags |= BLKDEV_DISCARD_SECURE;
  123. ST_LOG("%s %d:%d %lu %lu",secure?"SECDIS":"DIS",MAJOR(bdev->bd_dev),MINOR(bdev->bd_dev),start,len);
  124. return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
  125. }
  126. static int blk_ioctl_sanitize(struct block_device *bdev)
  127. {
  128. return blkdev_issue_sanitize(bdev, GFP_KERNEL);
  129. }
  130. static int put_ushort(unsigned long arg, unsigned short val)
  131. {
  132. return put_user(val, (unsigned short __user *)arg);
  133. }
  134. static int put_int(unsigned long arg, int val)
  135. {
  136. return put_user(val, (int __user *)arg);
  137. }
  138. static int put_uint(unsigned long arg, unsigned int val)
  139. {
  140. return put_user(val, (unsigned int __user *)arg);
  141. }
  142. static int put_long(unsigned long arg, long val)
  143. {
  144. return put_user(val, (long __user *)arg);
  145. }
  146. static int put_ulong(unsigned long arg, unsigned long val)
  147. {
  148. return put_user(val, (unsigned long __user *)arg);
  149. }
  150. static int put_u64(unsigned long arg, u64 val)
  151. {
  152. return put_user(val, (u64 __user *)arg);
  153. }
  154. int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
  155. unsigned cmd, unsigned long arg)
  156. {
  157. struct gendisk *disk = bdev->bd_disk;
  158. if (disk->fops->ioctl)
  159. return disk->fops->ioctl(bdev, mode, cmd, arg);
  160. return -ENOTTY;
  161. }
  162. /*
  163. * For the record: _GPL here is only because somebody decided to slap it
  164. * on the previous export. Sheer idiocy, since it wasn't copyrightable
  165. * at all and could be open-coded without any exports by anybody who cares.
  166. */
  167. EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
  168. /*
  169. * Is it an unrecognized ioctl? The correct returns are either
  170. * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
  171. * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
  172. * code before returning.
  173. *
  174. * Confused drivers sometimes return EINVAL, which is wrong. It
  175. * means "I understood the ioctl command, but the parameters to
  176. * it were wrong".
  177. *
  178. * We should aim to just fix the broken drivers, the EINVAL case
  179. * should go away.
  180. */
  181. static inline int is_unrecognized_ioctl(int ret)
  182. {
  183. return ret == -EINVAL ||
  184. ret == -ENOTTY ||
  185. ret == -ENOIOCTLCMD;
  186. }
  187. /*
  188. * always keep this in sync with compat_blkdev_ioctl()
  189. */
  190. int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
  191. unsigned long arg)
  192. {
  193. struct gendisk *disk = bdev->bd_disk;
  194. struct backing_dev_info *bdi;
  195. loff_t size;
  196. int ret, n;
  197. switch(cmd) {
  198. case BLKFLSBUF:
  199. if (!capable(CAP_SYS_ADMIN))
  200. return -EACCES;
  201. ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  202. if (!is_unrecognized_ioctl(ret))
  203. return ret;
  204. fsync_bdev(bdev);
  205. invalidate_bdev(bdev);
  206. return 0;
  207. case BLKROSET:
  208. ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  209. if (!is_unrecognized_ioctl(ret))
  210. return ret;
  211. if (!capable(CAP_SYS_ADMIN))
  212. return -EACCES;
  213. if (get_user(n, (int __user *)(arg)))
  214. return -EFAULT;
  215. set_device_ro(bdev, n);
  216. return 0;
  217. case BLKSANITIZE:
  218. ret = blk_ioctl_sanitize(bdev);
  219. break;
  220. case BLKDISCARD:
  221. case BLKSECDISCARD: {
  222. uint64_t range[2];
  223. if (!(mode & FMODE_WRITE))
  224. return -EBADF;
  225. if (copy_from_user(range, (void __user *)arg, sizeof(range)))
  226. return -EFAULT;
  227. return blk_ioctl_discard(bdev, range[0], range[1],
  228. cmd == BLKSECDISCARD);
  229. }
  230. case HDIO_GETGEO: {
  231. struct hd_geometry geo;
  232. if (!arg)
  233. return -EINVAL;
  234. if (!disk->fops->getgeo)
  235. return -ENOTTY;
  236. /*
  237. * We need to set the startsect first, the driver may
  238. * want to override it.
  239. */
  240. memset(&geo, 0, sizeof(geo));
  241. geo.start = get_start_sect(bdev);
  242. ret = disk->fops->getgeo(bdev, &geo);
  243. if (ret)
  244. return ret;
  245. if (copy_to_user((struct hd_geometry __user *)arg, &geo,
  246. sizeof(geo)))
  247. return -EFAULT;
  248. return 0;
  249. }
  250. case BLKRAGET:
  251. case BLKFRAGET:
  252. if (!arg)
  253. return -EINVAL;
  254. bdi = blk_get_backing_dev_info(bdev);
  255. if (bdi == NULL)
  256. return -ENOTTY;
  257. return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
  258. case BLKROGET:
  259. return put_int(arg, bdev_read_only(bdev) != 0);
  260. case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
  261. return put_int(arg, block_size(bdev));
  262. case BLKSSZGET: /* get block device logical block size */
  263. return put_int(arg, bdev_logical_block_size(bdev));
  264. case BLKPBSZGET: /* get block device physical block size */
  265. return put_uint(arg, bdev_physical_block_size(bdev));
  266. case BLKIOMIN:
  267. return put_uint(arg, bdev_io_min(bdev));
  268. case BLKIOOPT:
  269. return put_uint(arg, bdev_io_opt(bdev));
  270. case BLKALIGNOFF:
  271. return put_int(arg, bdev_alignment_offset(bdev));
  272. case BLKDISCARDZEROES:
  273. return put_uint(arg, bdev_discard_zeroes_data(bdev));
  274. case BLKSECTGET:
  275. return put_ushort(arg, queue_max_sectors(bdev_get_queue(bdev)));
  276. case BLKROTATIONAL:
  277. return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
  278. case BLKRASET:
  279. case BLKFRASET:
  280. if(!capable(CAP_SYS_ADMIN))
  281. return -EACCES;
  282. bdi = blk_get_backing_dev_info(bdev);
  283. if (bdi == NULL)
  284. return -ENOTTY;
  285. bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
  286. return 0;
  287. case BLKBSZSET:
  288. /* set the logical block size */
  289. if (!capable(CAP_SYS_ADMIN))
  290. return -EACCES;
  291. if (!arg)
  292. return -EINVAL;
  293. if (get_user(n, (int __user *) arg))
  294. return -EFAULT;
  295. if (!(mode & FMODE_EXCL)) {
  296. bdgrab(bdev);
  297. if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
  298. return -EBUSY;
  299. }
  300. ret = set_blocksize(bdev, n);
  301. if (!(mode & FMODE_EXCL))
  302. blkdev_put(bdev, mode | FMODE_EXCL);
  303. return ret;
  304. case BLKPG:
  305. ret = blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
  306. break;
  307. case BLKRRPART:
  308. ret = blkdev_reread_part(bdev);
  309. break;
  310. case BLKGETSIZE:
  311. size = i_size_read(bdev->bd_inode);
  312. if ((size >> 9) > ~0UL)
  313. return -EFBIG;
  314. return put_ulong(arg, size >> 9);
  315. case BLKGETSIZE64:
  316. return put_u64(arg, i_size_read(bdev->bd_inode));
  317. case BLKTRACESTART:
  318. case BLKTRACESTOP:
  319. case BLKTRACESETUP:
  320. case BLKTRACETEARDOWN:
  321. ret = blk_trace_ioctl(bdev, cmd, (char __user *) arg);
  322. break;
  323. default:
  324. ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  325. }
  326. return ret;
  327. }
  328. EXPORT_SYMBOL_GPL(blkdev_ioctl);