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 (disk->flags & GENHD_FL_NO_PART)
25 if (!capable(CAP_SYS_ADMIN))
27 if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
29 if (bdev_is_partition(bdev))
35 if (op == BLKPG_DEL_PARTITION)
36 return bdev_del_partition(disk, p.pno);
38 start = p.start >> SECTOR_SHIFT;
39 length = p.length >> SECTOR_SHIFT;
42 case BLKPG_ADD_PARTITION:
43 /* check if partition is aligned to blocksize */
44 if (p.start & (bdev_logical_block_size(bdev) - 1))
46 return bdev_add_partition(disk, p.pno, start, length);
47 case BLKPG_RESIZE_PARTITION:
48 return bdev_resize_partition(disk, p.pno, start, length);
54 static int blkpg_ioctl(struct block_device *bdev,
55 struct blkpg_ioctl_arg __user *arg)
57 struct blkpg_partition __user *udata;
60 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
63 return blkpg_do_ioctl(bdev, udata, op);
67 struct compat_blkpg_ioctl_arg {
74 static int compat_blkpg_ioctl(struct block_device *bdev,
75 struct compat_blkpg_ioctl_arg __user *arg)
80 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
83 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
87 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
92 struct inode *inode = bdev->bd_inode;
95 if (!(mode & FMODE_WRITE))
98 if (!bdev_max_discard_sectors(bdev))
101 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
112 if (start + len > bdev_nr_bytes(bdev))
115 filemap_invalidate_lock(inode->i_mapping);
116 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
119 err = blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
121 filemap_invalidate_unlock(inode->i_mapping);
125 static int blk_ioctl_secure_erase(struct block_device *bdev, fmode_t mode,
132 if (!(mode & FMODE_WRITE))
134 if (!bdev_max_secure_erase_sectors(bdev))
136 if (copy_from_user(range, argp, sizeof(range)))
141 if ((start & 511) || (len & 511))
143 if (start + len > bdev_nr_bytes(bdev))
146 filemap_invalidate_lock(bdev->bd_inode->i_mapping);
147 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
149 err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9,
151 filemap_invalidate_unlock(bdev->bd_inode->i_mapping);
156 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
160 uint64_t start, end, len;
161 struct inode *inode = bdev->bd_inode;
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)bdev_nr_bytes(bdev))
183 /* Invalidate the page cache, including dirty pages */
184 filemap_invalidate_lock(inode->i_mapping);
185 err = truncate_bdev_range(bdev, mode, start, end);
189 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
190 BLKDEV_ZERO_NOUNMAP);
193 filemap_invalidate_unlock(inode->i_mapping);
197 static int put_ushort(unsigned short __user *argp, unsigned short val)
199 return put_user(val, argp);
202 static int put_int(int __user *argp, int val)
204 return put_user(val, argp);
207 static int put_uint(unsigned int __user *argp, unsigned int val)
209 return put_user(val, argp);
212 static int put_long(long __user *argp, long val)
214 return put_user(val, argp);
217 static int put_ulong(unsigned long __user *argp, unsigned long val)
219 return put_user(val, argp);
222 static int put_u64(u64 __user *argp, u64 val)
224 return put_user(val, argp);
228 static int compat_put_long(compat_long_t __user *argp, long val)
230 return put_user(val, argp);
233 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
235 return put_user(val, argp);
241 * This is the equivalent of compat_ptr_ioctl(), to be used by block
242 * drivers that implement only commands that are completely compatible
243 * between 32-bit and 64-bit user space
245 int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
246 unsigned cmd, unsigned long arg)
248 struct gendisk *disk = bdev->bd_disk;
250 if (disk->fops->ioctl)
251 return disk->fops->ioctl(bdev, mode, cmd,
252 (unsigned long)compat_ptr(arg));
256 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
259 static int blkdev_pr_register(struct block_device *bdev,
260 struct pr_registration __user *arg)
262 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
263 struct pr_registration reg;
265 if (!capable(CAP_SYS_ADMIN))
267 if (!ops || !ops->pr_register)
269 if (copy_from_user(®, arg, sizeof(reg)))
272 if (reg.flags & ~PR_FL_IGNORE_KEY)
274 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
277 static int blkdev_pr_reserve(struct block_device *bdev,
278 struct pr_reservation __user *arg)
280 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
281 struct pr_reservation rsv;
283 if (!capable(CAP_SYS_ADMIN))
285 if (!ops || !ops->pr_reserve)
287 if (copy_from_user(&rsv, arg, sizeof(rsv)))
290 if (rsv.flags & ~PR_FL_IGNORE_KEY)
292 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
295 static int blkdev_pr_release(struct block_device *bdev,
296 struct pr_reservation __user *arg)
298 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
299 struct pr_reservation rsv;
301 if (!capable(CAP_SYS_ADMIN))
303 if (!ops || !ops->pr_release)
305 if (copy_from_user(&rsv, arg, sizeof(rsv)))
310 return ops->pr_release(bdev, rsv.key, rsv.type);
313 static int blkdev_pr_preempt(struct block_device *bdev,
314 struct pr_preempt __user *arg, bool abort)
316 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
319 if (!capable(CAP_SYS_ADMIN))
321 if (!ops || !ops->pr_preempt)
323 if (copy_from_user(&p, arg, sizeof(p)))
328 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
331 static int blkdev_pr_clear(struct block_device *bdev,
332 struct pr_clear __user *arg)
334 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
337 if (!capable(CAP_SYS_ADMIN))
339 if (!ops || !ops->pr_clear)
341 if (copy_from_user(&c, arg, sizeof(c)))
346 return ops->pr_clear(bdev, c.key);
349 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
350 unsigned cmd, unsigned long arg)
352 if (!capable(CAP_SYS_ADMIN))
355 invalidate_bdev(bdev);
359 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
360 unsigned cmd, unsigned long arg)
364 if (!capable(CAP_SYS_ADMIN))
367 if (get_user(n, (int __user *)arg))
369 if (bdev->bd_disk->fops->set_read_only) {
370 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
374 bdev->bd_read_only = n;
378 static int blkdev_getgeo(struct block_device *bdev,
379 struct hd_geometry __user *argp)
381 struct gendisk *disk = bdev->bd_disk;
382 struct hd_geometry geo;
387 if (!disk->fops->getgeo)
391 * We need to set the startsect first, the driver may
392 * want to override it.
394 memset(&geo, 0, sizeof(geo));
395 geo.start = get_start_sect(bdev);
396 ret = disk->fops->getgeo(bdev, &geo);
399 if (copy_to_user(argp, &geo, sizeof(geo)))
405 struct compat_hd_geometry {
407 unsigned char sectors;
408 unsigned short cylinders;
412 static int compat_hdio_getgeo(struct block_device *bdev,
413 struct compat_hd_geometry __user *ugeo)
415 struct gendisk *disk = bdev->bd_disk;
416 struct hd_geometry geo;
421 if (!disk->fops->getgeo)
424 memset(&geo, 0, sizeof(geo));
426 * We need to set the startsect first, the driver may
427 * want to override it.
429 geo.start = get_start_sect(bdev);
430 ret = disk->fops->getgeo(bdev, &geo);
434 ret = copy_to_user(ugeo, &geo, 4);
435 ret |= put_user(geo.start, &ugeo->start);
443 /* set the logical block size */
444 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
449 if (!capable(CAP_SYS_ADMIN))
453 if (get_user(n, argp))
456 if (mode & FMODE_EXCL)
457 return set_blocksize(bdev, n);
459 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
461 ret = set_blocksize(bdev, n);
462 blkdev_put(bdev, mode | FMODE_EXCL);
468 * Common commands that are handled the same way on native and compat
469 * user space. Note the separate arg/argp parameters that are needed
470 * to deal with the compat_ptr() conversion.
472 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
473 unsigned int cmd, unsigned long arg,
476 unsigned int max_sectors;
480 return blkdev_flushbuf(bdev, mode, cmd, arg);
482 return blkdev_roset(bdev, mode, cmd, arg);
484 return blk_ioctl_discard(bdev, mode, arg);
486 return blk_ioctl_secure_erase(bdev, mode, argp);
488 return blk_ioctl_zeroout(bdev, mode, arg);
490 return put_u64(argp, bdev->bd_disk->diskseq);
492 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
497 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
499 return put_uint(argp, bdev_zone_sectors(bdev));
501 return put_uint(argp, bdev_nr_zones(bdev));
503 return put_int(argp, bdev_read_only(bdev) != 0);
504 case BLKSSZGET: /* get block device logical block size */
505 return put_int(argp, bdev_logical_block_size(bdev));
506 case BLKPBSZGET: /* get block device physical block size */
507 return put_uint(argp, bdev_physical_block_size(bdev));
509 return put_uint(argp, bdev_io_min(bdev));
511 return put_uint(argp, bdev_io_opt(bdev));
513 return put_int(argp, bdev_alignment_offset(bdev));
514 case BLKDISCARDZEROES:
515 return put_uint(argp, 0);
517 max_sectors = min_t(unsigned int, USHRT_MAX,
518 queue_max_sectors(bdev_get_queue(bdev)));
519 return put_ushort(argp, max_sectors);
521 return put_ushort(argp, !bdev_nonrot(bdev));
524 if(!capable(CAP_SYS_ADMIN))
526 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
529 if (!capable(CAP_SYS_ADMIN))
531 if (bdev_is_partition(bdev))
533 return disk_scan_partitions(bdev->bd_disk, mode);
536 case BLKTRACETEARDOWN:
537 return blk_trace_ioctl(bdev, cmd, argp);
538 case IOC_PR_REGISTER:
539 return blkdev_pr_register(bdev, argp);
541 return blkdev_pr_reserve(bdev, argp);
543 return blkdev_pr_release(bdev, argp);
545 return blkdev_pr_preempt(bdev, argp, false);
546 case IOC_PR_PREEMPT_ABORT:
547 return blkdev_pr_preempt(bdev, argp, true);
549 return blkdev_pr_clear(bdev, argp);
556 * Always keep this in sync with compat_blkdev_ioctl()
557 * to handle all incompatible commands in both functions.
559 * New commands must be compatible and go into blkdev_common_ioctl
561 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
563 struct block_device *bdev = I_BDEV(file->f_mapping->host);
564 void __user *argp = (void __user *)arg;
565 fmode_t mode = file->f_mode;
569 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
570 * to updated it before every ioctl.
572 if (file->f_flags & O_NDELAY)
573 mode |= FMODE_NDELAY;
575 mode &= ~FMODE_NDELAY;
578 /* These need separate implementations for the data structure */
580 return blkdev_getgeo(bdev, argp);
582 return blkpg_ioctl(bdev, argp);
584 /* Compat mode returns 32-bit data instead of 'long' */
589 return put_long(argp,
590 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
592 if (bdev_nr_sectors(bdev) > ~0UL)
594 return put_ulong(argp, bdev_nr_sectors(bdev));
596 /* The data is compatible, but the command number is different */
597 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
598 return put_int(argp, block_size(bdev));
600 return blkdev_bszset(bdev, mode, argp);
602 return put_u64(argp, bdev_nr_bytes(bdev));
604 /* Incompatible alignment on i386 */
606 return blk_trace_ioctl(bdev, cmd, argp);
611 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
612 if (ret != -ENOIOCTLCMD)
615 if (!bdev->bd_disk->fops->ioctl)
617 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
622 #define BLKBSZGET_32 _IOR(0x12, 112, int)
623 #define BLKBSZSET_32 _IOW(0x12, 113, int)
624 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
626 /* Most of the generic ioctls are handled in the normal fallback path.
627 This assumes the blkdev's low level compat_ioctl always returns
628 ENOIOCTLCMD for unknown ioctls. */
629 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
632 void __user *argp = compat_ptr(arg);
633 struct block_device *bdev = I_BDEV(file->f_mapping->host);
634 struct gendisk *disk = bdev->bd_disk;
635 fmode_t mode = file->f_mode;
638 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
639 * to updated it before every ioctl.
641 if (file->f_flags & O_NDELAY)
642 mode |= FMODE_NDELAY;
644 mode &= ~FMODE_NDELAY;
647 /* These need separate implementations for the data structure */
649 return compat_hdio_getgeo(bdev, argp);
651 return compat_blkpg_ioctl(bdev, argp);
653 /* Compat mode returns 32-bit data instead of 'long' */
658 return compat_put_long(argp,
659 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
661 if (bdev_nr_sectors(bdev) > ~(compat_ulong_t)0)
663 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
665 /* The data is compatible, but the command number is different */
666 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
667 return put_int(argp, bdev_logical_block_size(bdev));
669 return blkdev_bszset(bdev, mode, argp);
670 case BLKGETSIZE64_32:
671 return put_u64(argp, bdev_nr_bytes(bdev));
673 /* Incompatible alignment on i386 */
674 case BLKTRACESETUP32:
675 return blk_trace_ioctl(bdev, cmd, argp);
680 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
681 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
682 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);