1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/capability.h>
3 #include <linux/blkdev.h>
4 #include <linux/export.h>
6 #include <linux/blkpg.h>
7 #include <linux/hdreg.h>
8 #include <linux/backing-dev.h>
10 #include <linux/blktrace_api.h>
12 #include <linux/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)
49 /* check if partition is aligned to blocksize */
50 if (p
.start
& (bdev_logical_block_size(bdev
) - 1))
53 mutex_lock(&bdev
->bd_mutex
);
56 disk_part_iter_init(&piter
, disk
,
57 DISK_PITER_INCL_EMPTY
);
58 while ((part
= disk_part_iter_next(&piter
))) {
59 if (!(start
+ length
<= part
->start_sect
||
60 start
>= part
->start_sect
+ part
->nr_sects
)) {
61 disk_part_iter_exit(&piter
);
62 mutex_unlock(&bdev
->bd_mutex
);
66 disk_part_iter_exit(&piter
);
69 part
= add_partition(disk
, partno
, start
, length
,
70 ADDPART_FLAG_NONE
, NULL
);
71 mutex_unlock(&bdev
->bd_mutex
);
72 return PTR_ERR_OR_ZERO(part
);
73 case BLKPG_DEL_PARTITION
:
74 part
= disk_get_part(disk
, partno
);
78 bdevp
= bdget(part_devt(part
));
83 mutex_lock(&bdevp
->bd_mutex
);
84 if (bdevp
->bd_openers
) {
85 mutex_unlock(&bdevp
->bd_mutex
);
91 invalidate_bdev(bdevp
);
93 mutex_lock_nested(&bdev
->bd_mutex
, 1);
94 delete_partition(disk
, partno
);
95 mutex_unlock(&bdev
->bd_mutex
);
96 mutex_unlock(&bdevp
->bd_mutex
);
100 case BLKPG_RESIZE_PARTITION
:
101 start
= p
.start
>> 9;
102 /* new length of partition in bytes */
103 length
= p
.length
>> 9;
104 /* check for fit in a hd_struct */
105 if (sizeof(sector_t
) == sizeof(long) &&
106 sizeof(long long) > sizeof(long)) {
107 long pstart
= start
, plength
= length
;
108 if (pstart
!= start
|| plength
!= length
109 || pstart
< 0 || plength
< 0)
112 part
= disk_get_part(disk
, partno
);
115 bdevp
= bdget(part_devt(part
));
120 mutex_lock(&bdevp
->bd_mutex
);
121 mutex_lock_nested(&bdev
->bd_mutex
, 1);
122 if (start
!= part
->start_sect
) {
123 mutex_unlock(&bdevp
->bd_mutex
);
124 mutex_unlock(&bdev
->bd_mutex
);
130 disk_part_iter_init(&piter
, disk
,
131 DISK_PITER_INCL_EMPTY
);
132 while ((lpart
= disk_part_iter_next(&piter
))) {
133 if (lpart
->partno
!= partno
&&
134 !(start
+ length
<= lpart
->start_sect
||
135 start
>= lpart
->start_sect
+ lpart
->nr_sects
)
137 disk_part_iter_exit(&piter
);
138 mutex_unlock(&bdevp
->bd_mutex
);
139 mutex_unlock(&bdev
->bd_mutex
);
145 disk_part_iter_exit(&piter
);
146 part_nr_sects_write(part
, (sector_t
)length
);
147 i_size_write(bdevp
->bd_inode
, p
.length
);
148 mutex_unlock(&bdevp
->bd_mutex
);
149 mutex_unlock(&bdev
->bd_mutex
);
158 static int blkdev_reread_part(struct block_device
*bdev
)
162 if (!disk_part_scan_enabled(bdev
->bd_disk
) || bdev
!= bdev
->bd_contains
)
164 if (!capable(CAP_SYS_ADMIN
))
167 mutex_lock(&bdev
->bd_mutex
);
168 ret
= bdev_disk_changed(bdev
, false);
169 mutex_unlock(&bdev
->bd_mutex
);
174 static int blk_ioctl_discard(struct block_device
*bdev
, fmode_t mode
,
175 unsigned long arg
, unsigned long flags
)
179 struct request_queue
*q
= bdev_get_queue(bdev
);
180 struct address_space
*mapping
= bdev
->bd_inode
->i_mapping
;
183 if (!(mode
& FMODE_WRITE
))
186 if (!blk_queue_discard(q
))
189 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
200 if (start
+ len
> i_size_read(bdev
->bd_inode
))
202 truncate_inode_pages_range(mapping
, start
, start
+ len
- 1);
203 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9,
207 static int blk_ioctl_zeroout(struct block_device
*bdev
, fmode_t mode
,
211 struct address_space
*mapping
;
212 uint64_t start
, end
, len
;
214 if (!(mode
& FMODE_WRITE
))
217 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
222 end
= start
+ len
- 1;
228 if (end
>= (uint64_t)i_size_read(bdev
->bd_inode
))
233 /* Invalidate the page cache, including dirty pages */
234 mapping
= bdev
->bd_inode
->i_mapping
;
235 truncate_inode_pages_range(mapping
, start
, end
);
237 return blkdev_issue_zeroout(bdev
, start
>> 9, len
>> 9, GFP_KERNEL
,
238 BLKDEV_ZERO_NOUNMAP
);
241 static int put_ushort(unsigned long arg
, unsigned short val
)
243 return put_user(val
, (unsigned short __user
*)arg
);
246 static int put_int(unsigned long arg
, int val
)
248 return put_user(val
, (int __user
*)arg
);
251 static int put_uint(unsigned long arg
, unsigned int val
)
253 return put_user(val
, (unsigned int __user
*)arg
);
256 static int put_long(unsigned long arg
, long val
)
258 return put_user(val
, (long __user
*)arg
);
261 static int put_ulong(unsigned long arg
, unsigned long val
)
263 return put_user(val
, (unsigned long __user
*)arg
);
266 static int put_u64(unsigned long arg
, u64 val
)
268 return put_user(val
, (u64 __user
*)arg
);
271 int __blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
272 unsigned cmd
, unsigned long arg
)
274 struct gendisk
*disk
= bdev
->bd_disk
;
276 if (disk
->fops
->ioctl
)
277 return disk
->fops
->ioctl(bdev
, mode
, cmd
, arg
);
282 * For the record: _GPL here is only because somebody decided to slap it
283 * on the previous export. Sheer idiocy, since it wasn't copyrightable
284 * at all and could be open-coded without any exports by anybody who cares.
286 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl
);
288 static int blkdev_pr_register(struct block_device
*bdev
,
289 struct pr_registration __user
*arg
)
291 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
292 struct pr_registration reg
;
294 if (!capable(CAP_SYS_ADMIN
))
296 if (!ops
|| !ops
->pr_register
)
298 if (copy_from_user(®
, arg
, sizeof(reg
)))
301 if (reg
.flags
& ~PR_FL_IGNORE_KEY
)
303 return ops
->pr_register(bdev
, reg
.old_key
, reg
.new_key
, reg
.flags
);
306 static int blkdev_pr_reserve(struct block_device
*bdev
,
307 struct pr_reservation __user
*arg
)
309 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
310 struct pr_reservation rsv
;
312 if (!capable(CAP_SYS_ADMIN
))
314 if (!ops
|| !ops
->pr_reserve
)
316 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
319 if (rsv
.flags
& ~PR_FL_IGNORE_KEY
)
321 return ops
->pr_reserve(bdev
, rsv
.key
, rsv
.type
, rsv
.flags
);
324 static int blkdev_pr_release(struct block_device
*bdev
,
325 struct pr_reservation __user
*arg
)
327 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
328 struct pr_reservation rsv
;
330 if (!capable(CAP_SYS_ADMIN
))
332 if (!ops
|| !ops
->pr_release
)
334 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
339 return ops
->pr_release(bdev
, rsv
.key
, rsv
.type
);
342 static int blkdev_pr_preempt(struct block_device
*bdev
,
343 struct pr_preempt __user
*arg
, bool abort
)
345 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
348 if (!capable(CAP_SYS_ADMIN
))
350 if (!ops
|| !ops
->pr_preempt
)
352 if (copy_from_user(&p
, arg
, sizeof(p
)))
357 return ops
->pr_preempt(bdev
, p
.old_key
, p
.new_key
, p
.type
, abort
);
360 static int blkdev_pr_clear(struct block_device
*bdev
,
361 struct pr_clear __user
*arg
)
363 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
366 if (!capable(CAP_SYS_ADMIN
))
368 if (!ops
|| !ops
->pr_clear
)
370 if (copy_from_user(&c
, arg
, sizeof(c
)))
375 return ops
->pr_clear(bdev
, c
.key
);
379 * Is it an unrecognized ioctl? The correct returns are either
380 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
381 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
382 * code before returning.
384 * Confused drivers sometimes return EINVAL, which is wrong. It
385 * means "I understood the ioctl command, but the parameters to
388 * We should aim to just fix the broken drivers, the EINVAL case
391 static inline int is_unrecognized_ioctl(int ret
)
393 return ret
== -EINVAL
||
398 static int blkdev_flushbuf(struct block_device
*bdev
, fmode_t mode
,
399 unsigned cmd
, unsigned long arg
)
403 if (!capable(CAP_SYS_ADMIN
))
406 ret
= __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
407 if (!is_unrecognized_ioctl(ret
))
411 invalidate_bdev(bdev
);
415 static int blkdev_roset(struct block_device
*bdev
, fmode_t mode
,
416 unsigned cmd
, unsigned long arg
)
420 if (!capable(CAP_SYS_ADMIN
))
423 ret
= __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
424 if (!is_unrecognized_ioctl(ret
))
426 if (get_user(n
, (int __user
*)arg
))
428 set_device_ro(bdev
, n
);
432 static int blkdev_getgeo(struct block_device
*bdev
,
433 struct hd_geometry __user
*argp
)
435 struct gendisk
*disk
= bdev
->bd_disk
;
436 struct hd_geometry geo
;
441 if (!disk
->fops
->getgeo
)
445 * We need to set the startsect first, the driver may
446 * want to override it.
448 memset(&geo
, 0, sizeof(geo
));
449 geo
.start
= get_start_sect(bdev
);
450 ret
= disk
->fops
->getgeo(bdev
, &geo
);
453 if (copy_to_user(argp
, &geo
, sizeof(geo
)))
458 /* set the logical block size */
459 static int blkdev_bszset(struct block_device
*bdev
, fmode_t mode
,
464 if (!capable(CAP_SYS_ADMIN
))
468 if (get_user(n
, argp
))
471 if (!(mode
& FMODE_EXCL
)) {
473 if (blkdev_get(bdev
, mode
| FMODE_EXCL
, &bdev
) < 0)
477 ret
= set_blocksize(bdev
, n
);
478 if (!(mode
& FMODE_EXCL
))
479 blkdev_put(bdev
, mode
| FMODE_EXCL
);
484 * always keep this in sync with compat_blkdev_ioctl()
486 int blkdev_ioctl(struct block_device
*bdev
, fmode_t mode
, unsigned cmd
,
489 void __user
*argp
= (void __user
*)arg
;
491 unsigned int max_sectors
;
495 return blkdev_flushbuf(bdev
, mode
, cmd
, arg
);
497 return blkdev_roset(bdev
, mode
, cmd
, arg
);
499 return blk_ioctl_discard(bdev
, mode
, arg
, 0);
501 return blk_ioctl_discard(bdev
, mode
, arg
,
502 BLKDEV_DISCARD_SECURE
);
504 return blk_ioctl_zeroout(bdev
, mode
, arg
);
506 return blkdev_report_zones_ioctl(bdev
, mode
, cmd
, arg
);
511 return blkdev_zone_mgmt_ioctl(bdev
, mode
, cmd
, arg
);
513 return put_uint(arg
, bdev_zone_sectors(bdev
));
515 return put_uint(arg
, blkdev_nr_zones(bdev
->bd_disk
));
517 return blkdev_getgeo(bdev
, argp
);
522 return put_long(arg
, (bdev
->bd_bdi
->ra_pages
*PAGE_SIZE
) / 512);
524 return put_int(arg
, bdev_read_only(bdev
) != 0);
525 case BLKBSZGET
: /* get block device soft block size (cf. BLKSSZGET) */
526 return put_int(arg
, block_size(bdev
));
527 case BLKSSZGET
: /* get block device logical block size */
528 return put_int(arg
, bdev_logical_block_size(bdev
));
529 case BLKPBSZGET
: /* get block device physical block size */
530 return put_uint(arg
, bdev_physical_block_size(bdev
));
532 return put_uint(arg
, bdev_io_min(bdev
));
534 return put_uint(arg
, bdev_io_opt(bdev
));
536 return put_int(arg
, bdev_alignment_offset(bdev
));
537 case BLKDISCARDZEROES
:
538 return put_uint(arg
, 0);
540 max_sectors
= min_t(unsigned int, USHRT_MAX
,
541 queue_max_sectors(bdev_get_queue(bdev
)));
542 return put_ushort(arg
, max_sectors
);
544 return put_ushort(arg
, !blk_queue_nonrot(bdev_get_queue(bdev
)));
547 if(!capable(CAP_SYS_ADMIN
))
549 bdev
->bd_bdi
->ra_pages
= (arg
* 512) / PAGE_SIZE
;
552 return blkdev_bszset(bdev
, mode
, argp
);
554 return blkpg_ioctl(bdev
, argp
);
556 return blkdev_reread_part(bdev
);
558 size
= i_size_read(bdev
->bd_inode
);
559 if ((size
>> 9) > ~0UL)
561 return put_ulong(arg
, size
>> 9);
563 return put_u64(arg
, i_size_read(bdev
->bd_inode
));
567 case BLKTRACETEARDOWN
:
568 return blk_trace_ioctl(bdev
, cmd
, argp
);
569 case IOC_PR_REGISTER
:
570 return blkdev_pr_register(bdev
, argp
);
572 return blkdev_pr_reserve(bdev
, argp
);
574 return blkdev_pr_release(bdev
, argp
);
576 return blkdev_pr_preempt(bdev
, argp
, false);
577 case IOC_PR_PREEMPT_ABORT
:
578 return blkdev_pr_preempt(bdev
, argp
, true);
580 return blkdev_pr_clear(bdev
, argp
);
582 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
585 EXPORT_SYMBOL_GPL(blkdev_ioctl
);