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