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 != bdev->bd_contains)
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)
97 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev != bdev->bd_contains)
99 if (!capable(CAP_SYS_ADMIN))
102 mutex_lock(&bdev->bd_mutex);
103 ret = bdev_disk_changed(bdev, false);
104 mutex_unlock(&bdev->bd_mutex);
109 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
110 unsigned long arg, unsigned long flags)
114 struct request_queue *q = bdev_get_queue(bdev);
115 struct address_space *mapping = bdev->bd_inode->i_mapping;
118 if (!(mode & FMODE_WRITE))
121 if (!blk_queue_discard(q))
124 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
135 if (start + len > i_size_read(bdev->bd_inode))
137 truncate_inode_pages_range(mapping, start, start + len - 1);
138 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
142 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
146 struct address_space *mapping;
147 uint64_t start, end, len;
149 if (!(mode & FMODE_WRITE))
152 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
157 end = start + len - 1;
163 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
168 /* Invalidate the page cache, including dirty pages */
169 mapping = bdev->bd_inode->i_mapping;
170 truncate_inode_pages_range(mapping, start, end);
172 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
173 BLKDEV_ZERO_NOUNMAP);
176 static int put_ushort(unsigned short __user *argp, unsigned short val)
178 return put_user(val, argp);
181 static int put_int(int __user *argp, int val)
183 return put_user(val, argp);
186 static int put_uint(unsigned int __user *argp, unsigned int val)
188 return put_user(val, argp);
191 static int put_long(long __user *argp, long val)
193 return put_user(val, argp);
196 static int put_ulong(unsigned long __user *argp, unsigned long val)
198 return put_user(val, argp);
201 static int put_u64(u64 __user *argp, u64 val)
203 return put_user(val, argp);
207 static int compat_put_long(compat_long_t __user *argp, long val)
209 return put_user(val, argp);
212 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
214 return put_user(val, argp);
218 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
219 unsigned cmd, unsigned long arg)
221 struct gendisk *disk = bdev->bd_disk;
223 if (disk->fops->ioctl)
224 return disk->fops->ioctl(bdev, mode, cmd, arg);
229 * For the record: _GPL here is only because somebody decided to slap it
230 * on the previous export. Sheer idiocy, since it wasn't copyrightable
231 * at all and could be open-coded without any exports by anybody who cares.
233 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
237 * This is the equivalent of compat_ptr_ioctl(), to be used by block
238 * drivers that implement only commands that are completely compatible
239 * between 32-bit and 64-bit user space
241 int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
242 unsigned cmd, unsigned long arg)
244 struct gendisk *disk = bdev->bd_disk;
246 if (disk->fops->ioctl)
247 return disk->fops->ioctl(bdev, mode, cmd,
248 (unsigned long)compat_ptr(arg));
252 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
255 static int blkdev_pr_register(struct block_device *bdev,
256 struct pr_registration __user *arg)
258 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
259 struct pr_registration reg;
261 if (!capable(CAP_SYS_ADMIN))
263 if (!ops || !ops->pr_register)
265 if (copy_from_user(®, arg, sizeof(reg)))
268 if (reg.flags & ~PR_FL_IGNORE_KEY)
270 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
273 static int blkdev_pr_reserve(struct block_device *bdev,
274 struct pr_reservation __user *arg)
276 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
277 struct pr_reservation rsv;
279 if (!capable(CAP_SYS_ADMIN))
281 if (!ops || !ops->pr_reserve)
283 if (copy_from_user(&rsv, arg, sizeof(rsv)))
286 if (rsv.flags & ~PR_FL_IGNORE_KEY)
288 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
291 static int blkdev_pr_release(struct block_device *bdev,
292 struct pr_reservation __user *arg)
294 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
295 struct pr_reservation rsv;
297 if (!capable(CAP_SYS_ADMIN))
299 if (!ops || !ops->pr_release)
301 if (copy_from_user(&rsv, arg, sizeof(rsv)))
306 return ops->pr_release(bdev, rsv.key, rsv.type);
309 static int blkdev_pr_preempt(struct block_device *bdev,
310 struct pr_preempt __user *arg, bool abort)
312 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
315 if (!capable(CAP_SYS_ADMIN))
317 if (!ops || !ops->pr_preempt)
319 if (copy_from_user(&p, arg, sizeof(p)))
324 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
327 static int blkdev_pr_clear(struct block_device *bdev,
328 struct pr_clear __user *arg)
330 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
333 if (!capable(CAP_SYS_ADMIN))
335 if (!ops || !ops->pr_clear)
337 if (copy_from_user(&c, arg, sizeof(c)))
342 return ops->pr_clear(bdev, c.key);
346 * Is it an unrecognized ioctl? The correct returns are either
347 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
348 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
349 * code before returning.
351 * Confused drivers sometimes return EINVAL, which is wrong. It
352 * means "I understood the ioctl command, but the parameters to
355 * We should aim to just fix the broken drivers, the EINVAL case
358 static inline int is_unrecognized_ioctl(int ret)
360 return ret == -EINVAL ||
365 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
366 unsigned cmd, unsigned long arg)
370 if (!capable(CAP_SYS_ADMIN))
373 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
374 if (!is_unrecognized_ioctl(ret))
378 invalidate_bdev(bdev);
382 static int blkdev_roset(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))
393 if (get_user(n, (int __user *)arg))
395 set_device_ro(bdev, n);
399 static int blkdev_getgeo(struct block_device *bdev,
400 struct hd_geometry __user *argp)
402 struct gendisk *disk = bdev->bd_disk;
403 struct hd_geometry geo;
408 if (!disk->fops->getgeo)
412 * We need to set the startsect first, the driver may
413 * want to override it.
415 memset(&geo, 0, sizeof(geo));
416 geo.start = get_start_sect(bdev);
417 ret = disk->fops->getgeo(bdev, &geo);
420 if (copy_to_user(argp, &geo, sizeof(geo)))
426 struct compat_hd_geometry {
428 unsigned char sectors;
429 unsigned short cylinders;
433 static int compat_hdio_getgeo(struct block_device *bdev,
434 struct compat_hd_geometry __user *ugeo)
436 struct gendisk *disk = bdev->bd_disk;
437 struct hd_geometry geo;
442 if (!disk->fops->getgeo)
445 memset(&geo, 0, sizeof(geo));
447 * We need to set the startsect first, the driver may
448 * want to override it.
450 geo.start = get_start_sect(bdev);
451 ret = disk->fops->getgeo(bdev, &geo);
455 ret = copy_to_user(ugeo, &geo, 4);
456 ret |= put_user(geo.start, &ugeo->start);
464 /* set the logical block size */
465 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
470 if (!capable(CAP_SYS_ADMIN))
474 if (get_user(n, argp))
477 if (!(mode & FMODE_EXCL)) {
479 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
483 ret = set_blocksize(bdev, n);
484 if (!(mode & FMODE_EXCL))
485 blkdev_put(bdev, mode | FMODE_EXCL);
490 * Common commands that are handled the same way on native and compat
491 * user space. Note the separate arg/argp parameters that are needed
492 * to deal with the compat_ptr() conversion.
494 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
495 unsigned cmd, unsigned long arg, void __user *argp)
497 unsigned int max_sectors;
501 return blkdev_flushbuf(bdev, mode, cmd, arg);
503 return blkdev_roset(bdev, mode, cmd, arg);
505 return blk_ioctl_discard(bdev, mode, arg, 0);
507 return blk_ioctl_discard(bdev, mode, arg,
508 BLKDEV_DISCARD_SECURE);
510 return blk_ioctl_zeroout(bdev, mode, arg);
512 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
517 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
519 return put_uint(argp, bdev_zone_sectors(bdev));
521 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
523 return put_int(argp, bdev_read_only(bdev) != 0);
524 case BLKSSZGET: /* get block device logical block size */
525 return put_int(argp, bdev_logical_block_size(bdev));
526 case BLKPBSZGET: /* get block device physical block size */
527 return put_uint(argp, bdev_physical_block_size(bdev));
529 return put_uint(argp, bdev_io_min(bdev));
531 return put_uint(argp, bdev_io_opt(bdev));
533 return put_int(argp, bdev_alignment_offset(bdev));
534 case BLKDISCARDZEROES:
535 return put_uint(argp, 0);
537 max_sectors = min_t(unsigned int, USHRT_MAX,
538 queue_max_sectors(bdev_get_queue(bdev)));
539 return put_ushort(argp, max_sectors);
541 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
544 if(!capable(CAP_SYS_ADMIN))
546 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
549 return blkdev_reread_part(bdev);
552 case BLKTRACETEARDOWN:
553 return blk_trace_ioctl(bdev, cmd, argp);
554 case IOC_PR_REGISTER:
555 return blkdev_pr_register(bdev, argp);
557 return blkdev_pr_reserve(bdev, argp);
559 return blkdev_pr_release(bdev, argp);
561 return blkdev_pr_preempt(bdev, argp, false);
562 case IOC_PR_PREEMPT_ABORT:
563 return blkdev_pr_preempt(bdev, argp, true);
565 return blkdev_pr_clear(bdev, argp);
572 * Always keep this in sync with compat_blkdev_ioctl()
573 * to handle all incompatible commands in both functions.
575 * New commands must be compatible and go into blkdev_common_ioctl
577 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
582 void __user *argp = (void __user *)arg;
585 /* These need separate implementations for the data structure */
587 return blkdev_getgeo(bdev, argp);
589 return blkpg_ioctl(bdev, argp);
591 /* Compat mode returns 32-bit data instead of 'long' */
596 return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
598 size = i_size_read(bdev->bd_inode);
599 if ((size >> 9) > ~0UL)
601 return put_ulong(argp, size >> 9);
603 /* The data is compatible, but the command number is different */
604 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
605 return put_int(argp, block_size(bdev));
607 return blkdev_bszset(bdev, mode, argp);
609 return put_u64(argp, i_size_read(bdev->bd_inode));
611 /* Incompatible alignment on i386 */
613 return blk_trace_ioctl(bdev, cmd, argp);
618 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
619 if (ret == -ENOIOCTLCMD)
620 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
624 EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
628 #define BLKBSZGET_32 _IOR(0x12, 112, int)
629 #define BLKBSZSET_32 _IOW(0x12, 113, int)
630 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
632 /* Most of the generic ioctls are handled in the normal fallback path.
633 This assumes the blkdev's low level compat_ioctl always returns
634 ENOIOCTLCMD for unknown ioctls. */
635 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
638 void __user *argp = compat_ptr(arg);
639 struct inode *inode = file->f_mapping->host;
640 struct block_device *bdev = inode->i_bdev;
641 struct gendisk *disk = bdev->bd_disk;
642 fmode_t mode = file->f_mode;
646 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
647 * to updated it before every ioctl.
649 if (file->f_flags & O_NDELAY)
650 mode |= FMODE_NDELAY;
652 mode &= ~FMODE_NDELAY;
655 /* These need separate implementations for the data structure */
657 return compat_hdio_getgeo(bdev, argp);
659 return compat_blkpg_ioctl(bdev, argp);
661 /* Compat mode returns 32-bit data instead of 'long' */
666 return compat_put_long(argp,
667 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
669 size = i_size_read(bdev->bd_inode);
670 if ((size >> 9) > ~0UL)
672 return compat_put_ulong(argp, size >> 9);
674 /* The data is compatible, but the command number is different */
675 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
676 return put_int(argp, bdev_logical_block_size(bdev));
678 return blkdev_bszset(bdev, mode, argp);
679 case BLKGETSIZE64_32:
680 return put_u64(argp, i_size_read(bdev->bd_inode));
682 /* Incompatible alignment on i386 */
683 case BLKTRACESETUP32:
684 return blk_trace_ioctl(bdev, cmd, argp);
689 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
690 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
691 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);