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