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 static int blkdev_locked_ioctl(struct file *file, struct block_device *bdev,
137 unsigned cmd, unsigned long arg)
139 struct backing_dev_info *bdi;
147 bdi = blk_get_backing_dev_info(bdev);
150 return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
152 return put_int(arg, bdev_read_only(bdev) != 0);
153 case BLKBSZGET: /* get the logical block size (cf. BLKSSZGET) */
154 return put_int(arg, block_size(bdev));
155 case BLKSSZGET: /* get block device hardware sector size */
156 return put_int(arg, bdev_hardsect_size(bdev));
158 return put_ushort(arg, bdev_get_queue(bdev)->max_sectors);
161 if(!capable(CAP_SYS_ADMIN))
163 bdi = blk_get_backing_dev_info(bdev);
166 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
169 /* set the logical block size */
170 if (!capable(CAP_SYS_ADMIN))
174 if (get_user(n, (int __user *) arg))
176 if (bd_claim(bdev, file) < 0)
178 ret = set_blocksize(bdev, n);
182 return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
184 return blkdev_reread_part(bdev);
186 if ((bdev->bd_inode->i_size >> 9) > ~0UL)
188 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
190 return put_u64(arg, bdev->bd_inode->i_size);
195 static int blkdev_driver_ioctl(struct inode *inode, struct file *file,
196 struct gendisk *disk, unsigned cmd, unsigned long arg)
199 if (disk->fops->unlocked_ioctl)
200 return disk->fops->unlocked_ioctl(file, cmd, arg);
202 if (disk->fops->ioctl) {
204 ret = disk->fops->ioctl(inode, file, cmd, arg);
212 int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
215 struct block_device *bdev = inode->i_bdev;
216 struct gendisk *disk = bdev->bd_disk;
221 if (!capable(CAP_SYS_ADMIN))
224 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
225 /* -EINVAL to handle old uncorrected drivers */
226 if (ret != -EINVAL && ret != -ENOTTY)
231 invalidate_bdev(bdev, 0);
236 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
237 /* -EINVAL to handle old uncorrected drivers */
238 if (ret != -EINVAL && ret != -ENOTTY)
240 if (!capable(CAP_SYS_ADMIN))
242 if (get_user(n, (int __user *)(arg)))
245 set_device_ro(bdev, n);
251 ret = blkdev_locked_ioctl(file, bdev, cmd, arg);
253 if (ret != -ENOIOCTLCMD)
256 return blkdev_driver_ioctl(inode, file, disk, cmd, arg);
259 /* Most of the generic ioctls are handled in the normal fallback path.
260 This assumes the blkdev's low level compat_ioctl always returns
261 ENOIOCTLCMD for unknown ioctls. */
262 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
264 struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
265 struct gendisk *disk = bdev->bd_disk;
266 int ret = -ENOIOCTLCMD;
267 if (disk->fops->compat_ioctl) {
269 ret = disk->fops->compat_ioctl(file, cmd, arg);
275 EXPORT_SYMBOL_GPL(blkdev_ioctl);