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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | #include <linux/blkdev.h> |
Paul Gortmaker | d5decd3 | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 4 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/blkpg.h> |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7 | #include <linux/hdreg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/backing-dev.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 9 | #include <linux/fs.h> |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 10 | #include <linux/blktrace_api.h> |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 11 | #include <linux/pr.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 12 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg) |
| 15 | { |
| 16 | struct block_device *bdevp; |
| 17 | struct gendisk *disk; |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 18 | struct hd_struct *part, *lpart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | struct blkpg_ioctl_arg a; |
| 20 | struct blkpg_partition p; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 21 | struct disk_part_iter piter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | long long start, length; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 23 | int partno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | if (!capable(CAP_SYS_ADMIN)) |
| 26 | return -EACCES; |
| 27 | if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg))) |
| 28 | return -EFAULT; |
| 29 | if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition))) |
| 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; |
| 37 | switch (a.op) { |
| 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 | |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 158 | static int blkdev_reread_part(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 160 | int ret; |
| 161 | |
Christoph Hellwig | 142fe8f | 2019-11-14 15:34:35 +0100 | [diff] [blame] | 162 | if (!disk_part_scan_enabled(bdev->bd_disk) || bdev != bdev->bd_contains) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | if (!capable(CAP_SYS_ADMIN)) |
| 165 | return -EACCES; |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 166 | |
Ming Lei | b04a5636 | 2015-05-06 12:26:27 +0800 | [diff] [blame] | 167 | mutex_lock(&bdev->bd_mutex); |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 168 | ret = bdev_disk_changed(bdev, false); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 169 | mutex_unlock(&bdev->bd_mutex); |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 170 | |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 171 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 174 | static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode, |
| 175 | unsigned long arg, unsigned long flags) |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 176 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 177 | uint64_t range[2]; |
| 178 | uint64_t start, len; |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 179 | struct request_queue *q = bdev_get_queue(bdev); |
| 180 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 181 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 182 | |
| 183 | if (!(mode & FMODE_WRITE)) |
| 184 | return -EBADF; |
| 185 | |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 186 | if (!blk_queue_discard(q)) |
| 187 | return -EOPNOTSUPP; |
| 188 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 189 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
| 190 | return -EFAULT; |
| 191 | |
| 192 | start = range[0]; |
| 193 | len = range[1]; |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 194 | |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 195 | if (start & 511) |
| 196 | return -EINVAL; |
| 197 | if (len & 511) |
| 198 | return -EINVAL; |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 199 | |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 200 | if (start + len > i_size_read(bdev->bd_inode)) |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 201 | return -EINVAL; |
Ming Lei | 0bd1ed4 | 2018-02-10 08:46:17 +0800 | [diff] [blame] | 202 | truncate_inode_pages_range(mapping, start, start + len - 1); |
Dmitry Monakhov | 351499a1 | 2017-10-24 18:44:57 -0600 | [diff] [blame] | 203 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, |
| 204 | GFP_KERNEL, flags); |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 207 | static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode, |
| 208 | unsigned long arg) |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 209 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 210 | uint64_t range[2]; |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 211 | struct address_space *mapping; |
| 212 | uint64_t start, end, len; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 213 | |
| 214 | if (!(mode & FMODE_WRITE)) |
| 215 | return -EBADF; |
| 216 | |
| 217 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
| 218 | return -EFAULT; |
| 219 | |
| 220 | start = range[0]; |
| 221 | len = range[1]; |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 222 | end = start + len - 1; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 223 | |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 224 | if (start & 511) |
| 225 | return -EINVAL; |
| 226 | if (len & 511) |
| 227 | return -EINVAL; |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 228 | if (end >= (uint64_t)i_size_read(bdev->bd_inode)) |
| 229 | return -EINVAL; |
| 230 | if (end < start) |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 231 | return -EINVAL; |
| 232 | |
Darrick J. Wong | 22dd6d35 | 2016-10-11 13:51:05 -0700 | [diff] [blame] | 233 | /* Invalidate the page cache, including dirty pages */ |
| 234 | mapping = bdev->bd_inode->i_mapping; |
| 235 | truncate_inode_pages_range(mapping, start, end); |
| 236 | |
| 237 | return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL, |
Christoph Hellwig | ee472d8 | 2017-04-05 19:21:08 +0200 | [diff] [blame] | 238 | BLKDEV_ZERO_NOUNMAP); |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | static int put_ushort(unsigned long arg, unsigned short val) |
| 242 | { |
| 243 | return put_user(val, (unsigned short __user *)arg); |
| 244 | } |
| 245 | |
| 246 | static int put_int(unsigned long arg, int val) |
| 247 | { |
| 248 | return put_user(val, (int __user *)arg); |
| 249 | } |
| 250 | |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 251 | static int put_uint(unsigned long arg, unsigned int val) |
| 252 | { |
| 253 | return put_user(val, (unsigned int __user *)arg); |
| 254 | } |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | static int put_long(unsigned long arg, long val) |
| 257 | { |
| 258 | return put_user(val, (long __user *)arg); |
| 259 | } |
| 260 | |
| 261 | static int put_ulong(unsigned long arg, unsigned long val) |
| 262 | { |
| 263 | return put_user(val, (unsigned long __user *)arg); |
| 264 | } |
| 265 | |
| 266 | static int put_u64(unsigned long arg, u64 val) |
| 267 | { |
| 268 | return put_user(val, (u64 __user *)arg); |
| 269 | } |
| 270 | |
Al Viro | 633a08b | 2007-08-29 20:34:12 -0400 | [diff] [blame] | 271 | int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode, |
| 272 | unsigned cmd, unsigned long arg) |
| 273 | { |
| 274 | struct gendisk *disk = bdev->bd_disk; |
Al Viro | d4430d6 | 2008-03-02 09:09:22 -0500 | [diff] [blame] | 275 | |
| 276 | if (disk->fops->ioctl) |
| 277 | return disk->fops->ioctl(bdev, mode, cmd, arg); |
| 278 | |
Al Viro | 633a08b | 2007-08-29 20:34:12 -0400 | [diff] [blame] | 279 | return -ENOTTY; |
| 280 | } |
| 281 | /* |
| 282 | * For the record: _GPL here is only because somebody decided to slap it |
| 283 | * on the previous export. Sheer idiocy, since it wasn't copyrightable |
| 284 | * at all and could be open-coded without any exports by anybody who cares. |
| 285 | */ |
| 286 | EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl); |
| 287 | |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 288 | static int blkdev_pr_register(struct block_device *bdev, |
| 289 | struct pr_registration __user *arg) |
| 290 | { |
| 291 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 292 | struct pr_registration reg; |
| 293 | |
| 294 | if (!capable(CAP_SYS_ADMIN)) |
| 295 | return -EPERM; |
| 296 | if (!ops || !ops->pr_register) |
| 297 | return -EOPNOTSUPP; |
| 298 | if (copy_from_user(®, arg, sizeof(reg))) |
| 299 | return -EFAULT; |
| 300 | |
| 301 | if (reg.flags & ~PR_FL_IGNORE_KEY) |
| 302 | return -EOPNOTSUPP; |
| 303 | return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags); |
| 304 | } |
| 305 | |
| 306 | static int blkdev_pr_reserve(struct block_device *bdev, |
| 307 | struct pr_reservation __user *arg) |
| 308 | { |
| 309 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 310 | struct pr_reservation rsv; |
| 311 | |
| 312 | if (!capable(CAP_SYS_ADMIN)) |
| 313 | return -EPERM; |
| 314 | if (!ops || !ops->pr_reserve) |
| 315 | return -EOPNOTSUPP; |
| 316 | if (copy_from_user(&rsv, arg, sizeof(rsv))) |
| 317 | return -EFAULT; |
| 318 | |
| 319 | if (rsv.flags & ~PR_FL_IGNORE_KEY) |
| 320 | return -EOPNOTSUPP; |
| 321 | return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags); |
| 322 | } |
| 323 | |
| 324 | static int blkdev_pr_release(struct block_device *bdev, |
| 325 | struct pr_reservation __user *arg) |
| 326 | { |
| 327 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 328 | struct pr_reservation rsv; |
| 329 | |
| 330 | if (!capable(CAP_SYS_ADMIN)) |
| 331 | return -EPERM; |
| 332 | if (!ops || !ops->pr_release) |
| 333 | return -EOPNOTSUPP; |
| 334 | if (copy_from_user(&rsv, arg, sizeof(rsv))) |
| 335 | return -EFAULT; |
| 336 | |
| 337 | if (rsv.flags) |
| 338 | return -EOPNOTSUPP; |
| 339 | return ops->pr_release(bdev, rsv.key, rsv.type); |
| 340 | } |
| 341 | |
| 342 | static int blkdev_pr_preempt(struct block_device *bdev, |
| 343 | struct pr_preempt __user *arg, bool abort) |
| 344 | { |
| 345 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 346 | struct pr_preempt p; |
| 347 | |
| 348 | if (!capable(CAP_SYS_ADMIN)) |
| 349 | return -EPERM; |
| 350 | if (!ops || !ops->pr_preempt) |
| 351 | return -EOPNOTSUPP; |
| 352 | if (copy_from_user(&p, arg, sizeof(p))) |
| 353 | return -EFAULT; |
| 354 | |
| 355 | if (p.flags) |
| 356 | return -EOPNOTSUPP; |
| 357 | return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort); |
| 358 | } |
| 359 | |
| 360 | static int blkdev_pr_clear(struct block_device *bdev, |
| 361 | struct pr_clear __user *arg) |
| 362 | { |
| 363 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 364 | struct pr_clear c; |
| 365 | |
| 366 | if (!capable(CAP_SYS_ADMIN)) |
| 367 | return -EPERM; |
| 368 | if (!ops || !ops->pr_clear) |
| 369 | return -EOPNOTSUPP; |
| 370 | if (copy_from_user(&c, arg, sizeof(c))) |
| 371 | return -EFAULT; |
| 372 | |
| 373 | if (c.flags) |
| 374 | return -EOPNOTSUPP; |
| 375 | return ops->pr_clear(bdev, c.key); |
| 376 | } |
| 377 | |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 378 | /* |
Linus Torvalds | 07d106d | 2012-01-05 15:40:12 -0800 | [diff] [blame] | 379 | * Is it an unrecognized ioctl? The correct returns are either |
| 380 | * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a |
| 381 | * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl |
| 382 | * code before returning. |
| 383 | * |
| 384 | * Confused drivers sometimes return EINVAL, which is wrong. It |
| 385 | * means "I understood the ioctl command, but the parameters to |
| 386 | * it were wrong". |
| 387 | * |
| 388 | * We should aim to just fix the broken drivers, the EINVAL case |
| 389 | * should go away. |
| 390 | */ |
| 391 | static inline int is_unrecognized_ioctl(int ret) |
| 392 | { |
| 393 | return ret == -EINVAL || |
| 394 | ret == -ENOTTY || |
| 395 | ret == -ENOIOCTLCMD; |
| 396 | } |
| 397 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 398 | static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode, |
| 399 | unsigned cmd, unsigned long arg) |
| 400 | { |
| 401 | int ret; |
| 402 | |
| 403 | if (!capable(CAP_SYS_ADMIN)) |
| 404 | return -EACCES; |
| 405 | |
| 406 | ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
| 407 | if (!is_unrecognized_ioctl(ret)) |
| 408 | return ret; |
| 409 | |
| 410 | fsync_bdev(bdev); |
| 411 | invalidate_bdev(bdev); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int blkdev_roset(struct block_device *bdev, fmode_t mode, |
| 416 | unsigned cmd, unsigned long arg) |
| 417 | { |
| 418 | int ret, n; |
| 419 | |
Ilya Dryomov | bb749b3 | 2017-10-18 14:38:38 +0200 | [diff] [blame] | 420 | if (!capable(CAP_SYS_ADMIN)) |
| 421 | return -EACCES; |
| 422 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 423 | ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
| 424 | if (!is_unrecognized_ioctl(ret)) |
| 425 | return ret; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 426 | if (get_user(n, (int __user *)arg)) |
| 427 | return -EFAULT; |
| 428 | set_device_ro(bdev, n); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static int blkdev_getgeo(struct block_device *bdev, |
| 433 | struct hd_geometry __user *argp) |
| 434 | { |
| 435 | struct gendisk *disk = bdev->bd_disk; |
| 436 | struct hd_geometry geo; |
| 437 | int ret; |
| 438 | |
| 439 | if (!argp) |
| 440 | return -EINVAL; |
| 441 | if (!disk->fops->getgeo) |
| 442 | return -ENOTTY; |
| 443 | |
| 444 | /* |
| 445 | * We need to set the startsect first, the driver may |
| 446 | * want to override it. |
| 447 | */ |
| 448 | memset(&geo, 0, sizeof(geo)); |
| 449 | geo.start = get_start_sect(bdev); |
| 450 | ret = disk->fops->getgeo(bdev, &geo); |
| 451 | if (ret) |
| 452 | return ret; |
| 453 | if (copy_to_user(argp, &geo, sizeof(geo))) |
| 454 | return -EFAULT; |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /* set the logical block size */ |
| 459 | static int blkdev_bszset(struct block_device *bdev, fmode_t mode, |
| 460 | int __user *argp) |
| 461 | { |
| 462 | int ret, n; |
| 463 | |
| 464 | if (!capable(CAP_SYS_ADMIN)) |
| 465 | return -EACCES; |
| 466 | if (!argp) |
| 467 | return -EINVAL; |
| 468 | if (get_user(n, argp)) |
| 469 | return -EFAULT; |
| 470 | |
| 471 | if (!(mode & FMODE_EXCL)) { |
| 472 | bdgrab(bdev); |
| 473 | if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0) |
| 474 | return -EBUSY; |
| 475 | } |
| 476 | |
| 477 | ret = set_blocksize(bdev, n); |
| 478 | if (!(mode & FMODE_EXCL)) |
| 479 | blkdev_put(bdev, mode | FMODE_EXCL); |
| 480 | return ret; |
| 481 | } |
| 482 | |
Linus Torvalds | 07d106d | 2012-01-05 15:40:12 -0800 | [diff] [blame] | 483 | /* |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 484 | * always keep this in sync with compat_blkdev_ioctl() |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 485 | */ |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 486 | int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 487 | unsigned long arg) |
| 488 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 489 | void __user *argp = (void __user *)arg; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 490 | loff_t size; |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 491 | unsigned int max_sectors; |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 492 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 493 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | case BLKFLSBUF: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 495 | return blkdev_flushbuf(bdev, mode, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | case BLKROSET: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 497 | return blkdev_roset(bdev, mode, cmd, arg); |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 498 | case BLKDISCARD: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 499 | return blk_ioctl_discard(bdev, mode, arg, 0); |
| 500 | case BLKSECDISCARD: |
| 501 | return blk_ioctl_discard(bdev, mode, arg, |
| 502 | BLKDEV_DISCARD_SECURE); |
| 503 | case BLKZEROOUT: |
| 504 | return blk_ioctl_zeroout(bdev, mode, arg); |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 505 | case BLKREPORTZONE: |
| 506 | return blkdev_report_zones_ioctl(bdev, mode, cmd, arg); |
| 507 | case BLKRESETZONE: |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 508 | case BLKOPENZONE: |
| 509 | case BLKCLOSEZONE: |
| 510 | case BLKFINISHZONE: |
| 511 | return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg); |
Damien Le Moal | 72cd875 | 2018-10-12 19:08:45 +0900 | [diff] [blame] | 512 | case BLKGETZONESZ: |
| 513 | return put_uint(arg, bdev_zone_sectors(bdev)); |
Damien Le Moal | 65e4e3e | 2018-10-12 19:08:46 +0900 | [diff] [blame] | 514 | case BLKGETNRZONES: |
| 515 | return put_uint(arg, blkdev_nr_zones(bdev)); |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 516 | case HDIO_GETGEO: |
| 517 | return blkdev_getgeo(bdev, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 518 | case BLKRAGET: |
| 519 | case BLKFRAGET: |
| 520 | if (!arg) |
| 521 | return -EINVAL; |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 522 | return put_long(arg, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 523 | case BLKROGET: |
| 524 | return put_int(arg, bdev_read_only(bdev) != 0); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 525 | case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */ |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 526 | return put_int(arg, block_size(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 527 | case BLKSSZGET: /* get block device logical block size */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 528 | return put_int(arg, bdev_logical_block_size(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 529 | case BLKPBSZGET: /* get block device physical block size */ |
| 530 | return put_uint(arg, bdev_physical_block_size(bdev)); |
| 531 | case BLKIOMIN: |
| 532 | return put_uint(arg, bdev_io_min(bdev)); |
| 533 | case BLKIOOPT: |
| 534 | return put_uint(arg, bdev_io_opt(bdev)); |
| 535 | case BLKALIGNOFF: |
| 536 | return put_int(arg, bdev_alignment_offset(bdev)); |
Martin K. Petersen | 98262f2 | 2009-12-03 09:24:48 +0100 | [diff] [blame] | 537 | case BLKDISCARDZEROES: |
Christoph Hellwig | 48920ff | 2017-04-05 19:21:23 +0200 | [diff] [blame] | 538 | return put_uint(arg, 0); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 539 | case BLKSECTGET: |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 540 | max_sectors = min_t(unsigned int, USHRT_MAX, |
| 541 | queue_max_sectors(bdev_get_queue(bdev))); |
| 542 | return put_ushort(arg, max_sectors); |
Martin K. Petersen | ef00f59 | 2012-01-11 16:29:31 +0100 | [diff] [blame] | 543 | case BLKROTATIONAL: |
| 544 | return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev))); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 545 | case BLKRASET: |
| 546 | case BLKFRASET: |
| 547 | if(!capable(CAP_SYS_ADMIN)) |
| 548 | return -EACCES; |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 549 | bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 550 | return 0; |
| 551 | case BLKBSZSET: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 552 | return blkdev_bszset(bdev, mode, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 553 | case BLKPG: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 554 | return blkpg_ioctl(bdev, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 555 | case BLKRRPART: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 556 | return blkdev_reread_part(bdev); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 557 | case BLKGETSIZE: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 558 | size = i_size_read(bdev->bd_inode); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 559 | if ((size >> 9) > ~0UL) |
| 560 | return -EFBIG; |
| 561 | return put_ulong(arg, size >> 9); |
| 562 | case BLKGETSIZE64: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 563 | return put_u64(arg, i_size_read(bdev->bd_inode)); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 564 | case BLKTRACESTART: |
| 565 | case BLKTRACESTOP: |
| 566 | case BLKTRACESETUP: |
| 567 | case BLKTRACETEARDOWN: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 568 | return blk_trace_ioctl(bdev, cmd, argp); |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 569 | case IOC_PR_REGISTER: |
| 570 | return blkdev_pr_register(bdev, argp); |
| 571 | case IOC_PR_RESERVE: |
| 572 | return blkdev_pr_reserve(bdev, argp); |
| 573 | case IOC_PR_RELEASE: |
| 574 | return blkdev_pr_release(bdev, argp); |
| 575 | case IOC_PR_PREEMPT: |
| 576 | return blkdev_pr_preempt(bdev, argp, false); |
| 577 | case IOC_PR_PREEMPT_ABORT: |
| 578 | return blkdev_pr_preempt(bdev, argp, true); |
| 579 | case IOC_PR_CLEAR: |
| 580 | return blkdev_pr_clear(bdev, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 581 | default: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 582 | return __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
Stephen Tweedie | 68f66fe | 2005-05-13 23:31:19 -0400 | [diff] [blame] | 585 | EXPORT_SYMBOL_GPL(blkdev_ioctl); |