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