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, fmode_t mode,
86 unsigned long arg, unsigned long flags)
90 struct request_queue *q = bdev_get_queue(bdev);
91 struct inode *inode = bdev->bd_inode;
94 if (!(mode & FMODE_WRITE))
97 if (!blk_queue_discard(q))
100 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
111 if (start + len > bdev_nr_bytes(bdev))
114 filemap_invalidate_lock(inode->i_mapping);
115 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
119 err = blkdev_issue_discard(bdev, start >> 9, len >> 9,
123 filemap_invalidate_unlock(inode->i_mapping);
127 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
131 uint64_t start, end, len;
132 struct inode *inode = bdev->bd_inode;
135 if (!(mode & FMODE_WRITE))
138 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
143 end = start + len - 1;
149 if (end >= (uint64_t)bdev_nr_bytes(bdev))
154 /* Invalidate the page cache, including dirty pages */
155 filemap_invalidate_lock(inode->i_mapping);
156 err = truncate_bdev_range(bdev, mode, start, end);
160 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
161 BLKDEV_ZERO_NOUNMAP);
164 filemap_invalidate_unlock(inode->i_mapping);
168 static int put_ushort(unsigned short __user *argp, unsigned short val)
170 return put_user(val, argp);
173 static int put_int(int __user *argp, int val)
175 return put_user(val, argp);
178 static int put_uint(unsigned int __user *argp, unsigned int val)
180 return put_user(val, argp);
183 static int put_long(long __user *argp, long val)
185 return put_user(val, argp);
188 static int put_ulong(unsigned long __user *argp, unsigned long val)
190 return put_user(val, argp);
193 static int put_u64(u64 __user *argp, u64 val)
195 return put_user(val, argp);
199 static int compat_put_long(compat_long_t __user *argp, long val)
201 return put_user(val, argp);
204 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
206 return put_user(val, argp);
212 * This is the equivalent of compat_ptr_ioctl(), to be used by block
213 * drivers that implement only commands that are completely compatible
214 * between 32-bit and 64-bit user space
216 int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
217 unsigned cmd, unsigned long arg)
219 struct gendisk *disk = bdev->bd_disk;
221 if (disk->fops->ioctl)
222 return disk->fops->ioctl(bdev, mode, cmd,
223 (unsigned long)compat_ptr(arg));
227 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
230 static int blkdev_pr_register(struct block_device *bdev,
231 struct pr_registration __user *arg)
233 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
234 struct pr_registration reg;
236 if (!capable(CAP_SYS_ADMIN))
238 if (!ops || !ops->pr_register)
240 if (copy_from_user(®, arg, sizeof(reg)))
243 if (reg.flags & ~PR_FL_IGNORE_KEY)
245 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
248 static int blkdev_pr_reserve(struct block_device *bdev,
249 struct pr_reservation __user *arg)
251 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
252 struct pr_reservation rsv;
254 if (!capable(CAP_SYS_ADMIN))
256 if (!ops || !ops->pr_reserve)
258 if (copy_from_user(&rsv, arg, sizeof(rsv)))
261 if (rsv.flags & ~PR_FL_IGNORE_KEY)
263 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
266 static int blkdev_pr_release(struct block_device *bdev,
267 struct pr_reservation __user *arg)
269 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
270 struct pr_reservation rsv;
272 if (!capable(CAP_SYS_ADMIN))
274 if (!ops || !ops->pr_release)
276 if (copy_from_user(&rsv, arg, sizeof(rsv)))
281 return ops->pr_release(bdev, rsv.key, rsv.type);
284 static int blkdev_pr_preempt(struct block_device *bdev,
285 struct pr_preempt __user *arg, bool abort)
287 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
290 if (!capable(CAP_SYS_ADMIN))
292 if (!ops || !ops->pr_preempt)
294 if (copy_from_user(&p, arg, sizeof(p)))
299 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
302 static int blkdev_pr_clear(struct block_device *bdev,
303 struct pr_clear __user *arg)
305 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
308 if (!capable(CAP_SYS_ADMIN))
310 if (!ops || !ops->pr_clear)
312 if (copy_from_user(&c, arg, sizeof(c)))
317 return ops->pr_clear(bdev, c.key);
320 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
321 unsigned cmd, unsigned long arg)
323 if (!capable(CAP_SYS_ADMIN))
326 invalidate_bdev(bdev);
330 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
331 unsigned cmd, unsigned long arg)
335 if (!capable(CAP_SYS_ADMIN))
338 if (get_user(n, (int __user *)arg))
340 if (bdev->bd_disk->fops->set_read_only) {
341 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
345 bdev->bd_read_only = n;
349 static int blkdev_getgeo(struct block_device *bdev,
350 struct hd_geometry __user *argp)
352 struct gendisk *disk = bdev->bd_disk;
353 struct hd_geometry geo;
358 if (!disk->fops->getgeo)
362 * We need to set the startsect first, the driver may
363 * want to override it.
365 memset(&geo, 0, sizeof(geo));
366 geo.start = get_start_sect(bdev);
367 ret = disk->fops->getgeo(bdev, &geo);
370 if (copy_to_user(argp, &geo, sizeof(geo)))
376 struct compat_hd_geometry {
378 unsigned char sectors;
379 unsigned short cylinders;
383 static int compat_hdio_getgeo(struct block_device *bdev,
384 struct compat_hd_geometry __user *ugeo)
386 struct gendisk *disk = bdev->bd_disk;
387 struct hd_geometry geo;
392 if (!disk->fops->getgeo)
395 memset(&geo, 0, sizeof(geo));
397 * We need to set the startsect first, the driver may
398 * want to override it.
400 geo.start = get_start_sect(bdev);
401 ret = disk->fops->getgeo(bdev, &geo);
405 ret = copy_to_user(ugeo, &geo, 4);
406 ret |= put_user(geo.start, &ugeo->start);
414 /* set the logical block size */
415 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
420 if (!capable(CAP_SYS_ADMIN))
424 if (get_user(n, argp))
427 if (mode & FMODE_EXCL)
428 return set_blocksize(bdev, n);
430 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
432 ret = set_blocksize(bdev, n);
433 blkdev_put(bdev, mode | FMODE_EXCL);
439 * Common commands that are handled the same way on native and compat
440 * user space. Note the separate arg/argp parameters that are needed
441 * to deal with the compat_ptr() conversion.
443 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
444 unsigned cmd, unsigned long arg, void __user *argp)
446 unsigned int max_sectors;
450 return blkdev_flushbuf(bdev, mode, cmd, arg);
452 return blkdev_roset(bdev, mode, cmd, arg);
454 return blk_ioctl_discard(bdev, mode, arg, 0);
456 return blk_ioctl_discard(bdev, mode, arg,
457 BLKDEV_DISCARD_SECURE);
459 return blk_ioctl_zeroout(bdev, mode, arg);
461 return put_u64(argp, bdev->bd_disk->diskseq);
463 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
468 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
470 return put_uint(argp, bdev_zone_sectors(bdev));
472 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
474 return put_int(argp, bdev_read_only(bdev) != 0);
475 case BLKSSZGET: /* get block device logical block size */
476 return put_int(argp, bdev_logical_block_size(bdev));
477 case BLKPBSZGET: /* get block device physical block size */
478 return put_uint(argp, bdev_physical_block_size(bdev));
480 return put_uint(argp, bdev_io_min(bdev));
482 return put_uint(argp, bdev_io_opt(bdev));
484 return put_int(argp, bdev_alignment_offset(bdev));
485 case BLKDISCARDZEROES:
486 return put_uint(argp, 0);
488 max_sectors = min_t(unsigned int, USHRT_MAX,
489 queue_max_sectors(bdev_get_queue(bdev)));
490 return put_ushort(argp, max_sectors);
492 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
495 if(!capable(CAP_SYS_ADMIN))
497 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
500 if (!capable(CAP_SYS_ADMIN))
502 if (bdev_is_partition(bdev))
504 return disk_scan_partitions(bdev->bd_disk, mode & ~FMODE_EXCL);
507 case BLKTRACETEARDOWN:
508 return blk_trace_ioctl(bdev, cmd, argp);
509 case IOC_PR_REGISTER:
510 return blkdev_pr_register(bdev, argp);
512 return blkdev_pr_reserve(bdev, argp);
514 return blkdev_pr_release(bdev, argp);
516 return blkdev_pr_preempt(bdev, argp, false);
517 case IOC_PR_PREEMPT_ABORT:
518 return blkdev_pr_preempt(bdev, argp, true);
520 return blkdev_pr_clear(bdev, argp);
527 * Always keep this in sync with compat_blkdev_ioctl()
528 * to handle all incompatible commands in both functions.
530 * New commands must be compatible and go into blkdev_common_ioctl
532 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
534 struct block_device *bdev = I_BDEV(file->f_mapping->host);
535 void __user *argp = (void __user *)arg;
536 fmode_t mode = file->f_mode;
540 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
541 * to updated it before every ioctl.
543 if (file->f_flags & O_NDELAY)
544 mode |= FMODE_NDELAY;
546 mode &= ~FMODE_NDELAY;
549 /* These need separate implementations for the data structure */
551 return blkdev_getgeo(bdev, argp);
553 return blkpg_ioctl(bdev, argp);
555 /* Compat mode returns 32-bit data instead of 'long' */
560 return put_long(argp,
561 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
563 if (bdev_nr_sectors(bdev) > ~0UL)
565 return put_ulong(argp, bdev_nr_sectors(bdev));
567 /* The data is compatible, but the command number is different */
568 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
569 return put_int(argp, block_size(bdev));
571 return blkdev_bszset(bdev, mode, argp);
573 return put_u64(argp, bdev_nr_bytes(bdev));
575 /* Incompatible alignment on i386 */
577 return blk_trace_ioctl(bdev, cmd, argp);
582 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
583 if (ret != -ENOIOCTLCMD)
586 if (!bdev->bd_disk->fops->ioctl)
588 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
593 #define BLKBSZGET_32 _IOR(0x12, 112, int)
594 #define BLKBSZSET_32 _IOW(0x12, 113, int)
595 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
597 /* Most of the generic ioctls are handled in the normal fallback path.
598 This assumes the blkdev's low level compat_ioctl always returns
599 ENOIOCTLCMD for unknown ioctls. */
600 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
603 void __user *argp = compat_ptr(arg);
604 struct block_device *bdev = I_BDEV(file->f_mapping->host);
605 struct gendisk *disk = bdev->bd_disk;
606 fmode_t mode = file->f_mode;
609 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
610 * to updated it before every ioctl.
612 if (file->f_flags & O_NDELAY)
613 mode |= FMODE_NDELAY;
615 mode &= ~FMODE_NDELAY;
618 /* These need separate implementations for the data structure */
620 return compat_hdio_getgeo(bdev, argp);
622 return compat_blkpg_ioctl(bdev, argp);
624 /* Compat mode returns 32-bit data instead of 'long' */
629 return compat_put_long(argp,
630 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
632 if (bdev_nr_sectors(bdev) > ~0UL)
634 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
636 /* The data is compatible, but the command number is different */
637 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
638 return put_int(argp, bdev_logical_block_size(bdev));
640 return blkdev_bszset(bdev, mode, argp);
641 case BLKGETSIZE64_32:
642 return put_u64(argp, bdev_nr_bytes(bdev));
644 /* Incompatible alignment on i386 */
645 case BLKTRACESETUP32:
646 return blk_trace_ioctl(bdev, cmd, argp);
651 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
652 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
653 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);