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 blkpg_ioctl_arg a
;
16 struct blkpg_partition p
;
17 long long start
, length
;
22 if (!capable(CAP_SYS_ADMIN
))
24 if (copy_from_user(&a
, arg
, sizeof(struct blkpg_ioctl_arg
)))
26 if (copy_from_user(&p
, a
.data
, sizeof(struct blkpg_partition
)))
29 if (bdev
!= bdev
->bd_contains
)
32 if (part
<= 0 || part
>= disk
->minors
)
35 case BLKPG_ADD_PARTITION
:
37 length
= p
.length
>> 9;
38 /* check for fit in a hd_struct */
39 if (sizeof(sector_t
) == sizeof(long) &&
40 sizeof(long long) > sizeof(long)) {
41 long pstart
= start
, plength
= length
;
42 if (pstart
!= start
|| plength
!= length
43 || pstart
< 0 || plength
< 0)
46 /* partition number in use? */
47 mutex_lock(&bdev
->bd_mutex
);
48 if (disk
->part
[part
- 1]) {
49 mutex_unlock(&bdev
->bd_mutex
);
53 for (i
= 0; i
< disk
->minors
- 1; i
++) {
54 struct hd_struct
*s
= disk
->part
[i
];
58 if (!(start
+length
<= s
->start_sect
||
59 start
>= s
->start_sect
+ s
->nr_sects
)) {
60 mutex_unlock(&bdev
->bd_mutex
);
65 err
= add_partition(disk
, part
, start
, length
, ADDPART_FLAG_NONE
);
66 mutex_unlock(&bdev
->bd_mutex
);
68 case BLKPG_DEL_PARTITION
:
69 if (!disk
->part
[part
-1])
71 if (disk
->part
[part
- 1]->nr_sects
== 0)
73 bdevp
= bdget_disk(disk
, part
);
76 mutex_lock(&bdevp
->bd_mutex
);
77 if (bdevp
->bd_openers
) {
78 mutex_unlock(&bdevp
->bd_mutex
);
84 invalidate_bdev(bdevp
);
86 mutex_lock_nested(&bdev
->bd_mutex
, 1);
87 delete_partition(disk
, part
);
88 mutex_unlock(&bdev
->bd_mutex
);
89 mutex_unlock(&bdevp
->bd_mutex
);
98 static int blkdev_reread_part(struct block_device
*bdev
)
100 struct gendisk
*disk
= bdev
->bd_disk
;
103 if (disk
->minors
== 1 || bdev
!= bdev
->bd_contains
)
105 if (!capable(CAP_SYS_ADMIN
))
107 if (!mutex_trylock(&bdev
->bd_mutex
))
109 res
= rescan_partitions(disk
, bdev
);
110 mutex_unlock(&bdev
->bd_mutex
);
114 static int put_ushort(unsigned long arg
, unsigned short val
)
116 return put_user(val
, (unsigned short __user
*)arg
);
119 static int put_int(unsigned long arg
, int val
)
121 return put_user(val
, (int __user
*)arg
);
124 static int put_long(unsigned long arg
, long val
)
126 return put_user(val
, (long __user
*)arg
);
129 static int put_ulong(unsigned long arg
, unsigned long val
)
131 return put_user(val
, (unsigned long __user
*)arg
);
134 static int put_u64(unsigned long arg
, u64 val
)
136 return put_user(val
, (u64 __user
*)arg
);
139 static int blkdev_locked_ioctl(struct file
*file
, struct block_device
*bdev
,
140 unsigned cmd
, unsigned long arg
)
142 struct backing_dev_info
*bdi
;
150 bdi
= blk_get_backing_dev_info(bdev
);
153 return put_long(arg
, (bdi
->ra_pages
* PAGE_CACHE_SIZE
) / 512);
155 return put_int(arg
, bdev_read_only(bdev
) != 0);
156 case BLKBSZGET
: /* get the logical block size (cf. BLKSSZGET) */
157 return put_int(arg
, block_size(bdev
));
158 case BLKSSZGET
: /* get block device hardware sector size */
159 return put_int(arg
, bdev_hardsect_size(bdev
));
161 return put_ushort(arg
, bdev_get_queue(bdev
)->max_sectors
);
164 if(!capable(CAP_SYS_ADMIN
))
166 bdi
= blk_get_backing_dev_info(bdev
);
169 bdi
->ra_pages
= (arg
* 512) / PAGE_CACHE_SIZE
;
172 /* set the logical block size */
173 if (!capable(CAP_SYS_ADMIN
))
177 if (get_user(n
, (int __user
*) arg
))
179 if (bd_claim(bdev
, file
) < 0)
181 ret
= set_blocksize(bdev
, n
);
185 return blkpg_ioctl(bdev
, (struct blkpg_ioctl_arg __user
*) arg
);
187 return blkdev_reread_part(bdev
);
189 if ((bdev
->bd_inode
->i_size
>> 9) > ~0UL)
191 return put_ulong(arg
, bdev
->bd_inode
->i_size
>> 9);
193 return put_u64(arg
, bdev
->bd_inode
->i_size
);
197 case BLKTRACETEARDOWN
:
198 return blk_trace_ioctl(bdev
, cmd
, (char __user
*) arg
);
203 int blkdev_driver_ioctl(struct inode
*inode
, struct file
*file
,
204 struct gendisk
*disk
, unsigned cmd
, unsigned long arg
)
207 if (disk
->fops
->unlocked_ioctl
)
208 return disk
->fops
->unlocked_ioctl(file
, cmd
, arg
);
210 if (disk
->fops
->ioctl
) {
212 ret
= disk
->fops
->ioctl(inode
, file
, cmd
, arg
);
219 EXPORT_SYMBOL_GPL(blkdev_driver_ioctl
);
222 * always keep this in sync with compat_blkdev_ioctl() and
223 * compat_blkdev_locked_ioctl()
225 int blkdev_ioctl(struct inode
*inode
, struct file
*file
, unsigned cmd
,
228 struct block_device
*bdev
= inode
->i_bdev
;
229 struct gendisk
*disk
= bdev
->bd_disk
;
234 if (!capable(CAP_SYS_ADMIN
))
237 ret
= blkdev_driver_ioctl(inode
, file
, disk
, cmd
, arg
);
238 /* -EINVAL to handle old uncorrected drivers */
239 if (ret
!= -EINVAL
&& ret
!= -ENOTTY
)
244 invalidate_bdev(bdev
);
249 ret
= blkdev_driver_ioctl(inode
, file
, disk
, cmd
, arg
);
250 /* -EINVAL to handle old uncorrected drivers */
251 if (ret
!= -EINVAL
&& ret
!= -ENOTTY
)
253 if (!capable(CAP_SYS_ADMIN
))
255 if (get_user(n
, (int __user
*)(arg
)))
258 set_device_ro(bdev
, n
);
262 struct hd_geometry geo
;
266 if (!disk
->fops
->getgeo
)
270 * We need to set the startsect first, the driver may
271 * want to override it.
273 geo
.start
= get_start_sect(bdev
);
274 ret
= disk
->fops
->getgeo(bdev
, &geo
);
277 if (copy_to_user((struct hd_geometry __user
*)arg
, &geo
,
285 ret
= blkdev_locked_ioctl(file
, bdev
, cmd
, arg
);
287 if (ret
!= -ENOIOCTLCMD
)
290 return blkdev_driver_ioctl(inode
, file
, disk
, cmd
, arg
);
292 EXPORT_SYMBOL_GPL(blkdev_ioctl
);