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
;
21 if (!capable(CAP_SYS_ADMIN
))
23 if (copy_from_user(&a
, arg
, sizeof(struct blkpg_ioctl_arg
)))
25 if (copy_from_user(&p
, a
.data
, sizeof(struct blkpg_partition
)))
28 if (bdev
!= bdev
->bd_contains
)
31 if (part
<= 0 || part
>= disk
->minors
)
34 case BLKPG_ADD_PARTITION
:
36 length
= p
.length
>> 9;
37 /* check for fit in a hd_struct */
38 if (sizeof(sector_t
) == sizeof(long) &&
39 sizeof(long long) > sizeof(long)) {
40 long pstart
= start
, plength
= length
;
41 if (pstart
!= start
|| plength
!= length
42 || pstart
< 0 || plength
< 0)
45 /* partition number in use? */
46 mutex_lock(&bdev
->bd_mutex
);
47 if (disk
->part
[part
- 1]) {
48 mutex_unlock(&bdev
->bd_mutex
);
52 for (i
= 0; i
< disk
->minors
- 1; i
++) {
53 struct hd_struct
*s
= disk
->part
[i
];
57 if (!(start
+length
<= s
->start_sect
||
58 start
>= s
->start_sect
+ s
->nr_sects
)) {
59 mutex_unlock(&bdev
->bd_mutex
);
64 add_partition(disk
, part
, start
, length
, ADDPART_FLAG_NONE
);
65 mutex_unlock(&bdev
->bd_mutex
);
67 case BLKPG_DEL_PARTITION
:
68 if (!disk
->part
[part
-1])
70 if (disk
->part
[part
- 1]->nr_sects
== 0)
72 bdevp
= bdget_disk(disk
, part
);
75 mutex_lock(&bdevp
->bd_mutex
);
76 if (bdevp
->bd_openers
) {
77 mutex_unlock(&bdevp
->bd_mutex
);
83 invalidate_bdev(bdevp
);
85 mutex_lock_nested(&bdev
->bd_mutex
, 1);
86 delete_partition(disk
, part
);
87 mutex_unlock(&bdev
->bd_mutex
);
88 mutex_unlock(&bdevp
->bd_mutex
);
97 static int blkdev_reread_part(struct block_device
*bdev
)
99 struct gendisk
*disk
= bdev
->bd_disk
;
102 if (disk
->minors
== 1 || bdev
!= bdev
->bd_contains
)
104 if (!capable(CAP_SYS_ADMIN
))
106 if (!mutex_trylock(&bdev
->bd_mutex
))
108 res
= rescan_partitions(disk
, bdev
);
109 mutex_unlock(&bdev
->bd_mutex
);
113 static int put_ushort(unsigned long arg
, unsigned short val
)
115 return put_user(val
, (unsigned short __user
*)arg
);
118 static int put_int(unsigned long arg
, int val
)
120 return put_user(val
, (int __user
*)arg
);
123 static int put_long(unsigned long arg
, long val
)
125 return put_user(val
, (long __user
*)arg
);
128 static int put_ulong(unsigned long arg
, unsigned long val
)
130 return put_user(val
, (unsigned long __user
*)arg
);
133 static int put_u64(unsigned long arg
, u64 val
)
135 return put_user(val
, (u64 __user
*)arg
);
138 static int blkdev_locked_ioctl(struct file
*file
, struct block_device
*bdev
,
139 unsigned cmd
, unsigned long arg
)
141 struct backing_dev_info
*bdi
;
149 bdi
= blk_get_backing_dev_info(bdev
);
152 return put_long(arg
, (bdi
->ra_pages
* PAGE_CACHE_SIZE
) / 512);
154 return put_int(arg
, bdev_read_only(bdev
) != 0);
155 case BLKBSZGET
: /* get the logical block size (cf. BLKSSZGET) */
156 return put_int(arg
, block_size(bdev
));
157 case BLKSSZGET
: /* get block device hardware sector size */
158 return put_int(arg
, bdev_hardsect_size(bdev
));
160 return put_ushort(arg
, bdev_get_queue(bdev
)->max_sectors
);
163 if(!capable(CAP_SYS_ADMIN
))
165 bdi
= blk_get_backing_dev_info(bdev
);
168 bdi
->ra_pages
= (arg
* 512) / PAGE_CACHE_SIZE
;
171 /* set the logical block size */
172 if (!capable(CAP_SYS_ADMIN
))
176 if (get_user(n
, (int __user
*) arg
))
178 if (bd_claim(bdev
, file
) < 0)
180 ret
= set_blocksize(bdev
, n
);
184 return blkpg_ioctl(bdev
, (struct blkpg_ioctl_arg __user
*) arg
);
186 return blkdev_reread_part(bdev
);
188 if ((bdev
->bd_inode
->i_size
>> 9) > ~0UL)
190 return put_ulong(arg
, bdev
->bd_inode
->i_size
>> 9);
192 return put_u64(arg
, bdev
->bd_inode
->i_size
);
196 case BLKTRACETEARDOWN
:
197 return blk_trace_ioctl(bdev
, cmd
, (char __user
*) arg
);
202 int blkdev_driver_ioctl(struct inode
*inode
, struct file
*file
,
203 struct gendisk
*disk
, unsigned cmd
, unsigned long arg
)
206 if (disk
->fops
->unlocked_ioctl
)
207 return disk
->fops
->unlocked_ioctl(file
, cmd
, arg
);
209 if (disk
->fops
->ioctl
) {
211 ret
= disk
->fops
->ioctl(inode
, file
, cmd
, arg
);
218 EXPORT_SYMBOL_GPL(blkdev_driver_ioctl
);
220 int blkdev_ioctl(struct inode
*inode
, struct file
*file
, unsigned cmd
,
223 struct block_device
*bdev
= inode
->i_bdev
;
224 struct gendisk
*disk
= bdev
->bd_disk
;
229 if (!capable(CAP_SYS_ADMIN
))
232 ret
= blkdev_driver_ioctl(inode
, file
, disk
, cmd
, arg
);
233 /* -EINVAL to handle old uncorrected drivers */
234 if (ret
!= -EINVAL
&& ret
!= -ENOTTY
)
239 invalidate_bdev(bdev
);
244 ret
= blkdev_driver_ioctl(inode
, file
, disk
, cmd
, arg
);
245 /* -EINVAL to handle old uncorrected drivers */
246 if (ret
!= -EINVAL
&& ret
!= -ENOTTY
)
248 if (!capable(CAP_SYS_ADMIN
))
250 if (get_user(n
, (int __user
*)(arg
)))
253 set_device_ro(bdev
, n
);
257 struct hd_geometry geo
;
261 if (!disk
->fops
->getgeo
)
265 * We need to set the startsect first, the driver may
266 * want to override it.
268 geo
.start
= get_start_sect(bdev
);
269 ret
= disk
->fops
->getgeo(bdev
, &geo
);
272 if (copy_to_user((struct hd_geometry __user
*)arg
, &geo
,
280 ret
= blkdev_locked_ioctl(file
, bdev
, cmd
, arg
);
282 if (ret
!= -ENOIOCTLCMD
)
285 return blkdev_driver_ioctl(inode
, file
, disk
, cmd
, arg
);
288 /* Most of the generic ioctls are handled in the normal fallback path.
289 This assumes the blkdev's low level compat_ioctl always returns
290 ENOIOCTLCMD for unknown ioctls. */
291 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
293 struct block_device
*bdev
= file
->f_path
.dentry
->d_inode
->i_bdev
;
294 struct gendisk
*disk
= bdev
->bd_disk
;
295 int ret
= -ENOIOCTLCMD
;
296 if (disk
->fops
->compat_ioctl
) {
298 ret
= disk
->fops
->compat_ioctl(file
, cmd
, arg
);
304 EXPORT_SYMBOL_GPL(blkdev_ioctl
);