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 | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <asm/uaccess.h> |
| 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 | } |
Tejun Heo | 88e3412 | 2008-08-25 19:30:16 +0900 | [diff] [blame] | 48 | |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 49 | mutex_lock(&bdev->bd_mutex); |
Tejun Heo | 88e3412 | 2008-08-25 19:30:16 +0900 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* overlap? */ |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 52 | disk_part_iter_init(&piter, disk, |
| 53 | DISK_PITER_INCL_EMPTY); |
| 54 | while ((part = disk_part_iter_next(&piter))) { |
| 55 | if (!(start + length <= part->start_sect || |
| 56 | start >= part->start_sect + part->nr_sects)) { |
| 57 | disk_part_iter_exit(&piter); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 58 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return -EBUSY; |
| 60 | } |
| 61 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 62 | disk_part_iter_exit(&piter); |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | /* all seems OK */ |
Tejun Heo | ba32929 | 2008-11-10 15:29:58 +0900 | [diff] [blame] | 65 | part = add_partition(disk, partno, start, length, |
Will Drewry | 6d1d805 | 2010-08-31 15:47:05 -0500 | [diff] [blame] | 66 | ADDPART_FLAG_NONE, NULL); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 67 | mutex_unlock(&bdev->bd_mutex); |
Duan Jiong | c7d1ba4 | 2013-11-06 15:56:39 +0800 | [diff] [blame] | 68 | return PTR_ERR_OR_ZERO(part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | case BLKPG_DEL_PARTITION: |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 70 | part = disk_get_part(disk, partno); |
| 71 | if (!part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return -ENXIO; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 73 | |
| 74 | bdevp = bdget(part_devt(part)); |
| 75 | disk_put_part(part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (!bdevp) |
| 77 | return -ENOMEM; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 78 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 79 | mutex_lock(&bdevp->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (bdevp->bd_openers) { |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 81 | mutex_unlock(&bdevp->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | bdput(bdevp); |
| 83 | return -EBUSY; |
| 84 | } |
| 85 | /* all seems OK */ |
| 86 | fsync_bdev(bdevp); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 87 | invalidate_bdev(bdevp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Peter Zijlstra | 6d740cd | 2007-02-20 13:58:18 -0800 | [diff] [blame] | 89 | mutex_lock_nested(&bdev->bd_mutex, 1); |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 90 | delete_partition(disk, partno); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 91 | mutex_unlock(&bdev->bd_mutex); |
| 92 | mutex_unlock(&bdevp->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | bdput(bdevp); |
| 94 | |
| 95 | return 0; |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 96 | case BLKPG_RESIZE_PARTITION: |
| 97 | start = p.start >> 9; |
| 98 | /* new length of partition in bytes */ |
| 99 | length = p.length >> 9; |
| 100 | /* check for fit in a hd_struct */ |
| 101 | if (sizeof(sector_t) == sizeof(long) && |
| 102 | sizeof(long long) > sizeof(long)) { |
| 103 | long pstart = start, plength = length; |
| 104 | if (pstart != start || plength != length |
| 105 | || pstart < 0 || plength < 0) |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | part = disk_get_part(disk, partno); |
| 109 | if (!part) |
| 110 | return -ENXIO; |
| 111 | bdevp = bdget(part_devt(part)); |
| 112 | if (!bdevp) { |
| 113 | disk_put_part(part); |
| 114 | return -ENOMEM; |
| 115 | } |
| 116 | mutex_lock(&bdevp->bd_mutex); |
| 117 | mutex_lock_nested(&bdev->bd_mutex, 1); |
| 118 | if (start != part->start_sect) { |
| 119 | mutex_unlock(&bdevp->bd_mutex); |
| 120 | mutex_unlock(&bdev->bd_mutex); |
| 121 | bdput(bdevp); |
| 122 | disk_put_part(part); |
| 123 | return -EINVAL; |
| 124 | } |
| 125 | /* overlap? */ |
| 126 | disk_part_iter_init(&piter, disk, |
| 127 | DISK_PITER_INCL_EMPTY); |
| 128 | while ((lpart = disk_part_iter_next(&piter))) { |
| 129 | if (lpart->partno != partno && |
| 130 | !(start + length <= lpart->start_sect || |
| 131 | start >= lpart->start_sect + lpart->nr_sects) |
| 132 | ) { |
| 133 | disk_part_iter_exit(&piter); |
| 134 | mutex_unlock(&bdevp->bd_mutex); |
| 135 | mutex_unlock(&bdev->bd_mutex); |
| 136 | bdput(bdevp); |
| 137 | disk_put_part(part); |
| 138 | return -EBUSY; |
| 139 | } |
| 140 | } |
| 141 | disk_part_iter_exit(&piter); |
| 142 | part_nr_sects_write(part, (sector_t)length); |
| 143 | i_size_write(bdevp->bd_inode, p.length); |
| 144 | mutex_unlock(&bdevp->bd_mutex); |
| 145 | mutex_unlock(&bdev->bd_mutex); |
| 146 | bdput(bdevp); |
| 147 | disk_put_part(part); |
| 148 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | default: |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | } |
| 153 | |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 154 | /* |
| 155 | * This is an exported API for the block driver, and will not |
| 156 | * acquire bd_mutex. This API should be used in case that |
| 157 | * caller has held bd_mutex already. |
| 158 | */ |
| 159 | int __blkdev_reread_part(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
| 161 | struct gendisk *disk = bdev->bd_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Tejun Heo | d27769e | 2011-08-23 20:01:04 +0200 | [diff] [blame] | 163 | if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return -EINVAL; |
| 165 | if (!capable(CAP_SYS_ADMIN)) |
| 166 | return -EACCES; |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 167 | |
| 168 | lockdep_assert_held(&bdev->bd_mutex); |
| 169 | |
| 170 | return rescan_partitions(disk, bdev); |
| 171 | } |
| 172 | EXPORT_SYMBOL(__blkdev_reread_part); |
| 173 | |
| 174 | /* |
| 175 | * This is an exported API for the block driver, and will |
| 176 | * try to acquire bd_mutex. If bd_mutex has been held already |
| 177 | * in current context, please call __blkdev_reread_part(). |
Ming Lei | b04a5636 | 2015-05-06 12:26:27 +0800 | [diff] [blame] | 178 | * |
| 179 | * Make sure the held locks in current context aren't required |
| 180 | * in open()/close() handler and I/O path for avoiding ABBA deadlock: |
| 181 | * - bd_mutex is held before calling block driver's open/close |
| 182 | * handler |
| 183 | * - reading partition table may submit I/O to the block device |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 184 | */ |
| 185 | int blkdev_reread_part(struct block_device *bdev) |
| 186 | { |
| 187 | int res; |
| 188 | |
Ming Lei | b04a5636 | 2015-05-06 12:26:27 +0800 | [diff] [blame] | 189 | mutex_lock(&bdev->bd_mutex); |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 190 | res = __blkdev_reread_part(bdev); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 191 | mutex_unlock(&bdev->bd_mutex); |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | return res; |
| 194 | } |
Jarod Wilson | be32417 | 2015-05-06 12:26:22 +0800 | [diff] [blame] | 195 | EXPORT_SYMBOL(blkdev_reread_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 197 | static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode, |
| 198 | unsigned long arg, unsigned long flags) |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 199 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 200 | uint64_t range[2]; |
| 201 | uint64_t start, len; |
| 202 | |
| 203 | if (!(mode & FMODE_WRITE)) |
| 204 | return -EBADF; |
| 205 | |
| 206 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
| 207 | return -EFAULT; |
| 208 | |
| 209 | start = range[0]; |
| 210 | len = range[1]; |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 211 | |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 212 | if (start & 511) |
| 213 | return -EINVAL; |
| 214 | if (len & 511) |
| 215 | return -EINVAL; |
| 216 | start >>= 9; |
| 217 | len >>= 9; |
| 218 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 219 | if (start + len > (i_size_read(bdev->bd_inode) >> 9)) |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 220 | return -EINVAL; |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 221 | return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags); |
David Woodhouse | d30a260 | 2008-08-11 15:58:42 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 224 | static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode, |
| 225 | unsigned long arg) |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 226 | { |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 227 | uint64_t range[2]; |
| 228 | uint64_t start, len; |
| 229 | |
| 230 | if (!(mode & FMODE_WRITE)) |
| 231 | return -EBADF; |
| 232 | |
| 233 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
| 234 | return -EFAULT; |
| 235 | |
| 236 | start = range[0]; |
| 237 | len = range[1]; |
| 238 | |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 239 | if (start & 511) |
| 240 | return -EINVAL; |
| 241 | if (len & 511) |
| 242 | return -EINVAL; |
| 243 | start >>= 9; |
| 244 | len >>= 9; |
| 245 | |
| 246 | if (start + len > (i_size_read(bdev->bd_inode) >> 9)) |
| 247 | return -EINVAL; |
| 248 | |
Martin K. Petersen | d93ba7a | 2015-01-20 20:06:30 -0500 | [diff] [blame] | 249 | return blkdev_issue_zeroout(bdev, start, len, GFP_KERNEL, false); |
Martin K. Petersen | 66ba32d | 2012-09-18 12:19:29 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | static int put_ushort(unsigned long arg, unsigned short val) |
| 253 | { |
| 254 | return put_user(val, (unsigned short __user *)arg); |
| 255 | } |
| 256 | |
| 257 | static int put_int(unsigned long arg, int val) |
| 258 | { |
| 259 | return put_user(val, (int __user *)arg); |
| 260 | } |
| 261 | |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 262 | static int put_uint(unsigned long arg, unsigned int val) |
| 263 | { |
| 264 | return put_user(val, (unsigned int __user *)arg); |
| 265 | } |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | static int put_long(unsigned long arg, long val) |
| 268 | { |
| 269 | return put_user(val, (long __user *)arg); |
| 270 | } |
| 271 | |
| 272 | static int put_ulong(unsigned long arg, unsigned long val) |
| 273 | { |
| 274 | return put_user(val, (unsigned long __user *)arg); |
| 275 | } |
| 276 | |
| 277 | static int put_u64(unsigned long arg, u64 val) |
| 278 | { |
| 279 | return put_user(val, (u64 __user *)arg); |
| 280 | } |
| 281 | |
Al Viro | 633a08b | 2007-08-29 20:34:12 -0400 | [diff] [blame] | 282 | int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode, |
| 283 | unsigned cmd, unsigned long arg) |
| 284 | { |
| 285 | struct gendisk *disk = bdev->bd_disk; |
Al Viro | d4430d6 | 2008-03-02 09:09:22 -0500 | [diff] [blame] | 286 | |
| 287 | if (disk->fops->ioctl) |
| 288 | return disk->fops->ioctl(bdev, mode, cmd, arg); |
| 289 | |
Al Viro | 633a08b | 2007-08-29 20:34:12 -0400 | [diff] [blame] | 290 | return -ENOTTY; |
| 291 | } |
| 292 | /* |
| 293 | * For the record: _GPL here is only because somebody decided to slap it |
| 294 | * on the previous export. Sheer idiocy, since it wasn't copyrightable |
| 295 | * at all and could be open-coded without any exports by anybody who cares. |
| 296 | */ |
| 297 | EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl); |
| 298 | |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 299 | static int blkdev_pr_register(struct block_device *bdev, |
| 300 | struct pr_registration __user *arg) |
| 301 | { |
| 302 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 303 | struct pr_registration reg; |
| 304 | |
| 305 | if (!capable(CAP_SYS_ADMIN)) |
| 306 | return -EPERM; |
| 307 | if (!ops || !ops->pr_register) |
| 308 | return -EOPNOTSUPP; |
| 309 | if (copy_from_user(®, arg, sizeof(reg))) |
| 310 | return -EFAULT; |
| 311 | |
| 312 | if (reg.flags & ~PR_FL_IGNORE_KEY) |
| 313 | return -EOPNOTSUPP; |
| 314 | return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags); |
| 315 | } |
| 316 | |
| 317 | static int blkdev_pr_reserve(struct block_device *bdev, |
| 318 | struct pr_reservation __user *arg) |
| 319 | { |
| 320 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 321 | struct pr_reservation rsv; |
| 322 | |
| 323 | if (!capable(CAP_SYS_ADMIN)) |
| 324 | return -EPERM; |
| 325 | if (!ops || !ops->pr_reserve) |
| 326 | return -EOPNOTSUPP; |
| 327 | if (copy_from_user(&rsv, arg, sizeof(rsv))) |
| 328 | return -EFAULT; |
| 329 | |
| 330 | if (rsv.flags & ~PR_FL_IGNORE_KEY) |
| 331 | return -EOPNOTSUPP; |
| 332 | return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags); |
| 333 | } |
| 334 | |
| 335 | static int blkdev_pr_release(struct block_device *bdev, |
| 336 | struct pr_reservation __user *arg) |
| 337 | { |
| 338 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 339 | struct pr_reservation rsv; |
| 340 | |
| 341 | if (!capable(CAP_SYS_ADMIN)) |
| 342 | return -EPERM; |
| 343 | if (!ops || !ops->pr_release) |
| 344 | return -EOPNOTSUPP; |
| 345 | if (copy_from_user(&rsv, arg, sizeof(rsv))) |
| 346 | return -EFAULT; |
| 347 | |
| 348 | if (rsv.flags) |
| 349 | return -EOPNOTSUPP; |
| 350 | return ops->pr_release(bdev, rsv.key, rsv.type); |
| 351 | } |
| 352 | |
| 353 | static int blkdev_pr_preempt(struct block_device *bdev, |
| 354 | struct pr_preempt __user *arg, bool abort) |
| 355 | { |
| 356 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 357 | struct pr_preempt p; |
| 358 | |
| 359 | if (!capable(CAP_SYS_ADMIN)) |
| 360 | return -EPERM; |
| 361 | if (!ops || !ops->pr_preempt) |
| 362 | return -EOPNOTSUPP; |
| 363 | if (copy_from_user(&p, arg, sizeof(p))) |
| 364 | return -EFAULT; |
| 365 | |
| 366 | if (p.flags) |
| 367 | return -EOPNOTSUPP; |
| 368 | return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort); |
| 369 | } |
| 370 | |
| 371 | static int blkdev_pr_clear(struct block_device *bdev, |
| 372 | struct pr_clear __user *arg) |
| 373 | { |
| 374 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; |
| 375 | struct pr_clear c; |
| 376 | |
| 377 | if (!capable(CAP_SYS_ADMIN)) |
| 378 | return -EPERM; |
| 379 | if (!ops || !ops->pr_clear) |
| 380 | return -EOPNOTSUPP; |
| 381 | if (copy_from_user(&c, arg, sizeof(c))) |
| 382 | return -EFAULT; |
| 383 | |
| 384 | if (c.flags) |
| 385 | return -EOPNOTSUPP; |
| 386 | return ops->pr_clear(bdev, c.key); |
| 387 | } |
| 388 | |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 389 | /* |
Linus Torvalds | 07d106d | 2012-01-05 15:40:12 -0800 | [diff] [blame] | 390 | * Is it an unrecognized ioctl? The correct returns are either |
| 391 | * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a |
| 392 | * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl |
| 393 | * code before returning. |
| 394 | * |
| 395 | * Confused drivers sometimes return EINVAL, which is wrong. It |
| 396 | * means "I understood the ioctl command, but the parameters to |
| 397 | * it were wrong". |
| 398 | * |
| 399 | * We should aim to just fix the broken drivers, the EINVAL case |
| 400 | * should go away. |
| 401 | */ |
| 402 | static inline int is_unrecognized_ioctl(int ret) |
| 403 | { |
| 404 | return ret == -EINVAL || |
| 405 | ret == -ENOTTY || |
| 406 | ret == -ENOIOCTLCMD; |
| 407 | } |
| 408 | |
Dan Williams | 5a023cd | 2015-11-30 10:20:29 -0800 | [diff] [blame^] | 409 | #ifdef CONFIG_FS_DAX |
| 410 | bool blkdev_dax_capable(struct block_device *bdev) |
| 411 | { |
| 412 | struct gendisk *disk = bdev->bd_disk; |
| 413 | |
| 414 | if (!disk->fops->direct_access) |
| 415 | return false; |
| 416 | |
| 417 | /* |
| 418 | * If the partition is not aligned on a page boundary, we can't |
| 419 | * do dax I/O to it. |
| 420 | */ |
| 421 | if ((bdev->bd_part->start_sect % (PAGE_SIZE / 512)) |
| 422 | || (bdev->bd_part->nr_sects % (PAGE_SIZE / 512))) |
| 423 | return false; |
| 424 | |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | static int blkdev_daxset(struct block_device *bdev, unsigned long argp) |
| 429 | { |
| 430 | unsigned long arg; |
| 431 | int rc = 0; |
| 432 | |
| 433 | if (!capable(CAP_SYS_ADMIN)) |
| 434 | return -EACCES; |
| 435 | |
| 436 | if (get_user(arg, (int __user *)(argp))) |
| 437 | return -EFAULT; |
| 438 | arg = !!arg; |
| 439 | if (arg == !!(bdev->bd_inode->i_flags & S_DAX)) |
| 440 | return 0; |
| 441 | |
| 442 | if (arg) |
| 443 | arg = S_DAX; |
| 444 | |
| 445 | if (arg && !blkdev_dax_capable(bdev)) |
| 446 | return -ENOTTY; |
| 447 | |
| 448 | mutex_lock(&bdev->bd_inode->i_mutex); |
| 449 | if (bdev->bd_map_count == 0) |
| 450 | inode_set_flags(bdev->bd_inode, arg, S_DAX); |
| 451 | else |
| 452 | rc = -EBUSY; |
| 453 | mutex_unlock(&bdev->bd_inode->i_mutex); |
| 454 | return rc; |
| 455 | } |
| 456 | #else |
| 457 | static int blkdev_daxset(struct block_device *bdev, int arg) |
| 458 | { |
| 459 | if (arg) |
| 460 | return -ENOTTY; |
| 461 | return 0; |
| 462 | } |
| 463 | #endif |
| 464 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 465 | static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode, |
| 466 | unsigned cmd, unsigned long arg) |
| 467 | { |
| 468 | int ret; |
| 469 | |
| 470 | if (!capable(CAP_SYS_ADMIN)) |
| 471 | return -EACCES; |
| 472 | |
| 473 | ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
| 474 | if (!is_unrecognized_ioctl(ret)) |
| 475 | return ret; |
| 476 | |
| 477 | fsync_bdev(bdev); |
| 478 | invalidate_bdev(bdev); |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int blkdev_roset(struct block_device *bdev, fmode_t mode, |
| 483 | unsigned cmd, unsigned long arg) |
| 484 | { |
| 485 | int ret, n; |
| 486 | |
| 487 | ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
| 488 | if (!is_unrecognized_ioctl(ret)) |
| 489 | return ret; |
| 490 | if (!capable(CAP_SYS_ADMIN)) |
| 491 | return -EACCES; |
| 492 | if (get_user(n, (int __user *)arg)) |
| 493 | return -EFAULT; |
| 494 | set_device_ro(bdev, n); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int blkdev_getgeo(struct block_device *bdev, |
| 499 | struct hd_geometry __user *argp) |
| 500 | { |
| 501 | struct gendisk *disk = bdev->bd_disk; |
| 502 | struct hd_geometry geo; |
| 503 | int ret; |
| 504 | |
| 505 | if (!argp) |
| 506 | return -EINVAL; |
| 507 | if (!disk->fops->getgeo) |
| 508 | return -ENOTTY; |
| 509 | |
| 510 | /* |
| 511 | * We need to set the startsect first, the driver may |
| 512 | * want to override it. |
| 513 | */ |
| 514 | memset(&geo, 0, sizeof(geo)); |
| 515 | geo.start = get_start_sect(bdev); |
| 516 | ret = disk->fops->getgeo(bdev, &geo); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | if (copy_to_user(argp, &geo, sizeof(geo))) |
| 520 | return -EFAULT; |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /* set the logical block size */ |
| 525 | static int blkdev_bszset(struct block_device *bdev, fmode_t mode, |
| 526 | int __user *argp) |
| 527 | { |
| 528 | int ret, n; |
| 529 | |
| 530 | if (!capable(CAP_SYS_ADMIN)) |
| 531 | return -EACCES; |
| 532 | if (!argp) |
| 533 | return -EINVAL; |
| 534 | if (get_user(n, argp)) |
| 535 | return -EFAULT; |
| 536 | |
| 537 | if (!(mode & FMODE_EXCL)) { |
| 538 | bdgrab(bdev); |
| 539 | if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0) |
| 540 | return -EBUSY; |
| 541 | } |
| 542 | |
| 543 | ret = set_blocksize(bdev, n); |
| 544 | if (!(mode & FMODE_EXCL)) |
| 545 | blkdev_put(bdev, mode | FMODE_EXCL); |
| 546 | return ret; |
| 547 | } |
| 548 | |
Linus Torvalds | 07d106d | 2012-01-05 15:40:12 -0800 | [diff] [blame] | 549 | /* |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 550 | * always keep this in sync with compat_blkdev_ioctl() |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 551 | */ |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 552 | int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 553 | unsigned long arg) |
| 554 | { |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 555 | struct backing_dev_info *bdi; |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 556 | void __user *argp = (void __user *)arg; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 557 | loff_t size; |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 558 | unsigned int max_sectors; |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 559 | |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 560 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | case BLKFLSBUF: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 562 | return blkdev_flushbuf(bdev, mode, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | case BLKROSET: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 564 | return blkdev_roset(bdev, mode, cmd, arg); |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 565 | case BLKDISCARD: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 566 | return blk_ioctl_discard(bdev, mode, arg, 0); |
| 567 | case BLKSECDISCARD: |
| 568 | return blk_ioctl_discard(bdev, mode, arg, |
| 569 | BLKDEV_DISCARD_SECURE); |
| 570 | case BLKZEROOUT: |
| 571 | return blk_ioctl_zeroout(bdev, mode, arg); |
| 572 | case HDIO_GETGEO: |
| 573 | return blkdev_getgeo(bdev, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 574 | case BLKRAGET: |
| 575 | case BLKFRAGET: |
| 576 | if (!arg) |
| 577 | return -EINVAL; |
| 578 | bdi = blk_get_backing_dev_info(bdev); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 579 | return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512); |
| 580 | case BLKROGET: |
| 581 | return put_int(arg, bdev_read_only(bdev) != 0); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 582 | case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */ |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 583 | return put_int(arg, block_size(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 584 | case BLKSSZGET: /* get block device logical block size */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 585 | return put_int(arg, bdev_logical_block_size(bdev)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 586 | case BLKPBSZGET: /* get block device physical block size */ |
| 587 | return put_uint(arg, bdev_physical_block_size(bdev)); |
| 588 | case BLKIOMIN: |
| 589 | return put_uint(arg, bdev_io_min(bdev)); |
| 590 | case BLKIOOPT: |
| 591 | return put_uint(arg, bdev_io_opt(bdev)); |
| 592 | case BLKALIGNOFF: |
| 593 | return put_int(arg, bdev_alignment_offset(bdev)); |
Martin K. Petersen | 98262f2 | 2009-12-03 09:24:48 +0100 | [diff] [blame] | 594 | case BLKDISCARDZEROES: |
| 595 | return put_uint(arg, bdev_discard_zeroes_data(bdev)); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 596 | case BLKSECTGET: |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 597 | max_sectors = min_t(unsigned int, USHRT_MAX, |
| 598 | queue_max_sectors(bdev_get_queue(bdev))); |
| 599 | return put_ushort(arg, max_sectors); |
Martin K. Petersen | ef00f59 | 2012-01-11 16:29:31 +0100 | [diff] [blame] | 600 | case BLKROTATIONAL: |
| 601 | return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev))); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 602 | case BLKRASET: |
| 603 | case BLKFRASET: |
| 604 | if(!capable(CAP_SYS_ADMIN)) |
| 605 | return -EACCES; |
| 606 | bdi = blk_get_backing_dev_info(bdev); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 607 | bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 608 | return 0; |
| 609 | case BLKBSZSET: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 610 | return blkdev_bszset(bdev, mode, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 611 | case BLKPG: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 612 | return blkpg_ioctl(bdev, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 613 | case BLKRRPART: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 614 | return blkdev_reread_part(bdev); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 615 | case BLKGETSIZE: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 616 | size = i_size_read(bdev->bd_inode); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 617 | if ((size >> 9) > ~0UL) |
| 618 | return -EFBIG; |
| 619 | return put_ulong(arg, size >> 9); |
| 620 | case BLKGETSIZE64: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 621 | return put_u64(arg, i_size_read(bdev->bd_inode)); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 622 | case BLKTRACESTART: |
| 623 | case BLKTRACESTOP: |
| 624 | case BLKTRACESETUP: |
| 625 | case BLKTRACETEARDOWN: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 626 | return blk_trace_ioctl(bdev, cmd, argp); |
Dan Williams | 5a023cd | 2015-11-30 10:20:29 -0800 | [diff] [blame^] | 627 | case BLKDAXSET: |
| 628 | return blkdev_daxset(bdev, arg); |
| 629 | case BLKDAXGET: |
| 630 | return put_int(arg, !!(bdev->bd_inode->i_flags & S_DAX)); |
| 631 | break; |
Christoph Hellwig | bbd3e06 | 2015-10-15 14:10:48 +0200 | [diff] [blame] | 632 | case IOC_PR_REGISTER: |
| 633 | return blkdev_pr_register(bdev, argp); |
| 634 | case IOC_PR_RESERVE: |
| 635 | return blkdev_pr_reserve(bdev, argp); |
| 636 | case IOC_PR_RELEASE: |
| 637 | return blkdev_pr_release(bdev, argp); |
| 638 | case IOC_PR_PREEMPT: |
| 639 | return blkdev_pr_preempt(bdev, argp, false); |
| 640 | case IOC_PR_PREEMPT_ABORT: |
| 641 | return blkdev_pr_preempt(bdev, argp, true); |
| 642 | case IOC_PR_CLEAR: |
| 643 | return blkdev_pr_clear(bdev, argp); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 644 | default: |
Christoph Hellwig | d8e4bb810 | 2015-10-15 14:10:47 +0200 | [diff] [blame] | 645 | return __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
Stephen Tweedie | 68f66fe | 2005-05-13 23:31:19 -0400 | [diff] [blame] | 648 | EXPORT_SYMBOL_GPL(blkdev_ioctl); |