]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - block/ioctl.c
1 #include <linux/capability.h>
2 #include <linux/blkdev.h>
3 #include <linux/export.h>
5 #include <linux/blkpg.h>
6 #include <linux/hdreg.h>
7 #include <linux/badblocks.h>
8 #include <linux/backing-dev.h>
10 #include <linux/blktrace_api.h>
12 #include <asm/uaccess.h>
14 static int blkpg_ioctl(struct block_device
*bdev
, struct blkpg_ioctl_arg __user
*arg
)
16 struct block_device
*bdevp
;
18 struct hd_struct
*part
, *lpart
;
19 struct blkpg_ioctl_arg a
;
20 struct blkpg_partition p
;
21 struct disk_part_iter piter
;
22 long long start
, length
;
25 if (!capable(CAP_SYS_ADMIN
))
27 if (copy_from_user(&a
, arg
, sizeof(struct blkpg_ioctl_arg
)))
29 if (copy_from_user(&p
, a
.data
, sizeof(struct blkpg_partition
)))
32 if (bdev
!= bdev
->bd_contains
)
38 case BLKPG_ADD_PARTITION
:
40 length
= p
.length
>> 9;
41 /* check for fit in a hd_struct */
42 if (sizeof(sector_t
) == sizeof(long) &&
43 sizeof(long long) > sizeof(long)) {
44 long pstart
= start
, plength
= length
;
45 if (pstart
!= start
|| plength
!= length
46 || pstart
< 0 || plength
< 0 || partno
> 65535)
50 mutex_lock(&bdev
->bd_mutex
);
53 disk_part_iter_init(&piter
, disk
,
54 DISK_PITER_INCL_EMPTY
);
55 while ((part
= disk_part_iter_next(&piter
))) {
56 if (!(start
+ length
<= part
->start_sect
||
57 start
>= part
->start_sect
+ part
->nr_sects
)) {
58 disk_part_iter_exit(&piter
);
59 mutex_unlock(&bdev
->bd_mutex
);
63 disk_part_iter_exit(&piter
);
66 part
= add_partition(disk
, partno
, start
, length
,
67 ADDPART_FLAG_NONE
, NULL
);
68 mutex_unlock(&bdev
->bd_mutex
);
69 return PTR_ERR_OR_ZERO(part
);
70 case BLKPG_DEL_PARTITION
:
71 part
= disk_get_part(disk
, partno
);
75 bdevp
= bdget(part_devt(part
));
80 mutex_lock(&bdevp
->bd_mutex
);
81 if (bdevp
->bd_openers
) {
82 mutex_unlock(&bdevp
->bd_mutex
);
88 invalidate_bdev(bdevp
);
90 mutex_lock_nested(&bdev
->bd_mutex
, 1);
91 delete_partition(disk
, partno
);
92 mutex_unlock(&bdev
->bd_mutex
);
93 mutex_unlock(&bdevp
->bd_mutex
);
97 case BLKPG_RESIZE_PARTITION
:
99 /* new length of partition in bytes */
100 length
= p
.length
>> 9;
101 /* check for fit in a hd_struct */
102 if (sizeof(sector_t
) == sizeof(long) &&
103 sizeof(long long) > sizeof(long)) {
104 long pstart
= start
, plength
= length
;
105 if (pstart
!= start
|| plength
!= length
106 || pstart
< 0 || plength
< 0)
109 part
= disk_get_part(disk
, partno
);
112 bdevp
= bdget(part_devt(part
));
117 mutex_lock(&bdevp
->bd_mutex
);
118 mutex_lock_nested(&bdev
->bd_mutex
, 1);
119 if (start
!= part
->start_sect
) {
120 mutex_unlock(&bdevp
->bd_mutex
);
121 mutex_unlock(&bdev
->bd_mutex
);
127 disk_part_iter_init(&piter
, disk
,
128 DISK_PITER_INCL_EMPTY
);
129 while ((lpart
= disk_part_iter_next(&piter
))) {
130 if (lpart
->partno
!= partno
&&
131 !(start
+ length
<= lpart
->start_sect
||
132 start
>= lpart
->start_sect
+ lpart
->nr_sects
)
134 disk_part_iter_exit(&piter
);
135 mutex_unlock(&bdevp
->bd_mutex
);
136 mutex_unlock(&bdev
->bd_mutex
);
142 disk_part_iter_exit(&piter
);
143 part_nr_sects_write(part
, (sector_t
)length
);
144 i_size_write(bdevp
->bd_inode
, p
.length
);
145 mutex_unlock(&bdevp
->bd_mutex
);
146 mutex_unlock(&bdev
->bd_mutex
);
156 * This is an exported API for the block driver, and will not
157 * acquire bd_mutex. This API should be used in case that
158 * caller has held bd_mutex already.
160 int __blkdev_reread_part(struct block_device
*bdev
)
162 struct gendisk
*disk
= bdev
->bd_disk
;
164 if (!disk_part_scan_enabled(disk
) || bdev
!= bdev
->bd_contains
)
166 if (!capable(CAP_SYS_ADMIN
))
169 lockdep_assert_held(&bdev
->bd_mutex
);
171 return rescan_partitions(disk
, bdev
);
173 EXPORT_SYMBOL(__blkdev_reread_part
);
176 * This is an exported API for the block driver, and will
177 * try to acquire bd_mutex. If bd_mutex has been held already
178 * in current context, please call __blkdev_reread_part().
180 * Make sure the held locks in current context aren't required
181 * in open()/close() handler and I/O path for avoiding ABBA deadlock:
182 * - bd_mutex is held before calling block driver's open/close
184 * - reading partition table may submit I/O to the block device
186 int blkdev_reread_part(struct block_device
*bdev
)
190 mutex_lock(&bdev
->bd_mutex
);
191 res
= __blkdev_reread_part(bdev
);
192 mutex_unlock(&bdev
->bd_mutex
);
196 EXPORT_SYMBOL(blkdev_reread_part
);
198 static int blk_ioctl_discard(struct block_device
*bdev
, fmode_t mode
,
199 unsigned long arg
, unsigned long flags
)
204 if (!(mode
& FMODE_WRITE
))
207 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
220 if (start
+ len
> (i_size_read(bdev
->bd_inode
) >> 9))
222 return blkdev_issue_discard(bdev
, start
, len
, GFP_KERNEL
, flags
);
225 static int blk_ioctl_zeroout(struct block_device
*bdev
, fmode_t mode
,
231 if (!(mode
& FMODE_WRITE
))
234 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
247 if (start
+ len
> (i_size_read(bdev
->bd_inode
) >> 9))
250 return blkdev_issue_zeroout(bdev
, start
, len
, GFP_KERNEL
, false);
253 static int put_ushort(unsigned long arg
, unsigned short val
)
255 return put_user(val
, (unsigned short __user
*)arg
);
258 static int put_int(unsigned long arg
, int val
)
260 return put_user(val
, (int __user
*)arg
);
263 static int put_uint(unsigned long arg
, unsigned int val
)
265 return put_user(val
, (unsigned int __user
*)arg
);
268 static int put_long(unsigned long arg
, long val
)
270 return put_user(val
, (long __user
*)arg
);
273 static int put_ulong(unsigned long arg
, unsigned long val
)
275 return put_user(val
, (unsigned long __user
*)arg
);
278 static int put_u64(unsigned long arg
, u64 val
)
280 return put_user(val
, (u64 __user
*)arg
);
283 int __blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
284 unsigned cmd
, unsigned long arg
)
286 struct gendisk
*disk
= bdev
->bd_disk
;
288 if (disk
->fops
->ioctl
)
289 return disk
->fops
->ioctl(bdev
, mode
, cmd
, arg
);
294 * For the record: _GPL here is only because somebody decided to slap it
295 * on the previous export. Sheer idiocy, since it wasn't copyrightable
296 * at all and could be open-coded without any exports by anybody who cares.
298 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl
);
300 static int blkdev_pr_register(struct block_device
*bdev
,
301 struct pr_registration __user
*arg
)
303 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
304 struct pr_registration reg
;
306 if (!capable(CAP_SYS_ADMIN
))
308 if (!ops
|| !ops
->pr_register
)
310 if (copy_from_user(®
, arg
, sizeof(reg
)))
313 if (reg
.flags
& ~PR_FL_IGNORE_KEY
)
315 return ops
->pr_register(bdev
, reg
.old_key
, reg
.new_key
, reg
.flags
);
318 static int blkdev_pr_reserve(struct block_device
*bdev
,
319 struct pr_reservation __user
*arg
)
321 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
322 struct pr_reservation rsv
;
324 if (!capable(CAP_SYS_ADMIN
))
326 if (!ops
|| !ops
->pr_reserve
)
328 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
331 if (rsv
.flags
& ~PR_FL_IGNORE_KEY
)
333 return ops
->pr_reserve(bdev
, rsv
.key
, rsv
.type
, rsv
.flags
);
336 static int blkdev_pr_release(struct block_device
*bdev
,
337 struct pr_reservation __user
*arg
)
339 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
340 struct pr_reservation rsv
;
342 if (!capable(CAP_SYS_ADMIN
))
344 if (!ops
|| !ops
->pr_release
)
346 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
351 return ops
->pr_release(bdev
, rsv
.key
, rsv
.type
);
354 static int blkdev_pr_preempt(struct block_device
*bdev
,
355 struct pr_preempt __user
*arg
, bool abort
)
357 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
360 if (!capable(CAP_SYS_ADMIN
))
362 if (!ops
|| !ops
->pr_preempt
)
364 if (copy_from_user(&p
, arg
, sizeof(p
)))
369 return ops
->pr_preempt(bdev
, p
.old_key
, p
.new_key
, p
.type
, abort
);
372 static int blkdev_pr_clear(struct block_device
*bdev
,
373 struct pr_clear __user
*arg
)
375 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
378 if (!capable(CAP_SYS_ADMIN
))
380 if (!ops
|| !ops
->pr_clear
)
382 if (copy_from_user(&c
, arg
, sizeof(c
)))
387 return ops
->pr_clear(bdev
, c
.key
);
391 * Is it an unrecognized ioctl? The correct returns are either
392 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
393 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
394 * code before returning.
396 * Confused drivers sometimes return EINVAL, which is wrong. It
397 * means "I understood the ioctl command, but the parameters to
400 * We should aim to just fix the broken drivers, the EINVAL case
403 static inline int is_unrecognized_ioctl(int ret
)
405 return ret
== -EINVAL
||
411 bool blkdev_dax_capable(struct block_device
*bdev
)
413 struct gendisk
*disk
= bdev
->bd_disk
;
415 if (!disk
->fops
->direct_access
)
419 * If the partition is not aligned on a page boundary, we can't
422 if ((bdev
->bd_part
->start_sect
% (PAGE_SIZE
/ 512))
423 || (bdev
->bd_part
->nr_sects
% (PAGE_SIZE
/ 512)))
427 * If the device has known bad blocks, force all I/O through the
428 * driver / page cache.
430 * TODO: support finer grained dax error handling
432 if (disk
->bb
&& disk
->bb
->count
)
438 static int blkdev_daxset(struct block_device
*bdev
, unsigned long argp
)
443 if (!capable(CAP_SYS_ADMIN
))
446 if (get_user(arg
, (int __user
*)(argp
)))
449 if (arg
== !!(bdev
->bd_inode
->i_flags
& S_DAX
))
455 if (arg
&& !blkdev_dax_capable(bdev
))
458 mutex_lock(&bdev
->bd_inode
->i_mutex
);
459 if (bdev
->bd_map_count
== 0)
460 inode_set_flags(bdev
->bd_inode
, arg
, S_DAX
);
463 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
467 static int blkdev_daxset(struct block_device
*bdev
, int arg
)
475 static int blkdev_flushbuf(struct block_device
*bdev
, fmode_t mode
,
476 unsigned cmd
, unsigned long arg
)
480 if (!capable(CAP_SYS_ADMIN
))
483 ret
= __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
484 if (!is_unrecognized_ioctl(ret
))
488 invalidate_bdev(bdev
);
492 static int blkdev_roset(struct block_device
*bdev
, fmode_t mode
,
493 unsigned cmd
, unsigned long arg
)
497 ret
= __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
498 if (!is_unrecognized_ioctl(ret
))
500 if (!capable(CAP_SYS_ADMIN
))
502 if (get_user(n
, (int __user
*)arg
))
504 set_device_ro(bdev
, n
);
508 static int blkdev_getgeo(struct block_device
*bdev
,
509 struct hd_geometry __user
*argp
)
511 struct gendisk
*disk
= bdev
->bd_disk
;
512 struct hd_geometry geo
;
517 if (!disk
->fops
->getgeo
)
521 * We need to set the startsect first, the driver may
522 * want to override it.
524 memset(&geo
, 0, sizeof(geo
));
525 geo
.start
= get_start_sect(bdev
);
526 ret
= disk
->fops
->getgeo(bdev
, &geo
);
529 if (copy_to_user(argp
, &geo
, sizeof(geo
)))
534 /* set the logical block size */
535 static int blkdev_bszset(struct block_device
*bdev
, fmode_t mode
,
540 if (!capable(CAP_SYS_ADMIN
))
544 if (get_user(n
, argp
))
547 if (!(mode
& FMODE_EXCL
)) {
549 if (blkdev_get(bdev
, mode
| FMODE_EXCL
, &bdev
) < 0)
553 ret
= set_blocksize(bdev
, n
);
554 if (!(mode
& FMODE_EXCL
))
555 blkdev_put(bdev
, mode
| FMODE_EXCL
);
560 * always keep this in sync with compat_blkdev_ioctl()
562 int blkdev_ioctl(struct block_device
*bdev
, fmode_t mode
, unsigned cmd
,
565 struct backing_dev_info
*bdi
;
566 void __user
*argp
= (void __user
*)arg
;
568 unsigned int max_sectors
;
572 return blkdev_flushbuf(bdev
, mode
, cmd
, arg
);
574 return blkdev_roset(bdev
, mode
, cmd
, arg
);
576 return blk_ioctl_discard(bdev
, mode
, arg
, 0);
578 return blk_ioctl_discard(bdev
, mode
, arg
,
579 BLKDEV_DISCARD_SECURE
);
581 return blk_ioctl_zeroout(bdev
, mode
, arg
);
583 return blkdev_getgeo(bdev
, argp
);
588 bdi
= blk_get_backing_dev_info(bdev
);
589 return put_long(arg
, (bdi
->ra_pages
* PAGE_CACHE_SIZE
) / 512);
591 return put_int(arg
, bdev_read_only(bdev
) != 0);
592 case BLKBSZGET
: /* get block device soft block size (cf. BLKSSZGET) */
593 return put_int(arg
, block_size(bdev
));
594 case BLKSSZGET
: /* get block device logical block size */
595 return put_int(arg
, bdev_logical_block_size(bdev
));
596 case BLKPBSZGET
: /* get block device physical block size */
597 return put_uint(arg
, bdev_physical_block_size(bdev
));
599 return put_uint(arg
, bdev_io_min(bdev
));
601 return put_uint(arg
, bdev_io_opt(bdev
));
603 return put_int(arg
, bdev_alignment_offset(bdev
));
604 case BLKDISCARDZEROES
:
605 return put_uint(arg
, bdev_discard_zeroes_data(bdev
));
607 max_sectors
= min_t(unsigned int, USHRT_MAX
,
608 queue_max_sectors(bdev_get_queue(bdev
)));
609 return put_ushort(arg
, max_sectors
);
611 return put_ushort(arg
, !blk_queue_nonrot(bdev_get_queue(bdev
)));
614 if(!capable(CAP_SYS_ADMIN
))
616 bdi
= blk_get_backing_dev_info(bdev
);
617 bdi
->ra_pages
= (arg
* 512) / PAGE_CACHE_SIZE
;
620 return blkdev_bszset(bdev
, mode
, argp
);
622 return blkpg_ioctl(bdev
, argp
);
624 return blkdev_reread_part(bdev
);
626 size
= i_size_read(bdev
->bd_inode
);
627 if ((size
>> 9) > ~0UL)
629 return put_ulong(arg
, size
>> 9);
631 return put_u64(arg
, i_size_read(bdev
->bd_inode
));
635 case BLKTRACETEARDOWN
:
636 return blk_trace_ioctl(bdev
, cmd
, argp
);
638 return blkdev_daxset(bdev
, arg
);
640 return put_int(arg
, !!(bdev
->bd_inode
->i_flags
& S_DAX
));
642 case IOC_PR_REGISTER
:
643 return blkdev_pr_register(bdev
, argp
);
645 return blkdev_pr_reserve(bdev
, argp
);
647 return blkdev_pr_release(bdev
, argp
);
649 return blkdev_pr_preempt(bdev
, argp
, false);
650 case IOC_PR_PREEMPT_ABORT
:
651 return blkdev_pr_preempt(bdev
, argp
, true);
653 return blkdev_pr_clear(bdev
, argp
);
655 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
658 EXPORT_SYMBOL_GPL(blkdev_ioctl
);