blob: d77f5e280a6e96c1f1034332ae2c0f280d290a00 [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;
Tejun Heocf771cb2008-09-03 09:01:09 +020018 int partno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 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;
Tejun Heocf771cb2008-09-03 09:01:09 +020031 partno = p.pno;
32 if (partno <= 0 || partno >= disk->minors)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 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 }
Tejun Heo88e34122008-08-25 19:30:16 +090046
Arjan van de Venc039e312006-03-23 03:00:28 -080047 mutex_lock(&bdev->bd_mutex);
Tejun Heo88e34122008-08-25 19:30:16 +090048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 /* overlap? */
50 for (i = 0; i < disk->minors - 1; i++) {
51 struct hd_struct *s = disk->part[i];
52
53 if (!s)
54 continue;
55 if (!(start+length <= s->start_sect ||
56 start >= s->start_sect + s->nr_sects)) {
Arjan van de Venc039e312006-03-23 03:00:28 -080057 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return -EBUSY;
59 }
60 }
61 /* all seems OK */
Tejun Heocf771cb2008-09-03 09:01:09 +020062 err = add_partition(disk, partno, start, length,
63 ADDPART_FLAG_NONE);
Arjan van de Venc039e312006-03-23 03:00:28 -080064 mutex_unlock(&bdev->bd_mutex);
Abdel Benamrouche04ebd4a2008-07-25 01:48:26 -070065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 case BLKPG_DEL_PARTITION:
Tejun Heocf771cb2008-09-03 09:01:09 +020067 if (!disk->part[partno - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return -ENXIO;
Tejun Heocf771cb2008-09-03 09:01:09 +020069 bdevp = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (!bdevp)
71 return -ENOMEM;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -080072 mutex_lock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (bdevp->bd_openers) {
Arjan van de Venc039e312006-03-23 03:00:28 -080074 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 bdput(bdevp);
76 return -EBUSY;
77 }
78 /* all seems OK */
79 fsync_bdev(bdevp);
Peter Zijlstraf98393a2007-05-06 14:49:54 -070080 invalidate_bdev(bdevp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Peter Zijlstra6d740cd2007-02-20 13:58:18 -080082 mutex_lock_nested(&bdev->bd_mutex, 1);
Tejun Heocf771cb2008-09-03 09:01:09 +020083 delete_partition(disk, partno);
Arjan van de Venc039e312006-03-23 03:00:28 -080084 mutex_unlock(&bdev->bd_mutex);
85 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 bdput(bdevp);
87
88 return 0;
89 default:
90 return -EINVAL;
91 }
92}
93
94static int blkdev_reread_part(struct block_device *bdev)
95{
96 struct gendisk *disk = bdev->bd_disk;
97 int res;
98
99 if (disk->minors == 1 || bdev != bdev->bd_contains)
100 return -EINVAL;
101 if (!capable(CAP_SYS_ADMIN))
102 return -EACCES;
Arjan van de Venc039e312006-03-23 03:00:28 -0800103 if (!mutex_trylock(&bdev->bd_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -EBUSY;
105 res = rescan_partitions(disk, bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800106 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return res;
108}
109
David Woodhoused30a2602008-08-11 15:58:42 +0100110static void blk_ioc_discard_endio(struct bio *bio, int err)
111{
112 if (err) {
113 if (err == -EOPNOTSUPP)
114 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
115 clear_bit(BIO_UPTODATE, &bio->bi_flags);
116 }
117 complete(bio->bi_private);
118}
119
120static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
121 uint64_t len)
122{
123 struct request_queue *q = bdev_get_queue(bdev);
124 int ret = 0;
125
126 if (start & 511)
127 return -EINVAL;
128 if (len & 511)
129 return -EINVAL;
130 start >>= 9;
131 len >>= 9;
132
133 if (start + len > (bdev->bd_inode->i_size >> 9))
134 return -EINVAL;
135
136 if (!q->prepare_discard_fn)
137 return -EOPNOTSUPP;
138
139 while (len && !ret) {
140 DECLARE_COMPLETION_ONSTACK(wait);
141 struct bio *bio;
142
143 bio = bio_alloc(GFP_KERNEL, 0);
144 if (!bio)
145 return -ENOMEM;
146
147 bio->bi_end_io = blk_ioc_discard_endio;
148 bio->bi_bdev = bdev;
149 bio->bi_private = &wait;
150 bio->bi_sector = start;
151
152 if (len > q->max_hw_sectors) {
153 bio->bi_size = q->max_hw_sectors << 9;
154 len -= q->max_hw_sectors;
155 start += q->max_hw_sectors;
156 } else {
157 bio->bi_size = len << 9;
158 len = 0;
159 }
David Woodhousee17fc0a2008-08-09 16:42:20 +0100160 submit_bio(DISCARD_NOBARRIER, bio);
David Woodhoused30a2602008-08-11 15:58:42 +0100161
162 wait_for_completion(&wait);
163
164 if (bio_flagged(bio, BIO_EOPNOTSUPP))
165 ret = -EOPNOTSUPP;
166 else if (!bio_flagged(bio, BIO_UPTODATE))
167 ret = -EIO;
168 bio_put(bio);
169 }
170 return ret;
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static int put_ushort(unsigned long arg, unsigned short val)
174{
175 return put_user(val, (unsigned short __user *)arg);
176}
177
178static int put_int(unsigned long arg, int val)
179{
180 return put_user(val, (int __user *)arg);
181}
182
183static int put_long(unsigned long arg, long val)
184{
185 return put_user(val, (long __user *)arg);
186}
187
188static int put_ulong(unsigned long arg, unsigned long val)
189{
190 return put_user(val, (unsigned long __user *)arg);
191}
192
193static int put_u64(unsigned long arg, u64 val)
194{
195 return put_user(val, (u64 __user *)arg);
196}
197
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700198static int blkdev_locked_ioctl(struct file *file, struct block_device *bdev,
199 unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct backing_dev_info *bdi;
202 int ret, n;
203
204 switch (cmd) {
205 case BLKRAGET:
206 case BLKFRAGET:
207 if (!arg)
208 return -EINVAL;
209 bdi = blk_get_backing_dev_info(bdev);
210 if (bdi == NULL)
211 return -ENOTTY;
212 return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
213 case BLKROGET:
214 return put_int(arg, bdev_read_only(bdev) != 0);
215 case BLKBSZGET: /* get the logical block size (cf. BLKSSZGET) */
216 return put_int(arg, block_size(bdev));
217 case BLKSSZGET: /* get block device hardware sector size */
218 return put_int(arg, bdev_hardsect_size(bdev));
219 case BLKSECTGET:
220 return put_ushort(arg, bdev_get_queue(bdev)->max_sectors);
221 case BLKRASET:
222 case BLKFRASET:
223 if(!capable(CAP_SYS_ADMIN))
224 return -EACCES;
225 bdi = blk_get_backing_dev_info(bdev);
226 if (bdi == NULL)
227 return -ENOTTY;
228 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
229 return 0;
230 case BLKBSZSET:
231 /* set the logical block size */
232 if (!capable(CAP_SYS_ADMIN))
233 return -EACCES;
234 if (!arg)
235 return -EINVAL;
236 if (get_user(n, (int __user *) arg))
237 return -EFAULT;
238 if (bd_claim(bdev, file) < 0)
239 return -EBUSY;
240 ret = set_blocksize(bdev, n);
241 bd_release(bdev);
242 return ret;
243 case BLKPG:
244 return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
245 case BLKRRPART:
246 return blkdev_reread_part(bdev);
247 case BLKGETSIZE:
248 if ((bdev->bd_inode->i_size >> 9) > ~0UL)
249 return -EFBIG;
250 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
251 case BLKGETSIZE64:
252 return put_u64(arg, bdev->bd_inode->i_size);
Jens Axboe2056a782006-03-23 20:00:26 +0100253 case BLKTRACESTART:
254 case BLKTRACESTOP:
255 case BLKTRACESETUP:
256 case BLKTRACETEARDOWN:
257 return blk_trace_ioctl(bdev, cmd, (char __user *) arg);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700258 }
259 return -ENOIOCTLCMD;
260}
261
Alasdair G Kergon7006f6e2006-10-03 01:15:21 -0700262int blkdev_driver_ioctl(struct inode *inode, struct file *file,
263 struct gendisk *disk, unsigned cmd, unsigned long arg)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700264{
265 int ret;
266 if (disk->fops->unlocked_ioctl)
267 return disk->fops->unlocked_ioctl(file, cmd, arg);
268
269 if (disk->fops->ioctl) {
270 lock_kernel();
271 ret = disk->fops->ioctl(inode, file, cmd, arg);
272 unlock_kernel();
273 return ret;
274 }
275
276 return -ENOTTY;
277}
Alasdair G Kergon7006f6e2006-10-03 01:15:21 -0700278EXPORT_SYMBOL_GPL(blkdev_driver_ioctl);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700279
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200280/*
281 * always keep this in sync with compat_blkdev_ioctl() and
282 * compat_blkdev_locked_ioctl()
283 */
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700284int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
285 unsigned long arg)
286{
287 struct block_device *bdev = inode->i_bdev;
288 struct gendisk *disk = bdev->bd_disk;
289 int ret, n;
290
291 switch(cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case BLKFLSBUF:
293 if (!capable(CAP_SYS_ADMIN))
294 return -EACCES;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700295
296 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
297 /* -EINVAL to handle old uncorrected drivers */
298 if (ret != -EINVAL && ret != -ENOTTY)
299 return ret;
300
301 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 fsync_bdev(bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700303 invalidate_bdev(bdev);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700304 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 case BLKROSET:
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700308 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
309 /* -EINVAL to handle old uncorrected drivers */
310 if (ret != -EINVAL && ret != -ENOTTY)
311 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!capable(CAP_SYS_ADMIN))
313 return -EACCES;
314 if (get_user(n, (int __user *)(arg)))
315 return -EFAULT;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700316 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 set_device_ro(bdev, n);
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700318 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 0;
David Woodhoused30a2602008-08-11 15:58:42 +0100320
321 case BLKDISCARD: {
322 uint64_t range[2];
323
324 if (!(file->f_mode & FMODE_WRITE))
325 return -EBADF;
326
327 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
328 return -EFAULT;
329
330 return blk_ioctl_discard(bdev, range[0], range[1]);
331 }
332
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800333 case HDIO_GETGEO: {
334 struct hd_geometry geo;
335
336 if (!arg)
337 return -EINVAL;
338 if (!disk->fops->getgeo)
339 return -ENOTTY;
340
341 /*
342 * We need to set the startsect first, the driver may
343 * want to override it.
344 */
345 geo.start = get_start_sect(bdev);
346 ret = disk->fops->getgeo(bdev, &geo);
347 if (ret)
348 return ret;
349 if (copy_to_user((struct hd_geometry __user *)arg, &geo,
350 sizeof(geo)))
351 return -EFAULT;
352 return 0;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700355
356 lock_kernel();
357 ret = blkdev_locked_ioctl(file, bdev, cmd, arg);
358 unlock_kernel();
359 if (ret != -ENOIOCTLCMD)
360 return ret;
361
362 return blkdev_driver_ioctl(inode, file, disk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
Stephen Tweedie68f66fe2005-05-13 23:31:19 -0400364EXPORT_SYMBOL_GPL(blkdev_ioctl);