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