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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Arnd Bergmann | 5fb889f | 2019-11-28 11:28:41 +0100 | [diff] [blame] | 15 | static int blkpg_do_ioctl(struct block_device *bdev, |
| 16 | struct blkpg_partition __user *upart, int op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | { |
| 18 | struct block_device *bdevp; |
| 19 | struct gendisk *disk; |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 20 | struct hd_struct *part, *lpart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | struct blkpg_partition p; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 22 | struct disk_part_iter piter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | long long start, length; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 24 | int partno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | if (!capable(CAP_SYS_ADMIN)) |
| 27 | return -EACCES; |
Arnd Bergmann | 5fb889f | 2019-11-28 11:28:41 +0100 | [diff] [blame] | 28 | if (copy_from_user(&p, upart, sizeof(struct blkpg_partition))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | return -EFAULT; |
| 30 | disk = bdev->bd_disk; |
| 31 | if (bdev != bdev->bd_contains) |
| 32 | return -EINVAL; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 33 | partno = p.pno; |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 34 | if (partno <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | return -EINVAL; |
Arnd Bergmann | 5fb889f | 2019-11-28 11:28:41 +0100 | [diff] [blame] | 36 | switch (op) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | case BLKPG_ADD_PARTITION: |
| 38 | start = p.start >> 9; |
| 39 | length = p.length >> 9; |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 40 | /* check for fit in a hd_struct */ |
| 41 | if (sizeof(sector_t) == sizeof(long) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | sizeof(long long) > sizeof(long)) { |
| 43 | long pstart = start, plength = length; |
| 44 | if (pstart != start || plength != length |
Alan Cox | 2bd6efa | 2012-09-17 11:47:13 +0100 | [diff] [blame] | 45 | || pstart < 0 || plength < 0 || partno > 65535) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | return -EINVAL; |
| 47 | } |
Stefan Haberland | 633395b | 2016-12-19 17:15:50 +0100 | [diff] [blame] | 48 | /* check if partition is aligned to blocksize */ |
| 49 | if (p.start & (bdev_logical_block_size(bdev) - 1)) |
| 50 | return -EINVAL; |
Tejun Heo | 88e3412 | 2008-08-25 19:30:16 +0900 | [diff] [blame] | 51 | |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 52 | mutex_lock(&bdev->bd_mutex); |
Tejun Heo | 88e3412 | 2008-08-25 19:30:16 +0900 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* overlap? */ |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 55 | disk_part_iter_init(&piter, disk, |
| 56 | DISK_PITER_INCL_EMPTY); |
| 57 | while ((part = disk_part_iter_next(&piter))) { |
| 58 | if (!(start + length <= part->start_sect || |
| 59 | start >= part->start_sect + part->nr_sects)) { |
| 60 | disk_part_iter_exit(&piter); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 61 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return -EBUSY; |
| 63 | } |
| 64 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 65 | disk_part_iter_exit(&piter); |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* all seems OK */ |
Tejun Heo | ba32929 | 2008-11-10 15:29:58 +0900 | [diff] [blame] | 68 | part = add_partition(disk, partno, start, length, |
Will Drewry | 6d1d805 | 2010-08-31 15:47:05 -0500 | [diff] [blame] | 69 | ADDPART_FLAG_NONE, NULL); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 70 | mutex_unlock(&bdev->bd_mutex); |
Duan Jiong | c7d1ba4 | 2013-11-06 15:56:39 +0800 | [diff] [blame] | 71 | return PTR_ERR_OR_ZERO(part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | case BLKPG_DEL_PARTITION: |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 73 | part = disk_get_part(disk, partno); |
| 74 | if (!part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return -ENXIO; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 76 | |
| 77 | bdevp = bdget(part_devt(part)); |
| 78 | disk_put_part(part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | if (!bdevp) |
| 80 | return -ENOMEM; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 81 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 82 | mutex_lock(&bdevp->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (bdevp->bd_openers) { |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 84 | mutex_unlock(&bdevp->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | bdput(bdevp); |
| 86 | return -EBUSY; |
| 87 | } |
| 88 | /* all seems OK */ |
| 89 | fsync_bdev(bdevp); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 90 | invalidate_bdev(bdevp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Peter Zijlstra | 6d740cd | 2007-02-20 13:58:18 -0800 | [diff] [blame] | 92 | mutex_lock_nested(&bdev->bd_mutex, 1); |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 93 | delete_partition(disk, partno); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 94 | mutex_unlock(&bdev->bd_mutex); |
| 95 | mutex_unlock(&bdevp->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | bdput(bdevp); |
| 97 | |
| 98 | return 0; |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 99 | case BLKPG_RESIZE_PARTITION: |
| 100 | start = p.start >> 9; |
| 101 | /* new length of partition in bytes */ |
| 102 | length = p.length >> 9; |
| 103 | /* check for fit in a hd_struct */ |
| 104 | if (sizeof(sector_t) == sizeof(long) && |
| 105 | sizeof(long long) > sizeof(long)) { |
| 106 | long pstart = start, plength = length; |
| 107 | if (pstart != start || plength != length |
| 108 | || pstart < 0 || plength < 0) |
| 109 | return -EINVAL; |
| 110 | } |
| 111 | part = disk_get_part(disk, partno); |
| 112 | if (!part) |
| 113 | return -ENXIO; |
| 114 | bdevp = bdget(part_devt(part)); |
| 115 | if (!bdevp) { |
| 116 | disk_put_part(part); |
| 117 | return -ENOMEM; |
| 118 | } |
| 119 | mutex_lock(&bdevp->bd_mutex); |
| 120 | mutex_lock_nested(&bdev->bd_mutex, 1); |
| 121 | if (start != part->start_sect) { |
| 122 | mutex_unlock(&bdevp->bd_mutex); |
| 123 | mutex_unlock(&bdev->bd_mutex); |
| 124 | bdput(bdevp); |
| 125 | disk_put_part(part); |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | /* overlap? */ |
| 129 | disk_part_iter_init(&piter, disk, |
| 130 | DISK_PITER_INCL_EMPTY); |
| 131 | while ((lpart = disk_part_iter_next(&piter))) { |
| 132 | if (lpart->partno != partno && |
| 133 | !(start + length <= lpart->start_sect || |
| 134 | start >= lpart->start_sect + lpart->nr_sects) |
| 135 | ) { |
| 136 | disk_part_iter_exit(&piter); |
| 137 | mutex_unlock(&bdevp->bd_mutex); |
| 138 | mutex_unlock(&bdev->bd_mutex); |
| 139 | bdput(bdevp); |
| 140 | disk_put_part(part); |
| 141 | return -EBUSY; |
| 142 | } |
| 143 | } |
| 144 | disk_part_iter_exit(&piter); |
| 145 | part_nr_sects_write(part, (sector_t)length); |
| 146 | i_size_write(bdevp->bd_inode, p.length); |
| 147 | mutex_unlock(&bdevp->bd_mutex); |
| 148 | mutex_unlock(&bdev->bd_mutex); |
| 149 | bdput(bdevp); |
| 150 | disk_put_part(part); |
| 151 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | default: |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | } |
| 156 | |
Arnd Bergmann | 5fb889f | 2019-11-28 11:28:41 +0100 | [diff] [blame] | 157 | static int blkpg_ioctl(struct block_device *bdev, |
| 158 | struct blkpg_ioctl_arg __user *arg) |
| 159 | { |
| 160 | struct blkpg_partition __user *udata; |
| 161 | int op; |
| 162 | |
| 163 | if (get_user(op, &arg->op) || get_user(udata, &arg->data)) |
| 164 | return -EFAULT; |
| 165 | |
| 166 | return blkpg_do_ioctl(bdev, udata, op); |
| 167 | } |
| 168 | |
| 169 | #ifdef CONFIG_COMPAT |
| 170 | struct compat_blkpg_ioctl_arg { |
| 171 | compat_int_t op; |
| 172 | compat_int_t flags; |
| 173 | compat_int_t datalen; |
| 174 | compat_caddr_t data; |
| 175 | }; |
| 176 | |
| 177 | static int compat_blkpg_ioctl(struct block_device *bdev, |
| 178 | struct compat_blkpg_ioctl_arg __user *arg) |
| 179 | { |
| 180 | compat_caddr_t udata; |
| 181 | int op; |
| 182 | |
| 183 | if (get_user(op, &arg->op) || get_user(udata, &arg->data)) |
| 184 | return -EFAULT; |
| 185 | |
| 186 | return blkpg_do_ioctl(bdev, compat_ptr(udata), op); |
| 187 | } |
| 188 | #endif |
| 189 | |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 190 | static int blkdev_reread_part(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 192 | int ret; |
| 193 | |
Christoph Hellwig | 142fe8f | 2019-11-14 15:34:35 +0100 | [diff] [blame] | 194 | if (!disk_part_scan_enabled(bdev->bd_disk) || bdev != bdev->bd_contains) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return -EINVAL; |
| 196 | if (!capable(CAP_SYS_ADMIN)) |
| 197 | return -EACCES; |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 198 | |
Ming Lei | b04a5636 | 2015-05-06 12:26:27 +0800 | [diff] [blame] | 199 | mutex_lock(&bdev->bd_mutex); |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 200 | ret = bdev_disk_changed(bdev, false); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 201 | mutex_unlock(&bdev->bd_mutex); |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 202 | |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 203 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 206 | static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode, |
| 207 | unsigned long arg, unsigned long flags) |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 208 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 209 | uint64_t range[2]; |
| 210 | uint64_t start, len; |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 211 | struct request_queue *q = bdev_get_queue(bdev); |
| 212 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 213 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 214 | |
| 215 | if (!(mode & FMODE_WRITE)) |
| 216 | return -EBADF; |
| 217 | |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 218 | if (!blk_queue_discard(q)) |
| 219 | return -EOPNOTSUPP; |
| 220 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 221 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
| 222 | return -EFAULT; |
| 223 | |
| 224 | start = range[0]; |
| 225 | len = range[1]; |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 226 | |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 227 | if (start & 511) |
| 228 | return -EINVAL; |
| 229 | if (len & 511) |
| 230 | return -EINVAL; |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 231 | |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 232 | if (start + len > i_size_read(bdev->bd_inode)) |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 233 | return -EINVAL; |
Ming Lei | 0bd1ed4 | 2018-02-10 08:46:17 +0800 | [diff] [blame] | 234 | truncate_inode_pages_range(mapping, start, start + len - 1); |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 235 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, |
| 236 | GFP_KERNEL, flags); |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 239 | static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode, |
| 240 | unsigned long arg) |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 241 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 242 | uint64_t range[2]; |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 243 | struct address_space *mapping; |
| 244 | uint64_t start, end, len; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 245 | |
| 246 | if (!(mode & FMODE_WRITE)) |
| 247 | return -EBADF; |
| 248 | |
| 249 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
| 250 | return -EFAULT; |
| 251 | |
| 252 | start = range[0]; |
| 253 | len = range[1]; |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 254 | end = start + len - 1; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 255 | |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 256 | if (start & 511) |
| 257 | return -EINVAL; |
| 258 | if (len & 511) |
| 259 | return -EINVAL; |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 260 | if (end >= (uint64_t)i_size_read(bdev->bd_inode)) |
| 261 | return -EINVAL; |
| 262 | if (end < start) |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 263 | return -EINVAL; |
| 264 | |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 265 | /* Invalidate the page cache, including dirty pages */ |
| 266 | mapping = bdev->bd_inode->i_mapping; |
| 267 | truncate_inode_pages_range(mapping, start, end); |
| 268 | |
| 269 | return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL, |
Christoph Hellwig | ee472d8 | 2017-04-05 19:21:08 +0200 | [diff] [blame] | 270 | BLKDEV_ZERO_NOUNMAP); |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 273 | static int put_ushort(unsigned short __user *argp, unsigned short val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 275 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 278 | static int put_int(int __user *argp, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 280 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 283 | static int put_uint(unsigned int __user *argp, unsigned int val) |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 284 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 285 | return put_user(val, argp); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 288 | static int put_long(long __user *argp, long val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 290 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 293 | static int put_ulong(unsigned long __user *argp, unsigned long val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 295 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 298 | static int put_u64(u64 __user *argp, u64 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 300 | return put_user(val, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 303 | #ifdef CONFIG_COMPAT |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 304 | static int compat_put_long(compat_long_t *argp, long val) |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 305 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 306 | return put_user(val, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 309 | static int compat_put_ulong(compat_ulong_t *argp, compat_ulong_t val) |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 310 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 311 | return put_user(val, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 312 | } |
| 313 | #endif |
| 314 | |
Al Viro | 633a08b | 2007-08-29 20:34:12 -0400 | [diff] [blame] | 315 | int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode, |
| 316 | unsigned cmd, unsigned long arg) |
| 317 | { |
| 318 | struct gendisk *disk = bdev->bd_disk; |
Al Viro | d4430d6 | 2008-03-02 09:09:22 -0500 | [diff] [blame] | 319 | |
| 320 | if (disk->fops->ioctl) |
| 321 | return disk->fops->ioctl(bdev, mode, cmd, arg); |
| 322 | |
Al Viro | 633a08b | 2007-08-29 20:34:12 -0400 | [diff] [blame] | 323 | return -ENOTTY; |
| 324 | } |
| 325 | /* |
| 326 | * For the record: _GPL here is only because somebody decided to slap it |
| 327 | * on the previous export. Sheer idiocy, since it wasn't copyrightable |
| 328 | * at all and could be open-coded without any exports by anybody who cares. |
| 329 | */ |
| 330 | EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl); |
| 331 | |
Arnd Bergmann | ee6a129 | 2019-11-28 15:48:10 +0100 | [diff] [blame] | 332 | #ifdef CONFIG_COMPAT |
| 333 | /* |
| 334 | * This is the equivalent of compat_ptr_ioctl(), to be used by block |
| 335 | * drivers that implement only commands that are completely compatible |
| 336 | * between 32-bit and 64-bit user space |
| 337 | */ |
| 338 | int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode, |
| 339 | unsigned cmd, unsigned long arg) |
| 340 | { |
| 341 | struct gendisk *disk = bdev->bd_disk; |
| 342 | |
| 343 | if (disk->fops->ioctl) |
| 344 | return disk->fops->ioctl(bdev, mode, cmd, |
| 345 | (unsigned long)compat_ptr(arg)); |
| 346 | |
| 347 | return -ENOIOCTLCMD; |
| 348 | } |
| 349 | EXPORT_SYMBOL(blkdev_compat_ptr_ioctl); |
| 350 | #endif |
| 351 | |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 352 | static int blkdev_pr_register(struct block_device *bdev, |
| 353 | struct pr_registration __user *arg) |
| 354 | { |
| 355 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 356 | struct pr_registration reg; |
| 357 | |
| 358 | if (!capable(CAP_SYS_ADMIN)) |
| 359 | return -EPERM; |
| 360 | if (!ops || !ops->pr_register) |
| 361 | return -EOPNOTSUPP; |
| 362 | if (copy_from_user(®, arg, sizeof(reg))) |
| 363 | return -EFAULT; |
| 364 | |
| 365 | if (reg.flags & ~PR_FL_IGNORE_KEY) |
| 366 | return -EOPNOTSUPP; |
| 367 | return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags); |
| 368 | } |
| 369 | |
| 370 | static int blkdev_pr_reserve(struct block_device *bdev, |
| 371 | struct pr_reservation __user *arg) |
| 372 | { |
| 373 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 374 | struct pr_reservation rsv; |
| 375 | |
| 376 | if (!capable(CAP_SYS_ADMIN)) |
| 377 | return -EPERM; |
| 378 | if (!ops || !ops->pr_reserve) |
| 379 | return -EOPNOTSUPP; |
| 380 | if (copy_from_user(&rsv, arg, sizeof(rsv))) |
| 381 | return -EFAULT; |
| 382 | |
| 383 | if (rsv.flags & ~PR_FL_IGNORE_KEY) |
| 384 | return -EOPNOTSUPP; |
| 385 | return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags); |
| 386 | } |
| 387 | |
| 388 | static int blkdev_pr_release(struct block_device *bdev, |
| 389 | struct pr_reservation __user *arg) |
| 390 | { |
| 391 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 392 | struct pr_reservation rsv; |
| 393 | |
| 394 | if (!capable(CAP_SYS_ADMIN)) |
| 395 | return -EPERM; |
| 396 | if (!ops || !ops->pr_release) |
| 397 | return -EOPNOTSUPP; |
| 398 | if (copy_from_user(&rsv, arg, sizeof(rsv))) |
| 399 | return -EFAULT; |
| 400 | |
| 401 | if (rsv.flags) |
| 402 | return -EOPNOTSUPP; |
| 403 | return ops->pr_release(bdev, rsv.key, rsv.type); |
| 404 | } |
| 405 | |
| 406 | static int blkdev_pr_preempt(struct block_device *bdev, |
| 407 | struct pr_preempt __user *arg, bool abort) |
| 408 | { |
| 409 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 410 | struct pr_preempt p; |
| 411 | |
| 412 | if (!capable(CAP_SYS_ADMIN)) |
| 413 | return -EPERM; |
| 414 | if (!ops || !ops->pr_preempt) |
| 415 | return -EOPNOTSUPP; |
| 416 | if (copy_from_user(&p, arg, sizeof(p))) |
| 417 | return -EFAULT; |
| 418 | |
| 419 | if (p.flags) |
| 420 | return -EOPNOTSUPP; |
| 421 | return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort); |
| 422 | } |
| 423 | |
| 424 | static int blkdev_pr_clear(struct block_device *bdev, |
| 425 | struct pr_clear __user *arg) |
| 426 | { |
| 427 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 428 | struct pr_clear c; |
| 429 | |
| 430 | if (!capable(CAP_SYS_ADMIN)) |
| 431 | return -EPERM; |
| 432 | if (!ops || !ops->pr_clear) |
| 433 | return -EOPNOTSUPP; |
| 434 | if (copy_from_user(&c, arg, sizeof(c))) |
| 435 | return -EFAULT; |
| 436 | |
| 437 | if (c.flags) |
| 438 | return -EOPNOTSUPP; |
| 439 | return ops->pr_clear(bdev, c.key); |
| 440 | } |
| 441 | |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 442 | /* |
Linus Torvalds | 07d106d | 2012-01-05 15:40:12 -0800 | [diff] [blame] | 443 | * Is it an unrecognized ioctl? The correct returns are either |
| 444 | * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a |
| 445 | * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl |
| 446 | * code before returning. |
| 447 | * |
| 448 | * Confused drivers sometimes return EINVAL, which is wrong. It |
| 449 | * means "I understood the ioctl command, but the parameters to |
| 450 | * it were wrong". |
| 451 | * |
| 452 | * We should aim to just fix the broken drivers, the EINVAL case |
| 453 | * should go away. |
| 454 | */ |
| 455 | static inline int is_unrecognized_ioctl(int ret) |
| 456 | { |
| 457 | return ret == -EINVAL || |
| 458 | ret == -ENOTTY || |
| 459 | ret == -ENOIOCTLCMD; |
| 460 | } |
| 461 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 462 | static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode, |
| 463 | unsigned cmd, unsigned long arg) |
| 464 | { |
| 465 | int ret; |
| 466 | |
| 467 | if (!capable(CAP_SYS_ADMIN)) |
| 468 | return -EACCES; |
| 469 | |
| 470 | ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
| 471 | if (!is_unrecognized_ioctl(ret)) |
| 472 | return ret; |
| 473 | |
| 474 | fsync_bdev(bdev); |
| 475 | invalidate_bdev(bdev); |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int blkdev_roset(struct block_device *bdev, fmode_t mode, |
| 480 | unsigned cmd, unsigned long arg) |
| 481 | { |
| 482 | int ret, n; |
| 483 | |
Ilya Dryomov | bb749b3 | 2017-10-18 14:38:38 +0200 | [diff] [blame] | 484 | if (!capable(CAP_SYS_ADMIN)) |
| 485 | return -EACCES; |
| 486 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 487 | ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
| 488 | if (!is_unrecognized_ioctl(ret)) |
| 489 | return ret; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 490 | if (get_user(n, (int __user *)arg)) |
| 491 | return -EFAULT; |
| 492 | set_device_ro(bdev, n); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int blkdev_getgeo(struct block_device *bdev, |
| 497 | struct hd_geometry __user *argp) |
| 498 | { |
| 499 | struct gendisk *disk = bdev->bd_disk; |
| 500 | struct hd_geometry geo; |
| 501 | int ret; |
| 502 | |
| 503 | if (!argp) |
| 504 | return -EINVAL; |
| 505 | if (!disk->fops->getgeo) |
| 506 | return -ENOTTY; |
| 507 | |
| 508 | /* |
| 509 | * We need to set the startsect first, the driver may |
| 510 | * want to override it. |
| 511 | */ |
| 512 | memset(&geo, 0, sizeof(geo)); |
| 513 | geo.start = get_start_sect(bdev); |
| 514 | ret = disk->fops->getgeo(bdev, &geo); |
| 515 | if (ret) |
| 516 | return ret; |
| 517 | if (copy_to_user(argp, &geo, sizeof(geo))) |
| 518 | return -EFAULT; |
| 519 | return 0; |
| 520 | } |
| 521 | |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 522 | #ifdef CONFIG_COMPAT |
| 523 | struct compat_hd_geometry { |
| 524 | unsigned char heads; |
| 525 | unsigned char sectors; |
| 526 | unsigned short cylinders; |
| 527 | u32 start; |
| 528 | }; |
| 529 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 530 | static int compat_hdio_getgeo(struct block_device *bdev, |
| 531 | struct compat_hd_geometry __user *ugeo) |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 532 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 533 | struct gendisk *disk = bdev->bd_disk; |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 534 | struct hd_geometry geo; |
| 535 | int ret; |
| 536 | |
| 537 | if (!ugeo) |
| 538 | return -EINVAL; |
| 539 | if (!disk->fops->getgeo) |
| 540 | return -ENOTTY; |
| 541 | |
| 542 | memset(&geo, 0, sizeof(geo)); |
| 543 | /* |
| 544 | * We need to set the startsect first, the driver may |
| 545 | * want to override it. |
| 546 | */ |
| 547 | geo.start = get_start_sect(bdev); |
| 548 | ret = disk->fops->getgeo(bdev, &geo); |
| 549 | if (ret) |
| 550 | return ret; |
| 551 | |
| 552 | ret = copy_to_user(ugeo, &geo, 4); |
| 553 | ret |= put_user(geo.start, &ugeo->start); |
| 554 | if (ret) |
| 555 | ret = -EFAULT; |
| 556 | |
| 557 | return ret; |
| 558 | } |
| 559 | #endif |
| 560 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 561 | /* set the logical block size */ |
| 562 | static int blkdev_bszset(struct block_device *bdev, fmode_t mode, |
| 563 | int __user *argp) |
| 564 | { |
| 565 | int ret, n; |
| 566 | |
| 567 | if (!capable(CAP_SYS_ADMIN)) |
| 568 | return -EACCES; |
| 569 | if (!argp) |
| 570 | return -EINVAL; |
| 571 | if (get_user(n, argp)) |
| 572 | return -EFAULT; |
| 573 | |
| 574 | if (!(mode & FMODE_EXCL)) { |
| 575 | bdgrab(bdev); |
| 576 | if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0) |
| 577 | return -EBUSY; |
| 578 | } |
| 579 | |
| 580 | ret = set_blocksize(bdev, n); |
| 581 | if (!(mode & FMODE_EXCL)) |
| 582 | blkdev_put(bdev, mode | FMODE_EXCL); |
| 583 | return ret; |
| 584 | } |
| 585 | |
Linus Torvalds | 07d106d | 2012-01-05 15:40:12 -0800 | [diff] [blame] | 586 | /* |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 587 | * Common commands that are handled the same way on native and compat |
| 588 | * user space. Note the separate arg/argp parameters that are needed |
| 589 | * to deal with the compat_ptr() conversion. |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 590 | */ |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 591 | static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode, |
| 592 | unsigned cmd, unsigned long arg, void __user *argp) |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 593 | { |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 594 | unsigned int max_sectors; |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 595 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 596 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | case BLKFLSBUF: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 598 | return blkdev_flushbuf(bdev, mode, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | case BLKROSET: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 600 | return blkdev_roset(bdev, mode, cmd, arg); |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 601 | case BLKDISCARD: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 602 | return blk_ioctl_discard(bdev, mode, arg, 0); |
| 603 | case BLKSECDISCARD: |
| 604 | return blk_ioctl_discard(bdev, mode, arg, |
| 605 | BLKDEV_DISCARD_SECURE); |
| 606 | case BLKZEROOUT: |
| 607 | return blk_ioctl_zeroout(bdev, mode, arg); |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 608 | case BLKREPORTZONE: |
| 609 | return blkdev_report_zones_ioctl(bdev, mode, cmd, arg); |
| 610 | case BLKRESETZONE: |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 611 | case BLKOPENZONE: |
| 612 | case BLKCLOSEZONE: |
| 613 | case BLKFINISHZONE: |
| 614 | return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg); |
Damien Le Moal | 72cd875 | 2018-10-12 19:08:45 +0900 | [diff] [blame] | 615 | case BLKGETZONESZ: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 616 | return put_uint(argp, bdev_zone_sectors(bdev)); |
Damien Le Moal | 65e4e3e | 2018-10-12 19:08:46 +0900 | [diff] [blame] | 617 | case BLKGETNRZONES: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 618 | return put_uint(argp, blkdev_nr_zones(bdev->bd_disk)); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 619 | case BLKROGET: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 620 | return put_int(argp, bdev_read_only(bdev) != 0); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 621 | case BLKSSZGET: /* get block device logical block size */ |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 622 | return put_int(argp, bdev_logical_block_size(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 623 | case BLKPBSZGET: /* get block device physical block size */ |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 624 | return put_uint(argp, bdev_physical_block_size(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 625 | case BLKIOMIN: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 626 | return put_uint(argp, bdev_io_min(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 627 | case BLKIOOPT: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 628 | return put_uint(argp, bdev_io_opt(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 629 | case BLKALIGNOFF: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 630 | return put_int(argp, bdev_alignment_offset(bdev)); |
Martin K. Petersen | 98262f2 | 2009-12-03 09:24:48 +0100 | [diff] [blame] | 631 | case BLKDISCARDZEROES: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 632 | return put_uint(argp, 0); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 633 | case BLKSECTGET: |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 634 | max_sectors = min_t(unsigned int, USHRT_MAX, |
| 635 | queue_max_sectors(bdev_get_queue(bdev))); |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 636 | return put_ushort(argp, max_sectors); |
Martin K. Petersen | ef00f59 | 2012-01-11 16:29:31 +0100 | [diff] [blame] | 637 | case BLKROTATIONAL: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 638 | return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev))); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 639 | case BLKRASET: |
| 640 | case BLKFRASET: |
| 641 | if(!capable(CAP_SYS_ADMIN)) |
| 642 | return -EACCES; |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 643 | bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 644 | return 0; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 645 | case BLKRRPART: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 646 | return blkdev_reread_part(bdev); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 647 | case BLKTRACESTART: |
| 648 | case BLKTRACESTOP: |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 649 | case BLKTRACETEARDOWN: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 650 | return blk_trace_ioctl(bdev, cmd, argp); |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 651 | case IOC_PR_REGISTER: |
| 652 | return blkdev_pr_register(bdev, argp); |
| 653 | case IOC_PR_RESERVE: |
| 654 | return blkdev_pr_reserve(bdev, argp); |
| 655 | case IOC_PR_RELEASE: |
| 656 | return blkdev_pr_release(bdev, argp); |
| 657 | case IOC_PR_PREEMPT: |
| 658 | return blkdev_pr_preempt(bdev, argp, false); |
| 659 | case IOC_PR_PREEMPT_ABORT: |
| 660 | return blkdev_pr_preempt(bdev, argp, true); |
| 661 | case IOC_PR_CLEAR: |
| 662 | return blkdev_pr_clear(bdev, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 663 | default: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 664 | return -ENOIOCTLCMD; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * Always keep this in sync with compat_blkdev_ioctl() |
| 670 | * to handle all incompatible commands in both functions. |
| 671 | * |
| 672 | * New commands must be compatible and go into blkdev_common_ioctl |
| 673 | */ |
| 674 | int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, |
| 675 | unsigned long arg) |
| 676 | { |
| 677 | int ret; |
| 678 | loff_t size; |
| 679 | void __user *argp = (void __user *)arg; |
| 680 | |
| 681 | switch (cmd) { |
| 682 | /* These need separate implementations for the data structure */ |
| 683 | case HDIO_GETGEO: |
| 684 | return blkdev_getgeo(bdev, argp); |
| 685 | case BLKPG: |
| 686 | return blkpg_ioctl(bdev, argp); |
| 687 | |
| 688 | /* Compat mode returns 32-bit data instead of 'long' */ |
| 689 | case BLKRAGET: |
| 690 | case BLKFRAGET: |
| 691 | if (!argp) |
| 692 | return -EINVAL; |
| 693 | return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512); |
| 694 | case BLKGETSIZE: |
| 695 | size = i_size_read(bdev->bd_inode); |
| 696 | if ((size >> 9) > ~0UL) |
| 697 | return -EFBIG; |
| 698 | return put_ulong(argp, size >> 9); |
| 699 | |
| 700 | /* The data is compatible, but the command number is different */ |
| 701 | case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */ |
| 702 | return put_int(argp, block_size(bdev)); |
| 703 | case BLKBSZSET: |
| 704 | return blkdev_bszset(bdev, mode, argp); |
| 705 | case BLKGETSIZE64: |
| 706 | return put_u64(argp, i_size_read(bdev->bd_inode)); |
| 707 | |
| 708 | /* Incompatible alignment on i386 */ |
| 709 | case BLKTRACESETUP: |
| 710 | return blk_trace_ioctl(bdev, cmd, argp); |
| 711 | default: |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); |
| 716 | if (ret == -ENOIOCTLCMD) |
| 717 | return __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
| 718 | |
| 719 | return ret; |
| 720 | } |
| 721 | EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */ |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 722 | |
| 723 | #ifdef CONFIG_COMPAT |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 724 | |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 725 | #define BLKBSZGET_32 _IOR(0x12, 112, int) |
| 726 | #define BLKBSZSET_32 _IOW(0x12, 113, int) |
| 727 | #define BLKGETSIZE64_32 _IOR(0x12, 114, int) |
| 728 | |
| 729 | /* Most of the generic ioctls are handled in the normal fallback path. |
| 730 | This assumes the blkdev's low level compat_ioctl always returns |
| 731 | ENOIOCTLCMD for unknown ioctls. */ |
| 732 | long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
| 733 | { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 734 | int ret; |
| 735 | void __user *argp = compat_ptr(arg); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 736 | struct inode *inode = file->f_mapping->host; |
| 737 | struct block_device *bdev = inode->i_bdev; |
| 738 | struct gendisk *disk = bdev->bd_disk; |
| 739 | fmode_t mode = file->f_mode; |
| 740 | loff_t size; |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 741 | |
| 742 | /* |
| 743 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have |
| 744 | * to updated it before every ioctl. |
| 745 | */ |
| 746 | if (file->f_flags & O_NDELAY) |
| 747 | mode |= FMODE_NDELAY; |
| 748 | else |
| 749 | mode &= ~FMODE_NDELAY; |
| 750 | |
| 751 | switch (cmd) { |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 752 | /* These need separate implementations for the data structure */ |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 753 | case HDIO_GETGEO: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 754 | return compat_hdio_getgeo(bdev, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 755 | case BLKPG: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 756 | return compat_blkpg_ioctl(bdev, argp); |
| 757 | |
| 758 | /* Compat mode returns 32-bit data instead of 'long' */ |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 759 | case BLKRAGET: |
| 760 | case BLKFRAGET: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 761 | if (!argp) |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 762 | return -EINVAL; |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 763 | return compat_put_long(argp, |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 764 | (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 765 | case BLKGETSIZE: |
| 766 | size = i_size_read(bdev->bd_inode); |
| 767 | if ((size >> 9) > ~0UL) |
| 768 | return -EFBIG; |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 769 | return compat_put_ulong(argp, size >> 9); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 770 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 771 | /* The data is compatible, but the command number is different */ |
| 772 | case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */ |
| 773 | return put_int(argp, bdev_logical_block_size(bdev)); |
| 774 | case BLKBSZSET_32: |
| 775 | return blkdev_bszset(bdev, mode, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 776 | case BLKGETSIZE64_32: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 777 | return put_u64(argp, i_size_read(bdev->bd_inode)); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 778 | |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 779 | /* Incompatible alignment on i386 */ |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 780 | case BLKTRACESETUP32: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 781 | return blk_trace_ioctl(bdev, cmd, argp); |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 782 | default: |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 783 | break; |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 784 | } |
Arnd Bergmann | 9b81648 | 2019-11-29 11:48:31 +0100 | [diff] [blame] | 785 | |
| 786 | ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); |
| 787 | if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl) |
| 788 | ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg); |
| 789 | |
| 790 | return ret; |
Arnd Bergmann | bdc1dda | 2019-11-29 11:45:30 +0100 | [diff] [blame] | 791 | } |
| 792 | #endif |