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 sector_t 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 if (p.start < 0 || p.length <= 0 || p.start + p.length < 0)
37 /* Check that the partition is aligned to the block size */
38 if (!IS_ALIGNED(p.start | p.length, bdev_logical_block_size(bdev)))
41 start = p.start >> SECTOR_SHIFT;
42 length = p.length >> SECTOR_SHIFT;
44 /* check for fit in a hd_struct */
45 if (sizeof(sector_t) < sizeof(long long)) {
46 long pstart = start, plength = length;
48 if (pstart != start || plength != length || pstart < 0 ||
49 plength < 0 || p.pno > 65535)
54 case BLKPG_ADD_PARTITION:
55 return bdev_add_partition(bdev, p.pno, start, length);
56 case BLKPG_RESIZE_PARTITION:
57 return bdev_resize_partition(bdev, p.pno, start, length);
63 static int blkpg_ioctl(struct block_device *bdev,
64 struct blkpg_ioctl_arg __user *arg)
66 struct blkpg_partition __user *udata;
69 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
72 return blkpg_do_ioctl(bdev, udata, op);
76 struct compat_blkpg_ioctl_arg {
83 static int compat_blkpg_ioctl(struct block_device *bdev,
84 struct compat_blkpg_ioctl_arg __user *arg)
89 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
92 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
96 static int blkdev_reread_part(struct block_device *bdev, fmode_t mode)
98 struct block_device *tmp;
100 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev))
102 if (!capable(CAP_SYS_ADMIN))
104 if (bdev->bd_part_count)
108 * Reopen the device to revalidate the driver state and force a
112 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
114 tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL);
117 blkdev_put(tmp, mode);
121 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
122 unsigned long arg, unsigned long flags)
126 struct request_queue *q = bdev_get_queue(bdev);
129 if (!(mode & FMODE_WRITE))
132 if (!blk_queue_discard(q))
135 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
146 if (start + len > i_size_read(bdev->bd_inode))
149 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
153 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
157 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
161 uint64_t start, end, len;
164 if (!(mode & FMODE_WRITE))
167 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
172 end = start + len - 1;
178 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
183 /* Invalidate the page cache, including dirty pages */
184 err = truncate_bdev_range(bdev, mode, start, end);
188 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
189 BLKDEV_ZERO_NOUNMAP);
192 static int put_ushort(unsigned short __user *argp, unsigned short val)
194 return put_user(val, argp);
197 static int put_int(int __user *argp, int val)
199 return put_user(val, argp);
202 static int put_uint(unsigned int __user *argp, unsigned int val)
204 return put_user(val, argp);
207 static int put_long(long __user *argp, long val)
209 return put_user(val, argp);
212 static int put_ulong(unsigned long __user *argp, unsigned long val)
214 return put_user(val, argp);
217 static int put_u64(u64 __user *argp, u64 val)
219 return put_user(val, argp);
223 static int compat_put_long(compat_long_t __user *argp, long val)
225 return put_user(val, argp);
228 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
230 return put_user(val, argp);
234 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
235 unsigned cmd, unsigned long arg)
237 struct gendisk *disk = bdev->bd_disk;
239 if (disk->fops->ioctl)
240 return disk->fops->ioctl(bdev, mode, cmd, arg);
245 * For the record: _GPL here is only because somebody decided to slap it
246 * on the previous export. Sheer idiocy, since it wasn't copyrightable
247 * at all and could be open-coded without any exports by anybody who cares.
249 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
253 * This is the equivalent of compat_ptr_ioctl(), to be used by block
254 * drivers that implement only commands that are completely compatible
255 * between 32-bit and 64-bit user space
257 int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
258 unsigned cmd, unsigned long arg)
260 struct gendisk *disk = bdev->bd_disk;
262 if (disk->fops->ioctl)
263 return disk->fops->ioctl(bdev, mode, cmd,
264 (unsigned long)compat_ptr(arg));
268 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
271 static int blkdev_pr_register(struct block_device *bdev,
272 struct pr_registration __user *arg)
274 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
275 struct pr_registration reg;
277 if (!capable(CAP_SYS_ADMIN))
279 if (!ops || !ops->pr_register)
281 if (copy_from_user(®, arg, sizeof(reg)))
284 if (reg.flags & ~PR_FL_IGNORE_KEY)
286 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
289 static int blkdev_pr_reserve(struct block_device *bdev,
290 struct pr_reservation __user *arg)
292 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
293 struct pr_reservation rsv;
295 if (!capable(CAP_SYS_ADMIN))
297 if (!ops || !ops->pr_reserve)
299 if (copy_from_user(&rsv, arg, sizeof(rsv)))
302 if (rsv.flags & ~PR_FL_IGNORE_KEY)
304 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
307 static int blkdev_pr_release(struct block_device *bdev,
308 struct pr_reservation __user *arg)
310 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
311 struct pr_reservation rsv;
313 if (!capable(CAP_SYS_ADMIN))
315 if (!ops || !ops->pr_release)
317 if (copy_from_user(&rsv, arg, sizeof(rsv)))
322 return ops->pr_release(bdev, rsv.key, rsv.type);
325 static int blkdev_pr_preempt(struct block_device *bdev,
326 struct pr_preempt __user *arg, bool abort)
328 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
331 if (!capable(CAP_SYS_ADMIN))
333 if (!ops || !ops->pr_preempt)
335 if (copy_from_user(&p, arg, sizeof(p)))
340 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
343 static int blkdev_pr_clear(struct block_device *bdev,
344 struct pr_clear __user *arg)
346 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
349 if (!capable(CAP_SYS_ADMIN))
351 if (!ops || !ops->pr_clear)
353 if (copy_from_user(&c, arg, sizeof(c)))
358 return ops->pr_clear(bdev, c.key);
362 * Is it an unrecognized ioctl? The correct returns are either
363 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
364 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
365 * code before returning.
367 * Confused drivers sometimes return EINVAL, which is wrong. It
368 * means "I understood the ioctl command, but the parameters to
371 * We should aim to just fix the broken drivers, the EINVAL case
374 static inline int is_unrecognized_ioctl(int ret)
376 return ret == -EINVAL ||
381 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
382 unsigned cmd, unsigned long arg)
386 if (!capable(CAP_SYS_ADMIN))
389 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
390 if (!is_unrecognized_ioctl(ret))
394 invalidate_bdev(bdev);
398 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
399 unsigned cmd, unsigned long arg)
403 if (!capable(CAP_SYS_ADMIN))
406 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
407 if (!is_unrecognized_ioctl(ret))
409 if (get_user(n, (int __user *)arg))
411 if (bdev->bd_disk->fops->set_read_only) {
412 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
416 set_device_ro(bdev, n);
420 static int blkdev_getgeo(struct block_device *bdev,
421 struct hd_geometry __user *argp)
423 struct gendisk *disk = bdev->bd_disk;
424 struct hd_geometry geo;
429 if (!disk->fops->getgeo)
433 * We need to set the startsect first, the driver may
434 * want to override it.
436 memset(&geo, 0, sizeof(geo));
437 geo.start = get_start_sect(bdev);
438 ret = disk->fops->getgeo(bdev, &geo);
441 if (copy_to_user(argp, &geo, sizeof(geo)))
447 struct compat_hd_geometry {
449 unsigned char sectors;
450 unsigned short cylinders;
454 static int compat_hdio_getgeo(struct block_device *bdev,
455 struct compat_hd_geometry __user *ugeo)
457 struct gendisk *disk = bdev->bd_disk;
458 struct hd_geometry geo;
463 if (!disk->fops->getgeo)
466 memset(&geo, 0, sizeof(geo));
468 * We need to set the startsect first, the driver may
469 * want to override it.
471 geo.start = get_start_sect(bdev);
472 ret = disk->fops->getgeo(bdev, &geo);
476 ret = copy_to_user(ugeo, &geo, 4);
477 ret |= put_user(geo.start, &ugeo->start);
485 /* set the logical block size */
486 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
491 if (!capable(CAP_SYS_ADMIN))
495 if (get_user(n, argp))
498 if (mode & FMODE_EXCL)
499 return set_blocksize(bdev, n);
501 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
503 ret = set_blocksize(bdev, n);
504 blkdev_put(bdev, mode | FMODE_EXCL);
510 * Common commands that are handled the same way on native and compat
511 * user space. Note the separate arg/argp parameters that are needed
512 * to deal with the compat_ptr() conversion.
514 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
515 unsigned cmd, unsigned long arg, void __user *argp)
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);
537 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
539 return put_uint(argp, bdev_zone_sectors(bdev));
541 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
543 return put_int(argp, bdev_read_only(bdev) != 0);
544 case BLKSSZGET: /* get block device logical block size */
545 return put_int(argp, bdev_logical_block_size(bdev));
546 case BLKPBSZGET: /* get block device physical block size */
547 return put_uint(argp, bdev_physical_block_size(bdev));
549 return put_uint(argp, bdev_io_min(bdev));
551 return put_uint(argp, bdev_io_opt(bdev));
553 return put_int(argp, bdev_alignment_offset(bdev));
554 case BLKDISCARDZEROES:
555 return put_uint(argp, 0);
557 max_sectors = min_t(unsigned int, USHRT_MAX,
558 queue_max_sectors(bdev_get_queue(bdev)));
559 return put_ushort(argp, max_sectors);
561 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
564 if(!capable(CAP_SYS_ADMIN))
566 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
569 return blkdev_reread_part(bdev, mode);
572 case BLKTRACETEARDOWN:
573 return blk_trace_ioctl(bdev, cmd, argp);
574 case IOC_PR_REGISTER:
575 return blkdev_pr_register(bdev, argp);
577 return blkdev_pr_reserve(bdev, argp);
579 return blkdev_pr_release(bdev, argp);
581 return blkdev_pr_preempt(bdev, argp, false);
582 case IOC_PR_PREEMPT_ABORT:
583 return blkdev_pr_preempt(bdev, argp, true);
585 return blkdev_pr_clear(bdev, argp);
592 * Always keep this in sync with compat_blkdev_ioctl()
593 * to handle all incompatible commands in both functions.
595 * New commands must be compatible and go into blkdev_common_ioctl
597 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
602 void __user *argp = (void __user *)arg;
605 /* These need separate implementations for the data structure */
607 return blkdev_getgeo(bdev, argp);
609 return blkpg_ioctl(bdev, argp);
611 /* Compat mode returns 32-bit data instead of 'long' */
616 return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
618 size = i_size_read(bdev->bd_inode);
619 if ((size >> 9) > ~0UL)
621 return put_ulong(argp, size >> 9);
623 /* The data is compatible, but the command number is different */
624 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
625 return put_int(argp, block_size(bdev));
627 return blkdev_bszset(bdev, mode, argp);
629 return put_u64(argp, i_size_read(bdev->bd_inode));
631 /* Incompatible alignment on i386 */
633 return blk_trace_ioctl(bdev, cmd, argp);
638 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
639 if (ret == -ENOIOCTLCMD)
640 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
644 EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
648 #define BLKBSZGET_32 _IOR(0x12, 112, int)
649 #define BLKBSZSET_32 _IOW(0x12, 113, int)
650 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
652 /* Most of the generic ioctls are handled in the normal fallback path.
653 This assumes the blkdev's low level compat_ioctl always returns
654 ENOIOCTLCMD for unknown ioctls. */
655 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
658 void __user *argp = compat_ptr(arg);
659 struct inode *inode = file->f_mapping->host;
660 struct block_device *bdev = inode->i_bdev;
661 struct gendisk *disk = bdev->bd_disk;
662 fmode_t mode = file->f_mode;
666 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
667 * to updated it before every ioctl.
669 if (file->f_flags & O_NDELAY)
670 mode |= FMODE_NDELAY;
672 mode &= ~FMODE_NDELAY;
675 /* These need separate implementations for the data structure */
677 return compat_hdio_getgeo(bdev, argp);
679 return compat_blkpg_ioctl(bdev, argp);
681 /* Compat mode returns 32-bit data instead of 'long' */
686 return compat_put_long(argp,
687 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
689 size = i_size_read(bdev->bd_inode);
690 if ((size >> 9) > ~(compat_ulong_t)0)
692 return compat_put_ulong(argp, size >> 9);
694 /* The data is compatible, but the command number is different */
695 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
696 return put_int(argp, bdev_logical_block_size(bdev));
698 return blkdev_bszset(bdev, mode, argp);
699 case BLKGETSIZE64_32:
700 return put_u64(argp, i_size_read(bdev->bd_inode));
702 /* Incompatible alignment on i386 */
703 case BLKTRACESETUP32:
704 return blk_trace_ioctl(bdev, cmd, argp);
709 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
710 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
711 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);