block: misc updates
[linux-2.6.git] / block / ioctl.c
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>
10
11 static 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         int err;
21
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
47                         mutex_lock(&bdev->bd_mutex);
48
49                         /* 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)) {
57                                         mutex_unlock(&bdev->bd_mutex);
58                                         return -EBUSY;
59                                 }
60                         }
61                         /* all seems OK */
62                         err = add_partition(disk, part, start, length, ADDPART_FLAG_NONE);
63                         mutex_unlock(&bdev->bd_mutex);
64                         return err;
65                 case BLKPG_DEL_PARTITION:
66                         if (!disk->part[part-1])
67                                 return -ENXIO;
68                         bdevp = bdget_disk(disk, part);
69                         if (!bdevp)
70                                 return -ENOMEM;
71                         mutex_lock(&bdevp->bd_mutex);
72                         if (bdevp->bd_openers) {
73                                 mutex_unlock(&bdevp->bd_mutex);
74                                 bdput(bdevp);
75                                 return -EBUSY;
76                         }
77                         /* all seems OK */
78                         fsync_bdev(bdevp);
79                         invalidate_bdev(bdevp);
80
81                         mutex_lock_nested(&bdev->bd_mutex, 1);
82                         delete_partition(disk, part);
83                         mutex_unlock(&bdev->bd_mutex);
84                         mutex_unlock(&bdevp->bd_mutex);
85                         bdput(bdevp);
86
87                         return 0;
88                 default:
89                         return -EINVAL;
90         }
91 }
92
93 static int blkdev_reread_part(struct block_device *bdev)
94 {
95         struct gendisk *disk = bdev->bd_disk;
96         int res;
97
98         if (disk->minors == 1 || bdev != bdev->bd_contains)
99                 return -EINVAL;
100         if (!capable(CAP_SYS_ADMIN))
101                 return -EACCES;
102         if (!mutex_trylock(&bdev->bd_mutex))
103                 return -EBUSY;
104         res = rescan_partitions(disk, bdev);
105         mutex_unlock(&bdev->bd_mutex);
106         return res;
107 }
108
109 static void blk_ioc_discard_endio(struct bio *bio, int err)
110 {
111         if (err) {
112                 if (err == -EOPNOTSUPP)
113                         set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
114                 clear_bit(BIO_UPTODATE, &bio->bi_flags);
115         }
116         complete(bio->bi_private);
117 }
118
119 static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
120                              uint64_t len)
121 {
122         struct request_queue *q = bdev_get_queue(bdev);
123         int ret = 0;
124
125         if (start & 511)
126                 return -EINVAL;
127         if (len & 511)
128                 return -EINVAL;
129         start >>= 9;
130         len >>= 9;
131
132         if (start + len > (bdev->bd_inode->i_size >> 9))
133                 return -EINVAL;
134
135         if (!q->prepare_discard_fn)
136                 return -EOPNOTSUPP;
137
138         while (len && !ret) {
139                 DECLARE_COMPLETION_ONSTACK(wait);
140                 struct bio *bio;
141
142                 bio = bio_alloc(GFP_KERNEL, 0);
143                 if (!bio)
144                         return -ENOMEM;
145
146                 bio->bi_end_io = blk_ioc_discard_endio;
147                 bio->bi_bdev = bdev;
148                 bio->bi_private = &wait;
149                 bio->bi_sector = start;
150
151                 if (len > q->max_hw_sectors) {
152                         bio->bi_size = q->max_hw_sectors << 9;
153                         len -= q->max_hw_sectors;
154                         start += q->max_hw_sectors;
155                 } else {
156                         bio->bi_size = len << 9;
157                         len = 0;
158                 }
159                 submit_bio(DISCARD_NOBARRIER, bio);
160
161                 wait_for_completion(&wait);
162
163                 if (bio_flagged(bio, BIO_EOPNOTSUPP))
164                         ret = -EOPNOTSUPP;
165                 else if (!bio_flagged(bio, BIO_UPTODATE))
166                         ret = -EIO;
167                 bio_put(bio);
168         }
169         return ret;
170 }
171
172 static int put_ushort(unsigned long arg, unsigned short val)
173 {
174         return put_user(val, (unsigned short __user *)arg);
175 }
176
177 static int put_int(unsigned long arg, int val)
178 {
179         return put_user(val, (int __user *)arg);
180 }
181
182 static int put_long(unsigned long arg, long val)
183 {
184         return put_user(val, (long __user *)arg);
185 }
186
187 static int put_ulong(unsigned long arg, unsigned long val)
188 {
189         return put_user(val, (unsigned long __user *)arg);
190 }
191
192 static int put_u64(unsigned long arg, u64 val)
193 {
194         return put_user(val, (u64 __user *)arg);
195 }
196
197 static int blkdev_locked_ioctl(struct file *file, struct block_device *bdev,
198                                 unsigned cmd, unsigned long arg)
199 {
200         struct backing_dev_info *bdi;
201         int ret, n;
202
203         switch (cmd) {
204         case BLKRAGET:
205         case BLKFRAGET:
206                 if (!arg)
207                         return -EINVAL;
208                 bdi = blk_get_backing_dev_info(bdev);
209                 if (bdi == NULL)
210                         return -ENOTTY;
211                 return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
212         case BLKROGET:
213                 return put_int(arg, bdev_read_only(bdev) != 0);
214         case BLKBSZGET: /* get the logical block size (cf. BLKSSZGET) */
215                 return put_int(arg, block_size(bdev));
216         case BLKSSZGET: /* get block device hardware sector size */
217                 return put_int(arg, bdev_hardsect_size(bdev));
218         case BLKSECTGET:
219                 return put_ushort(arg, bdev_get_queue(bdev)->max_sectors);
220         case BLKRASET:
221         case BLKFRASET:
222                 if(!capable(CAP_SYS_ADMIN))
223                         return -EACCES;
224                 bdi = blk_get_backing_dev_info(bdev);
225                 if (bdi == NULL)
226                         return -ENOTTY;
227                 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
228                 return 0;
229         case BLKBSZSET:
230                 /* set the logical block size */
231                 if (!capable(CAP_SYS_ADMIN))
232                         return -EACCES;
233                 if (!arg)
234                         return -EINVAL;
235                 if (get_user(n, (int __user *) arg))
236                         return -EFAULT;
237                 if (bd_claim(bdev, file) < 0)
238                         return -EBUSY;
239                 ret = set_blocksize(bdev, n);
240                 bd_release(bdev);
241                 return ret;
242         case BLKPG:
243                 return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
244         case BLKRRPART:
245                 return blkdev_reread_part(bdev);
246         case BLKGETSIZE:
247                 if ((bdev->bd_inode->i_size >> 9) > ~0UL)
248                         return -EFBIG;
249                 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
250         case BLKGETSIZE64:
251                 return put_u64(arg, bdev->bd_inode->i_size);
252         case BLKTRACESTART:
253         case BLKTRACESTOP:
254         case BLKTRACESETUP:
255         case BLKTRACETEARDOWN:
256                 return blk_trace_ioctl(bdev, cmd, (char __user *) arg);
257         }
258         return -ENOIOCTLCMD;
259 }
260
261 int blkdev_driver_ioctl(struct inode *inode, struct file *file,
262                         struct gendisk *disk, unsigned cmd, unsigned long arg)
263 {
264         int ret;
265         if (disk->fops->unlocked_ioctl)
266                 return disk->fops->unlocked_ioctl(file, cmd, arg);
267
268         if (disk->fops->ioctl) {
269                 lock_kernel();
270                 ret = disk->fops->ioctl(inode, file, cmd, arg);
271                 unlock_kernel();
272                 return ret;
273         }
274
275         return -ENOTTY;
276 }
277 EXPORT_SYMBOL_GPL(blkdev_driver_ioctl);
278
279 /*
280  * always keep this in sync with compat_blkdev_ioctl() and
281  * compat_blkdev_locked_ioctl()
282  */
283 int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
284                         unsigned long arg)
285 {
286         struct block_device *bdev = inode->i_bdev;
287         struct gendisk *disk = bdev->bd_disk;
288         int ret, n;
289
290         switch(cmd) {
291         case BLKFLSBUF:
292                 if (!capable(CAP_SYS_ADMIN))
293                         return -EACCES;
294
295                 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
296                 /* -EINVAL to handle old uncorrected drivers */
297                 if (ret != -EINVAL && ret != -ENOTTY)
298                         return ret;
299
300                 lock_kernel();
301                 fsync_bdev(bdev);
302                 invalidate_bdev(bdev);
303                 unlock_kernel();
304                 return 0;
305
306         case BLKROSET:
307                 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
308                 /* -EINVAL to handle old uncorrected drivers */
309                 if (ret != -EINVAL && ret != -ENOTTY)
310                         return ret;
311                 if (!capable(CAP_SYS_ADMIN))
312                         return -EACCES;
313                 if (get_user(n, (int __user *)(arg)))
314                         return -EFAULT;
315                 lock_kernel();
316                 set_device_ro(bdev, n);
317                 unlock_kernel();
318                 return 0;
319
320         case BLKDISCARD: {
321                 uint64_t range[2];
322
323                 if (!(file->f_mode & FMODE_WRITE))
324                         return -EBADF;
325
326                 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
327                         return -EFAULT;
328
329                 return blk_ioctl_discard(bdev, range[0], range[1]);
330         }
331
332         case HDIO_GETGEO: {
333                 struct hd_geometry geo;
334
335                 if (!arg)
336                         return -EINVAL;
337                 if (!disk->fops->getgeo)
338                         return -ENOTTY;
339
340                 /*
341                  * We need to set the startsect first, the driver may
342                  * want to override it.
343                  */
344                 geo.start = get_start_sect(bdev);
345                 ret = disk->fops->getgeo(bdev, &geo);
346                 if (ret)
347                         return ret;
348                 if (copy_to_user((struct hd_geometry __user *)arg, &geo,
349                                         sizeof(geo)))
350                         return -EFAULT;
351                 return 0;
352         }
353         }
354
355         lock_kernel();
356         ret = blkdev_locked_ioctl(file, bdev, cmd, arg);
357         unlock_kernel();
358         if (ret != -ENOIOCTLCMD)
359                 return ret;
360
361         return blkdev_driver_ioctl(inode, file, disk, cmd, arg);
362 }
363 EXPORT_SYMBOL_GPL(blkdev_ioctl);