ioctl.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include <linux/capability.h>
  2. #include <linux/blkdev.h>
  3. #include <linux/gfp.h>
  4. #include <linux/blkpg.h>
  5. #include <linux/hdreg.h>
  6. #include <linux/backing-dev.h>
  7. #include <linux/buffer_head.h>
  8. #include <linux/blktrace_api.h>
  9. #include <asm/uaccess.h>
  10. static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
  11. {
  12. struct block_device *bdevp;
  13. struct gendisk *disk;
  14. struct hd_struct *part;
  15. struct blkpg_ioctl_arg a;
  16. struct blkpg_partition p;
  17. struct disk_part_iter piter;
  18. long long start, length;
  19. int partno;
  20. if (!capable(CAP_SYS_ADMIN))
  21. return -EACCES;
  22. if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
  23. return -EFAULT;
  24. if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
  25. return -EFAULT;
  26. disk = bdev->bd_disk;
  27. if (bdev != bdev->bd_contains)
  28. return -EINVAL;
  29. partno = p.pno;
  30. if (partno <= 0)
  31. return -EINVAL;
  32. switch (a.op) {
  33. case BLKPG_ADD_PARTITION:
  34. start = p.start >> 9;
  35. length = p.length >> 9;
  36. /* check for fit in a hd_struct */
  37. if (sizeof(sector_t) == sizeof(long) &&
  38. sizeof(long long) > sizeof(long)) {
  39. long pstart = start, plength = length;
  40. if (pstart != start || plength != length
  41. || pstart < 0 || plength < 0)
  42. return -EINVAL;
  43. }
  44. mutex_lock(&bdev->bd_mutex);
  45. /* overlap? */
  46. disk_part_iter_init(&piter, disk,
  47. DISK_PITER_INCL_EMPTY);
  48. while ((part = disk_part_iter_next(&piter))) {
  49. if (!(start + length <= part->start_sect ||
  50. start >= part->start_sect + part->nr_sects)) {
  51. disk_part_iter_exit(&piter);
  52. mutex_unlock(&bdev->bd_mutex);
  53. return -EBUSY;
  54. }
  55. }
  56. disk_part_iter_exit(&piter);
  57. /* all seems OK */
  58. part = add_partition(disk, partno, start, length,
  59. ADDPART_FLAG_NONE, NULL);
  60. mutex_unlock(&bdev->bd_mutex);
  61. return IS_ERR(part) ? PTR_ERR(part) : 0;
  62. case BLKPG_DEL_PARTITION:
  63. part = disk_get_part(disk, partno);
  64. if (!part)
  65. return -ENXIO;
  66. bdevp = bdget(part_devt(part));
  67. disk_put_part(part);
  68. if (!bdevp)
  69. return -ENOMEM;
  70. mutex_lock(&bdevp->bd_mutex);
  71. if (bdevp->bd_openers) {
  72. mutex_unlock(&bdevp->bd_mutex);
  73. bdput(bdevp);
  74. return -EBUSY;
  75. }
  76. /* all seems OK */
  77. fsync_bdev(bdevp);
  78. invalidate_bdev(bdevp);
  79. mutex_lock_nested(&bdev->bd_mutex, 1);
  80. delete_partition(disk, partno);
  81. mutex_unlock(&bdev->bd_mutex);
  82. mutex_unlock(&bdevp->bd_mutex);
  83. bdput(bdevp);
  84. return 0;
  85. default:
  86. return -EINVAL;
  87. }
  88. }
  89. static int blkdev_reread_part(struct block_device *bdev)
  90. {
  91. struct gendisk *disk = bdev->bd_disk;
  92. int res;
  93. if (!disk_partitionable(disk) || bdev != bdev->bd_contains)
  94. return -EINVAL;
  95. if (!capable(CAP_SYS_ADMIN))
  96. return -EACCES;
  97. if (!mutex_trylock(&bdev->bd_mutex))
  98. return -EBUSY;
  99. res = rescan_partitions(disk, bdev);
  100. mutex_unlock(&bdev->bd_mutex);
  101. return res;
  102. }
  103. static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
  104. uint64_t len, int secure)
  105. {
  106. unsigned long flags = 0;
  107. if (start & 511)
  108. return -EINVAL;
  109. if (len & 511)
  110. return -EINVAL;
  111. start >>= 9;
  112. len >>= 9;
  113. if (start + len > (i_size_read(bdev->bd_inode) >> 9))
  114. return -EINVAL;
  115. if (secure)
  116. flags |= BLKDEV_DISCARD_SECURE;
  117. return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
  118. }
  119. static int put_ushort(unsigned long arg, unsigned short val)
  120. {
  121. return put_user(val, (unsigned short __user *)arg);
  122. }
  123. static int put_int(unsigned long arg, int val)
  124. {
  125. return put_user(val, (int __user *)arg);
  126. }
  127. static int put_uint(unsigned long arg, unsigned int val)
  128. {
  129. return put_user(val, (unsigned int __user *)arg);
  130. }
  131. static int put_long(unsigned long arg, long val)
  132. {
  133. return put_user(val, (long __user *)arg);
  134. }
  135. static int put_ulong(unsigned long arg, unsigned long val)
  136. {
  137. return put_user(val, (unsigned long __user *)arg);
  138. }
  139. static int put_u64(unsigned long arg, u64 val)
  140. {
  141. return put_user(val, (u64 __user *)arg);
  142. }
  143. int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
  144. unsigned cmd, unsigned long arg)
  145. {
  146. struct gendisk *disk = bdev->bd_disk;
  147. if (disk->fops->ioctl)
  148. return disk->fops->ioctl(bdev, mode, cmd, arg);
  149. return -ENOTTY;
  150. }
  151. /*
  152. * For the record: _GPL here is only because somebody decided to slap it
  153. * on the previous export. Sheer idiocy, since it wasn't copyrightable
  154. * at all and could be open-coded without any exports by anybody who cares.
  155. */
  156. EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
  157. /*
  158. * always keep this in sync with compat_blkdev_ioctl()
  159. */
  160. int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
  161. unsigned long arg)
  162. {
  163. struct gendisk *disk = bdev->bd_disk;
  164. struct backing_dev_info *bdi;
  165. loff_t size;
  166. int ret, n;
  167. switch(cmd) {
  168. case BLKFLSBUF:
  169. if (!capable(CAP_SYS_ADMIN))
  170. return -EACCES;
  171. ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  172. /* -EINVAL to handle old uncorrected drivers */
  173. if (ret != -EINVAL && ret != -ENOTTY)
  174. return ret;
  175. fsync_bdev(bdev);
  176. invalidate_bdev(bdev);
  177. return 0;
  178. case BLKROSET:
  179. ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  180. /* -EINVAL to handle old uncorrected drivers */
  181. if (ret != -EINVAL && ret != -ENOTTY)
  182. return ret;
  183. if (!capable(CAP_SYS_ADMIN))
  184. return -EACCES;
  185. if (get_user(n, (int __user *)(arg)))
  186. return -EFAULT;
  187. set_device_ro(bdev, n);
  188. return 0;
  189. case BLKDISCARD:
  190. case BLKSECDISCARD: {
  191. uint64_t range[2];
  192. if (!(mode & FMODE_WRITE))
  193. return -EBADF;
  194. if (copy_from_user(range, (void __user *)arg, sizeof(range)))
  195. return -EFAULT;
  196. return blk_ioctl_discard(bdev, range[0], range[1],
  197. cmd == BLKSECDISCARD);
  198. }
  199. case HDIO_GETGEO: {
  200. struct hd_geometry geo;
  201. if (!arg)
  202. return -EINVAL;
  203. if (!disk->fops->getgeo)
  204. return -ENOTTY;
  205. /*
  206. * We need to set the startsect first, the driver may
  207. * want to override it.
  208. */
  209. memset(&geo, 0, sizeof(geo));
  210. geo.start = get_start_sect(bdev);
  211. ret = disk->fops->getgeo(bdev, &geo);
  212. if (ret)
  213. return ret;
  214. if (copy_to_user((struct hd_geometry __user *)arg, &geo,
  215. sizeof(geo)))
  216. return -EFAULT;
  217. return 0;
  218. }
  219. case BLKRAGET:
  220. case BLKFRAGET:
  221. if (!arg)
  222. return -EINVAL;
  223. bdi = blk_get_backing_dev_info(bdev);
  224. if (bdi == NULL)
  225. return -ENOTTY;
  226. return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
  227. case BLKROGET:
  228. return put_int(arg, bdev_read_only(bdev) != 0);
  229. case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
  230. return put_int(arg, block_size(bdev));
  231. case BLKSSZGET: /* get block device logical block size */
  232. return put_int(arg, bdev_logical_block_size(bdev));
  233. case BLKPBSZGET: /* get block device physical block size */
  234. return put_uint(arg, bdev_physical_block_size(bdev));
  235. case BLKIOMIN:
  236. return put_uint(arg, bdev_io_min(bdev));
  237. case BLKIOOPT:
  238. return put_uint(arg, bdev_io_opt(bdev));
  239. case BLKALIGNOFF:
  240. return put_int(arg, bdev_alignment_offset(bdev));
  241. case BLKDISCARDZEROES:
  242. return put_uint(arg, bdev_discard_zeroes_data(bdev));
  243. case BLKSECTGET:
  244. return put_ushort(arg, queue_max_sectors(bdev_get_queue(bdev)));
  245. case BLKRASET:
  246. case BLKFRASET:
  247. if(!capable(CAP_SYS_ADMIN))
  248. return -EACCES;
  249. bdi = blk_get_backing_dev_info(bdev);
  250. if (bdi == NULL)
  251. return -ENOTTY;
  252. bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
  253. return 0;
  254. case BLKBSZSET:
  255. /* set the logical block size */
  256. if (!capable(CAP_SYS_ADMIN))
  257. return -EACCES;
  258. if (!arg)
  259. return -EINVAL;
  260. if (get_user(n, (int __user *) arg))
  261. return -EFAULT;
  262. if (!(mode & FMODE_EXCL)) {
  263. bdgrab(bdev);
  264. if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
  265. return -EBUSY;
  266. }
  267. ret = set_blocksize(bdev, n);
  268. if (!(mode & FMODE_EXCL))
  269. blkdev_put(bdev, mode | FMODE_EXCL);
  270. return ret;
  271. case BLKPG:
  272. ret = blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
  273. break;
  274. case BLKRRPART:
  275. ret = blkdev_reread_part(bdev);
  276. break;
  277. case BLKGETSIZE:
  278. size = i_size_read(bdev->bd_inode);
  279. if ((size >> 9) > ~0UL)
  280. return -EFBIG;
  281. return put_ulong(arg, size >> 9);
  282. case BLKGETSIZE64:
  283. return put_u64(arg, i_size_read(bdev->bd_inode));
  284. case BLKTRACESTART:
  285. case BLKTRACESTOP:
  286. case BLKTRACESETUP:
  287. case BLKTRACETEARDOWN:
  288. ret = blk_trace_ioctl(bdev, cmd, (char __user *) arg);
  289. break;
  290. default:
  291. ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  292. }
  293. return ret;
  294. }
  295. EXPORT_SYMBOL_GPL(blkdev_ioctl);