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