Christoph Hellwig | 3dcf60b | 2019-04-30 14:42:43 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 2 | #include <linux/capability.h> |
Arnd Bergmann | ee6a129 | 2019-11-28 15:48:10 +0100 | [diff] [blame] | 3 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/blkdev.h> |
Paul Gortmaker | d5decd3 | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 5 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/blkpg.h> |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 8 | #include <linux/hdreg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/backing-dev.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 10 | #include <linux/fs.h> |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 11 | #include <linux/blktrace_api.h> |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 12 | #include <linux/pr.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Christoph Hellwig | 581e260 | 2020-03-25 16:48:41 +0100 | [diff] [blame] | 14 | #include "blk.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Arnd Bergmann | 5fb889f | 2019-11-28 11:28:41 +0100 | [diff] [blame] | 16 | static int blkpg_do_ioctl(struct block_device *bdev, |
| 17 | struct blkpg_partition __user *upart, int op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | { |
Christoph Hellwig | 7f6be37 | 2021-08-10 17:45:10 +0200 | [diff] [blame] | 19 | struct gendisk *disk = bdev->bd_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | struct blkpg_partition p; |
| 21 | long long start, length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | if (!capable(CAP_SYS_ADMIN)) |
| 24 | return -EACCES; |
Arnd Bergmann | 5fb889f | 2019-11-28 11:28:41 +0100 | [diff] [blame] | 25 | if (copy_from_user(&p, upart, sizeof(struct blkpg_partition))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | return -EFAULT; |
Christoph Hellwig | fa01b1e | 2020-09-03 07:40:57 +0200 | [diff] [blame] | 27 | if (bdev_is_partition(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | return -EINVAL; |
Christoph Hellwig | fa9156a | 2020-04-14 09:28:53 +0200 | [diff] [blame] | 29 | |
| 30 | if (p.pno <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | return -EINVAL; |
Tejun Heo | 88e3412 | 2008-08-25 19:30:16 +0900 | [diff] [blame] | 32 | |
Christoph Hellwig | fa9156a | 2020-04-14 09:28:53 +0200 | [diff] [blame] | 33 | if (op == BLKPG_DEL_PARTITION) |
Christoph Hellwig | 926fbb1 | 2021-08-10 17:45:11 +0200 | [diff] [blame] | 34 | return bdev_del_partition(disk, p.pno); |
Tejun Heo | 88e3412 | 2008-08-25 19:30:16 +0900 | [diff] [blame] | 35 | |
Christoph Hellwig | fa9156a | 2020-04-14 09:28:53 +0200 | [diff] [blame] | 36 | start = p.start >> SECTOR_SHIFT; |
| 37 | length = p.length >> SECTOR_SHIFT; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 38 | |
Christoph Hellwig | fa9156a | 2020-04-14 09:28:53 +0200 | [diff] [blame] | 39 | switch (op) { |
| 40 | case BLKPG_ADD_PARTITION: |
| 41 | /* check if partition is aligned to blocksize */ |
| 42 | if (p.start & (bdev_logical_block_size(bdev) - 1)) |
| 43 | return -EINVAL; |
Christoph Hellwig | 7f6be37 | 2021-08-10 17:45:10 +0200 | [diff] [blame] | 44 | return bdev_add_partition(disk, p.pno, start, length); |
Christoph Hellwig | fa9156a | 2020-04-14 09:28:53 +0200 | [diff] [blame] | 45 | case BLKPG_RESIZE_PARTITION: |
Christoph Hellwig | 3d2e798 | 2021-08-10 17:45:12 +0200 | [diff] [blame] | 46 | return bdev_resize_partition(disk, p.pno, start, length); |
Christoph Hellwig | fa9156a | 2020-04-14 09:28:53 +0200 | [diff] [blame] | 47 | default: |
| 48 | return -EINVAL; |
| 49 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Arnd Bergmann | 5fb889f | 2019-11-28 11:28:41 +0100 | [diff] [blame] | 52 | static int blkpg_ioctl(struct block_device *bdev, |
| 53 | struct blkpg_ioctl_arg __user *arg) |
| 54 | { |
| 55 | struct blkpg_partition __user *udata; |
| 56 | int op; |
| 57 | |
| 58 | if (get_user(op, &arg->op) || get_user(udata, &arg->data)) |
| 59 | return -EFAULT; |
| 60 | |
| 61 | return blkpg_do_ioctl(bdev, udata, op); |
| 62 | } |
| 63 | |
| 64 | #ifdef CONFIG_COMPAT |
| 65 | struct compat_blkpg_ioctl_arg { |
| 66 | compat_int_t op; |
| 67 | compat_int_t flags; |
| 68 | compat_int_t datalen; |
| 69 | compat_caddr_t data; |
| 70 | }; |
| 71 | |
| 72 | static int compat_blkpg_ioctl(struct block_device *bdev, |
| 73 | struct compat_blkpg_ioctl_arg __user *arg) |
| 74 | { |
| 75 | compat_caddr_t udata; |
| 76 | int op; |
| 77 | |
| 78 | if (get_user(op, &arg->op) || get_user(udata, &arg->data)) |
| 79 | return -EFAULT; |
| 80 | |
| 81 | return blkpg_do_ioctl(bdev, compat_ptr(udata), op); |
| 82 | } |
| 83 | #endif |
| 84 | |
Christoph Hellwig | 4601b4b | 2021-02-23 16:18:22 +0100 | [diff] [blame] | 85 | static int blkdev_reread_part(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Christoph Hellwig | 4601b4b | 2021-02-23 16:18:22 +0100 | [diff] [blame] | 87 | struct block_device *tmp; |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 88 | |
Christoph Hellwig | fa01b1e | 2020-09-03 07:40:57 +0200 | [diff] [blame] | 89 | if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | return -EINVAL; |
| 91 | if (!capable(CAP_SYS_ADMIN)) |
| 92 | return -EACCES; |
Christoph Hellwig | ab4b570 | 2021-05-25 08:12:59 +0200 | [diff] [blame] | 93 | if (bdev->bd_disk->open_partitions) |
Christoph Hellwig | 68e6582 | 2021-04-21 18:05:02 +0200 | [diff] [blame] | 94 | return -EBUSY; |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 95 | |
Christoph Hellwig | 4601b4b | 2021-02-23 16:18:22 +0100 | [diff] [blame] | 96 | /* |
| 97 | * Reopen the device to revalidate the driver state and force a |
| 98 | * partition rescan. |
| 99 | */ |
| 100 | mode &= ~FMODE_EXCL; |
| 101 | set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state); |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 102 | |
Christoph Hellwig | 4601b4b | 2021-02-23 16:18:22 +0100 | [diff] [blame] | 103 | tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL); |
| 104 | if (IS_ERR(tmp)) |
| 105 | return PTR_ERR(tmp); |
| 106 | blkdev_put(tmp, mode); |
| 107 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 110 | static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode, |
| 111 | unsigned long arg, unsigned long flags) |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 112 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 113 | uint64_t range[2]; |
| 114 | uint64_t start, len; |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 115 | struct request_queue *q = bdev_get_queue(bdev); |
Shin'ichiro Kawasaki | 7607c44 | 2021-11-09 19:47:22 +0900 | [diff] [blame] | 116 | struct inode *inode = bdev->bd_inode; |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 117 | int err; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 118 | |
| 119 | if (!(mode & FMODE_WRITE)) |
| 120 | return -EBADF; |
| 121 | |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 122 | if (!blk_queue_discard(q)) |
| 123 | return -EOPNOTSUPP; |
| 124 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 125 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
| 126 | return -EFAULT; |
| 127 | |
| 128 | start = range[0]; |
| 129 | len = range[1]; |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 130 | |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 131 | if (start & 511) |
| 132 | return -EINVAL; |
| 133 | if (len & 511) |
| 134 | return -EINVAL; |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 135 | |
Christoph Hellwig | 946e993 | 2021-10-19 08:20:22 +0200 | [diff] [blame] | 136 | if (start + len > bdev_nr_bytes(bdev)) |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 137 | return -EINVAL; |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 138 | |
Shin'ichiro Kawasaki | 7607c44 | 2021-11-09 19:47:22 +0900 | [diff] [blame] | 139 | filemap_invalidate_lock(inode->i_mapping); |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 140 | err = truncate_bdev_range(bdev, mode, start, start + len - 1); |
| 141 | if (err) |
Shin'ichiro Kawasaki | 7607c44 | 2021-11-09 19:47:22 +0900 | [diff] [blame] | 142 | goto fail; |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 143 | |
Shin'ichiro Kawasaki | 7607c44 | 2021-11-09 19:47:22 +0900 | [diff] [blame] | 144 | err = blkdev_issue_discard(bdev, start >> 9, len >> 9, |
| 145 | GFP_KERNEL, flags); |
| 146 | |
| 147 | fail: |
| 148 | filemap_invalidate_unlock(inode->i_mapping); |
| 149 | return err; |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 152 | static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode, |
| 153 | unsigned long arg) |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 154 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 155 | uint64_t range[2]; |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 156 | uint64_t start, end, len; |
Shin'ichiro Kawasaki | 35e4c6c | 2021-11-09 19:47:23 +0900 | [diff] [blame] | 157 | struct inode *inode = bdev->bd_inode; |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 158 | int err; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 159 | |
| 160 | if (!(mode & FMODE_WRITE)) |
| 161 | return -EBADF; |
| 162 | |
| 163 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
| 164 | return -EFAULT; |
| 165 | |
| 166 | start = range[0]; |
| 167 | len = range[1]; |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 168 | end = start + len - 1; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 169 | |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 170 | if (start & 511) |
| 171 | return -EINVAL; |
| 172 | if (len & 511) |
| 173 | return -EINVAL; |
Christoph Hellwig | 946e993 | 2021-10-19 08:20:22 +0200 | [diff] [blame] | 174 | if (end >= (uint64_t)bdev_nr_bytes(bdev)) |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 175 | return -EINVAL; |
| 176 | if (end < start) |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 177 | return -EINVAL; |
| 178 | |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 179 | /* Invalidate the page cache, including dirty pages */ |
Shin'ichiro Kawasaki | 35e4c6c | 2021-11-09 19:47:23 +0900 | [diff] [blame] | 180 | filemap_invalidate_lock(inode->i_mapping); |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 181 | err = truncate_bdev_range(bdev, mode, start, end); |
| 182 | if (err) |
Shin'ichiro Kawasaki | 35e4c6c | 2021-11-09 19:47:23 +0900 | [diff] [blame] | 183 | goto fail; |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 184 | |
Shin'ichiro Kawasaki | 35e4c6c | 2021-11-09 19:47:23 +0900 | [diff] [blame] | 185 | err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL, |
| 186 | BLKDEV_ZERO_NOUNMAP); |
| 187 | |
| 188 | fail: |
| 189 | filemap_invalidate_unlock(inode->i_mapping); |
| 190 | return err; |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 193 | static int put_ushort(unsigned short __user *argp, unsigned short val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 195 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 198 | static int put_int(int __user *argp, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 200 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 203 | static int put_uint(unsigned int __user *argp, unsigned int val) |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 204 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 205 | return put_user(val, argp); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 208 | static int put_long(long __user *argp, long val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 210 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 213 | static int put_ulong(unsigned long __user *argp, unsigned long val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 215 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 218 | static int put_u64(u64 __user *argp, u64 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 220 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 223 | #ifdef CONFIG_COMPAT |
Bart Van Assche | c8210a5 | 2020-05-18 21:07:34 -0700 | [diff] [blame] | 224 | static int compat_put_long(compat_long_t __user *argp, long val) |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 225 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 226 | return put_user(val, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Bart Van Assche | c8210a5 | 2020-05-18 21:07:34 -0700 | [diff] [blame] | 229 | static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val) |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 230 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 231 | return put_user(val, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 232 | } |
| 233 | #endif |
| 234 | |
Arnd Bergmann | ee6a129 | 2019-11-28 15:48:10 +0100 | [diff] [blame] | 235 | #ifdef CONFIG_COMPAT |
| 236 | /* |
| 237 | * This is the equivalent of compat_ptr_ioctl(), to be used by block |
| 238 | * drivers that implement only commands that are completely compatible |
| 239 | * between 32-bit and 64-bit user space |
| 240 | */ |
| 241 | int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode, |
| 242 | unsigned cmd, unsigned long arg) |
| 243 | { |
| 244 | struct gendisk *disk = bdev->bd_disk; |
| 245 | |
| 246 | if (disk->fops->ioctl) |
| 247 | return disk->fops->ioctl(bdev, mode, cmd, |
| 248 | (unsigned long)compat_ptr(arg)); |
| 249 | |
| 250 | return -ENOIOCTLCMD; |
| 251 | } |
| 252 | EXPORT_SYMBOL(blkdev_compat_ptr_ioctl); |
| 253 | #endif |
| 254 | |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 255 | static int blkdev_pr_register(struct block_device *bdev, |
| 256 | struct pr_registration __user *arg) |
| 257 | { |
| 258 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 259 | struct pr_registration reg; |
| 260 | |
| 261 | if (!capable(CAP_SYS_ADMIN)) |
| 262 | return -EPERM; |
| 263 | if (!ops || !ops->pr_register) |
| 264 | return -EOPNOTSUPP; |
| 265 | if (copy_from_user(®, arg, sizeof(reg))) |
| 266 | return -EFAULT; |
| 267 | |
| 268 | if (reg.flags & ~PR_FL_IGNORE_KEY) |
| 269 | return -EOPNOTSUPP; |
| 270 | return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags); |
| 271 | } |
| 272 | |
| 273 | static int blkdev_pr_reserve(struct block_device *bdev, |
| 274 | struct pr_reservation __user *arg) |
| 275 | { |
| 276 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 277 | struct pr_reservation rsv; |
| 278 | |
| 279 | if (!capable(CAP_SYS_ADMIN)) |
| 280 | return -EPERM; |
| 281 | if (!ops || !ops->pr_reserve) |
| 282 | return -EOPNOTSUPP; |
| 283 | if (copy_from_user(&rsv, arg, sizeof(rsv))) |
| 284 | return -EFAULT; |
| 285 | |
| 286 | if (rsv.flags & ~PR_FL_IGNORE_KEY) |
| 287 | return -EOPNOTSUPP; |
| 288 | return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags); |
| 289 | } |
| 290 | |
| 291 | static int blkdev_pr_release(struct block_device *bdev, |
| 292 | struct pr_reservation __user *arg) |
| 293 | { |
| 294 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 295 | struct pr_reservation rsv; |
| 296 | |
| 297 | if (!capable(CAP_SYS_ADMIN)) |
| 298 | return -EPERM; |
| 299 | if (!ops || !ops->pr_release) |
| 300 | return -EOPNOTSUPP; |
| 301 | if (copy_from_user(&rsv, arg, sizeof(rsv))) |
| 302 | return -EFAULT; |
| 303 | |
| 304 | if (rsv.flags) |
| 305 | return -EOPNOTSUPP; |
| 306 | return ops->pr_release(bdev, rsv.key, rsv.type); |
| 307 | } |
| 308 | |
| 309 | static int blkdev_pr_preempt(struct block_device *bdev, |
| 310 | struct pr_preempt __user *arg, bool abort) |
| 311 | { |
| 312 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 313 | struct pr_preempt p; |
| 314 | |
| 315 | if (!capable(CAP_SYS_ADMIN)) |
| 316 | return -EPERM; |
| 317 | if (!ops || !ops->pr_preempt) |
| 318 | return -EOPNOTSUPP; |
| 319 | if (copy_from_user(&p, arg, sizeof(p))) |
| 320 | return -EFAULT; |
| 321 | |
| 322 | if (p.flags) |
| 323 | return -EOPNOTSUPP; |
| 324 | return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort); |
| 325 | } |
| 326 | |
| 327 | static int blkdev_pr_clear(struct block_device *bdev, |
| 328 | struct pr_clear __user *arg) |
| 329 | { |
| 330 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 331 | struct pr_clear c; |
| 332 | |
| 333 | if (!capable(CAP_SYS_ADMIN)) |
| 334 | return -EPERM; |
| 335 | if (!ops || !ops->pr_clear) |
| 336 | return -EOPNOTSUPP; |
| 337 | if (copy_from_user(&c, arg, sizeof(c))) |
| 338 | return -EFAULT; |
| 339 | |
| 340 | if (c.flags) |
| 341 | return -EOPNOTSUPP; |
| 342 | return ops->pr_clear(bdev, c.key); |
| 343 | } |
| 344 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 345 | static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode, |
| 346 | unsigned cmd, unsigned long arg) |
| 347 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 348 | if (!capable(CAP_SYS_ADMIN)) |
| 349 | return -EACCES; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 350 | fsync_bdev(bdev); |
| 351 | invalidate_bdev(bdev); |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int blkdev_roset(struct block_device *bdev, fmode_t mode, |
| 356 | unsigned cmd, unsigned long arg) |
| 357 | { |
| 358 | int ret, n; |
| 359 | |
Ilya Dryomov | bb749b3 | 2017-10-18 14:38:38 +0200 | [diff] [blame] | 360 | if (!capable(CAP_SYS_ADMIN)) |
| 361 | return -EACCES; |
| 362 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 363 | if (get_user(n, (int __user *)arg)) |
| 364 | return -EFAULT; |
Christoph Hellwig | e00adca | 2020-11-03 11:00:11 +0100 | [diff] [blame] | 365 | if (bdev->bd_disk->fops->set_read_only) { |
| 366 | ret = bdev->bd_disk->fops->set_read_only(bdev, n); |
| 367 | if (ret) |
| 368 | return ret; |
| 369 | } |
Christoph Hellwig | 83950d3 | 2020-11-23 16:36:02 +0100 | [diff] [blame] | 370 | bdev->bd_read_only = n; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int blkdev_getgeo(struct block_device *bdev, |
| 375 | struct hd_geometry __user *argp) |
| 376 | { |
| 377 | struct gendisk *disk = bdev->bd_disk; |
| 378 | struct hd_geometry geo; |
| 379 | int ret; |
| 380 | |
| 381 | if (!argp) |
| 382 | return -EINVAL; |
| 383 | if (!disk->fops->getgeo) |
| 384 | return -ENOTTY; |
| 385 | |
| 386 | /* |
| 387 | * We need to set the startsect first, the driver may |
| 388 | * want to override it. |
| 389 | */ |
| 390 | memset(&geo, 0, sizeof(geo)); |
| 391 | geo.start = get_start_sect(bdev); |
| 392 | ret = disk->fops->getgeo(bdev, &geo); |
| 393 | if (ret) |
| 394 | return ret; |
| 395 | if (copy_to_user(argp, &geo, sizeof(geo))) |
| 396 | return -EFAULT; |
| 397 | return 0; |
| 398 | } |
| 399 | |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 400 | #ifdef CONFIG_COMPAT |
| 401 | struct compat_hd_geometry { |
| 402 | unsigned char heads; |
| 403 | unsigned char sectors; |
| 404 | unsigned short cylinders; |
| 405 | u32 start; |
| 406 | }; |
| 407 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 408 | static int compat_hdio_getgeo(struct block_device *bdev, |
| 409 | struct compat_hd_geometry __user *ugeo) |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 410 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 411 | struct gendisk *disk = bdev->bd_disk; |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 412 | struct hd_geometry geo; |
| 413 | int ret; |
| 414 | |
| 415 | if (!ugeo) |
| 416 | return -EINVAL; |
| 417 | if (!disk->fops->getgeo) |
| 418 | return -ENOTTY; |
| 419 | |
| 420 | memset(&geo, 0, sizeof(geo)); |
| 421 | /* |
| 422 | * We need to set the startsect first, the driver may |
| 423 | * want to override it. |
| 424 | */ |
| 425 | geo.start = get_start_sect(bdev); |
| 426 | ret = disk->fops->getgeo(bdev, &geo); |
| 427 | if (ret) |
| 428 | return ret; |
| 429 | |
| 430 | ret = copy_to_user(ugeo, &geo, 4); |
| 431 | ret |= put_user(geo.start, &ugeo->start); |
| 432 | if (ret) |
| 433 | ret = -EFAULT; |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | #endif |
| 438 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 439 | /* set the logical block size */ |
| 440 | static int blkdev_bszset(struct block_device *bdev, fmode_t mode, |
| 441 | int __user *argp) |
| 442 | { |
| 443 | int ret, n; |
| 444 | |
| 445 | if (!capable(CAP_SYS_ADMIN)) |
| 446 | return -EACCES; |
| 447 | if (!argp) |
| 448 | return -EINVAL; |
| 449 | if (get_user(n, argp)) |
| 450 | return -EFAULT; |
| 451 | |
Christoph Hellwig | 4781628 | 2020-09-21 09:19:47 +0200 | [diff] [blame] | 452 | if (mode & FMODE_EXCL) |
| 453 | return set_blocksize(bdev, n); |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 454 | |
Christoph Hellwig | 4781628 | 2020-09-21 09:19:47 +0200 | [diff] [blame] | 455 | if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev))) |
| 456 | return -EBUSY; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 457 | ret = set_blocksize(bdev, n); |
Christoph Hellwig | 4781628 | 2020-09-21 09:19:47 +0200 | [diff] [blame] | 458 | blkdev_put(bdev, mode | FMODE_EXCL); |
| 459 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 460 | return ret; |
| 461 | } |
| 462 | |
Linus Torvalds | 07d106d | 2012-01-05 15:40:12 -0800 | [diff] [blame] | 463 | /* |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 464 | * Common commands that are handled the same way on native and compat |
| 465 | * user space. Note the separate arg/argp parameters that are needed |
| 466 | * to deal with the compat_ptr() conversion. |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 467 | */ |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 468 | static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode, |
| 469 | unsigned cmd, unsigned long arg, void __user *argp) |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 470 | { |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 471 | unsigned int max_sectors; |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 472 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 473 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | case BLKFLSBUF: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 475 | return blkdev_flushbuf(bdev, mode, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | case BLKROSET: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 477 | return blkdev_roset(bdev, mode, cmd, arg); |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 478 | case BLKDISCARD: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 479 | return blk_ioctl_discard(bdev, mode, arg, 0); |
| 480 | case BLKSECDISCARD: |
| 481 | return blk_ioctl_discard(bdev, mode, arg, |
| 482 | BLKDEV_DISCARD_SECURE); |
| 483 | case BLKZEROOUT: |
| 484 | return blk_ioctl_zeroout(bdev, mode, arg); |
Matteo Croce | 7957d93 | 2021-07-13 01:05:27 +0200 | [diff] [blame] | 485 | case BLKGETDISKSEQ: |
| 486 | return put_u64(argp, bdev->bd_disk->diskseq); |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 487 | case BLKREPORTZONE: |
| 488 | return blkdev_report_zones_ioctl(bdev, mode, cmd, arg); |
| 489 | case BLKRESETZONE: |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 490 | case BLKOPENZONE: |
| 491 | case BLKCLOSEZONE: |
| 492 | case BLKFINISHZONE: |
| 493 | return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg); |
Damien Le Moal | 72cd875 | 2018-10-12 19:08:45 +0900 | [diff] [blame] | 494 | case BLKGETZONESZ: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 495 | return put_uint(argp, bdev_zone_sectors(bdev)); |
Damien Le Moal | 65e4e3e | 2018-10-12 19:08:46 +0900 | [diff] [blame] | 496 | case BLKGETNRZONES: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 497 | return put_uint(argp, blkdev_nr_zones(bdev->bd_disk)); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 498 | case BLKROGET: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 499 | return put_int(argp, bdev_read_only(bdev) != 0); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 500 | case BLKSSZGET: /* get block device logical block size */ |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 501 | return put_int(argp, bdev_logical_block_size(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 502 | case BLKPBSZGET: /* get block device physical block size */ |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 503 | return put_uint(argp, bdev_physical_block_size(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 504 | case BLKIOMIN: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 505 | return put_uint(argp, bdev_io_min(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 506 | case BLKIOOPT: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 507 | return put_uint(argp, bdev_io_opt(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 508 | case BLKALIGNOFF: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 509 | return put_int(argp, bdev_alignment_offset(bdev)); |
Martin K. Petersen | 98262f2 | 2009-12-03 09:24:48 +0100 | [diff] [blame] | 510 | case BLKDISCARDZEROES: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 511 | return put_uint(argp, 0); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 512 | case BLKSECTGET: |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 513 | max_sectors = min_t(unsigned int, USHRT_MAX, |
| 514 | queue_max_sectors(bdev_get_queue(bdev))); |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 515 | return put_ushort(argp, max_sectors); |
Martin K. Petersen | ef00f59 | 2012-01-11 16:29:31 +0100 | [diff] [blame] | 516 | case BLKROTATIONAL: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 517 | return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev))); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 518 | case BLKRASET: |
| 519 | case BLKFRASET: |
| 520 | if(!capable(CAP_SYS_ADMIN)) |
| 521 | return -EACCES; |
Christoph Hellwig | a11d7fc | 2021-08-09 16:17:44 +0200 | [diff] [blame] | 522 | bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 523 | return 0; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 524 | case BLKRRPART: |
Christoph Hellwig | 4601b4b | 2021-02-23 16:18:22 +0100 | [diff] [blame] | 525 | return blkdev_reread_part(bdev, mode); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 526 | case BLKTRACESTART: |
| 527 | case BLKTRACESTOP: |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 528 | case BLKTRACETEARDOWN: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 529 | return blk_trace_ioctl(bdev, cmd, argp); |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 530 | case IOC_PR_REGISTER: |
| 531 | return blkdev_pr_register(bdev, argp); |
| 532 | case IOC_PR_RESERVE: |
| 533 | return blkdev_pr_reserve(bdev, argp); |
| 534 | case IOC_PR_RELEASE: |
| 535 | return blkdev_pr_release(bdev, argp); |
| 536 | case IOC_PR_PREEMPT: |
| 537 | return blkdev_pr_preempt(bdev, argp, false); |
| 538 | case IOC_PR_PREEMPT_ABORT: |
| 539 | return blkdev_pr_preempt(bdev, argp, true); |
| 540 | case IOC_PR_CLEAR: |
| 541 | return blkdev_pr_clear(bdev, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 542 | default: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 543 | return -ENOIOCTLCMD; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * Always keep this in sync with compat_blkdev_ioctl() |
| 549 | * to handle all incompatible commands in both functions. |
| 550 | * |
| 551 | * New commands must be compatible and go into blkdev_common_ioctl |
| 552 | */ |
Christoph Hellwig | 8a70951 | 2021-10-12 12:44:50 +0200 | [diff] [blame] | 553 | long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 554 | { |
Christoph Hellwig | 8a70951 | 2021-10-12 12:44:50 +0200 | [diff] [blame] | 555 | struct block_device *bdev = I_BDEV(file->f_mapping->host); |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 556 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | 8a70951 | 2021-10-12 12:44:50 +0200 | [diff] [blame] | 557 | fmode_t mode = file->f_mode; |
Christoph Hellwig | 8a70951 | 2021-10-12 12:44:50 +0200 | [diff] [blame] | 558 | int ret; |
| 559 | |
| 560 | /* |
| 561 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have |
| 562 | * to updated it before every ioctl. |
| 563 | */ |
| 564 | if (file->f_flags & O_NDELAY) |
| 565 | mode |= FMODE_NDELAY; |
| 566 | else |
| 567 | mode &= ~FMODE_NDELAY; |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 568 | |
| 569 | switch (cmd) { |
| 570 | /* These need separate implementations for the data structure */ |
| 571 | case HDIO_GETGEO: |
| 572 | return blkdev_getgeo(bdev, argp); |
| 573 | case BLKPG: |
| 574 | return blkpg_ioctl(bdev, argp); |
| 575 | |
| 576 | /* Compat mode returns 32-bit data instead of 'long' */ |
| 577 | case BLKRAGET: |
| 578 | case BLKFRAGET: |
| 579 | if (!argp) |
| 580 | return -EINVAL; |
Christoph Hellwig | a11d7fc | 2021-08-09 16:17:44 +0200 | [diff] [blame] | 581 | return put_long(argp, |
| 582 | (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512); |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 583 | case BLKGETSIZE: |
Christoph Hellwig | 946e993 | 2021-10-19 08:20:22 +0200 | [diff] [blame] | 584 | if (bdev_nr_sectors(bdev) > ~0UL) |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 585 | return -EFBIG; |
Christoph Hellwig | 946e993 | 2021-10-19 08:20:22 +0200 | [diff] [blame] | 586 | return put_ulong(argp, bdev_nr_sectors(bdev)); |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 587 | |
| 588 | /* The data is compatible, but the command number is different */ |
| 589 | case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */ |
| 590 | return put_int(argp, block_size(bdev)); |
| 591 | case BLKBSZSET: |
| 592 | return blkdev_bszset(bdev, mode, argp); |
| 593 | case BLKGETSIZE64: |
Christoph Hellwig | 946e993 | 2021-10-19 08:20:22 +0200 | [diff] [blame] | 594 | return put_u64(argp, bdev_nr_bytes(bdev)); |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 595 | |
| 596 | /* Incompatible alignment on i386 */ |
| 597 | case BLKTRACESETUP: |
| 598 | return blk_trace_ioctl(bdev, cmd, argp); |
| 599 | default: |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); |
Christoph Hellwig | a7cb3d2 | 2020-11-03 11:00:18 +0100 | [diff] [blame] | 604 | if (ret != -ENOIOCTLCMD) |
| 605 | return ret; |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 606 | |
Christoph Hellwig | a7cb3d2 | 2020-11-03 11:00:18 +0100 | [diff] [blame] | 607 | if (!bdev->bd_disk->fops->ioctl) |
| 608 | return -ENOTTY; |
| 609 | return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 610 | } |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 611 | |
| 612 | #ifdef CONFIG_COMPAT |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 613 | |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 614 | #define BLKBSZGET_32 _IOR(0x12, 112, int) |
| 615 | #define BLKBSZSET_32 _IOW(0x12, 113, int) |
| 616 | #define BLKGETSIZE64_32 _IOR(0x12, 114, int) |
| 617 | |
| 618 | /* Most of the generic ioctls are handled in the normal fallback path. |
| 619 | This assumes the blkdev's low level compat_ioctl always returns |
| 620 | ENOIOCTLCMD for unknown ioctls. */ |
| 621 | long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
| 622 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 623 | int ret; |
| 624 | void __user *argp = compat_ptr(arg); |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 625 | struct block_device *bdev = I_BDEV(file->f_mapping->host); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 626 | struct gendisk *disk = bdev->bd_disk; |
| 627 | fmode_t mode = file->f_mode; |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have |
| 631 | * to updated it before every ioctl. |
| 632 | */ |
| 633 | if (file->f_flags & O_NDELAY) |
| 634 | mode |= FMODE_NDELAY; |
| 635 | else |
| 636 | mode &= ~FMODE_NDELAY; |
| 637 | |
| 638 | switch (cmd) { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 639 | /* These need separate implementations for the data structure */ |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 640 | case HDIO_GETGEO: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 641 | return compat_hdio_getgeo(bdev, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 642 | case BLKPG: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 643 | return compat_blkpg_ioctl(bdev, argp); |
| 644 | |
| 645 | /* Compat mode returns 32-bit data instead of 'long' */ |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 646 | case BLKRAGET: |
| 647 | case BLKFRAGET: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 648 | if (!argp) |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 649 | return -EINVAL; |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 650 | return compat_put_long(argp, |
Christoph Hellwig | a11d7fc | 2021-08-09 16:17:44 +0200 | [diff] [blame] | 651 | (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 652 | case BLKGETSIZE: |
Christoph Hellwig | 946e993 | 2021-10-19 08:20:22 +0200 | [diff] [blame] | 653 | if (bdev_nr_sectors(bdev) > ~0UL) |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 654 | return -EFBIG; |
Christoph Hellwig | 946e993 | 2021-10-19 08:20:22 +0200 | [diff] [blame] | 655 | return compat_put_ulong(argp, bdev_nr_sectors(bdev)); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 656 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 657 | /* The data is compatible, but the command number is different */ |
| 658 | case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */ |
| 659 | return put_int(argp, bdev_logical_block_size(bdev)); |
| 660 | case BLKBSZSET_32: |
| 661 | return blkdev_bszset(bdev, mode, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 662 | case BLKGETSIZE64_32: |
Christoph Hellwig | 946e993 | 2021-10-19 08:20:22 +0200 | [diff] [blame] | 663 | return put_u64(argp, bdev_nr_bytes(bdev)); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 664 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 665 | /* Incompatible alignment on i386 */ |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 666 | case BLKTRACESETUP32: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 667 | return blk_trace_ioctl(bdev, cmd, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 668 | default: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 669 | break; |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 670 | } |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 671 | |
| 672 | ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); |
| 673 | if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl) |
| 674 | ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg); |
| 675 | |
| 676 | return ret; |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 677 | } |
| 678 | #endif |