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 <asm/uaccess.h>
8 static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg *arg)
10 struct block_device *bdevp;
12 struct blkpg_ioctl_arg a;
13 struct blkpg_partition p;
14 long long start, length;
18 if (!capable(CAP_SYS_ADMIN))
20 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
22 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
25 if (bdev != bdev->bd_contains)
28 if (part <= 0 || part >= disk->minors)
31 case BLKPG_ADD_PARTITION:
33 length = p.length >> 9;
34 /* check for fit in a hd_struct */
35 if (sizeof(sector_t) == sizeof(long) &&
36 sizeof(long long) > sizeof(long)) {
37 long pstart = start, plength = length;
38 if (pstart != start || plength != length
39 || pstart < 0 || plength < 0)
42 /* partition number in use? */
44 if (disk->part[part - 1]) {
49 for (i = 0; i < disk->minors - 1; i++) {
50 struct hd_struct *s = disk->part[i];
54 if (!(start+length <= s->start_sect ||
55 start >= s->start_sect + s->nr_sects)) {
61 add_partition(disk, part, start, length);
64 case BLKPG_DEL_PARTITION:
65 if (!disk->part[part-1])
67 if (disk->part[part - 1]->nr_sects == 0)
69 bdevp = bdget_disk(disk, part);
73 if (bdevp->bd_openers) {
80 invalidate_bdev(bdevp, 0);
83 delete_partition(disk, part);
94 static int blkdev_reread_part(struct block_device *bdev)
96 struct gendisk *disk = bdev->bd_disk;
99 if (disk->minors == 1 || bdev != bdev->bd_contains)
101 if (!capable(CAP_SYS_ADMIN))
103 if (down_trylock(&bdev->bd_sem))
105 res = rescan_partitions(disk, bdev);
110 static int put_ushort(unsigned long arg, unsigned short val)
112 return put_user(val, (unsigned short *)arg);
115 static int put_int(unsigned long arg, int val)
117 return put_user(val, (int *)arg);
120 static int put_long(unsigned long arg, long val)
122 return put_user(val, (long *)arg);
125 static int put_ulong(unsigned long arg, unsigned long val)
127 return put_user(val, (unsigned long *)arg);
130 static int put_u64(unsigned long arg, u64 val)
132 return put_user(val, (u64 *)arg);
135 int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
138 struct block_device *bdev = inode->i_bdev;
139 struct gendisk *disk = bdev->bd_disk;
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 *) arg))
177 if (bd_claim(bdev, file) < 0)
179 ret = set_blocksize(bdev, n);
183 return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg *) 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);
193 if (!capable(CAP_SYS_ADMIN))
195 if (disk->fops->ioctl) {
196 ret = disk->fops->ioctl(inode, file, cmd, arg);
201 invalidate_bdev(bdev, 0);
204 if (disk->fops->ioctl) {
205 ret = disk->fops->ioctl(inode, file, cmd, arg);
209 if (!capable(CAP_SYS_ADMIN))
211 if (get_user(n, (int *)(arg)))
213 set_device_ro(bdev, n);
216 if (disk->fops->ioctl)
217 return disk->fops->ioctl(inode, file, cmd, arg);