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 (!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 start = p.start >> SECTOR_SHIFT;
37 length = p.length >> SECTOR_SHIFT;
40 case BLKPG_ADD_PARTITION:
41 /* check if partition is aligned to blocksize */
42 if (p.start & (bdev_logical_block_size(bdev) - 1))
44 return bdev_add_partition(disk, p.pno, start, length);
45 case BLKPG_RESIZE_PARTITION:
46 return bdev_resize_partition(disk, p.pno, start, length);
52 static int blkpg_ioctl(struct block_device *bdev,
53 struct blkpg_ioctl_arg __user *arg)
55 struct blkpg_partition __user *udata;
58 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
61 return blkpg_do_ioctl(bdev, udata, op);
65 struct compat_blkpg_ioctl_arg {
72 static int compat_blkpg_ioctl(struct block_device *bdev,
73 struct compat_blkpg_ioctl_arg __user *arg)
78 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
81 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
85 static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode,
90 struct inode *inode = bdev->bd_inode;
93 if (!(mode & BLK_OPEN_WRITE))
96 if (!bdev_max_discard_sectors(bdev))
99 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
110 if (start + len > bdev_nr_bytes(bdev))
113 filemap_invalidate_lock(inode->i_mapping);
114 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
117 err = blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
119 filemap_invalidate_unlock(inode->i_mapping);
123 static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode,
130 if (!(mode & BLK_OPEN_WRITE))
132 if (!bdev_max_secure_erase_sectors(bdev))
134 if (copy_from_user(range, argp, sizeof(range)))
139 if ((start & 511) || (len & 511))
141 if (start + len > bdev_nr_bytes(bdev))
144 filemap_invalidate_lock(bdev->bd_inode->i_mapping);
145 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
147 err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9,
149 filemap_invalidate_unlock(bdev->bd_inode->i_mapping);
154 static int blk_ioctl_zeroout(struct block_device *bdev, blk_mode_t mode,
158 uint64_t start, end, len;
159 struct inode *inode = bdev->bd_inode;
162 if (!(mode & BLK_OPEN_WRITE))
165 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
170 end = start + len - 1;
176 if (end >= (uint64_t)bdev_nr_bytes(bdev))
181 /* Invalidate the page cache, including dirty pages */
182 filemap_invalidate_lock(inode->i_mapping);
183 err = truncate_bdev_range(bdev, mode, start, end);
187 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
188 BLKDEV_ZERO_NOUNMAP);
191 filemap_invalidate_unlock(inode->i_mapping);
195 static int put_ushort(unsigned short __user *argp, unsigned short val)
197 return put_user(val, argp);
200 static int put_int(int __user *argp, int val)
202 return put_user(val, argp);
205 static int put_uint(unsigned int __user *argp, unsigned int val)
207 return put_user(val, argp);
210 static int put_long(long __user *argp, long val)
212 return put_user(val, argp);
215 static int put_ulong(unsigned long __user *argp, unsigned long val)
217 return put_user(val, argp);
220 static int put_u64(u64 __user *argp, u64 val)
222 return put_user(val, argp);
226 static int compat_put_long(compat_long_t __user *argp, long val)
228 return put_user(val, argp);
231 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
233 return put_user(val, argp);
239 * This is the equivalent of compat_ptr_ioctl(), to be used by block
240 * drivers that implement only commands that are completely compatible
241 * between 32-bit and 64-bit user space
243 int blkdev_compat_ptr_ioctl(struct block_device *bdev, blk_mode_t mode,
244 unsigned cmd, unsigned long arg)
246 struct gendisk *disk = bdev->bd_disk;
248 if (disk->fops->ioctl)
249 return disk->fops->ioctl(bdev, mode, cmd,
250 (unsigned long)compat_ptr(arg));
254 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
257 static bool blkdev_pr_allowed(struct block_device *bdev, blk_mode_t mode)
259 /* no sense to make reservations for partitions */
260 if (bdev_is_partition(bdev))
263 if (capable(CAP_SYS_ADMIN))
266 * Only allow unprivileged reservations if the file descriptor is open
269 return mode & BLK_OPEN_WRITE;
272 static int blkdev_pr_register(struct block_device *bdev, blk_mode_t mode,
273 struct pr_registration __user *arg)
275 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
276 struct pr_registration reg;
278 if (!blkdev_pr_allowed(bdev, mode))
280 if (!ops || !ops->pr_register)
282 if (copy_from_user(®, arg, sizeof(reg)))
285 if (reg.flags & ~PR_FL_IGNORE_KEY)
287 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
290 static int blkdev_pr_reserve(struct block_device *bdev, blk_mode_t mode,
291 struct pr_reservation __user *arg)
293 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
294 struct pr_reservation rsv;
296 if (!blkdev_pr_allowed(bdev, mode))
298 if (!ops || !ops->pr_reserve)
300 if (copy_from_user(&rsv, arg, sizeof(rsv)))
303 if (rsv.flags & ~PR_FL_IGNORE_KEY)
305 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
308 static int blkdev_pr_release(struct block_device *bdev, blk_mode_t mode,
309 struct pr_reservation __user *arg)
311 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
312 struct pr_reservation rsv;
314 if (!blkdev_pr_allowed(bdev, mode))
316 if (!ops || !ops->pr_release)
318 if (copy_from_user(&rsv, arg, sizeof(rsv)))
323 return ops->pr_release(bdev, rsv.key, rsv.type);
326 static int blkdev_pr_preempt(struct block_device *bdev, blk_mode_t mode,
327 struct pr_preempt __user *arg, bool abort)
329 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
332 if (!blkdev_pr_allowed(bdev, mode))
334 if (!ops || !ops->pr_preempt)
336 if (copy_from_user(&p, arg, sizeof(p)))
341 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
344 static int blkdev_pr_clear(struct block_device *bdev, blk_mode_t mode,
345 struct pr_clear __user *arg)
347 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
350 if (!blkdev_pr_allowed(bdev, mode))
352 if (!ops || !ops->pr_clear)
354 if (copy_from_user(&c, arg, sizeof(c)))
359 return ops->pr_clear(bdev, c.key);
362 static int blkdev_flushbuf(struct block_device *bdev, unsigned cmd,
365 if (!capable(CAP_SYS_ADMIN))
368 invalidate_bdev(bdev);
372 static int blkdev_roset(struct block_device *bdev, unsigned cmd,
377 if (!capable(CAP_SYS_ADMIN))
380 if (get_user(n, (int __user *)arg))
382 if (bdev->bd_disk->fops->set_read_only) {
383 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
387 bdev->bd_read_only = n;
391 static int blkdev_getgeo(struct block_device *bdev,
392 struct hd_geometry __user *argp)
394 struct gendisk *disk = bdev->bd_disk;
395 struct hd_geometry geo;
400 if (!disk->fops->getgeo)
404 * We need to set the startsect first, the driver may
405 * want to override it.
407 memset(&geo, 0, sizeof(geo));
408 geo.start = get_start_sect(bdev);
409 ret = disk->fops->getgeo(bdev, &geo);
412 if (copy_to_user(argp, &geo, sizeof(geo)))
418 struct compat_hd_geometry {
420 unsigned char sectors;
421 unsigned short cylinders;
425 static int compat_hdio_getgeo(struct block_device *bdev,
426 struct compat_hd_geometry __user *ugeo)
428 struct gendisk *disk = bdev->bd_disk;
429 struct hd_geometry geo;
434 if (!disk->fops->getgeo)
437 memset(&geo, 0, sizeof(geo));
439 * We need to set the startsect first, the driver may
440 * want to override it.
442 geo.start = get_start_sect(bdev);
443 ret = disk->fops->getgeo(bdev, &geo);
447 ret = copy_to_user(ugeo, &geo, 4);
448 ret |= put_user(geo.start, &ugeo->start);
456 /* set the logical block size */
457 static int blkdev_bszset(struct block_device *bdev, blk_mode_t mode,
462 if (!capable(CAP_SYS_ADMIN))
466 if (get_user(n, argp))
469 if (mode & BLK_OPEN_EXCL)
470 return set_blocksize(bdev, n);
472 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode, &bdev, NULL)))
474 ret = set_blocksize(bdev, n);
475 blkdev_put(bdev, &bdev);
481 * Common commands that are handled the same way on native and compat
482 * user space. Note the separate arg/argp parameters that are needed
483 * to deal with the compat_ptr() conversion.
485 static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,
486 unsigned int cmd, unsigned long arg,
489 unsigned int max_sectors;
493 return blkdev_flushbuf(bdev, cmd, arg);
495 return blkdev_roset(bdev, cmd, arg);
497 return blk_ioctl_discard(bdev, mode, arg);
499 return blk_ioctl_secure_erase(bdev, mode, argp);
501 return blk_ioctl_zeroout(bdev, mode, arg);
503 return put_u64(argp, bdev->bd_disk->diskseq);
505 return blkdev_report_zones_ioctl(bdev, cmd, arg);
510 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
512 return put_uint(argp, bdev_zone_sectors(bdev));
514 return put_uint(argp, bdev_nr_zones(bdev));
516 return put_int(argp, bdev_read_only(bdev) != 0);
517 case BLKSSZGET: /* get block device logical block size */
518 return put_int(argp, bdev_logical_block_size(bdev));
519 case BLKPBSZGET: /* get block device physical block size */
520 return put_uint(argp, bdev_physical_block_size(bdev));
522 return put_uint(argp, bdev_io_min(bdev));
524 return put_uint(argp, bdev_io_opt(bdev));
526 return put_int(argp, bdev_alignment_offset(bdev));
527 case BLKDISCARDZEROES:
528 return put_uint(argp, 0);
530 max_sectors = min_t(unsigned int, USHRT_MAX,
531 queue_max_sectors(bdev_get_queue(bdev)));
532 return put_ushort(argp, max_sectors);
534 return put_ushort(argp, !bdev_nonrot(bdev));
537 if(!capable(CAP_SYS_ADMIN))
539 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
542 if (!capable(CAP_SYS_ADMIN))
544 if (bdev_is_partition(bdev))
546 return disk_scan_partitions(bdev->bd_disk, mode);
549 case BLKTRACETEARDOWN:
550 return blk_trace_ioctl(bdev, cmd, argp);
551 case IOC_PR_REGISTER:
552 return blkdev_pr_register(bdev, mode, argp);
554 return blkdev_pr_reserve(bdev, mode, argp);
556 return blkdev_pr_release(bdev, mode, argp);
558 return blkdev_pr_preempt(bdev, mode, argp, false);
559 case IOC_PR_PREEMPT_ABORT:
560 return blkdev_pr_preempt(bdev, mode, argp, true);
562 return blkdev_pr_clear(bdev, mode, argp);
569 * Always keep this in sync with compat_blkdev_ioctl()
570 * to handle all incompatible commands in both functions.
572 * New commands must be compatible and go into blkdev_common_ioctl
574 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
576 struct block_device *bdev = I_BDEV(file->f_mapping->host);
577 void __user *argp = (void __user *)arg;
578 blk_mode_t mode = file_to_blk_mode(file);
582 /* These need separate implementations for the data structure */
584 return blkdev_getgeo(bdev, argp);
586 return blkpg_ioctl(bdev, argp);
588 /* Compat mode returns 32-bit data instead of 'long' */
593 return put_long(argp,
594 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
596 if (bdev_nr_sectors(bdev) > ~0UL)
598 return put_ulong(argp, bdev_nr_sectors(bdev));
600 /* The data is compatible, but the command number is different */
601 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
602 return put_int(argp, block_size(bdev));
604 return blkdev_bszset(bdev, mode, argp);
606 return put_u64(argp, bdev_nr_bytes(bdev));
608 /* Incompatible alignment on i386 */
610 return blk_trace_ioctl(bdev, cmd, argp);
615 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
616 if (ret != -ENOIOCTLCMD)
619 if (!bdev->bd_disk->fops->ioctl)
621 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
626 #define BLKBSZGET_32 _IOR(0x12, 112, int)
627 #define BLKBSZSET_32 _IOW(0x12, 113, int)
628 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
630 /* Most of the generic ioctls are handled in the normal fallback path.
631 This assumes the blkdev's low level compat_ioctl always returns
632 ENOIOCTLCMD for unknown ioctls. */
633 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
636 void __user *argp = compat_ptr(arg);
637 struct block_device *bdev = I_BDEV(file->f_mapping->host);
638 struct gendisk *disk = bdev->bd_disk;
639 blk_mode_t mode = file_to_blk_mode(file);
642 /* These need separate implementations for the data structure */
644 return compat_hdio_getgeo(bdev, argp);
646 return compat_blkpg_ioctl(bdev, argp);
648 /* Compat mode returns 32-bit data instead of 'long' */
653 return compat_put_long(argp,
654 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
656 if (bdev_nr_sectors(bdev) > ~(compat_ulong_t)0)
658 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
660 /* The data is compatible, but the command number is different */
661 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
662 return put_int(argp, bdev_logical_block_size(bdev));
664 return blkdev_bszset(bdev, mode, argp);
665 case BLKGETSIZE64_32:
666 return put_u64(argp, bdev_nr_bytes(bdev));
668 /* Incompatible alignment on i386 */
669 case BLKTRACESETUP32:
670 return blk_trace_ioctl(bdev, cmd, argp);
675 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
676 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
677 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);