xref: /openbmc/linux/block/ioctl.c (revision e2f1cf25)
1 #include <linux/capability.h>
2 #include <linux/blkdev.h>
3 #include <linux/export.h>
4 #include <linux/gfp.h>
5 #include <linux/blkpg.h>
6 #include <linux/hdreg.h>
7 #include <linux/backing-dev.h>
8 #include <linux/fs.h>
9 #include <linux/blktrace_api.h>
10 #include <asm/uaccess.h>
11 
12 static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
13 {
14 	struct block_device *bdevp;
15 	struct gendisk *disk;
16 	struct hd_struct *part, *lpart;
17 	struct blkpg_ioctl_arg a;
18 	struct blkpg_partition p;
19 	struct disk_part_iter piter;
20 	long long start, length;
21 	int partno;
22 
23 	if (!capable(CAP_SYS_ADMIN))
24 		return -EACCES;
25 	if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
26 		return -EFAULT;
27 	if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
28 		return -EFAULT;
29 	disk = bdev->bd_disk;
30 	if (bdev != bdev->bd_contains)
31 		return -EINVAL;
32 	partno = p.pno;
33 	if (partno <= 0)
34 		return -EINVAL;
35 	switch (a.op) {
36 		case BLKPG_ADD_PARTITION:
37 			start = p.start >> 9;
38 			length = p.length >> 9;
39 			/* check for fit in a hd_struct */
40 			if (sizeof(sector_t) == sizeof(long) &&
41 			    sizeof(long long) > sizeof(long)) {
42 				long pstart = start, plength = length;
43 				if (pstart != start || plength != length
44 				    || pstart < 0 || plength < 0 || partno > 65535)
45 					return -EINVAL;
46 			}
47 
48 			mutex_lock(&bdev->bd_mutex);
49 
50 			/* overlap? */
51 			disk_part_iter_init(&piter, disk,
52 					    DISK_PITER_INCL_EMPTY);
53 			while ((part = disk_part_iter_next(&piter))) {
54 				if (!(start + length <= part->start_sect ||
55 				      start >= part->start_sect + part->nr_sects)) {
56 					disk_part_iter_exit(&piter);
57 					mutex_unlock(&bdev->bd_mutex);
58 					return -EBUSY;
59 				}
60 			}
61 			disk_part_iter_exit(&piter);
62 
63 			/* all seems OK */
64 			part = add_partition(disk, partno, start, length,
65 					     ADDPART_FLAG_NONE, NULL);
66 			mutex_unlock(&bdev->bd_mutex);
67 			return PTR_ERR_OR_ZERO(part);
68 		case BLKPG_DEL_PARTITION:
69 			part = disk_get_part(disk, partno);
70 			if (!part)
71 				return -ENXIO;
72 
73 			bdevp = bdget(part_devt(part));
74 			disk_put_part(part);
75 			if (!bdevp)
76 				return -ENOMEM;
77 
78 			mutex_lock(&bdevp->bd_mutex);
79 			if (bdevp->bd_openers) {
80 				mutex_unlock(&bdevp->bd_mutex);
81 				bdput(bdevp);
82 				return -EBUSY;
83 			}
84 			/* all seems OK */
85 			fsync_bdev(bdevp);
86 			invalidate_bdev(bdevp);
87 
88 			mutex_lock_nested(&bdev->bd_mutex, 1);
89 			delete_partition(disk, partno);
90 			mutex_unlock(&bdev->bd_mutex);
91 			mutex_unlock(&bdevp->bd_mutex);
92 			bdput(bdevp);
93 
94 			return 0;
95 		case BLKPG_RESIZE_PARTITION:
96 			start = p.start >> 9;
97 			/* new length of partition in bytes */
98 			length = p.length >> 9;
99 			/* check for fit in a hd_struct */
100 			if (sizeof(sector_t) == sizeof(long) &&
101 			    sizeof(long long) > sizeof(long)) {
102 				long pstart = start, plength = length;
103 				if (pstart != start || plength != length
104 				    || pstart < 0 || plength < 0)
105 					return -EINVAL;
106 			}
107 			part = disk_get_part(disk, partno);
108 			if (!part)
109 				return -ENXIO;
110 			bdevp = bdget(part_devt(part));
111 			if (!bdevp) {
112 				disk_put_part(part);
113 				return -ENOMEM;
114 			}
115 			mutex_lock(&bdevp->bd_mutex);
116 			mutex_lock_nested(&bdev->bd_mutex, 1);
117 			if (start != part->start_sect) {
118 				mutex_unlock(&bdevp->bd_mutex);
119 				mutex_unlock(&bdev->bd_mutex);
120 				bdput(bdevp);
121 				disk_put_part(part);
122 				return -EINVAL;
123 			}
124 			/* overlap? */
125 			disk_part_iter_init(&piter, disk,
126 					    DISK_PITER_INCL_EMPTY);
127 			while ((lpart = disk_part_iter_next(&piter))) {
128 				if (lpart->partno != partno &&
129 				   !(start + length <= lpart->start_sect ||
130 				   start >= lpart->start_sect + lpart->nr_sects)
131 				   ) {
132 					disk_part_iter_exit(&piter);
133 					mutex_unlock(&bdevp->bd_mutex);
134 					mutex_unlock(&bdev->bd_mutex);
135 					bdput(bdevp);
136 					disk_put_part(part);
137 					return -EBUSY;
138 				}
139 			}
140 			disk_part_iter_exit(&piter);
141 			part_nr_sects_write(part, (sector_t)length);
142 			i_size_write(bdevp->bd_inode, p.length);
143 			mutex_unlock(&bdevp->bd_mutex);
144 			mutex_unlock(&bdev->bd_mutex);
145 			bdput(bdevp);
146 			disk_put_part(part);
147 			return 0;
148 		default:
149 			return -EINVAL;
150 	}
151 }
152 
153 /*
154  * This is an exported API for the block driver, and will not
155  * acquire bd_mutex. This API should be used in case that
156  * caller has held bd_mutex already.
157  */
158 int __blkdev_reread_part(struct block_device *bdev)
159 {
160 	struct gendisk *disk = bdev->bd_disk;
161 
162 	if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
163 		return -EINVAL;
164 	if (!capable(CAP_SYS_ADMIN))
165 		return -EACCES;
166 
167 	lockdep_assert_held(&bdev->bd_mutex);
168 
169 	return rescan_partitions(disk, bdev);
170 }
171 EXPORT_SYMBOL(__blkdev_reread_part);
172 
173 /*
174  * This is an exported API for the block driver, and will
175  * try to acquire bd_mutex. If bd_mutex has been held already
176  * in current context, please call __blkdev_reread_part().
177  *
178  * Make sure the held locks in current context aren't required
179  * in open()/close() handler and I/O path for avoiding ABBA deadlock:
180  * - bd_mutex is held before calling block driver's open/close
181  *   handler
182  * - reading partition table may submit I/O to the block device
183  */
184 int blkdev_reread_part(struct block_device *bdev)
185 {
186 	int res;
187 
188 	mutex_lock(&bdev->bd_mutex);
189 	res = __blkdev_reread_part(bdev);
190 	mutex_unlock(&bdev->bd_mutex);
191 
192 	return res;
193 }
194 EXPORT_SYMBOL(blkdev_reread_part);
195 
196 static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
197 			     uint64_t len, int secure)
198 {
199 	unsigned long flags = 0;
200 
201 	if (start & 511)
202 		return -EINVAL;
203 	if (len & 511)
204 		return -EINVAL;
205 	start >>= 9;
206 	len >>= 9;
207 
208 	if (start + len > (i_size_read(bdev->bd_inode) >> 9))
209 		return -EINVAL;
210 	if (secure)
211 		flags |= BLKDEV_DISCARD_SECURE;
212 	return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
213 }
214 
215 static int blk_ioctl_zeroout(struct block_device *bdev, uint64_t start,
216 			     uint64_t len)
217 {
218 	if (start & 511)
219 		return -EINVAL;
220 	if (len & 511)
221 		return -EINVAL;
222 	start >>= 9;
223 	len >>= 9;
224 
225 	if (start + len > (i_size_read(bdev->bd_inode) >> 9))
226 		return -EINVAL;
227 
228 	return blkdev_issue_zeroout(bdev, start, len, GFP_KERNEL, false);
229 }
230 
231 static int put_ushort(unsigned long arg, unsigned short val)
232 {
233 	return put_user(val, (unsigned short __user *)arg);
234 }
235 
236 static int put_int(unsigned long arg, int val)
237 {
238 	return put_user(val, (int __user *)arg);
239 }
240 
241 static int put_uint(unsigned long arg, unsigned int val)
242 {
243 	return put_user(val, (unsigned int __user *)arg);
244 }
245 
246 static int put_long(unsigned long arg, long val)
247 {
248 	return put_user(val, (long __user *)arg);
249 }
250 
251 static int put_ulong(unsigned long arg, unsigned long val)
252 {
253 	return put_user(val, (unsigned long __user *)arg);
254 }
255 
256 static int put_u64(unsigned long arg, u64 val)
257 {
258 	return put_user(val, (u64 __user *)arg);
259 }
260 
261 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
262 			unsigned cmd, unsigned long arg)
263 {
264 	struct gendisk *disk = bdev->bd_disk;
265 
266 	if (disk->fops->ioctl)
267 		return disk->fops->ioctl(bdev, mode, cmd, arg);
268 
269 	return -ENOTTY;
270 }
271 /*
272  * For the record: _GPL here is only because somebody decided to slap it
273  * on the previous export.  Sheer idiocy, since it wasn't copyrightable
274  * at all and could be open-coded without any exports by anybody who cares.
275  */
276 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
277 
278 /*
279  * Is it an unrecognized ioctl? The correct returns are either
280  * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
281  * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
282  * code before returning.
283  *
284  * Confused drivers sometimes return EINVAL, which is wrong. It
285  * means "I understood the ioctl command, but the parameters to
286  * it were wrong".
287  *
288  * We should aim to just fix the broken drivers, the EINVAL case
289  * should go away.
290  */
291 static inline int is_unrecognized_ioctl(int ret)
292 {
293 	return	ret == -EINVAL ||
294 		ret == -ENOTTY ||
295 		ret == -ENOIOCTLCMD;
296 }
297 
298 /*
299  * always keep this in sync with compat_blkdev_ioctl()
300  */
301 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
302 			unsigned long arg)
303 {
304 	struct gendisk *disk = bdev->bd_disk;
305 	struct backing_dev_info *bdi;
306 	loff_t size;
307 	int ret, n;
308 	unsigned int max_sectors;
309 
310 	switch(cmd) {
311 	case BLKFLSBUF:
312 		if (!capable(CAP_SYS_ADMIN))
313 			return -EACCES;
314 
315 		ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
316 		if (!is_unrecognized_ioctl(ret))
317 			return ret;
318 
319 		fsync_bdev(bdev);
320 		invalidate_bdev(bdev);
321 		return 0;
322 
323 	case BLKROSET:
324 		ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
325 		if (!is_unrecognized_ioctl(ret))
326 			return ret;
327 		if (!capable(CAP_SYS_ADMIN))
328 			return -EACCES;
329 		if (get_user(n, (int __user *)(arg)))
330 			return -EFAULT;
331 		set_device_ro(bdev, n);
332 		return 0;
333 
334 	case BLKDISCARD:
335 	case BLKSECDISCARD: {
336 		uint64_t range[2];
337 
338 		if (!(mode & FMODE_WRITE))
339 			return -EBADF;
340 
341 		if (copy_from_user(range, (void __user *)arg, sizeof(range)))
342 			return -EFAULT;
343 
344 		return blk_ioctl_discard(bdev, range[0], range[1],
345 					 cmd == BLKSECDISCARD);
346 	}
347 	case BLKZEROOUT: {
348 		uint64_t range[2];
349 
350 		if (!(mode & FMODE_WRITE))
351 			return -EBADF;
352 
353 		if (copy_from_user(range, (void __user *)arg, sizeof(range)))
354 			return -EFAULT;
355 
356 		return blk_ioctl_zeroout(bdev, range[0], range[1]);
357 	}
358 
359 	case HDIO_GETGEO: {
360 		struct hd_geometry geo;
361 
362 		if (!arg)
363 			return -EINVAL;
364 		if (!disk->fops->getgeo)
365 			return -ENOTTY;
366 
367 		/*
368 		 * We need to set the startsect first, the driver may
369 		 * want to override it.
370 		 */
371 		memset(&geo, 0, sizeof(geo));
372 		geo.start = get_start_sect(bdev);
373 		ret = disk->fops->getgeo(bdev, &geo);
374 		if (ret)
375 			return ret;
376 		if (copy_to_user((struct hd_geometry __user *)arg, &geo,
377 					sizeof(geo)))
378 			return -EFAULT;
379 		return 0;
380 	}
381 	case BLKRAGET:
382 	case BLKFRAGET:
383 		if (!arg)
384 			return -EINVAL;
385 		bdi = blk_get_backing_dev_info(bdev);
386 		return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
387 	case BLKROGET:
388 		return put_int(arg, bdev_read_only(bdev) != 0);
389 	case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
390 		return put_int(arg, block_size(bdev));
391 	case BLKSSZGET: /* get block device logical block size */
392 		return put_int(arg, bdev_logical_block_size(bdev));
393 	case BLKPBSZGET: /* get block device physical block size */
394 		return put_uint(arg, bdev_physical_block_size(bdev));
395 	case BLKIOMIN:
396 		return put_uint(arg, bdev_io_min(bdev));
397 	case BLKIOOPT:
398 		return put_uint(arg, bdev_io_opt(bdev));
399 	case BLKALIGNOFF:
400 		return put_int(arg, bdev_alignment_offset(bdev));
401 	case BLKDISCARDZEROES:
402 		return put_uint(arg, bdev_discard_zeroes_data(bdev));
403 	case BLKSECTGET:
404 		max_sectors = min_t(unsigned int, USHRT_MAX,
405 				    queue_max_sectors(bdev_get_queue(bdev)));
406 		return put_ushort(arg, max_sectors);
407 	case BLKROTATIONAL:
408 		return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
409 	case BLKRASET:
410 	case BLKFRASET:
411 		if(!capable(CAP_SYS_ADMIN))
412 			return -EACCES;
413 		bdi = blk_get_backing_dev_info(bdev);
414 		bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
415 		return 0;
416 	case BLKBSZSET:
417 		/* set the logical block size */
418 		if (!capable(CAP_SYS_ADMIN))
419 			return -EACCES;
420 		if (!arg)
421 			return -EINVAL;
422 		if (get_user(n, (int __user *) arg))
423 			return -EFAULT;
424 		if (!(mode & FMODE_EXCL)) {
425 			bdgrab(bdev);
426 			if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
427 				return -EBUSY;
428 		}
429 		ret = set_blocksize(bdev, n);
430 		if (!(mode & FMODE_EXCL))
431 			blkdev_put(bdev, mode | FMODE_EXCL);
432 		return ret;
433 	case BLKPG:
434 		ret = blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
435 		break;
436 	case BLKRRPART:
437 		ret = blkdev_reread_part(bdev);
438 		break;
439 	case BLKGETSIZE:
440 		size = i_size_read(bdev->bd_inode);
441 		if ((size >> 9) > ~0UL)
442 			return -EFBIG;
443 		return put_ulong(arg, size >> 9);
444 	case BLKGETSIZE64:
445 		return put_u64(arg, i_size_read(bdev->bd_inode));
446 	case BLKTRACESTART:
447 	case BLKTRACESTOP:
448 	case BLKTRACESETUP:
449 	case BLKTRACETEARDOWN:
450 		ret = blk_trace_ioctl(bdev, cmd, (char __user *) arg);
451 		break;
452 	default:
453 		ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
454 	}
455 	return ret;
456 }
457 EXPORT_SYMBOL_GPL(blkdev_ioctl);
458