]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - block/ioctl.c
Merge tag 'md/4.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shli/md
[mirror_ubuntu-zesty-kernel.git] / block / ioctl.c
CommitLineData
c59ede7b 1#include <linux/capability.h>
1da177e4 2#include <linux/blkdev.h>
d5decd3b 3#include <linux/export.h>
5a0e3ad6 4#include <linux/gfp.h>
1da177e4 5#include <linux/blkpg.h>
a885c8c4 6#include <linux/hdreg.h>
1da177e4 7#include <linux/backing-dev.h>
ff01bb48 8#include <linux/fs.h>
2056a782 9#include <linux/blktrace_api.h>
bbd3e064 10#include <linux/pr.h>
1da177e4
LT
11#include <asm/uaccess.h>
12
13static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
14{
15 struct block_device *bdevp;
16 struct gendisk *disk;
c83f6bf9 17 struct hd_struct *part, *lpart;
1da177e4
LT
18 struct blkpg_ioctl_arg a;
19 struct blkpg_partition p;
e71bf0d0 20 struct disk_part_iter piter;
1da177e4 21 long long start, length;
cf771cb5 22 int partno;
1da177e4
LT
23
24 if (!capable(CAP_SYS_ADMIN))
25 return -EACCES;
26 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
27 return -EFAULT;
28 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
29 return -EFAULT;
30 disk = bdev->bd_disk;
31 if (bdev != bdev->bd_contains)
32 return -EINVAL;
cf771cb5 33 partno = p.pno;
540eed56 34 if (partno <= 0)
1da177e4
LT
35 return -EINVAL;
36 switch (a.op) {
37 case BLKPG_ADD_PARTITION:
38 start = p.start >> 9;
39 length = p.length >> 9;
c83f6bf9
VG
40 /* check for fit in a hd_struct */
41 if (sizeof(sector_t) == sizeof(long) &&
1da177e4
LT
42 sizeof(long long) > sizeof(long)) {
43 long pstart = start, plength = length;
44 if (pstart != start || plength != length
2bd6efad 45 || pstart < 0 || plength < 0 || partno > 65535)
1da177e4
LT
46 return -EINVAL;
47 }
88e34126 48
c039e313 49 mutex_lock(&bdev->bd_mutex);
88e34126 50
1da177e4 51 /* overlap? */
e71bf0d0
TH
52 disk_part_iter_init(&piter, disk,
53 DISK_PITER_INCL_EMPTY);
54 while ((part = disk_part_iter_next(&piter))) {
55 if (!(start + length <= part->start_sect ||
56 start >= part->start_sect + part->nr_sects)) {
57 disk_part_iter_exit(&piter);
c039e313 58 mutex_unlock(&bdev->bd_mutex);
1da177e4
LT
59 return -EBUSY;
60 }
61 }
e71bf0d0
TH
62 disk_part_iter_exit(&piter);
63
1da177e4 64 /* all seems OK */
ba32929a 65 part = add_partition(disk, partno, start, length,
6d1d8050 66 ADDPART_FLAG_NONE, NULL);
c039e313 67 mutex_unlock(&bdev->bd_mutex);
c7d1ba41 68 return PTR_ERR_OR_ZERO(part);
1da177e4 69 case BLKPG_DEL_PARTITION:
e71bf0d0
TH
70 part = disk_get_part(disk, partno);
71 if (!part)
1da177e4 72 return -ENXIO;
e71bf0d0
TH
73
74 bdevp = bdget(part_devt(part));
75 disk_put_part(part);
1da177e4
LT
76 if (!bdevp)
77 return -ENOMEM;
e71bf0d0 78
2e7b651d 79 mutex_lock(&bdevp->bd_mutex);
1da177e4 80 if (bdevp->bd_openers) {
c039e313 81 mutex_unlock(&bdevp->bd_mutex);
1da177e4
LT
82 bdput(bdevp);
83 return -EBUSY;
84 }
85 /* all seems OK */
86 fsync_bdev(bdevp);
f98393a6 87 invalidate_bdev(bdevp);
1da177e4 88
6d740cd5 89 mutex_lock_nested(&bdev->bd_mutex, 1);
cf771cb5 90 delete_partition(disk, partno);
c039e313
AV
91 mutex_unlock(&bdev->bd_mutex);
92 mutex_unlock(&bdevp->bd_mutex);
1da177e4
LT
93 bdput(bdevp);
94
c83f6bf9
VG
95 return 0;
96 case BLKPG_RESIZE_PARTITION:
97 start = p.start >> 9;
98 /* new length of partition in bytes */
99 length = p.length >> 9;
100 /* check for fit in a hd_struct */
101 if (sizeof(sector_t) == sizeof(long) &&
102 sizeof(long long) > sizeof(long)) {
103 long pstart = start, plength = length;
104 if (pstart != start || plength != length
105 || pstart < 0 || plength < 0)
106 return -EINVAL;
107 }
108 part = disk_get_part(disk, partno);
109 if (!part)
110 return -ENXIO;
111 bdevp = bdget(part_devt(part));
112 if (!bdevp) {
113 disk_put_part(part);
114 return -ENOMEM;
115 }
116 mutex_lock(&bdevp->bd_mutex);
117 mutex_lock_nested(&bdev->bd_mutex, 1);
118 if (start != part->start_sect) {
119 mutex_unlock(&bdevp->bd_mutex);
120 mutex_unlock(&bdev->bd_mutex);
121 bdput(bdevp);
122 disk_put_part(part);
123 return -EINVAL;
124 }
125 /* overlap? */
126 disk_part_iter_init(&piter, disk,
127 DISK_PITER_INCL_EMPTY);
128 while ((lpart = disk_part_iter_next(&piter))) {
129 if (lpart->partno != partno &&
130 !(start + length <= lpart->start_sect ||
131 start >= lpart->start_sect + lpart->nr_sects)
132 ) {
133 disk_part_iter_exit(&piter);
134 mutex_unlock(&bdevp->bd_mutex);
135 mutex_unlock(&bdev->bd_mutex);
136 bdput(bdevp);
137 disk_put_part(part);
138 return -EBUSY;
139 }
140 }
141 disk_part_iter_exit(&piter);
142 part_nr_sects_write(part, (sector_t)length);
143 i_size_write(bdevp->bd_inode, p.length);
144 mutex_unlock(&bdevp->bd_mutex);
145 mutex_unlock(&bdev->bd_mutex);
146 bdput(bdevp);
147 disk_put_part(part);
1da177e4
LT
148 return 0;
149 default:
150 return -EINVAL;
151 }
152}
153
be324177
JW
154/*
155 * This is an exported API for the block driver, and will not
156 * acquire bd_mutex. This API should be used in case that
157 * caller has held bd_mutex already.
158 */
159int __blkdev_reread_part(struct block_device *bdev)
1da177e4
LT
160{
161 struct gendisk *disk = bdev->bd_disk;
1da177e4 162
d27769ec 163 if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
1da177e4
LT
164 return -EINVAL;
165 if (!capable(CAP_SYS_ADMIN))
166 return -EACCES;
be324177
JW
167
168 lockdep_assert_held(&bdev->bd_mutex);
169
170 return rescan_partitions(disk, bdev);
171}
172EXPORT_SYMBOL(__blkdev_reread_part);
173
174/*
175 * This is an exported API for the block driver, and will
176 * try to acquire bd_mutex. If bd_mutex has been held already
177 * in current context, please call __blkdev_reread_part().
b04a5636
ML
178 *
179 * Make sure the held locks in current context aren't required
180 * in open()/close() handler and I/O path for avoiding ABBA deadlock:
181 * - bd_mutex is held before calling block driver's open/close
182 * handler
183 * - reading partition table may submit I/O to the block device
be324177
JW
184 */
185int blkdev_reread_part(struct block_device *bdev)
186{
187 int res;
188
b04a5636 189 mutex_lock(&bdev->bd_mutex);
be324177 190 res = __blkdev_reread_part(bdev);
c039e313 191 mutex_unlock(&bdev->bd_mutex);
be324177 192
1da177e4
LT
193 return res;
194}
be324177 195EXPORT_SYMBOL(blkdev_reread_part);
1da177e4 196
d8e4bb81
CH
197static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
198 unsigned long arg, unsigned long flags)
d30a2605 199{
d8e4bb81
CH
200 uint64_t range[2];
201 uint64_t start, len;
202
203 if (!(mode & FMODE_WRITE))
204 return -EBADF;
205
206 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
207 return -EFAULT;
208
209 start = range[0];
210 len = range[1];
8d57a98c 211
d30a2605
DW
212 if (start & 511)
213 return -EINVAL;
214 if (len & 511)
215 return -EINVAL;
216 start >>= 9;
217 len >>= 9;
218
77304d2a 219 if (start + len > (i_size_read(bdev->bd_inode) >> 9))
d30a2605 220 return -EINVAL;
8d57a98c 221 return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
d30a2605
DW
222}
223
d8e4bb81
CH
224static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
225 unsigned long arg)
66ba32dc 226{
d8e4bb81
CH
227 uint64_t range[2];
228 uint64_t start, len;
229
230 if (!(mode & FMODE_WRITE))
231 return -EBADF;
232
233 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
234 return -EFAULT;
235
236 start = range[0];
237 len = range[1];
238
66ba32dc
MP
239 if (start & 511)
240 return -EINVAL;
241 if (len & 511)
242 return -EINVAL;
243 start >>= 9;
244 len >>= 9;
245
246 if (start + len > (i_size_read(bdev->bd_inode) >> 9))
247 return -EINVAL;
248
d93ba7a5 249 return blkdev_issue_zeroout(bdev, start, len, GFP_KERNEL, false);
66ba32dc
MP
250}
251
1da177e4
LT
252static int put_ushort(unsigned long arg, unsigned short val)
253{
254 return put_user(val, (unsigned short __user *)arg);
255}
256
257static int put_int(unsigned long arg, int val)
258{
259 return put_user(val, (int __user *)arg);
260}
261
ac481c20
MP
262static int put_uint(unsigned long arg, unsigned int val)
263{
264 return put_user(val, (unsigned int __user *)arg);
265}
266
1da177e4
LT
267static int put_long(unsigned long arg, long val)
268{
269 return put_user(val, (long __user *)arg);
270}
271
272static int put_ulong(unsigned long arg, unsigned long val)
273{
274 return put_user(val, (unsigned long __user *)arg);
275}
276
277static int put_u64(unsigned long arg, u64 val)
278{
279 return put_user(val, (u64 __user *)arg);
280}
281
633a08b8
AV
282int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
283 unsigned cmd, unsigned long arg)
284{
285 struct gendisk *disk = bdev->bd_disk;
d4430d62
AV
286
287 if (disk->fops->ioctl)
288 return disk->fops->ioctl(bdev, mode, cmd, arg);
633a08b8 289
633a08b8
AV
290 return -ENOTTY;
291}
292/*
293 * For the record: _GPL here is only because somebody decided to slap it
294 * on the previous export. Sheer idiocy, since it wasn't copyrightable
295 * at all and could be open-coded without any exports by anybody who cares.
296 */
297EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
298
bbd3e064
CH
299static int blkdev_pr_register(struct block_device *bdev,
300 struct pr_registration __user *arg)
301{
302 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
303 struct pr_registration reg;
304
305 if (!capable(CAP_SYS_ADMIN))
306 return -EPERM;
307 if (!ops || !ops->pr_register)
308 return -EOPNOTSUPP;
309 if (copy_from_user(&reg, arg, sizeof(reg)))
310 return -EFAULT;
311
312 if (reg.flags & ~PR_FL_IGNORE_KEY)
313 return -EOPNOTSUPP;
314 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
315}
316
317static int blkdev_pr_reserve(struct block_device *bdev,
318 struct pr_reservation __user *arg)
319{
320 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
321 struct pr_reservation rsv;
322
323 if (!capable(CAP_SYS_ADMIN))
324 return -EPERM;
325 if (!ops || !ops->pr_reserve)
326 return -EOPNOTSUPP;
327 if (copy_from_user(&rsv, arg, sizeof(rsv)))
328 return -EFAULT;
329
330 if (rsv.flags & ~PR_FL_IGNORE_KEY)
331 return -EOPNOTSUPP;
332 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
333}
334
335static int blkdev_pr_release(struct block_device *bdev,
336 struct pr_reservation __user *arg)
337{
338 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
339 struct pr_reservation rsv;
340
341 if (!capable(CAP_SYS_ADMIN))
342 return -EPERM;
343 if (!ops || !ops->pr_release)
344 return -EOPNOTSUPP;
345 if (copy_from_user(&rsv, arg, sizeof(rsv)))
346 return -EFAULT;
347
348 if (rsv.flags)
349 return -EOPNOTSUPP;
350 return ops->pr_release(bdev, rsv.key, rsv.type);
351}
352
353static int blkdev_pr_preempt(struct block_device *bdev,
354 struct pr_preempt __user *arg, bool abort)
355{
356 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
357 struct pr_preempt p;
358
359 if (!capable(CAP_SYS_ADMIN))
360 return -EPERM;
361 if (!ops || !ops->pr_preempt)
362 return -EOPNOTSUPP;
363 if (copy_from_user(&p, arg, sizeof(p)))
364 return -EFAULT;
365
366 if (p.flags)
367 return -EOPNOTSUPP;
368 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
369}
370
371static int blkdev_pr_clear(struct block_device *bdev,
372 struct pr_clear __user *arg)
373{
374 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
375 struct pr_clear c;
376
377 if (!capable(CAP_SYS_ADMIN))
378 return -EPERM;
379 if (!ops || !ops->pr_clear)
380 return -EOPNOTSUPP;
381 if (copy_from_user(&c, arg, sizeof(c)))
382 return -EFAULT;
383
384 if (c.flags)
385 return -EOPNOTSUPP;
386 return ops->pr_clear(bdev, c.key);
387}
388
07d106d0
LT
389/*
390 * Is it an unrecognized ioctl? The correct returns are either
391 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
392 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
393 * code before returning.
394 *
395 * Confused drivers sometimes return EINVAL, which is wrong. It
396 * means "I understood the ioctl command, but the parameters to
397 * it were wrong".
398 *
399 * We should aim to just fix the broken drivers, the EINVAL case
400 * should go away.
401 */
402static inline int is_unrecognized_ioctl(int ret)
403{
404 return ret == -EINVAL ||
405 ret == -ENOTTY ||
406 ret == -ENOIOCTLCMD;
407}
408
d8e4bb81
CH
409static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
410 unsigned cmd, unsigned long arg)
bb93e3a5 411{
d8e4bb81 412 int ret;
bb93e3a5 413
d8e4bb81
CH
414 if (!capable(CAP_SYS_ADMIN))
415 return -EACCES;
bb93e3a5 416
d8e4bb81
CH
417 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
418 if (!is_unrecognized_ioctl(ret))
419 return ret;
bb93e3a5 420
d8e4bb81
CH
421 fsync_bdev(bdev);
422 invalidate_bdev(bdev);
423 return 0;
424}
d30a2605 425
d8e4bb81
CH
426static int blkdev_roset(struct block_device *bdev, fmode_t mode,
427 unsigned cmd, unsigned long arg)
428{
429 int ret, n;
d30a2605 430
d8e4bb81
CH
431 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
432 if (!is_unrecognized_ioctl(ret))
433 return ret;
434 if (!capable(CAP_SYS_ADMIN))
435 return -EACCES;
436 if (get_user(n, (int __user *)arg))
437 return -EFAULT;
438 set_device_ro(bdev, n);
439 return 0;
440}
d30a2605 441
d8e4bb81
CH
442static int blkdev_getgeo(struct block_device *bdev,
443 struct hd_geometry __user *argp)
444{
445 struct gendisk *disk = bdev->bd_disk;
446 struct hd_geometry geo;
447 int ret;
d30a2605 448
d8e4bb81
CH
449 if (!argp)
450 return -EINVAL;
451 if (!disk->fops->getgeo)
452 return -ENOTTY;
453
454 /*
455 * We need to set the startsect first, the driver may
456 * want to override it.
457 */
458 memset(&geo, 0, sizeof(geo));
459 geo.start = get_start_sect(bdev);
460 ret = disk->fops->getgeo(bdev, &geo);
461 if (ret)
462 return ret;
463 if (copy_to_user(argp, &geo, sizeof(geo)))
464 return -EFAULT;
465 return 0;
466}
66ba32dc 467
d8e4bb81
CH
468/* set the logical block size */
469static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
470 int __user *argp)
471{
472 int ret, n;
66ba32dc 473
d8e4bb81
CH
474 if (!capable(CAP_SYS_ADMIN))
475 return -EACCES;
476 if (!argp)
477 return -EINVAL;
478 if (get_user(n, argp))
479 return -EFAULT;
66ba32dc 480
d8e4bb81
CH
481 if (!(mode & FMODE_EXCL)) {
482 bdgrab(bdev);
483 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
484 return -EBUSY;
66ba32dc 485 }
d30a2605 486
d8e4bb81
CH
487 ret = set_blocksize(bdev, n);
488 if (!(mode & FMODE_EXCL))
489 blkdev_put(bdev, mode | FMODE_EXCL);
490 return ret;
491}
a885c8c4 492
d8e4bb81
CH
493/*
494 * always keep this in sync with compat_blkdev_ioctl()
495 */
496int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
497 unsigned long arg)
498{
499 struct backing_dev_info *bdi;
500 void __user *argp = (void __user *)arg;
501 loff_t size;
502 unsigned int max_sectors;
503
504 switch (cmd) {
505 case BLKFLSBUF:
506 return blkdev_flushbuf(bdev, mode, cmd, arg);
507 case BLKROSET:
508 return blkdev_roset(bdev, mode, cmd, arg);
509 case BLKDISCARD:
510 return blk_ioctl_discard(bdev, mode, arg, 0);
511 case BLKSECDISCARD:
512 return blk_ioctl_discard(bdev, mode, arg,
513 BLKDEV_DISCARD_SECURE);
514 case BLKZEROOUT:
515 return blk_ioctl_zeroout(bdev, mode, arg);
516 case HDIO_GETGEO:
517 return blkdev_getgeo(bdev, argp);
45048d09
AV
518 case BLKRAGET:
519 case BLKFRAGET:
520 if (!arg)
521 return -EINVAL;
522 bdi = blk_get_backing_dev_info(bdev);
09cbfeaf 523 return put_long(arg, (bdi->ra_pages * PAGE_SIZE) / 512);
45048d09
AV
524 case BLKROGET:
525 return put_int(arg, bdev_read_only(bdev) != 0);
ac481c20 526 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
45048d09 527 return put_int(arg, block_size(bdev));
ac481c20 528 case BLKSSZGET: /* get block device logical block size */
e1defc4f 529 return put_int(arg, bdev_logical_block_size(bdev));
ac481c20
MP
530 case BLKPBSZGET: /* get block device physical block size */
531 return put_uint(arg, bdev_physical_block_size(bdev));
532 case BLKIOMIN:
533 return put_uint(arg, bdev_io_min(bdev));
534 case BLKIOOPT:
535 return put_uint(arg, bdev_io_opt(bdev));
536 case BLKALIGNOFF:
537 return put_int(arg, bdev_alignment_offset(bdev));
98262f27
MP
538 case BLKDISCARDZEROES:
539 return put_uint(arg, bdev_discard_zeroes_data(bdev));
45048d09 540 case BLKSECTGET:
63f26496
AM
541 max_sectors = min_t(unsigned int, USHRT_MAX,
542 queue_max_sectors(bdev_get_queue(bdev)));
543 return put_ushort(arg, max_sectors);
ef00f59c
MP
544 case BLKROTATIONAL:
545 return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
45048d09
AV
546 case BLKRASET:
547 case BLKFRASET:
548 if(!capable(CAP_SYS_ADMIN))
549 return -EACCES;
550 bdi = blk_get_backing_dev_info(bdev);
09cbfeaf 551 bdi->ra_pages = (arg * 512) / PAGE_SIZE;
45048d09
AV
552 return 0;
553 case BLKBSZSET:
d8e4bb81 554 return blkdev_bszset(bdev, mode, argp);
45048d09 555 case BLKPG:
d8e4bb81 556 return blkpg_ioctl(bdev, argp);
45048d09 557 case BLKRRPART:
d8e4bb81 558 return blkdev_reread_part(bdev);
45048d09 559 case BLKGETSIZE:
77304d2a 560 size = i_size_read(bdev->bd_inode);
45048d09
AV
561 if ((size >> 9) > ~0UL)
562 return -EFBIG;
563 return put_ulong(arg, size >> 9);
564 case BLKGETSIZE64:
77304d2a 565 return put_u64(arg, i_size_read(bdev->bd_inode));
45048d09
AV
566 case BLKTRACESTART:
567 case BLKTRACESTOP:
568 case BLKTRACESETUP:
569 case BLKTRACETEARDOWN:
d8e4bb81 570 return blk_trace_ioctl(bdev, cmd, argp);
bbd3e064
CH
571 case IOC_PR_REGISTER:
572 return blkdev_pr_register(bdev, argp);
573 case IOC_PR_RESERVE:
574 return blkdev_pr_reserve(bdev, argp);
575 case IOC_PR_RELEASE:
576 return blkdev_pr_release(bdev, argp);
577 case IOC_PR_PREEMPT:
578 return blkdev_pr_preempt(bdev, argp, false);
579 case IOC_PR_PREEMPT_ABORT:
580 return blkdev_pr_preempt(bdev, argp, true);
581 case IOC_PR_CLEAR:
582 return blkdev_pr_clear(bdev, argp);
45048d09 583 default:
d8e4bb81 584 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
45048d09 585 }
1da177e4 586}
68f66feb 587EXPORT_SYMBOL_GPL(blkdev_ioctl);