blob: 58aab630dfc1a55eaf06975d5a48600b5f14e49e [file] [log] [blame]
Randy.Dunlapc59ede72006-01-11 12:17:46 -08001#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/blkdev.h>
3#include <linux/blkpg.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/backing-dev.h>
6#include <linux/buffer_head.h>
7#include <linux/smp_lock.h>
Jens Axboe2056a782006-03-23 20:00:26 +01008#include <linux/blktrace_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/uaccess.h>
10
11static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
12{
13 struct block_device *bdevp;
14 struct gendisk *disk;
15 struct blkpg_ioctl_arg a;
16 struct blkpg_partition p;
17 long long start, length;
18 int part;
19 int i;
20
21 if (!capable(CAP_SYS_ADMIN))
22 return -EACCES;
23 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
24 return -EFAULT;
25 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
26 return -EFAULT;
27 disk = bdev->bd_disk;
28 if (bdev != bdev->bd_contains)
29 return -EINVAL;
30 part = p.pno;
31 if (part <= 0 || part >= disk->minors)
32 return -EINVAL;
33 switch (a.op) {
34 case BLKPG_ADD_PARTITION:
35 start = p.start >> 9;
36 length = p.length >> 9;
37 /* check for fit in a hd_struct */
38 if (sizeof(sector_t) == sizeof(long) &&
39 sizeof(long long) > sizeof(long)) {
40 long pstart = start, plength = length;
41 if (pstart != start || plength != length
42 || pstart < 0 || plength < 0)
43 return -EINVAL;
44 }
45 /* partition number in use? */
Arjan van de Venc039e312006-03-23 03:00:28 -080046 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (disk->part[part - 1]) {
Arjan van de Venc039e312006-03-23 03:00:28 -080048 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return -EBUSY;
50 }
51 /* overlap? */
52 for (i = 0; i < disk->minors - 1; i++) {
53 struct hd_struct *s = disk->part[i];
54
55 if (!s)
56 continue;
57 if (!(start+length <= s->start_sect ||
58 start >= s->start_sect + s->nr_sects)) {
Arjan van de Venc039e312006-03-23 03:00:28 -080059 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return -EBUSY;
61 }
62 }
63 /* all seems OK */
64 add_partition(disk, part, start, length);
Arjan van de Venc039e312006-03-23 03:00:28 -080065 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
67 case BLKPG_DEL_PARTITION:
68 if (!disk->part[part-1])
69 return -ENXIO;
70 if (disk->part[part - 1]->nr_sects == 0)
71 return -ENXIO;
72 bdevp = bdget_disk(disk, part);
73 if (!bdevp)
74 return -ENOMEM;
Arjan van de Venddca60c2006-07-14 00:24:12 -070075 mutex_lock_nested(&bdevp->bd_mutex, BD_MUTEX_PARTITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (bdevp->bd_openers) {
Arjan van de Venc039e312006-03-23 03:00:28 -080077 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 bdput(bdevp);
79 return -EBUSY;
80 }
81 /* all seems OK */
82 fsync_bdev(bdevp);
83 invalidate_bdev(bdevp, 0);
84
Arjan van de Venddca60c2006-07-14 00:24:12 -070085 mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_WHOLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 delete_partition(disk, part);
Arjan van de Venc039e312006-03-23 03:00:28 -080087 mutex_unlock(&bdev->bd_mutex);
88 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 bdput(bdevp);
90
91 return 0;
92 default:
93 return -EINVAL;
94 }
95}
96
97static int blkdev_reread_part(struct block_device *bdev)
98{
99 struct gendisk *disk = bdev->bd_disk;
100 int res;
101
102 if (disk->minors == 1 || bdev != bdev->bd_contains)
103 return -EINVAL;
104 if (!capable(CAP_SYS_ADMIN))
105 return -EACCES;
Arjan van de Venc039e312006-03-23 03:00:28 -0800106 if (!mutex_trylock(&bdev->bd_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -EBUSY;
108 res = rescan_partitions(disk, bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800109 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return res;
111}
112
113static int put_ushort(unsigned long arg, unsigned short val)
114{
115 return put_user(val, (unsigned short __user *)arg);
116}
117
118static int put_int(unsigned long arg, int val)
119{
120 return put_user(val, (int __user *)arg);
121}
122
123static int put_long(unsigned long arg, long val)
124{
125 return put_user(val, (long __user *)arg);
126}
127
128static int put_ulong(unsigned long arg, unsigned long val)
129{
130 return put_user(val, (unsigned long __user *)arg);
131}
132
133static int put_u64(unsigned long arg, u64 val)
134{
135 return put_user(val, (u64 __user *)arg);
136}
137
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700138static int blkdev_locked_ioctl(struct file *file, struct block_device *bdev,
139 unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct backing_dev_info *bdi;
142 int ret, n;
143
144 switch (cmd) {
145 case BLKRAGET:
146 case BLKFRAGET:
147 if (!arg)
148 return -EINVAL;
149 bdi = blk_get_backing_dev_info(bdev);
150 if (bdi == NULL)
151 return -ENOTTY;
152 return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
153 case BLKROGET:
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));
159 case BLKSECTGET:
160 return put_ushort(arg, bdev_get_queue(bdev)->max_sectors);
161 case BLKRASET:
162 case BLKFRASET:
163 if(!capable(CAP_SYS_ADMIN))
164 return -EACCES;
165 bdi = blk_get_backing_dev_info(bdev);
166 if (bdi == NULL)
167 return -ENOTTY;
168 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
169 return 0;
170 case BLKBSZSET:
171 /* set the logical block size */
172 if (!capable(CAP_SYS_ADMIN))
173 return -EACCES;
174 if (!arg)
175 return -EINVAL;
176 if (get_user(n, (int __user *) arg))
177 return -EFAULT;
178 if (bd_claim(bdev, file) < 0)
179 return -EBUSY;
180 ret = set_blocksize(bdev, n);
181 bd_release(bdev);
182 return ret;
183 case BLKPG:
184 return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
185 case BLKRRPART:
186 return blkdev_reread_part(bdev);
187 case BLKGETSIZE:
188 if ((bdev->bd_inode->i_size >> 9) > ~0UL)
189 return -EFBIG;
190 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
191 case BLKGETSIZE64:
192 return put_u64(arg, bdev->bd_inode->i_size);
Jens Axboe2056a782006-03-23 20:00:26 +0100193 case BLKTRACESTART:
194 case BLKTRACESTOP:
195 case BLKTRACESETUP:
196 case BLKTRACETEARDOWN:
197 return blk_trace_ioctl(bdev, cmd, (char __user *) arg);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700198 }
199 return -ENOIOCTLCMD;
200}
201
Alasdair G Kergon7006f6e2006-10-03 01:15:21 -0700202int blkdev_driver_ioctl(struct inode *inode, struct file *file,
203 struct gendisk *disk, unsigned cmd, unsigned long arg)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700204{
205 int ret;
206 if (disk->fops->unlocked_ioctl)
207 return disk->fops->unlocked_ioctl(file, cmd, arg);
208
209 if (disk->fops->ioctl) {
210 lock_kernel();
211 ret = disk->fops->ioctl(inode, file, cmd, arg);
212 unlock_kernel();
213 return ret;
214 }
215
216 return -ENOTTY;
217}
Alasdair G Kergon7006f6e2006-10-03 01:15:21 -0700218EXPORT_SYMBOL_GPL(blkdev_driver_ioctl);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700219
220int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
221 unsigned long arg)
222{
223 struct block_device *bdev = inode->i_bdev;
224 struct gendisk *disk = bdev->bd_disk;
225 int ret, n;
226
227 switch(cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 case BLKFLSBUF:
229 if (!capable(CAP_SYS_ADMIN))
230 return -EACCES;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700231
232 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
233 /* -EINVAL to handle old uncorrected drivers */
234 if (ret != -EINVAL && ret != -ENOTTY)
235 return ret;
236
237 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 fsync_bdev(bdev);
239 invalidate_bdev(bdev, 0);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700240 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return 0;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 case BLKROSET:
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700244 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
245 /* -EINVAL to handle old uncorrected drivers */
246 if (ret != -EINVAL && ret != -ENOTTY)
247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!capable(CAP_SYS_ADMIN))
249 return -EACCES;
250 if (get_user(n, (int __user *)(arg)))
251 return -EFAULT;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700252 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 set_device_ro(bdev, n);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700254 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800256 case HDIO_GETGEO: {
257 struct hd_geometry geo;
258
259 if (!arg)
260 return -EINVAL;
261 if (!disk->fops->getgeo)
262 return -ENOTTY;
263
264 /*
265 * We need to set the startsect first, the driver may
266 * want to override it.
267 */
268 geo.start = get_start_sect(bdev);
269 ret = disk->fops->getgeo(bdev, &geo);
270 if (ret)
271 return ret;
272 if (copy_to_user((struct hd_geometry __user *)arg, &geo,
273 sizeof(geo)))
274 return -EFAULT;
275 return 0;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700278
279 lock_kernel();
280 ret = blkdev_locked_ioctl(file, bdev, cmd, arg);
281 unlock_kernel();
282 if (ret != -ENOIOCTLCMD)
283 return ret;
284
285 return blkdev_driver_ioctl(inode, file, disk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/* Most of the generic ioctls are handled in the normal fallback path.
289 This assumes the blkdev's low level compat_ioctl always returns
290 ENOIOCTLCMD for unknown ioctls. */
291long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
292{
293 struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
294 struct gendisk *disk = bdev->bd_disk;
295 int ret = -ENOIOCTLCMD;
296 if (disk->fops->compat_ioctl) {
297 lock_kernel();
298 ret = disk->fops->compat_ioctl(file, cmd, arg);
299 unlock_kernel();
300 }
301 return ret;
302}
Stephen Tweedie68f66fe2005-05-13 23:31:19 -0400303
304EXPORT_SYMBOL_GPL(blkdev_ioctl);