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