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 <asm/uaccess.h>
10 static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
12 struct block_device *bdevp;
14 struct blkpg_ioctl_arg a;
15 struct blkpg_partition p;
16 long long start, length;
20 if (!capable(CAP_SYS_ADMIN))
22 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
24 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
27 if (bdev != bdev->bd_contains)
30 if (part <= 0 || part >= disk->minors)
33 case BLKPG_ADD_PARTITION:
35 length = p.length >> 9;
36 /* check for fit in a hd_struct */
37 if (sizeof(sector_t) == sizeof(long) &&
38 sizeof(long long) > sizeof(long)) {
39 long pstart = start, plength = length;
40 if (pstart != start || plength != length
41 || pstart < 0 || plength < 0)
44 /* partition number in use? */
46 if (disk->part[part - 1]) {
51 for (i = 0; i < disk->minors - 1; i++) {
52 struct hd_struct *s = disk->part[i];
56 if (!(start+length <= s->start_sect ||
57 start >= s->start_sect + s->nr_sects)) {
63 add_partition(disk, part, start, length);
66 case BLKPG_DEL_PARTITION:
67 if (!disk->part[part-1])
69 if (disk->part[part - 1]->nr_sects == 0)
71 bdevp = bdget_disk(disk, part);
75 if (bdevp->bd_openers) {
82 invalidate_bdev(bdevp, 0);
85 delete_partition(disk, part);
96 static int blkdev_reread_part(struct block_device *bdev)
98 struct gendisk *disk = bdev->bd_disk;
101 if (disk->minors == 1 || bdev != bdev->bd_contains)
103 if (!capable(CAP_SYS_ADMIN))
105 if (down_trylock(&bdev->bd_sem))
107 res = rescan_partitions(disk, bdev);
112 static int put_ushort(unsigned long arg, unsigned short val)
114 return put_user(val, (unsigned short __user *)arg);
117 static int put_int(unsigned long arg, int val)
119 return put_user(val, (int __user *)arg);
122 static int put_long(unsigned long arg, long val)
124 return put_user(val, (long __user *)arg);
127 static int put_ulong(unsigned long arg, unsigned long val)
129 return put_user(val, (unsigned long __user *)arg);
132 static int put_u64(unsigned long arg, u64 val)
134 return put_user(val, (u64 __user *)arg);
137 static int blkdev_locked_ioctl(struct file *file, struct block_device *bdev,
138 unsigned cmd, unsigned long arg)
140 struct backing_dev_info *bdi;
148 bdi = blk_get_backing_dev_info(bdev);
151 return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
153 return put_int(arg, bdev_read_only(bdev) != 0);
154 case BLKBSZGET: /* get the logical block size (cf. BLKSSZGET) */
155 return put_int(arg, block_size(bdev));
156 case BLKSSZGET: /* get block device hardware sector size */
157 return put_int(arg, bdev_hardsect_size(bdev));
159 return put_ushort(arg, bdev_get_queue(bdev)->max_sectors);
162 if(!capable(CAP_SYS_ADMIN))
164 bdi = blk_get_backing_dev_info(bdev);
167 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
170 /* set the logical block size */
171 if (!capable(CAP_SYS_ADMIN))
175 if (get_user(n, (int __user *) arg))
177 if (bd_claim(bdev, file) < 0)
179 ret = set_blocksize(bdev, n);
183 return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
185 return blkdev_reread_part(bdev);
187 if ((bdev->bd_inode->i_size >> 9) > ~0UL)
189 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
191 return put_u64(arg, bdev->bd_inode->i_size);
196 static int blkdev_driver_ioctl(struct inode *inode, struct file *file,
197 struct gendisk *disk, unsigned cmd, unsigned long arg)
200 if (disk->fops->unlocked_ioctl)
201 return disk->fops->unlocked_ioctl(file, cmd, arg);
203 if (disk->fops->ioctl) {
205 ret = disk->fops->ioctl(inode, file, cmd, arg);
213 int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
216 struct block_device *bdev = inode->i_bdev;
217 struct gendisk *disk = bdev->bd_disk;
222 if (!capable(CAP_SYS_ADMIN))
225 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
226 /* -EINVAL to handle old uncorrected drivers */
227 if (ret != -EINVAL && ret != -ENOTTY)
232 invalidate_bdev(bdev, 0);
237 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
238 /* -EINVAL to handle old uncorrected drivers */
239 if (ret != -EINVAL && ret != -ENOTTY)
241 if (!capable(CAP_SYS_ADMIN))
243 if (get_user(n, (int __user *)(arg)))
246 set_device_ro(bdev, n);
250 struct hd_geometry geo;
254 if (!disk->fops->getgeo)
258 * We need to set the startsect first, the driver may
259 * want to override it.
261 geo.start = get_start_sect(bdev);
262 ret = disk->fops->getgeo(bdev, &geo);
265 if (copy_to_user((struct hd_geometry __user *)arg, &geo,
273 ret = blkdev_locked_ioctl(file, bdev, cmd, arg);
275 if (ret != -ENOIOCTLCMD)
278 return blkdev_driver_ioctl(inode, file, disk, cmd, arg);
281 /* Most of the generic ioctls are handled in the normal fallback path.
282 This assumes the blkdev's low level compat_ioctl always returns
283 ENOIOCTLCMD for unknown ioctls. */
284 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
286 struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
287 struct gendisk *disk = bdev->bd_disk;
288 int ret = -ENOIOCTLCMD;
289 if (disk->fops->compat_ioctl) {
291 ret = disk->fops->compat_ioctl(file, cmd, arg);
297 EXPORT_SYMBOL_GPL(blkdev_ioctl);