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, blk_mode_t mode,
92 struct inode *inode = bdev->bd_inode;
95 if (!(mode & BLK_OPEN_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, blk_mode_t mode,
132 if (!(mode & BLK_OPEN_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, blk_mode_t mode,
160 uint64_t start, end, len;
161 struct inode *inode = bdev->bd_inode;
164 if (!(mode & BLK_OPEN_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, blk_mode_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 bool blkdev_pr_allowed(struct block_device *bdev, blk_mode_t mode)
261 /* no sense to make reservations for partitions */
262 if (bdev_is_partition(bdev))
265 if (capable(CAP_SYS_ADMIN))
268 * Only allow unprivileged reservations if the file descriptor is open
271 return mode & BLK_OPEN_WRITE;
274 static int blkdev_pr_register(struct block_device *bdev, blk_mode_t mode,
275 struct pr_registration __user *arg)
277 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
278 struct pr_registration reg;
280 if (!blkdev_pr_allowed(bdev, mode))
282 if (!ops || !ops->pr_register)
284 if (copy_from_user(®, arg, sizeof(reg)))
287 if (reg.flags & ~PR_FL_IGNORE_KEY)
289 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
292 static int blkdev_pr_reserve(struct block_device *bdev, blk_mode_t mode,
293 struct pr_reservation __user *arg)
295 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
296 struct pr_reservation rsv;
298 if (!blkdev_pr_allowed(bdev, mode))
300 if (!ops || !ops->pr_reserve)
302 if (copy_from_user(&rsv, arg, sizeof(rsv)))
305 if (rsv.flags & ~PR_FL_IGNORE_KEY)
307 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
310 static int blkdev_pr_release(struct block_device *bdev, blk_mode_t mode,
311 struct pr_reservation __user *arg)
313 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
314 struct pr_reservation rsv;
316 if (!blkdev_pr_allowed(bdev, mode))
318 if (!ops || !ops->pr_release)
320 if (copy_from_user(&rsv, arg, sizeof(rsv)))
325 return ops->pr_release(bdev, rsv.key, rsv.type);
328 static int blkdev_pr_preempt(struct block_device *bdev, blk_mode_t mode,
329 struct pr_preempt __user *arg, bool abort)
331 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
334 if (!blkdev_pr_allowed(bdev, mode))
336 if (!ops || !ops->pr_preempt)
338 if (copy_from_user(&p, arg, sizeof(p)))
343 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
346 static int blkdev_pr_clear(struct block_device *bdev, blk_mode_t mode,
347 struct pr_clear __user *arg)
349 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
352 if (!blkdev_pr_allowed(bdev, mode))
354 if (!ops || !ops->pr_clear)
356 if (copy_from_user(&c, arg, sizeof(c)))
361 return ops->pr_clear(bdev, c.key);
364 static int blkdev_flushbuf(struct block_device *bdev, unsigned cmd,
367 if (!capable(CAP_SYS_ADMIN))
370 mutex_lock(&bdev->bd_holder_lock);
371 if (bdev->bd_holder_ops && bdev->bd_holder_ops->sync)
372 bdev->bd_holder_ops->sync(bdev);
375 mutex_unlock(&bdev->bd_holder_lock);
377 invalidate_bdev(bdev);
381 static int blkdev_roset(struct block_device *bdev, unsigned cmd,
386 if (!capable(CAP_SYS_ADMIN))
389 if (get_user(n, (int __user *)arg))
391 if (bdev->bd_disk->fops->set_read_only) {
392 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
396 bdev->bd_read_only = n;
400 static int blkdev_getgeo(struct block_device *bdev,
401 struct hd_geometry __user *argp)
403 struct gendisk *disk = bdev->bd_disk;
404 struct hd_geometry geo;
409 if (!disk->fops->getgeo)
413 * We need to set the startsect first, the driver may
414 * want to override it.
416 memset(&geo, 0, sizeof(geo));
417 geo.start = get_start_sect(bdev);
418 ret = disk->fops->getgeo(bdev, &geo);
421 if (copy_to_user(argp, &geo, sizeof(geo)))
427 struct compat_hd_geometry {
429 unsigned char sectors;
430 unsigned short cylinders;
434 static int compat_hdio_getgeo(struct block_device *bdev,
435 struct compat_hd_geometry __user *ugeo)
437 struct gendisk *disk = bdev->bd_disk;
438 struct hd_geometry geo;
443 if (!disk->fops->getgeo)
446 memset(&geo, 0, sizeof(geo));
448 * We need to set the startsect first, the driver may
449 * want to override it.
451 geo.start = get_start_sect(bdev);
452 ret = disk->fops->getgeo(bdev, &geo);
456 ret = copy_to_user(ugeo, &geo, 4);
457 ret |= put_user(geo.start, &ugeo->start);
465 /* set the logical block size */
466 static int blkdev_bszset(struct block_device *bdev, blk_mode_t mode,
471 if (!capable(CAP_SYS_ADMIN))
475 if (get_user(n, argp))
478 if (mode & BLK_OPEN_EXCL)
479 return set_blocksize(bdev, n);
481 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode, &bdev, NULL)))
483 ret = set_blocksize(bdev, n);
484 blkdev_put(bdev, &bdev);
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, blk_mode_t mode,
495 unsigned int cmd, unsigned long arg,
498 unsigned int max_sectors;
502 return blkdev_flushbuf(bdev, cmd, arg);
504 return blkdev_roset(bdev, cmd, arg);
506 return blk_ioctl_discard(bdev, mode, arg);
508 return blk_ioctl_secure_erase(bdev, mode, argp);
510 return blk_ioctl_zeroout(bdev, mode, arg);
512 return put_u64(argp, bdev->bd_disk->diskseq);
514 return blkdev_report_zones_ioctl(bdev, cmd, arg);
519 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
521 return put_uint(argp, bdev_zone_sectors(bdev));
523 return put_uint(argp, bdev_nr_zones(bdev));
525 return put_int(argp, bdev_read_only(bdev) != 0);
526 case BLKSSZGET: /* get block device logical block size */
527 return put_int(argp, bdev_logical_block_size(bdev));
528 case BLKPBSZGET: /* get block device physical block size */
529 return put_uint(argp, bdev_physical_block_size(bdev));
531 return put_uint(argp, bdev_io_min(bdev));
533 return put_uint(argp, bdev_io_opt(bdev));
535 return put_int(argp, bdev_alignment_offset(bdev));
536 case BLKDISCARDZEROES:
537 return put_uint(argp, 0);
539 max_sectors = min_t(unsigned int, USHRT_MAX,
540 queue_max_sectors(bdev_get_queue(bdev)));
541 return put_ushort(argp, max_sectors);
543 return put_ushort(argp, !bdev_nonrot(bdev));
546 if(!capable(CAP_SYS_ADMIN))
548 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
551 if (!capable(CAP_SYS_ADMIN))
553 if (bdev_is_partition(bdev))
555 return disk_scan_partitions(bdev->bd_disk, mode);
558 case BLKTRACETEARDOWN:
559 return blk_trace_ioctl(bdev, cmd, argp);
560 case IOC_PR_REGISTER:
561 return blkdev_pr_register(bdev, mode, argp);
563 return blkdev_pr_reserve(bdev, mode, argp);
565 return blkdev_pr_release(bdev, mode, argp);
567 return blkdev_pr_preempt(bdev, mode, argp, false);
568 case IOC_PR_PREEMPT_ABORT:
569 return blkdev_pr_preempt(bdev, mode, argp, true);
571 return blkdev_pr_clear(bdev, mode, argp);
578 * Always keep this in sync with compat_blkdev_ioctl()
579 * to handle all incompatible commands in both functions.
581 * New commands must be compatible and go into blkdev_common_ioctl
583 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
585 struct block_device *bdev = I_BDEV(file->f_mapping->host);
586 void __user *argp = (void __user *)arg;
587 blk_mode_t mode = file_to_blk_mode(file);
591 /* These need separate implementations for the data structure */
593 return blkdev_getgeo(bdev, argp);
595 return blkpg_ioctl(bdev, argp);
597 /* Compat mode returns 32-bit data instead of 'long' */
602 return put_long(argp,
603 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
605 if (bdev_nr_sectors(bdev) > ~0UL)
607 return put_ulong(argp, bdev_nr_sectors(bdev));
609 /* The data is compatible, but the command number is different */
610 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
611 return put_int(argp, block_size(bdev));
613 return blkdev_bszset(bdev, mode, argp);
615 return put_u64(argp, bdev_nr_bytes(bdev));
617 /* Incompatible alignment on i386 */
619 return blk_trace_ioctl(bdev, cmd, argp);
624 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
625 if (ret != -ENOIOCTLCMD)
628 if (!bdev->bd_disk->fops->ioctl)
630 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
635 #define BLKBSZGET_32 _IOR(0x12, 112, int)
636 #define BLKBSZSET_32 _IOW(0x12, 113, int)
637 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
639 /* Most of the generic ioctls are handled in the normal fallback path.
640 This assumes the blkdev's low level compat_ioctl always returns
641 ENOIOCTLCMD for unknown ioctls. */
642 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
645 void __user *argp = compat_ptr(arg);
646 struct block_device *bdev = I_BDEV(file->f_mapping->host);
647 struct gendisk *disk = bdev->bd_disk;
648 blk_mode_t mode = file_to_blk_mode(file);
651 /* These need separate implementations for the data structure */
653 return compat_hdio_getgeo(bdev, argp);
655 return compat_blkpg_ioctl(bdev, argp);
657 /* Compat mode returns 32-bit data instead of 'long' */
662 return compat_put_long(argp,
663 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
665 if (bdev_nr_sectors(bdev) > ~(compat_ulong_t)0)
667 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
669 /* The data is compatible, but the command number is different */
670 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
671 return put_int(argp, bdev_logical_block_size(bdev));
673 return blkdev_bszset(bdev, mode, argp);
674 case BLKGETSIZE64_32:
675 return put_u64(argp, bdev_nr_bytes(bdev));
677 /* Incompatible alignment on i386 */
678 case BLKTRACESETUP32:
679 return blk_trace_ioctl(bdev, cmd, argp);
684 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
685 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
686 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);