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 <asm/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)
49 mutex_lock(&bdev->bd_mutex);
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);
62 disk_part_iter_exit(&piter);
65 part = add_partition(disk, partno, start, length,
66 ADDPART_FLAG_NONE, NULL);
67 mutex_unlock(&bdev->bd_mutex);
68 return PTR_ERR_OR_ZERO(part);
69 case BLKPG_DEL_PARTITION:
70 part = disk_get_part(disk, partno);
74 bdevp = bdget(part_devt(part));
79 mutex_lock(&bdevp->bd_mutex);
80 if (bdevp->bd_openers) {
81 mutex_unlock(&bdevp->bd_mutex);
87 invalidate_bdev(bdevp);
89 mutex_lock_nested(&bdev->bd_mutex, 1);
90 delete_partition(disk, partno);
91 mutex_unlock(&bdev->bd_mutex);
92 mutex_unlock(&bdevp->bd_mutex);
96 case BLKPG_RESIZE_PARTITION:
98 /* new length of partition in bytes */
99 length = p.length >> 9;
100 /* check for fit in a hd_struct */
101 if (sizeof(sector_t) == sizeof(long) &&
102 sizeof(long long) > sizeof(long)) {
103 long pstart = start, plength = length;
104 if (pstart != start || plength != length
105 || pstart < 0 || plength < 0)
108 part = disk_get_part(disk, partno);
111 bdevp = bdget(part_devt(part));
116 mutex_lock(&bdevp->bd_mutex);
117 mutex_lock_nested(&bdev->bd_mutex, 1);
118 if (start != part->start_sect) {
119 mutex_unlock(&bdevp->bd_mutex);
120 mutex_unlock(&bdev->bd_mutex);
126 disk_part_iter_init(&piter, disk,
127 DISK_PITER_INCL_EMPTY);
128 while ((lpart = disk_part_iter_next(&piter))) {
129 if (lpart->partno != partno &&
130 !(start + length <= lpart->start_sect ||
131 start >= lpart->start_sect + lpart->nr_sects)
133 disk_part_iter_exit(&piter);
134 mutex_unlock(&bdevp->bd_mutex);
135 mutex_unlock(&bdev->bd_mutex);
141 disk_part_iter_exit(&piter);
142 part_nr_sects_write(part, (sector_t)length);
143 i_size_write(bdevp->bd_inode, p.length);
144 mutex_unlock(&bdevp->bd_mutex);
145 mutex_unlock(&bdev->bd_mutex);
155 * This is an exported API for the block driver, and will not
156 * acquire bd_mutex. This API should be used in case that
157 * caller has held bd_mutex already.
159 int __blkdev_reread_part(struct block_device *bdev)
161 struct gendisk *disk = bdev->bd_disk;
163 if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
165 if (!capable(CAP_SYS_ADMIN))
168 lockdep_assert_held(&bdev->bd_mutex);
170 return rescan_partitions(disk, bdev);
172 EXPORT_SYMBOL(__blkdev_reread_part);
175 * This is an exported API for the block driver, and will
176 * try to acquire bd_mutex. If bd_mutex has been held already
177 * in current context, please call __blkdev_reread_part().
179 * Make sure the held locks in current context aren't required
180 * in open()/close() handler and I/O path for avoiding ABBA deadlock:
181 * - bd_mutex is held before calling block driver's open/close
183 * - reading partition table may submit I/O to the block device
185 int blkdev_reread_part(struct block_device *bdev)
189 mutex_lock(&bdev->bd_mutex);
190 res = __blkdev_reread_part(bdev);
191 mutex_unlock(&bdev->bd_mutex);
195 EXPORT_SYMBOL(blkdev_reread_part);
197 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
198 unsigned long arg, unsigned long flags)
203 if (!(mode & FMODE_WRITE))
206 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
219 if (start + len > (i_size_read(bdev->bd_inode) >> 9))
221 return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
224 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
230 if (!(mode & FMODE_WRITE))
233 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
246 if (start + len > (i_size_read(bdev->bd_inode) >> 9))
249 return blkdev_issue_zeroout(bdev, start, len, GFP_KERNEL, false);
252 static int put_ushort(unsigned long arg, unsigned short val)
254 return put_user(val, (unsigned short __user *)arg);
257 static int put_int(unsigned long arg, int val)
259 return put_user(val, (int __user *)arg);
262 static int put_uint(unsigned long arg, unsigned int val)
264 return put_user(val, (unsigned int __user *)arg);
267 static int put_long(unsigned long arg, long val)
269 return put_user(val, (long __user *)arg);
272 static int put_ulong(unsigned long arg, unsigned long val)
274 return put_user(val, (unsigned long __user *)arg);
277 static int put_u64(unsigned long arg, u64 val)
279 return put_user(val, (u64 __user *)arg);
282 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
283 unsigned cmd, unsigned long arg)
285 struct gendisk *disk = bdev->bd_disk;
287 if (disk->fops->ioctl)
288 return disk->fops->ioctl(bdev, mode, cmd, arg);
293 * For the record: _GPL here is only because somebody decided to slap it
294 * on the previous export. Sheer idiocy, since it wasn't copyrightable
295 * at all and could be open-coded without any exports by anybody who cares.
297 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
299 static int blkdev_pr_register(struct block_device *bdev,
300 struct pr_registration __user *arg)
302 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
303 struct pr_registration reg;
305 if (!capable(CAP_SYS_ADMIN))
307 if (!ops || !ops->pr_register)
309 if (copy_from_user(®, arg, sizeof(reg)))
312 if (reg.flags & ~PR_FL_IGNORE_KEY)
314 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
317 static int blkdev_pr_reserve(struct block_device *bdev,
318 struct pr_reservation __user *arg)
320 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
321 struct pr_reservation rsv;
323 if (!capable(CAP_SYS_ADMIN))
325 if (!ops || !ops->pr_reserve)
327 if (copy_from_user(&rsv, arg, sizeof(rsv)))
330 if (rsv.flags & ~PR_FL_IGNORE_KEY)
332 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
335 static int blkdev_pr_release(struct block_device *bdev,
336 struct pr_reservation __user *arg)
338 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
339 struct pr_reservation rsv;
341 if (!capable(CAP_SYS_ADMIN))
343 if (!ops || !ops->pr_release)
345 if (copy_from_user(&rsv, arg, sizeof(rsv)))
350 return ops->pr_release(bdev, rsv.key, rsv.type);
353 static int blkdev_pr_preempt(struct block_device *bdev,
354 struct pr_preempt __user *arg, bool abort)
356 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
359 if (!capable(CAP_SYS_ADMIN))
361 if (!ops || !ops->pr_preempt)
363 if (copy_from_user(&p, arg, sizeof(p)))
368 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
371 static int blkdev_pr_clear(struct block_device *bdev,
372 struct pr_clear __user *arg)
374 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
377 if (!capable(CAP_SYS_ADMIN))
379 if (!ops || !ops->pr_clear)
381 if (copy_from_user(&c, arg, sizeof(c)))
386 return ops->pr_clear(bdev, c.key);
390 * Is it an unrecognized ioctl? The correct returns are either
391 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
392 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
393 * code before returning.
395 * Confused drivers sometimes return EINVAL, which is wrong. It
396 * means "I understood the ioctl command, but the parameters to
399 * We should aim to just fix the broken drivers, the EINVAL case
402 static inline int is_unrecognized_ioctl(int ret)
404 return ret == -EINVAL ||
409 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
410 unsigned cmd, unsigned long arg)
414 if (!capable(CAP_SYS_ADMIN))
417 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
418 if (!is_unrecognized_ioctl(ret))
422 invalidate_bdev(bdev);
426 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
427 unsigned cmd, unsigned long arg)
431 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
432 if (!is_unrecognized_ioctl(ret))
434 if (!capable(CAP_SYS_ADMIN))
436 if (get_user(n, (int __user *)arg))
438 set_device_ro(bdev, n);
442 static int blkdev_getgeo(struct block_device *bdev,
443 struct hd_geometry __user *argp)
445 struct gendisk *disk = bdev->bd_disk;
446 struct hd_geometry geo;
451 if (!disk->fops->getgeo)
455 * We need to set the startsect first, the driver may
456 * want to override it.
458 memset(&geo, 0, sizeof(geo));
459 geo.start = get_start_sect(bdev);
460 ret = disk->fops->getgeo(bdev, &geo);
463 if (copy_to_user(argp, &geo, sizeof(geo)))
468 /* set the logical block size */
469 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
474 if (!capable(CAP_SYS_ADMIN))
478 if (get_user(n, argp))
481 if (!(mode & FMODE_EXCL)) {
483 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
487 ret = set_blocksize(bdev, n);
488 if (!(mode & FMODE_EXCL))
489 blkdev_put(bdev, mode | FMODE_EXCL);
494 * always keep this in sync with compat_blkdev_ioctl()
496 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
499 struct backing_dev_info *bdi;
500 void __user *argp = (void __user *)arg;
502 unsigned int max_sectors;
506 return blkdev_flushbuf(bdev, mode, cmd, arg);
508 return blkdev_roset(bdev, mode, cmd, arg);
510 return blk_ioctl_discard(bdev, mode, arg, 0);
512 return blk_ioctl_discard(bdev, mode, arg,
513 BLKDEV_DISCARD_SECURE);
515 return blk_ioctl_zeroout(bdev, mode, arg);
517 return blkdev_getgeo(bdev, argp);
522 bdi = blk_get_backing_dev_info(bdev);
523 return put_long(arg, (bdi->ra_pages * PAGE_SIZE) / 512);
525 return put_int(arg, bdev_read_only(bdev) != 0);
526 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
527 return put_int(arg, block_size(bdev));
528 case BLKSSZGET: /* get block device logical block size */
529 return put_int(arg, bdev_logical_block_size(bdev));
530 case BLKPBSZGET: /* get block device physical block size */
531 return put_uint(arg, bdev_physical_block_size(bdev));
533 return put_uint(arg, bdev_io_min(bdev));
535 return put_uint(arg, bdev_io_opt(bdev));
537 return put_int(arg, bdev_alignment_offset(bdev));
538 case BLKDISCARDZEROES:
539 return put_uint(arg, bdev_discard_zeroes_data(bdev));
541 max_sectors = min_t(unsigned int, USHRT_MAX,
542 queue_max_sectors(bdev_get_queue(bdev)));
543 return put_ushort(arg, max_sectors);
545 return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
548 if(!capable(CAP_SYS_ADMIN))
550 bdi = blk_get_backing_dev_info(bdev);
551 bdi->ra_pages = (arg * 512) / PAGE_SIZE;
554 return blkdev_bszset(bdev, mode, argp);
556 return blkpg_ioctl(bdev, argp);
558 return blkdev_reread_part(bdev);
560 size = i_size_read(bdev->bd_inode);
561 if ((size >> 9) > ~0UL)
563 return put_ulong(arg, size >> 9);
565 return put_u64(arg, i_size_read(bdev->bd_inode));
569 case BLKTRACETEARDOWN:
570 return blk_trace_ioctl(bdev, cmd, argp);
571 case IOC_PR_REGISTER:
572 return blkdev_pr_register(bdev, argp);
574 return blkdev_pr_reserve(bdev, argp);
576 return blkdev_pr_release(bdev, argp);
578 return blkdev_pr_preempt(bdev, argp, false);
579 case IOC_PR_PREEMPT_ABORT:
580 return blkdev_pr_preempt(bdev, argp, true);
582 return blkdev_pr_clear(bdev, argp);
584 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
587 EXPORT_SYMBOL_GPL(blkdev_ioctl);