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