1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/capability.h>
3 #include <linux/compat.h>
4 #include <linux/blkdev.h>
5 #include <linux/export.h>
7 #include <linux/blkpg.h>
8 #include <linux/hdreg.h>
9 #include <linux/backing-dev.h>
11 #include <linux/blktrace_api.h>
13 #include <linux/uaccess.h>
16 static int blkpg_do_ioctl(struct block_device *bdev,
17 struct blkpg_partition __user *upart, int op)
19 struct blkpg_partition p;
20 long long start, length;
22 if (!capable(CAP_SYS_ADMIN))
24 if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
26 if (bdev_is_partition(bdev))
32 if (op == BLKPG_DEL_PARTITION)
33 return bdev_del_partition(bdev, p.pno);
35 start = p.start >> SECTOR_SHIFT;
36 length = p.length >> SECTOR_SHIFT;
38 /* check for fit in a hd_struct */
39 if (sizeof(sector_t) < sizeof(long long)) {
40 long pstart = start, plength = length;
42 if (pstart != start || plength != length || pstart < 0 ||
43 plength < 0 || p.pno > 65535)
48 case BLKPG_ADD_PARTITION:
49 /* check if partition is aligned to blocksize */
50 if (p.start & (bdev_logical_block_size(bdev) - 1))
52 return bdev_add_partition(bdev, p.pno, start, length);
53 case BLKPG_RESIZE_PARTITION:
54 return bdev_resize_partition(bdev, p.pno, start, length);
60 static int blkpg_ioctl(struct block_device *bdev,
61 struct blkpg_ioctl_arg __user *arg)
63 struct blkpg_partition __user *udata;
66 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
69 return blkpg_do_ioctl(bdev, udata, op);
73 struct compat_blkpg_ioctl_arg {
80 static int compat_blkpg_ioctl(struct block_device *bdev,
81 struct compat_blkpg_ioctl_arg __user *arg)
86 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
89 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
93 static int blkdev_reread_part(struct block_device *bdev, fmode_t mode)
95 struct block_device *tmp;
97 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev))
99 if (!capable(CAP_SYS_ADMIN))
101 if (bdev->bd_part_count)
105 * Reopen the device to revalidate the driver state and force a
109 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
111 tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL);
114 blkdev_put(tmp, mode);
118 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
119 unsigned long arg, unsigned long flags)
123 struct request_queue *q = bdev_get_queue(bdev);
126 if (!(mode & FMODE_WRITE))
129 if (!blk_queue_discard(q))
132 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
143 if (start + len > i_size_read(bdev->bd_inode))
146 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
150 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
154 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
158 uint64_t start, end, len;
161 if (!(mode & FMODE_WRITE))
164 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
169 end = start + len - 1;
175 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
180 /* Invalidate the page cache, including dirty pages */
181 err = truncate_bdev_range(bdev, mode, start, end);
185 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
186 BLKDEV_ZERO_NOUNMAP);
189 static int put_ushort(unsigned short __user *argp, unsigned short val)
191 return put_user(val, argp);
194 static int put_int(int __user *argp, int val)
196 return put_user(val, argp);
199 static int put_uint(unsigned int __user *argp, unsigned int val)
201 return put_user(val, argp);
204 static int put_long(long __user *argp, long val)
206 return put_user(val, argp);
209 static int put_ulong(unsigned long __user *argp, unsigned long val)
211 return put_user(val, argp);
214 static int put_u64(u64 __user *argp, u64 val)
216 return put_user(val, argp);
220 static int compat_put_long(compat_long_t __user *argp, long val)
222 return put_user(val, argp);
225 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
227 return put_user(val, argp);
231 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
232 unsigned cmd, unsigned long arg)
234 struct gendisk *disk = bdev->bd_disk;
236 if (disk->fops->ioctl)
237 return disk->fops->ioctl(bdev, mode, cmd, arg);
242 * For the record: _GPL here is only because somebody decided to slap it
243 * on the previous export. Sheer idiocy, since it wasn't copyrightable
244 * at all and could be open-coded without any exports by anybody who cares.
246 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
250 * This is the equivalent of compat_ptr_ioctl(), to be used by block
251 * drivers that implement only commands that are completely compatible
252 * between 32-bit and 64-bit user space
254 int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
255 unsigned cmd, unsigned long arg)
257 struct gendisk *disk = bdev->bd_disk;
259 if (disk->fops->ioctl)
260 return disk->fops->ioctl(bdev, mode, cmd,
261 (unsigned long)compat_ptr(arg));
265 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
268 static int blkdev_pr_register(struct block_device *bdev,
269 struct pr_registration __user *arg)
271 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
272 struct pr_registration reg;
274 if (!capable(CAP_SYS_ADMIN))
276 if (!ops || !ops->pr_register)
278 if (copy_from_user(®, arg, sizeof(reg)))
281 if (reg.flags & ~PR_FL_IGNORE_KEY)
283 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
286 static int blkdev_pr_reserve(struct block_device *bdev,
287 struct pr_reservation __user *arg)
289 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
290 struct pr_reservation rsv;
292 if (!capable(CAP_SYS_ADMIN))
294 if (!ops || !ops->pr_reserve)
296 if (copy_from_user(&rsv, arg, sizeof(rsv)))
299 if (rsv.flags & ~PR_FL_IGNORE_KEY)
301 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
304 static int blkdev_pr_release(struct block_device *bdev,
305 struct pr_reservation __user *arg)
307 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
308 struct pr_reservation rsv;
310 if (!capable(CAP_SYS_ADMIN))
312 if (!ops || !ops->pr_release)
314 if (copy_from_user(&rsv, arg, sizeof(rsv)))
319 return ops->pr_release(bdev, rsv.key, rsv.type);
322 static int blkdev_pr_preempt(struct block_device *bdev,
323 struct pr_preempt __user *arg, bool abort)
325 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
328 if (!capable(CAP_SYS_ADMIN))
330 if (!ops || !ops->pr_preempt)
332 if (copy_from_user(&p, arg, sizeof(p)))
337 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
340 static int blkdev_pr_clear(struct block_device *bdev,
341 struct pr_clear __user *arg)
343 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
346 if (!capable(CAP_SYS_ADMIN))
348 if (!ops || !ops->pr_clear)
350 if (copy_from_user(&c, arg, sizeof(c)))
355 return ops->pr_clear(bdev, c.key);
359 * Is it an unrecognized ioctl? The correct returns are either
360 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
361 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
362 * code before returning.
364 * Confused drivers sometimes return EINVAL, which is wrong. It
365 * means "I understood the ioctl command, but the parameters to
368 * We should aim to just fix the broken drivers, the EINVAL case
371 static inline int is_unrecognized_ioctl(int ret)
373 return ret == -EINVAL ||
378 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
379 unsigned cmd, unsigned long arg)
383 if (!capable(CAP_SYS_ADMIN))
386 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
387 if (!is_unrecognized_ioctl(ret))
391 invalidate_bdev(bdev);
395 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
396 unsigned cmd, unsigned long arg)
400 if (!capable(CAP_SYS_ADMIN))
403 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
404 if (!is_unrecognized_ioctl(ret))
406 if (get_user(n, (int __user *)arg))
408 set_device_ro(bdev, n);
412 static int blkdev_getgeo(struct block_device *bdev,
413 struct hd_geometry __user *argp)
415 struct gendisk *disk = bdev->bd_disk;
416 struct hd_geometry geo;
421 if (!disk->fops->getgeo)
425 * We need to set the startsect first, the driver may
426 * want to override it.
428 memset(&geo, 0, sizeof(geo));
429 geo.start = get_start_sect(bdev);
430 ret = disk->fops->getgeo(bdev, &geo);
433 if (copy_to_user(argp, &geo, sizeof(geo)))
439 struct compat_hd_geometry {
441 unsigned char sectors;
442 unsigned short cylinders;
446 static int compat_hdio_getgeo(struct block_device *bdev,
447 struct compat_hd_geometry __user *ugeo)
449 struct gendisk *disk = bdev->bd_disk;
450 struct hd_geometry geo;
455 if (!disk->fops->getgeo)
458 memset(&geo, 0, sizeof(geo));
460 * We need to set the startsect first, the driver may
461 * want to override it.
463 geo.start = get_start_sect(bdev);
464 ret = disk->fops->getgeo(bdev, &geo);
468 ret = copy_to_user(ugeo, &geo, 4);
469 ret |= put_user(geo.start, &ugeo->start);
477 /* set the logical block size */
478 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
483 if (!capable(CAP_SYS_ADMIN))
487 if (get_user(n, argp))
490 if (mode & FMODE_EXCL)
491 return set_blocksize(bdev, n);
493 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
495 ret = set_blocksize(bdev, n);
496 blkdev_put(bdev, mode | FMODE_EXCL);
502 * Common commands that are handled the same way on native and compat
503 * user space. Note the separate arg/argp parameters that are needed
504 * to deal with the compat_ptr() conversion.
506 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
507 unsigned cmd, unsigned long arg, void __user *argp)
509 unsigned int max_sectors;
513 return blkdev_flushbuf(bdev, mode, cmd, arg);
515 return blkdev_roset(bdev, mode, cmd, arg);
517 return blk_ioctl_discard(bdev, mode, arg, 0);
519 return blk_ioctl_discard(bdev, mode, arg,
520 BLKDEV_DISCARD_SECURE);
522 return blk_ioctl_zeroout(bdev, mode, arg);
524 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
529 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
531 return put_uint(argp, bdev_zone_sectors(bdev));
533 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
535 return put_int(argp, bdev_read_only(bdev) != 0);
536 case BLKSSZGET: /* get block device logical block size */
537 return put_int(argp, bdev_logical_block_size(bdev));
538 case BLKPBSZGET: /* get block device physical block size */
539 return put_uint(argp, bdev_physical_block_size(bdev));
541 return put_uint(argp, bdev_io_min(bdev));
543 return put_uint(argp, bdev_io_opt(bdev));
545 return put_int(argp, bdev_alignment_offset(bdev));
546 case BLKDISCARDZEROES:
547 return put_uint(argp, 0);
549 max_sectors = min_t(unsigned int, USHRT_MAX,
550 queue_max_sectors(bdev_get_queue(bdev)));
551 return put_ushort(argp, max_sectors);
553 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
556 if(!capable(CAP_SYS_ADMIN))
558 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
561 return blkdev_reread_part(bdev, mode);
564 case BLKTRACETEARDOWN:
565 return blk_trace_ioctl(bdev, cmd, argp);
566 case IOC_PR_REGISTER:
567 return blkdev_pr_register(bdev, argp);
569 return blkdev_pr_reserve(bdev, argp);
571 return blkdev_pr_release(bdev, argp);
573 return blkdev_pr_preempt(bdev, argp, false);
574 case IOC_PR_PREEMPT_ABORT:
575 return blkdev_pr_preempt(bdev, argp, true);
577 return blkdev_pr_clear(bdev, argp);
584 * Always keep this in sync with compat_blkdev_ioctl()
585 * to handle all incompatible commands in both functions.
587 * New commands must be compatible and go into blkdev_common_ioctl
589 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
594 void __user *argp = (void __user *)arg;
597 /* These need separate implementations for the data structure */
599 return blkdev_getgeo(bdev, argp);
601 return blkpg_ioctl(bdev, argp);
603 /* Compat mode returns 32-bit data instead of 'long' */
608 return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
610 size = i_size_read(bdev->bd_inode);
611 if ((size >> 9) > ~0UL)
613 return put_ulong(argp, size >> 9);
615 /* The data is compatible, but the command number is different */
616 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
617 return put_int(argp, block_size(bdev));
619 return blkdev_bszset(bdev, mode, argp);
621 return put_u64(argp, i_size_read(bdev->bd_inode));
623 /* Incompatible alignment on i386 */
625 return blk_trace_ioctl(bdev, cmd, argp);
630 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
631 if (ret == -ENOIOCTLCMD)
632 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
636 EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
640 #define BLKBSZGET_32 _IOR(0x12, 112, int)
641 #define BLKBSZSET_32 _IOW(0x12, 113, int)
642 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
644 /* Most of the generic ioctls are handled in the normal fallback path.
645 This assumes the blkdev's low level compat_ioctl always returns
646 ENOIOCTLCMD for unknown ioctls. */
647 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
650 void __user *argp = compat_ptr(arg);
651 struct inode *inode = file->f_mapping->host;
652 struct block_device *bdev = inode->i_bdev;
653 struct gendisk *disk = bdev->bd_disk;
654 fmode_t mode = file->f_mode;
658 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
659 * to updated it before every ioctl.
661 if (file->f_flags & O_NDELAY)
662 mode |= FMODE_NDELAY;
664 mode &= ~FMODE_NDELAY;
667 /* These need separate implementations for the data structure */
669 return compat_hdio_getgeo(bdev, argp);
671 return compat_blkpg_ioctl(bdev, argp);
673 /* Compat mode returns 32-bit data instead of 'long' */
678 return compat_put_long(argp,
679 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
681 size = i_size_read(bdev->bd_inode);
682 if ((size >> 9) > ~0UL)
684 return compat_put_ulong(argp, size >> 9);
686 /* The data is compatible, but the command number is different */
687 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
688 return put_int(argp, bdev_logical_block_size(bdev));
690 return blkdev_bszset(bdev, mode, argp);
691 case BLKGETSIZE64_32:
692 return put_u64(argp, i_size_read(bdev->bd_inode));
694 /* Incompatible alignment on i386 */
695 case BLKTRACESETUP32:
696 return blk_trace_ioctl(bdev, cmd, argp);
701 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
702 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
703 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);