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