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;
39 case BLKPG_ADD_PARTITION:
40 /* check if partition is aligned to blocksize */
41 if (p.start & (bdev_logical_block_size(bdev) - 1))
43 return bdev_add_partition(bdev, p.pno, start, length);
44 case BLKPG_RESIZE_PARTITION:
45 return bdev_resize_partition(bdev, p.pno, start, length);
51 static int blkpg_ioctl(struct block_device *bdev,
52 struct blkpg_ioctl_arg __user *arg)
54 struct blkpg_partition __user *udata;
57 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
60 return blkpg_do_ioctl(bdev, udata, op);
64 struct compat_blkpg_ioctl_arg {
71 static int compat_blkpg_ioctl(struct block_device *bdev,
72 struct compat_blkpg_ioctl_arg __user *arg)
77 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
80 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
84 static int blkdev_reread_part(struct block_device *bdev, fmode_t mode)
86 struct block_device *tmp;
88 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev))
90 if (!capable(CAP_SYS_ADMIN))
92 if (bdev->bd_part_count)
96 * Reopen the device to revalidate the driver state and force a
100 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
102 tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL);
105 blkdev_put(tmp, mode);
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);
117 if (!(mode & FMODE_WRITE))
120 if (!blk_queue_discard(q))
123 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
134 if (start + len > i_size_read(bdev->bd_inode))
137 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
141 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
145 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
149 uint64_t start, end, len;
152 if (!(mode & FMODE_WRITE))
155 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
160 end = start + len - 1;
166 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
171 /* Invalidate the page cache, including dirty pages */
172 err = truncate_bdev_range(bdev, mode, start, end);
176 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
177 BLKDEV_ZERO_NOUNMAP);
180 static int put_ushort(unsigned short __user *argp, unsigned short val)
182 return put_user(val, argp);
185 static int put_int(int __user *argp, int val)
187 return put_user(val, argp);
190 static int put_uint(unsigned int __user *argp, unsigned int val)
192 return put_user(val, argp);
195 static int put_long(long __user *argp, long val)
197 return put_user(val, argp);
200 static int put_ulong(unsigned long __user *argp, unsigned long val)
202 return put_user(val, argp);
205 static int put_u64(u64 __user *argp, u64 val)
207 return put_user(val, argp);
211 static int compat_put_long(compat_long_t __user *argp, long val)
213 return put_user(val, argp);
216 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
218 return put_user(val, argp);
224 * This is the equivalent of compat_ptr_ioctl(), to be used by block
225 * drivers that implement only commands that are completely compatible
226 * between 32-bit and 64-bit user space
228 int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
229 unsigned cmd, unsigned long arg)
231 struct gendisk *disk = bdev->bd_disk;
233 if (disk->fops->ioctl)
234 return disk->fops->ioctl(bdev, mode, cmd,
235 (unsigned long)compat_ptr(arg));
239 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
242 static int blkdev_pr_register(struct block_device *bdev,
243 struct pr_registration __user *arg)
245 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
246 struct pr_registration reg;
248 if (!capable(CAP_SYS_ADMIN))
250 if (!ops || !ops->pr_register)
252 if (copy_from_user(®, arg, sizeof(reg)))
255 if (reg.flags & ~PR_FL_IGNORE_KEY)
257 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
260 static int blkdev_pr_reserve(struct block_device *bdev,
261 struct pr_reservation __user *arg)
263 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
264 struct pr_reservation rsv;
266 if (!capable(CAP_SYS_ADMIN))
268 if (!ops || !ops->pr_reserve)
270 if (copy_from_user(&rsv, arg, sizeof(rsv)))
273 if (rsv.flags & ~PR_FL_IGNORE_KEY)
275 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
278 static int blkdev_pr_release(struct block_device *bdev,
279 struct pr_reservation __user *arg)
281 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
282 struct pr_reservation rsv;
284 if (!capable(CAP_SYS_ADMIN))
286 if (!ops || !ops->pr_release)
288 if (copy_from_user(&rsv, arg, sizeof(rsv)))
293 return ops->pr_release(bdev, rsv.key, rsv.type);
296 static int blkdev_pr_preempt(struct block_device *bdev,
297 struct pr_preempt __user *arg, bool abort)
299 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
302 if (!capable(CAP_SYS_ADMIN))
304 if (!ops || !ops->pr_preempt)
306 if (copy_from_user(&p, arg, sizeof(p)))
311 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
314 static int blkdev_pr_clear(struct block_device *bdev,
315 struct pr_clear __user *arg)
317 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
320 if (!capable(CAP_SYS_ADMIN))
322 if (!ops || !ops->pr_clear)
324 if (copy_from_user(&c, arg, sizeof(c)))
329 return ops->pr_clear(bdev, c.key);
332 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
333 unsigned cmd, unsigned long arg)
335 if (!capable(CAP_SYS_ADMIN))
338 invalidate_bdev(bdev);
342 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
343 unsigned cmd, unsigned long arg)
347 if (!capable(CAP_SYS_ADMIN))
350 if (get_user(n, (int __user *)arg))
352 if (bdev->bd_disk->fops->set_read_only) {
353 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
357 bdev->bd_read_only = n;
361 static int blkdev_getgeo(struct block_device *bdev,
362 struct hd_geometry __user *argp)
364 struct gendisk *disk = bdev->bd_disk;
365 struct hd_geometry geo;
370 if (!disk->fops->getgeo)
374 * We need to set the startsect first, the driver may
375 * want to override it.
377 memset(&geo, 0, sizeof(geo));
378 geo.start = get_start_sect(bdev);
379 ret = disk->fops->getgeo(bdev, &geo);
382 if (copy_to_user(argp, &geo, sizeof(geo)))
388 struct compat_hd_geometry {
390 unsigned char sectors;
391 unsigned short cylinders;
395 static int compat_hdio_getgeo(struct block_device *bdev,
396 struct compat_hd_geometry __user *ugeo)
398 struct gendisk *disk = bdev->bd_disk;
399 struct hd_geometry geo;
404 if (!disk->fops->getgeo)
407 memset(&geo, 0, sizeof(geo));
409 * We need to set the startsect first, the driver may
410 * want to override it.
412 geo.start = get_start_sect(bdev);
413 ret = disk->fops->getgeo(bdev, &geo);
417 ret = copy_to_user(ugeo, &geo, 4);
418 ret |= put_user(geo.start, &ugeo->start);
426 /* set the logical block size */
427 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
432 if (!capable(CAP_SYS_ADMIN))
436 if (get_user(n, argp))
439 if (mode & FMODE_EXCL)
440 return set_blocksize(bdev, n);
442 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
444 ret = set_blocksize(bdev, n);
445 blkdev_put(bdev, mode | FMODE_EXCL);
451 * Common commands that are handled the same way on native and compat
452 * user space. Note the separate arg/argp parameters that are needed
453 * to deal with the compat_ptr() conversion.
455 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
456 unsigned cmd, unsigned long arg, void __user *argp)
458 unsigned int max_sectors;
462 return blkdev_flushbuf(bdev, mode, cmd, arg);
464 return blkdev_roset(bdev, mode, cmd, arg);
466 return blk_ioctl_discard(bdev, mode, arg, 0);
468 return blk_ioctl_discard(bdev, mode, arg,
469 BLKDEV_DISCARD_SECURE);
471 return blk_ioctl_zeroout(bdev, mode, arg);
473 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
478 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
480 return put_uint(argp, bdev_zone_sectors(bdev));
482 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
484 return put_int(argp, bdev_read_only(bdev) != 0);
485 case BLKSSZGET: /* get block device logical block size */
486 return put_int(argp, bdev_logical_block_size(bdev));
487 case BLKPBSZGET: /* get block device physical block size */
488 return put_uint(argp, bdev_physical_block_size(bdev));
490 return put_uint(argp, bdev_io_min(bdev));
492 return put_uint(argp, bdev_io_opt(bdev));
494 return put_int(argp, bdev_alignment_offset(bdev));
495 case BLKDISCARDZEROES:
496 return put_uint(argp, 0);
498 max_sectors = min_t(unsigned int, USHRT_MAX,
499 queue_max_sectors(bdev_get_queue(bdev)));
500 return put_ushort(argp, max_sectors);
502 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
505 if(!capable(CAP_SYS_ADMIN))
507 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
510 return blkdev_reread_part(bdev, mode);
513 case BLKTRACETEARDOWN:
514 return blk_trace_ioctl(bdev, cmd, argp);
515 case IOC_PR_REGISTER:
516 return blkdev_pr_register(bdev, argp);
518 return blkdev_pr_reserve(bdev, argp);
520 return blkdev_pr_release(bdev, argp);
522 return blkdev_pr_preempt(bdev, argp, false);
523 case IOC_PR_PREEMPT_ABORT:
524 return blkdev_pr_preempt(bdev, argp, true);
526 return blkdev_pr_clear(bdev, argp);
533 * Always keep this in sync with compat_blkdev_ioctl()
534 * to handle all incompatible commands in both functions.
536 * New commands must be compatible and go into blkdev_common_ioctl
538 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
543 void __user *argp = (void __user *)arg;
546 /* These need separate implementations for the data structure */
548 return blkdev_getgeo(bdev, argp);
550 return blkpg_ioctl(bdev, argp);
552 /* Compat mode returns 32-bit data instead of 'long' */
557 return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
559 size = i_size_read(bdev->bd_inode);
560 if ((size >> 9) > ~0UL)
562 return put_ulong(argp, size >> 9);
564 /* The data is compatible, but the command number is different */
565 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
566 return put_int(argp, block_size(bdev));
568 return blkdev_bszset(bdev, mode, argp);
570 return put_u64(argp, i_size_read(bdev->bd_inode));
572 /* Incompatible alignment on i386 */
574 return blk_trace_ioctl(bdev, cmd, argp);
579 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
580 if (ret != -ENOIOCTLCMD)
583 if (!bdev->bd_disk->fops->ioctl)
585 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
587 EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
591 #define BLKBSZGET_32 _IOR(0x12, 112, int)
592 #define BLKBSZSET_32 _IOW(0x12, 113, int)
593 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
595 /* Most of the generic ioctls are handled in the normal fallback path.
596 This assumes the blkdev's low level compat_ioctl always returns
597 ENOIOCTLCMD for unknown ioctls. */
598 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
601 void __user *argp = compat_ptr(arg);
602 struct block_device *bdev = I_BDEV(file->f_mapping->host);
603 struct gendisk *disk = bdev->bd_disk;
604 fmode_t mode = file->f_mode;
608 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
609 * to updated it before every ioctl.
611 if (file->f_flags & O_NDELAY)
612 mode |= FMODE_NDELAY;
614 mode &= ~FMODE_NDELAY;
617 /* These need separate implementations for the data structure */
619 return compat_hdio_getgeo(bdev, argp);
621 return compat_blkpg_ioctl(bdev, argp);
623 /* Compat mode returns 32-bit data instead of 'long' */
628 return compat_put_long(argp,
629 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
631 size = i_size_read(bdev->bd_inode);
632 if ((size >> 9) > ~0UL)
634 return compat_put_ulong(argp, size >> 9);
636 /* The data is compatible, but the command number is different */
637 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
638 return put_int(argp, bdev_logical_block_size(bdev));
640 return blkdev_bszset(bdev, mode, argp);
641 case BLKGETSIZE64_32:
642 return put_u64(argp, i_size_read(bdev->bd_inode));
644 /* Incompatible alignment on i386 */
645 case BLKTRACESETUP32:
646 return blk_trace_ioctl(bdev, cmd, argp);
651 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
652 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
653 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);