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