xref: /openbmc/linux/block/ioctl.c (revision c83f6bf98dc1f1a194118b3830706cebbebda8c4)
1c59ede7bSRandy.Dunlap #include <linux/capability.h>
23a65dfe8SJens Axboe #include <linux/blkdev.h>
3d5decd3bSPaul Gortmaker #include <linux/export.h>
45a0e3ad6STejun Heo #include <linux/gfp.h>
53a65dfe8SJens Axboe #include <linux/blkpg.h>
6a885c8c4SChristoph Hellwig #include <linux/hdreg.h>
73a65dfe8SJens Axboe #include <linux/backing-dev.h>
8ff01bb48SAl Viro #include <linux/fs.h>
92056a782SJens Axboe #include <linux/blktrace_api.h>
103a65dfe8SJens Axboe #include <asm/uaccess.h>
113a65dfe8SJens Axboe 
123a65dfe8SJens Axboe static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
133a65dfe8SJens Axboe {
143a65dfe8SJens Axboe 	struct block_device *bdevp;
153a65dfe8SJens Axboe 	struct gendisk *disk;
16*c83f6bf9SVivek Goyal 	struct hd_struct *part, *lpart;
173a65dfe8SJens Axboe 	struct blkpg_ioctl_arg a;
183a65dfe8SJens Axboe 	struct blkpg_partition p;
19e71bf0d0STejun Heo 	struct disk_part_iter piter;
203a65dfe8SJens Axboe 	long long start, length;
21cf771cb5STejun Heo 	int partno;
223a65dfe8SJens Axboe 
233a65dfe8SJens Axboe 	if (!capable(CAP_SYS_ADMIN))
243a65dfe8SJens Axboe 		return -EACCES;
253a65dfe8SJens Axboe 	if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
263a65dfe8SJens Axboe 		return -EFAULT;
273a65dfe8SJens Axboe 	if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
283a65dfe8SJens Axboe 		return -EFAULT;
293a65dfe8SJens Axboe 	disk = bdev->bd_disk;
303a65dfe8SJens Axboe 	if (bdev != bdev->bd_contains)
313a65dfe8SJens Axboe 		return -EINVAL;
32cf771cb5STejun Heo 	partno = p.pno;
33540eed56STejun Heo 	if (partno <= 0)
343a65dfe8SJens Axboe 		return -EINVAL;
353a65dfe8SJens Axboe 	switch (a.op) {
363a65dfe8SJens Axboe 		case BLKPG_ADD_PARTITION:
373a65dfe8SJens Axboe 			start = p.start >> 9;
383a65dfe8SJens Axboe 			length = p.length >> 9;
393a65dfe8SJens Axboe 			/* check for fit in a hd_struct */
403a65dfe8SJens Axboe 			if (sizeof(sector_t) == sizeof(long) &&
413a65dfe8SJens Axboe 			    sizeof(long long) > sizeof(long)) {
423a65dfe8SJens Axboe 				long pstart = start, plength = length;
433a65dfe8SJens Axboe 				if (pstart != start || plength != length
443a65dfe8SJens Axboe 				    || pstart < 0 || plength < 0)
453a65dfe8SJens Axboe 					return -EINVAL;
463a65dfe8SJens Axboe 			}
4788e34126STejun Heo 
48c039e313SArjan van de Ven 			mutex_lock(&bdev->bd_mutex);
4988e34126STejun Heo 
503a65dfe8SJens Axboe 			/* overlap? */
51e71bf0d0STejun Heo 			disk_part_iter_init(&piter, disk,
52e71bf0d0STejun Heo 					    DISK_PITER_INCL_EMPTY);
53e71bf0d0STejun Heo 			while ((part = disk_part_iter_next(&piter))) {
54e71bf0d0STejun Heo 				if (!(start + length <= part->start_sect ||
55e71bf0d0STejun Heo 				      start >= part->start_sect + part->nr_sects)) {
56e71bf0d0STejun Heo 					disk_part_iter_exit(&piter);
57c039e313SArjan van de Ven 					mutex_unlock(&bdev->bd_mutex);
583a65dfe8SJens Axboe 					return -EBUSY;
593a65dfe8SJens Axboe 				}
603a65dfe8SJens Axboe 			}
61e71bf0d0STejun Heo 			disk_part_iter_exit(&piter);
62e71bf0d0STejun Heo 
633a65dfe8SJens Axboe 			/* all seems OK */
64ba32929aSTejun Heo 			part = add_partition(disk, partno, start, length,
656d1d8050SWill Drewry 					     ADDPART_FLAG_NONE, NULL);
66c039e313SArjan van de Ven 			mutex_unlock(&bdev->bd_mutex);
67ba32929aSTejun Heo 			return IS_ERR(part) ? PTR_ERR(part) : 0;
683a65dfe8SJens Axboe 		case BLKPG_DEL_PARTITION:
69e71bf0d0STejun Heo 			part = disk_get_part(disk, partno);
70e71bf0d0STejun Heo 			if (!part)
713a65dfe8SJens Axboe 				return -ENXIO;
72e71bf0d0STejun Heo 
73e71bf0d0STejun Heo 			bdevp = bdget(part_devt(part));
74e71bf0d0STejun Heo 			disk_put_part(part);
753a65dfe8SJens Axboe 			if (!bdevp)
763a65dfe8SJens Axboe 				return -ENOMEM;
77e71bf0d0STejun Heo 
782e7b651dSPeter Zijlstra 			mutex_lock(&bdevp->bd_mutex);
793a65dfe8SJens Axboe 			if (bdevp->bd_openers) {
80c039e313SArjan van de Ven 				mutex_unlock(&bdevp->bd_mutex);
813a65dfe8SJens Axboe 				bdput(bdevp);
823a65dfe8SJens Axboe 				return -EBUSY;
833a65dfe8SJens Axboe 			}
843a65dfe8SJens Axboe 			/* all seems OK */
853a65dfe8SJens Axboe 			fsync_bdev(bdevp);
86f98393a6SPeter Zijlstra 			invalidate_bdev(bdevp);
873a65dfe8SJens Axboe 
886d740cd5SPeter Zijlstra 			mutex_lock_nested(&bdev->bd_mutex, 1);
89cf771cb5STejun Heo 			delete_partition(disk, partno);
90c039e313SArjan van de Ven 			mutex_unlock(&bdev->bd_mutex);
91c039e313SArjan van de Ven 			mutex_unlock(&bdevp->bd_mutex);
923a65dfe8SJens Axboe 			bdput(bdevp);
933a65dfe8SJens Axboe 
943a65dfe8SJens Axboe 			return 0;
95*c83f6bf9SVivek Goyal 		case BLKPG_RESIZE_PARTITION:
96*c83f6bf9SVivek Goyal 			start = p.start >> 9;
97*c83f6bf9SVivek Goyal 			/* new length of partition in bytes */
98*c83f6bf9SVivek Goyal 			length = p.length >> 9;
99*c83f6bf9SVivek Goyal 			/* check for fit in a hd_struct */
100*c83f6bf9SVivek Goyal 			if (sizeof(sector_t) == sizeof(long) &&
101*c83f6bf9SVivek Goyal 			    sizeof(long long) > sizeof(long)) {
102*c83f6bf9SVivek Goyal 				long pstart = start, plength = length;
103*c83f6bf9SVivek Goyal 				if (pstart != start || plength != length
104*c83f6bf9SVivek Goyal 				    || pstart < 0 || plength < 0)
105*c83f6bf9SVivek Goyal 					return -EINVAL;
106*c83f6bf9SVivek Goyal 			}
107*c83f6bf9SVivek Goyal 			part = disk_get_part(disk, partno);
108*c83f6bf9SVivek Goyal 			if (!part)
109*c83f6bf9SVivek Goyal 				return -ENXIO;
110*c83f6bf9SVivek Goyal 			bdevp = bdget(part_devt(part));
111*c83f6bf9SVivek Goyal 			if (!bdevp) {
112*c83f6bf9SVivek Goyal 				disk_put_part(part);
113*c83f6bf9SVivek Goyal 				return -ENOMEM;
114*c83f6bf9SVivek Goyal 			}
115*c83f6bf9SVivek Goyal 			mutex_lock(&bdevp->bd_mutex);
116*c83f6bf9SVivek Goyal 			mutex_lock_nested(&bdev->bd_mutex, 1);
117*c83f6bf9SVivek Goyal 			if (start != part->start_sect) {
118*c83f6bf9SVivek Goyal 				mutex_unlock(&bdevp->bd_mutex);
119*c83f6bf9SVivek Goyal 				mutex_unlock(&bdev->bd_mutex);
120*c83f6bf9SVivek Goyal 				bdput(bdevp);
121*c83f6bf9SVivek Goyal 				disk_put_part(part);
122*c83f6bf9SVivek Goyal 				return -EINVAL;
123*c83f6bf9SVivek Goyal 			}
124*c83f6bf9SVivek Goyal 			/* overlap? */
125*c83f6bf9SVivek Goyal 			disk_part_iter_init(&piter, disk,
126*c83f6bf9SVivek Goyal 					    DISK_PITER_INCL_EMPTY);
127*c83f6bf9SVivek Goyal 			while ((lpart = disk_part_iter_next(&piter))) {
128*c83f6bf9SVivek Goyal 				if (lpart->partno != partno &&
129*c83f6bf9SVivek Goyal 				   !(start + length <= lpart->start_sect ||
130*c83f6bf9SVivek Goyal 				   start >= lpart->start_sect + lpart->nr_sects)
131*c83f6bf9SVivek Goyal 				   ) {
132*c83f6bf9SVivek Goyal 					disk_part_iter_exit(&piter);
133*c83f6bf9SVivek Goyal 					mutex_unlock(&bdevp->bd_mutex);
134*c83f6bf9SVivek Goyal 					mutex_unlock(&bdev->bd_mutex);
135*c83f6bf9SVivek Goyal 					bdput(bdevp);
136*c83f6bf9SVivek Goyal 					disk_put_part(part);
137*c83f6bf9SVivek Goyal 					return -EBUSY;
138*c83f6bf9SVivek Goyal 				}
139*c83f6bf9SVivek Goyal 			}
140*c83f6bf9SVivek Goyal 			disk_part_iter_exit(&piter);
141*c83f6bf9SVivek Goyal 			part_nr_sects_write(part, (sector_t)length);
142*c83f6bf9SVivek Goyal 			i_size_write(bdevp->bd_inode, p.length);
143*c83f6bf9SVivek Goyal 			mutex_unlock(&bdevp->bd_mutex);
144*c83f6bf9SVivek Goyal 			mutex_unlock(&bdev->bd_mutex);
145*c83f6bf9SVivek Goyal 			bdput(bdevp);
146*c83f6bf9SVivek Goyal 			disk_put_part(part);
147*c83f6bf9SVivek Goyal 			return 0;
1483a65dfe8SJens Axboe 		default:
1493a65dfe8SJens Axboe 			return -EINVAL;
1503a65dfe8SJens Axboe 	}
1513a65dfe8SJens Axboe }
1523a65dfe8SJens Axboe 
1533a65dfe8SJens Axboe static int blkdev_reread_part(struct block_device *bdev)
1543a65dfe8SJens Axboe {
1553a65dfe8SJens Axboe 	struct gendisk *disk = bdev->bd_disk;
1563a65dfe8SJens Axboe 	int res;
1573a65dfe8SJens Axboe 
158d27769ecSTejun Heo 	if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
1593a65dfe8SJens Axboe 		return -EINVAL;
1603a65dfe8SJens Axboe 	if (!capable(CAP_SYS_ADMIN))
1613a65dfe8SJens Axboe 		return -EACCES;
162c039e313SArjan van de Ven 	if (!mutex_trylock(&bdev->bd_mutex))
1633a65dfe8SJens Axboe 		return -EBUSY;
1643a65dfe8SJens Axboe 	res = rescan_partitions(disk, bdev);
165c039e313SArjan van de Ven 	mutex_unlock(&bdev->bd_mutex);
1663a65dfe8SJens Axboe 	return res;
1673a65dfe8SJens Axboe }
1683a65dfe8SJens Axboe 
169d30a2605SDavid Woodhouse static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
1708d57a98cSAdrian Hunter 			     uint64_t len, int secure)
171d30a2605SDavid Woodhouse {
172dd3932edSChristoph Hellwig 	unsigned long flags = 0;
1738d57a98cSAdrian Hunter 
174d30a2605SDavid Woodhouse 	if (start & 511)
175d30a2605SDavid Woodhouse 		return -EINVAL;
176d30a2605SDavid Woodhouse 	if (len & 511)
177d30a2605SDavid Woodhouse 		return -EINVAL;
178d30a2605SDavid Woodhouse 	start >>= 9;
179d30a2605SDavid Woodhouse 	len >>= 9;
180d30a2605SDavid Woodhouse 
18177304d2aSMike Snitzer 	if (start + len > (i_size_read(bdev->bd_inode) >> 9))
182d30a2605SDavid Woodhouse 		return -EINVAL;
1838d57a98cSAdrian Hunter 	if (secure)
184dd3932edSChristoph Hellwig 		flags |= BLKDEV_DISCARD_SECURE;
1858d57a98cSAdrian Hunter 	return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
186d30a2605SDavid Woodhouse }
187d30a2605SDavid Woodhouse 
1883a65dfe8SJens Axboe static int put_ushort(unsigned long arg, unsigned short val)
1893a65dfe8SJens Axboe {
1903a65dfe8SJens Axboe 	return put_user(val, (unsigned short __user *)arg);
1913a65dfe8SJens Axboe }
1923a65dfe8SJens Axboe 
1933a65dfe8SJens Axboe static int put_int(unsigned long arg, int val)
1943a65dfe8SJens Axboe {
1953a65dfe8SJens Axboe 	return put_user(val, (int __user *)arg);
1963a65dfe8SJens Axboe }
1973a65dfe8SJens Axboe 
198ac481c20SMartin K. Petersen static int put_uint(unsigned long arg, unsigned int val)
199ac481c20SMartin K. Petersen {
200ac481c20SMartin K. Petersen 	return put_user(val, (unsigned int __user *)arg);
201ac481c20SMartin K. Petersen }
202ac481c20SMartin K. Petersen 
2033a65dfe8SJens Axboe static int put_long(unsigned long arg, long val)
2043a65dfe8SJens Axboe {
2053a65dfe8SJens Axboe 	return put_user(val, (long __user *)arg);
2063a65dfe8SJens Axboe }
2073a65dfe8SJens Axboe 
2083a65dfe8SJens Axboe static int put_ulong(unsigned long arg, unsigned long val)
2093a65dfe8SJens Axboe {
2103a65dfe8SJens Axboe 	return put_user(val, (unsigned long __user *)arg);
2113a65dfe8SJens Axboe }
2123a65dfe8SJens Axboe 
2133a65dfe8SJens Axboe static int put_u64(unsigned long arg, u64 val)
2143a65dfe8SJens Axboe {
2153a65dfe8SJens Axboe 	return put_user(val, (u64 __user *)arg);
2163a65dfe8SJens Axboe }
2173a65dfe8SJens Axboe 
218633a08b8SAl Viro int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
219633a08b8SAl Viro 			unsigned cmd, unsigned long arg)
220633a08b8SAl Viro {
221633a08b8SAl Viro 	struct gendisk *disk = bdev->bd_disk;
222d4430d62SAl Viro 
223d4430d62SAl Viro 	if (disk->fops->ioctl)
224d4430d62SAl Viro 		return disk->fops->ioctl(bdev, mode, cmd, arg);
225d4430d62SAl Viro 
226633a08b8SAl Viro 	return -ENOTTY;
227633a08b8SAl Viro }
228633a08b8SAl Viro /*
229633a08b8SAl Viro  * For the record: _GPL here is only because somebody decided to slap it
230633a08b8SAl Viro  * on the previous export.  Sheer idiocy, since it wasn't copyrightable
231633a08b8SAl Viro  * at all and could be open-coded without any exports by anybody who cares.
232633a08b8SAl Viro  */
233633a08b8SAl Viro EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
234633a08b8SAl Viro 
235f58c4c0aSArnd Bergmann /*
23607d106d0SLinus Torvalds  * Is it an unrecognized ioctl? The correct returns are either
23707d106d0SLinus Torvalds  * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
23807d106d0SLinus Torvalds  * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
23907d106d0SLinus Torvalds  * code before returning.
24007d106d0SLinus Torvalds  *
24107d106d0SLinus Torvalds  * Confused drivers sometimes return EINVAL, which is wrong. It
24207d106d0SLinus Torvalds  * means "I understood the ioctl command, but the parameters to
24307d106d0SLinus Torvalds  * it were wrong".
24407d106d0SLinus Torvalds  *
24507d106d0SLinus Torvalds  * We should aim to just fix the broken drivers, the EINVAL case
24607d106d0SLinus Torvalds  * should go away.
24707d106d0SLinus Torvalds  */
24807d106d0SLinus Torvalds static inline int is_unrecognized_ioctl(int ret)
24907d106d0SLinus Torvalds {
25007d106d0SLinus Torvalds 	return	ret == -EINVAL ||
25107d106d0SLinus Torvalds 		ret == -ENOTTY ||
25207d106d0SLinus Torvalds 		ret == -ENOIOCTLCMD;
25307d106d0SLinus Torvalds }
25407d106d0SLinus Torvalds 
25507d106d0SLinus Torvalds /*
2568a6cfeb6SArnd Bergmann  * always keep this in sync with compat_blkdev_ioctl()
257f58c4c0aSArnd Bergmann  */
25856b26addSAl Viro int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
2593a65dfe8SJens Axboe 			unsigned long arg)
2603a65dfe8SJens Axboe {
2613a65dfe8SJens Axboe 	struct gendisk *disk = bdev->bd_disk;
26245048d09SAl Viro 	struct backing_dev_info *bdi;
26345048d09SAl Viro 	loff_t size;
2643a65dfe8SJens Axboe 	int ret, n;
2653a65dfe8SJens Axboe 
2663a65dfe8SJens Axboe 	switch(cmd) {
2673a65dfe8SJens Axboe 	case BLKFLSBUF:
2683a65dfe8SJens Axboe 		if (!capable(CAP_SYS_ADMIN))
2693a65dfe8SJens Axboe 			return -EACCES;
2703a65dfe8SJens Axboe 
271e436fdaeSAl Viro 		ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
27207d106d0SLinus Torvalds 		if (!is_unrecognized_ioctl(ret))
2733a65dfe8SJens Axboe 			return ret;
2743a65dfe8SJens Axboe 
2753a65dfe8SJens Axboe 		fsync_bdev(bdev);
276f98393a6SPeter Zijlstra 		invalidate_bdev(bdev);
2773a65dfe8SJens Axboe 		return 0;
2783a65dfe8SJens Axboe 
2793a65dfe8SJens Axboe 	case BLKROSET:
280e436fdaeSAl Viro 		ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
28107d106d0SLinus Torvalds 		if (!is_unrecognized_ioctl(ret))
2823a65dfe8SJens Axboe 			return ret;
2833a65dfe8SJens Axboe 		if (!capable(CAP_SYS_ADMIN))
2843a65dfe8SJens Axboe 			return -EACCES;
2853a65dfe8SJens Axboe 		if (get_user(n, (int __user *)(arg)))
2863a65dfe8SJens Axboe 			return -EFAULT;
2873a65dfe8SJens Axboe 		set_device_ro(bdev, n);
2883a65dfe8SJens Axboe 		return 0;
289d30a2605SDavid Woodhouse 
2908d57a98cSAdrian Hunter 	case BLKDISCARD:
2918d57a98cSAdrian Hunter 	case BLKSECDISCARD: {
292d30a2605SDavid Woodhouse 		uint64_t range[2];
293d30a2605SDavid Woodhouse 
294e436fdaeSAl Viro 		if (!(mode & FMODE_WRITE))
295d30a2605SDavid Woodhouse 			return -EBADF;
296d30a2605SDavid Woodhouse 
297d30a2605SDavid Woodhouse 		if (copy_from_user(range, (void __user *)arg, sizeof(range)))
298d30a2605SDavid Woodhouse 			return -EFAULT;
299d30a2605SDavid Woodhouse 
3008d57a98cSAdrian Hunter 		return blk_ioctl_discard(bdev, range[0], range[1],
3018d57a98cSAdrian Hunter 					 cmd == BLKSECDISCARD);
302d30a2605SDavid Woodhouse 	}
303d30a2605SDavid Woodhouse 
304a885c8c4SChristoph Hellwig 	case HDIO_GETGEO: {
305a885c8c4SChristoph Hellwig 		struct hd_geometry geo;
306a885c8c4SChristoph Hellwig 
307a885c8c4SChristoph Hellwig 		if (!arg)
308a885c8c4SChristoph Hellwig 			return -EINVAL;
309a885c8c4SChristoph Hellwig 		if (!disk->fops->getgeo)
310a885c8c4SChristoph Hellwig 			return -ENOTTY;
311a885c8c4SChristoph Hellwig 
312a885c8c4SChristoph Hellwig 		/*
313a885c8c4SChristoph Hellwig 		 * We need to set the startsect first, the driver may
314a885c8c4SChristoph Hellwig 		 * want to override it.
315a885c8c4SChristoph Hellwig 		 */
316a014741cSVasiliy Kulikov 		memset(&geo, 0, sizeof(geo));
317a885c8c4SChristoph Hellwig 		geo.start = get_start_sect(bdev);
318a885c8c4SChristoph Hellwig 		ret = disk->fops->getgeo(bdev, &geo);
319a885c8c4SChristoph Hellwig 		if (ret)
320a885c8c4SChristoph Hellwig 			return ret;
321a885c8c4SChristoph Hellwig 		if (copy_to_user((struct hd_geometry __user *)arg, &geo,
322a885c8c4SChristoph Hellwig 					sizeof(geo)))
323a885c8c4SChristoph Hellwig 			return -EFAULT;
324a885c8c4SChristoph Hellwig 		return 0;
325a885c8c4SChristoph Hellwig 	}
32645048d09SAl Viro 	case BLKRAGET:
32745048d09SAl Viro 	case BLKFRAGET:
32845048d09SAl Viro 		if (!arg)
32945048d09SAl Viro 			return -EINVAL;
33045048d09SAl Viro 		bdi = blk_get_backing_dev_info(bdev);
33145048d09SAl Viro 		if (bdi == NULL)
33245048d09SAl Viro 			return -ENOTTY;
33345048d09SAl Viro 		return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
33445048d09SAl Viro 	case BLKROGET:
33545048d09SAl Viro 		return put_int(arg, bdev_read_only(bdev) != 0);
336ac481c20SMartin K. Petersen 	case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
33745048d09SAl Viro 		return put_int(arg, block_size(bdev));
338ac481c20SMartin K. Petersen 	case BLKSSZGET: /* get block device logical block size */
339e1defc4fSMartin K. Petersen 		return put_int(arg, bdev_logical_block_size(bdev));
340ac481c20SMartin K. Petersen 	case BLKPBSZGET: /* get block device physical block size */
341ac481c20SMartin K. Petersen 		return put_uint(arg, bdev_physical_block_size(bdev));
342ac481c20SMartin K. Petersen 	case BLKIOMIN:
343ac481c20SMartin K. Petersen 		return put_uint(arg, bdev_io_min(bdev));
344ac481c20SMartin K. Petersen 	case BLKIOOPT:
345ac481c20SMartin K. Petersen 		return put_uint(arg, bdev_io_opt(bdev));
346ac481c20SMartin K. Petersen 	case BLKALIGNOFF:
347ac481c20SMartin K. Petersen 		return put_int(arg, bdev_alignment_offset(bdev));
34898262f27SMartin K. Petersen 	case BLKDISCARDZEROES:
34998262f27SMartin K. Petersen 		return put_uint(arg, bdev_discard_zeroes_data(bdev));
35045048d09SAl Viro 	case BLKSECTGET:
351ae03bf63SMartin K. Petersen 		return put_ushort(arg, queue_max_sectors(bdev_get_queue(bdev)));
352ef00f59cSMartin K. Petersen 	case BLKROTATIONAL:
353ef00f59cSMartin K. Petersen 		return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
35445048d09SAl Viro 	case BLKRASET:
35545048d09SAl Viro 	case BLKFRASET:
35645048d09SAl Viro 		if(!capable(CAP_SYS_ADMIN))
35745048d09SAl Viro 			return -EACCES;
35845048d09SAl Viro 		bdi = blk_get_backing_dev_info(bdev);
35945048d09SAl Viro 		if (bdi == NULL)
36045048d09SAl Viro 			return -ENOTTY;
36145048d09SAl Viro 		bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
36245048d09SAl Viro 		return 0;
36345048d09SAl Viro 	case BLKBSZSET:
36445048d09SAl Viro 		/* set the logical block size */
36545048d09SAl Viro 		if (!capable(CAP_SYS_ADMIN))
36645048d09SAl Viro 			return -EACCES;
36745048d09SAl Viro 		if (!arg)
36845048d09SAl Viro 			return -EINVAL;
36945048d09SAl Viro 		if (get_user(n, (int __user *) arg))
37045048d09SAl Viro 			return -EFAULT;
3713c522cedSMiklos Szeredi 		if (!(mode & FMODE_EXCL)) {
3723c522cedSMiklos Szeredi 			bdgrab(bdev);
3733c522cedSMiklos Szeredi 			if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
37445048d09SAl Viro 				return -EBUSY;
3753c522cedSMiklos Szeredi 		}
37645048d09SAl Viro 		ret = set_blocksize(bdev, n);
3776af3a56eSAl Viro 		if (!(mode & FMODE_EXCL))
378e525fd89STejun Heo 			blkdev_put(bdev, mode | FMODE_EXCL);
3793a65dfe8SJens Axboe 		return ret;
38045048d09SAl Viro 	case BLKPG:
38145048d09SAl Viro 		ret = blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
38245048d09SAl Viro 		break;
38345048d09SAl Viro 	case BLKRRPART:
38445048d09SAl Viro 		ret = blkdev_reread_part(bdev);
38545048d09SAl Viro 		break;
38645048d09SAl Viro 	case BLKGETSIZE:
38777304d2aSMike Snitzer 		size = i_size_read(bdev->bd_inode);
38845048d09SAl Viro 		if ((size >> 9) > ~0UL)
38945048d09SAl Viro 			return -EFBIG;
39045048d09SAl Viro 		return put_ulong(arg, size >> 9);
39145048d09SAl Viro 	case BLKGETSIZE64:
39277304d2aSMike Snitzer 		return put_u64(arg, i_size_read(bdev->bd_inode));
39345048d09SAl Viro 	case BLKTRACESTART:
39445048d09SAl Viro 	case BLKTRACESTOP:
39545048d09SAl Viro 	case BLKTRACESETUP:
39645048d09SAl Viro 	case BLKTRACETEARDOWN:
39745048d09SAl Viro 		ret = blk_trace_ioctl(bdev, cmd, (char __user *) arg);
39845048d09SAl Viro 		break;
39945048d09SAl Viro 	default:
400e436fdaeSAl Viro 		ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
4013a65dfe8SJens Axboe 	}
40245048d09SAl Viro 	return ret;
40345048d09SAl Viro }
4043a65dfe8SJens Axboe EXPORT_SYMBOL_GPL(blkdev_ioctl);
405