[PATCH] beginning of methods conversion
[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 hd_struct *part;
16         struct blkpg_ioctl_arg a;
17         struct blkpg_partition p;
18         struct disk_part_iter piter;
19         long long start, length;
20         int partno;
21         int err;
22
23         if (!capable(CAP_SYS_ADMIN))
24                 return -EACCES;
25         if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
26                 return -EFAULT;
27         if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
28                 return -EFAULT;
29         disk = bdev->bd_disk;
30         if (bdev != bdev->bd_contains)
31                 return -EINVAL;
32         partno = p.pno;
33         if (partno <= 0)
34                 return -EINVAL;
35         switch (a.op) {
36                 case BLKPG_ADD_PARTITION:
37                         start = p.start >> 9;
38                         length = p.length >> 9;
39                         /* check for fit in a hd_struct */ 
40                         if (sizeof(sector_t) == sizeof(long) && 
41                             sizeof(long long) > sizeof(long)) {
42                                 long pstart = start, plength = length;
43                                 if (pstart != start || plength != length
44                                     || pstart < 0 || plength < 0)
45                                         return -EINVAL;
46                         }
47
48                         mutex_lock(&bdev->bd_mutex);
49
50                         /* overlap? */
51                         disk_part_iter_init(&piter, disk,
52                                             DISK_PITER_INCL_EMPTY);
53                         while ((part = disk_part_iter_next(&piter))) {
54                                 if (!(start + length <= part->start_sect ||
55                                       start >= part->start_sect + part->nr_sects)) {
56                                         disk_part_iter_exit(&piter);
57                                         mutex_unlock(&bdev->bd_mutex);
58                                         return -EBUSY;
59                                 }
60                         }
61                         disk_part_iter_exit(&piter);
62
63                         /* all seems OK */
64                         err = add_partition(disk, partno, start, length,
65                                             ADDPART_FLAG_NONE);
66                         mutex_unlock(&bdev->bd_mutex);
67                         return err;
68                 case BLKPG_DEL_PARTITION:
69                         part = disk_get_part(disk, partno);
70                         if (!part)
71                                 return -ENXIO;
72
73                         bdevp = bdget(part_devt(part));
74                         disk_put_part(part);
75                         if (!bdevp)
76                                 return -ENOMEM;
77
78                         mutex_lock(&bdevp->bd_mutex);
79                         if (bdevp->bd_openers) {
80                                 mutex_unlock(&bdevp->bd_mutex);
81                                 bdput(bdevp);
82                                 return -EBUSY;
83                         }
84                         /* all seems OK */
85                         fsync_bdev(bdevp);
86                         invalidate_bdev(bdevp);
87
88                         mutex_lock_nested(&bdev->bd_mutex, 1);
89                         delete_partition(disk, partno);
90                         mutex_unlock(&bdev->bd_mutex);
91                         mutex_unlock(&bdevp->bd_mutex);
92                         bdput(bdevp);
93
94                         return 0;
95                 default:
96                         return -EINVAL;
97         }
98 }
99
100 static int blkdev_reread_part(struct block_device *bdev)
101 {
102         struct gendisk *disk = bdev->bd_disk;
103         int res;
104
105         if (!disk_partitionable(disk) || bdev != bdev->bd_contains)
106                 return -EINVAL;
107         if (!capable(CAP_SYS_ADMIN))
108                 return -EACCES;
109         if (!mutex_trylock(&bdev->bd_mutex))
110                 return -EBUSY;
111         res = rescan_partitions(disk, bdev);
112         mutex_unlock(&bdev->bd_mutex);
113         return res;
114 }
115
116 static void blk_ioc_discard_endio(struct bio *bio, int err)
117 {
118         if (err) {
119                 if (err == -EOPNOTSUPP)
120                         set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
121                 clear_bit(BIO_UPTODATE, &bio->bi_flags);
122         }
123         complete(bio->bi_private);
124 }
125
126 static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
127                              uint64_t len)
128 {
129         struct request_queue *q = bdev_get_queue(bdev);
130         int ret = 0;
131
132         if (start & 511)
133                 return -EINVAL;
134         if (len & 511)
135                 return -EINVAL;
136         start >>= 9;
137         len >>= 9;
138
139         if (start + len > (bdev->bd_inode->i_size >> 9))
140                 return -EINVAL;
141
142         if (!q->prepare_discard_fn)
143                 return -EOPNOTSUPP;
144
145         while (len && !ret) {
146                 DECLARE_COMPLETION_ONSTACK(wait);
147                 struct bio *bio;
148
149                 bio = bio_alloc(GFP_KERNEL, 0);
150                 if (!bio)
151                         return -ENOMEM;
152
153                 bio->bi_end_io = blk_ioc_discard_endio;
154                 bio->bi_bdev = bdev;
155                 bio->bi_private = &wait;
156                 bio->bi_sector = start;
157
158                 if (len > q->max_hw_sectors) {
159                         bio->bi_size = q->max_hw_sectors << 9;
160                         len -= q->max_hw_sectors;
161                         start += q->max_hw_sectors;
162                 } else {
163                         bio->bi_size = len << 9;
164                         len = 0;
165                 }
166                 submit_bio(DISCARD_NOBARRIER, bio);
167
168                 wait_for_completion(&wait);
169
170                 if (bio_flagged(bio, BIO_EOPNOTSUPP))
171                         ret = -EOPNOTSUPP;
172                 else if (!bio_flagged(bio, BIO_UPTODATE))
173                         ret = -EIO;
174                 bio_put(bio);
175         }
176         return ret;
177 }
178
179 static int put_ushort(unsigned long arg, unsigned short val)
180 {
181         return put_user(val, (unsigned short __user *)arg);
182 }
183
184 static int put_int(unsigned long arg, int val)
185 {
186         return put_user(val, (int __user *)arg);
187 }
188
189 static int put_long(unsigned long arg, long val)
190 {
191         return put_user(val, (long __user *)arg);
192 }
193
194 static int put_ulong(unsigned long arg, unsigned long val)
195 {
196         return put_user(val, (unsigned long __user *)arg);
197 }
198
199 static int put_u64(unsigned long arg, u64 val)
200 {
201         return put_user(val, (u64 __user *)arg);
202 }
203
204 static int blkdev_locked_ioctl(struct file *file, struct block_device *bdev,
205                                 unsigned cmd, unsigned long arg)
206 {
207         struct backing_dev_info *bdi;
208         int ret, n;
209
210         switch (cmd) {
211         case BLKRAGET:
212         case BLKFRAGET:
213                 if (!arg)
214                         return -EINVAL;
215                 bdi = blk_get_backing_dev_info(bdev);
216                 if (bdi == NULL)
217                         return -ENOTTY;
218                 return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
219         case BLKROGET:
220                 return put_int(arg, bdev_read_only(bdev) != 0);
221         case BLKBSZGET: /* get the logical block size (cf. BLKSSZGET) */
222                 return put_int(arg, block_size(bdev));
223         case BLKSSZGET: /* get block device hardware sector size */
224                 return put_int(arg, bdev_hardsect_size(bdev));
225         case BLKSECTGET:
226                 return put_ushort(arg, bdev_get_queue(bdev)->max_sectors);
227         case BLKRASET:
228         case BLKFRASET:
229                 if(!capable(CAP_SYS_ADMIN))
230                         return -EACCES;
231                 bdi = blk_get_backing_dev_info(bdev);
232                 if (bdi == NULL)
233                         return -ENOTTY;
234                 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
235                 return 0;
236         case BLKBSZSET:
237                 /* set the logical block size */
238                 if (!capable(CAP_SYS_ADMIN))
239                         return -EACCES;
240                 if (!arg)
241                         return -EINVAL;
242                 if (get_user(n, (int __user *) arg))
243                         return -EFAULT;
244                 if (bd_claim(bdev, file) < 0)
245                         return -EBUSY;
246                 ret = set_blocksize(bdev, n);
247                 bd_release(bdev);
248                 return ret;
249         case BLKPG:
250                 return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
251         case BLKRRPART:
252                 return blkdev_reread_part(bdev);
253         case BLKGETSIZE:
254                 if ((bdev->bd_inode->i_size >> 9) > ~0UL)
255                         return -EFBIG;
256                 return put_ulong(arg, bdev->bd_inode->i_size >> 9);
257         case BLKGETSIZE64:
258                 return put_u64(arg, bdev->bd_inode->i_size);
259         case BLKTRACESTART:
260         case BLKTRACESTOP:
261         case BLKTRACESETUP:
262         case BLKTRACETEARDOWN:
263                 return blk_trace_ioctl(bdev, cmd, (char __user *) arg);
264         }
265         return -ENOIOCTLCMD;
266 }
267
268 int blkdev_driver_ioctl(struct inode *inode, struct file *file,
269                         struct gendisk *disk, unsigned cmd, unsigned long arg)
270 {
271         int ret;
272         fmode_t mode = 0;
273         if (file) {
274                 mode = file->f_mode;
275                 if (file->f_flags & O_NDELAY)
276                         mode |= FMODE_NDELAY_NOW;
277         }
278
279         if (disk->fops->__unlocked_ioctl)
280                 return disk->fops->__unlocked_ioctl(file, cmd, arg);
281
282         if (disk->fops->__ioctl) {
283                 lock_kernel();
284                 ret = disk->fops->__ioctl(inode, file, cmd, arg);
285                 unlock_kernel();
286                 return ret;
287         }
288
289         return __blkdev_driver_ioctl(inode->i_bdev, mode, cmd, arg);
290 }
291 EXPORT_SYMBOL_GPL(blkdev_driver_ioctl);
292
293 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
294                         unsigned cmd, unsigned long arg)
295 {
296         struct gendisk *disk = bdev->bd_disk;
297         int ret;
298         /* you bet it'll go away by the end of patch series */
299         struct file fake_file = {};
300         struct dentry fake_dentry = {};
301         fake_file.f_mode = mode;
302         fake_file.f_path.dentry = &fake_dentry;
303         fake_dentry.d_inode = bdev->bd_inode;
304
305         if (disk->fops->__unlocked_ioctl)
306                 return disk->fops->__unlocked_ioctl(&fake_file, cmd, arg);
307
308         if (disk->fops->__ioctl) {
309                 lock_kernel();
310                 ret = disk->fops->__ioctl(bdev->bd_inode, &fake_file, cmd, arg);
311                 unlock_kernel();
312                 return ret;
313         }
314
315         if (disk->fops->ioctl)
316                 return disk->fops->ioctl(bdev, mode, cmd, arg);
317
318         if (disk->fops->locked_ioctl) {
319                 lock_kernel();
320                 ret = disk->fops->locked_ioctl(bdev, mode, cmd, arg);
321                 unlock_kernel();
322                 return ret;
323         }
324
325         return -ENOTTY;
326 }
327 /*
328  * For the record: _GPL here is only because somebody decided to slap it
329  * on the previous export.  Sheer idiocy, since it wasn't copyrightable
330  * at all and could be open-coded without any exports by anybody who cares.
331  */
332 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
333
334 /*
335  * always keep this in sync with compat_blkdev_ioctl() and
336  * compat_blkdev_locked_ioctl()
337  */
338 int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
339                         unsigned long arg)
340 {
341         struct block_device *bdev = inode->i_bdev;
342         struct gendisk *disk = bdev->bd_disk;
343         int ret, n;
344
345         switch(cmd) {
346         case BLKFLSBUF:
347                 if (!capable(CAP_SYS_ADMIN))
348                         return -EACCES;
349
350                 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
351                 /* -EINVAL to handle old uncorrected drivers */
352                 if (ret != -EINVAL && ret != -ENOTTY)
353                         return ret;
354
355                 lock_kernel();
356                 fsync_bdev(bdev);
357                 invalidate_bdev(bdev);
358                 unlock_kernel();
359                 return 0;
360
361         case BLKROSET:
362                 ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg);
363                 /* -EINVAL to handle old uncorrected drivers */
364                 if (ret != -EINVAL && ret != -ENOTTY)
365                         return ret;
366                 if (!capable(CAP_SYS_ADMIN))
367                         return -EACCES;
368                 if (get_user(n, (int __user *)(arg)))
369                         return -EFAULT;
370                 lock_kernel();
371                 set_device_ro(bdev, n);
372                 unlock_kernel();
373                 return 0;
374
375         case BLKDISCARD: {
376                 uint64_t range[2];
377
378                 if (!(file->f_mode & FMODE_WRITE))
379                         return -EBADF;
380
381                 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
382                         return -EFAULT;
383
384                 return blk_ioctl_discard(bdev, range[0], range[1]);
385         }
386
387         case HDIO_GETGEO: {
388                 struct hd_geometry geo;
389
390                 if (!arg)
391                         return -EINVAL;
392                 if (!disk->fops->getgeo)
393                         return -ENOTTY;
394
395                 /*
396                  * We need to set the startsect first, the driver may
397                  * want to override it.
398                  */
399                 geo.start = get_start_sect(bdev);
400                 ret = disk->fops->getgeo(bdev, &geo);
401                 if (ret)
402                         return ret;
403                 if (copy_to_user((struct hd_geometry __user *)arg, &geo,
404                                         sizeof(geo)))
405                         return -EFAULT;
406                 return 0;
407         }
408         }
409
410         lock_kernel();
411         ret = blkdev_locked_ioctl(file, bdev, cmd, arg);
412         unlock_kernel();
413         if (ret != -ENOIOCTLCMD)
414                 return ret;
415
416         return blkdev_driver_ioctl(inode, file, disk, cmd, arg);
417 }
418 EXPORT_SYMBOL_GPL(blkdev_ioctl);