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 sector_t start, length, capacity, end;
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 if (p.start < 0 || p.length <= 0 || LLONG_MAX - p.length < p.start)
38 /* Check that the partition is aligned to the block size */
39 if (!IS_ALIGNED(p.start | p.length, bdev_logical_block_size(bdev)))
42 start = p.start >> SECTOR_SHIFT;
43 length = p.length >> SECTOR_SHIFT;
44 capacity = get_capacity(disk);
46 if (check_add_overflow(start, length, &end))
49 if (start >= capacity || end > capacity)
53 case BLKPG_ADD_PARTITION:
54 return bdev_add_partition(disk, p.pno, start, length);
55 case BLKPG_RESIZE_PARTITION:
56 return bdev_resize_partition(disk, p.pno, start, length);
62 static int blkpg_ioctl(struct block_device *bdev,
63 struct blkpg_ioctl_arg __user *arg)
65 struct blkpg_partition __user *udata;
68 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
71 return blkpg_do_ioctl(bdev, udata, op);
75 struct compat_blkpg_ioctl_arg {
82 static int compat_blkpg_ioctl(struct block_device *bdev,
83 struct compat_blkpg_ioctl_arg __user *arg)
88 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
91 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
95 static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode,
98 unsigned int bs_mask = bdev_logical_block_size(bdev) - 1;
99 struct inode *inode = bdev->bd_inode;
100 uint64_t range[2], start, len, end;
101 struct bio *prev = NULL, *bio;
102 sector_t sector, nr_sects;
103 struct blk_plug plug;
106 if (!(mode & BLK_OPEN_WRITE))
109 if (!bdev_max_discard_sectors(bdev))
111 if (bdev_read_only(bdev))
114 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
122 if ((start | len) & bs_mask)
125 if (check_add_overflow(start, len, &end) ||
126 end > bdev_nr_bytes(bdev))
129 filemap_invalidate_lock(inode->i_mapping);
130 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
134 sector = start >> SECTOR_SHIFT;
135 nr_sects = len >> SECTOR_SHIFT;
137 blk_start_plug(&plug);
139 if (fatal_signal_pending(current)) {
141 bio_await_chain(prev);
145 bio = blk_alloc_discard_bio(bdev, §or, &nr_sects,
149 prev = bio_chain_and_submit(prev, bio);
152 err = submit_bio_wait(prev);
153 if (err == -EOPNOTSUPP)
158 blk_finish_plug(&plug);
160 filemap_invalidate_unlock(inode->i_mapping);
164 static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode,
171 if (!(mode & BLK_OPEN_WRITE))
173 if (!bdev_max_secure_erase_sectors(bdev))
175 if (copy_from_user(range, argp, sizeof(range)))
180 if ((start & 511) || (len & 511))
182 if (start + len > bdev_nr_bytes(bdev))
185 filemap_invalidate_lock(bdev->bd_inode->i_mapping);
186 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
188 err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9,
190 filemap_invalidate_unlock(bdev->bd_inode->i_mapping);
195 static int blk_ioctl_zeroout(struct block_device *bdev, blk_mode_t mode,
199 uint64_t start, end, len;
200 struct inode *inode = bdev->bd_inode;
203 if (!(mode & BLK_OPEN_WRITE))
206 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
211 end = start + len - 1;
217 if (end >= (uint64_t)bdev_nr_bytes(bdev))
222 /* Invalidate the page cache, including dirty pages */
223 filemap_invalidate_lock(inode->i_mapping);
224 err = truncate_bdev_range(bdev, mode, start, end);
228 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
229 BLKDEV_ZERO_NOUNMAP);
232 filemap_invalidate_unlock(inode->i_mapping);
236 static int put_ushort(unsigned short __user *argp, unsigned short val)
238 return put_user(val, argp);
241 static int put_int(int __user *argp, int val)
243 return put_user(val, argp);
246 static int put_uint(unsigned int __user *argp, unsigned int val)
248 return put_user(val, argp);
251 static int put_long(long __user *argp, long val)
253 return put_user(val, argp);
256 static int put_ulong(unsigned long __user *argp, unsigned long val)
258 return put_user(val, argp);
261 static int put_u64(u64 __user *argp, u64 val)
263 return put_user(val, argp);
267 static int compat_put_long(compat_long_t __user *argp, long val)
269 return put_user(val, argp);
272 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
274 return put_user(val, argp);
280 * This is the equivalent of compat_ptr_ioctl(), to be used by block
281 * drivers that implement only commands that are completely compatible
282 * between 32-bit and 64-bit user space
284 int blkdev_compat_ptr_ioctl(struct block_device *bdev, blk_mode_t mode,
285 unsigned cmd, unsigned long arg)
287 struct gendisk *disk = bdev->bd_disk;
289 if (disk->fops->ioctl)
290 return disk->fops->ioctl(bdev, mode, cmd,
291 (unsigned long)compat_ptr(arg));
295 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
298 static bool blkdev_pr_allowed(struct block_device *bdev, blk_mode_t mode)
300 /* no sense to make reservations for partitions */
301 if (bdev_is_partition(bdev))
304 if (capable(CAP_SYS_ADMIN))
307 * Only allow unprivileged reservations if the file descriptor is open
310 return mode & BLK_OPEN_WRITE;
313 static int blkdev_pr_register(struct block_device *bdev, blk_mode_t mode,
314 struct pr_registration __user *arg)
316 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
317 struct pr_registration reg;
319 if (!blkdev_pr_allowed(bdev, mode))
321 if (!ops || !ops->pr_register)
323 if (copy_from_user(®, arg, sizeof(reg)))
326 if (reg.flags & ~PR_FL_IGNORE_KEY)
328 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
331 static int blkdev_pr_reserve(struct block_device *bdev, blk_mode_t mode,
332 struct pr_reservation __user *arg)
334 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
335 struct pr_reservation rsv;
337 if (!blkdev_pr_allowed(bdev, mode))
339 if (!ops || !ops->pr_reserve)
341 if (copy_from_user(&rsv, arg, sizeof(rsv)))
344 if (rsv.flags & ~PR_FL_IGNORE_KEY)
346 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
349 static int blkdev_pr_release(struct block_device *bdev, blk_mode_t mode,
350 struct pr_reservation __user *arg)
352 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
353 struct pr_reservation rsv;
355 if (!blkdev_pr_allowed(bdev, mode))
357 if (!ops || !ops->pr_release)
359 if (copy_from_user(&rsv, arg, sizeof(rsv)))
364 return ops->pr_release(bdev, rsv.key, rsv.type);
367 static int blkdev_pr_preempt(struct block_device *bdev, blk_mode_t mode,
368 struct pr_preempt __user *arg, bool abort)
370 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
373 if (!blkdev_pr_allowed(bdev, mode))
375 if (!ops || !ops->pr_preempt)
377 if (copy_from_user(&p, arg, sizeof(p)))
382 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
385 static int blkdev_pr_clear(struct block_device *bdev, blk_mode_t mode,
386 struct pr_clear __user *arg)
388 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
391 if (!blkdev_pr_allowed(bdev, mode))
393 if (!ops || !ops->pr_clear)
395 if (copy_from_user(&c, arg, sizeof(c)))
400 return ops->pr_clear(bdev, c.key);
403 static int blkdev_flushbuf(struct block_device *bdev, unsigned cmd,
406 if (!capable(CAP_SYS_ADMIN))
409 mutex_lock(&bdev->bd_holder_lock);
410 if (bdev->bd_holder_ops && bdev->bd_holder_ops->sync)
411 bdev->bd_holder_ops->sync(bdev);
413 mutex_unlock(&bdev->bd_holder_lock);
417 invalidate_bdev(bdev);
421 static int blkdev_roset(struct block_device *bdev, unsigned cmd,
426 if (!capable(CAP_SYS_ADMIN))
429 if (get_user(n, (int __user *)arg))
431 if (bdev->bd_disk->fops->set_read_only) {
432 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
436 bdev->bd_read_only = n;
440 static int blkdev_getgeo(struct block_device *bdev,
441 struct hd_geometry __user *argp)
443 struct gendisk *disk = bdev->bd_disk;
444 struct hd_geometry geo;
449 if (!disk->fops->getgeo)
453 * We need to set the startsect first, the driver may
454 * want to override it.
456 memset(&geo, 0, sizeof(geo));
457 geo.start = get_start_sect(bdev);
458 ret = disk->fops->getgeo(bdev, &geo);
461 if (copy_to_user(argp, &geo, sizeof(geo)))
467 struct compat_hd_geometry {
469 unsigned char sectors;
470 unsigned short cylinders;
474 static int compat_hdio_getgeo(struct block_device *bdev,
475 struct compat_hd_geometry __user *ugeo)
477 struct gendisk *disk = bdev->bd_disk;
478 struct hd_geometry geo;
483 if (!disk->fops->getgeo)
486 memset(&geo, 0, sizeof(geo));
488 * We need to set the startsect first, the driver may
489 * want to override it.
491 geo.start = get_start_sect(bdev);
492 ret = disk->fops->getgeo(bdev, &geo);
496 ret = copy_to_user(ugeo, &geo, 4);
497 ret |= put_user(geo.start, &ugeo->start);
505 /* set the logical block size */
506 static int blkdev_bszset(struct block_device *bdev, blk_mode_t mode,
512 if (!capable(CAP_SYS_ADMIN))
516 if (get_user(n, argp))
519 if (mode & BLK_OPEN_EXCL)
520 return set_blocksize(bdev, n);
522 file = bdev_file_open_by_dev(bdev->bd_dev, mode, &bdev, NULL);
525 ret = set_blocksize(bdev, n);
531 * Common commands that are handled the same way on native and compat
532 * user space. Note the separate arg/argp parameters that are needed
533 * to deal with the compat_ptr() conversion.
535 static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,
536 unsigned int cmd, unsigned long arg,
539 unsigned int max_sectors;
543 return blkdev_flushbuf(bdev, cmd, arg);
545 return blkdev_roset(bdev, cmd, arg);
547 return blk_ioctl_discard(bdev, mode, arg);
549 return blk_ioctl_secure_erase(bdev, mode, argp);
551 return blk_ioctl_zeroout(bdev, mode, arg);
553 return put_u64(argp, bdev->bd_disk->diskseq);
555 return blkdev_report_zones_ioctl(bdev, cmd, arg);
560 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
562 return put_uint(argp, bdev_zone_sectors(bdev));
564 return put_uint(argp, bdev_nr_zones(bdev));
566 return put_int(argp, bdev_read_only(bdev) != 0);
567 case BLKSSZGET: /* get block device logical block size */
568 return put_int(argp, bdev_logical_block_size(bdev));
569 case BLKPBSZGET: /* get block device physical block size */
570 return put_uint(argp, bdev_physical_block_size(bdev));
572 return put_uint(argp, bdev_io_min(bdev));
574 return put_uint(argp, bdev_io_opt(bdev));
576 return put_int(argp, bdev_alignment_offset(bdev));
577 case BLKDISCARDZEROES:
578 return put_uint(argp, 0);
580 max_sectors = min_t(unsigned int, USHRT_MAX,
581 queue_max_sectors(bdev_get_queue(bdev)));
582 return put_ushort(argp, max_sectors);
584 return put_ushort(argp, !bdev_nonrot(bdev));
587 if(!capable(CAP_SYS_ADMIN))
589 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
592 if (!capable(CAP_SYS_ADMIN))
594 if (bdev_is_partition(bdev))
596 return disk_scan_partitions(bdev->bd_disk,
597 mode | BLK_OPEN_STRICT_SCAN);
600 case BLKTRACETEARDOWN:
601 return blk_trace_ioctl(bdev, cmd, argp);
602 case IOC_PR_REGISTER:
603 return blkdev_pr_register(bdev, mode, argp);
605 return blkdev_pr_reserve(bdev, mode, argp);
607 return blkdev_pr_release(bdev, mode, argp);
609 return blkdev_pr_preempt(bdev, mode, argp, false);
610 case IOC_PR_PREEMPT_ABORT:
611 return blkdev_pr_preempt(bdev, mode, argp, true);
613 return blkdev_pr_clear(bdev, mode, argp);
620 * Always keep this in sync with compat_blkdev_ioctl()
621 * to handle all incompatible commands in both functions.
623 * New commands must be compatible and go into blkdev_common_ioctl
625 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
627 struct block_device *bdev = I_BDEV(file->f_mapping->host);
628 void __user *argp = (void __user *)arg;
629 blk_mode_t mode = file_to_blk_mode(file);
633 /* These need separate implementations for the data structure */
635 return blkdev_getgeo(bdev, argp);
637 return blkpg_ioctl(bdev, argp);
639 /* Compat mode returns 32-bit data instead of 'long' */
644 return put_long(argp,
645 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
647 if (bdev_nr_sectors(bdev) > ~0UL)
649 return put_ulong(argp, bdev_nr_sectors(bdev));
651 /* The data is compatible, but the command number is different */
652 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
653 return put_int(argp, block_size(bdev));
655 return blkdev_bszset(bdev, mode, argp);
657 return put_u64(argp, bdev_nr_bytes(bdev));
659 /* Incompatible alignment on i386 */
661 return blk_trace_ioctl(bdev, cmd, argp);
666 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
667 if (ret != -ENOIOCTLCMD)
670 if (!bdev->bd_disk->fops->ioctl)
672 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
677 #define BLKBSZGET_32 _IOR(0x12, 112, int)
678 #define BLKBSZSET_32 _IOW(0x12, 113, int)
679 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
681 /* Most of the generic ioctls are handled in the normal fallback path.
682 This assumes the blkdev's low level compat_ioctl always returns
683 ENOIOCTLCMD for unknown ioctls. */
684 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
687 void __user *argp = compat_ptr(arg);
688 struct block_device *bdev = I_BDEV(file->f_mapping->host);
689 struct gendisk *disk = bdev->bd_disk;
690 blk_mode_t mode = file_to_blk_mode(file);
693 /* These need separate implementations for the data structure */
695 return compat_hdio_getgeo(bdev, argp);
697 return compat_blkpg_ioctl(bdev, argp);
699 /* Compat mode returns 32-bit data instead of 'long' */
704 return compat_put_long(argp,
705 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
707 if (bdev_nr_sectors(bdev) > ~(compat_ulong_t)0)
709 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
711 /* The data is compatible, but the command number is different */
712 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
713 return put_int(argp, bdev_logical_block_size(bdev));
715 return blkdev_bszset(bdev, mode, argp);
716 case BLKGETSIZE64_32:
717 return put_u64(argp, bdev_nr_bytes(bdev));
719 /* Incompatible alignment on i386 */
720 case BLKTRACESETUP32:
721 return blk_trace_ioctl(bdev, cmd, argp);
726 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
727 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
728 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);