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/backing-dev.h>
9 #include <linux/blktrace_api.h>
11 #include <linux/uaccess.h>
13 static int blkpg_ioctl(struct block_device
*bdev
, struct blkpg_ioctl_arg __user
*arg
)
15 struct block_device
*bdevp
;
17 struct hd_struct
*part
, *lpart
;
18 struct blkpg_ioctl_arg a
;
19 struct blkpg_partition p
;
20 struct disk_part_iter piter
;
21 long long start
, length
;
24 if (!capable(CAP_SYS_ADMIN
))
26 if (copy_from_user(&a
, arg
, sizeof(struct blkpg_ioctl_arg
)))
28 if (copy_from_user(&p
, a
.data
, sizeof(struct blkpg_partition
)))
31 if (bdev
!= bdev
->bd_contains
)
37 case BLKPG_ADD_PARTITION
:
39 length
= p
.length
>> 9;
40 /* check for fit in a hd_struct */
41 if (sizeof(sector_t
) == sizeof(long) &&
42 sizeof(long long) > sizeof(long)) {
43 long pstart
= start
, plength
= length
;
44 if (pstart
!= start
|| plength
!= length
45 || pstart
< 0 || plength
< 0 || partno
> 65535)
48 /* check if partition is aligned to blocksize */
49 if (p
.start
& (bdev_logical_block_size(bdev
) - 1))
52 mutex_lock(&bdev
->bd_mutex
);
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
);
61 mutex_unlock(&bdev
->bd_mutex
);
65 disk_part_iter_exit(&piter
);
68 part
= add_partition(disk
, partno
, start
, length
,
69 ADDPART_FLAG_NONE
, NULL
);
70 mutex_unlock(&bdev
->bd_mutex
);
71 return PTR_ERR_OR_ZERO(part
);
72 case BLKPG_DEL_PARTITION
:
73 part
= disk_get_part(disk
, partno
);
77 bdevp
= bdget(part_devt(part
));
82 mutex_lock(&bdevp
->bd_mutex
);
83 if (bdevp
->bd_openers
) {
84 mutex_unlock(&bdevp
->bd_mutex
);
90 invalidate_bdev(bdevp
);
92 mutex_lock_nested(&bdev
->bd_mutex
, 1);
93 delete_partition(disk
, partno
);
94 mutex_unlock(&bdev
->bd_mutex
);
95 mutex_unlock(&bdevp
->bd_mutex
);
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)
111 part
= disk_get_part(disk
, partno
);
114 bdevp
= bdget(part_devt(part
));
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
);
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
)
136 disk_part_iter_exit(&piter
);
137 mutex_unlock(&bdevp
->bd_mutex
);
138 mutex_unlock(&bdev
->bd_mutex
);
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
);
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.
162 int __blkdev_reread_part(struct block_device
*bdev
)
164 struct gendisk
*disk
= bdev
->bd_disk
;
166 if (!disk_part_scan_enabled(disk
) || bdev
!= bdev
->bd_contains
)
168 if (!capable(CAP_SYS_ADMIN
))
171 lockdep_assert_held(&bdev
->bd_mutex
);
173 return rescan_partitions(disk
, bdev
);
175 EXPORT_SYMBOL(__blkdev_reread_part
);
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().
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
186 * - reading partition table may submit I/O to the block device
188 int blkdev_reread_part(struct block_device
*bdev
)
192 mutex_lock(&bdev
->bd_mutex
);
193 res
= __blkdev_reread_part(bdev
);
194 mutex_unlock(&bdev
->bd_mutex
);
198 EXPORT_SYMBOL(blkdev_reread_part
);
200 static int blk_ioctl_discard(struct block_device
*bdev
, fmode_t mode
,
201 unsigned long arg
, unsigned long flags
)
206 if (!(mode
& FMODE_WRITE
))
209 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
222 if (start
+ len
> (i_size_read(bdev
->bd_inode
) >> 9))
224 return blkdev_issue_discard(bdev
, start
, len
, GFP_KERNEL
, flags
);
227 static int blk_ioctl_zeroout(struct block_device
*bdev
, fmode_t mode
,
231 struct address_space
*mapping
;
232 uint64_t start
, end
, len
;
234 if (!(mode
& FMODE_WRITE
))
237 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
242 end
= start
+ len
- 1;
248 if (end
>= (uint64_t)i_size_read(bdev
->bd_inode
))
253 /* Invalidate the page cache, including dirty pages */
254 mapping
= bdev
->bd_inode
->i_mapping
;
255 truncate_inode_pages_range(mapping
, start
, end
);
257 return blkdev_issue_zeroout(bdev
, start
>> 9, len
>> 9, GFP_KERNEL
,
258 BLKDEV_ZERO_NOUNMAP
);
261 static int put_ushort(unsigned long arg
, unsigned short val
)
263 return put_user(val
, (unsigned short __user
*)arg
);
266 static int put_int(unsigned long arg
, int val
)
268 return put_user(val
, (int __user
*)arg
);
271 static int put_uint(unsigned long arg
, unsigned int val
)
273 return put_user(val
, (unsigned int __user
*)arg
);
276 static int put_long(unsigned long arg
, long val
)
278 return put_user(val
, (long __user
*)arg
);
281 static int put_ulong(unsigned long arg
, unsigned long val
)
283 return put_user(val
, (unsigned long __user
*)arg
);
286 static int put_u64(unsigned long arg
, u64 val
)
288 return put_user(val
, (u64 __user
*)arg
);
291 int __blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
292 unsigned cmd
, unsigned long arg
)
294 struct gendisk
*disk
= bdev
->bd_disk
;
296 if (disk
->fops
->ioctl
)
297 return disk
->fops
->ioctl(bdev
, mode
, cmd
, arg
);
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.
306 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl
);
308 static int blkdev_pr_register(struct block_device
*bdev
,
309 struct pr_registration __user
*arg
)
311 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
312 struct pr_registration reg
;
314 if (!capable(CAP_SYS_ADMIN
))
316 if (!ops
|| !ops
->pr_register
)
318 if (copy_from_user(®
, arg
, sizeof(reg
)))
321 if (reg
.flags
& ~PR_FL_IGNORE_KEY
)
323 return ops
->pr_register(bdev
, reg
.old_key
, reg
.new_key
, reg
.flags
);
326 static int blkdev_pr_reserve(struct block_device
*bdev
,
327 struct pr_reservation __user
*arg
)
329 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
330 struct pr_reservation rsv
;
332 if (!capable(CAP_SYS_ADMIN
))
334 if (!ops
|| !ops
->pr_reserve
)
336 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
339 if (rsv
.flags
& ~PR_FL_IGNORE_KEY
)
341 return ops
->pr_reserve(bdev
, rsv
.key
, rsv
.type
, rsv
.flags
);
344 static int blkdev_pr_release(struct block_device
*bdev
,
345 struct pr_reservation __user
*arg
)
347 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
348 struct pr_reservation rsv
;
350 if (!capable(CAP_SYS_ADMIN
))
352 if (!ops
|| !ops
->pr_release
)
354 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
359 return ops
->pr_release(bdev
, rsv
.key
, rsv
.type
);
362 static int blkdev_pr_preempt(struct block_device
*bdev
,
363 struct pr_preempt __user
*arg
, bool abort
)
365 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
368 if (!capable(CAP_SYS_ADMIN
))
370 if (!ops
|| !ops
->pr_preempt
)
372 if (copy_from_user(&p
, arg
, sizeof(p
)))
377 return ops
->pr_preempt(bdev
, p
.old_key
, p
.new_key
, p
.type
, abort
);
380 static int blkdev_pr_clear(struct block_device
*bdev
,
381 struct pr_clear __user
*arg
)
383 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
386 if (!capable(CAP_SYS_ADMIN
))
388 if (!ops
|| !ops
->pr_clear
)
390 if (copy_from_user(&c
, arg
, sizeof(c
)))
395 return ops
->pr_clear(bdev
, c
.key
);
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.
404 * Confused drivers sometimes return EINVAL, which is wrong. It
405 * means "I understood the ioctl command, but the parameters to
408 * We should aim to just fix the broken drivers, the EINVAL case
411 static inline int is_unrecognized_ioctl(int ret
)
413 return ret
== -EINVAL
||
418 static int blkdev_flushbuf(struct block_device
*bdev
, fmode_t mode
,
419 unsigned cmd
, unsigned long arg
)
423 if (!capable(CAP_SYS_ADMIN
))
426 ret
= __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
427 if (!is_unrecognized_ioctl(ret
))
431 invalidate_bdev(bdev
);
435 static int blkdev_roset(struct block_device
*bdev
, fmode_t mode
,
436 unsigned cmd
, unsigned long arg
)
440 ret
= __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
441 if (!is_unrecognized_ioctl(ret
))
443 if (!capable(CAP_SYS_ADMIN
))
445 if (get_user(n
, (int __user
*)arg
))
447 set_device_ro(bdev
, n
);
451 static int blkdev_getgeo(struct block_device
*bdev
,
452 struct hd_geometry __user
*argp
)
454 struct gendisk
*disk
= bdev
->bd_disk
;
455 struct hd_geometry geo
;
460 if (!disk
->fops
->getgeo
)
464 * We need to set the startsect first, the driver may
465 * want to override it.
467 memset(&geo
, 0, sizeof(geo
));
468 geo
.start
= get_start_sect(bdev
);
469 ret
= disk
->fops
->getgeo(bdev
, &geo
);
472 if (copy_to_user(argp
, &geo
, sizeof(geo
)))
477 /* set the logical block size */
478 static int blkdev_bszset(struct block_device
*bdev
, fmode_t mode
,
483 if (!capable(CAP_SYS_ADMIN
))
487 if (get_user(n
, argp
))
490 if (!(mode
& FMODE_EXCL
)) {
492 if (blkdev_get(bdev
, mode
| FMODE_EXCL
, &bdev
) < 0)
496 ret
= set_blocksize(bdev
, n
);
497 if (!(mode
& FMODE_EXCL
))
498 blkdev_put(bdev
, mode
| FMODE_EXCL
);
503 * always keep this in sync with compat_blkdev_ioctl()
505 int blkdev_ioctl(struct block_device
*bdev
, fmode_t mode
, unsigned cmd
,
508 void __user
*argp
= (void __user
*)arg
;
510 unsigned int max_sectors
;
514 return blkdev_flushbuf(bdev
, mode
, cmd
, arg
);
516 return blkdev_roset(bdev
, mode
, cmd
, arg
);
518 return blk_ioctl_discard(bdev
, mode
, arg
, 0);
520 return blk_ioctl_discard(bdev
, mode
, arg
,
521 BLKDEV_DISCARD_SECURE
);
523 return blk_ioctl_zeroout(bdev
, mode
, arg
);
525 return blkdev_report_zones_ioctl(bdev
, mode
, cmd
, arg
);
527 return blkdev_reset_zones_ioctl(bdev
, mode
, cmd
, arg
);
529 return blkdev_getgeo(bdev
, argp
);
534 return put_long(arg
, (bdev
->bd_bdi
->ra_pages
*PAGE_SIZE
) / 512);
536 return put_int(arg
, bdev_read_only(bdev
) != 0);
537 case BLKBSZGET
: /* get block device soft block size (cf. BLKSSZGET) */
538 return put_int(arg
, block_size(bdev
));
539 case BLKSSZGET
: /* get block device logical block size */
540 return put_int(arg
, bdev_logical_block_size(bdev
));
541 case BLKPBSZGET
: /* get block device physical block size */
542 return put_uint(arg
, bdev_physical_block_size(bdev
));
544 return put_uint(arg
, bdev_io_min(bdev
));
546 return put_uint(arg
, bdev_io_opt(bdev
));
548 return put_int(arg
, bdev_alignment_offset(bdev
));
549 case BLKDISCARDZEROES
:
550 return put_uint(arg
, 0);
552 max_sectors
= min_t(unsigned int, USHRT_MAX
,
553 queue_max_sectors(bdev_get_queue(bdev
)));
554 return put_ushort(arg
, max_sectors
);
556 return put_ushort(arg
, !blk_queue_nonrot(bdev_get_queue(bdev
)));
559 if(!capable(CAP_SYS_ADMIN
))
561 bdev
->bd_bdi
->ra_pages
= (arg
* 512) / PAGE_SIZE
;
564 return blkdev_bszset(bdev
, mode
, argp
);
566 return blkpg_ioctl(bdev
, argp
);
568 return blkdev_reread_part(bdev
);
570 size
= i_size_read(bdev
->bd_inode
);
571 if ((size
>> 9) > ~0UL)
573 return put_ulong(arg
, size
>> 9);
575 return put_u64(arg
, i_size_read(bdev
->bd_inode
));
579 case BLKTRACETEARDOWN
:
580 return blk_trace_ioctl(bdev
, cmd
, argp
);
581 case IOC_PR_REGISTER
:
582 return blkdev_pr_register(bdev
, argp
);
584 return blkdev_pr_reserve(bdev
, argp
);
586 return blkdev_pr_release(bdev
, argp
);
588 return blkdev_pr_preempt(bdev
, argp
, false);
589 case IOC_PR_PREEMPT_ABORT
:
590 return blkdev_pr_preempt(bdev
, argp
, true);
592 return blkdev_pr_clear(bdev
, argp
);
594 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
597 EXPORT_SYMBOL_GPL(blkdev_ioctl
);