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 gendisk *disk = bdev->bd_disk;
20 struct blkpg_partition p;
21 long long start, length;
23 if (!capable(CAP_SYS_ADMIN))
25 if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
27 if (bdev_is_partition(bdev))
33 if (op == BLKPG_DEL_PARTITION)
34 return bdev_del_partition(disk, p.pno);
36 start = p.start >> SECTOR_SHIFT;
37 length = p.length >> SECTOR_SHIFT;
40 case BLKPG_ADD_PARTITION:
41 /* check if partition is aligned to blocksize */
42 if (p.start & (bdev_logical_block_size(bdev) - 1))
44 return bdev_add_partition(disk, p.pno, start, length);
45 case BLKPG_RESIZE_PARTITION:
46 return bdev_resize_partition(disk, p.pno, start, length);
52 static int blkpg_ioctl(struct block_device *bdev,
53 struct blkpg_ioctl_arg __user *arg)
55 struct blkpg_partition __user *udata;
58 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
61 return blkpg_do_ioctl(bdev, udata, op);
65 struct compat_blkpg_ioctl_arg {
72 static int compat_blkpg_ioctl(struct block_device *bdev,
73 struct compat_blkpg_ioctl_arg __user *arg)
78 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
81 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
85 static int blkdev_reread_part(struct block_device *bdev, fmode_t mode)
87 struct block_device *tmp;
89 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev))
91 if (!capable(CAP_SYS_ADMIN))
93 if (bdev->bd_disk->open_partitions)
97 * Reopen the device to revalidate the driver state and force a
101 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
103 tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL);
106 blkdev_put(tmp, mode);
110 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
111 unsigned long arg, unsigned long flags)
115 struct request_queue *q = bdev_get_queue(bdev);
116 struct inode *inode = bdev->bd_inode;
119 if (!(mode & FMODE_WRITE))
122 if (!blk_queue_discard(q))
125 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
136 if (start + len > i_size_read(bdev->bd_inode))
139 filemap_invalidate_lock(inode->i_mapping);
140 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
144 err = blkdev_issue_discard(bdev, start >> 9, len >> 9,
148 filemap_invalidate_unlock(inode->i_mapping);
152 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
156 uint64_t start, end, len;
157 struct inode *inode = bdev->bd_inode;
160 if (!(mode & FMODE_WRITE))
163 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
168 end = start + len - 1;
174 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
179 /* Invalidate the page cache, including dirty pages */
180 filemap_invalidate_lock(inode->i_mapping);
181 err = truncate_bdev_range(bdev, mode, start, end);
185 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
186 BLKDEV_ZERO_NOUNMAP);
189 filemap_invalidate_unlock(inode->i_mapping);
193 static int put_ushort(unsigned short __user *argp, unsigned short val)
195 return put_user(val, argp);
198 static int put_int(int __user *argp, int val)
200 return put_user(val, argp);
203 static int put_uint(unsigned int __user *argp, unsigned int val)
205 return put_user(val, argp);
208 static int put_long(long __user *argp, long val)
210 return put_user(val, argp);
213 static int put_ulong(unsigned long __user *argp, unsigned long val)
215 return put_user(val, argp);
218 static int put_u64(u64 __user *argp, u64 val)
220 return put_user(val, argp);
224 static int compat_put_long(compat_long_t __user *argp, long val)
226 return put_user(val, argp);
229 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
231 return put_user(val, argp);
235 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
236 unsigned cmd, unsigned long arg)
238 struct gendisk *disk = bdev->bd_disk;
240 if (disk->fops->ioctl)
241 return disk->fops->ioctl(bdev, mode, cmd, arg);
246 * For the record: _GPL here is only because somebody decided to slap it
247 * on the previous export. Sheer idiocy, since it wasn't copyrightable
248 * at all and could be open-coded without any exports by anybody who cares.
250 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
254 * This is the equivalent of compat_ptr_ioctl(), to be used by block
255 * drivers that implement only commands that are completely compatible
256 * between 32-bit and 64-bit user space
258 int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
259 unsigned cmd, unsigned long arg)
261 struct gendisk *disk = bdev->bd_disk;
263 if (disk->fops->ioctl)
264 return disk->fops->ioctl(bdev, mode, cmd,
265 (unsigned long)compat_ptr(arg));
269 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
272 static int blkdev_pr_register(struct block_device *bdev,
273 struct pr_registration __user *arg)
275 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
276 struct pr_registration reg;
278 if (!capable(CAP_SYS_ADMIN))
280 if (!ops || !ops->pr_register)
282 if (copy_from_user(®, arg, sizeof(reg)))
285 if (reg.flags & ~PR_FL_IGNORE_KEY)
287 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
290 static int blkdev_pr_reserve(struct block_device *bdev,
291 struct pr_reservation __user *arg)
293 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
294 struct pr_reservation rsv;
296 if (!capable(CAP_SYS_ADMIN))
298 if (!ops || !ops->pr_reserve)
300 if (copy_from_user(&rsv, arg, sizeof(rsv)))
303 if (rsv.flags & ~PR_FL_IGNORE_KEY)
305 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
308 static int blkdev_pr_release(struct block_device *bdev,
309 struct pr_reservation __user *arg)
311 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
312 struct pr_reservation rsv;
314 if (!capable(CAP_SYS_ADMIN))
316 if (!ops || !ops->pr_release)
318 if (copy_from_user(&rsv, arg, sizeof(rsv)))
323 return ops->pr_release(bdev, rsv.key, rsv.type);
326 static int blkdev_pr_preempt(struct block_device *bdev,
327 struct pr_preempt __user *arg, bool abort)
329 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
332 if (!capable(CAP_SYS_ADMIN))
334 if (!ops || !ops->pr_preempt)
336 if (copy_from_user(&p, arg, sizeof(p)))
341 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
344 static int blkdev_pr_clear(struct block_device *bdev,
345 struct pr_clear __user *arg)
347 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
350 if (!capable(CAP_SYS_ADMIN))
352 if (!ops || !ops->pr_clear)
354 if (copy_from_user(&c, arg, sizeof(c)))
359 return ops->pr_clear(bdev, c.key);
363 * Is it an unrecognized ioctl? The correct returns are either
364 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
365 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
366 * code before returning.
368 * Confused drivers sometimes return EINVAL, which is wrong. It
369 * means "I understood the ioctl command, but the parameters to
372 * We should aim to just fix the broken drivers, the EINVAL case
375 static inline int is_unrecognized_ioctl(int ret)
377 return ret == -EINVAL ||
382 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
383 unsigned cmd, unsigned long arg)
387 if (!capable(CAP_SYS_ADMIN))
390 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
391 if (!is_unrecognized_ioctl(ret))
395 invalidate_bdev(bdev);
399 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
400 unsigned cmd, unsigned long arg)
404 if (!capable(CAP_SYS_ADMIN))
407 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
408 if (!is_unrecognized_ioctl(ret))
410 if (get_user(n, (int __user *)arg))
412 if (bdev->bd_disk->fops->set_read_only) {
413 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
417 bdev->bd_read_only = n;
421 static int blkdev_getgeo(struct block_device *bdev,
422 struct hd_geometry __user *argp)
424 struct gendisk *disk = bdev->bd_disk;
425 struct hd_geometry geo;
430 if (!disk->fops->getgeo)
434 * We need to set the startsect first, the driver may
435 * want to override it.
437 memset(&geo, 0, sizeof(geo));
438 geo.start = get_start_sect(bdev);
439 ret = disk->fops->getgeo(bdev, &geo);
442 if (copy_to_user(argp, &geo, sizeof(geo)))
448 struct compat_hd_geometry {
450 unsigned char sectors;
451 unsigned short cylinders;
455 static int compat_hdio_getgeo(struct block_device *bdev,
456 struct compat_hd_geometry __user *ugeo)
458 struct gendisk *disk = bdev->bd_disk;
459 struct hd_geometry geo;
464 if (!disk->fops->getgeo)
467 memset(&geo, 0, sizeof(geo));
469 * We need to set the startsect first, the driver may
470 * want to override it.
472 geo.start = get_start_sect(bdev);
473 ret = disk->fops->getgeo(bdev, &geo);
477 ret = copy_to_user(ugeo, &geo, 4);
478 ret |= put_user(geo.start, &ugeo->start);
486 /* set the logical block size */
487 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
492 if (!capable(CAP_SYS_ADMIN))
496 if (get_user(n, argp))
499 if (mode & FMODE_EXCL)
500 return set_blocksize(bdev, n);
502 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
504 ret = set_blocksize(bdev, n);
505 blkdev_put(bdev, mode | FMODE_EXCL);
511 * Common commands that are handled the same way on native and compat
512 * user space. Note the separate arg/argp parameters that are needed
513 * to deal with the compat_ptr() conversion.
515 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
516 unsigned cmd, unsigned long arg, void __user *argp)
518 unsigned int max_sectors;
522 return blkdev_flushbuf(bdev, mode, cmd, arg);
524 return blkdev_roset(bdev, mode, cmd, arg);
526 return blk_ioctl_discard(bdev, mode, arg, 0);
528 return blk_ioctl_discard(bdev, mode, arg,
529 BLKDEV_DISCARD_SECURE);
531 return blk_ioctl_zeroout(bdev, mode, arg);
533 return put_u64(argp, bdev->bd_disk->diskseq);
535 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
540 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
542 return put_uint(argp, bdev_zone_sectors(bdev));
544 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
546 return put_int(argp, bdev_read_only(bdev) != 0);
547 case BLKSSZGET: /* get block device logical block size */
548 return put_int(argp, bdev_logical_block_size(bdev));
549 case BLKPBSZGET: /* get block device physical block size */
550 return put_uint(argp, bdev_physical_block_size(bdev));
552 return put_uint(argp, bdev_io_min(bdev));
554 return put_uint(argp, bdev_io_opt(bdev));
556 return put_int(argp, bdev_alignment_offset(bdev));
557 case BLKDISCARDZEROES:
558 return put_uint(argp, 0);
560 max_sectors = min_t(unsigned int, USHRT_MAX,
561 queue_max_sectors(bdev_get_queue(bdev)));
562 return put_ushort(argp, max_sectors);
564 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
567 if(!capable(CAP_SYS_ADMIN))
569 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
572 return blkdev_reread_part(bdev, mode);
575 case BLKTRACETEARDOWN:
576 return blk_trace_ioctl(bdev, cmd, argp);
577 case IOC_PR_REGISTER:
578 return blkdev_pr_register(bdev, argp);
580 return blkdev_pr_reserve(bdev, argp);
582 return blkdev_pr_release(bdev, argp);
584 return blkdev_pr_preempt(bdev, argp, false);
585 case IOC_PR_PREEMPT_ABORT:
586 return blkdev_pr_preempt(bdev, argp, true);
588 return blkdev_pr_clear(bdev, argp);
595 * Always keep this in sync with compat_blkdev_ioctl()
596 * to handle all incompatible commands in both functions.
598 * New commands must be compatible and go into blkdev_common_ioctl
600 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
605 void __user *argp = (void __user *)arg;
608 /* These need separate implementations for the data structure */
610 return blkdev_getgeo(bdev, argp);
612 return blkpg_ioctl(bdev, argp);
614 /* Compat mode returns 32-bit data instead of 'long' */
619 return put_long(argp,
620 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
622 size = i_size_read(bdev->bd_inode);
623 if ((size >> 9) > ~0UL)
625 return put_ulong(argp, size >> 9);
627 /* The data is compatible, but the command number is different */
628 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
629 return put_int(argp, block_size(bdev));
631 return blkdev_bszset(bdev, mode, argp);
633 return put_u64(argp, i_size_read(bdev->bd_inode));
635 /* Incompatible alignment on i386 */
637 return blk_trace_ioctl(bdev, cmd, argp);
642 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
643 if (ret == -ENOIOCTLCMD)
644 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
648 EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
652 #define BLKBSZGET_32 _IOR(0x12, 112, int)
653 #define BLKBSZSET_32 _IOW(0x12, 113, int)
654 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
656 /* Most of the generic ioctls are handled in the normal fallback path.
657 This assumes the blkdev's low level compat_ioctl always returns
658 ENOIOCTLCMD for unknown ioctls. */
659 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
662 void __user *argp = compat_ptr(arg);
663 struct block_device *bdev = I_BDEV(file->f_mapping->host);
664 struct gendisk *disk = bdev->bd_disk;
665 fmode_t mode = file->f_mode;
669 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
670 * to updated it before every ioctl.
672 if (file->f_flags & O_NDELAY)
673 mode |= FMODE_NDELAY;
675 mode &= ~FMODE_NDELAY;
678 /* These need separate implementations for the data structure */
680 return compat_hdio_getgeo(bdev, argp);
682 return compat_blkpg_ioctl(bdev, argp);
684 /* Compat mode returns 32-bit data instead of 'long' */
689 return compat_put_long(argp,
690 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
692 size = i_size_read(bdev->bd_inode);
693 if ((size >> 9) > ~(compat_ulong_t)0)
695 return compat_put_ulong(argp, size >> 9);
697 /* The data is compatible, but the command number is different */
698 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
699 return put_int(argp, bdev_logical_block_size(bdev));
701 return blkdev_bszset(bdev, mode, argp);
702 case BLKGETSIZE64_32:
703 return put_u64(argp, i_size_read(bdev->bd_inode));
705 /* Incompatible alignment on i386 */
706 case BLKTRACESETUP32:
707 return blk_trace_ioctl(bdev, cmd, argp);
712 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
713 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
714 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);