1 #include <linux/capability.h>
2 #include <linux/blkdev.h>
3 #include <linux/export.h>
5 #include <linux/blkpg.h>
6 #include <linux/hdreg.h>
7 #include <linux/backing-dev.h>
9 #include <linux/blktrace_api.h>
11 #include <linux/uaccess.h>
13 static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
15 struct block_device *bdevp;
17 struct hd_struct *part, *lpart;
18 struct blkpg_ioctl_arg a;
19 struct blkpg_partition p;
20 struct disk_part_iter piter;
21 long long start, length;
24 if (!capable(CAP_SYS_ADMIN))
26 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
28 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
31 if (bdev != bdev->bd_contains)
37 case BLKPG_ADD_PARTITION:
39 length = p.length >> 9;
40 /* check for fit in a hd_struct */
41 if (sizeof(sector_t) == sizeof(long) &&
42 sizeof(long long) > sizeof(long)) {
43 long pstart = start, plength = length;
44 if (pstart != start || plength != length
45 || pstart < 0 || plength < 0 || partno > 65535)
48 /* check if partition is aligned to blocksize */
49 if (p.start & (bdev_logical_block_size(bdev) - 1))
52 mutex_lock(&bdev->bd_mutex);
55 disk_part_iter_init(&piter, disk,
56 DISK_PITER_INCL_EMPTY);
57 while ((part = disk_part_iter_next(&piter))) {
58 if (!(start + length <= part->start_sect ||
59 start >= part->start_sect + part->nr_sects)) {
60 disk_part_iter_exit(&piter);
61 mutex_unlock(&bdev->bd_mutex);
65 disk_part_iter_exit(&piter);
68 part = add_partition(disk, partno, start, length,
69 ADDPART_FLAG_NONE, NULL);
70 mutex_unlock(&bdev->bd_mutex);
71 return PTR_ERR_OR_ZERO(part);
72 case BLKPG_DEL_PARTITION:
73 part = disk_get_part(disk, partno);
77 bdevp = bdget(part_devt(part));
82 mutex_lock(&bdevp->bd_mutex);
83 if (bdevp->bd_openers) {
84 mutex_unlock(&bdevp->bd_mutex);
90 invalidate_bdev(bdevp);
92 mutex_lock_nested(&bdev->bd_mutex, 1);
93 delete_partition(disk, partno);
94 mutex_unlock(&bdev->bd_mutex);
95 mutex_unlock(&bdevp->bd_mutex);
99 case BLKPG_RESIZE_PARTITION:
100 start = p.start >> 9;
101 /* new length of partition in bytes */
102 length = p.length >> 9;
103 /* check for fit in a hd_struct */
104 if (sizeof(sector_t) == sizeof(long) &&
105 sizeof(long long) > sizeof(long)) {
106 long pstart = start, plength = length;
107 if (pstart != start || plength != length
108 || pstart < 0 || plength < 0)
111 part = disk_get_part(disk, partno);
114 bdevp = bdget(part_devt(part));
119 mutex_lock(&bdevp->bd_mutex);
120 mutex_lock_nested(&bdev->bd_mutex, 1);
121 if (start != part->start_sect) {
122 mutex_unlock(&bdevp->bd_mutex);
123 mutex_unlock(&bdev->bd_mutex);
129 disk_part_iter_init(&piter, disk,
130 DISK_PITER_INCL_EMPTY);
131 while ((lpart = disk_part_iter_next(&piter))) {
132 if (lpart->partno != partno &&
133 !(start + length <= lpart->start_sect ||
134 start >= lpart->start_sect + lpart->nr_sects)
136 disk_part_iter_exit(&piter);
137 mutex_unlock(&bdevp->bd_mutex);
138 mutex_unlock(&bdev->bd_mutex);
144 disk_part_iter_exit(&piter);
145 part_nr_sects_write(part, (sector_t)length);
146 i_size_write(bdevp->bd_inode, p.length);
147 mutex_unlock(&bdevp->bd_mutex);
148 mutex_unlock(&bdev->bd_mutex);
158 * This is an exported API for the block driver, and will not
159 * acquire bd_mutex. This API should be used in case that
160 * caller has held bd_mutex already.
162 int __blkdev_reread_part(struct block_device *bdev)
164 struct gendisk *disk = bdev->bd_disk;
166 if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
168 if (!capable(CAP_SYS_ADMIN))
171 lockdep_assert_held(&bdev->bd_mutex);
173 return rescan_partitions(disk, bdev);
175 EXPORT_SYMBOL(__blkdev_reread_part);
178 * This is an exported API for the block driver, and will
179 * try to acquire bd_mutex. If bd_mutex has been held already
180 * in current context, please call __blkdev_reread_part().
182 * Make sure the held locks in current context aren't required
183 * in open()/close() handler and I/O path for avoiding ABBA deadlock:
184 * - bd_mutex is held before calling block driver's open/close
186 * - reading partition table may submit I/O to the block device
188 int blkdev_reread_part(struct block_device *bdev)
192 mutex_lock(&bdev->bd_mutex);
193 res = __blkdev_reread_part(bdev);
194 mutex_unlock(&bdev->bd_mutex);
198 EXPORT_SYMBOL(blkdev_reread_part);
200 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
201 unsigned long arg, unsigned long flags)
205 struct request_queue *q = bdev_get_queue(bdev);
206 struct address_space *mapping = bdev->bd_inode->i_mapping;
209 if (!(mode & FMODE_WRITE))
212 if (!blk_queue_discard(q))
215 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
226 if (start + len > i_size_read(bdev->bd_inode))
228 truncate_inode_pages_range(mapping, start, start + len);
229 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
233 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
237 struct address_space *mapping;
238 uint64_t start, end, len;
240 if (!(mode & FMODE_WRITE))
243 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
248 end = start + len - 1;
254 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
259 /* Invalidate the page cache, including dirty pages */
260 mapping = bdev->bd_inode->i_mapping;
261 truncate_inode_pages_range(mapping, start, end);
263 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
264 BLKDEV_ZERO_NOUNMAP);
267 static int put_ushort(unsigned long arg, unsigned short val)
269 return put_user(val, (unsigned short __user *)arg);
272 static int put_int(unsigned long arg, int val)
274 return put_user(val, (int __user *)arg);
277 static int put_uint(unsigned long arg, unsigned int val)
279 return put_user(val, (unsigned int __user *)arg);
282 static int put_long(unsigned long arg, long val)
284 return put_user(val, (long __user *)arg);
287 static int put_ulong(unsigned long arg, unsigned long val)
289 return put_user(val, (unsigned long __user *)arg);
292 static int put_u64(unsigned long arg, u64 val)
294 return put_user(val, (u64 __user *)arg);
297 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
298 unsigned cmd, unsigned long arg)
300 struct gendisk *disk = bdev->bd_disk;
302 if (disk->fops->ioctl)
303 return disk->fops->ioctl(bdev, mode, cmd, arg);
308 * For the record: _GPL here is only because somebody decided to slap it
309 * on the previous export. Sheer idiocy, since it wasn't copyrightable
310 * at all and could be open-coded without any exports by anybody who cares.
312 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
314 static int blkdev_pr_register(struct block_device *bdev,
315 struct pr_registration __user *arg)
317 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
318 struct pr_registration reg;
320 if (!capable(CAP_SYS_ADMIN))
322 if (!ops || !ops->pr_register)
324 if (copy_from_user(®, arg, sizeof(reg)))
327 if (reg.flags & ~PR_FL_IGNORE_KEY)
329 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
332 static int blkdev_pr_reserve(struct block_device *bdev,
333 struct pr_reservation __user *arg)
335 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
336 struct pr_reservation rsv;
338 if (!capable(CAP_SYS_ADMIN))
340 if (!ops || !ops->pr_reserve)
342 if (copy_from_user(&rsv, arg, sizeof(rsv)))
345 if (rsv.flags & ~PR_FL_IGNORE_KEY)
347 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
350 static int blkdev_pr_release(struct block_device *bdev,
351 struct pr_reservation __user *arg)
353 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
354 struct pr_reservation rsv;
356 if (!capable(CAP_SYS_ADMIN))
358 if (!ops || !ops->pr_release)
360 if (copy_from_user(&rsv, arg, sizeof(rsv)))
365 return ops->pr_release(bdev, rsv.key, rsv.type);
368 static int blkdev_pr_preempt(struct block_device *bdev,
369 struct pr_preempt __user *arg, bool abort)
371 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
374 if (!capable(CAP_SYS_ADMIN))
376 if (!ops || !ops->pr_preempt)
378 if (copy_from_user(&p, arg, sizeof(p)))
383 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
386 static int blkdev_pr_clear(struct block_device *bdev,
387 struct pr_clear __user *arg)
389 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
392 if (!capable(CAP_SYS_ADMIN))
394 if (!ops || !ops->pr_clear)
396 if (copy_from_user(&c, arg, sizeof(c)))
401 return ops->pr_clear(bdev, c.key);
405 * Is it an unrecognized ioctl? The correct returns are either
406 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
407 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
408 * code before returning.
410 * Confused drivers sometimes return EINVAL, which is wrong. It
411 * means "I understood the ioctl command, but the parameters to
414 * We should aim to just fix the broken drivers, the EINVAL case
417 static inline int is_unrecognized_ioctl(int ret)
419 return ret == -EINVAL ||
424 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
425 unsigned cmd, unsigned long arg)
429 if (!capable(CAP_SYS_ADMIN))
432 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
433 if (!is_unrecognized_ioctl(ret))
437 invalidate_bdev(bdev);
441 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
442 unsigned cmd, unsigned long arg)
446 if (!capable(CAP_SYS_ADMIN))
449 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
450 if (!is_unrecognized_ioctl(ret))
452 if (get_user(n, (int __user *)arg))
454 set_device_ro(bdev, n);
458 static int blkdev_getgeo(struct block_device *bdev,
459 struct hd_geometry __user *argp)
461 struct gendisk *disk = bdev->bd_disk;
462 struct hd_geometry geo;
467 if (!disk->fops->getgeo)
471 * We need to set the startsect first, the driver may
472 * want to override it.
474 memset(&geo, 0, sizeof(geo));
475 geo.start = get_start_sect(bdev);
476 ret = disk->fops->getgeo(bdev, &geo);
479 if (copy_to_user(argp, &geo, sizeof(geo)))
484 /* set the logical block size */
485 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
490 if (!capable(CAP_SYS_ADMIN))
494 if (get_user(n, argp))
497 if (!(mode & FMODE_EXCL)) {
499 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
503 ret = set_blocksize(bdev, n);
504 if (!(mode & FMODE_EXCL))
505 blkdev_put(bdev, mode | FMODE_EXCL);
510 * always keep this in sync with compat_blkdev_ioctl()
512 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
515 void __user *argp = (void __user *)arg;
517 unsigned int max_sectors;
521 return blkdev_flushbuf(bdev, mode, cmd, arg);
523 return blkdev_roset(bdev, mode, cmd, arg);
525 return blk_ioctl_discard(bdev, mode, arg, 0);
527 return blk_ioctl_discard(bdev, mode, arg,
528 BLKDEV_DISCARD_SECURE);
530 return blk_ioctl_zeroout(bdev, mode, arg);
532 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
534 return blkdev_reset_zones_ioctl(bdev, mode, cmd, arg);
536 return blkdev_getgeo(bdev, argp);
541 return put_long(arg, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
543 return put_int(arg, bdev_read_only(bdev) != 0);
544 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
545 return put_int(arg, block_size(bdev));
546 case BLKSSZGET: /* get block device logical block size */
547 return put_int(arg, bdev_logical_block_size(bdev));
548 case BLKPBSZGET: /* get block device physical block size */
549 return put_uint(arg, bdev_physical_block_size(bdev));
551 return put_uint(arg, bdev_io_min(bdev));
553 return put_uint(arg, bdev_io_opt(bdev));
555 return put_int(arg, bdev_alignment_offset(bdev));
556 case BLKDISCARDZEROES:
557 return put_uint(arg, 0);
559 max_sectors = min_t(unsigned int, USHRT_MAX,
560 queue_max_sectors(bdev_get_queue(bdev)));
561 return put_ushort(arg, max_sectors);
563 return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
566 if(!capable(CAP_SYS_ADMIN))
568 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
571 return blkdev_bszset(bdev, mode, argp);
573 return blkpg_ioctl(bdev, argp);
575 return blkdev_reread_part(bdev);
577 size = i_size_read(bdev->bd_inode);
578 if ((size >> 9) > ~0UL)
580 return put_ulong(arg, size >> 9);
582 return put_u64(arg, i_size_read(bdev->bd_inode));
586 case BLKTRACETEARDOWN:
587 return blk_trace_ioctl(bdev, cmd, argp);
588 case IOC_PR_REGISTER:
589 return blkdev_pr_register(bdev, argp);
591 return blkdev_pr_reserve(bdev, argp);
593 return blkdev_pr_release(bdev, argp);
595 return blkdev_pr_preempt(bdev, argp, false);
596 case IOC_PR_PREEMPT_ABORT:
597 return blkdev_pr_preempt(bdev, argp, true);
599 return blkdev_pr_clear(bdev, argp);
601 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
604 EXPORT_SYMBOL_GPL(blkdev_ioctl);