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