]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - block/ioctl.c
1 #include <linux/capability.h>
2 #include <linux/blkdev.h>
3 #include <linux/blkpg.h>
4 #include <linux/hdreg.h>
5 #include <linux/backing-dev.h>
6 #include <linux/buffer_head.h>
7 #include <linux/smp_lock.h>
8 #include <linux/blktrace_api.h>
9 #include <asm/uaccess.h>
11 static int blkpg_ioctl(struct block_device
*bdev
, struct blkpg_ioctl_arg __user
*arg
)
13 struct block_device
*bdevp
;
15 struct hd_struct
*part
;
16 struct blkpg_ioctl_arg a
;
17 struct blkpg_partition p
;
18 struct disk_part_iter piter
;
19 long long start
, length
;
23 if (!capable(CAP_SYS_ADMIN
))
25 if (copy_from_user(&a
, arg
, sizeof(struct blkpg_ioctl_arg
)))
27 if (copy_from_user(&p
, a
.data
, sizeof(struct blkpg_partition
)))
30 if (bdev
!= bdev
->bd_contains
)
36 case BLKPG_ADD_PARTITION
:
38 length
= p
.length
>> 9;
39 /* check for fit in a hd_struct */
40 if (sizeof(sector_t
) == sizeof(long) &&
41 sizeof(long long) > sizeof(long)) {
42 long pstart
= start
, plength
= length
;
43 if (pstart
!= start
|| plength
!= length
44 || pstart
< 0 || plength
< 0)
48 mutex_lock(&bdev
->bd_mutex
);
51 disk_part_iter_init(&piter
, disk
,
52 DISK_PITER_INCL_EMPTY
);
53 while ((part
= disk_part_iter_next(&piter
))) {
54 if (!(start
+ length
<= part
->start_sect
||
55 start
>= part
->start_sect
+ part
->nr_sects
)) {
56 disk_part_iter_exit(&piter
);
57 mutex_unlock(&bdev
->bd_mutex
);
61 disk_part_iter_exit(&piter
);
64 err
= add_partition(disk
, partno
, start
, length
,
66 mutex_unlock(&bdev
->bd_mutex
);
68 case BLKPG_DEL_PARTITION
:
69 part
= disk_get_part(disk
, partno
);
73 bdevp
= bdget(part_devt(part
));
78 mutex_lock(&bdevp
->bd_mutex
);
79 if (bdevp
->bd_openers
) {
80 mutex_unlock(&bdevp
->bd_mutex
);
86 invalidate_bdev(bdevp
);
88 mutex_lock_nested(&bdev
->bd_mutex
, 1);
89 delete_partition(disk
, partno
);
90 mutex_unlock(&bdev
->bd_mutex
);
91 mutex_unlock(&bdevp
->bd_mutex
);
100 static int blkdev_reread_part(struct block_device
*bdev
)
102 struct gendisk
*disk
= bdev
->bd_disk
;
105 if (!disk_partitionable(disk
) || bdev
!= bdev
->bd_contains
)
107 if (!capable(CAP_SYS_ADMIN
))
109 if (!mutex_trylock(&bdev
->bd_mutex
))
111 res
= rescan_partitions(disk
, bdev
);
112 mutex_unlock(&bdev
->bd_mutex
);
116 static void blk_ioc_discard_endio(struct bio
*bio
, int err
)
119 if (err
== -EOPNOTSUPP
)
120 set_bit(BIO_EOPNOTSUPP
, &bio
->bi_flags
);
121 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
123 complete(bio
->bi_private
);
126 static int blk_ioctl_discard(struct block_device
*bdev
, uint64_t start
,
129 struct request_queue
*q
= bdev_get_queue(bdev
);
139 if (start
+ len
> (bdev
->bd_inode
->i_size
>> 9))
142 if (!q
->prepare_discard_fn
)
145 while (len
&& !ret
) {
146 DECLARE_COMPLETION_ONSTACK(wait
);
149 bio
= bio_alloc(GFP_KERNEL
, 0);
153 bio
->bi_end_io
= blk_ioc_discard_endio
;
155 bio
->bi_private
= &wait
;
156 bio
->bi_sector
= start
;
158 if (len
> q
->max_hw_sectors
) {
159 bio
->bi_size
= q
->max_hw_sectors
<< 9;
160 len
-= q
->max_hw_sectors
;
161 start
+= q
->max_hw_sectors
;
163 bio
->bi_size
= len
<< 9;
166 submit_bio(DISCARD_NOBARRIER
, bio
);
168 wait_for_completion(&wait
);
170 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
172 else if (!bio_flagged(bio
, BIO_UPTODATE
))
179 static int put_ushort(unsigned long arg
, unsigned short val
)
181 return put_user(val
, (unsigned short __user
*)arg
);
184 static int put_int(unsigned long arg
, int val
)
186 return put_user(val
, (int __user
*)arg
);
189 static int put_long(unsigned long arg
, long val
)
191 return put_user(val
, (long __user
*)arg
);
194 static int put_ulong(unsigned long arg
, unsigned long val
)
196 return put_user(val
, (unsigned long __user
*)arg
);
199 static int put_u64(unsigned long arg
, u64 val
)
201 return put_user(val
, (u64 __user
*)arg
);
204 static int blkdev_locked_ioctl(struct file
*file
, struct block_device
*bdev
,
205 unsigned cmd
, unsigned long arg
)
207 struct backing_dev_info
*bdi
;
215 bdi
= blk_get_backing_dev_info(bdev
);
218 return put_long(arg
, (bdi
->ra_pages
* PAGE_CACHE_SIZE
) / 512);
220 return put_int(arg
, bdev_read_only(bdev
) != 0);
221 case BLKBSZGET
: /* get the logical block size (cf. BLKSSZGET) */
222 return put_int(arg
, block_size(bdev
));
223 case BLKSSZGET
: /* get block device hardware sector size */
224 return put_int(arg
, bdev_hardsect_size(bdev
));
226 return put_ushort(arg
, bdev_get_queue(bdev
)->max_sectors
);
229 if(!capable(CAP_SYS_ADMIN
))
231 bdi
= blk_get_backing_dev_info(bdev
);
234 bdi
->ra_pages
= (arg
* 512) / PAGE_CACHE_SIZE
;
237 /* set the logical block size */
238 if (!capable(CAP_SYS_ADMIN
))
242 if (get_user(n
, (int __user
*) arg
))
244 if (bd_claim(bdev
, file
) < 0)
246 ret
= set_blocksize(bdev
, n
);
250 return blkpg_ioctl(bdev
, (struct blkpg_ioctl_arg __user
*) arg
);
252 return blkdev_reread_part(bdev
);
254 if ((bdev
->bd_inode
->i_size
>> 9) > ~0UL)
256 return put_ulong(arg
, bdev
->bd_inode
->i_size
>> 9);
258 return put_u64(arg
, bdev
->bd_inode
->i_size
);
262 case BLKTRACETEARDOWN
:
263 return blk_trace_ioctl(bdev
, cmd
, (char __user
*) arg
);
268 int blkdev_driver_ioctl(struct inode
*inode
, struct file
*file
,
269 struct gendisk
*disk
, unsigned cmd
, unsigned long arg
)
272 if (disk
->fops
->unlocked_ioctl
)
273 return disk
->fops
->unlocked_ioctl(file
, cmd
, arg
);
275 if (disk
->fops
->ioctl
) {
277 ret
= disk
->fops
->ioctl(inode
, file
, cmd
, arg
);
284 EXPORT_SYMBOL_GPL(blkdev_driver_ioctl
);
286 int __blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
287 unsigned cmd
, unsigned long arg
)
289 struct gendisk
*disk
= bdev
->bd_disk
;
291 /* you bet it'll go away by the end of patch series */
292 struct file fake_file
= {};
293 struct dentry fake_dentry
= {};
294 fake_file
.f_mode
= mode
;
295 fake_file
.f_path
.dentry
= &fake_dentry
;
296 fake_dentry
.d_inode
= bdev
->bd_inode
;
298 if (disk
->fops
->unlocked_ioctl
)
299 return disk
->fops
->unlocked_ioctl(&fake_file
, cmd
, arg
);
301 if (disk
->fops
->ioctl
) {
303 ret
= disk
->fops
->ioctl(bdev
->bd_inode
, &fake_file
, cmd
, arg
);
311 * For the record: _GPL here is only because somebody decided to slap it
312 * on the previous export. Sheer idiocy, since it wasn't copyrightable
313 * at all and could be open-coded without any exports by anybody who cares.
315 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl
);
318 * always keep this in sync with compat_blkdev_ioctl() and
319 * compat_blkdev_locked_ioctl()
321 int blkdev_ioctl(struct inode
*inode
, struct file
*file
, unsigned cmd
,
324 struct block_device
*bdev
= inode
->i_bdev
;
325 struct gendisk
*disk
= bdev
->bd_disk
;
330 if (!capable(CAP_SYS_ADMIN
))
333 ret
= blkdev_driver_ioctl(inode
, file
, disk
, cmd
, arg
);
334 /* -EINVAL to handle old uncorrected drivers */
335 if (ret
!= -EINVAL
&& ret
!= -ENOTTY
)
340 invalidate_bdev(bdev
);
345 ret
= blkdev_driver_ioctl(inode
, file
, disk
, cmd
, arg
);
346 /* -EINVAL to handle old uncorrected drivers */
347 if (ret
!= -EINVAL
&& ret
!= -ENOTTY
)
349 if (!capable(CAP_SYS_ADMIN
))
351 if (get_user(n
, (int __user
*)(arg
)))
354 set_device_ro(bdev
, n
);
361 if (!(file
->f_mode
& FMODE_WRITE
))
364 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
367 return blk_ioctl_discard(bdev
, range
[0], range
[1]);
371 struct hd_geometry geo
;
375 if (!disk
->fops
->getgeo
)
379 * We need to set the startsect first, the driver may
380 * want to override it.
382 geo
.start
= get_start_sect(bdev
);
383 ret
= disk
->fops
->getgeo(bdev
, &geo
);
386 if (copy_to_user((struct hd_geometry __user
*)arg
, &geo
,
394 ret
= blkdev_locked_ioctl(file
, bdev
, cmd
, arg
);
396 if (ret
!= -ENOIOCTLCMD
)
399 return blkdev_driver_ioctl(inode
, file
, disk
, cmd
, arg
);
401 EXPORT_SYMBOL_GPL(blkdev_ioctl
);