1 #include <linux/sched.h> /* for capable() */
2 #include <linux/blkdev.h>
3 #include <linux/blkpg.h>
4 #include <linux/backing-dev.h>
5 #include <linux/buffer_head.h>
6 #include <linux/smp_lock.h>
7 #include <asm/uaccess.h>
9 static int blkpg_ioctl(struct block_device
*bdev
, struct blkpg_ioctl_arg __user
*arg
)
11 struct block_device
*bdevp
;
13 struct blkpg_ioctl_arg a
;
14 struct blkpg_partition p
;
15 long long start
, length
;
19 if (!capable(CAP_SYS_ADMIN
))
21 if (copy_from_user(&a
, arg
, sizeof(struct blkpg_ioctl_arg
)))
23 if (copy_from_user(&p
, a
.data
, sizeof(struct blkpg_partition
)))
26 if (bdev
!= bdev
->bd_contains
)
29 if (part
<= 0 || part
>= disk
->minors
)
32 case BLKPG_ADD_PARTITION
:
34 length
= p
.length
>> 9;
35 /* check for fit in a hd_struct */
36 if (sizeof(sector_t
) == sizeof(long) &&
37 sizeof(long long) > sizeof(long)) {
38 long pstart
= start
, plength
= length
;
39 if (pstart
!= start
|| plength
!= length
40 || pstart
< 0 || plength
< 0)
43 /* partition number in use? */
45 if (disk
->part
[part
- 1]) {
50 for (i
= 0; i
< disk
->minors
- 1; i
++) {
51 struct hd_struct
*s
= disk
->part
[i
];
55 if (!(start
+length
<= s
->start_sect
||
56 start
>= s
->start_sect
+ s
->nr_sects
)) {
62 add_partition(disk
, part
, start
, length
);
65 case BLKPG_DEL_PARTITION
:
66 if (!disk
->part
[part
-1])
68 if (disk
->part
[part
- 1]->nr_sects
== 0)
70 bdevp
= bdget_disk(disk
, part
);
74 if (bdevp
->bd_openers
) {
81 invalidate_bdev(bdevp
, 0);
84 delete_partition(disk
, part
);
95 static int blkdev_reread_part(struct block_device
*bdev
)
97 struct gendisk
*disk
= bdev
->bd_disk
;
100 if (disk
->minors
== 1 || bdev
!= bdev
->bd_contains
)
102 if (!capable(CAP_SYS_ADMIN
))
104 if (down_trylock(&bdev
->bd_sem
))
106 res
= rescan_partitions(disk
, bdev
);
111 static int put_ushort(unsigned long arg
, unsigned short val
)
113 return put_user(val
, (unsigned short __user
*)arg
);
116 static int put_int(unsigned long arg
, int val
)
118 return put_user(val
, (int __user
*)arg
);
121 static int put_long(unsigned long arg
, long val
)
123 return put_user(val
, (long __user
*)arg
);
126 static int put_ulong(unsigned long arg
, unsigned long val
)
128 return put_user(val
, (unsigned long __user
*)arg
);
131 static int put_u64(unsigned long arg
, u64 val
)
133 return put_user(val
, (u64 __user
*)arg
);
136 int blkdev_ioctl(struct inode
*inode
, struct file
*file
, unsigned cmd
,
139 struct block_device
*bdev
= inode
->i_bdev
;
140 struct gendisk
*disk
= bdev
->bd_disk
;
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
);
194 if (!capable(CAP_SYS_ADMIN
))
196 if (disk
->fops
->ioctl
) {
197 ret
= disk
->fops
->ioctl(inode
, file
, cmd
, arg
);
198 /* -EINVAL to handle old uncorrected drivers */
199 if (ret
!= -EINVAL
&& ret
!= -ENOTTY
)
203 invalidate_bdev(bdev
, 0);
206 if (disk
->fops
->ioctl
) {
207 ret
= disk
->fops
->ioctl(inode
, file
, cmd
, arg
);
208 /* -EINVAL to handle old uncorrected drivers */
209 if (ret
!= -EINVAL
&& ret
!= -ENOTTY
)
212 if (!capable(CAP_SYS_ADMIN
))
214 if (get_user(n
, (int __user
*)(arg
)))
216 set_device_ro(bdev
, n
);
219 if (disk
->fops
->ioctl
)
220 return disk
->fops
->ioctl(inode
, file
, cmd
, arg
);
225 /* Most of the generic ioctls are handled in the normal fallback path.
226 This assumes the blkdev's low level compat_ioctl always returns
227 ENOIOCTLCMD for unknown ioctls. */
228 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
230 struct block_device
*bdev
= file
->f_dentry
->d_inode
->i_bdev
;
231 struct gendisk
*disk
= bdev
->bd_disk
;
232 int ret
= -ENOIOCTLCMD
;
233 if (disk
->fops
->compat_ioctl
) {
235 ret
= disk
->fops
->compat_ioctl(file
, cmd
, arg
);
241 EXPORT_SYMBOL_GPL(blkdev_ioctl
);