[PATCH] dvb: av7110: fix indentation
[linux-2.6] / drivers / block / ioctl.c
1 #include <linux/sched.h>                /* for capable() */
2 #include <linux/blkdev.h>
3 #include <linux/blkpg.h>
4 #include <linux/backing-dev.h>
5 #include <linux/buffer_head.h>
6 #include <linux/smp_lock.h>
7 #include <asm/uaccess.h>
8
9 static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
10 {
11         struct block_device *bdevp;
12         struct gendisk *disk;
13         struct blkpg_ioctl_arg a;
14         struct blkpg_partition p;
15         long long start, length;
16         int part;
17         int i;
18
19         if (!capable(CAP_SYS_ADMIN))
20                 return -EACCES;
21         if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
22                 return -EFAULT;
23         if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
24                 return -EFAULT;
25         disk = bdev->bd_disk;
26         if (bdev != bdev->bd_contains)
27                 return -EINVAL;
28         part = p.pno;
29         if (part <= 0 || part >= disk->minors)
30                 return -EINVAL;
31         switch (a.op) {
32                 case BLKPG_ADD_PARTITION:
33                         start = p.start >> 9;
34                         length = p.length >> 9;
35                         /* check for fit in a hd_struct */ 
36                         if (sizeof(sector_t) == sizeof(long) && 
37                             sizeof(long long) > sizeof(long)) {
38                                 long pstart = start, plength = length;
39                                 if (pstart != start || plength != length
40                                     || pstart < 0 || plength < 0)
41                                         return -EINVAL;
42                         }
43                         /* partition number in use? */
44                         down(&bdev->bd_sem);
45                         if (disk->part[part - 1]) {
46                                 up(&bdev->bd_sem);
47                                 return -EBUSY;
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                                         up(&bdev->bd_sem);
58                                         return -EBUSY;
59                                 }
60                         }
61                         /* all seems OK */
62                         add_partition(disk, part, start, length);
63                         up(&bdev->bd_sem);
64                         return 0;
65                 case BLKPG_DEL_PARTITION:
66                         if (!disk->part[part-1])
67                                 return -ENXIO;
68                         if (disk->part[part - 1]->nr_sects == 0)
69                                 return -ENXIO;
70                         bdevp = bdget_disk(disk, part);
71                         if (!bdevp)
72                                 return -ENOMEM;
73                         down(&bdevp->bd_sem);
74                         if (bdevp->bd_openers) {
75                                 up(&bdevp->bd_sem);
76                                 bdput(bdevp);
77                                 return -EBUSY;
78                         }
79                         /* all seems OK */
80                         fsync_bdev(bdevp);
81                         invalidate_bdev(bdevp, 0);
82
83                         down(&bdev->bd_sem);
84                         delete_partition(disk, part);
85                         up(&bdev->bd_sem);
86                         up(&bdevp->bd_sem);
87                         bdput(bdevp);
88
89                         return 0;
90                 default:
91                         return -EINVAL;
92         }
93 }
94
95 static int blkdev_reread_part(struct block_device *bdev)
96 {
97         struct gendisk *disk = bdev->bd_disk;
98         int res;
99
100         if (disk->minors == 1 || bdev != bdev->bd_contains)
101                 return -EINVAL;
102         if (!capable(CAP_SYS_ADMIN))
103                 return -EACCES;
104         if (down_trylock(&bdev->bd_sem))
105                 return -EBUSY;
106         res = rescan_partitions(disk, bdev);
107         up(&bdev->bd_sem);
108         return res;
109 }
110
111 static int put_ushort(unsigned long arg, unsigned short val)
112 {
113         return put_user(val, (unsigned short __user *)arg);
114 }
115
116 static int put_int(unsigned long arg, int val)
117 {
118         return put_user(val, (int __user *)arg);
119 }
120
121 static int put_long(unsigned long arg, long val)
122 {
123         return put_user(val, (long __user *)arg);
124 }
125
126 static int put_ulong(unsigned long arg, unsigned long val)
127 {
128         return put_user(val, (unsigned long __user *)arg);
129 }
130
131 static int put_u64(unsigned long arg, u64 val)
132 {
133         return put_user(val, (u64 __user *)arg);
134 }
135
136 int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
137                         unsigned long arg)
138 {
139         struct block_device *bdev = inode->i_bdev;
140         struct gendisk *disk = bdev->bd_disk;
141         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);
193         case BLKFLSBUF:
194                 if (!capable(CAP_SYS_ADMIN))
195                         return -EACCES;
196                 if (disk->fops->ioctl) {
197                         ret = disk->fops->ioctl(inode, file, cmd, arg);
198                         /* -EINVAL to handle old uncorrected drivers */
199                         if (ret != -EINVAL && ret != -ENOTTY)
200                                 return ret;
201                 }
202                 fsync_bdev(bdev);
203                 invalidate_bdev(bdev, 0);
204                 return 0;
205         case BLKROSET:
206                 if (disk->fops->ioctl) {
207                         ret = disk->fops->ioctl(inode, file, cmd, arg);
208                         /* -EINVAL to handle old uncorrected drivers */
209                         if (ret != -EINVAL && ret != -ENOTTY)
210                                 return ret;
211                 }
212                 if (!capable(CAP_SYS_ADMIN))
213                         return -EACCES;
214                 if (get_user(n, (int __user *)(arg)))
215                         return -EFAULT;
216                 set_device_ro(bdev, n);
217                 return 0;
218         default:
219                 if (disk->fops->ioctl)
220                         return disk->fops->ioctl(inode, file, cmd, arg);
221         }
222         return -ENOTTY;
223 }
224
225 /* Most of the generic ioctls are handled in the normal fallback path.
226    This assumes the blkdev's low level compat_ioctl always returns
227    ENOIOCTLCMD for unknown ioctls. */
228 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
229 {
230         struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
231         struct gendisk *disk = bdev->bd_disk;
232         int ret = -ENOIOCTLCMD;
233         if (disk->fops->compat_ioctl) {
234                 lock_kernel();
235                 ret = disk->fops->compat_ioctl(file, cmd, arg);
236                 unlock_kernel();
237         }
238         return ret;
239 }
240
241 EXPORT_SYMBOL_GPL(blkdev_ioctl);