blob: 77185e5c026a659e300c69b276de3c5ccbb7327b [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;
Abdel Benamrouche04ebd4a2008-07-25 01:48:26 -070020 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22 if (!capable(CAP_SYS_ADMIN))
23 return -EACCES;
24 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
25 return -EFAULT;
26 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
27 return -EFAULT;
28 disk = bdev->bd_disk;
29 if (bdev != bdev->bd_contains)
30 return -EINVAL;
31 part = p.pno;
32 if (part <= 0 || part >= disk->minors)
33 return -EINVAL;
34 switch (a.op) {
35 case BLKPG_ADD_PARTITION:
36 start = p.start >> 9;
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)
44 return -EINVAL;
45 }
46 /* partition number in use? */
Arjan van de Venc039e312006-03-23 03:00:28 -080047 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (disk->part[part - 1]) {
Arjan van de Venc039e312006-03-23 03:00:28 -080049 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return -EBUSY;
51 }
52 /* overlap? */
53 for (i = 0; i < disk->minors - 1; i++) {
54 struct hd_struct *s = disk->part[i];
55
56 if (!s)
57 continue;
58 if (!(start+length <= s->start_sect ||
59 start >= s->start_sect + s->nr_sects)) {
Arjan van de Venc039e312006-03-23 03:00:28 -080060 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return -EBUSY;
62 }
63 }
64 /* all seems OK */
Abdel Benamrouche04ebd4a2008-07-25 01:48:26 -070065 err = add_partition(disk, part, start, length, ADDPART_FLAG_NONE);
Arjan van de Venc039e312006-03-23 03:00:28 -080066 mutex_unlock(&bdev->bd_mutex);
Abdel Benamrouche04ebd4a2008-07-25 01:48:26 -070067 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 case BLKPG_DEL_PARTITION:
69 if (!disk->part[part-1])
70 return -ENXIO;
71 if (disk->part[part - 1]->nr_sects == 0)
72 return -ENXIO;
73 bdevp = bdget_disk(disk, part);
74 if (!bdevp)
75 return -ENOMEM;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -080076 mutex_lock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (bdevp->bd_openers) {
Arjan van de Venc039e312006-03-23 03:00:28 -080078 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 bdput(bdevp);
80 return -EBUSY;
81 }
82 /* all seems OK */
83 fsync_bdev(bdevp);
Peter Zijlstraf98393a2007-05-06 14:49:54 -070084 invalidate_bdev(bdevp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Peter Zijlstra6d740cd2007-02-20 13:58:18 -080086 mutex_lock_nested(&bdev->bd_mutex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 delete_partition(disk, part);
Arjan van de Venc039e312006-03-23 03:00:28 -080088 mutex_unlock(&bdev->bd_mutex);
89 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 bdput(bdevp);
91
92 return 0;
93 default:
94 return -EINVAL;
95 }
96}
97
98static int blkdev_reread_part(struct block_device *bdev)
99{
100 struct gendisk *disk = bdev->bd_disk;
101 int res;
102
103 if (disk->minors == 1 || bdev != bdev->bd_contains)
104 return -EINVAL;
105 if (!capable(CAP_SYS_ADMIN))
106 return -EACCES;
Arjan van de Venc039e312006-03-23 03:00:28 -0800107 if (!mutex_trylock(&bdev->bd_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -EBUSY;
109 res = rescan_partitions(disk, bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800110 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return res;
112}
113
114static int put_ushort(unsigned long arg, unsigned short val)
115{
116 return put_user(val, (unsigned short __user *)arg);
117}
118
119static int put_int(unsigned long arg, int val)
120{
121 return put_user(val, (int __user *)arg);
122}
123
124static int put_long(unsigned long arg, long val)
125{
126 return put_user(val, (long __user *)arg);
127}
128
129static int put_ulong(unsigned long arg, unsigned long val)
130{
131 return put_user(val, (unsigned long __user *)arg);
132}
133
134static int put_u64(unsigned long arg, u64 val)
135{
136 return put_user(val, (u64 __user *)arg);
137}
138
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700139static int blkdev_locked_ioctl(struct file *file, struct block_device *bdev,
140 unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct backing_dev_info *bdi;
143 int ret, n;
144
145 switch (cmd) {
146 case BLKRAGET:
147 case BLKFRAGET:
148 if (!arg)
149 return -EINVAL;
150 bdi = blk_get_backing_dev_info(bdev);
151 if (bdi == NULL)
152 return -ENOTTY;
153 return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
154 case BLKROGET:
155 return put_int(arg, bdev_read_only(bdev) != 0);
156 case BLKBSZGET: /* get the logical block size (cf. BLKSSZGET) */
157 return put_int(arg, block_size(bdev));
158 case BLKSSZGET: /* get block device hardware sector size */
159 return put_int(arg, bdev_hardsect_size(bdev));
160 case BLKSECTGET:
161 return put_ushort(arg, bdev_get_queue(bdev)->max_sectors);
162 case BLKRASET:
163 case BLKFRASET:
164 if(!capable(CAP_SYS_ADMIN))
165 return -EACCES;
166 bdi = blk_get_backing_dev_info(bdev);
167 if (bdi == NULL)
168 return -ENOTTY;
169 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
170 return 0;
171 case BLKBSZSET:
172 /* set the logical block size */
173 if (!capable(CAP_SYS_ADMIN))
174 return -EACCES;
175 if (!arg)
176 return -EINVAL;
177 if (get_user(n, (int __user *) arg))
178 return -EFAULT;
179 if (bd_claim(bdev, file) < 0)
180 return -EBUSY;
181 ret = set_blocksize(bdev, n);
182 bd_release(bdev);
183 return ret;
184 case BLKPG:
185 return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
186 case BLKRRPART:
187 return blkdev_reread_part(bdev);
188 case BLKGETSIZE:
189 if ((bdev->bd_inode->i_size >> 9) > ~0UL)
190 return -EFBIG;
191 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
192 case BLKGETSIZE64:
193 return put_u64(arg, bdev->bd_inode->i_size);
Jens Axboe2056a782006-03-23 20:00:26 +0100194 case BLKTRACESTART:
195 case BLKTRACESTOP:
196 case BLKTRACESETUP:
197 case BLKTRACETEARDOWN:
198 return blk_trace_ioctl(bdev, cmd, (char __user *) arg);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700199 }
200 return -ENOIOCTLCMD;
201}
202
Alasdair G Kergon7006f6e2006-10-03 01:15:21 -0700203int blkdev_driver_ioctl(struct inode *inode, struct file *file,
204 struct gendisk *disk, unsigned cmd, unsigned long arg)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700205{
206 int ret;
207 if (disk->fops->unlocked_ioctl)
208 return disk->fops->unlocked_ioctl(file, cmd, arg);
209
210 if (disk->fops->ioctl) {
211 lock_kernel();
212 ret = disk->fops->ioctl(inode, file, cmd, arg);
213 unlock_kernel();
214 return ret;
215 }
216
217 return -ENOTTY;
218}
Alasdair G Kergon7006f6e2006-10-03 01:15:21 -0700219EXPORT_SYMBOL_GPL(blkdev_driver_ioctl);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700220
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200221/*
222 * always keep this in sync with compat_blkdev_ioctl() and
223 * compat_blkdev_locked_ioctl()
224 */
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700225int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
226 unsigned long arg)
227{
228 struct block_device *bdev = inode->i_bdev;
229 struct gendisk *disk = bdev->bd_disk;
230 int ret, n;
231
232 switch(cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 case BLKFLSBUF:
234 if (!capable(CAP_SYS_ADMIN))
235 return -EACCES;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700236
237 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
238 /* -EINVAL to handle old uncorrected drivers */
239 if (ret != -EINVAL && ret != -ENOTTY)
240 return ret;
241
242 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 fsync_bdev(bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700244 invalidate_bdev(bdev);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700245 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 case BLKROSET:
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700249 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
250 /* -EINVAL to handle old uncorrected drivers */
251 if (ret != -EINVAL && ret != -ENOTTY)
252 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!capable(CAP_SYS_ADMIN))
254 return -EACCES;
255 if (get_user(n, (int __user *)(arg)))
256 return -EFAULT;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700257 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 set_device_ro(bdev, n);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700259 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800261 case HDIO_GETGEO: {
262 struct hd_geometry geo;
263
264 if (!arg)
265 return -EINVAL;
266 if (!disk->fops->getgeo)
267 return -ENOTTY;
268
269 /*
270 * We need to set the startsect first, the driver may
271 * want to override it.
272 */
273 geo.start = get_start_sect(bdev);
274 ret = disk->fops->getgeo(bdev, &geo);
275 if (ret)
276 return ret;
277 if (copy_to_user((struct hd_geometry __user *)arg, &geo,
278 sizeof(geo)))
279 return -EFAULT;
280 return 0;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700283
284 lock_kernel();
285 ret = blkdev_locked_ioctl(file, bdev, cmd, arg);
286 unlock_kernel();
287 if (ret != -ENOIOCTLCMD)
288 return ret;
289
290 return blkdev_driver_ioctl(inode, file, disk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
Stephen Tweedie68f66fe2005-05-13 23:31:19 -0400292EXPORT_SYMBOL_GPL(blkdev_ioctl);