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,
90 struct inode *inode = bdev->bd_inode;
93 if (!(mode & FMODE_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, fmode_t mode,
130 if (!(mode & FMODE_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, fmode_t mode,
158 uint64_t start, end, len;
159 struct inode *inode = bdev->bd_inode;
162 if (!(mode & FMODE_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, fmode_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 int blkdev_pr_register(struct block_device *bdev,
258 struct pr_registration __user *arg)
260 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
261 struct pr_registration reg;
263 if (!capable(CAP_SYS_ADMIN))
265 if (!ops || !ops->pr_register)
267 if (copy_from_user(®, arg, sizeof(reg)))
270 if (reg.flags & ~PR_FL_IGNORE_KEY)
272 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
275 static int blkdev_pr_reserve(struct block_device *bdev,
276 struct pr_reservation __user *arg)
278 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
279 struct pr_reservation rsv;
281 if (!capable(CAP_SYS_ADMIN))
283 if (!ops || !ops->pr_reserve)
285 if (copy_from_user(&rsv, arg, sizeof(rsv)))
288 if (rsv.flags & ~PR_FL_IGNORE_KEY)
290 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
293 static int blkdev_pr_release(struct block_device *bdev,
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 (!capable(CAP_SYS_ADMIN))
301 if (!ops || !ops->pr_release)
303 if (copy_from_user(&rsv, arg, sizeof(rsv)))
308 return ops->pr_release(bdev, rsv.key, rsv.type);
311 static int blkdev_pr_preempt(struct block_device *bdev,
312 struct pr_preempt __user *arg, bool abort)
314 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
317 if (!capable(CAP_SYS_ADMIN))
319 if (!ops || !ops->pr_preempt)
321 if (copy_from_user(&p, arg, sizeof(p)))
326 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
329 static int blkdev_pr_clear(struct block_device *bdev,
330 struct pr_clear __user *arg)
332 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
335 if (!capable(CAP_SYS_ADMIN))
337 if (!ops || !ops->pr_clear)
339 if (copy_from_user(&c, arg, sizeof(c)))
344 return ops->pr_clear(bdev, c.key);
347 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
348 unsigned cmd, unsigned long arg)
350 if (!capable(CAP_SYS_ADMIN))
353 invalidate_bdev(bdev);
357 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
358 unsigned cmd, unsigned long arg)
362 if (!capable(CAP_SYS_ADMIN))
365 if (get_user(n, (int __user *)arg))
367 if (bdev->bd_disk->fops->set_read_only) {
368 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
372 bdev->bd_read_only = n;
376 static int blkdev_getgeo(struct block_device *bdev,
377 struct hd_geometry __user *argp)
379 struct gendisk *disk = bdev->bd_disk;
380 struct hd_geometry geo;
385 if (!disk->fops->getgeo)
389 * We need to set the startsect first, the driver may
390 * want to override it.
392 memset(&geo, 0, sizeof(geo));
393 geo.start = get_start_sect(bdev);
394 ret = disk->fops->getgeo(bdev, &geo);
397 if (copy_to_user(argp, &geo, sizeof(geo)))
403 struct compat_hd_geometry {
405 unsigned char sectors;
406 unsigned short cylinders;
410 static int compat_hdio_getgeo(struct block_device *bdev,
411 struct compat_hd_geometry __user *ugeo)
413 struct gendisk *disk = bdev->bd_disk;
414 struct hd_geometry geo;
419 if (!disk->fops->getgeo)
422 memset(&geo, 0, sizeof(geo));
424 * We need to set the startsect first, the driver may
425 * want to override it.
427 geo.start = get_start_sect(bdev);
428 ret = disk->fops->getgeo(bdev, &geo);
432 ret = copy_to_user(ugeo, &geo, 4);
433 ret |= put_user(geo.start, &ugeo->start);
441 /* set the logical block size */
442 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
447 if (!capable(CAP_SYS_ADMIN))
451 if (get_user(n, argp))
454 if (mode & FMODE_EXCL)
455 return set_blocksize(bdev, n);
457 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
459 ret = set_blocksize(bdev, n);
460 blkdev_put(bdev, mode | FMODE_EXCL);
466 * Common commands that are handled the same way on native and compat
467 * user space. Note the separate arg/argp parameters that are needed
468 * to deal with the compat_ptr() conversion.
470 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
471 unsigned int cmd, unsigned long arg,
474 unsigned int max_sectors;
478 return blkdev_flushbuf(bdev, mode, cmd, arg);
480 return blkdev_roset(bdev, mode, cmd, arg);
482 return blk_ioctl_discard(bdev, mode, arg);
484 return blk_ioctl_secure_erase(bdev, mode, argp);
486 return blk_ioctl_zeroout(bdev, mode, arg);
488 return put_u64(argp, bdev->bd_disk->diskseq);
490 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
495 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
497 return put_uint(argp, bdev_zone_sectors(bdev));
499 return put_uint(argp, bdev_nr_zones(bdev));
501 return put_int(argp, bdev_read_only(bdev) != 0);
502 case BLKSSZGET: /* get block device logical block size */
503 return put_int(argp, bdev_logical_block_size(bdev));
504 case BLKPBSZGET: /* get block device physical block size */
505 return put_uint(argp, bdev_physical_block_size(bdev));
507 return put_uint(argp, bdev_io_min(bdev));
509 return put_uint(argp, bdev_io_opt(bdev));
511 return put_int(argp, bdev_alignment_offset(bdev));
512 case BLKDISCARDZEROES:
513 return put_uint(argp, 0);
515 max_sectors = min_t(unsigned int, USHRT_MAX,
516 queue_max_sectors(bdev_get_queue(bdev)));
517 return put_ushort(argp, max_sectors);
519 return put_ushort(argp, !bdev_nonrot(bdev));
522 if(!capable(CAP_SYS_ADMIN))
524 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
527 if (!capable(CAP_SYS_ADMIN))
529 if (bdev_is_partition(bdev))
531 return disk_scan_partitions(bdev->bd_disk, mode);
534 case BLKTRACETEARDOWN:
535 return blk_trace_ioctl(bdev, cmd, argp);
536 case IOC_PR_REGISTER:
537 return blkdev_pr_register(bdev, argp);
539 return blkdev_pr_reserve(bdev, argp);
541 return blkdev_pr_release(bdev, argp);
543 return blkdev_pr_preempt(bdev, argp, false);
544 case IOC_PR_PREEMPT_ABORT:
545 return blkdev_pr_preempt(bdev, argp, true);
547 return blkdev_pr_clear(bdev, argp);
554 * Always keep this in sync with compat_blkdev_ioctl()
555 * to handle all incompatible commands in both functions.
557 * New commands must be compatible and go into blkdev_common_ioctl
559 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
561 struct block_device *bdev = I_BDEV(file->f_mapping->host);
562 void __user *argp = (void __user *)arg;
563 fmode_t mode = file->f_mode;
567 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
568 * to updated it before every ioctl.
570 if (file->f_flags & O_NDELAY)
571 mode |= FMODE_NDELAY;
573 mode &= ~FMODE_NDELAY;
576 /* These need separate implementations for the data structure */
578 return blkdev_getgeo(bdev, argp);
580 return blkpg_ioctl(bdev, argp);
582 /* Compat mode returns 32-bit data instead of 'long' */
587 return put_long(argp,
588 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
590 if (bdev_nr_sectors(bdev) > ~0UL)
592 return put_ulong(argp, bdev_nr_sectors(bdev));
594 /* The data is compatible, but the command number is different */
595 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
596 return put_int(argp, block_size(bdev));
598 return blkdev_bszset(bdev, mode, argp);
600 return put_u64(argp, bdev_nr_bytes(bdev));
602 /* Incompatible alignment on i386 */
604 return blk_trace_ioctl(bdev, cmd, argp);
609 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
610 if (ret != -ENOIOCTLCMD)
613 if (!bdev->bd_disk->fops->ioctl)
615 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
620 #define BLKBSZGET_32 _IOR(0x12, 112, int)
621 #define BLKBSZSET_32 _IOW(0x12, 113, int)
622 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
624 /* Most of the generic ioctls are handled in the normal fallback path.
625 This assumes the blkdev's low level compat_ioctl always returns
626 ENOIOCTLCMD for unknown ioctls. */
627 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
630 void __user *argp = compat_ptr(arg);
631 struct block_device *bdev = I_BDEV(file->f_mapping->host);
632 struct gendisk *disk = bdev->bd_disk;
633 fmode_t mode = file->f_mode;
636 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
637 * to updated it before every ioctl.
639 if (file->f_flags & O_NDELAY)
640 mode |= FMODE_NDELAY;
642 mode &= ~FMODE_NDELAY;
645 /* These need separate implementations for the data structure */
647 return compat_hdio_getgeo(bdev, argp);
649 return compat_blkpg_ioctl(bdev, argp);
651 /* Compat mode returns 32-bit data instead of 'long' */
656 return compat_put_long(argp,
657 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
659 if (bdev_nr_sectors(bdev) > ~(compat_ulong_t)0)
661 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
663 /* The data is compatible, but the command number is different */
664 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
665 return put_int(argp, bdev_logical_block_size(bdev));
667 return blkdev_bszset(bdev, mode, argp);
668 case BLKGETSIZE64_32:
669 return put_u64(argp, bdev_nr_bytes(bdev));
671 /* Incompatible alignment on i386 */
672 case BLKTRACESETUP32:
673 return blk_trace_ioctl(bdev, cmd, argp);
678 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
679 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
680 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);