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 void blk_ioc_discard_endio(struct bio *bio, int err)
117 if (err == -EOPNOTSUPP)
118 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
119 clear_bit(BIO_UPTODATE, &bio->bi_flags);
121 complete(bio->bi_private);
124 static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
127 struct request_queue *q = bdev_get_queue(bdev);
137 if (start + len > (bdev->bd_inode->i_size >> 9))
140 if (!q->prepare_discard_fn)
143 while (len && !ret) {
144 DECLARE_COMPLETION_ONSTACK(wait);
147 bio = bio_alloc(GFP_KERNEL, 0);
151 bio->bi_end_io = blk_ioc_discard_endio;
153 bio->bi_private = &wait;
154 bio->bi_sector = start;
156 if (len > q->max_hw_sectors) {
157 bio->bi_size = q->max_hw_sectors << 9;
158 len -= q->max_hw_sectors;
159 start += q->max_hw_sectors;
161 bio->bi_size = len << 9;
164 submit_bio(DISCARD_NOBARRIER, bio);
166 wait_for_completion(&wait);
168 if (bio_flagged(bio, BIO_EOPNOTSUPP))
170 else if (!bio_flagged(bio, BIO_UPTODATE))
177 static int put_ushort(unsigned long arg, unsigned short val)
179 return put_user(val, (unsigned short __user *)arg);
182 static int put_int(unsigned long arg, int val)
184 return put_user(val, (int __user *)arg);
187 static int put_long(unsigned long arg, long val)
189 return put_user(val, (long __user *)arg);
192 static int put_ulong(unsigned long arg, unsigned long val)
194 return put_user(val, (unsigned long __user *)arg);
197 static int put_u64(unsigned long arg, u64 val)
199 return put_user(val, (u64 __user *)arg);
202 static int blkdev_locked_ioctl(struct file *file, struct block_device *bdev,
203 unsigned cmd, unsigned long arg)
205 struct backing_dev_info *bdi;
213 bdi = blk_get_backing_dev_info(bdev);
216 return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
218 return put_int(arg, bdev_read_only(bdev) != 0);
219 case BLKBSZGET: /* get the logical block size (cf. BLKSSZGET) */
220 return put_int(arg, block_size(bdev));
221 case BLKSSZGET: /* get block device hardware sector size */
222 return put_int(arg, bdev_hardsect_size(bdev));
224 return put_ushort(arg, bdev_get_queue(bdev)->max_sectors);
227 if(!capable(CAP_SYS_ADMIN))
229 bdi = blk_get_backing_dev_info(bdev);
232 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
235 /* set the logical block size */
236 if (!capable(CAP_SYS_ADMIN))
240 if (get_user(n, (int __user *) arg))
242 if (bd_claim(bdev, file) < 0)
244 ret = set_blocksize(bdev, n);
248 return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
250 return blkdev_reread_part(bdev);
252 if ((bdev->bd_inode->i_size >> 9) > ~0UL)
254 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
256 return put_u64(arg, bdev->bd_inode->i_size);
260 case BLKTRACETEARDOWN:
261 return blk_trace_ioctl(bdev, cmd, (char __user *) arg);
266 int blkdev_driver_ioctl(struct inode *inode, struct file *file,
267 struct gendisk *disk, unsigned cmd, unsigned long arg)
270 if (disk->fops->unlocked_ioctl)
271 return disk->fops->unlocked_ioctl(file, cmd, arg);
273 if (disk->fops->ioctl) {
275 ret = disk->fops->ioctl(inode, file, cmd, arg);
282 EXPORT_SYMBOL_GPL(blkdev_driver_ioctl);
285 * always keep this in sync with compat_blkdev_ioctl() and
286 * compat_blkdev_locked_ioctl()
288 int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
291 struct block_device *bdev = inode->i_bdev;
292 struct gendisk *disk = bdev->bd_disk;
297 if (!capable(CAP_SYS_ADMIN))
300 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
301 /* -EINVAL to handle old uncorrected drivers */
302 if (ret != -EINVAL && ret != -ENOTTY)
307 invalidate_bdev(bdev);
312 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
313 /* -EINVAL to handle old uncorrected drivers */
314 if (ret != -EINVAL && ret != -ENOTTY)
316 if (!capable(CAP_SYS_ADMIN))
318 if (get_user(n, (int __user *)(arg)))
321 set_device_ro(bdev, n);
328 if (!(file->f_mode & FMODE_WRITE))
331 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
334 return blk_ioctl_discard(bdev, range[0], range[1]);
338 struct hd_geometry geo;
342 if (!disk->fops->getgeo)
346 * We need to set the startsect first, the driver may
347 * want to override it.
349 geo.start = get_start_sect(bdev);
350 ret = disk->fops->getgeo(bdev, &geo);
353 if (copy_to_user((struct hd_geometry __user *)arg, &geo,
361 ret = blkdev_locked_ioctl(file, bdev, cmd, arg);
363 if (ret != -ENOIOCTLCMD)
366 return blkdev_driver_ioctl(inode, file, disk, cmd, arg);
368 EXPORT_SYMBOL_GPL(blkdev_ioctl);