netfilter: conntrack: Use consistent ct id hash calculation
[linux/fpc-iii.git] / block / compat_ioctl.c
blob3c9fdd6983aa34f5283fae2e13f2523cfc0171e6
1 #include <linux/blkdev.h>
2 #include <linux/blkpg.h>
3 #include <linux/blktrace_api.h>
4 #include <linux/cdrom.h>
5 #include <linux/compat.h>
6 #include <linux/elevator.h>
7 #include <linux/hdreg.h>
8 #include <linux/slab.h>
9 #include <linux/syscalls.h>
10 #include <linux/types.h>
11 #include <linux/uaccess.h>
13 static int compat_put_ushort(unsigned long arg, unsigned short val)
15 return put_user(val, (unsigned short __user *)compat_ptr(arg));
18 static int compat_put_int(unsigned long arg, int val)
20 return put_user(val, (compat_int_t __user *)compat_ptr(arg));
23 static int compat_put_uint(unsigned long arg, unsigned int val)
25 return put_user(val, (compat_uint_t __user *)compat_ptr(arg));
28 static int compat_put_long(unsigned long arg, long val)
30 return put_user(val, (compat_long_t __user *)compat_ptr(arg));
33 static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
35 return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
38 static int compat_put_u64(unsigned long arg, u64 val)
40 return put_user(val, (compat_u64 __user *)compat_ptr(arg));
43 struct compat_hd_geometry {
44 unsigned char heads;
45 unsigned char sectors;
46 unsigned short cylinders;
47 u32 start;
50 static int compat_hdio_getgeo(struct gendisk *disk, struct block_device *bdev,
51 struct compat_hd_geometry __user *ugeo)
53 struct hd_geometry geo;
54 int ret;
56 if (!ugeo)
57 return -EINVAL;
58 if (!disk->fops->getgeo)
59 return -ENOTTY;
61 memset(&geo, 0, sizeof(geo));
63 * We need to set the startsect first, the driver may
64 * want to override it.
66 geo.start = get_start_sect(bdev);
67 ret = disk->fops->getgeo(bdev, &geo);
68 if (ret)
69 return ret;
71 ret = copy_to_user(ugeo, &geo, 4);
72 ret |= put_user(geo.start, &ugeo->start);
73 if (ret)
74 ret = -EFAULT;
76 return ret;
79 static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,
80 unsigned int cmd, unsigned long arg)
82 mm_segment_t old_fs = get_fs();
83 unsigned long kval;
84 unsigned int __user *uvp;
85 int error;
87 set_fs(KERNEL_DS);
88 error = __blkdev_driver_ioctl(bdev, mode,
89 cmd, (unsigned long)(&kval));
90 set_fs(old_fs);
92 if (error == 0) {
93 uvp = compat_ptr(arg);
94 if (put_user(kval, uvp))
95 error = -EFAULT;
97 return error;
100 struct compat_cdrom_read_audio {
101 union cdrom_addr addr;
102 u8 addr_format;
103 compat_int_t nframes;
104 compat_caddr_t buf;
107 struct compat_cdrom_generic_command {
108 unsigned char cmd[CDROM_PACKET_SIZE];
109 compat_caddr_t buffer;
110 compat_uint_t buflen;
111 compat_int_t stat;
112 compat_caddr_t sense;
113 unsigned char data_direction;
114 compat_int_t quiet;
115 compat_int_t timeout;
116 compat_caddr_t reserved[1];
119 static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode,
120 unsigned int cmd, unsigned long arg)
122 struct cdrom_read_audio __user *cdread_audio;
123 struct compat_cdrom_read_audio __user *cdread_audio32;
124 __u32 data;
125 void __user *datap;
127 cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio));
128 cdread_audio32 = compat_ptr(arg);
130 if (copy_in_user(&cdread_audio->addr,
131 &cdread_audio32->addr,
132 (sizeof(*cdread_audio32) -
133 sizeof(compat_caddr_t))))
134 return -EFAULT;
136 if (get_user(data, &cdread_audio32->buf))
137 return -EFAULT;
138 datap = compat_ptr(data);
139 if (put_user(datap, &cdread_audio->buf))
140 return -EFAULT;
142 return __blkdev_driver_ioctl(bdev, mode, cmd,
143 (unsigned long)cdread_audio);
146 static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode,
147 unsigned int cmd, unsigned long arg)
149 struct cdrom_generic_command __user *cgc;
150 struct compat_cdrom_generic_command __user *cgc32;
151 u32 data;
152 unsigned char dir;
153 int itmp;
155 cgc = compat_alloc_user_space(sizeof(*cgc));
156 cgc32 = compat_ptr(arg);
158 if (copy_in_user(&cgc->cmd, &cgc32->cmd, sizeof(cgc->cmd)) ||
159 get_user(data, &cgc32->buffer) ||
160 put_user(compat_ptr(data), &cgc->buffer) ||
161 copy_in_user(&cgc->buflen, &cgc32->buflen,
162 (sizeof(unsigned int) + sizeof(int))) ||
163 get_user(data, &cgc32->sense) ||
164 put_user(compat_ptr(data), &cgc->sense) ||
165 get_user(dir, &cgc32->data_direction) ||
166 put_user(dir, &cgc->data_direction) ||
167 get_user(itmp, &cgc32->quiet) ||
168 put_user(itmp, &cgc->quiet) ||
169 get_user(itmp, &cgc32->timeout) ||
170 put_user(itmp, &cgc->timeout) ||
171 get_user(data, &cgc32->reserved[0]) ||
172 put_user(compat_ptr(data), &cgc->reserved[0]))
173 return -EFAULT;
175 return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc);
178 struct compat_blkpg_ioctl_arg {
179 compat_int_t op;
180 compat_int_t flags;
181 compat_int_t datalen;
182 compat_caddr_t data;
185 static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
186 unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
188 struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
189 compat_caddr_t udata;
190 compat_int_t n;
191 int err;
193 err = get_user(n, &ua32->op);
194 err |= put_user(n, &a->op);
195 err |= get_user(n, &ua32->flags);
196 err |= put_user(n, &a->flags);
197 err |= get_user(n, &ua32->datalen);
198 err |= put_user(n, &a->datalen);
199 err |= get_user(udata, &ua32->data);
200 err |= put_user(compat_ptr(udata), &a->data);
201 if (err)
202 return err;
204 return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
207 #define BLKBSZGET_32 _IOR(0x12, 112, int)
208 #define BLKBSZSET_32 _IOW(0x12, 113, int)
209 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
211 static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
212 unsigned cmd, unsigned long arg)
214 switch (cmd) {
215 case HDIO_GET_UNMASKINTR:
216 case HDIO_GET_MULTCOUNT:
217 case HDIO_GET_KEEPSETTINGS:
218 case HDIO_GET_32BIT:
219 case HDIO_GET_NOWERR:
220 case HDIO_GET_DMA:
221 case HDIO_GET_NICE:
222 case HDIO_GET_WCACHE:
223 case HDIO_GET_ACOUSTIC:
224 case HDIO_GET_ADDRESS:
225 case HDIO_GET_BUSSTATE:
226 return compat_hdio_ioctl(bdev, mode, cmd, arg);
227 case CDROMREADAUDIO:
228 return compat_cdrom_read_audio(bdev, mode, cmd, arg);
229 case CDROM_SEND_PACKET:
230 return compat_cdrom_generic_command(bdev, mode, cmd, arg);
233 * No handler required for the ones below, we just need to
234 * convert arg to a 64 bit pointer.
236 case BLKSECTSET:
238 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
239 * Some need translations, these do not.
241 case HDIO_GET_IDENTITY:
242 case HDIO_DRIVE_TASK:
243 case HDIO_DRIVE_CMD:
244 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
245 case 0x330:
246 /* CDROM stuff */
247 case CDROMPAUSE:
248 case CDROMRESUME:
249 case CDROMPLAYMSF:
250 case CDROMPLAYTRKIND:
251 case CDROMREADTOCHDR:
252 case CDROMREADTOCENTRY:
253 case CDROMSTOP:
254 case CDROMSTART:
255 case CDROMEJECT:
256 case CDROMVOLCTRL:
257 case CDROMSUBCHNL:
258 case CDROMMULTISESSION:
259 case CDROM_GET_MCN:
260 case CDROMRESET:
261 case CDROMVOLREAD:
262 case CDROMSEEK:
263 case CDROMPLAYBLK:
264 case CDROMCLOSETRAY:
265 case CDROM_DISC_STATUS:
266 case CDROM_CHANGER_NSLOTS:
267 case CDROM_GET_CAPABILITY:
268 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
269 * not take a struct cdrom_read, instead they take a struct cdrom_msf
270 * which is compatible.
272 case CDROMREADMODE2:
273 case CDROMREADMODE1:
274 case CDROMREADRAW:
275 case CDROMREADCOOKED:
276 case CDROMREADALL:
277 /* DVD ioctls */
278 case DVD_READ_STRUCT:
279 case DVD_WRITE_STRUCT:
280 case DVD_AUTH:
281 arg = (unsigned long)compat_ptr(arg);
282 /* These intepret arg as an unsigned long, not as a pointer,
283 * so we must not do compat_ptr() conversion. */
284 case HDIO_SET_MULTCOUNT:
285 case HDIO_SET_UNMASKINTR:
286 case HDIO_SET_KEEPSETTINGS:
287 case HDIO_SET_32BIT:
288 case HDIO_SET_NOWERR:
289 case HDIO_SET_DMA:
290 case HDIO_SET_PIO_MODE:
291 case HDIO_SET_NICE:
292 case HDIO_SET_WCACHE:
293 case HDIO_SET_ACOUSTIC:
294 case HDIO_SET_BUSSTATE:
295 case HDIO_SET_ADDRESS:
296 case CDROMEJECT_SW:
297 case CDROM_SET_OPTIONS:
298 case CDROM_CLEAR_OPTIONS:
299 case CDROM_SELECT_SPEED:
300 case CDROM_SELECT_DISC:
301 case CDROM_MEDIA_CHANGED:
302 case CDROM_DRIVE_STATUS:
303 case CDROM_LOCKDOOR:
304 case CDROM_DEBUG:
305 break;
306 default:
307 /* unknown ioctl number */
308 return -ENOIOCTLCMD;
311 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
314 /* Most of the generic ioctls are handled in the normal fallback path.
315 This assumes the blkdev's low level compat_ioctl always returns
316 ENOIOCTLCMD for unknown ioctls. */
317 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
319 int ret = -ENOIOCTLCMD;
320 struct inode *inode = file->f_mapping->host;
321 struct block_device *bdev = inode->i_bdev;
322 struct gendisk *disk = bdev->bd_disk;
323 fmode_t mode = file->f_mode;
324 struct backing_dev_info *bdi;
325 loff_t size;
326 unsigned int max_sectors;
329 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
330 * to updated it before every ioctl.
332 if (file->f_flags & O_NDELAY)
333 mode |= FMODE_NDELAY;
334 else
335 mode &= ~FMODE_NDELAY;
337 switch (cmd) {
338 case HDIO_GETGEO:
339 return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
340 case BLKPBSZGET:
341 return compat_put_uint(arg, bdev_physical_block_size(bdev));
342 case BLKIOMIN:
343 return compat_put_uint(arg, bdev_io_min(bdev));
344 case BLKIOOPT:
345 return compat_put_uint(arg, bdev_io_opt(bdev));
346 case BLKALIGNOFF:
347 return compat_put_int(arg, bdev_alignment_offset(bdev));
348 case BLKDISCARDZEROES:
349 return compat_put_uint(arg, bdev_discard_zeroes_data(bdev));
350 case BLKFLSBUF:
351 case BLKROSET:
352 case BLKDISCARD:
353 case BLKSECDISCARD:
354 case BLKZEROOUT:
356 * the ones below are implemented in blkdev_locked_ioctl,
357 * but we call blkdev_ioctl, which gets the lock for us
359 case BLKRRPART:
360 return blkdev_ioctl(bdev, mode, cmd,
361 (unsigned long)compat_ptr(arg));
362 case BLKBSZSET_32:
363 return blkdev_ioctl(bdev, mode, BLKBSZSET,
364 (unsigned long)compat_ptr(arg));
365 case BLKPG:
366 return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
367 case BLKRAGET:
368 case BLKFRAGET:
369 if (!arg)
370 return -EINVAL;
371 bdi = blk_get_backing_dev_info(bdev);
372 return compat_put_long(arg,
373 (bdi->ra_pages * PAGE_SIZE) / 512);
374 case BLKROGET: /* compatible */
375 return compat_put_int(arg, bdev_read_only(bdev) != 0);
376 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
377 return compat_put_int(arg, block_size(bdev));
378 case BLKSSZGET: /* get block device hardware sector size */
379 return compat_put_int(arg, bdev_logical_block_size(bdev));
380 case BLKSECTGET:
381 max_sectors = min_t(unsigned int, USHRT_MAX,
382 queue_max_sectors(bdev_get_queue(bdev)));
383 return compat_put_ushort(arg, max_sectors);
384 case BLKROTATIONAL:
385 return compat_put_ushort(arg,
386 !blk_queue_nonrot(bdev_get_queue(bdev)));
387 case BLKRASET: /* compatible, but no compat_ptr (!) */
388 case BLKFRASET:
389 if (!capable(CAP_SYS_ADMIN))
390 return -EACCES;
391 bdi = blk_get_backing_dev_info(bdev);
392 bdi->ra_pages = (arg * 512) / PAGE_SIZE;
393 return 0;
394 case BLKGETSIZE:
395 size = i_size_read(bdev->bd_inode);
396 if ((size >> 9) > ~0UL)
397 return -EFBIG;
398 return compat_put_ulong(arg, size >> 9);
400 case BLKGETSIZE64_32:
401 return compat_put_u64(arg, i_size_read(bdev->bd_inode));
403 case BLKTRACESETUP32:
404 case BLKTRACESTART: /* compatible */
405 case BLKTRACESTOP: /* compatible */
406 case BLKTRACETEARDOWN: /* compatible */
407 ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
408 return ret;
409 default:
410 if (disk->fops->compat_ioctl)
411 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
412 if (ret == -ENOIOCTLCMD)
413 ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);
414 return ret;