xref: /openbmc/linux/block/ioctl.c (revision ac481c20ef8f6c6f2be75d581863f40c43874ef7)
1c59ede7bSRandy.Dunlap #include <linux/capability.h>
23a65dfe8SJens Axboe #include <linux/blkdev.h>
33a65dfe8SJens Axboe #include <linux/blkpg.h>
4a885c8c4SChristoph Hellwig #include <linux/hdreg.h>
53a65dfe8SJens Axboe #include <linux/backing-dev.h>
63a65dfe8SJens Axboe #include <linux/buffer_head.h>
73a65dfe8SJens Axboe #include <linux/smp_lock.h>
82056a782SJens Axboe #include <linux/blktrace_api.h>
93a65dfe8SJens Axboe #include <asm/uaccess.h>
103a65dfe8SJens Axboe 
113a65dfe8SJens Axboe static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
123a65dfe8SJens Axboe {
133a65dfe8SJens Axboe 	struct block_device *bdevp;
143a65dfe8SJens Axboe 	struct gendisk *disk;
15e71bf0d0STejun Heo 	struct hd_struct *part;
163a65dfe8SJens Axboe 	struct blkpg_ioctl_arg a;
173a65dfe8SJens Axboe 	struct blkpg_partition p;
18e71bf0d0STejun Heo 	struct disk_part_iter piter;
193a65dfe8SJens Axboe 	long long start, length;
20cf771cb5STejun Heo 	int partno;
213a65dfe8SJens Axboe 
223a65dfe8SJens Axboe 	if (!capable(CAP_SYS_ADMIN))
233a65dfe8SJens Axboe 		return -EACCES;
243a65dfe8SJens Axboe 	if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
253a65dfe8SJens Axboe 		return -EFAULT;
263a65dfe8SJens Axboe 	if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
273a65dfe8SJens Axboe 		return -EFAULT;
283a65dfe8SJens Axboe 	disk = bdev->bd_disk;
293a65dfe8SJens Axboe 	if (bdev != bdev->bd_contains)
303a65dfe8SJens Axboe 		return -EINVAL;
31cf771cb5STejun Heo 	partno = p.pno;
32540eed56STejun Heo 	if (partno <= 0)
333a65dfe8SJens Axboe 		return -EINVAL;
343a65dfe8SJens Axboe 	switch (a.op) {
353a65dfe8SJens Axboe 		case BLKPG_ADD_PARTITION:
363a65dfe8SJens Axboe 			start = p.start >> 9;
373a65dfe8SJens Axboe 			length = p.length >> 9;
383a65dfe8SJens Axboe 			/* check for fit in a hd_struct */
393a65dfe8SJens Axboe 			if (sizeof(sector_t) == sizeof(long) &&
403a65dfe8SJens Axboe 			    sizeof(long long) > sizeof(long)) {
413a65dfe8SJens Axboe 				long pstart = start, plength = length;
423a65dfe8SJens Axboe 				if (pstart != start || plength != length
433a65dfe8SJens Axboe 				    || pstart < 0 || plength < 0)
443a65dfe8SJens Axboe 					return -EINVAL;
453a65dfe8SJens Axboe 			}
4688e34126STejun Heo 
47c039e313SArjan van de Ven 			mutex_lock(&bdev->bd_mutex);
4888e34126STejun Heo 
493a65dfe8SJens Axboe 			/* overlap? */
50e71bf0d0STejun Heo 			disk_part_iter_init(&piter, disk,
51e71bf0d0STejun Heo 					    DISK_PITER_INCL_EMPTY);
52e71bf0d0STejun Heo 			while ((part = disk_part_iter_next(&piter))) {
53e71bf0d0STejun Heo 				if (!(start + length <= part->start_sect ||
54e71bf0d0STejun Heo 				      start >= part->start_sect + part->nr_sects)) {
55e71bf0d0STejun Heo 					disk_part_iter_exit(&piter);
56c039e313SArjan van de Ven 					mutex_unlock(&bdev->bd_mutex);
573a65dfe8SJens Axboe 					return -EBUSY;
583a65dfe8SJens Axboe 				}
593a65dfe8SJens Axboe 			}
60e71bf0d0STejun Heo 			disk_part_iter_exit(&piter);
61e71bf0d0STejun Heo 
623a65dfe8SJens Axboe 			/* all seems OK */
63ba32929aSTejun Heo 			part = add_partition(disk, partno, start, length,
64cf771cb5STejun Heo 					     ADDPART_FLAG_NONE);
65c039e313SArjan van de Ven 			mutex_unlock(&bdev->bd_mutex);
66ba32929aSTejun Heo 			return IS_ERR(part) ? PTR_ERR(part) : 0;
673a65dfe8SJens Axboe 		case BLKPG_DEL_PARTITION:
68e71bf0d0STejun Heo 			part = disk_get_part(disk, partno);
69e71bf0d0STejun Heo 			if (!part)
703a65dfe8SJens Axboe 				return -ENXIO;
71e71bf0d0STejun Heo 
72e71bf0d0STejun Heo 			bdevp = bdget(part_devt(part));
73e71bf0d0STejun Heo 			disk_put_part(part);
743a65dfe8SJens Axboe 			if (!bdevp)
753a65dfe8SJens Axboe 				return -ENOMEM;
76e71bf0d0STejun Heo 
772e7b651dSPeter Zijlstra 			mutex_lock(&bdevp->bd_mutex);
783a65dfe8SJens Axboe 			if (bdevp->bd_openers) {
79c039e313SArjan van de Ven 				mutex_unlock(&bdevp->bd_mutex);
803a65dfe8SJens Axboe 				bdput(bdevp);
813a65dfe8SJens Axboe 				return -EBUSY;
823a65dfe8SJens Axboe 			}
833a65dfe8SJens Axboe 			/* all seems OK */
843a65dfe8SJens Axboe 			fsync_bdev(bdevp);
85f98393a6SPeter Zijlstra 			invalidate_bdev(bdevp);
863a65dfe8SJens Axboe 
876d740cd5SPeter Zijlstra 			mutex_lock_nested(&bdev->bd_mutex, 1);
88cf771cb5STejun Heo 			delete_partition(disk, partno);
89c039e313SArjan van de Ven 			mutex_unlock(&bdev->bd_mutex);
90c039e313SArjan van de Ven 			mutex_unlock(&bdevp->bd_mutex);
913a65dfe8SJens Axboe 			bdput(bdevp);
923a65dfe8SJens Axboe 
933a65dfe8SJens Axboe 			return 0;
943a65dfe8SJens Axboe 		default:
953a65dfe8SJens Axboe 			return -EINVAL;
963a65dfe8SJens Axboe 	}
973a65dfe8SJens Axboe }
983a65dfe8SJens Axboe 
993a65dfe8SJens Axboe static int blkdev_reread_part(struct block_device *bdev)
1003a65dfe8SJens Axboe {
1013a65dfe8SJens Axboe 	struct gendisk *disk = bdev->bd_disk;
1023a65dfe8SJens Axboe 	int res;
1033a65dfe8SJens Axboe 
104b5d0b9dfSTejun Heo 	if (!disk_partitionable(disk) || bdev != bdev->bd_contains)
1053a65dfe8SJens Axboe 		return -EINVAL;
1063a65dfe8SJens Axboe 	if (!capable(CAP_SYS_ADMIN))
1073a65dfe8SJens Axboe 		return -EACCES;
108c039e313SArjan van de Ven 	if (!mutex_trylock(&bdev->bd_mutex))
1093a65dfe8SJens Axboe 		return -EBUSY;
1103a65dfe8SJens Axboe 	res = rescan_partitions(disk, bdev);
111c039e313SArjan van de Ven 	mutex_unlock(&bdev->bd_mutex);
1123a65dfe8SJens Axboe 	return res;
1133a65dfe8SJens Axboe }
1143a65dfe8SJens Axboe 
115d30a2605SDavid Woodhouse static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
116d30a2605SDavid Woodhouse 			     uint64_t len)
117d30a2605SDavid Woodhouse {
118d30a2605SDavid Woodhouse 	if (start & 511)
119d30a2605SDavid Woodhouse 		return -EINVAL;
120d30a2605SDavid Woodhouse 	if (len & 511)
121d30a2605SDavid Woodhouse 		return -EINVAL;
122d30a2605SDavid Woodhouse 	start >>= 9;
123d30a2605SDavid Woodhouse 	len >>= 9;
124d30a2605SDavid Woodhouse 
125d30a2605SDavid Woodhouse 	if (start + len > (bdev->bd_inode->i_size >> 9))
126d30a2605SDavid Woodhouse 		return -EINVAL;
127746cd1e7SChristoph Hellwig 	return blkdev_issue_discard(bdev, start, len, GFP_KERNEL,
128746cd1e7SChristoph Hellwig 				    DISCARD_FL_WAIT);
129d30a2605SDavid Woodhouse }
130d30a2605SDavid Woodhouse 
1313a65dfe8SJens Axboe static int put_ushort(unsigned long arg, unsigned short val)
1323a65dfe8SJens Axboe {
1333a65dfe8SJens Axboe 	return put_user(val, (unsigned short __user *)arg);
1343a65dfe8SJens Axboe }
1353a65dfe8SJens Axboe 
1363a65dfe8SJens Axboe static int put_int(unsigned long arg, int val)
1373a65dfe8SJens Axboe {
1383a65dfe8SJens Axboe 	return put_user(val, (int __user *)arg);
1393a65dfe8SJens Axboe }
1403a65dfe8SJens Axboe 
141*ac481c20SMartin K. Petersen static int put_uint(unsigned long arg, unsigned int val)
142*ac481c20SMartin K. Petersen {
143*ac481c20SMartin K. Petersen 	return put_user(val, (unsigned int __user *)arg);
144*ac481c20SMartin K. Petersen }
145*ac481c20SMartin K. Petersen 
1463a65dfe8SJens Axboe static int put_long(unsigned long arg, long val)
1473a65dfe8SJens Axboe {
1483a65dfe8SJens Axboe 	return put_user(val, (long __user *)arg);
1493a65dfe8SJens Axboe }
1503a65dfe8SJens Axboe 
1513a65dfe8SJens Axboe static int put_ulong(unsigned long arg, unsigned long val)
1523a65dfe8SJens Axboe {
1533a65dfe8SJens Axboe 	return put_user(val, (unsigned long __user *)arg);
1543a65dfe8SJens Axboe }
1553a65dfe8SJens Axboe 
1563a65dfe8SJens Axboe static int put_u64(unsigned long arg, u64 val)
1573a65dfe8SJens Axboe {
1583a65dfe8SJens Axboe 	return put_user(val, (u64 __user *)arg);
1593a65dfe8SJens Axboe }
1603a65dfe8SJens Axboe 
161633a08b8SAl Viro int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
162633a08b8SAl Viro 			unsigned cmd, unsigned long arg)
163633a08b8SAl Viro {
164633a08b8SAl Viro 	struct gendisk *disk = bdev->bd_disk;
165633a08b8SAl Viro 	int ret;
166d4430d62SAl Viro 
167d4430d62SAl Viro 	if (disk->fops->ioctl)
168d4430d62SAl Viro 		return disk->fops->ioctl(bdev, mode, cmd, arg);
169d4430d62SAl Viro 
170d4430d62SAl Viro 	if (disk->fops->locked_ioctl) {
171d4430d62SAl Viro 		lock_kernel();
172d4430d62SAl Viro 		ret = disk->fops->locked_ioctl(bdev, mode, cmd, arg);
173633a08b8SAl Viro 		unlock_kernel();
174633a08b8SAl Viro 		return ret;
175633a08b8SAl Viro 	}
176633a08b8SAl Viro 
177633a08b8SAl Viro 	return -ENOTTY;
178633a08b8SAl Viro }
179633a08b8SAl Viro /*
180633a08b8SAl Viro  * For the record: _GPL here is only because somebody decided to slap it
181633a08b8SAl Viro  * on the previous export.  Sheer idiocy, since it wasn't copyrightable
182633a08b8SAl Viro  * at all and could be open-coded without any exports by anybody who cares.
183633a08b8SAl Viro  */
184633a08b8SAl Viro EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
185633a08b8SAl Viro 
186f58c4c0aSArnd Bergmann /*
187f58c4c0aSArnd Bergmann  * always keep this in sync with compat_blkdev_ioctl() and
188f58c4c0aSArnd Bergmann  * compat_blkdev_locked_ioctl()
189f58c4c0aSArnd Bergmann  */
19056b26addSAl Viro int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
1913a65dfe8SJens Axboe 			unsigned long arg)
1923a65dfe8SJens Axboe {
1933a65dfe8SJens Axboe 	struct gendisk *disk = bdev->bd_disk;
19445048d09SAl Viro 	struct backing_dev_info *bdi;
19545048d09SAl Viro 	loff_t size;
1963a65dfe8SJens Axboe 	int ret, n;
1973a65dfe8SJens Axboe 
1983a65dfe8SJens Axboe 	switch(cmd) {
1993a65dfe8SJens Axboe 	case BLKFLSBUF:
2003a65dfe8SJens Axboe 		if (!capable(CAP_SYS_ADMIN))
2013a65dfe8SJens Axboe 			return -EACCES;
2023a65dfe8SJens Axboe 
203e436fdaeSAl Viro 		ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
2043a65dfe8SJens Axboe 		/* -EINVAL to handle old uncorrected drivers */
2053a65dfe8SJens Axboe 		if (ret != -EINVAL && ret != -ENOTTY)
2063a65dfe8SJens Axboe 			return ret;
2073a65dfe8SJens Axboe 
2083a65dfe8SJens Axboe 		lock_kernel();
2093a65dfe8SJens Axboe 		fsync_bdev(bdev);
210f98393a6SPeter Zijlstra 		invalidate_bdev(bdev);
2113a65dfe8SJens Axboe 		unlock_kernel();
2123a65dfe8SJens Axboe 		return 0;
2133a65dfe8SJens Axboe 
2143a65dfe8SJens Axboe 	case BLKROSET:
215e436fdaeSAl Viro 		ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
2163a65dfe8SJens Axboe 		/* -EINVAL to handle old uncorrected drivers */
2173a65dfe8SJens Axboe 		if (ret != -EINVAL && ret != -ENOTTY)
2183a65dfe8SJens Axboe 			return ret;
2193a65dfe8SJens Axboe 		if (!capable(CAP_SYS_ADMIN))
2203a65dfe8SJens Axboe 			return -EACCES;
2213a65dfe8SJens Axboe 		if (get_user(n, (int __user *)(arg)))
2223a65dfe8SJens Axboe 			return -EFAULT;
2233a65dfe8SJens Axboe 		lock_kernel();
2243a65dfe8SJens Axboe 		set_device_ro(bdev, n);
2253a65dfe8SJens Axboe 		unlock_kernel();
2263a65dfe8SJens Axboe 		return 0;
227d30a2605SDavid Woodhouse 
228d30a2605SDavid Woodhouse 	case BLKDISCARD: {
229d30a2605SDavid Woodhouse 		uint64_t range[2];
230d30a2605SDavid Woodhouse 
231e436fdaeSAl Viro 		if (!(mode & FMODE_WRITE))
232d30a2605SDavid Woodhouse 			return -EBADF;
233d30a2605SDavid Woodhouse 
234d30a2605SDavid Woodhouse 		if (copy_from_user(range, (void __user *)arg, sizeof(range)))
235d30a2605SDavid Woodhouse 			return -EFAULT;
236d30a2605SDavid Woodhouse 
237d30a2605SDavid Woodhouse 		return blk_ioctl_discard(bdev, range[0], range[1]);
238d30a2605SDavid Woodhouse 	}
239d30a2605SDavid Woodhouse 
240a885c8c4SChristoph Hellwig 	case HDIO_GETGEO: {
241a885c8c4SChristoph Hellwig 		struct hd_geometry geo;
242a885c8c4SChristoph Hellwig 
243a885c8c4SChristoph Hellwig 		if (!arg)
244a885c8c4SChristoph Hellwig 			return -EINVAL;
245a885c8c4SChristoph Hellwig 		if (!disk->fops->getgeo)
246a885c8c4SChristoph Hellwig 			return -ENOTTY;
247a885c8c4SChristoph Hellwig 
248a885c8c4SChristoph Hellwig 		/*
249a885c8c4SChristoph Hellwig 		 * We need to set the startsect first, the driver may
250a885c8c4SChristoph Hellwig 		 * want to override it.
251a885c8c4SChristoph Hellwig 		 */
252a885c8c4SChristoph Hellwig 		geo.start = get_start_sect(bdev);
253a885c8c4SChristoph Hellwig 		ret = disk->fops->getgeo(bdev, &geo);
254a885c8c4SChristoph Hellwig 		if (ret)
255a885c8c4SChristoph Hellwig 			return ret;
256a885c8c4SChristoph Hellwig 		if (copy_to_user((struct hd_geometry __user *)arg, &geo,
257a885c8c4SChristoph Hellwig 					sizeof(geo)))
258a885c8c4SChristoph Hellwig 			return -EFAULT;
259a885c8c4SChristoph Hellwig 		return 0;
260a885c8c4SChristoph Hellwig 	}
26145048d09SAl Viro 	case BLKRAGET:
26245048d09SAl Viro 	case BLKFRAGET:
26345048d09SAl Viro 		if (!arg)
26445048d09SAl Viro 			return -EINVAL;
26545048d09SAl Viro 		bdi = blk_get_backing_dev_info(bdev);
26645048d09SAl Viro 		if (bdi == NULL)
26745048d09SAl Viro 			return -ENOTTY;
26845048d09SAl Viro 		return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
26945048d09SAl Viro 	case BLKROGET:
27045048d09SAl Viro 		return put_int(arg, bdev_read_only(bdev) != 0);
271*ac481c20SMartin K. Petersen 	case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
27245048d09SAl Viro 		return put_int(arg, block_size(bdev));
273*ac481c20SMartin K. Petersen 	case BLKSSZGET: /* get block device logical block size */
274e1defc4fSMartin K. Petersen 		return put_int(arg, bdev_logical_block_size(bdev));
275*ac481c20SMartin K. Petersen 	case BLKPBSZGET: /* get block device physical block size */
276*ac481c20SMartin K. Petersen 		return put_uint(arg, bdev_physical_block_size(bdev));
277*ac481c20SMartin K. Petersen 	case BLKIOMIN:
278*ac481c20SMartin K. Petersen 		return put_uint(arg, bdev_io_min(bdev));
279*ac481c20SMartin K. Petersen 	case BLKIOOPT:
280*ac481c20SMartin K. Petersen 		return put_uint(arg, bdev_io_opt(bdev));
281*ac481c20SMartin K. Petersen 	case BLKALIGNOFF:
282*ac481c20SMartin K. Petersen 		return put_int(arg, bdev_alignment_offset(bdev));
28345048d09SAl Viro 	case BLKSECTGET:
284ae03bf63SMartin K. Petersen 		return put_ushort(arg, queue_max_sectors(bdev_get_queue(bdev)));
28545048d09SAl Viro 	case BLKRASET:
28645048d09SAl Viro 	case BLKFRASET:
28745048d09SAl Viro 		if(!capable(CAP_SYS_ADMIN))
28845048d09SAl Viro 			return -EACCES;
28945048d09SAl Viro 		bdi = blk_get_backing_dev_info(bdev);
29045048d09SAl Viro 		if (bdi == NULL)
29145048d09SAl Viro 			return -ENOTTY;
29245048d09SAl Viro 		bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
29345048d09SAl Viro 		return 0;
29445048d09SAl Viro 	case BLKBSZSET:
29545048d09SAl Viro 		/* set the logical block size */
29645048d09SAl Viro 		if (!capable(CAP_SYS_ADMIN))
29745048d09SAl Viro 			return -EACCES;
29845048d09SAl Viro 		if (!arg)
29945048d09SAl Viro 			return -EINVAL;
30045048d09SAl Viro 		if (get_user(n, (int __user *) arg))
30145048d09SAl Viro 			return -EFAULT;
3026af3a56eSAl Viro 		if (!(mode & FMODE_EXCL) && bd_claim(bdev, &bdev) < 0)
30345048d09SAl Viro 			return -EBUSY;
30445048d09SAl Viro 		ret = set_blocksize(bdev, n);
3056af3a56eSAl Viro 		if (!(mode & FMODE_EXCL))
30645048d09SAl Viro 			bd_release(bdev);
3073a65dfe8SJens Axboe 		return ret;
30845048d09SAl Viro 	case BLKPG:
30945048d09SAl Viro 		lock_kernel();
31045048d09SAl Viro 		ret = blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
31145048d09SAl Viro 		unlock_kernel();
31245048d09SAl Viro 		break;
31345048d09SAl Viro 	case BLKRRPART:
31445048d09SAl Viro 		lock_kernel();
31545048d09SAl Viro 		ret = blkdev_reread_part(bdev);
31645048d09SAl Viro 		unlock_kernel();
31745048d09SAl Viro 		break;
31845048d09SAl Viro 	case BLKGETSIZE:
31945048d09SAl Viro 		size = bdev->bd_inode->i_size;
32045048d09SAl Viro 		if ((size >> 9) > ~0UL)
32145048d09SAl Viro 			return -EFBIG;
32245048d09SAl Viro 		return put_ulong(arg, size >> 9);
32345048d09SAl Viro 	case BLKGETSIZE64:
32445048d09SAl Viro 		return put_u64(arg, bdev->bd_inode->i_size);
32545048d09SAl Viro 	case BLKTRACESTART:
32645048d09SAl Viro 	case BLKTRACESTOP:
32745048d09SAl Viro 	case BLKTRACESETUP:
32845048d09SAl Viro 	case BLKTRACETEARDOWN:
32945048d09SAl Viro 		lock_kernel();
33045048d09SAl Viro 		ret = blk_trace_ioctl(bdev, cmd, (char __user *) arg);
33145048d09SAl Viro 		unlock_kernel();
33245048d09SAl Viro 		break;
33345048d09SAl Viro 	default:
334e436fdaeSAl Viro 		ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
3353a65dfe8SJens Axboe 	}
33645048d09SAl Viro 	return ret;
33745048d09SAl Viro }
3383a65dfe8SJens Axboe EXPORT_SYMBOL_GPL(blkdev_ioctl);
339