Commit | Line | Data |
---|---|---|
3dcf60bc | 1 | // SPDX-License-Identifier: GPL-2.0 |
c59ede7b | 2 | #include <linux/capability.h> |
ee6a129d | 3 | #include <linux/compat.h> |
1da177e4 | 4 | #include <linux/blkdev.h> |
d5decd3b | 5 | #include <linux/export.h> |
5a0e3ad6 | 6 | #include <linux/gfp.h> |
1da177e4 | 7 | #include <linux/blkpg.h> |
a885c8c4 | 8 | #include <linux/hdreg.h> |
1da177e4 | 9 | #include <linux/backing-dev.h> |
ff01bb48 | 10 | #include <linux/fs.h> |
2056a782 | 11 | #include <linux/blktrace_api.h> |
bbd3e064 | 12 | #include <linux/pr.h> |
7c0f6ba6 | 13 | #include <linux/uaccess.h> |
581e2600 | 14 | #include "blk.h" |
1da177e4 | 15 | |
5fb889f5 AB |
16 | static int blkpg_do_ioctl(struct block_device *bdev, |
17 | struct blkpg_partition __user *upart, int op) | |
1da177e4 | 18 | { |
7f6be376 | 19 | struct gendisk *disk = bdev->bd_disk; |
1da177e4 LT |
20 | struct blkpg_partition p; |
21 | long long start, length; | |
1da177e4 LT |
22 | |
23 | if (!capable(CAP_SYS_ADMIN)) | |
24 | return -EACCES; | |
5fb889f5 | 25 | if (copy_from_user(&p, upart, sizeof(struct blkpg_partition))) |
1da177e4 | 26 | return -EFAULT; |
fa01b1e9 | 27 | if (bdev_is_partition(bdev)) |
1da177e4 | 28 | return -EINVAL; |
88e34126 | 29 | |
fa9156ae CH |
30 | if (p.pno <= 0) |
31 | return -EINVAL; | |
e71bf0d0 | 32 | |
fa9156ae | 33 | if (op == BLKPG_DEL_PARTITION) |
926fbb16 | 34 | return bdev_del_partition(disk, p.pno); |
e71bf0d0 | 35 | |
fa9156ae CH |
36 | start = p.start >> SECTOR_SHIFT; |
37 | length = p.length >> SECTOR_SHIFT; | |
e71bf0d0 | 38 | |
fa9156ae CH |
39 | switch (op) { |
40 | case BLKPG_ADD_PARTITION: | |
41 | /* check if partition is aligned to blocksize */ | |
42 | if (p.start & (bdev_logical_block_size(bdev) - 1)) | |
1da177e4 | 43 | return -EINVAL; |
7f6be376 | 44 | return bdev_add_partition(disk, p.pno, start, length); |
fa9156ae | 45 | case BLKPG_RESIZE_PARTITION: |
3d2e7989 | 46 | return bdev_resize_partition(disk, p.pno, start, length); |
fa9156ae CH |
47 | default: |
48 | return -EINVAL; | |
1da177e4 LT |
49 | } |
50 | } | |
51 | ||
5fb889f5 AB |
52 | static int blkpg_ioctl(struct block_device *bdev, |
53 | struct blkpg_ioctl_arg __user *arg) | |
54 | { | |
55 | struct blkpg_partition __user *udata; | |
56 | int op; | |
57 | ||
58 | if (get_user(op, &arg->op) || get_user(udata, &arg->data)) | |
59 | return -EFAULT; | |
60 | ||
61 | return blkpg_do_ioctl(bdev, udata, op); | |
62 | } | |
63 | ||
64 | #ifdef CONFIG_COMPAT | |
65 | struct compat_blkpg_ioctl_arg { | |
66 | compat_int_t op; | |
67 | compat_int_t flags; | |
68 | compat_int_t datalen; | |
69 | compat_caddr_t data; | |
70 | }; | |
71 | ||
72 | static int compat_blkpg_ioctl(struct block_device *bdev, | |
73 | struct compat_blkpg_ioctl_arg __user *arg) | |
74 | { | |
75 | compat_caddr_t udata; | |
76 | int op; | |
77 | ||
78 | if (get_user(op, &arg->op) || get_user(udata, &arg->data)) | |
79 | return -EFAULT; | |
80 | ||
81 | return blkpg_do_ioctl(bdev, compat_ptr(udata), op); | |
82 | } | |
83 | #endif | |
84 | ||
05bdb996 | 85 | static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, |
44abff2c | 86 | unsigned long arg) |
d30a2605 | 87 | { |
d8e4bb81 CH |
88 | uint64_t range[2]; |
89 | uint64_t start, len; | |
7607c44c | 90 | struct inode *inode = bdev->bd_inode; |
384d87ef | 91 | int err; |
d8e4bb81 | 92 | |
05bdb996 | 93 | if (!(mode & BLK_OPEN_WRITE)) |
d8e4bb81 CH |
94 | return -EBADF; |
95 | ||
70200574 | 96 | if (!bdev_max_discard_sectors(bdev)) |
351499a1 DM |
97 | return -EOPNOTSUPP; |
98 | ||
d8e4bb81 CH |
99 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
100 | return -EFAULT; | |
101 | ||
102 | start = range[0]; | |
103 | len = range[1]; | |
8d57a98c | 104 | |
d30a2605 DW |
105 | if (start & 511) |
106 | return -EINVAL; | |
107 | if (len & 511) | |
108 | return -EINVAL; | |
d30a2605 | 109 | |
946e9937 | 110 | if (start + len > bdev_nr_bytes(bdev)) |
d30a2605 | 111 | return -EINVAL; |
384d87ef | 112 | |
7607c44c | 113 | filemap_invalidate_lock(inode->i_mapping); |
384d87ef JK |
114 | err = truncate_bdev_range(bdev, mode, start, start + len - 1); |
115 | if (err) | |
7607c44c | 116 | goto fail; |
44abff2c | 117 | err = blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); |
7607c44c SK |
118 | fail: |
119 | filemap_invalidate_unlock(inode->i_mapping); | |
120 | return err; | |
d30a2605 DW |
121 | } |
122 | ||
05bdb996 | 123 | static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode, |
44abff2c CH |
124 | void __user *argp) |
125 | { | |
126 | uint64_t start, len; | |
127 | uint64_t range[2]; | |
128 | int err; | |
129 | ||
05bdb996 | 130 | if (!(mode & BLK_OPEN_WRITE)) |
44abff2c CH |
131 | return -EBADF; |
132 | if (!bdev_max_secure_erase_sectors(bdev)) | |
133 | return -EOPNOTSUPP; | |
134 | if (copy_from_user(range, argp, sizeof(range))) | |
135 | return -EFAULT; | |
136 | ||
137 | start = range[0]; | |
138 | len = range[1]; | |
139 | if ((start & 511) || (len & 511)) | |
140 | return -EINVAL; | |
141 | if (start + len > bdev_nr_bytes(bdev)) | |
142 | return -EINVAL; | |
143 | ||
144 | filemap_invalidate_lock(bdev->bd_inode->i_mapping); | |
145 | err = truncate_bdev_range(bdev, mode, start, start + len - 1); | |
146 | if (!err) | |
147 | err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9, | |
148 | GFP_KERNEL); | |
149 | filemap_invalidate_unlock(bdev->bd_inode->i_mapping); | |
150 | return err; | |
151 | } | |
152 | ||
153 | ||
05bdb996 | 154 | static int blk_ioctl_zeroout(struct block_device *bdev, blk_mode_t mode, |
d8e4bb81 | 155 | unsigned long arg) |
66ba32dc | 156 | { |
d8e4bb81 | 157 | uint64_t range[2]; |
22dd6d35 | 158 | uint64_t start, end, len; |
35e4c6c1 | 159 | struct inode *inode = bdev->bd_inode; |
384d87ef | 160 | int err; |
d8e4bb81 | 161 | |
05bdb996 | 162 | if (!(mode & BLK_OPEN_WRITE)) |
d8e4bb81 CH |
163 | return -EBADF; |
164 | ||
165 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) | |
166 | return -EFAULT; | |
167 | ||
168 | start = range[0]; | |
169 | len = range[1]; | |
22dd6d35 | 170 | end = start + len - 1; |
d8e4bb81 | 171 | |
66ba32dc MP |
172 | if (start & 511) |
173 | return -EINVAL; | |
174 | if (len & 511) | |
175 | return -EINVAL; | |
946e9937 | 176 | if (end >= (uint64_t)bdev_nr_bytes(bdev)) |
22dd6d35 DW |
177 | return -EINVAL; |
178 | if (end < start) | |
66ba32dc MP |
179 | return -EINVAL; |
180 | ||
22dd6d35 | 181 | /* Invalidate the page cache, including dirty pages */ |
35e4c6c1 | 182 | filemap_invalidate_lock(inode->i_mapping); |
384d87ef JK |
183 | err = truncate_bdev_range(bdev, mode, start, end); |
184 | if (err) | |
35e4c6c1 SK |
185 | goto fail; |
186 | ||
187 | err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL, | |
188 | BLKDEV_ZERO_NOUNMAP); | |
22dd6d35 | 189 | |
35e4c6c1 SK |
190 | fail: |
191 | filemap_invalidate_unlock(inode->i_mapping); | |
192 | return err; | |
66ba32dc MP |
193 | } |
194 | ||
9b81648c | 195 | static int put_ushort(unsigned short __user *argp, unsigned short val) |
1da177e4 | 196 | { |
9b81648c | 197 | return put_user(val, argp); |
1da177e4 LT |
198 | } |
199 | ||
9b81648c | 200 | static int put_int(int __user *argp, int val) |
1da177e4 | 201 | { |
9b81648c | 202 | return put_user(val, argp); |
1da177e4 LT |
203 | } |
204 | ||
9b81648c | 205 | static int put_uint(unsigned int __user *argp, unsigned int val) |
ac481c20 | 206 | { |
9b81648c | 207 | return put_user(val, argp); |
ac481c20 MP |
208 | } |
209 | ||
9b81648c | 210 | static int put_long(long __user *argp, long val) |
1da177e4 | 211 | { |
9b81648c | 212 | return put_user(val, argp); |
1da177e4 LT |
213 | } |
214 | ||
9b81648c | 215 | static int put_ulong(unsigned long __user *argp, unsigned long val) |
1da177e4 | 216 | { |
9b81648c | 217 | return put_user(val, argp); |
1da177e4 LT |
218 | } |
219 | ||
9b81648c | 220 | static int put_u64(u64 __user *argp, u64 val) |
1da177e4 | 221 | { |
9b81648c | 222 | return put_user(val, argp); |
1da177e4 LT |
223 | } |
224 | ||
bdc1ddad | 225 | #ifdef CONFIG_COMPAT |
c8210a57 | 226 | static int compat_put_long(compat_long_t __user *argp, long val) |
bdc1ddad | 227 | { |
9b81648c | 228 | return put_user(val, argp); |
bdc1ddad AB |
229 | } |
230 | ||
c8210a57 | 231 | static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val) |
bdc1ddad | 232 | { |
9b81648c | 233 | return put_user(val, argp); |
bdc1ddad AB |
234 | } |
235 | #endif | |
236 | ||
ee6a129d AB |
237 | #ifdef CONFIG_COMPAT |
238 | /* | |
239 | * This is the equivalent of compat_ptr_ioctl(), to be used by block | |
240 | * drivers that implement only commands that are completely compatible | |
241 | * between 32-bit and 64-bit user space | |
242 | */ | |
05bdb996 | 243 | int blkdev_compat_ptr_ioctl(struct block_device *bdev, blk_mode_t mode, |
ee6a129d AB |
244 | unsigned cmd, unsigned long arg) |
245 | { | |
246 | struct gendisk *disk = bdev->bd_disk; | |
247 | ||
248 | if (disk->fops->ioctl) | |
249 | return disk->fops->ioctl(bdev, mode, cmd, | |
250 | (unsigned long)compat_ptr(arg)); | |
251 | ||
252 | return -ENOIOCTLCMD; | |
253 | } | |
254 | EXPORT_SYMBOL(blkdev_compat_ptr_ioctl); | |
255 | #endif | |
256 | ||
9a72a024 | 257 | static bool blkdev_pr_allowed(struct block_device *bdev, blk_mode_t mode) |
12629621 JX |
258 | { |
259 | /* no sense to make reservations for partitions */ | |
260 | if (bdev_is_partition(bdev)) | |
261 | return false; | |
262 | ||
263 | if (capable(CAP_SYS_ADMIN)) | |
264 | return true; | |
9a72a024 JX |
265 | /* |
266 | * Only allow unprivileged reservations if the file descriptor is open | |
267 | * for writing. | |
268 | */ | |
269 | return mode & BLK_OPEN_WRITE; | |
12629621 JX |
270 | } |
271 | ||
9a72a024 | 272 | static int blkdev_pr_register(struct block_device *bdev, blk_mode_t mode, |
bbd3e064 CH |
273 | struct pr_registration __user *arg) |
274 | { | |
275 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
276 | struct pr_registration reg; | |
277 | ||
9a72a024 | 278 | if (!blkdev_pr_allowed(bdev, mode)) |
bbd3e064 CH |
279 | return -EPERM; |
280 | if (!ops || !ops->pr_register) | |
281 | return -EOPNOTSUPP; | |
282 | if (copy_from_user(®, arg, sizeof(reg))) | |
283 | return -EFAULT; | |
284 | ||
285 | if (reg.flags & ~PR_FL_IGNORE_KEY) | |
286 | return -EOPNOTSUPP; | |
287 | return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags); | |
288 | } | |
289 | ||
9a72a024 | 290 | static int blkdev_pr_reserve(struct block_device *bdev, blk_mode_t mode, |
bbd3e064 CH |
291 | struct pr_reservation __user *arg) |
292 | { | |
293 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
294 | struct pr_reservation rsv; | |
295 | ||
9a72a024 | 296 | if (!blkdev_pr_allowed(bdev, mode)) |
bbd3e064 CH |
297 | return -EPERM; |
298 | if (!ops || !ops->pr_reserve) | |
299 | return -EOPNOTSUPP; | |
300 | if (copy_from_user(&rsv, arg, sizeof(rsv))) | |
301 | return -EFAULT; | |
302 | ||
303 | if (rsv.flags & ~PR_FL_IGNORE_KEY) | |
304 | return -EOPNOTSUPP; | |
305 | return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags); | |
306 | } | |
307 | ||
9a72a024 | 308 | static int blkdev_pr_release(struct block_device *bdev, blk_mode_t mode, |
bbd3e064 CH |
309 | struct pr_reservation __user *arg) |
310 | { | |
311 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
312 | struct pr_reservation rsv; | |
313 | ||
9a72a024 | 314 | if (!blkdev_pr_allowed(bdev, mode)) |
bbd3e064 CH |
315 | return -EPERM; |
316 | if (!ops || !ops->pr_release) | |
317 | return -EOPNOTSUPP; | |
318 | if (copy_from_user(&rsv, arg, sizeof(rsv))) | |
319 | return -EFAULT; | |
320 | ||
321 | if (rsv.flags) | |
322 | return -EOPNOTSUPP; | |
323 | return ops->pr_release(bdev, rsv.key, rsv.type); | |
324 | } | |
325 | ||
9a72a024 | 326 | static int blkdev_pr_preempt(struct block_device *bdev, blk_mode_t mode, |
bbd3e064 CH |
327 | struct pr_preempt __user *arg, bool abort) |
328 | { | |
329 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
330 | struct pr_preempt p; | |
331 | ||
9a72a024 | 332 | if (!blkdev_pr_allowed(bdev, mode)) |
bbd3e064 CH |
333 | return -EPERM; |
334 | if (!ops || !ops->pr_preempt) | |
335 | return -EOPNOTSUPP; | |
336 | if (copy_from_user(&p, arg, sizeof(p))) | |
337 | return -EFAULT; | |
338 | ||
339 | if (p.flags) | |
340 | return -EOPNOTSUPP; | |
341 | return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort); | |
342 | } | |
343 | ||
9a72a024 | 344 | static int blkdev_pr_clear(struct block_device *bdev, blk_mode_t mode, |
bbd3e064 CH |
345 | struct pr_clear __user *arg) |
346 | { | |
347 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
348 | struct pr_clear c; | |
349 | ||
9a72a024 | 350 | if (!blkdev_pr_allowed(bdev, mode)) |
bbd3e064 CH |
351 | return -EPERM; |
352 | if (!ops || !ops->pr_clear) | |
353 | return -EOPNOTSUPP; | |
354 | if (copy_from_user(&c, arg, sizeof(c))) | |
355 | return -EFAULT; | |
356 | ||
357 | if (c.flags) | |
358 | return -EOPNOTSUPP; | |
359 | return ops->pr_clear(bdev, c.key); | |
360 | } | |
361 | ||
5e4ea834 CH |
362 | static int blkdev_flushbuf(struct block_device *bdev, unsigned cmd, |
363 | unsigned long arg) | |
bb93e3a5 | 364 | { |
d8e4bb81 CH |
365 | if (!capable(CAP_SYS_ADMIN)) |
366 | return -EACCES; | |
d8e4bb81 CH |
367 | fsync_bdev(bdev); |
368 | invalidate_bdev(bdev); | |
369 | return 0; | |
370 | } | |
d30a2605 | 371 | |
5e4ea834 CH |
372 | static int blkdev_roset(struct block_device *bdev, unsigned cmd, |
373 | unsigned long arg) | |
d8e4bb81 CH |
374 | { |
375 | int ret, n; | |
d30a2605 | 376 | |
bb749b31 ID |
377 | if (!capable(CAP_SYS_ADMIN)) |
378 | return -EACCES; | |
379 | ||
d8e4bb81 CH |
380 | if (get_user(n, (int __user *)arg)) |
381 | return -EFAULT; | |
e00adcad CH |
382 | if (bdev->bd_disk->fops->set_read_only) { |
383 | ret = bdev->bd_disk->fops->set_read_only(bdev, n); | |
384 | if (ret) | |
385 | return ret; | |
386 | } | |
83950d35 | 387 | bdev->bd_read_only = n; |
d8e4bb81 CH |
388 | return 0; |
389 | } | |
d30a2605 | 390 | |
d8e4bb81 CH |
391 | static int blkdev_getgeo(struct block_device *bdev, |
392 | struct hd_geometry __user *argp) | |
393 | { | |
394 | struct gendisk *disk = bdev->bd_disk; | |
395 | struct hd_geometry geo; | |
396 | int ret; | |
d30a2605 | 397 | |
d8e4bb81 CH |
398 | if (!argp) |
399 | return -EINVAL; | |
400 | if (!disk->fops->getgeo) | |
401 | return -ENOTTY; | |
402 | ||
403 | /* | |
404 | * We need to set the startsect first, the driver may | |
405 | * want to override it. | |
406 | */ | |
407 | memset(&geo, 0, sizeof(geo)); | |
408 | geo.start = get_start_sect(bdev); | |
409 | ret = disk->fops->getgeo(bdev, &geo); | |
410 | if (ret) | |
411 | return ret; | |
412 | if (copy_to_user(argp, &geo, sizeof(geo))) | |
413 | return -EFAULT; | |
414 | return 0; | |
415 | } | |
66ba32dc | 416 | |
bdc1ddad AB |
417 | #ifdef CONFIG_COMPAT |
418 | struct compat_hd_geometry { | |
419 | unsigned char heads; | |
420 | unsigned char sectors; | |
421 | unsigned short cylinders; | |
422 | u32 start; | |
423 | }; | |
424 | ||
9b81648c AB |
425 | static int compat_hdio_getgeo(struct block_device *bdev, |
426 | struct compat_hd_geometry __user *ugeo) | |
bdc1ddad | 427 | { |
9b81648c | 428 | struct gendisk *disk = bdev->bd_disk; |
bdc1ddad AB |
429 | struct hd_geometry geo; |
430 | int ret; | |
431 | ||
432 | if (!ugeo) | |
433 | return -EINVAL; | |
434 | if (!disk->fops->getgeo) | |
435 | return -ENOTTY; | |
436 | ||
437 | memset(&geo, 0, sizeof(geo)); | |
438 | /* | |
439 | * We need to set the startsect first, the driver may | |
440 | * want to override it. | |
441 | */ | |
442 | geo.start = get_start_sect(bdev); | |
443 | ret = disk->fops->getgeo(bdev, &geo); | |
444 | if (ret) | |
445 | return ret; | |
446 | ||
447 | ret = copy_to_user(ugeo, &geo, 4); | |
448 | ret |= put_user(geo.start, &ugeo->start); | |
449 | if (ret) | |
450 | ret = -EFAULT; | |
451 | ||
452 | return ret; | |
453 | } | |
454 | #endif | |
455 | ||
d8e4bb81 | 456 | /* set the logical block size */ |
05bdb996 | 457 | static int blkdev_bszset(struct block_device *bdev, blk_mode_t mode, |
d8e4bb81 CH |
458 | int __user *argp) |
459 | { | |
460 | int ret, n; | |
66ba32dc | 461 | |
d8e4bb81 CH |
462 | if (!capable(CAP_SYS_ADMIN)) |
463 | return -EACCES; | |
464 | if (!argp) | |
465 | return -EINVAL; | |
466 | if (get_user(n, argp)) | |
467 | return -EFAULT; | |
66ba32dc | 468 | |
05bdb996 | 469 | if (mode & BLK_OPEN_EXCL) |
47816282 | 470 | return set_blocksize(bdev, n); |
d30a2605 | 471 | |
2736e8ee | 472 | if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode, &bdev, NULL))) |
47816282 | 473 | return -EBUSY; |
d8e4bb81 | 474 | ret = set_blocksize(bdev, n); |
2736e8ee | 475 | blkdev_put(bdev, &bdev); |
47816282 | 476 | |
d8e4bb81 CH |
477 | return ret; |
478 | } | |
a885c8c4 | 479 | |
d8e4bb81 | 480 | /* |
9b81648c AB |
481 | * Common commands that are handled the same way on native and compat |
482 | * user space. Note the separate arg/argp parameters that are needed | |
483 | * to deal with the compat_ptr() conversion. | |
d8e4bb81 | 484 | */ |
05bdb996 | 485 | static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, |
0f77b29a YK |
486 | unsigned int cmd, unsigned long arg, |
487 | void __user *argp) | |
d8e4bb81 | 488 | { |
d8e4bb81 CH |
489 | unsigned int max_sectors; |
490 | ||
491 | switch (cmd) { | |
492 | case BLKFLSBUF: | |
5e4ea834 | 493 | return blkdev_flushbuf(bdev, cmd, arg); |
d8e4bb81 | 494 | case BLKROSET: |
5e4ea834 | 495 | return blkdev_roset(bdev, cmd, arg); |
d8e4bb81 | 496 | case BLKDISCARD: |
44abff2c | 497 | return blk_ioctl_discard(bdev, mode, arg); |
d8e4bb81 | 498 | case BLKSECDISCARD: |
44abff2c | 499 | return blk_ioctl_secure_erase(bdev, mode, argp); |
d8e4bb81 CH |
500 | case BLKZEROOUT: |
501 | return blk_ioctl_zeroout(bdev, mode, arg); | |
7957d93b MC |
502 | case BLKGETDISKSEQ: |
503 | return put_u64(argp, bdev->bd_disk->diskseq); | |
3ed05a98 | 504 | case BLKREPORTZONE: |
5e4ea834 | 505 | return blkdev_report_zones_ioctl(bdev, cmd, arg); |
3ed05a98 | 506 | case BLKRESETZONE: |
e876df1f AJ |
507 | case BLKOPENZONE: |
508 | case BLKCLOSEZONE: | |
509 | case BLKFINISHZONE: | |
510 | return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg); | |
72cd8757 | 511 | case BLKGETZONESZ: |
9b81648c | 512 | return put_uint(argp, bdev_zone_sectors(bdev)); |
65e4e3ee | 513 | case BLKGETNRZONES: |
b623e347 | 514 | return put_uint(argp, bdev_nr_zones(bdev)); |
45048d09 | 515 | case BLKROGET: |
9b81648c | 516 | return put_int(argp, bdev_read_only(bdev) != 0); |
ac481c20 | 517 | case BLKSSZGET: /* get block device logical block size */ |
9b81648c | 518 | return put_int(argp, bdev_logical_block_size(bdev)); |
ac481c20 | 519 | case BLKPBSZGET: /* get block device physical block size */ |
9b81648c | 520 | return put_uint(argp, bdev_physical_block_size(bdev)); |
ac481c20 | 521 | case BLKIOMIN: |
9b81648c | 522 | return put_uint(argp, bdev_io_min(bdev)); |
ac481c20 | 523 | case BLKIOOPT: |
9b81648c | 524 | return put_uint(argp, bdev_io_opt(bdev)); |
ac481c20 | 525 | case BLKALIGNOFF: |
9b81648c | 526 | return put_int(argp, bdev_alignment_offset(bdev)); |
98262f27 | 527 | case BLKDISCARDZEROES: |
9b81648c | 528 | return put_uint(argp, 0); |
45048d09 | 529 | case BLKSECTGET: |
63f26496 AM |
530 | max_sectors = min_t(unsigned int, USHRT_MAX, |
531 | queue_max_sectors(bdev_get_queue(bdev))); | |
9b81648c | 532 | return put_ushort(argp, max_sectors); |
ef00f59c | 533 | case BLKROTATIONAL: |
10f0d2a5 | 534 | return put_ushort(argp, !bdev_nonrot(bdev)); |
45048d09 AV |
535 | case BLKRASET: |
536 | case BLKFRASET: | |
537 | if(!capable(CAP_SYS_ADMIN)) | |
538 | return -EACCES; | |
a11d7fc2 | 539 | bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE; |
45048d09 | 540 | return 0; |
45048d09 | 541 | case BLKRRPART: |
e16e506c CH |
542 | if (!capable(CAP_SYS_ADMIN)) |
543 | return -EACCES; | |
544 | if (bdev_is_partition(bdev)) | |
545 | return -EINVAL; | |
e5cfefa9 | 546 | return disk_scan_partitions(bdev->bd_disk, mode); |
45048d09 AV |
547 | case BLKTRACESTART: |
548 | case BLKTRACESTOP: | |
45048d09 | 549 | case BLKTRACETEARDOWN: |
d8e4bb81 | 550 | return blk_trace_ioctl(bdev, cmd, argp); |
bbd3e064 | 551 | case IOC_PR_REGISTER: |
9a72a024 | 552 | return blkdev_pr_register(bdev, mode, argp); |
bbd3e064 | 553 | case IOC_PR_RESERVE: |
9a72a024 | 554 | return blkdev_pr_reserve(bdev, mode, argp); |
bbd3e064 | 555 | case IOC_PR_RELEASE: |
9a72a024 | 556 | return blkdev_pr_release(bdev, mode, argp); |
bbd3e064 | 557 | case IOC_PR_PREEMPT: |
9a72a024 | 558 | return blkdev_pr_preempt(bdev, mode, argp, false); |
bbd3e064 | 559 | case IOC_PR_PREEMPT_ABORT: |
9a72a024 | 560 | return blkdev_pr_preempt(bdev, mode, argp, true); |
bbd3e064 | 561 | case IOC_PR_CLEAR: |
9a72a024 | 562 | return blkdev_pr_clear(bdev, mode, argp); |
45048d09 | 563 | default: |
9b81648c | 564 | return -ENOIOCTLCMD; |
45048d09 | 565 | } |
1da177e4 | 566 | } |
9b81648c AB |
567 | |
568 | /* | |
569 | * Always keep this in sync with compat_blkdev_ioctl() | |
570 | * to handle all incompatible commands in both functions. | |
571 | * | |
572 | * New commands must be compatible and go into blkdev_common_ioctl | |
573 | */ | |
8a709512 | 574 | long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
9b81648c | 575 | { |
8a709512 | 576 | struct block_device *bdev = I_BDEV(file->f_mapping->host); |
9b81648c | 577 | void __user *argp = (void __user *)arg; |
05bdb996 | 578 | blk_mode_t mode = file_to_blk_mode(file); |
8a709512 CH |
579 | int ret; |
580 | ||
9b81648c AB |
581 | switch (cmd) { |
582 | /* These need separate implementations for the data structure */ | |
583 | case HDIO_GETGEO: | |
584 | return blkdev_getgeo(bdev, argp); | |
585 | case BLKPG: | |
586 | return blkpg_ioctl(bdev, argp); | |
587 | ||
588 | /* Compat mode returns 32-bit data instead of 'long' */ | |
589 | case BLKRAGET: | |
590 | case BLKFRAGET: | |
591 | if (!argp) | |
592 | return -EINVAL; | |
a11d7fc2 CH |
593 | return put_long(argp, |
594 | (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512); | |
9b81648c | 595 | case BLKGETSIZE: |
946e9937 | 596 | if (bdev_nr_sectors(bdev) > ~0UL) |
9b81648c | 597 | return -EFBIG; |
946e9937 | 598 | return put_ulong(argp, bdev_nr_sectors(bdev)); |
9b81648c AB |
599 | |
600 | /* The data is compatible, but the command number is different */ | |
601 | case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */ | |
602 | return put_int(argp, block_size(bdev)); | |
603 | case BLKBSZSET: | |
604 | return blkdev_bszset(bdev, mode, argp); | |
605 | case BLKGETSIZE64: | |
946e9937 | 606 | return put_u64(argp, bdev_nr_bytes(bdev)); |
9b81648c AB |
607 | |
608 | /* Incompatible alignment on i386 */ | |
609 | case BLKTRACESETUP: | |
610 | return blk_trace_ioctl(bdev, cmd, argp); | |
611 | default: | |
612 | break; | |
613 | } | |
614 | ||
0f77b29a | 615 | ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); |
a7cb3d2f CH |
616 | if (ret != -ENOIOCTLCMD) |
617 | return ret; | |
9b81648c | 618 | |
a7cb3d2f CH |
619 | if (!bdev->bd_disk->fops->ioctl) |
620 | return -ENOTTY; | |
621 | return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); | |
9b81648c | 622 | } |
bdc1ddad AB |
623 | |
624 | #ifdef CONFIG_COMPAT | |
9b81648c | 625 | |
bdc1ddad AB |
626 | #define BLKBSZGET_32 _IOR(0x12, 112, int) |
627 | #define BLKBSZSET_32 _IOW(0x12, 113, int) | |
628 | #define BLKGETSIZE64_32 _IOR(0x12, 114, int) | |
629 | ||
630 | /* Most of the generic ioctls are handled in the normal fallback path. | |
631 | This assumes the blkdev's low level compat_ioctl always returns | |
632 | ENOIOCTLCMD for unknown ioctls. */ | |
633 | long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) | |
634 | { | |
9b81648c AB |
635 | int ret; |
636 | void __user *argp = compat_ptr(arg); | |
4e7b5671 | 637 | struct block_device *bdev = I_BDEV(file->f_mapping->host); |
bdc1ddad | 638 | struct gendisk *disk = bdev->bd_disk; |
05bdb996 | 639 | blk_mode_t mode = file_to_blk_mode(file); |
bdc1ddad AB |
640 | |
641 | switch (cmd) { | |
9b81648c | 642 | /* These need separate implementations for the data structure */ |
bdc1ddad | 643 | case HDIO_GETGEO: |
9b81648c | 644 | return compat_hdio_getgeo(bdev, argp); |
bdc1ddad | 645 | case BLKPG: |
9b81648c AB |
646 | return compat_blkpg_ioctl(bdev, argp); |
647 | ||
648 | /* Compat mode returns 32-bit data instead of 'long' */ | |
bdc1ddad AB |
649 | case BLKRAGET: |
650 | case BLKFRAGET: | |
9b81648c | 651 | if (!argp) |
bdc1ddad | 652 | return -EINVAL; |
9b81648c | 653 | return compat_put_long(argp, |
a11d7fc2 | 654 | (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512); |
bdc1ddad | 655 | case BLKGETSIZE: |
ccf16413 | 656 | if (bdev_nr_sectors(bdev) > ~(compat_ulong_t)0) |
bdc1ddad | 657 | return -EFBIG; |
946e9937 | 658 | return compat_put_ulong(argp, bdev_nr_sectors(bdev)); |
bdc1ddad | 659 | |
9b81648c AB |
660 | /* The data is compatible, but the command number is different */ |
661 | case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */ | |
662 | return put_int(argp, bdev_logical_block_size(bdev)); | |
663 | case BLKBSZSET_32: | |
664 | return blkdev_bszset(bdev, mode, argp); | |
bdc1ddad | 665 | case BLKGETSIZE64_32: |
946e9937 | 666 | return put_u64(argp, bdev_nr_bytes(bdev)); |
bdc1ddad | 667 | |
9b81648c | 668 | /* Incompatible alignment on i386 */ |
bdc1ddad | 669 | case BLKTRACESETUP32: |
9b81648c | 670 | return blk_trace_ioctl(bdev, cmd, argp); |
bdc1ddad | 671 | default: |
9b81648c | 672 | break; |
bdc1ddad | 673 | } |
9b81648c | 674 | |
0f77b29a | 675 | ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); |
9b81648c AB |
676 | if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl) |
677 | ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg); | |
678 | ||
679 | return ret; | |
bdc1ddad AB |
680 | } |
681 | #endif |