xref: /openbmc/linux/block/ioctl.c (revision b58c6630)
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 block_device *bdevp;
20 	struct gendisk *disk;
21 	struct hd_struct *part, *lpart;
22 	struct blkpg_partition p;
23 	struct disk_part_iter piter;
24 	long long start, length;
25 	int partno;
26 
27 	if (!capable(CAP_SYS_ADMIN))
28 		return -EACCES;
29 	if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
30 		return -EFAULT;
31 	disk = bdev->bd_disk;
32 	if (bdev != bdev->bd_contains)
33 		return -EINVAL;
34 	partno = p.pno;
35 	if (partno <= 0)
36 		return -EINVAL;
37 	switch (op) {
38 		case BLKPG_ADD_PARTITION:
39 			start = p.start >> 9;
40 			length = p.length >> 9;
41 			/* check for fit in a hd_struct */
42 			if (sizeof(sector_t) == sizeof(long) &&
43 			    sizeof(long long) > sizeof(long)) {
44 				long pstart = start, plength = length;
45 				if (pstart != start || plength != length
46 				    || pstart < 0 || plength < 0 || partno > 65535)
47 					return -EINVAL;
48 			}
49 			/* check if partition is aligned to blocksize */
50 			if (p.start & (bdev_logical_block_size(bdev) - 1))
51 				return -EINVAL;
52 
53 			mutex_lock(&bdev->bd_mutex);
54 
55 			/* overlap? */
56 			disk_part_iter_init(&piter, disk,
57 					    DISK_PITER_INCL_EMPTY);
58 			while ((part = disk_part_iter_next(&piter))) {
59 				if (!(start + length <= part->start_sect ||
60 				      start >= part->start_sect + part->nr_sects)) {
61 					disk_part_iter_exit(&piter);
62 					mutex_unlock(&bdev->bd_mutex);
63 					return -EBUSY;
64 				}
65 			}
66 			disk_part_iter_exit(&piter);
67 
68 			/* all seems OK */
69 			part = add_partition(disk, partno, start, length,
70 					     ADDPART_FLAG_NONE, NULL);
71 			mutex_unlock(&bdev->bd_mutex);
72 			return PTR_ERR_OR_ZERO(part);
73 		case BLKPG_DEL_PARTITION:
74 			part = disk_get_part(disk, partno);
75 			if (!part)
76 				return -ENXIO;
77 
78 			bdevp = bdget(part_devt(part));
79 			disk_put_part(part);
80 			if (!bdevp)
81 				return -ENOMEM;
82 
83 			mutex_lock(&bdevp->bd_mutex);
84 			if (bdevp->bd_openers) {
85 				mutex_unlock(&bdevp->bd_mutex);
86 				bdput(bdevp);
87 				return -EBUSY;
88 			}
89 			/* all seems OK */
90 			fsync_bdev(bdevp);
91 			invalidate_bdev(bdevp);
92 
93 			mutex_lock_nested(&bdev->bd_mutex, 1);
94 			delete_partition(disk, partno);
95 			mutex_unlock(&bdev->bd_mutex);
96 			mutex_unlock(&bdevp->bd_mutex);
97 			bdput(bdevp);
98 
99 			return 0;
100 		case BLKPG_RESIZE_PARTITION:
101 			start = p.start >> 9;
102 			/* new length of partition in bytes */
103 			length = p.length >> 9;
104 			/* check for fit in a hd_struct */
105 			if (sizeof(sector_t) == sizeof(long) &&
106 			    sizeof(long long) > sizeof(long)) {
107 				long pstart = start, plength = length;
108 				if (pstart != start || plength != length
109 				    || pstart < 0 || plength < 0)
110 					return -EINVAL;
111 			}
112 			part = disk_get_part(disk, partno);
113 			if (!part)
114 				return -ENXIO;
115 			bdevp = bdget(part_devt(part));
116 			if (!bdevp) {
117 				disk_put_part(part);
118 				return -ENOMEM;
119 			}
120 			mutex_lock(&bdevp->bd_mutex);
121 			mutex_lock_nested(&bdev->bd_mutex, 1);
122 			if (start != part->start_sect) {
123 				mutex_unlock(&bdevp->bd_mutex);
124 				mutex_unlock(&bdev->bd_mutex);
125 				bdput(bdevp);
126 				disk_put_part(part);
127 				return -EINVAL;
128 			}
129 			/* overlap? */
130 			disk_part_iter_init(&piter, disk,
131 					    DISK_PITER_INCL_EMPTY);
132 			while ((lpart = disk_part_iter_next(&piter))) {
133 				if (lpart->partno != partno &&
134 				   !(start + length <= lpart->start_sect ||
135 				   start >= lpart->start_sect + lpart->nr_sects)
136 				   ) {
137 					disk_part_iter_exit(&piter);
138 					mutex_unlock(&bdevp->bd_mutex);
139 					mutex_unlock(&bdev->bd_mutex);
140 					bdput(bdevp);
141 					disk_put_part(part);
142 					return -EBUSY;
143 				}
144 			}
145 			disk_part_iter_exit(&piter);
146 			part_nr_sects_write(part, (sector_t)length);
147 			i_size_write(bdevp->bd_inode, p.length);
148 			mutex_unlock(&bdevp->bd_mutex);
149 			mutex_unlock(&bdev->bd_mutex);
150 			bdput(bdevp);
151 			disk_put_part(part);
152 			return 0;
153 		default:
154 			return -EINVAL;
155 	}
156 }
157 
158 static int blkpg_ioctl(struct block_device *bdev,
159 		       struct blkpg_ioctl_arg __user *arg)
160 {
161 	struct blkpg_partition __user *udata;
162 	int op;
163 
164 	if (get_user(op, &arg->op) || get_user(udata, &arg->data))
165 		return -EFAULT;
166 
167 	return blkpg_do_ioctl(bdev, udata, op);
168 }
169 
170 #ifdef CONFIG_COMPAT
171 struct compat_blkpg_ioctl_arg {
172 	compat_int_t op;
173 	compat_int_t flags;
174 	compat_int_t datalen;
175 	compat_caddr_t data;
176 };
177 
178 static int compat_blkpg_ioctl(struct block_device *bdev,
179 			      struct compat_blkpg_ioctl_arg __user *arg)
180 {
181 	compat_caddr_t udata;
182 	int op;
183 
184 	if (get_user(op, &arg->op) || get_user(udata, &arg->data))
185 		return -EFAULT;
186 
187 	return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
188 }
189 #endif
190 
191 static int blkdev_reread_part(struct block_device *bdev)
192 {
193 	int ret;
194 
195 	if (!disk_part_scan_enabled(bdev->bd_disk) || bdev != bdev->bd_contains)
196 		return -EINVAL;
197 	if (!capable(CAP_SYS_ADMIN))
198 		return -EACCES;
199 
200 	mutex_lock(&bdev->bd_mutex);
201 	ret = bdev_disk_changed(bdev, false);
202 	mutex_unlock(&bdev->bd_mutex);
203 
204 	return ret;
205 }
206 
207 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
208 		unsigned long arg, unsigned long flags)
209 {
210 	uint64_t range[2];
211 	uint64_t start, len;
212 	struct request_queue *q = bdev_get_queue(bdev);
213 	struct address_space *mapping = bdev->bd_inode->i_mapping;
214 
215 
216 	if (!(mode & FMODE_WRITE))
217 		return -EBADF;
218 
219 	if (!blk_queue_discard(q))
220 		return -EOPNOTSUPP;
221 
222 	if (copy_from_user(range, (void __user *)arg, sizeof(range)))
223 		return -EFAULT;
224 
225 	start = range[0];
226 	len = range[1];
227 
228 	if (start & 511)
229 		return -EINVAL;
230 	if (len & 511)
231 		return -EINVAL;
232 
233 	if (start + len > i_size_read(bdev->bd_inode))
234 		return -EINVAL;
235 	truncate_inode_pages_range(mapping, start, start + len - 1);
236 	return blkdev_issue_discard(bdev, start >> 9, len >> 9,
237 				    GFP_KERNEL, flags);
238 }
239 
240 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
241 		unsigned long arg)
242 {
243 	uint64_t range[2];
244 	struct address_space *mapping;
245 	uint64_t start, end, len;
246 
247 	if (!(mode & FMODE_WRITE))
248 		return -EBADF;
249 
250 	if (copy_from_user(range, (void __user *)arg, sizeof(range)))
251 		return -EFAULT;
252 
253 	start = range[0];
254 	len = range[1];
255 	end = start + len - 1;
256 
257 	if (start & 511)
258 		return -EINVAL;
259 	if (len & 511)
260 		return -EINVAL;
261 	if (end >= (uint64_t)i_size_read(bdev->bd_inode))
262 		return -EINVAL;
263 	if (end < start)
264 		return -EINVAL;
265 
266 	/* Invalidate the page cache, including dirty pages */
267 	mapping = bdev->bd_inode->i_mapping;
268 	truncate_inode_pages_range(mapping, start, end);
269 
270 	return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
271 			BLKDEV_ZERO_NOUNMAP);
272 }
273 
274 static int put_ushort(unsigned short __user *argp, unsigned short val)
275 {
276 	return put_user(val, argp);
277 }
278 
279 static int put_int(int __user *argp, int val)
280 {
281 	return put_user(val, argp);
282 }
283 
284 static int put_uint(unsigned int __user *argp, unsigned int val)
285 {
286 	return put_user(val, argp);
287 }
288 
289 static int put_long(long __user *argp, long val)
290 {
291 	return put_user(val, argp);
292 }
293 
294 static int put_ulong(unsigned long __user *argp, unsigned long val)
295 {
296 	return put_user(val, argp);
297 }
298 
299 static int put_u64(u64 __user *argp, u64 val)
300 {
301 	return put_user(val, argp);
302 }
303 
304 #ifdef CONFIG_COMPAT
305 static int compat_put_long(compat_long_t *argp, long val)
306 {
307 	return put_user(val, argp);
308 }
309 
310 static int compat_put_ulong(compat_ulong_t *argp, compat_ulong_t val)
311 {
312 	return put_user(val, argp);
313 }
314 #endif
315 
316 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
317 			unsigned cmd, unsigned long arg)
318 {
319 	struct gendisk *disk = bdev->bd_disk;
320 
321 	if (disk->fops->ioctl)
322 		return disk->fops->ioctl(bdev, mode, cmd, arg);
323 
324 	return -ENOTTY;
325 }
326 /*
327  * For the record: _GPL here is only because somebody decided to slap it
328  * on the previous export.  Sheer idiocy, since it wasn't copyrightable
329  * at all and could be open-coded without any exports by anybody who cares.
330  */
331 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
332 
333 #ifdef CONFIG_COMPAT
334 /*
335  * This is the equivalent of compat_ptr_ioctl(), to be used by block
336  * drivers that implement only commands that are completely compatible
337  * between 32-bit and 64-bit user space
338  */
339 int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
340 			unsigned cmd, unsigned long arg)
341 {
342 	struct gendisk *disk = bdev->bd_disk;
343 
344 	if (disk->fops->ioctl)
345 		return disk->fops->ioctl(bdev, mode, cmd,
346 					 (unsigned long)compat_ptr(arg));
347 
348 	return -ENOIOCTLCMD;
349 }
350 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
351 #endif
352 
353 static int blkdev_pr_register(struct block_device *bdev,
354 		struct pr_registration __user *arg)
355 {
356 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
357 	struct pr_registration reg;
358 
359 	if (!capable(CAP_SYS_ADMIN))
360 		return -EPERM;
361 	if (!ops || !ops->pr_register)
362 		return -EOPNOTSUPP;
363 	if (copy_from_user(&reg, arg, sizeof(reg)))
364 		return -EFAULT;
365 
366 	if (reg.flags & ~PR_FL_IGNORE_KEY)
367 		return -EOPNOTSUPP;
368 	return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
369 }
370 
371 static int blkdev_pr_reserve(struct block_device *bdev,
372 		struct pr_reservation __user *arg)
373 {
374 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
375 	struct pr_reservation rsv;
376 
377 	if (!capable(CAP_SYS_ADMIN))
378 		return -EPERM;
379 	if (!ops || !ops->pr_reserve)
380 		return -EOPNOTSUPP;
381 	if (copy_from_user(&rsv, arg, sizeof(rsv)))
382 		return -EFAULT;
383 
384 	if (rsv.flags & ~PR_FL_IGNORE_KEY)
385 		return -EOPNOTSUPP;
386 	return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
387 }
388 
389 static int blkdev_pr_release(struct block_device *bdev,
390 		struct pr_reservation __user *arg)
391 {
392 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
393 	struct pr_reservation rsv;
394 
395 	if (!capable(CAP_SYS_ADMIN))
396 		return -EPERM;
397 	if (!ops || !ops->pr_release)
398 		return -EOPNOTSUPP;
399 	if (copy_from_user(&rsv, arg, sizeof(rsv)))
400 		return -EFAULT;
401 
402 	if (rsv.flags)
403 		return -EOPNOTSUPP;
404 	return ops->pr_release(bdev, rsv.key, rsv.type);
405 }
406 
407 static int blkdev_pr_preempt(struct block_device *bdev,
408 		struct pr_preempt __user *arg, bool abort)
409 {
410 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
411 	struct pr_preempt p;
412 
413 	if (!capable(CAP_SYS_ADMIN))
414 		return -EPERM;
415 	if (!ops || !ops->pr_preempt)
416 		return -EOPNOTSUPP;
417 	if (copy_from_user(&p, arg, sizeof(p)))
418 		return -EFAULT;
419 
420 	if (p.flags)
421 		return -EOPNOTSUPP;
422 	return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
423 }
424 
425 static int blkdev_pr_clear(struct block_device *bdev,
426 		struct pr_clear __user *arg)
427 {
428 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
429 	struct pr_clear c;
430 
431 	if (!capable(CAP_SYS_ADMIN))
432 		return -EPERM;
433 	if (!ops || !ops->pr_clear)
434 		return -EOPNOTSUPP;
435 	if (copy_from_user(&c, arg, sizeof(c)))
436 		return -EFAULT;
437 
438 	if (c.flags)
439 		return -EOPNOTSUPP;
440 	return ops->pr_clear(bdev, c.key);
441 }
442 
443 /*
444  * Is it an unrecognized ioctl? The correct returns are either
445  * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
446  * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
447  * code before returning.
448  *
449  * Confused drivers sometimes return EINVAL, which is wrong. It
450  * means "I understood the ioctl command, but the parameters to
451  * it were wrong".
452  *
453  * We should aim to just fix the broken drivers, the EINVAL case
454  * should go away.
455  */
456 static inline int is_unrecognized_ioctl(int ret)
457 {
458 	return	ret == -EINVAL ||
459 		ret == -ENOTTY ||
460 		ret == -ENOIOCTLCMD;
461 }
462 
463 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
464 		unsigned cmd, unsigned long arg)
465 {
466 	int ret;
467 
468 	if (!capable(CAP_SYS_ADMIN))
469 		return -EACCES;
470 
471 	ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
472 	if (!is_unrecognized_ioctl(ret))
473 		return ret;
474 
475 	fsync_bdev(bdev);
476 	invalidate_bdev(bdev);
477 	return 0;
478 }
479 
480 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
481 		unsigned cmd, unsigned long arg)
482 {
483 	int ret, n;
484 
485 	if (!capable(CAP_SYS_ADMIN))
486 		return -EACCES;
487 
488 	ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
489 	if (!is_unrecognized_ioctl(ret))
490 		return ret;
491 	if (get_user(n, (int __user *)arg))
492 		return -EFAULT;
493 	set_device_ro(bdev, n);
494 	return 0;
495 }
496 
497 static int blkdev_getgeo(struct block_device *bdev,
498 		struct hd_geometry __user *argp)
499 {
500 	struct gendisk *disk = bdev->bd_disk;
501 	struct hd_geometry geo;
502 	int ret;
503 
504 	if (!argp)
505 		return -EINVAL;
506 	if (!disk->fops->getgeo)
507 		return -ENOTTY;
508 
509 	/*
510 	 * We need to set the startsect first, the driver may
511 	 * want to override it.
512 	 */
513 	memset(&geo, 0, sizeof(geo));
514 	geo.start = get_start_sect(bdev);
515 	ret = disk->fops->getgeo(bdev, &geo);
516 	if (ret)
517 		return ret;
518 	if (copy_to_user(argp, &geo, sizeof(geo)))
519 		return -EFAULT;
520 	return 0;
521 }
522 
523 #ifdef CONFIG_COMPAT
524 struct compat_hd_geometry {
525 	unsigned char heads;
526 	unsigned char sectors;
527 	unsigned short cylinders;
528 	u32 start;
529 };
530 
531 static int compat_hdio_getgeo(struct block_device *bdev,
532 			      struct compat_hd_geometry __user *ugeo)
533 {
534 	struct gendisk *disk = bdev->bd_disk;
535 	struct hd_geometry geo;
536 	int ret;
537 
538 	if (!ugeo)
539 		return -EINVAL;
540 	if (!disk->fops->getgeo)
541 		return -ENOTTY;
542 
543 	memset(&geo, 0, sizeof(geo));
544 	/*
545 	 * We need to set the startsect first, the driver may
546 	 * want to override it.
547 	 */
548 	geo.start = get_start_sect(bdev);
549 	ret = disk->fops->getgeo(bdev, &geo);
550 	if (ret)
551 		return ret;
552 
553 	ret = copy_to_user(ugeo, &geo, 4);
554 	ret |= put_user(geo.start, &ugeo->start);
555 	if (ret)
556 		ret = -EFAULT;
557 
558 	return ret;
559 }
560 #endif
561 
562 /* set the logical block size */
563 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
564 		int __user *argp)
565 {
566 	int ret, n;
567 
568 	if (!capable(CAP_SYS_ADMIN))
569 		return -EACCES;
570 	if (!argp)
571 		return -EINVAL;
572 	if (get_user(n, argp))
573 		return -EFAULT;
574 
575 	if (!(mode & FMODE_EXCL)) {
576 		bdgrab(bdev);
577 		if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
578 			return -EBUSY;
579 	}
580 
581 	ret = set_blocksize(bdev, n);
582 	if (!(mode & FMODE_EXCL))
583 		blkdev_put(bdev, mode | FMODE_EXCL);
584 	return ret;
585 }
586 
587 /*
588  * Common commands that are handled the same way on native and compat
589  * user space. Note the separate arg/argp parameters that are needed
590  * to deal with the compat_ptr() conversion.
591  */
592 static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
593 				unsigned cmd, unsigned long arg, void __user *argp)
594 {
595 	unsigned int max_sectors;
596 
597 	switch (cmd) {
598 	case BLKFLSBUF:
599 		return blkdev_flushbuf(bdev, mode, cmd, arg);
600 	case BLKROSET:
601 		return blkdev_roset(bdev, mode, cmd, arg);
602 	case BLKDISCARD:
603 		return blk_ioctl_discard(bdev, mode, arg, 0);
604 	case BLKSECDISCARD:
605 		return blk_ioctl_discard(bdev, mode, arg,
606 				BLKDEV_DISCARD_SECURE);
607 	case BLKZEROOUT:
608 		return blk_ioctl_zeroout(bdev, mode, arg);
609 	case BLKREPORTZONE:
610 		return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
611 	case BLKRESETZONE:
612 	case BLKOPENZONE:
613 	case BLKCLOSEZONE:
614 	case BLKFINISHZONE:
615 		return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
616 	case BLKGETZONESZ:
617 		return put_uint(argp, bdev_zone_sectors(bdev));
618 	case BLKGETNRZONES:
619 		return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
620 	case BLKROGET:
621 		return put_int(argp, bdev_read_only(bdev) != 0);
622 	case BLKSSZGET: /* get block device logical block size */
623 		return put_int(argp, bdev_logical_block_size(bdev));
624 	case BLKPBSZGET: /* get block device physical block size */
625 		return put_uint(argp, bdev_physical_block_size(bdev));
626 	case BLKIOMIN:
627 		return put_uint(argp, bdev_io_min(bdev));
628 	case BLKIOOPT:
629 		return put_uint(argp, bdev_io_opt(bdev));
630 	case BLKALIGNOFF:
631 		return put_int(argp, bdev_alignment_offset(bdev));
632 	case BLKDISCARDZEROES:
633 		return put_uint(argp, 0);
634 	case BLKSECTGET:
635 		max_sectors = min_t(unsigned int, USHRT_MAX,
636 				    queue_max_sectors(bdev_get_queue(bdev)));
637 		return put_ushort(argp, max_sectors);
638 	case BLKROTATIONAL:
639 		return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
640 	case BLKRASET:
641 	case BLKFRASET:
642 		if(!capable(CAP_SYS_ADMIN))
643 			return -EACCES;
644 		bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
645 		return 0;
646 	case BLKRRPART:
647 		return blkdev_reread_part(bdev);
648 	case BLKTRACESTART:
649 	case BLKTRACESTOP:
650 	case BLKTRACETEARDOWN:
651 		return blk_trace_ioctl(bdev, cmd, argp);
652 	case IOC_PR_REGISTER:
653 		return blkdev_pr_register(bdev, argp);
654 	case IOC_PR_RESERVE:
655 		return blkdev_pr_reserve(bdev, argp);
656 	case IOC_PR_RELEASE:
657 		return blkdev_pr_release(bdev, argp);
658 	case IOC_PR_PREEMPT:
659 		return blkdev_pr_preempt(bdev, argp, false);
660 	case IOC_PR_PREEMPT_ABORT:
661 		return blkdev_pr_preempt(bdev, argp, true);
662 	case IOC_PR_CLEAR:
663 		return blkdev_pr_clear(bdev, argp);
664 	default:
665 		return -ENOIOCTLCMD;
666 	}
667 }
668 
669 /*
670  * Always keep this in sync with compat_blkdev_ioctl()
671  * to handle all incompatible commands in both functions.
672  *
673  * New commands must be compatible and go into blkdev_common_ioctl
674  */
675 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
676 			unsigned long arg)
677 {
678 	int ret;
679 	loff_t size;
680 	void __user *argp = (void __user *)arg;
681 
682 	switch (cmd) {
683 	/* These need separate implementations for the data structure */
684 	case HDIO_GETGEO:
685 		return blkdev_getgeo(bdev, argp);
686 	case BLKPG:
687 		return blkpg_ioctl(bdev, argp);
688 
689 	/* Compat mode returns 32-bit data instead of 'long' */
690 	case BLKRAGET:
691 	case BLKFRAGET:
692 		if (!argp)
693 			return -EINVAL;
694 		return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
695 	case BLKGETSIZE:
696 		size = i_size_read(bdev->bd_inode);
697 		if ((size >> 9) > ~0UL)
698 			return -EFBIG;
699 		return put_ulong(argp, size >> 9);
700 
701 	/* The data is compatible, but the command number is different */
702 	case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
703 		return put_int(argp, block_size(bdev));
704 	case BLKBSZSET:
705 		return blkdev_bszset(bdev, mode, argp);
706 	case BLKGETSIZE64:
707 		return put_u64(argp, i_size_read(bdev->bd_inode));
708 
709 	/* Incompatible alignment on i386 */
710 	case BLKTRACESETUP:
711 		return blk_trace_ioctl(bdev, cmd, argp);
712 	default:
713 		break;
714 	}
715 
716 	ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
717 	if (ret == -ENOIOCTLCMD)
718 		return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
719 
720 	return ret;
721 }
722 EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
723 
724 #ifdef CONFIG_COMPAT
725 
726 #define BLKBSZGET_32		_IOR(0x12, 112, int)
727 #define BLKBSZSET_32		_IOW(0x12, 113, int)
728 #define BLKGETSIZE64_32		_IOR(0x12, 114, int)
729 
730 /* Most of the generic ioctls are handled in the normal fallback path.
731    This assumes the blkdev's low level compat_ioctl always returns
732    ENOIOCTLCMD for unknown ioctls. */
733 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
734 {
735 	int ret;
736 	void __user *argp = compat_ptr(arg);
737 	struct inode *inode = file->f_mapping->host;
738 	struct block_device *bdev = inode->i_bdev;
739 	struct gendisk *disk = bdev->bd_disk;
740 	fmode_t mode = file->f_mode;
741 	loff_t size;
742 
743 	/*
744 	 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
745 	 * to updated it before every ioctl.
746 	 */
747 	if (file->f_flags & O_NDELAY)
748 		mode |= FMODE_NDELAY;
749 	else
750 		mode &= ~FMODE_NDELAY;
751 
752 	switch (cmd) {
753 	/* These need separate implementations for the data structure */
754 	case HDIO_GETGEO:
755 		return compat_hdio_getgeo(bdev, argp);
756 	case BLKPG:
757 		return compat_blkpg_ioctl(bdev, argp);
758 
759 	/* Compat mode returns 32-bit data instead of 'long' */
760 	case BLKRAGET:
761 	case BLKFRAGET:
762 		if (!argp)
763 			return -EINVAL;
764 		return compat_put_long(argp,
765 			       (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
766 	case BLKGETSIZE:
767 		size = i_size_read(bdev->bd_inode);
768 		if ((size >> 9) > ~0UL)
769 			return -EFBIG;
770 		return compat_put_ulong(argp, size >> 9);
771 
772 	/* The data is compatible, but the command number is different */
773 	case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
774 		return put_int(argp, bdev_logical_block_size(bdev));
775 	case BLKBSZSET_32:
776 		return blkdev_bszset(bdev, mode, argp);
777 	case BLKGETSIZE64_32:
778 		return put_u64(argp, i_size_read(bdev->bd_inode));
779 
780 	/* Incompatible alignment on i386 */
781 	case BLKTRACESETUP32:
782 		return blk_trace_ioctl(bdev, cmd, argp);
783 	default:
784 		break;
785 	}
786 
787 	ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
788 	if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
789 		ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
790 
791 	return ret;
792 }
793 #endif
794