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 | ||
4601b4b1 | 85 | static int blkdev_reread_part(struct block_device *bdev, fmode_t mode) |
1da177e4 | 86 | { |
4601b4b1 | 87 | struct block_device *tmp; |
f0b870df | 88 | |
fa01b1e9 | 89 | if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev)) |
1da177e4 LT |
90 | return -EINVAL; |
91 | if (!capable(CAP_SYS_ADMIN)) | |
92 | return -EACCES; | |
ab4b5705 | 93 | if (bdev->bd_disk->open_partitions) |
68e6582e | 94 | return -EBUSY; |
be324177 | 95 | |
4601b4b1 CH |
96 | /* |
97 | * Reopen the device to revalidate the driver state and force a | |
98 | * partition rescan. | |
99 | */ | |
100 | mode &= ~FMODE_EXCL; | |
101 | set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state); | |
be324177 | 102 | |
4601b4b1 CH |
103 | tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL); |
104 | if (IS_ERR(tmp)) | |
105 | return PTR_ERR(tmp); | |
106 | blkdev_put(tmp, mode); | |
107 | return 0; | |
1da177e4 LT |
108 | } |
109 | ||
d8e4bb81 CH |
110 | static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode, |
111 | unsigned long arg, unsigned long flags) | |
d30a2605 | 112 | { |
d8e4bb81 CH |
113 | uint64_t range[2]; |
114 | uint64_t start, len; | |
351499a1 | 115 | struct request_queue *q = bdev_get_queue(bdev); |
384d87ef | 116 | int err; |
d8e4bb81 CH |
117 | |
118 | if (!(mode & FMODE_WRITE)) | |
119 | return -EBADF; | |
120 | ||
351499a1 DM |
121 | if (!blk_queue_discard(q)) |
122 | return -EOPNOTSUPP; | |
123 | ||
d8e4bb81 CH |
124 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) |
125 | return -EFAULT; | |
126 | ||
127 | start = range[0]; | |
128 | len = range[1]; | |
8d57a98c | 129 | |
d30a2605 DW |
130 | if (start & 511) |
131 | return -EINVAL; | |
132 | if (len & 511) | |
133 | return -EINVAL; | |
d30a2605 | 134 | |
351499a1 | 135 | if (start + len > i_size_read(bdev->bd_inode)) |
d30a2605 | 136 | return -EINVAL; |
384d87ef JK |
137 | |
138 | err = truncate_bdev_range(bdev, mode, start, start + len - 1); | |
139 | if (err) | |
140 | return err; | |
141 | ||
351499a1 DM |
142 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, |
143 | GFP_KERNEL, flags); | |
d30a2605 DW |
144 | } |
145 | ||
d8e4bb81 CH |
146 | static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode, |
147 | unsigned long arg) | |
66ba32dc | 148 | { |
d8e4bb81 | 149 | uint64_t range[2]; |
22dd6d35 | 150 | uint64_t start, end, len; |
384d87ef | 151 | int err; |
d8e4bb81 CH |
152 | |
153 | if (!(mode & FMODE_WRITE)) | |
154 | return -EBADF; | |
155 | ||
156 | if (copy_from_user(range, (void __user *)arg, sizeof(range))) | |
157 | return -EFAULT; | |
158 | ||
159 | start = range[0]; | |
160 | len = range[1]; | |
22dd6d35 | 161 | end = start + len - 1; |
d8e4bb81 | 162 | |
66ba32dc MP |
163 | if (start & 511) |
164 | return -EINVAL; | |
165 | if (len & 511) | |
166 | return -EINVAL; | |
22dd6d35 DW |
167 | if (end >= (uint64_t)i_size_read(bdev->bd_inode)) |
168 | return -EINVAL; | |
169 | if (end < start) | |
66ba32dc MP |
170 | return -EINVAL; |
171 | ||
22dd6d35 | 172 | /* Invalidate the page cache, including dirty pages */ |
384d87ef JK |
173 | err = truncate_bdev_range(bdev, mode, start, end); |
174 | if (err) | |
175 | return err; | |
22dd6d35 DW |
176 | |
177 | return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL, | |
ee472d83 | 178 | BLKDEV_ZERO_NOUNMAP); |
66ba32dc MP |
179 | } |
180 | ||
9b81648c | 181 | static int put_ushort(unsigned short __user *argp, unsigned short val) |
1da177e4 | 182 | { |
9b81648c | 183 | return put_user(val, argp); |
1da177e4 LT |
184 | } |
185 | ||
9b81648c | 186 | static int put_int(int __user *argp, int val) |
1da177e4 | 187 | { |
9b81648c | 188 | return put_user(val, argp); |
1da177e4 LT |
189 | } |
190 | ||
9b81648c | 191 | static int put_uint(unsigned int __user *argp, unsigned int val) |
ac481c20 | 192 | { |
9b81648c | 193 | return put_user(val, argp); |
ac481c20 MP |
194 | } |
195 | ||
9b81648c | 196 | static int put_long(long __user *argp, long val) |
1da177e4 | 197 | { |
9b81648c | 198 | return put_user(val, argp); |
1da177e4 LT |
199 | } |
200 | ||
9b81648c | 201 | static int put_ulong(unsigned long __user *argp, unsigned long val) |
1da177e4 | 202 | { |
9b81648c | 203 | return put_user(val, argp); |
1da177e4 LT |
204 | } |
205 | ||
9b81648c | 206 | static int put_u64(u64 __user *argp, u64 val) |
1da177e4 | 207 | { |
9b81648c | 208 | return put_user(val, argp); |
1da177e4 LT |
209 | } |
210 | ||
bdc1ddad | 211 | #ifdef CONFIG_COMPAT |
c8210a57 | 212 | static int compat_put_long(compat_long_t __user *argp, long val) |
bdc1ddad | 213 | { |
9b81648c | 214 | return put_user(val, argp); |
bdc1ddad AB |
215 | } |
216 | ||
c8210a57 | 217 | static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val) |
bdc1ddad | 218 | { |
9b81648c | 219 | return put_user(val, argp); |
bdc1ddad AB |
220 | } |
221 | #endif | |
222 | ||
ee6a129d AB |
223 | #ifdef CONFIG_COMPAT |
224 | /* | |
225 | * This is the equivalent of compat_ptr_ioctl(), to be used by block | |
226 | * drivers that implement only commands that are completely compatible | |
227 | * between 32-bit and 64-bit user space | |
228 | */ | |
229 | int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode, | |
230 | unsigned cmd, unsigned long arg) | |
231 | { | |
232 | struct gendisk *disk = bdev->bd_disk; | |
233 | ||
234 | if (disk->fops->ioctl) | |
235 | return disk->fops->ioctl(bdev, mode, cmd, | |
236 | (unsigned long)compat_ptr(arg)); | |
237 | ||
238 | return -ENOIOCTLCMD; | |
239 | } | |
240 | EXPORT_SYMBOL(blkdev_compat_ptr_ioctl); | |
241 | #endif | |
242 | ||
bbd3e064 CH |
243 | static int blkdev_pr_register(struct block_device *bdev, |
244 | struct pr_registration __user *arg) | |
245 | { | |
246 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
247 | struct pr_registration reg; | |
248 | ||
249 | if (!capable(CAP_SYS_ADMIN)) | |
250 | return -EPERM; | |
251 | if (!ops || !ops->pr_register) | |
252 | return -EOPNOTSUPP; | |
253 | if (copy_from_user(®, arg, sizeof(reg))) | |
254 | return -EFAULT; | |
255 | ||
256 | if (reg.flags & ~PR_FL_IGNORE_KEY) | |
257 | return -EOPNOTSUPP; | |
258 | return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags); | |
259 | } | |
260 | ||
261 | static int blkdev_pr_reserve(struct block_device *bdev, | |
262 | struct pr_reservation __user *arg) | |
263 | { | |
264 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
265 | struct pr_reservation rsv; | |
266 | ||
267 | if (!capable(CAP_SYS_ADMIN)) | |
268 | return -EPERM; | |
269 | if (!ops || !ops->pr_reserve) | |
270 | return -EOPNOTSUPP; | |
271 | if (copy_from_user(&rsv, arg, sizeof(rsv))) | |
272 | return -EFAULT; | |
273 | ||
274 | if (rsv.flags & ~PR_FL_IGNORE_KEY) | |
275 | return -EOPNOTSUPP; | |
276 | return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags); | |
277 | } | |
278 | ||
279 | static int blkdev_pr_release(struct block_device *bdev, | |
280 | struct pr_reservation __user *arg) | |
281 | { | |
282 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
283 | struct pr_reservation rsv; | |
284 | ||
285 | if (!capable(CAP_SYS_ADMIN)) | |
286 | return -EPERM; | |
287 | if (!ops || !ops->pr_release) | |
288 | return -EOPNOTSUPP; | |
289 | if (copy_from_user(&rsv, arg, sizeof(rsv))) | |
290 | return -EFAULT; | |
291 | ||
292 | if (rsv.flags) | |
293 | return -EOPNOTSUPP; | |
294 | return ops->pr_release(bdev, rsv.key, rsv.type); | |
295 | } | |
296 | ||
297 | static int blkdev_pr_preempt(struct block_device *bdev, | |
298 | struct pr_preempt __user *arg, bool abort) | |
299 | { | |
300 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
301 | struct pr_preempt p; | |
302 | ||
303 | if (!capable(CAP_SYS_ADMIN)) | |
304 | return -EPERM; | |
305 | if (!ops || !ops->pr_preempt) | |
306 | return -EOPNOTSUPP; | |
307 | if (copy_from_user(&p, arg, sizeof(p))) | |
308 | return -EFAULT; | |
309 | ||
310 | if (p.flags) | |
311 | return -EOPNOTSUPP; | |
312 | return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort); | |
313 | } | |
314 | ||
315 | static int blkdev_pr_clear(struct block_device *bdev, | |
316 | struct pr_clear __user *arg) | |
317 | { | |
318 | const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; | |
319 | struct pr_clear c; | |
320 | ||
321 | if (!capable(CAP_SYS_ADMIN)) | |
322 | return -EPERM; | |
323 | if (!ops || !ops->pr_clear) | |
324 | return -EOPNOTSUPP; | |
325 | if (copy_from_user(&c, arg, sizeof(c))) | |
326 | return -EFAULT; | |
327 | ||
328 | if (c.flags) | |
329 | return -EOPNOTSUPP; | |
330 | return ops->pr_clear(bdev, c.key); | |
331 | } | |
332 | ||
d8e4bb81 CH |
333 | static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode, |
334 | unsigned cmd, unsigned long arg) | |
bb93e3a5 | 335 | { |
d8e4bb81 CH |
336 | if (!capable(CAP_SYS_ADMIN)) |
337 | return -EACCES; | |
d8e4bb81 CH |
338 | fsync_bdev(bdev); |
339 | invalidate_bdev(bdev); | |
340 | return 0; | |
341 | } | |
d30a2605 | 342 | |
d8e4bb81 CH |
343 | static int blkdev_roset(struct block_device *bdev, fmode_t mode, |
344 | unsigned cmd, unsigned long arg) | |
345 | { | |
346 | int ret, n; | |
d30a2605 | 347 | |
bb749b31 ID |
348 | if (!capable(CAP_SYS_ADMIN)) |
349 | return -EACCES; | |
350 | ||
d8e4bb81 CH |
351 | if (get_user(n, (int __user *)arg)) |
352 | return -EFAULT; | |
e00adcad CH |
353 | if (bdev->bd_disk->fops->set_read_only) { |
354 | ret = bdev->bd_disk->fops->set_read_only(bdev, n); | |
355 | if (ret) | |
356 | return ret; | |
357 | } | |
83950d35 | 358 | bdev->bd_read_only = n; |
d8e4bb81 CH |
359 | return 0; |
360 | } | |
d30a2605 | 361 | |
d8e4bb81 CH |
362 | static int blkdev_getgeo(struct block_device *bdev, |
363 | struct hd_geometry __user *argp) | |
364 | { | |
365 | struct gendisk *disk = bdev->bd_disk; | |
366 | struct hd_geometry geo; | |
367 | int ret; | |
d30a2605 | 368 | |
d8e4bb81 CH |
369 | if (!argp) |
370 | return -EINVAL; | |
371 | if (!disk->fops->getgeo) | |
372 | return -ENOTTY; | |
373 | ||
374 | /* | |
375 | * We need to set the startsect first, the driver may | |
376 | * want to override it. | |
377 | */ | |
378 | memset(&geo, 0, sizeof(geo)); | |
379 | geo.start = get_start_sect(bdev); | |
380 | ret = disk->fops->getgeo(bdev, &geo); | |
381 | if (ret) | |
382 | return ret; | |
383 | if (copy_to_user(argp, &geo, sizeof(geo))) | |
384 | return -EFAULT; | |
385 | return 0; | |
386 | } | |
66ba32dc | 387 | |
bdc1ddad AB |
388 | #ifdef CONFIG_COMPAT |
389 | struct compat_hd_geometry { | |
390 | unsigned char heads; | |
391 | unsigned char sectors; | |
392 | unsigned short cylinders; | |
393 | u32 start; | |
394 | }; | |
395 | ||
9b81648c AB |
396 | static int compat_hdio_getgeo(struct block_device *bdev, |
397 | struct compat_hd_geometry __user *ugeo) | |
bdc1ddad | 398 | { |
9b81648c | 399 | struct gendisk *disk = bdev->bd_disk; |
bdc1ddad AB |
400 | struct hd_geometry geo; |
401 | int ret; | |
402 | ||
403 | if (!ugeo) | |
404 | return -EINVAL; | |
405 | if (!disk->fops->getgeo) | |
406 | return -ENOTTY; | |
407 | ||
408 | memset(&geo, 0, sizeof(geo)); | |
409 | /* | |
410 | * We need to set the startsect first, the driver may | |
411 | * want to override it. | |
412 | */ | |
413 | geo.start = get_start_sect(bdev); | |
414 | ret = disk->fops->getgeo(bdev, &geo); | |
415 | if (ret) | |
416 | return ret; | |
417 | ||
418 | ret = copy_to_user(ugeo, &geo, 4); | |
419 | ret |= put_user(geo.start, &ugeo->start); | |
420 | if (ret) | |
421 | ret = -EFAULT; | |
422 | ||
423 | return ret; | |
424 | } | |
425 | #endif | |
426 | ||
d8e4bb81 CH |
427 | /* set the logical block size */ |
428 | static int blkdev_bszset(struct block_device *bdev, fmode_t mode, | |
429 | int __user *argp) | |
430 | { | |
431 | int ret, n; | |
66ba32dc | 432 | |
d8e4bb81 CH |
433 | if (!capable(CAP_SYS_ADMIN)) |
434 | return -EACCES; | |
435 | if (!argp) | |
436 | return -EINVAL; | |
437 | if (get_user(n, argp)) | |
438 | return -EFAULT; | |
66ba32dc | 439 | |
47816282 CH |
440 | if (mode & FMODE_EXCL) |
441 | return set_blocksize(bdev, n); | |
d30a2605 | 442 | |
47816282 CH |
443 | if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev))) |
444 | return -EBUSY; | |
d8e4bb81 | 445 | ret = set_blocksize(bdev, n); |
47816282 CH |
446 | blkdev_put(bdev, mode | FMODE_EXCL); |
447 | ||
d8e4bb81 CH |
448 | return ret; |
449 | } | |
a885c8c4 | 450 | |
d8e4bb81 | 451 | /* |
9b81648c AB |
452 | * Common commands that are handled the same way on native and compat |
453 | * user space. Note the separate arg/argp parameters that are needed | |
454 | * to deal with the compat_ptr() conversion. | |
d8e4bb81 | 455 | */ |
9b81648c AB |
456 | static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode, |
457 | unsigned cmd, unsigned long arg, void __user *argp) | |
d8e4bb81 | 458 | { |
d8e4bb81 CH |
459 | unsigned int max_sectors; |
460 | ||
461 | switch (cmd) { | |
462 | case BLKFLSBUF: | |
463 | return blkdev_flushbuf(bdev, mode, cmd, arg); | |
464 | case BLKROSET: | |
465 | return blkdev_roset(bdev, mode, cmd, arg); | |
466 | case BLKDISCARD: | |
467 | return blk_ioctl_discard(bdev, mode, arg, 0); | |
468 | case BLKSECDISCARD: | |
469 | return blk_ioctl_discard(bdev, mode, arg, | |
470 | BLKDEV_DISCARD_SECURE); | |
471 | case BLKZEROOUT: | |
472 | return blk_ioctl_zeroout(bdev, mode, arg); | |
7957d93b MC |
473 | case BLKGETDISKSEQ: |
474 | return put_u64(argp, bdev->bd_disk->diskseq); | |
3ed05a98 ST |
475 | case BLKREPORTZONE: |
476 | return blkdev_report_zones_ioctl(bdev, mode, cmd, arg); | |
477 | case BLKRESETZONE: | |
e876df1f AJ |
478 | case BLKOPENZONE: |
479 | case BLKCLOSEZONE: | |
480 | case BLKFINISHZONE: | |
481 | return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg); | |
72cd8757 | 482 | case BLKGETZONESZ: |
9b81648c | 483 | return put_uint(argp, bdev_zone_sectors(bdev)); |
65e4e3ee | 484 | case BLKGETNRZONES: |
9b81648c | 485 | return put_uint(argp, blkdev_nr_zones(bdev->bd_disk)); |
45048d09 | 486 | case BLKROGET: |
9b81648c | 487 | return put_int(argp, bdev_read_only(bdev) != 0); |
ac481c20 | 488 | case BLKSSZGET: /* get block device logical block size */ |
9b81648c | 489 | return put_int(argp, bdev_logical_block_size(bdev)); |
ac481c20 | 490 | case BLKPBSZGET: /* get block device physical block size */ |
9b81648c | 491 | return put_uint(argp, bdev_physical_block_size(bdev)); |
ac481c20 | 492 | case BLKIOMIN: |
9b81648c | 493 | return put_uint(argp, bdev_io_min(bdev)); |
ac481c20 | 494 | case BLKIOOPT: |
9b81648c | 495 | return put_uint(argp, bdev_io_opt(bdev)); |
ac481c20 | 496 | case BLKALIGNOFF: |
9b81648c | 497 | return put_int(argp, bdev_alignment_offset(bdev)); |
98262f27 | 498 | case BLKDISCARDZEROES: |
9b81648c | 499 | return put_uint(argp, 0); |
45048d09 | 500 | case BLKSECTGET: |
63f26496 AM |
501 | max_sectors = min_t(unsigned int, USHRT_MAX, |
502 | queue_max_sectors(bdev_get_queue(bdev))); | |
9b81648c | 503 | return put_ushort(argp, max_sectors); |
ef00f59c | 504 | case BLKROTATIONAL: |
9b81648c | 505 | return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev))); |
45048d09 AV |
506 | case BLKRASET: |
507 | case BLKFRASET: | |
508 | if(!capable(CAP_SYS_ADMIN)) | |
509 | return -EACCES; | |
a11d7fc2 | 510 | bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE; |
45048d09 | 511 | return 0; |
45048d09 | 512 | case BLKRRPART: |
4601b4b1 | 513 | return blkdev_reread_part(bdev, mode); |
45048d09 AV |
514 | case BLKTRACESTART: |
515 | case BLKTRACESTOP: | |
45048d09 | 516 | case BLKTRACETEARDOWN: |
d8e4bb81 | 517 | return blk_trace_ioctl(bdev, cmd, argp); |
bbd3e064 CH |
518 | case IOC_PR_REGISTER: |
519 | return blkdev_pr_register(bdev, argp); | |
520 | case IOC_PR_RESERVE: | |
521 | return blkdev_pr_reserve(bdev, argp); | |
522 | case IOC_PR_RELEASE: | |
523 | return blkdev_pr_release(bdev, argp); | |
524 | case IOC_PR_PREEMPT: | |
525 | return blkdev_pr_preempt(bdev, argp, false); | |
526 | case IOC_PR_PREEMPT_ABORT: | |
527 | return blkdev_pr_preempt(bdev, argp, true); | |
528 | case IOC_PR_CLEAR: | |
529 | return blkdev_pr_clear(bdev, argp); | |
45048d09 | 530 | default: |
9b81648c | 531 | return -ENOIOCTLCMD; |
45048d09 | 532 | } |
1da177e4 | 533 | } |
9b81648c AB |
534 | |
535 | /* | |
536 | * Always keep this in sync with compat_blkdev_ioctl() | |
537 | * to handle all incompatible commands in both functions. | |
538 | * | |
539 | * New commands must be compatible and go into blkdev_common_ioctl | |
540 | */ | |
541 | int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, | |
542 | unsigned long arg) | |
543 | { | |
544 | int ret; | |
545 | loff_t size; | |
546 | void __user *argp = (void __user *)arg; | |
547 | ||
548 | switch (cmd) { | |
549 | /* These need separate implementations for the data structure */ | |
550 | case HDIO_GETGEO: | |
551 | return blkdev_getgeo(bdev, argp); | |
552 | case BLKPG: | |
553 | return blkpg_ioctl(bdev, argp); | |
554 | ||
555 | /* Compat mode returns 32-bit data instead of 'long' */ | |
556 | case BLKRAGET: | |
557 | case BLKFRAGET: | |
558 | if (!argp) | |
559 | return -EINVAL; | |
a11d7fc2 CH |
560 | return put_long(argp, |
561 | (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512); | |
9b81648c AB |
562 | case BLKGETSIZE: |
563 | size = i_size_read(bdev->bd_inode); | |
564 | if ((size >> 9) > ~0UL) | |
565 | return -EFBIG; | |
566 | return put_ulong(argp, size >> 9); | |
567 | ||
568 | /* The data is compatible, but the command number is different */ | |
569 | case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */ | |
570 | return put_int(argp, block_size(bdev)); | |
571 | case BLKBSZSET: | |
572 | return blkdev_bszset(bdev, mode, argp); | |
573 | case BLKGETSIZE64: | |
574 | return put_u64(argp, i_size_read(bdev->bd_inode)); | |
575 | ||
576 | /* Incompatible alignment on i386 */ | |
577 | case BLKTRACESETUP: | |
578 | return blk_trace_ioctl(bdev, cmd, argp); | |
579 | default: | |
580 | break; | |
581 | } | |
582 | ||
583 | ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); | |
a7cb3d2f CH |
584 | if (ret != -ENOIOCTLCMD) |
585 | return ret; | |
9b81648c | 586 | |
a7cb3d2f CH |
587 | if (!bdev->bd_disk->fops->ioctl) |
588 | return -ENOTTY; | |
589 | return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); | |
9b81648c AB |
590 | } |
591 | EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */ | |
bdc1ddad AB |
592 | |
593 | #ifdef CONFIG_COMPAT | |
9b81648c | 594 | |
bdc1ddad AB |
595 | #define BLKBSZGET_32 _IOR(0x12, 112, int) |
596 | #define BLKBSZSET_32 _IOW(0x12, 113, int) | |
597 | #define BLKGETSIZE64_32 _IOR(0x12, 114, int) | |
598 | ||
599 | /* Most of the generic ioctls are handled in the normal fallback path. | |
600 | This assumes the blkdev's low level compat_ioctl always returns | |
601 | ENOIOCTLCMD for unknown ioctls. */ | |
602 | long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) | |
603 | { | |
9b81648c AB |
604 | int ret; |
605 | void __user *argp = compat_ptr(arg); | |
4e7b5671 | 606 | struct block_device *bdev = I_BDEV(file->f_mapping->host); |
bdc1ddad AB |
607 | struct gendisk *disk = bdev->bd_disk; |
608 | fmode_t mode = file->f_mode; | |
609 | loff_t size; | |
bdc1ddad AB |
610 | |
611 | /* | |
612 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have | |
613 | * to updated it before every ioctl. | |
614 | */ | |
615 | if (file->f_flags & O_NDELAY) | |
616 | mode |= FMODE_NDELAY; | |
617 | else | |
618 | mode &= ~FMODE_NDELAY; | |
619 | ||
620 | switch (cmd) { | |
9b81648c | 621 | /* These need separate implementations for the data structure */ |
bdc1ddad | 622 | case HDIO_GETGEO: |
9b81648c | 623 | return compat_hdio_getgeo(bdev, argp); |
bdc1ddad | 624 | case BLKPG: |
9b81648c AB |
625 | return compat_blkpg_ioctl(bdev, argp); |
626 | ||
627 | /* Compat mode returns 32-bit data instead of 'long' */ | |
bdc1ddad AB |
628 | case BLKRAGET: |
629 | case BLKFRAGET: | |
9b81648c | 630 | if (!argp) |
bdc1ddad | 631 | return -EINVAL; |
9b81648c | 632 | return compat_put_long(argp, |
a11d7fc2 | 633 | (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512); |
bdc1ddad AB |
634 | case BLKGETSIZE: |
635 | size = i_size_read(bdev->bd_inode); | |
636 | if ((size >> 9) > ~0UL) | |
637 | return -EFBIG; | |
9b81648c | 638 | return compat_put_ulong(argp, size >> 9); |
bdc1ddad | 639 | |
9b81648c AB |
640 | /* The data is compatible, but the command number is different */ |
641 | case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */ | |
642 | return put_int(argp, bdev_logical_block_size(bdev)); | |
643 | case BLKBSZSET_32: | |
644 | return blkdev_bszset(bdev, mode, argp); | |
bdc1ddad | 645 | case BLKGETSIZE64_32: |
9b81648c | 646 | return put_u64(argp, i_size_read(bdev->bd_inode)); |
bdc1ddad | 647 | |
9b81648c | 648 | /* Incompatible alignment on i386 */ |
bdc1ddad | 649 | case BLKTRACESETUP32: |
9b81648c | 650 | return blk_trace_ioctl(bdev, cmd, argp); |
bdc1ddad | 651 | default: |
9b81648c | 652 | break; |
bdc1ddad | 653 | } |
9b81648c AB |
654 | |
655 | ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); | |
656 | if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl) | |
657 | ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg); | |
658 | ||
659 | return ret; | |
bdc1ddad AB |
660 | } |
661 | #endif |