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;
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 || p.start + p.length < 0)
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;
46 case BLKPG_ADD_PARTITION:
47 return bdev_add_partition(disk, p.pno, start, length);
48 case BLKPG_RESIZE_PARTITION:
49 return bdev_resize_partition(disk, p.pno, start, length);
55 static int blkpg_ioctl(struct block_device *bdev,
56 struct blkpg_ioctl_arg __user *arg)
58 struct blkpg_partition __user *udata;
61 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
64 return blkpg_do_ioctl(bdev, udata, op);
68 struct compat_blkpg_ioctl_arg {
75 static int compat_blkpg_ioctl(struct block_device *bdev,
76 struct compat_blkpg_ioctl_arg __user *arg)
81 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
84 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
88 static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode,
93 struct inode *inode = bdev->bd_inode;
96 if (!(mode & BLK_OPEN_WRITE))
99 if (!bdev_max_discard_sectors(bdev))
102 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
113 if (start + len > bdev_nr_bytes(bdev))
116 filemap_invalidate_lock(inode->i_mapping);
117 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
120 err = blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
122 filemap_invalidate_unlock(inode->i_mapping);
126 static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode,
133 if (!(mode & BLK_OPEN_WRITE))
135 if (!bdev_max_secure_erase_sectors(bdev))
137 if (copy_from_user(range, argp, sizeof(range)))
142 if ((start & 511) || (len & 511))
144 if (start + len > bdev_nr_bytes(bdev))
147 filemap_invalidate_lock(bdev->bd_inode->i_mapping);
148 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
150 err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9,
152 filemap_invalidate_unlock(bdev->bd_inode->i_mapping);
157 static int blk_ioctl_zeroout(struct block_device *bdev, blk_mode_t mode,
161 uint64_t start, end, len;
162 struct inode *inode = bdev->bd_inode;
165 if (!(mode & BLK_OPEN_WRITE))
168 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
173 end = start + len - 1;
179 if (end >= (uint64_t)bdev_nr_bytes(bdev))
184 /* Invalidate the page cache, including dirty pages */
185 filemap_invalidate_lock(inode->i_mapping);
186 err = truncate_bdev_range(bdev, mode, start, end);
190 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
191 BLKDEV_ZERO_NOUNMAP);
194 filemap_invalidate_unlock(inode->i_mapping);
198 static int put_ushort(unsigned short __user *argp, unsigned short val)
200 return put_user(val, argp);
203 static int put_int(int __user *argp, int val)
205 return put_user(val, argp);
208 static int put_uint(unsigned int __user *argp, unsigned int val)
210 return put_user(val, argp);
213 static int put_long(long __user *argp, long val)
215 return put_user(val, argp);
218 static int put_ulong(unsigned long __user *argp, unsigned long val)
220 return put_user(val, argp);
223 static int put_u64(u64 __user *argp, u64 val)
225 return put_user(val, argp);
229 static int compat_put_long(compat_long_t __user *argp, long val)
231 return put_user(val, argp);
234 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
236 return put_user(val, argp);
242 * This is the equivalent of compat_ptr_ioctl(), to be used by block
243 * drivers that implement only commands that are completely compatible
244 * between 32-bit and 64-bit user space
246 int blkdev_compat_ptr_ioctl(struct block_device *bdev, blk_mode_t mode,
247 unsigned cmd, unsigned long arg)
249 struct gendisk *disk = bdev->bd_disk;
251 if (disk->fops->ioctl)
252 return disk->fops->ioctl(bdev, mode, cmd,
253 (unsigned long)compat_ptr(arg));
257 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
260 static bool blkdev_pr_allowed(struct block_device *bdev, blk_mode_t mode)
262 /* no sense to make reservations for partitions */
263 if (bdev_is_partition(bdev))
266 if (capable(CAP_SYS_ADMIN))
269 * Only allow unprivileged reservations if the file descriptor is open
272 return mode & BLK_OPEN_WRITE;
275 static int blkdev_pr_register(struct block_device *bdev, blk_mode_t mode,
276 struct pr_registration __user *arg)
278 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
279 struct pr_registration reg;
281 if (!blkdev_pr_allowed(bdev, mode))
283 if (!ops || !ops->pr_register)
285 if (copy_from_user(®, arg, sizeof(reg)))
288 if (reg.flags & ~PR_FL_IGNORE_KEY)
290 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
293 static int blkdev_pr_reserve(struct block_device *bdev, blk_mode_t mode,
294 struct pr_reservation __user *arg)
296 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
297 struct pr_reservation rsv;
299 if (!blkdev_pr_allowed(bdev, mode))
301 if (!ops || !ops->pr_reserve)
303 if (copy_from_user(&rsv, arg, sizeof(rsv)))
306 if (rsv.flags & ~PR_FL_IGNORE_KEY)
308 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
311 static int blkdev_pr_release(struct block_device *bdev, blk_mode_t mode,
312 struct pr_reservation __user *arg)
314 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
315 struct pr_reservation rsv;
317 if (!blkdev_pr_allowed(bdev, mode))
319 if (!ops || !ops->pr_release)
321 if (copy_from_user(&rsv, arg, sizeof(rsv)))
326 return ops->pr_release(bdev, rsv.key, rsv.type);
329 static int blkdev_pr_preempt(struct block_device *bdev, blk_mode_t mode,
330 struct pr_preempt __user *arg, bool abort)
332 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
335 if (!blkdev_pr_allowed(bdev, mode))
337 if (!ops || !ops->pr_preempt)
339 if (copy_from_user(&p, arg, sizeof(p)))
344 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
347 static int blkdev_pr_clear(struct block_device *bdev, blk_mode_t mode,
348 struct pr_clear __user *arg)
350 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
353 if (!blkdev_pr_allowed(bdev, mode))
355 if (!ops || !ops->pr_clear)
357 if (copy_from_user(&c, arg, sizeof(c)))
362 return ops->pr_clear(bdev, c.key);
365 static int blkdev_flushbuf(struct block_device *bdev, unsigned cmd,
368 if (!capable(CAP_SYS_ADMIN))
371 mutex_lock(&bdev->bd_holder_lock);
372 if (bdev->bd_holder_ops && bdev->bd_holder_ops->sync)
373 bdev->bd_holder_ops->sync(bdev);
376 mutex_unlock(&bdev->bd_holder_lock);
378 invalidate_bdev(bdev);
382 static int blkdev_roset(struct block_device *bdev, unsigned cmd,
387 if (!capable(CAP_SYS_ADMIN))
390 if (get_user(n, (int __user *)arg))
392 if (bdev->bd_disk->fops->set_read_only) {
393 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
397 bdev->bd_read_only = n;
401 static int blkdev_getgeo(struct block_device *bdev,
402 struct hd_geometry __user *argp)
404 struct gendisk *disk = bdev->bd_disk;
405 struct hd_geometry geo;
410 if (!disk->fops->getgeo)
414 * We need to set the startsect first, the driver may
415 * want to override it.
417 memset(&geo, 0, sizeof(geo));
418 geo.start = get_start_sect(bdev);
419 ret = disk->fops->getgeo(bdev, &geo);
422 if (copy_to_user(argp, &geo, sizeof(geo)))
428 struct compat_hd_geometry {
430 unsigned char sectors;
431 unsigned short cylinders;
435 static int compat_hdio_getgeo(struct block_device *bdev,
436 struct compat_hd_geometry __user *ugeo)
438 struct gendisk *disk = bdev->bd_disk;
439 struct hd_geometry geo;
444 if (!disk->fops->getgeo)
447 memset(&geo, 0, sizeof(geo));
449 * We need to set the startsect first, the driver may
450 * want to override it.
452 geo.start = get_start_sect(bdev);
453 ret = disk->fops->getgeo(bdev, &geo);
457 ret = copy_to_user(ugeo, &geo, 4);
458 ret |= put_user(geo.start, &ugeo->start);
466 /* set the logical block size */
467 static int blkdev_bszset(struct block_device *bdev, blk_mode_t mode,
472 if (!capable(CAP_SYS_ADMIN))
476 if (get_user(n, argp))
479 if (mode & BLK_OPEN_EXCL)
480 return set_blocksize(bdev, n);
482 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode, &bdev, NULL)))
484 ret = set_blocksize(bdev, n);
485 blkdev_put(bdev, &bdev);
491 * Common commands that are handled the same way on native and compat
492 * user space. Note the separate arg/argp parameters that are needed
493 * to deal with the compat_ptr() conversion.
495 static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,
496 unsigned int cmd, unsigned long arg,
499 unsigned int max_sectors;
503 return blkdev_flushbuf(bdev, cmd, arg);
505 return blkdev_roset(bdev, cmd, arg);
507 return blk_ioctl_discard(bdev, mode, arg);
509 return blk_ioctl_secure_erase(bdev, mode, argp);
511 return blk_ioctl_zeroout(bdev, mode, arg);
513 return put_u64(argp, bdev->bd_disk->diskseq);
515 return blkdev_report_zones_ioctl(bdev, cmd, arg);
520 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
522 return put_uint(argp, bdev_zone_sectors(bdev));
524 return put_uint(argp, bdev_nr_zones(bdev));
526 return put_int(argp, bdev_read_only(bdev) != 0);
527 case BLKSSZGET: /* get block device logical block size */
528 return put_int(argp, bdev_logical_block_size(bdev));
529 case BLKPBSZGET: /* get block device physical block size */
530 return put_uint(argp, bdev_physical_block_size(bdev));
532 return put_uint(argp, bdev_io_min(bdev));
534 return put_uint(argp, bdev_io_opt(bdev));
536 return put_int(argp, bdev_alignment_offset(bdev));
537 case BLKDISCARDZEROES:
538 return put_uint(argp, 0);
540 max_sectors = min_t(unsigned int, USHRT_MAX,
541 queue_max_sectors(bdev_get_queue(bdev)));
542 return put_ushort(argp, max_sectors);
544 return put_ushort(argp, !bdev_nonrot(bdev));
547 if(!capable(CAP_SYS_ADMIN))
549 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
552 if (!capable(CAP_SYS_ADMIN))
554 if (bdev_is_partition(bdev))
556 return disk_scan_partitions(bdev->bd_disk, mode);
559 case BLKTRACETEARDOWN:
560 return blk_trace_ioctl(bdev, cmd, argp);
561 case IOC_PR_REGISTER:
562 return blkdev_pr_register(bdev, mode, argp);
564 return blkdev_pr_reserve(bdev, mode, argp);
566 return blkdev_pr_release(bdev, mode, argp);
568 return blkdev_pr_preempt(bdev, mode, argp, false);
569 case IOC_PR_PREEMPT_ABORT:
570 return blkdev_pr_preempt(bdev, mode, argp, true);
572 return blkdev_pr_clear(bdev, mode, argp);
579 * Always keep this in sync with compat_blkdev_ioctl()
580 * to handle all incompatible commands in both functions.
582 * New commands must be compatible and go into blkdev_common_ioctl
584 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
586 struct block_device *bdev = I_BDEV(file->f_mapping->host);
587 void __user *argp = (void __user *)arg;
588 blk_mode_t mode = file_to_blk_mode(file);
592 /* These need separate implementations for the data structure */
594 return blkdev_getgeo(bdev, argp);
596 return blkpg_ioctl(bdev, argp);
598 /* Compat mode returns 32-bit data instead of 'long' */
603 return put_long(argp,
604 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
606 if (bdev_nr_sectors(bdev) > ~0UL)
608 return put_ulong(argp, bdev_nr_sectors(bdev));
610 /* The data is compatible, but the command number is different */
611 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
612 return put_int(argp, block_size(bdev));
614 return blkdev_bszset(bdev, mode, argp);
616 return put_u64(argp, bdev_nr_bytes(bdev));
618 /* Incompatible alignment on i386 */
620 return blk_trace_ioctl(bdev, cmd, argp);
625 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
626 if (ret != -ENOIOCTLCMD)
629 if (!bdev->bd_disk->fops->ioctl)
631 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
636 #define BLKBSZGET_32 _IOR(0x12, 112, int)
637 #define BLKBSZSET_32 _IOW(0x12, 113, int)
638 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
640 /* Most of the generic ioctls are handled in the normal fallback path.
641 This assumes the blkdev's low level compat_ioctl always returns
642 ENOIOCTLCMD for unknown ioctls. */
643 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
646 void __user *argp = compat_ptr(arg);
647 struct block_device *bdev = I_BDEV(file->f_mapping->host);
648 struct gendisk *disk = bdev->bd_disk;
649 blk_mode_t mode = file_to_blk_mode(file);
652 /* These need separate implementations for the data structure */
654 return compat_hdio_getgeo(bdev, argp);
656 return compat_blkpg_ioctl(bdev, argp);
658 /* Compat mode returns 32-bit data instead of 'long' */
663 return compat_put_long(argp,
664 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
666 if (bdev_nr_sectors(bdev) > ~(compat_ulong_t)0)
668 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
670 /* The data is compatible, but the command number is different */
671 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
672 return put_int(argp, bdev_logical_block_size(bdev));
674 return blkdev_bszset(bdev, mode, argp);
675 case BLKGETSIZE64_32:
676 return put_u64(argp, bdev_nr_bytes(bdev));
678 /* Incompatible alignment on i386 */
679 case BLKTRACESETUP32:
680 return blk_trace_ioctl(bdev, cmd, argp);
685 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
686 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
687 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);