diff options
Diffstat (limited to 'block/ioctl.c')
-rw-r--r-- | block/ioctl.c | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/block/ioctl.c b/block/ioctl.c new file mode 100644 index 000000000000..6e278474f9a8 --- /dev/null +++ b/block/ioctl.c | |||
@@ -0,0 +1,275 @@ | |||
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 | static int blkdev_locked_ioctl(struct file *file, struct block_device *bdev, | ||
137 | unsigned cmd, unsigned long arg) | ||
138 | { | ||
139 | struct backing_dev_info *bdi; | ||
140 | int ret, n; | ||
141 | |||
142 | switch (cmd) { | ||
143 | case BLKRAGET: | ||
144 | case BLKFRAGET: | ||
145 | if (!arg) | ||
146 | return -EINVAL; | ||
147 | bdi = blk_get_backing_dev_info(bdev); | ||
148 | if (bdi == NULL) | ||
149 | return -ENOTTY; | ||
150 | return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512); | ||
151 | case BLKROGET: | ||
152 | return put_int(arg, bdev_read_only(bdev) != 0); | ||
153 | case BLKBSZGET: /* get the logical block size (cf. BLKSSZGET) */ | ||
154 | return put_int(arg, block_size(bdev)); | ||
155 | case BLKSSZGET: /* get block device hardware sector size */ | ||
156 | return put_int(arg, bdev_hardsect_size(bdev)); | ||
157 | case BLKSECTGET: | ||
158 | return put_ushort(arg, bdev_get_queue(bdev)->max_sectors); | ||
159 | case BLKRASET: | ||
160 | case BLKFRASET: | ||
161 | if(!capable(CAP_SYS_ADMIN)) | ||
162 | return -EACCES; | ||
163 | bdi = blk_get_backing_dev_info(bdev); | ||
164 | if (bdi == NULL) | ||
165 | return -ENOTTY; | ||
166 | bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE; | ||
167 | return 0; | ||
168 | case BLKBSZSET: | ||
169 | /* set the logical block size */ | ||
170 | if (!capable(CAP_SYS_ADMIN)) | ||
171 | return -EACCES; | ||
172 | if (!arg) | ||
173 | return -EINVAL; | ||
174 | if (get_user(n, (int __user *) arg)) | ||
175 | return -EFAULT; | ||
176 | if (bd_claim(bdev, file) < 0) | ||
177 | return -EBUSY; | ||
178 | ret = set_blocksize(bdev, n); | ||
179 | bd_release(bdev); | ||
180 | return ret; | ||
181 | case BLKPG: | ||
182 | return blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg); | ||
183 | case BLKRRPART: | ||
184 | return blkdev_reread_part(bdev); | ||
185 | case BLKGETSIZE: | ||
186 | if ((bdev->bd_inode->i_size >> 9) > ~0UL) | ||
187 | return -EFBIG; | ||
188 | return put_ulong(arg, bdev->bd_inode->i_size >> 9); | ||
189 | case BLKGETSIZE64: | ||
190 | return put_u64(arg, bdev->bd_inode->i_size); | ||
191 | } | ||
192 | return -ENOIOCTLCMD; | ||
193 | } | ||
194 | |||
195 | static int blkdev_driver_ioctl(struct inode *inode, struct file *file, | ||
196 | struct gendisk *disk, unsigned cmd, unsigned long arg) | ||
197 | { | ||
198 | int ret; | ||
199 | if (disk->fops->unlocked_ioctl) | ||
200 | return disk->fops->unlocked_ioctl(file, cmd, arg); | ||
201 | |||
202 | if (disk->fops->ioctl) { | ||
203 | lock_kernel(); | ||
204 | ret = disk->fops->ioctl(inode, file, cmd, arg); | ||
205 | unlock_kernel(); | ||
206 | return ret; | ||
207 | } | ||
208 | |||
209 | return -ENOTTY; | ||
210 | } | ||
211 | |||
212 | int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd, | ||
213 | unsigned long arg) | ||
214 | { | ||
215 | struct block_device *bdev = inode->i_bdev; | ||
216 | struct gendisk *disk = bdev->bd_disk; | ||
217 | int ret, n; | ||
218 | |||
219 | switch(cmd) { | ||
220 | case BLKFLSBUF: | ||
221 | if (!capable(CAP_SYS_ADMIN)) | ||
222 | return -EACCES; | ||
223 | |||
224 | ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg); | ||
225 | /* -EINVAL to handle old uncorrected drivers */ | ||
226 | if (ret != -EINVAL && ret != -ENOTTY) | ||
227 | return ret; | ||
228 | |||
229 | lock_kernel(); | ||
230 | fsync_bdev(bdev); | ||
231 | invalidate_bdev(bdev, 0); | ||
232 | unlock_kernel(); | ||
233 | return 0; | ||
234 | |||
235 | case BLKROSET: | ||
236 | ret = blkdev_driver_ioctl(inode, file, disk, cmd, arg); | ||
237 | /* -EINVAL to handle old uncorrected drivers */ | ||
238 | if (ret != -EINVAL && ret != -ENOTTY) | ||
239 | return ret; | ||
240 | if (!capable(CAP_SYS_ADMIN)) | ||
241 | return -EACCES; | ||
242 | if (get_user(n, (int __user *)(arg))) | ||
243 | return -EFAULT; | ||
244 | lock_kernel(); | ||
245 | set_device_ro(bdev, n); | ||
246 | unlock_kernel(); | ||
247 | return 0; | ||
248 | } | ||
249 | |||
250 | lock_kernel(); | ||
251 | ret = blkdev_locked_ioctl(file, bdev, cmd, arg); | ||
252 | unlock_kernel(); | ||
253 | if (ret != -ENOIOCTLCMD) | ||
254 | return ret; | ||
255 | |||
256 | return blkdev_driver_ioctl(inode, file, disk, cmd, arg); | ||
257 | } | ||
258 | |||
259 | /* Most of the generic ioctls are handled in the normal fallback path. | ||
260 | This assumes the blkdev's low level compat_ioctl always returns | ||
261 | ENOIOCTLCMD for unknown ioctls. */ | ||
262 | long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) | ||
263 | { | ||
264 | struct block_device *bdev = file->f_dentry->d_inode->i_bdev; | ||
265 | struct gendisk *disk = bdev->bd_disk; | ||
266 | int ret = -ENOIOCTLCMD; | ||
267 | if (disk->fops->compat_ioctl) { | ||
268 | lock_kernel(); | ||
269 | ret = disk->fops->compat_ioctl(file, cmd, arg); | ||
270 | unlock_kernel(); | ||
271 | } | ||
272 | return ret; | ||
273 | } | ||
274 | |||
275 | EXPORT_SYMBOL_GPL(blkdev_ioctl); | ||