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);
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))
138 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
142 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
146 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
150 uint64_t start, end, len;
153 if (!(mode & FMODE_WRITE))
156 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
161 end = start + len - 1;
167 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
172 /* Invalidate the page cache, including dirty pages */
173 err = truncate_bdev_range(bdev, mode, start, end);
177 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
178 BLKDEV_ZERO_NOUNMAP);
181 static int put_ushort(unsigned short __user *argp, unsigned short val)
183 return put_user(val, argp);
186 static int put_int(int __user *argp, int val)
188 return put_user(val, argp);
191 static int put_uint(unsigned int __user *argp, unsigned int val)
193 return put_user(val, argp);
196 static int put_long(long __user *argp, long val)
198 return put_user(val, argp);
201 static int put_ulong(unsigned long __user *argp, unsigned long val)
203 return put_user(val, argp);
206 static int put_u64(u64 __user *argp, u64 val)
208 return put_user(val, argp);
212 static int compat_put_long(compat_long_t __user *argp, long val)
214 return put_user(val, argp);
217 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
219 return put_user(val, argp);
225 * This is the equivalent of compat_ptr_ioctl(), to be used by block
226 * drivers that implement only commands that are completely compatible
227 * between 32-bit and 64-bit user space
229 int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
230 unsigned cmd, unsigned long arg)
232 struct gendisk *disk = bdev->bd_disk;
234 if (disk->fops->ioctl)
235 return disk->fops->ioctl(bdev, mode, cmd,
236 (unsigned long)compat_ptr(arg));
240 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
243 static int blkdev_pr_register(struct block_device *bdev,
244 struct pr_registration __user *arg)
246 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
247 struct pr_registration reg;
249 if (!capable(CAP_SYS_ADMIN))
251 if (!ops || !ops->pr_register)
253 if (copy_from_user(®, arg, sizeof(reg)))
256 if (reg.flags & ~PR_FL_IGNORE_KEY)
258 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
261 static int blkdev_pr_reserve(struct block_device *bdev,
262 struct pr_reservation __user *arg)
264 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
265 struct pr_reservation rsv;
267 if (!capable(CAP_SYS_ADMIN))
269 if (!ops || !ops->pr_reserve)
271 if (copy_from_user(&rsv, arg, sizeof(rsv)))
274 if (rsv.flags & ~PR_FL_IGNORE_KEY)
276 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
279 static int blkdev_pr_release(struct block_device *bdev,
280 struct pr_reservation __user *arg)
282 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
283 struct pr_reservation rsv;
285 if (!capable(CAP_SYS_ADMIN))
287 if (!ops || !ops->pr_release)
289 if (copy_from_user(&rsv, arg, sizeof(rsv)))
294 return ops->pr_release(bdev, rsv.key, rsv.type);
297 static int blkdev_pr_preempt(struct block_device *bdev,
298 struct pr_preempt __user *arg, bool abort)
300 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
303 if (!capable(CAP_SYS_ADMIN))
305 if (!ops || !ops->pr_preempt)
307 if (copy_from_user(&p, arg, sizeof(p)))
312 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
315 static int blkdev_pr_clear(struct block_device *bdev,
316 struct pr_clear __user *arg)
318 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
321 if (!capable(CAP_SYS_ADMIN))
323 if (!ops || !ops->pr_clear)
325 if (copy_from_user(&c, arg, sizeof(c)))
330 return ops->pr_clear(bdev, c.key);
333 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
334 unsigned cmd, unsigned long arg)
336 if (!capable(CAP_SYS_ADMIN))
339 invalidate_bdev(bdev);
343 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
344 unsigned cmd, unsigned long arg)
348 if (!capable(CAP_SYS_ADMIN))
351 if (get_user(n, (int __user *)arg))
353 if (bdev->bd_disk->fops->set_read_only) {
354 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
358 bdev->bd_read_only = n;
362 static int blkdev_getgeo(struct block_device *bdev,
363 struct hd_geometry __user *argp)
365 struct gendisk *disk = bdev->bd_disk;
366 struct hd_geometry geo;
371 if (!disk->fops->getgeo)
375 * We need to set the startsect first, the driver may
376 * want to override it.
378 memset(&geo, 0, sizeof(geo));
379 geo.start = get_start_sect(bdev);
380 ret = disk->fops->getgeo(bdev, &geo);
383 if (copy_to_user(argp, &geo, sizeof(geo)))
389 struct compat_hd_geometry {
391 unsigned char sectors;
392 unsigned short cylinders;
396 static int compat_hdio_getgeo(struct block_device *bdev,
397 struct compat_hd_geometry __user *ugeo)
399 struct gendisk *disk = bdev->bd_disk;
400 struct hd_geometry geo;
405 if (!disk->fops->getgeo)
408 memset(&geo, 0, sizeof(geo));
410 * We need to set the startsect first, the driver may
411 * want to override it.
413 geo.start = get_start_sect(bdev);
414 ret = disk->fops->getgeo(bdev, &geo);
418 ret = copy_to_user(ugeo, &geo, 4);
419 ret |= put_user(geo.start, &ugeo->start);
427 /* set the logical block size */
428 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
433 if (!capable(CAP_SYS_ADMIN))
437 if (get_user(n, argp))
440 if (mode & FMODE_EXCL)
441 return set_blocksize(bdev, n);
443 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
445 ret = set_blocksize(bdev, n);
446 blkdev_put(bdev, mode | FMODE_EXCL);
452 * Common commands that are handled the same way on native and compat
453 * user space. Note the separate arg/argp parameters that are needed
454 * to deal with the compat_ptr() conversion.
456 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
457 unsigned cmd, unsigned long arg, void __user *argp)
459 unsigned int max_sectors;
463 return blkdev_flushbuf(bdev, mode, cmd, arg);
465 return blkdev_roset(bdev, mode, cmd, arg);
467 return blk_ioctl_discard(bdev, mode, arg, 0);
469 return blk_ioctl_discard(bdev, mode, arg,
470 BLKDEV_DISCARD_SECURE);
472 return blk_ioctl_zeroout(bdev, mode, arg);
474 return put_u64(argp, bdev->bd_disk->diskseq);
476 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
481 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
483 return put_uint(argp, bdev_zone_sectors(bdev));
485 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
487 return put_int(argp, bdev_read_only(bdev) != 0);
488 case BLKSSZGET: /* get block device logical block size */
489 return put_int(argp, bdev_logical_block_size(bdev));
490 case BLKPBSZGET: /* get block device physical block size */
491 return put_uint(argp, bdev_physical_block_size(bdev));
493 return put_uint(argp, bdev_io_min(bdev));
495 return put_uint(argp, bdev_io_opt(bdev));
497 return put_int(argp, bdev_alignment_offset(bdev));
498 case BLKDISCARDZEROES:
499 return put_uint(argp, 0);
501 max_sectors = min_t(unsigned int, USHRT_MAX,
502 queue_max_sectors(bdev_get_queue(bdev)));
503 return put_ushort(argp, max_sectors);
505 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
508 if(!capable(CAP_SYS_ADMIN))
510 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
513 return blkdev_reread_part(bdev, mode);
516 case BLKTRACETEARDOWN:
517 return blk_trace_ioctl(bdev, cmd, argp);
518 case IOC_PR_REGISTER:
519 return blkdev_pr_register(bdev, argp);
521 return blkdev_pr_reserve(bdev, argp);
523 return blkdev_pr_release(bdev, argp);
525 return blkdev_pr_preempt(bdev, argp, false);
526 case IOC_PR_PREEMPT_ABORT:
527 return blkdev_pr_preempt(bdev, argp, true);
529 return blkdev_pr_clear(bdev, argp);
536 * Always keep this in sync with compat_blkdev_ioctl()
537 * to handle all incompatible commands in both functions.
539 * New commands must be compatible and go into blkdev_common_ioctl
541 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
546 void __user *argp = (void __user *)arg;
549 /* These need separate implementations for the data structure */
551 return blkdev_getgeo(bdev, argp);
553 return blkpg_ioctl(bdev, argp);
555 /* Compat mode returns 32-bit data instead of 'long' */
560 return put_long(argp,
561 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
563 size = i_size_read(bdev->bd_inode);
564 if ((size >> 9) > ~0UL)
566 return put_ulong(argp, size >> 9);
568 /* The data is compatible, but the command number is different */
569 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
570 return put_int(argp, block_size(bdev));
572 return blkdev_bszset(bdev, mode, argp);
574 return put_u64(argp, i_size_read(bdev->bd_inode));
576 /* Incompatible alignment on i386 */
578 return blk_trace_ioctl(bdev, cmd, argp);
583 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
584 if (ret != -ENOIOCTLCMD)
587 if (!bdev->bd_disk->fops->ioctl)
589 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
591 EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
595 #define BLKBSZGET_32 _IOR(0x12, 112, int)
596 #define BLKBSZSET_32 _IOW(0x12, 113, int)
597 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
599 /* Most of the generic ioctls are handled in the normal fallback path.
600 This assumes the blkdev's low level compat_ioctl always returns
601 ENOIOCTLCMD for unknown ioctls. */
602 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
605 void __user *argp = compat_ptr(arg);
606 struct block_device *bdev = I_BDEV(file->f_mapping->host);
607 struct gendisk *disk = bdev->bd_disk;
608 fmode_t mode = file->f_mode;
612 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
613 * to updated it before every ioctl.
615 if (file->f_flags & O_NDELAY)
616 mode |= FMODE_NDELAY;
618 mode &= ~FMODE_NDELAY;
621 /* These need separate implementations for the data structure */
623 return compat_hdio_getgeo(bdev, argp);
625 return compat_blkpg_ioctl(bdev, argp);
627 /* Compat mode returns 32-bit data instead of 'long' */
632 return compat_put_long(argp,
633 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
635 size = i_size_read(bdev->bd_inode);
636 if ((size >> 9) > ~0UL)
638 return compat_put_ulong(argp, size >> 9);
640 /* The data is compatible, but the command number is different */
641 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
642 return put_int(argp, bdev_logical_block_size(bdev));
644 return blkdev_bszset(bdev, mode, argp);
645 case BLKGETSIZE64_32:
646 return put_u64(argp, i_size_read(bdev->bd_inode));
648 /* Incompatible alignment on i386 */
649 case BLKTRACESETUP32:
650 return blk_trace_ioctl(bdev, cmd, argp);
655 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
656 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
657 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);