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