]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/compat_ioctl.c
compat_ioctl: remove translation for sound ioctls
[linux.git] / fs / compat_ioctl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ioctl32.c: Conversion between 32bit and 64bit native ioctls.
4  *
5  * Copyright (C) 1997-2000  Jakub Jelinek  (jakub@redhat.com)
6  * Copyright (C) 1998  Eddie C. Dost  (ecd@skynet.be)
7  * Copyright (C) 2001,2002  Andi Kleen, SuSE Labs 
8  * Copyright (C) 2003       Pavel Machek (pavel@ucw.cz)
9  *
10  * These routines maintain argument size conversion between 32bit and 64bit
11  * ioctls.
12  */
13
14 #include <linux/joystick.h>
15
16 #include <linux/types.h>
17 #include <linux/compat.h>
18 #include <linux/kernel.h>
19 #include <linux/capability.h>
20 #include <linux/compiler.h>
21 #include <linux/sched.h>
22 #include <linux/smp.h>
23 #include <linux/ioctl.h>
24 #include <linux/if.h>
25 #include <linux/raid/md_u.h>
26 #include <linux/falloc.h>
27 #include <linux/file.h>
28 #include <linux/ppp-ioctl.h>
29 #include <linux/if_pppox.h>
30 #include <linux/tty.h>
31 #include <linux/vt_kern.h>
32 #include <linux/raw.h>
33 #include <linux/blkdev.h>
34 #include <linux/pci.h>
35 #include <linux/serial.h>
36 #include <linux/ctype.h>
37 #include <linux/syscalls.h>
38 #include <linux/gfp.h>
39 #include <linux/cec.h>
40
41 #include "internal.h"
42
43 #ifdef CONFIG_BLOCK
44 #include <linux/cdrom.h>
45 #include <linux/fd.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_ioctl.h>
48 #include <scsi/sg.h>
49 #endif
50
51 #include <linux/uaccess.h>
52 #include <linux/watchdog.h>
53
54 #include <linux/hiddev.h>
55
56
57 #include <linux/sort.h>
58
59 #ifdef CONFIG_SPARC
60 #include <linux/fb.h>
61 #include <asm/fbio.h>
62 #endif
63
64 #define convert_in_user(srcptr, dstptr)                 \
65 ({                                                      \
66         typeof(*srcptr) val;                            \
67                                                         \
68         get_user(val, srcptr) || put_user(val, dstptr); \
69 })
70
71 static int do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
72 {
73         int err;
74
75         err = security_file_ioctl(file, cmd, arg);
76         if (err)
77                 return err;
78
79         return vfs_ioctl(file, cmd, arg);
80 }
81
82 #ifdef CONFIG_BLOCK
83 typedef struct sg_io_hdr32 {
84         compat_int_t interface_id;      /* [i] 'S' for SCSI generic (required) */
85         compat_int_t dxfer_direction;   /* [i] data transfer direction  */
86         unsigned char cmd_len;          /* [i] SCSI command length ( <= 16 bytes) */
87         unsigned char mx_sb_len;                /* [i] max length to write to sbp */
88         unsigned short iovec_count;     /* [i] 0 implies no scatter gather */
89         compat_uint_t dxfer_len;                /* [i] byte count of data transfer */
90         compat_uint_t dxferp;           /* [i], [*io] points to data transfer memory
91                                               or scatter gather list */
92         compat_uptr_t cmdp;             /* [i], [*i] points to command to perform */
93         compat_uptr_t sbp;              /* [i], [*o] points to sense_buffer memory */
94         compat_uint_t timeout;          /* [i] MAX_UINT->no timeout (unit: millisec) */
95         compat_uint_t flags;            /* [i] 0 -> default, see SG_FLAG... */
96         compat_int_t pack_id;           /* [i->o] unused internally (normally) */
97         compat_uptr_t usr_ptr;          /* [i->o] unused internally */
98         unsigned char status;           /* [o] scsi status */
99         unsigned char masked_status;    /* [o] shifted, masked scsi status */
100         unsigned char msg_status;               /* [o] messaging level data (optional) */
101         unsigned char sb_len_wr;                /* [o] byte count actually written to sbp */
102         unsigned short host_status;     /* [o] errors from host adapter */
103         unsigned short driver_status;   /* [o] errors from software driver */
104         compat_int_t resid;             /* [o] dxfer_len - actual_transferred */
105         compat_uint_t duration;         /* [o] time taken by cmd (unit: millisec) */
106         compat_uint_t info;             /* [o] auxiliary information */
107 } sg_io_hdr32_t;  /* 64 bytes long (on sparc32) */
108
109 typedef struct sg_iovec32 {
110         compat_uint_t iov_base;
111         compat_uint_t iov_len;
112 } sg_iovec32_t;
113
114 static int sg_build_iovec(sg_io_hdr_t __user *sgio, void __user *dxferp, u16 iovec_count)
115 {
116         sg_iovec_t __user *iov = (sg_iovec_t __user *) (sgio + 1);
117         sg_iovec32_t __user *iov32 = dxferp;
118         int i;
119
120         for (i = 0; i < iovec_count; i++) {
121                 u32 base, len;
122
123                 if (get_user(base, &iov32[i].iov_base) ||
124                     get_user(len, &iov32[i].iov_len) ||
125                     put_user(compat_ptr(base), &iov[i].iov_base) ||
126                     put_user(len, &iov[i].iov_len))
127                         return -EFAULT;
128         }
129
130         if (put_user(iov, &sgio->dxferp))
131                 return -EFAULT;
132         return 0;
133 }
134
135 static int sg_ioctl_trans(struct file *file, unsigned int cmd,
136                         sg_io_hdr32_t __user *sgio32)
137 {
138         sg_io_hdr_t __user *sgio;
139         u16 iovec_count;
140         u32 data;
141         void __user *dxferp;
142         int err;
143         int interface_id;
144
145         if (get_user(interface_id, &sgio32->interface_id))
146                 return -EFAULT;
147         if (interface_id != 'S')
148                 return do_ioctl(file, cmd, (unsigned long)sgio32);
149
150         if (get_user(iovec_count, &sgio32->iovec_count))
151                 return -EFAULT;
152
153         {
154                 void __user *top = compat_alloc_user_space(0);
155                 void __user *new = compat_alloc_user_space(sizeof(sg_io_hdr_t) +
156                                        (iovec_count * sizeof(sg_iovec_t)));
157                 if (new > top)
158                         return -EINVAL;
159
160                 sgio = new;
161         }
162
163         /* Ok, now construct.  */
164         if (copy_in_user(&sgio->interface_id, &sgio32->interface_id,
165                          (2 * sizeof(int)) +
166                          (2 * sizeof(unsigned char)) +
167                          (1 * sizeof(unsigned short)) +
168                          (1 * sizeof(unsigned int))))
169                 return -EFAULT;
170
171         if (get_user(data, &sgio32->dxferp))
172                 return -EFAULT;
173         dxferp = compat_ptr(data);
174         if (iovec_count) {
175                 if (sg_build_iovec(sgio, dxferp, iovec_count))
176                         return -EFAULT;
177         } else {
178                 if (put_user(dxferp, &sgio->dxferp))
179                         return -EFAULT;
180         }
181
182         {
183                 unsigned char __user *cmdp;
184                 unsigned char __user *sbp;
185
186                 if (get_user(data, &sgio32->cmdp))
187                         return -EFAULT;
188                 cmdp = compat_ptr(data);
189
190                 if (get_user(data, &sgio32->sbp))
191                         return -EFAULT;
192                 sbp = compat_ptr(data);
193
194                 if (put_user(cmdp, &sgio->cmdp) ||
195                     put_user(sbp, &sgio->sbp))
196                         return -EFAULT;
197         }
198
199         if (copy_in_user(&sgio->timeout, &sgio32->timeout,
200                          3 * sizeof(int)))
201                 return -EFAULT;
202
203         if (get_user(data, &sgio32->usr_ptr))
204                 return -EFAULT;
205         if (put_user(compat_ptr(data), &sgio->usr_ptr))
206                 return -EFAULT;
207
208         err = do_ioctl(file, cmd, (unsigned long) sgio);
209
210         if (err >= 0) {
211                 void __user *datap;
212
213                 if (copy_in_user(&sgio32->pack_id, &sgio->pack_id,
214                                  sizeof(int)) ||
215                     get_user(datap, &sgio->usr_ptr) ||
216                     put_user((u32)(unsigned long)datap,
217                              &sgio32->usr_ptr) ||
218                     copy_in_user(&sgio32->status, &sgio->status,
219                                  (4 * sizeof(unsigned char)) +
220                                  (2 * sizeof(unsigned short)) +
221                                  (3 * sizeof(int))))
222                         err = -EFAULT;
223         }
224
225         return err;
226 }
227
228 struct compat_sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() */
229         char req_state;
230         char orphan;
231         char sg_io_owned;
232         char problem;
233         int pack_id;
234         compat_uptr_t usr_ptr;
235         unsigned int duration;
236         int unused;
237 };
238
239 static int sg_grt_trans(struct file *file,
240                 unsigned int cmd, struct compat_sg_req_info __user *o)
241 {
242         int err, i;
243         sg_req_info_t __user *r;
244         r = compat_alloc_user_space(sizeof(sg_req_info_t)*SG_MAX_QUEUE);
245         err = do_ioctl(file, cmd, (unsigned long)r);
246         if (err < 0)
247                 return err;
248         for (i = 0; i < SG_MAX_QUEUE; i++) {
249                 void __user *ptr;
250                 int d;
251
252                 if (copy_in_user(o + i, r + i, offsetof(sg_req_info_t, usr_ptr)) ||
253                     get_user(ptr, &r[i].usr_ptr) ||
254                     get_user(d, &r[i].duration) ||
255                     put_user((u32)(unsigned long)(ptr), &o[i].usr_ptr) ||
256                     put_user(d, &o[i].duration))
257                         return -EFAULT;
258         }
259         return err;
260 }
261 #endif /* CONFIG_BLOCK */
262
263 struct sock_fprog32 {
264         unsigned short  len;
265         compat_caddr_t  filter;
266 };
267
268 #define PPPIOCSPASS32   _IOW('t', 71, struct sock_fprog32)
269 #define PPPIOCSACTIVE32 _IOW('t', 70, struct sock_fprog32)
270
271 static int ppp_sock_fprog_ioctl_trans(struct file *file,
272                 unsigned int cmd, struct sock_fprog32 __user *u_fprog32)
273 {
274         struct sock_fprog __user *u_fprog64 = compat_alloc_user_space(sizeof(struct sock_fprog));
275         void __user *fptr64;
276         u32 fptr32;
277         u16 flen;
278
279         if (get_user(flen, &u_fprog32->len) ||
280             get_user(fptr32, &u_fprog32->filter))
281                 return -EFAULT;
282
283         fptr64 = compat_ptr(fptr32);
284
285         if (put_user(flen, &u_fprog64->len) ||
286             put_user(fptr64, &u_fprog64->filter))
287                 return -EFAULT;
288
289         if (cmd == PPPIOCSPASS32)
290                 cmd = PPPIOCSPASS;
291         else
292                 cmd = PPPIOCSACTIVE;
293
294         return do_ioctl(file, cmd, (unsigned long) u_fprog64);
295 }
296
297 struct ppp_option_data32 {
298         compat_caddr_t  ptr;
299         u32                     length;
300         compat_int_t            transmit;
301 };
302 #define PPPIOCSCOMPRESS32       _IOW('t', 77, struct ppp_option_data32)
303
304 struct ppp_idle32 {
305         compat_time_t xmit_idle;
306         compat_time_t recv_idle;
307 };
308 #define PPPIOCGIDLE32           _IOR('t', 63, struct ppp_idle32)
309
310 static int ppp_gidle(struct file *file, unsigned int cmd,
311                 struct ppp_idle32 __user *idle32)
312 {
313         struct ppp_idle __user *idle;
314         __kernel_time_t xmit, recv;
315         int err;
316
317         idle = compat_alloc_user_space(sizeof(*idle));
318
319         err = do_ioctl(file, PPPIOCGIDLE, (unsigned long) idle);
320
321         if (!err) {
322                 if (get_user(xmit, &idle->xmit_idle) ||
323                     get_user(recv, &idle->recv_idle) ||
324                     put_user(xmit, &idle32->xmit_idle) ||
325                     put_user(recv, &idle32->recv_idle))
326                         err = -EFAULT;
327         }
328         return err;
329 }
330
331 static int ppp_scompress(struct file *file, unsigned int cmd,
332         struct ppp_option_data32 __user *odata32)
333 {
334         struct ppp_option_data __user *odata;
335         __u32 data;
336         void __user *datap;
337
338         odata = compat_alloc_user_space(sizeof(*odata));
339
340         if (get_user(data, &odata32->ptr))
341                 return -EFAULT;
342
343         datap = compat_ptr(data);
344         if (put_user(datap, &odata->ptr))
345                 return -EFAULT;
346
347         if (copy_in_user(&odata->length, &odata32->length,
348                          sizeof(__u32) + sizeof(int)))
349                 return -EFAULT;
350
351         return do_ioctl(file, PPPIOCSCOMPRESS, (unsigned long) odata);
352 }
353
354 /*
355  * simple reversible transform to make our table more evenly
356  * distributed after sorting.
357  */
358 #define XFORM(i) (((i) ^ ((i) << 27) ^ ((i) << 17)) & 0xffffffff)
359
360 #define COMPATIBLE_IOCTL(cmd) XFORM((u32)cmd),
361 /* ioctl should not be warned about even if it's not implemented.
362    Valid reasons to use this:
363    - It is implemented with ->compat_ioctl on some device, but programs
364    call it on others too.
365    - The ioctl is not implemented in the native kernel, but programs
366    call it commonly anyways.
367    Most other reasons are not valid. */
368 #define IGNORE_IOCTL(cmd) COMPATIBLE_IOCTL(cmd)
369
370 static unsigned int ioctl_pointer[] = {
371 /* compatible ioctls first */
372 /* Little t */
373 COMPATIBLE_IOCTL(TIOCOUTQ)
374 /* 'X' - originally XFS but some now in the VFS */
375 COMPATIBLE_IOCTL(FITRIM)
376 #ifdef CONFIG_BLOCK
377 /* Big S */
378 COMPATIBLE_IOCTL(SCSI_IOCTL_GET_IDLUN)
379 COMPATIBLE_IOCTL(SCSI_IOCTL_DOORLOCK)
380 COMPATIBLE_IOCTL(SCSI_IOCTL_DOORUNLOCK)
381 COMPATIBLE_IOCTL(SCSI_IOCTL_TEST_UNIT_READY)
382 COMPATIBLE_IOCTL(SCSI_IOCTL_GET_BUS_NUMBER)
383 COMPATIBLE_IOCTL(SCSI_IOCTL_SEND_COMMAND)
384 COMPATIBLE_IOCTL(SCSI_IOCTL_PROBE_HOST)
385 COMPATIBLE_IOCTL(SCSI_IOCTL_GET_PCI)
386 #endif
387 /* Big V (don't complain on serial console) */
388 IGNORE_IOCTL(VT_OPENQRY)
389 IGNORE_IOCTL(VT_GETMODE)
390 /*
391  * These two are only for the sbus rtc driver, but
392  * hwclock tries them on every rtc device first when
393  * running on sparc.  On other architectures the entries
394  * are useless but harmless.
395  */
396 COMPATIBLE_IOCTL(_IOR('p', 20, int[7])) /* RTCGET */
397 COMPATIBLE_IOCTL(_IOW('p', 21, int[7])) /* RTCSET */
398 #ifdef CONFIG_BLOCK
399 /* md calls this on random blockdevs */
400 IGNORE_IOCTL(RAID_VERSION)
401 /* qemu/qemu-img might call these two on plain files for probing */
402 IGNORE_IOCTL(CDROM_DRIVE_STATUS)
403 IGNORE_IOCTL(FDGETPRM32)
404 /* SG stuff */
405 COMPATIBLE_IOCTL(SG_SET_TIMEOUT)
406 COMPATIBLE_IOCTL(SG_GET_TIMEOUT)
407 COMPATIBLE_IOCTL(SG_EMULATED_HOST)
408 COMPATIBLE_IOCTL(SG_GET_TRANSFORM)
409 COMPATIBLE_IOCTL(SG_SET_RESERVED_SIZE)
410 COMPATIBLE_IOCTL(SG_GET_RESERVED_SIZE)
411 COMPATIBLE_IOCTL(SG_GET_SCSI_ID)
412 COMPATIBLE_IOCTL(SG_SET_FORCE_LOW_DMA)
413 COMPATIBLE_IOCTL(SG_GET_LOW_DMA)
414 COMPATIBLE_IOCTL(SG_SET_FORCE_PACK_ID)
415 COMPATIBLE_IOCTL(SG_GET_PACK_ID)
416 COMPATIBLE_IOCTL(SG_GET_NUM_WAITING)
417 COMPATIBLE_IOCTL(SG_SET_DEBUG)
418 COMPATIBLE_IOCTL(SG_GET_SG_TABLESIZE)
419 COMPATIBLE_IOCTL(SG_GET_COMMAND_Q)
420 COMPATIBLE_IOCTL(SG_SET_COMMAND_Q)
421 COMPATIBLE_IOCTL(SG_GET_VERSION_NUM)
422 COMPATIBLE_IOCTL(SG_NEXT_CMD_LEN)
423 COMPATIBLE_IOCTL(SG_SCSI_RESET)
424 COMPATIBLE_IOCTL(SG_GET_REQUEST_TABLE)
425 COMPATIBLE_IOCTL(SG_SET_KEEP_ORPHAN)
426 COMPATIBLE_IOCTL(SG_GET_KEEP_ORPHAN)
427 #endif
428 /* PPP stuff */
429 COMPATIBLE_IOCTL(PPPIOCGFLAGS)
430 COMPATIBLE_IOCTL(PPPIOCSFLAGS)
431 COMPATIBLE_IOCTL(PPPIOCGASYNCMAP)
432 COMPATIBLE_IOCTL(PPPIOCSASYNCMAP)
433 COMPATIBLE_IOCTL(PPPIOCGUNIT)
434 COMPATIBLE_IOCTL(PPPIOCGRASYNCMAP)
435 COMPATIBLE_IOCTL(PPPIOCSRASYNCMAP)
436 COMPATIBLE_IOCTL(PPPIOCGMRU)
437 COMPATIBLE_IOCTL(PPPIOCSMRU)
438 COMPATIBLE_IOCTL(PPPIOCSMAXCID)
439 COMPATIBLE_IOCTL(PPPIOCGXASYNCMAP)
440 COMPATIBLE_IOCTL(PPPIOCSXASYNCMAP)
441 COMPATIBLE_IOCTL(PPPIOCXFERUNIT)
442 /* PPPIOCSCOMPRESS is translated */
443 COMPATIBLE_IOCTL(PPPIOCGNPMODE)
444 COMPATIBLE_IOCTL(PPPIOCSNPMODE)
445 COMPATIBLE_IOCTL(PPPIOCGDEBUG)
446 COMPATIBLE_IOCTL(PPPIOCSDEBUG)
447 /* PPPIOCSPASS is translated */
448 /* PPPIOCSACTIVE is translated */
449 /* PPPIOCGIDLE is translated */
450 COMPATIBLE_IOCTL(PPPIOCNEWUNIT)
451 COMPATIBLE_IOCTL(PPPIOCATTACH)
452 COMPATIBLE_IOCTL(PPPIOCDETACH)
453 COMPATIBLE_IOCTL(PPPIOCSMRRU)
454 COMPATIBLE_IOCTL(PPPIOCCONNECT)
455 COMPATIBLE_IOCTL(PPPIOCDISCONN)
456 COMPATIBLE_IOCTL(PPPIOCATTCHAN)
457 COMPATIBLE_IOCTL(PPPIOCGCHAN)
458 COMPATIBLE_IOCTL(PPPIOCGL2TPSTATS)
459 /* Raw devices */
460 COMPATIBLE_IOCTL(RAW_SETBIND)
461 COMPATIBLE_IOCTL(RAW_GETBIND)
462 /* Watchdog */
463 COMPATIBLE_IOCTL(WDIOC_GETSUPPORT)
464 COMPATIBLE_IOCTL(WDIOC_GETSTATUS)
465 COMPATIBLE_IOCTL(WDIOC_GETBOOTSTATUS)
466 COMPATIBLE_IOCTL(WDIOC_GETTEMP)
467 COMPATIBLE_IOCTL(WDIOC_SETOPTIONS)
468 COMPATIBLE_IOCTL(WDIOC_KEEPALIVE)
469 COMPATIBLE_IOCTL(WDIOC_SETTIMEOUT)
470 COMPATIBLE_IOCTL(WDIOC_GETTIMEOUT)
471 COMPATIBLE_IOCTL(WDIOC_SETPRETIMEOUT)
472 COMPATIBLE_IOCTL(WDIOC_GETPRETIMEOUT)
473 /* Big R */
474 COMPATIBLE_IOCTL(RNDGETENTCNT)
475 COMPATIBLE_IOCTL(RNDADDTOENTCNT)
476 COMPATIBLE_IOCTL(RNDGETPOOL)
477 COMPATIBLE_IOCTL(RNDADDENTROPY)
478 COMPATIBLE_IOCTL(RNDZAPENTCNT)
479 COMPATIBLE_IOCTL(RNDCLEARPOOL)
480 /* Misc. */
481 COMPATIBLE_IOCTL(PCIIOC_CONTROLLER)
482 COMPATIBLE_IOCTL(PCIIOC_MMAP_IS_IO)
483 COMPATIBLE_IOCTL(PCIIOC_MMAP_IS_MEM)
484 COMPATIBLE_IOCTL(PCIIOC_WRITE_COMBINE)
485 /* joystick */
486 COMPATIBLE_IOCTL(JSIOCGVERSION)
487 COMPATIBLE_IOCTL(JSIOCGAXES)
488 COMPATIBLE_IOCTL(JSIOCGBUTTONS)
489 COMPATIBLE_IOCTL(JSIOCGNAME(0))
490
491 /* fat 'r' ioctls. These are handled by fat with ->compat_ioctl,
492    but we don't want warnings on other file systems. So declare
493    them as compatible here. */
494 #define VFAT_IOCTL_READDIR_BOTH32       _IOR('r', 1, struct compat_dirent[2])
495 #define VFAT_IOCTL_READDIR_SHORT32      _IOR('r', 2, struct compat_dirent[2])
496
497 IGNORE_IOCTL(VFAT_IOCTL_READDIR_BOTH32)
498 IGNORE_IOCTL(VFAT_IOCTL_READDIR_SHORT32)
499
500 #ifdef CONFIG_SPARC
501 /* Sparc framebuffers, handled in sbusfb_compat_ioctl() */
502 IGNORE_IOCTL(FBIOGTYPE)
503 IGNORE_IOCTL(FBIOSATTR)
504 IGNORE_IOCTL(FBIOGATTR)
505 IGNORE_IOCTL(FBIOSVIDEO)
506 IGNORE_IOCTL(FBIOGVIDEO)
507 IGNORE_IOCTL(FBIOSCURPOS)
508 IGNORE_IOCTL(FBIOGCURPOS)
509 IGNORE_IOCTL(FBIOGCURMAX)
510 IGNORE_IOCTL(FBIOPUTCMAP32)
511 IGNORE_IOCTL(FBIOGETCMAP32)
512 IGNORE_IOCTL(FBIOSCURSOR32)
513 IGNORE_IOCTL(FBIOGCURSOR32)
514 #endif
515 };
516
517 /*
518  * Convert common ioctl arguments based on their command number
519  *
520  * Please do not add any code in here. Instead, implement
521  * a compat_ioctl operation in the place that handleŃ• the
522  * ioctl for the native case.
523  */
524 static long do_ioctl_trans(unsigned int cmd,
525                  unsigned long arg, struct file *file)
526 {
527         void __user *argp = compat_ptr(arg);
528
529         switch (cmd) {
530         case PPPIOCGIDLE32:
531                 return ppp_gidle(file, cmd, argp);
532         case PPPIOCSCOMPRESS32:
533                 return ppp_scompress(file, cmd, argp);
534         case PPPIOCSPASS32:
535         case PPPIOCSACTIVE32:
536                 return ppp_sock_fprog_ioctl_trans(file, cmd, argp);
537 #ifdef CONFIG_BLOCK
538         case SG_IO:
539                 return sg_ioctl_trans(file, cmd, argp);
540         case SG_GET_REQUEST_TABLE:
541                 return sg_grt_trans(file, cmd, argp);
542 #endif
543         }
544
545         /*
546          * These take an integer instead of a pointer as 'arg',
547          * so we must not do a compat_ptr() translation.
548          */
549         switch (cmd) {
550         /* RAID */
551         case HOT_REMOVE_DISK:
552         case HOT_ADD_DISK:
553         case SET_DISK_FAULTY:
554         case SET_BITMAP_FILE:
555                 return vfs_ioctl(file, cmd, arg);
556         }
557
558         return -ENOIOCTLCMD;
559 }
560
561 static int compat_ioctl_check_table(unsigned int xcmd)
562 {
563         int i;
564         const int max = ARRAY_SIZE(ioctl_pointer) - 1;
565
566         BUILD_BUG_ON(max >= (1 << 16));
567
568         /* guess initial offset into table, assuming a
569            normalized distribution */
570         i = ((xcmd >> 16) * max) >> 16;
571
572         /* do linear search up first, until greater or equal */
573         while (ioctl_pointer[i] < xcmd && i < max)
574                 i++;
575
576         /* then do linear search down */
577         while (ioctl_pointer[i] > xcmd && i > 0)
578                 i--;
579
580         return ioctl_pointer[i] == xcmd;
581 }
582
583 COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
584                        compat_ulong_t, arg32)
585 {
586         unsigned long arg = arg32;
587         struct fd f = fdget(fd);
588         int error = -EBADF;
589         if (!f.file)
590                 goto out;
591
592         /* RED-PEN how should LSM module know it's handling 32bit? */
593         error = security_file_ioctl(f.file, cmd, arg);
594         if (error)
595                 goto out_fput;
596
597         switch (cmd) {
598         /* these are never seen by ->ioctl(), no argument or int argument */
599         case FIOCLEX:
600         case FIONCLEX:
601         case FIFREEZE:
602         case FITHAW:
603         case FICLONE:
604                 goto do_ioctl;
605         /* these are never seen by ->ioctl(), pointer argument */
606         case FIONBIO:
607         case FIOASYNC:
608         case FIOQSIZE:
609         case FS_IOC_FIEMAP:
610         case FIGETBSZ:
611         case FICLONERANGE:
612         case FIDEDUPERANGE:
613                 goto found_handler;
614         /*
615          * The next group is the stuff handled inside file_ioctl().
616          * For regular files these never reach ->ioctl(); for
617          * devices, sockets, etc. they do and one (FIONREAD) is
618          * even accepted in some cases.  In all those cases
619          * argument has the same type, so we can handle these
620          * here, shunting them towards do_vfs_ioctl().
621          * ->compat_ioctl() will never see any of those.
622          */
623         /* pointer argument, never actually handled by ->ioctl() */
624         case FIBMAP:
625                 goto found_handler;
626         /* handled by some ->ioctl(); always a pointer to int */
627         case FIONREAD:
628                 goto found_handler;
629         /* these two get messy on amd64 due to alignment differences */
630 #if defined(CONFIG_X86_64)
631         case FS_IOC_RESVSP_32:
632         case FS_IOC_RESVSP64_32:
633                 error = compat_ioctl_preallocate(f.file, compat_ptr(arg));
634                 goto out_fput;
635 #else
636         case FS_IOC_RESVSP:
637         case FS_IOC_RESVSP64:
638                 goto found_handler;
639 #endif
640
641         default:
642                 if (f.file->f_op->compat_ioctl) {
643                         error = f.file->f_op->compat_ioctl(f.file, cmd, arg);
644                         if (error != -ENOIOCTLCMD)
645                                 goto out_fput;
646                 }
647
648                 if (!f.file->f_op->unlocked_ioctl)
649                         goto do_ioctl;
650                 break;
651         }
652
653         if (compat_ioctl_check_table(XFORM(cmd)))
654                 goto found_handler;
655
656         error = do_ioctl_trans(cmd, arg, f.file);
657         if (error == -ENOIOCTLCMD)
658                 error = -ENOTTY;
659
660         goto out_fput;
661
662  found_handler:
663         arg = (unsigned long)compat_ptr(arg);
664  do_ioctl:
665         error = do_vfs_ioctl(f.file, fd, cmd, arg);
666  out_fput:
667         fdput(f);
668  out:
669         return error;
670 }
671
672 static int __init init_sys32_ioctl_cmp(const void *p, const void *q)
673 {
674         unsigned int a, b;
675         a = *(unsigned int *)p;
676         b = *(unsigned int *)q;
677         if (a > b)
678                 return 1;
679         if (a < b)
680                 return -1;
681         return 0;
682 }
683
684 static int __init init_sys32_ioctl(void)
685 {
686         sort(ioctl_pointer, ARRAY_SIZE(ioctl_pointer), sizeof(*ioctl_pointer),
687                 init_sys32_ioctl_cmp, NULL);
688         return 0;
689 }
690 __initcall(init_sys32_ioctl);