[PATCH] x86-64: Print type and size correctly for unknown compat ioctls
[safe/jmp/linux-2.6] / fs / compat.c
1 /*
2  *  linux/fs/compat.c
3  *
4  *  Kernel compatibililty routines for e.g. 32 bit syscall support
5  *  on 64 bit kernels.
6  *
7  *  Copyright (C) 2002       Stephen Rothwell, IBM Corporation
8  *  Copyright (C) 1997-2000  Jakub Jelinek  (jakub@redhat.com)
9  *  Copyright (C) 1998       Eddie C. Dost  (ecd@skynet.be)
10  *  Copyright (C) 2001,2002  Andi Kleen, SuSE Labs 
11  *  Copyright (C) 2003       Pavel Machek (pavel@suse.cz)
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License version 2 as
15  *  published by the Free Software Foundation.
16  */
17
18 #include <linux/linkage.h>
19 #include <linux/compat.h>
20 #include <linux/errno.h>
21 #include <linux/time.h>
22 #include <linux/fs.h>
23 #include <linux/fcntl.h>
24 #include <linux/namei.h>
25 #include <linux/file.h>
26 #include <linux/vfs.h>
27 #include <linux/ioctl32.h>
28 #include <linux/ioctl.h>
29 #include <linux/init.h>
30 #include <linux/sockios.h>      /* for SIOCDEVPRIVATE */
31 #include <linux/smb.h>
32 #include <linux/smb_mount.h>
33 #include <linux/ncp_mount.h>
34 #include <linux/nfs4_mount.h>
35 #include <linux/smp_lock.h>
36 #include <linux/syscalls.h>
37 #include <linux/ctype.h>
38 #include <linux/module.h>
39 #include <linux/dirent.h>
40 #include <linux/fsnotify.h>
41 #include <linux/highuid.h>
42 #include <linux/sunrpc/svc.h>
43 #include <linux/nfsd/nfsd.h>
44 #include <linux/nfsd/syscall.h>
45 #include <linux/personality.h>
46 #include <linux/rwsem.h>
47 #include <linux/tsacct_kern.h>
48 #include <linux/highmem.h>
49 #include <linux/poll.h>
50 #include <linux/mm.h>
51 #include <linux/eventpoll.h>
52
53 #include <net/sock.h>           /* siocdevprivate_ioctl */
54
55 #include <asm/uaccess.h>
56 #include <asm/mmu_context.h>
57 #include <asm/ioctls.h>
58 #include "internal.h"
59
60 int compat_log = 1;
61
62 int compat_printk(const char *fmt, ...)
63 {
64         va_list ap;
65         int ret;
66         if (!compat_log)
67                 return 0;
68         va_start(ap, fmt);
69         ret = vprintk(fmt, ap);
70         va_end(ap);
71         return ret;
72 }
73
74 #include "read_write.h"
75
76 /*
77  * Not all architectures have sys_utime, so implement this in terms
78  * of sys_utimes.
79  */
80 asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
81 {
82         struct timeval tv[2];
83
84         if (t) {
85                 if (get_user(tv[0].tv_sec, &t->actime) ||
86                     get_user(tv[1].tv_sec, &t->modtime))
87                         return -EFAULT;
88                 tv[0].tv_usec = 0;
89                 tv[1].tv_usec = 0;
90         }
91         return do_utimes(AT_FDCWD, filename, t ? tv : NULL);
92 }
93
94 asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t)
95 {
96         struct timeval tv[2];
97
98         if (t) {
99                 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
100                     get_user(tv[0].tv_usec, &t[0].tv_usec) ||
101                     get_user(tv[1].tv_sec, &t[1].tv_sec) ||
102                     get_user(tv[1].tv_usec, &t[1].tv_usec))
103                         return -EFAULT;
104         }
105         return do_utimes(dfd, filename, t ? tv : NULL);
106 }
107
108 asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
109 {
110         return compat_sys_futimesat(AT_FDCWD, filename, t);
111 }
112
113 asmlinkage long compat_sys_newstat(char __user * filename,
114                 struct compat_stat __user *statbuf)
115 {
116         struct kstat stat;
117         int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
118
119         if (!error)
120                 error = cp_compat_stat(&stat, statbuf);
121         return error;
122 }
123
124 asmlinkage long compat_sys_newlstat(char __user * filename,
125                 struct compat_stat __user *statbuf)
126 {
127         struct kstat stat;
128         int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
129
130         if (!error)
131                 error = cp_compat_stat(&stat, statbuf);
132         return error;
133 }
134
135 #ifndef __ARCH_WANT_STAT64
136 asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user *filename,
137                 struct compat_stat __user *statbuf, int flag)
138 {
139         struct kstat stat;
140         int error = -EINVAL;
141
142         if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
143                 goto out;
144
145         if (flag & AT_SYMLINK_NOFOLLOW)
146                 error = vfs_lstat_fd(dfd, filename, &stat);
147         else
148                 error = vfs_stat_fd(dfd, filename, &stat);
149
150         if (!error)
151                 error = cp_compat_stat(&stat, statbuf);
152
153 out:
154         return error;
155 }
156 #endif
157
158 asmlinkage long compat_sys_newfstat(unsigned int fd,
159                 struct compat_stat __user * statbuf)
160 {
161         struct kstat stat;
162         int error = vfs_fstat(fd, &stat);
163
164         if (!error)
165                 error = cp_compat_stat(&stat, statbuf);
166         return error;
167 }
168
169 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
170 {
171         
172         if (sizeof ubuf->f_blocks == 4) {
173                 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
174                     0xffffffff00000000ULL)
175                         return -EOVERFLOW;
176                 /* f_files and f_ffree may be -1; it's okay
177                  * to stuff that into 32 bits */
178                 if (kbuf->f_files != 0xffffffffffffffffULL
179                  && (kbuf->f_files & 0xffffffff00000000ULL))
180                         return -EOVERFLOW;
181                 if (kbuf->f_ffree != 0xffffffffffffffffULL
182                  && (kbuf->f_ffree & 0xffffffff00000000ULL))
183                         return -EOVERFLOW;
184         }
185         if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
186             __put_user(kbuf->f_type, &ubuf->f_type) ||
187             __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
188             __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
189             __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
190             __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
191             __put_user(kbuf->f_files, &ubuf->f_files) ||
192             __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
193             __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
194             __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
195             __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
196             __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
197             __put_user(0, &ubuf->f_spare[0]) || 
198             __put_user(0, &ubuf->f_spare[1]) || 
199             __put_user(0, &ubuf->f_spare[2]) || 
200             __put_user(0, &ubuf->f_spare[3]) || 
201             __put_user(0, &ubuf->f_spare[4]))
202                 return -EFAULT;
203         return 0;
204 }
205
206 /*
207  * The following statfs calls are copies of code from fs/open.c and
208  * should be checked against those from time to time
209  */
210 asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
211 {
212         struct nameidata nd;
213         int error;
214
215         error = user_path_walk(path, &nd);
216         if (!error) {
217                 struct kstatfs tmp;
218                 error = vfs_statfs(nd.dentry, &tmp);
219                 if (!error)
220                         error = put_compat_statfs(buf, &tmp);
221                 path_release(&nd);
222         }
223         return error;
224 }
225
226 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
227 {
228         struct file * file;
229         struct kstatfs tmp;
230         int error;
231
232         error = -EBADF;
233         file = fget(fd);
234         if (!file)
235                 goto out;
236         error = vfs_statfs(file->f_path.dentry, &tmp);
237         if (!error)
238                 error = put_compat_statfs(buf, &tmp);
239         fput(file);
240 out:
241         return error;
242 }
243
244 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
245 {
246         if (sizeof ubuf->f_blocks == 4) {
247                 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
248                     0xffffffff00000000ULL)
249                         return -EOVERFLOW;
250                 /* f_files and f_ffree may be -1; it's okay
251                  * to stuff that into 32 bits */
252                 if (kbuf->f_files != 0xffffffffffffffffULL
253                  && (kbuf->f_files & 0xffffffff00000000ULL))
254                         return -EOVERFLOW;
255                 if (kbuf->f_ffree != 0xffffffffffffffffULL
256                  && (kbuf->f_ffree & 0xffffffff00000000ULL))
257                         return -EOVERFLOW;
258         }
259         if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
260             __put_user(kbuf->f_type, &ubuf->f_type) ||
261             __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
262             __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
263             __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
264             __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
265             __put_user(kbuf->f_files, &ubuf->f_files) ||
266             __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
267             __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
268             __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
269             __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
270             __put_user(kbuf->f_frsize, &ubuf->f_frsize))
271                 return -EFAULT;
272         return 0;
273 }
274
275 asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
276 {
277         struct nameidata nd;
278         int error;
279
280         if (sz != sizeof(*buf))
281                 return -EINVAL;
282
283         error = user_path_walk(path, &nd);
284         if (!error) {
285                 struct kstatfs tmp;
286                 error = vfs_statfs(nd.dentry, &tmp);
287                 if (!error)
288                         error = put_compat_statfs64(buf, &tmp);
289                 path_release(&nd);
290         }
291         return error;
292 }
293
294 asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
295 {
296         struct file * file;
297         struct kstatfs tmp;
298         int error;
299
300         if (sz != sizeof(*buf))
301                 return -EINVAL;
302
303         error = -EBADF;
304         file = fget(fd);
305         if (!file)
306                 goto out;
307         error = vfs_statfs(file->f_path.dentry, &tmp);
308         if (!error)
309                 error = put_compat_statfs64(buf, &tmp);
310         fput(file);
311 out:
312         return error;
313 }
314
315 /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
316
317 #define IOCTL_HASHSIZE 256
318 static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
319
320 static inline unsigned long ioctl32_hash(unsigned long cmd)
321 {
322         return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
323 }
324
325 static void ioctl32_insert_translation(struct ioctl_trans *trans)
326 {
327         unsigned long hash;
328         struct ioctl_trans *t;
329
330         hash = ioctl32_hash (trans->cmd);
331         if (!ioctl32_hash_table[hash])
332                 ioctl32_hash_table[hash] = trans;
333         else {
334                 t = ioctl32_hash_table[hash];
335                 while (t->next)
336                         t = t->next;
337                 trans->next = NULL;
338                 t->next = trans;
339         }
340 }
341
342 static int __init init_sys32_ioctl(void)
343 {
344         int i;
345
346         for (i = 0; i < ioctl_table_size; i++) {
347                 if (ioctl_start[i].next != 0) { 
348                         printk("ioctl translation %d bad\n",i); 
349                         return -1;
350                 }
351
352                 ioctl32_insert_translation(&ioctl_start[i]);
353         }
354         return 0;
355 }
356
357 __initcall(init_sys32_ioctl);
358
359 static void compat_ioctl_error(struct file *filp, unsigned int fd,
360                 unsigned int cmd, unsigned long arg)
361 {
362         char buf[10];
363         char *fn = "?";
364         char *path;
365
366         /* find the name of the device. */
367         path = (char *)__get_free_page(GFP_KERNEL);
368         if (path) {
369                 fn = d_path(filp->f_path.dentry, filp->f_path.mnt, path, PAGE_SIZE);
370                 if (IS_ERR(fn))
371                         fn = "?";
372         }
373
374         sprintf(buf,"'%c'", (cmd>>_IOC_TYPESHIFT) & _IOC_TYPEMASK);
375         if (!isprint(buf[1]))
376                 sprintf(buf, "%02x", buf[1]);
377         compat_printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
378                         "cmd(%08x){t:%s;sz:%u} arg(%08x) on %s\n",
379                         current->comm, current->pid,
380                         (int)fd, (unsigned int)cmd, buf,
381                         (cmd >> _IOC_SIZESHIFT) & _IOC_SIZEMASK,
382                         (unsigned int)arg, fn);
383
384         if (path)
385                 free_page((unsigned long)path);
386 }
387
388 asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
389                                 unsigned long arg)
390 {
391         struct file *filp;
392         int error = -EBADF;
393         struct ioctl_trans *t;
394         int fput_needed;
395
396         filp = fget_light(fd, &fput_needed);
397         if (!filp)
398                 goto out;
399
400         /* RED-PEN how should LSM module know it's handling 32bit? */
401         error = security_file_ioctl(filp, cmd, arg);
402         if (error)
403                 goto out_fput;
404
405         /*
406          * To allow the compat_ioctl handlers to be self contained
407          * we need to check the common ioctls here first.
408          * Just handle them with the standard handlers below.
409          */
410         switch (cmd) {
411         case FIOCLEX:
412         case FIONCLEX:
413         case FIONBIO:
414         case FIOASYNC:
415         case FIOQSIZE:
416                 break;
417
418         case FIBMAP:
419         case FIGETBSZ:
420         case FIONREAD:
421                 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
422                         break;
423                 /*FALL THROUGH*/
424
425         default:
426                 if (filp->f_op && filp->f_op->compat_ioctl) {
427                         error = filp->f_op->compat_ioctl(filp, cmd, arg);
428                         if (error != -ENOIOCTLCMD)
429                                 goto out_fput;
430                 }
431
432                 if (!filp->f_op ||
433                     (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
434                         goto do_ioctl;
435                 break;
436         }
437
438         for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
439                 if (t->cmd == cmd)
440                         goto found_handler;
441         }
442
443         if (S_ISSOCK(filp->f_path.dentry->d_inode->i_mode) &&
444             cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
445                 error = siocdevprivate_ioctl(fd, cmd, arg);
446         } else {
447                 static int count;
448
449                 if (++count <= 50)
450                         compat_ioctl_error(filp, fd, cmd, arg);
451                 error = -EINVAL;
452         }
453
454         goto out_fput;
455
456  found_handler:
457         if (t->handler) {
458                 lock_kernel();
459                 error = t->handler(fd, cmd, arg, filp);
460                 unlock_kernel();
461                 goto out_fput;
462         }
463
464  do_ioctl:
465         error = vfs_ioctl(filp, fd, cmd, arg);
466  out_fput:
467         fput_light(filp, fput_needed);
468  out:
469         return error;
470 }
471
472 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
473 {
474         if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
475             __get_user(kfl->l_type, &ufl->l_type) ||
476             __get_user(kfl->l_whence, &ufl->l_whence) ||
477             __get_user(kfl->l_start, &ufl->l_start) ||
478             __get_user(kfl->l_len, &ufl->l_len) ||
479             __get_user(kfl->l_pid, &ufl->l_pid))
480                 return -EFAULT;
481         return 0;
482 }
483
484 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
485 {
486         if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
487             __put_user(kfl->l_type, &ufl->l_type) ||
488             __put_user(kfl->l_whence, &ufl->l_whence) ||
489             __put_user(kfl->l_start, &ufl->l_start) ||
490             __put_user(kfl->l_len, &ufl->l_len) ||
491             __put_user(kfl->l_pid, &ufl->l_pid))
492                 return -EFAULT;
493         return 0;
494 }
495
496 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
497 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
498 {
499         if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
500             __get_user(kfl->l_type, &ufl->l_type) ||
501             __get_user(kfl->l_whence, &ufl->l_whence) ||
502             __get_user(kfl->l_start, &ufl->l_start) ||
503             __get_user(kfl->l_len, &ufl->l_len) ||
504             __get_user(kfl->l_pid, &ufl->l_pid))
505                 return -EFAULT;
506         return 0;
507 }
508 #endif
509
510 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
511 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
512 {
513         if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
514             __put_user(kfl->l_type, &ufl->l_type) ||
515             __put_user(kfl->l_whence, &ufl->l_whence) ||
516             __put_user(kfl->l_start, &ufl->l_start) ||
517             __put_user(kfl->l_len, &ufl->l_len) ||
518             __put_user(kfl->l_pid, &ufl->l_pid))
519                 return -EFAULT;
520         return 0;
521 }
522 #endif
523
524 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
525                 unsigned long arg)
526 {
527         mm_segment_t old_fs;
528         struct flock f;
529         long ret;
530
531         switch (cmd) {
532         case F_GETLK:
533         case F_SETLK:
534         case F_SETLKW:
535                 ret = get_compat_flock(&f, compat_ptr(arg));
536                 if (ret != 0)
537                         break;
538                 old_fs = get_fs();
539                 set_fs(KERNEL_DS);
540                 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
541                 set_fs(old_fs);
542                 if (cmd == F_GETLK && ret == 0) {
543                         /* GETLK was successfule and we need to return the data...
544                          * but it needs to fit in the compat structure.
545                          * l_start shouldn't be too big, unless the original
546                          * start + end is greater than COMPAT_OFF_T_MAX, in which
547                          * case the app was asking for trouble, so we return
548                          * -EOVERFLOW in that case.
549                          * l_len could be too big, in which case we just truncate it,
550                          * and only allow the app to see that part of the conflicting
551                          * lock that might make sense to it anyway
552                          */
553
554                         if (f.l_start > COMPAT_OFF_T_MAX)
555                                 ret = -EOVERFLOW;
556                         if (f.l_len > COMPAT_OFF_T_MAX)
557                                 f.l_len = COMPAT_OFF_T_MAX;
558                         if (ret == 0)
559                                 ret = put_compat_flock(&f, compat_ptr(arg));
560                 }
561                 break;
562
563         case F_GETLK64:
564         case F_SETLK64:
565         case F_SETLKW64:
566                 ret = get_compat_flock64(&f, compat_ptr(arg));
567                 if (ret != 0)
568                         break;
569                 old_fs = get_fs();
570                 set_fs(KERNEL_DS);
571                 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
572                                 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
573                                 (unsigned long)&f);
574                 set_fs(old_fs);
575                 if (cmd == F_GETLK64 && ret == 0) {
576                         /* need to return lock information - see above for commentary */
577                         if (f.l_start > COMPAT_LOFF_T_MAX)
578                                 ret = -EOVERFLOW;
579                         if (f.l_len > COMPAT_LOFF_T_MAX)
580                                 f.l_len = COMPAT_LOFF_T_MAX;
581                         if (ret == 0)
582                                 ret = put_compat_flock64(&f, compat_ptr(arg));
583                 }
584                 break;
585
586         default:
587                 ret = sys_fcntl(fd, cmd, arg);
588                 break;
589         }
590         return ret;
591 }
592
593 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
594                 unsigned long arg)
595 {
596         if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
597                 return -EINVAL;
598         return compat_sys_fcntl64(fd, cmd, arg);
599 }
600
601 asmlinkage long
602 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
603 {
604         long ret;
605         aio_context_t ctx64;
606
607         mm_segment_t oldfs = get_fs();
608         if (unlikely(get_user(ctx64, ctx32p)))
609                 return -EFAULT;
610
611         set_fs(KERNEL_DS);
612         /* The __user pointer cast is valid because of the set_fs() */
613         ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
614         set_fs(oldfs);
615         /* truncating is ok because it's a user address */
616         if (!ret)
617                 ret = put_user((u32) ctx64, ctx32p);
618         return ret;
619 }
620
621 asmlinkage long
622 compat_sys_io_getevents(aio_context_t ctx_id,
623                                  unsigned long min_nr,
624                                  unsigned long nr,
625                                  struct io_event __user *events,
626                                  struct compat_timespec __user *timeout)
627 {
628         long ret;
629         struct timespec t;
630         struct timespec __user *ut = NULL;
631
632         ret = -EFAULT;
633         if (unlikely(!access_ok(VERIFY_WRITE, events, 
634                                 nr * sizeof(struct io_event))))
635                 goto out;
636         if (timeout) {
637                 if (get_compat_timespec(&t, timeout))
638                         goto out;
639
640                 ut = compat_alloc_user_space(sizeof(*ut));
641                 if (copy_to_user(ut, &t, sizeof(t)) )
642                         goto out;
643         } 
644         ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
645 out:
646         return ret;
647 }
648
649 static inline long
650 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
651 {
652         compat_uptr_t uptr;
653         int i;
654
655         for (i = 0; i < nr; ++i) {
656                 if (get_user(uptr, ptr32 + i))
657                         return -EFAULT;
658                 if (put_user(compat_ptr(uptr), ptr64 + i))
659                         return -EFAULT;
660         }
661         return 0;
662 }
663
664 #define MAX_AIO_SUBMITS         (PAGE_SIZE/sizeof(struct iocb *))
665
666 asmlinkage long
667 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
668 {
669         struct iocb __user * __user *iocb64; 
670         long ret;
671
672         if (unlikely(nr < 0))
673                 return -EINVAL;
674
675         if (nr > MAX_AIO_SUBMITS)
676                 nr = MAX_AIO_SUBMITS;
677         
678         iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
679         ret = copy_iocb(nr, iocb, iocb64);
680         if (!ret)
681                 ret = sys_io_submit(ctx_id, nr, iocb64);
682         return ret;
683 }
684
685 struct compat_ncp_mount_data {
686         compat_int_t version;
687         compat_uint_t ncp_fd;
688         __compat_uid_t mounted_uid;
689         compat_pid_t wdog_pid;
690         unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
691         compat_uint_t time_out;
692         compat_uint_t retry_count;
693         compat_uint_t flags;
694         __compat_uid_t uid;
695         __compat_gid_t gid;
696         compat_mode_t file_mode;
697         compat_mode_t dir_mode;
698 };
699
700 struct compat_ncp_mount_data_v4 {
701         compat_int_t version;
702         compat_ulong_t flags;
703         compat_ulong_t mounted_uid;
704         compat_long_t wdog_pid;
705         compat_uint_t ncp_fd;
706         compat_uint_t time_out;
707         compat_uint_t retry_count;
708         compat_ulong_t uid;
709         compat_ulong_t gid;
710         compat_ulong_t file_mode;
711         compat_ulong_t dir_mode;
712 };
713
714 static void *do_ncp_super_data_conv(void *raw_data)
715 {
716         int version = *(unsigned int *)raw_data;
717
718         if (version == 3) {
719                 struct compat_ncp_mount_data *c_n = raw_data;
720                 struct ncp_mount_data *n = raw_data;
721
722                 n->dir_mode = c_n->dir_mode;
723                 n->file_mode = c_n->file_mode;
724                 n->gid = c_n->gid;
725                 n->uid = c_n->uid;
726                 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
727                 n->wdog_pid = c_n->wdog_pid;
728                 n->mounted_uid = c_n->mounted_uid;
729         } else if (version == 4) {
730                 struct compat_ncp_mount_data_v4 *c_n = raw_data;
731                 struct ncp_mount_data_v4 *n = raw_data;
732
733                 n->dir_mode = c_n->dir_mode;
734                 n->file_mode = c_n->file_mode;
735                 n->gid = c_n->gid;
736                 n->uid = c_n->uid;
737                 n->retry_count = c_n->retry_count;
738                 n->time_out = c_n->time_out;
739                 n->ncp_fd = c_n->ncp_fd;
740                 n->wdog_pid = c_n->wdog_pid;
741                 n->mounted_uid = c_n->mounted_uid;
742                 n->flags = c_n->flags;
743         } else if (version != 5) {
744                 return NULL;
745         }
746
747         return raw_data;
748 }
749
750 struct compat_smb_mount_data {
751         compat_int_t version;
752         __compat_uid_t mounted_uid;
753         __compat_uid_t uid;
754         __compat_gid_t gid;
755         compat_mode_t file_mode;
756         compat_mode_t dir_mode;
757 };
758
759 static void *do_smb_super_data_conv(void *raw_data)
760 {
761         struct smb_mount_data *s = raw_data;
762         struct compat_smb_mount_data *c_s = raw_data;
763
764         if (c_s->version != SMB_MOUNT_OLDVERSION)
765                 goto out;
766         s->dir_mode = c_s->dir_mode;
767         s->file_mode = c_s->file_mode;
768         s->gid = c_s->gid;
769         s->uid = c_s->uid;
770         s->mounted_uid = c_s->mounted_uid;
771  out:
772         return raw_data;
773 }
774
775 struct compat_nfs_string {
776         compat_uint_t len;
777         compat_uptr_t data;
778 };
779
780 static inline void compat_nfs_string(struct nfs_string *dst,
781                                      struct compat_nfs_string *src)
782 {
783         dst->data = compat_ptr(src->data);
784         dst->len = src->len;
785 }
786
787 struct compat_nfs4_mount_data_v1 {
788         compat_int_t version;
789         compat_int_t flags;
790         compat_int_t rsize;
791         compat_int_t wsize;
792         compat_int_t timeo;
793         compat_int_t retrans;
794         compat_int_t acregmin;
795         compat_int_t acregmax;
796         compat_int_t acdirmin;
797         compat_int_t acdirmax;
798         struct compat_nfs_string client_addr;
799         struct compat_nfs_string mnt_path;
800         struct compat_nfs_string hostname;
801         compat_uint_t host_addrlen;
802         compat_uptr_t host_addr;
803         compat_int_t proto;
804         compat_int_t auth_flavourlen;
805         compat_uptr_t auth_flavours;
806 };
807
808 static int do_nfs4_super_data_conv(void *raw_data)
809 {
810         int version = *(compat_uint_t *) raw_data;
811
812         if (version == 1) {
813                 struct compat_nfs4_mount_data_v1 *raw = raw_data;
814                 struct nfs4_mount_data *real = raw_data;
815
816                 /* copy the fields backwards */
817                 real->auth_flavours = compat_ptr(raw->auth_flavours);
818                 real->auth_flavourlen = raw->auth_flavourlen;
819                 real->proto = raw->proto;
820                 real->host_addr = compat_ptr(raw->host_addr);
821                 real->host_addrlen = raw->host_addrlen;
822                 compat_nfs_string(&real->hostname, &raw->hostname);
823                 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
824                 compat_nfs_string(&real->client_addr, &raw->client_addr);
825                 real->acdirmax = raw->acdirmax;
826                 real->acdirmin = raw->acdirmin;
827                 real->acregmax = raw->acregmax;
828                 real->acregmin = raw->acregmin;
829                 real->retrans = raw->retrans;
830                 real->timeo = raw->timeo;
831                 real->wsize = raw->wsize;
832                 real->rsize = raw->rsize;
833                 real->flags = raw->flags;
834                 real->version = raw->version;
835         }
836         else {
837                 return -EINVAL;
838         }
839
840         return 0;
841 }
842
843 #define SMBFS_NAME      "smbfs"
844 #define NCPFS_NAME      "ncpfs"
845 #define NFS4_NAME       "nfs4"
846
847 asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
848                                  char __user * type, unsigned long flags,
849                                  void __user * data)
850 {
851         unsigned long type_page;
852         unsigned long data_page;
853         unsigned long dev_page;
854         char *dir_page;
855         int retval;
856
857         retval = copy_mount_options (type, &type_page);
858         if (retval < 0)
859                 goto out;
860
861         dir_page = getname(dir_name);
862         retval = PTR_ERR(dir_page);
863         if (IS_ERR(dir_page))
864                 goto out1;
865
866         retval = copy_mount_options (dev_name, &dev_page);
867         if (retval < 0)
868                 goto out2;
869
870         retval = copy_mount_options (data, &data_page);
871         if (retval < 0)
872                 goto out3;
873
874         retval = -EINVAL;
875
876         if (type_page && data_page) {
877                 if (!strcmp((char *)type_page, SMBFS_NAME)) {
878                         do_smb_super_data_conv((void *)data_page);
879                 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
880                         do_ncp_super_data_conv((void *)data_page);
881                 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
882                         if (do_nfs4_super_data_conv((void *) data_page))
883                                 goto out4;
884                 }
885         }
886
887         lock_kernel();
888         retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
889                         flags, (void*)data_page);
890         unlock_kernel();
891
892  out4:
893         free_page(data_page);
894  out3:
895         free_page(dev_page);
896  out2:
897         putname(dir_page);
898  out1:
899         free_page(type_page);
900  out:
901         return retval;
902 }
903
904 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
905 #define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
906                                 ~(sizeof(compat_long_t)-1))
907
908 struct compat_old_linux_dirent {
909         compat_ulong_t  d_ino;
910         compat_ulong_t  d_offset;
911         unsigned short  d_namlen;
912         char            d_name[1];
913 };
914
915 struct compat_readdir_callback {
916         struct compat_old_linux_dirent __user *dirent;
917         int result;
918 };
919
920 static int compat_fillonedir(void *__buf, const char *name, int namlen,
921                         loff_t offset, u64 ino, unsigned int d_type)
922 {
923         struct compat_readdir_callback *buf = __buf;
924         struct compat_old_linux_dirent __user *dirent;
925         compat_ulong_t d_ino;
926
927         if (buf->result)
928                 return -EINVAL;
929         d_ino = ino;
930         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
931                 return -EOVERFLOW;
932         buf->result++;
933         dirent = buf->dirent;
934         if (!access_ok(VERIFY_WRITE, dirent,
935                         (unsigned long)(dirent->d_name + namlen + 1) -
936                                 (unsigned long)dirent))
937                 goto efault;
938         if (    __put_user(d_ino, &dirent->d_ino) ||
939                 __put_user(offset, &dirent->d_offset) ||
940                 __put_user(namlen, &dirent->d_namlen) ||
941                 __copy_to_user(dirent->d_name, name, namlen) ||
942                 __put_user(0, dirent->d_name + namlen))
943                 goto efault;
944         return 0;
945 efault:
946         buf->result = -EFAULT;
947         return -EFAULT;
948 }
949
950 asmlinkage long compat_sys_old_readdir(unsigned int fd,
951         struct compat_old_linux_dirent __user *dirent, unsigned int count)
952 {
953         int error;
954         struct file *file;
955         struct compat_readdir_callback buf;
956
957         error = -EBADF;
958         file = fget(fd);
959         if (!file)
960                 goto out;
961
962         buf.result = 0;
963         buf.dirent = dirent;
964
965         error = vfs_readdir(file, compat_fillonedir, &buf);
966         if (error >= 0)
967                 error = buf.result;
968
969         fput(file);
970 out:
971         return error;
972 }
973
974 struct compat_linux_dirent {
975         compat_ulong_t  d_ino;
976         compat_ulong_t  d_off;
977         unsigned short  d_reclen;
978         char            d_name[1];
979 };
980
981 struct compat_getdents_callback {
982         struct compat_linux_dirent __user *current_dir;
983         struct compat_linux_dirent __user *previous;
984         int count;
985         int error;
986 };
987
988 static int compat_filldir(void *__buf, const char *name, int namlen,
989                 loff_t offset, u64 ino, unsigned int d_type)
990 {
991         struct compat_linux_dirent __user * dirent;
992         struct compat_getdents_callback *buf = __buf;
993         compat_ulong_t d_ino;
994         int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
995
996         buf->error = -EINVAL;   /* only used if we fail.. */
997         if (reclen > buf->count)
998                 return -EINVAL;
999         d_ino = ino;
1000         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1001                 return -EOVERFLOW;
1002         dirent = buf->previous;
1003         if (dirent) {
1004                 if (__put_user(offset, &dirent->d_off))
1005                         goto efault;
1006         }
1007         dirent = buf->current_dir;
1008         if (__put_user(d_ino, &dirent->d_ino))
1009                 goto efault;
1010         if (__put_user(reclen, &dirent->d_reclen))
1011                 goto efault;
1012         if (copy_to_user(dirent->d_name, name, namlen))
1013                 goto efault;
1014         if (__put_user(0, dirent->d_name + namlen))
1015                 goto efault;
1016         if (__put_user(d_type, (char  __user *) dirent + reclen - 1))
1017                 goto efault;
1018         buf->previous = dirent;
1019         dirent = (void __user *)dirent + reclen;
1020         buf->current_dir = dirent;
1021         buf->count -= reclen;
1022         return 0;
1023 efault:
1024         buf->error = -EFAULT;
1025         return -EFAULT;
1026 }
1027
1028 asmlinkage long compat_sys_getdents(unsigned int fd,
1029                 struct compat_linux_dirent __user *dirent, unsigned int count)
1030 {
1031         struct file * file;
1032         struct compat_linux_dirent __user * lastdirent;
1033         struct compat_getdents_callback buf;
1034         int error;
1035
1036         error = -EFAULT;
1037         if (!access_ok(VERIFY_WRITE, dirent, count))
1038                 goto out;
1039
1040         error = -EBADF;
1041         file = fget(fd);
1042         if (!file)
1043                 goto out;
1044
1045         buf.current_dir = dirent;
1046         buf.previous = NULL;
1047         buf.count = count;
1048         buf.error = 0;
1049
1050         error = vfs_readdir(file, compat_filldir, &buf);
1051         if (error < 0)
1052                 goto out_putf;
1053         error = buf.error;
1054         lastdirent = buf.previous;
1055         if (lastdirent) {
1056                 if (put_user(file->f_pos, &lastdirent->d_off))
1057                         error = -EFAULT;
1058                 else
1059                         error = count - buf.count;
1060         }
1061
1062 out_putf:
1063         fput(file);
1064 out:
1065         return error;
1066 }
1067
1068 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1069 #define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1070
1071 struct compat_getdents_callback64 {
1072         struct linux_dirent64 __user *current_dir;
1073         struct linux_dirent64 __user *previous;
1074         int count;
1075         int error;
1076 };
1077
1078 static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1079                      u64 ino, unsigned int d_type)
1080 {
1081         struct linux_dirent64 __user *dirent;
1082         struct compat_getdents_callback64 *buf = __buf;
1083         int jj = NAME_OFFSET(dirent);
1084         int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1085         u64 off;
1086
1087         buf->error = -EINVAL;   /* only used if we fail.. */
1088         if (reclen > buf->count)
1089                 return -EINVAL;
1090         dirent = buf->previous;
1091
1092         if (dirent) {
1093                 if (__put_user_unaligned(offset, &dirent->d_off))
1094                         goto efault;
1095         }
1096         dirent = buf->current_dir;
1097         if (__put_user_unaligned(ino, &dirent->d_ino))
1098                 goto efault;
1099         off = 0;
1100         if (__put_user_unaligned(off, &dirent->d_off))
1101                 goto efault;
1102         if (__put_user(reclen, &dirent->d_reclen))
1103                 goto efault;
1104         if (__put_user(d_type, &dirent->d_type))
1105                 goto efault;
1106         if (copy_to_user(dirent->d_name, name, namlen))
1107                 goto efault;
1108         if (__put_user(0, dirent->d_name + namlen))
1109                 goto efault;
1110         buf->previous = dirent;
1111         dirent = (void __user *)dirent + reclen;
1112         buf->current_dir = dirent;
1113         buf->count -= reclen;
1114         return 0;
1115 efault:
1116         buf->error = -EFAULT;
1117         return -EFAULT;
1118 }
1119
1120 asmlinkage long compat_sys_getdents64(unsigned int fd,
1121                 struct linux_dirent64 __user * dirent, unsigned int count)
1122 {
1123         struct file * file;
1124         struct linux_dirent64 __user * lastdirent;
1125         struct compat_getdents_callback64 buf;
1126         int error;
1127
1128         error = -EFAULT;
1129         if (!access_ok(VERIFY_WRITE, dirent, count))
1130                 goto out;
1131
1132         error = -EBADF;
1133         file = fget(fd);
1134         if (!file)
1135                 goto out;
1136
1137         buf.current_dir = dirent;
1138         buf.previous = NULL;
1139         buf.count = count;
1140         buf.error = 0;
1141
1142         error = vfs_readdir(file, compat_filldir64, &buf);
1143         if (error < 0)
1144                 goto out_putf;
1145         error = buf.error;
1146         lastdirent = buf.previous;
1147         if (lastdirent) {
1148                 typeof(lastdirent->d_off) d_off = file->f_pos;
1149                 error = -EFAULT;
1150                 if (__put_user_unaligned(d_off, &lastdirent->d_off))
1151                         goto out_putf;
1152                 error = count - buf.count;
1153         }
1154
1155 out_putf:
1156         fput(file);
1157 out:
1158         return error;
1159 }
1160 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1161
1162 static ssize_t compat_do_readv_writev(int type, struct file *file,
1163                                const struct compat_iovec __user *uvector,
1164                                unsigned long nr_segs, loff_t *pos)
1165 {
1166         compat_ssize_t tot_len;
1167         struct iovec iovstack[UIO_FASTIOV];
1168         struct iovec *iov=iovstack, *vector;
1169         ssize_t ret;
1170         int seg;
1171         io_fn_t fn;
1172         iov_fn_t fnv;
1173
1174         /*
1175          * SuS says "The readv() function *may* fail if the iovcnt argument
1176          * was less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1177          * traditionally returned zero for zero segments, so...
1178          */
1179         ret = 0;
1180         if (nr_segs == 0)
1181                 goto out;
1182
1183         /*
1184          * First get the "struct iovec" from user memory and
1185          * verify all the pointers
1186          */
1187         ret = -EINVAL;
1188         if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1189                 goto out;
1190         if (!file->f_op)
1191                 goto out;
1192         if (nr_segs > UIO_FASTIOV) {
1193                 ret = -ENOMEM;
1194                 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1195                 if (!iov)
1196                         goto out;
1197         }
1198         ret = -EFAULT;
1199         if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1200                 goto out;
1201
1202         /*
1203          * Single unix specification:
1204          * We should -EINVAL if an element length is not >= 0 and fitting an
1205          * ssize_t.  The total length is fitting an ssize_t
1206          *
1207          * Be careful here because iov_len is a size_t not an ssize_t
1208          */
1209         tot_len = 0;
1210         vector = iov;
1211         ret = -EINVAL;
1212         for (seg = 0 ; seg < nr_segs; seg++) {
1213                 compat_ssize_t tmp = tot_len;
1214                 compat_ssize_t len;
1215                 compat_uptr_t buf;
1216
1217                 if (__get_user(len, &uvector->iov_len) ||
1218                     __get_user(buf, &uvector->iov_base)) {
1219                         ret = -EFAULT;
1220                         goto out;
1221                 }
1222                 if (len < 0)    /* size_t not fitting an compat_ssize_t .. */
1223                         goto out;
1224                 tot_len += len;
1225                 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1226                         goto out;
1227                 vector->iov_base = compat_ptr(buf);
1228                 vector->iov_len = (compat_size_t) len;
1229                 uvector++;
1230                 vector++;
1231         }
1232         if (tot_len == 0) {
1233                 ret = 0;
1234                 goto out;
1235         }
1236
1237         ret = rw_verify_area(type, file, pos, tot_len);
1238         if (ret < 0)
1239                 goto out;
1240
1241         ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE);
1242         if (ret)
1243                 goto out;
1244
1245         fnv = NULL;
1246         if (type == READ) {
1247                 fn = file->f_op->read;
1248                 fnv = file->f_op->aio_read;
1249         } else {
1250                 fn = (io_fn_t)file->f_op->write;
1251                 fnv = file->f_op->aio_write;
1252         }
1253
1254         if (fnv)
1255                 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1256                                                 pos, fnv);
1257         else
1258                 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
1259
1260 out:
1261         if (iov != iovstack)
1262                 kfree(iov);
1263         if ((ret + (type == READ)) > 0) {
1264                 struct dentry *dentry = file->f_path.dentry;
1265                 if (type == READ)
1266                         fsnotify_access(dentry);
1267                 else
1268                         fsnotify_modify(dentry);
1269         }
1270         return ret;
1271 }
1272
1273 asmlinkage ssize_t
1274 compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1275 {
1276         struct file *file;
1277         ssize_t ret = -EBADF;
1278
1279         file = fget(fd);
1280         if (!file)
1281                 return -EBADF;
1282
1283         if (!(file->f_mode & FMODE_READ))
1284                 goto out;
1285
1286         ret = -EINVAL;
1287         if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
1288                 goto out;
1289
1290         ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1291
1292 out:
1293         fput(file);
1294         return ret;
1295 }
1296
1297 asmlinkage ssize_t
1298 compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1299 {
1300         struct file *file;
1301         ssize_t ret = -EBADF;
1302
1303         file = fget(fd);
1304         if (!file)
1305                 return -EBADF;
1306         if (!(file->f_mode & FMODE_WRITE))
1307                 goto out;
1308
1309         ret = -EINVAL;
1310         if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
1311                 goto out;
1312
1313         ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1314
1315 out:
1316         fput(file);
1317         return ret;
1318 }
1319
1320 asmlinkage long
1321 compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
1322                     unsigned int nr_segs, unsigned int flags)
1323 {
1324         unsigned i;
1325         struct iovec __user *iov;
1326         if (nr_segs > UIO_MAXIOV)
1327                 return -EINVAL;
1328         iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1329         for (i = 0; i < nr_segs; i++) {
1330                 struct compat_iovec v;
1331                 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1332                     get_user(v.iov_len, &iov32[i].iov_len) ||
1333                     put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1334                     put_user(v.iov_len, &iov[i].iov_len))
1335                         return -EFAULT;
1336         }
1337         return sys_vmsplice(fd, iov, nr_segs, flags);
1338 }
1339
1340 /*
1341  * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1342  * O_LARGEFILE flag.
1343  */
1344 asmlinkage long
1345 compat_sys_open(const char __user *filename, int flags, int mode)
1346 {
1347         return do_sys_open(AT_FDCWD, filename, flags, mode);
1348 }
1349
1350 /*
1351  * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1352  * O_LARGEFILE flag.
1353  */
1354 asmlinkage long
1355 compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
1356 {
1357         return do_sys_open(dfd, filename, flags, mode);
1358 }
1359
1360 /*
1361  * compat_count() counts the number of arguments/envelopes. It is basically
1362  * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1363  * and envp pointers.
1364  */
1365 static int compat_count(compat_uptr_t __user *argv, int max)
1366 {
1367         int i = 0;
1368
1369         if (argv != NULL) {
1370                 for (;;) {
1371                         compat_uptr_t p;
1372
1373                         if (get_user(p, argv))
1374                                 return -EFAULT;
1375                         if (!p)
1376                                 break;
1377                         argv++;
1378                         if(++i > max)
1379                                 return -E2BIG;
1380                 }
1381         }
1382         return i;
1383 }
1384
1385 /*
1386  * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1387  * except that it works with 32 bit argv and envp pointers.
1388  */
1389 static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1390                                 struct linux_binprm *bprm)
1391 {
1392         struct page *kmapped_page = NULL;
1393         char *kaddr = NULL;
1394         int ret;
1395
1396         while (argc-- > 0) {
1397                 compat_uptr_t str;
1398                 int len;
1399                 unsigned long pos;
1400
1401                 if (get_user(str, argv+argc) ||
1402                         !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1403                         ret = -EFAULT;
1404                         goto out;
1405                 }
1406
1407                 if (bprm->p < len)  {
1408                         ret = -E2BIG;
1409                         goto out;
1410                 }
1411
1412                 bprm->p -= len;
1413                 /* XXX: add architecture specific overflow check here. */
1414                 pos = bprm->p;
1415
1416                 while (len > 0) {
1417                         int i, new, err;
1418                         int offset, bytes_to_copy;
1419                         struct page *page;
1420
1421                         offset = pos % PAGE_SIZE;
1422                         i = pos/PAGE_SIZE;
1423                         page = bprm->page[i];
1424                         new = 0;
1425                         if (!page) {
1426                                 page = alloc_page(GFP_HIGHUSER);
1427                                 bprm->page[i] = page;
1428                                 if (!page) {
1429                                         ret = -ENOMEM;
1430                                         goto out;
1431                                 }
1432                                 new = 1;
1433                         }
1434
1435                         if (page != kmapped_page) {
1436                                 if (kmapped_page)
1437                                         kunmap(kmapped_page);
1438                                 kmapped_page = page;
1439                                 kaddr = kmap(kmapped_page);
1440                         }
1441                         if (new && offset)
1442                                 memset(kaddr, 0, offset);
1443                         bytes_to_copy = PAGE_SIZE - offset;
1444                         if (bytes_to_copy > len) {
1445                                 bytes_to_copy = len;
1446                                 if (new)
1447                                         memset(kaddr+offset+len, 0,
1448                                                 PAGE_SIZE-offset-len);
1449                         }
1450                         err = copy_from_user(kaddr+offset, compat_ptr(str),
1451                                                 bytes_to_copy);
1452                         if (err) {
1453                                 ret = -EFAULT;
1454                                 goto out;
1455                         }
1456
1457                         pos += bytes_to_copy;
1458                         str += bytes_to_copy;
1459                         len -= bytes_to_copy;
1460                 }
1461         }
1462         ret = 0;
1463 out:
1464         if (kmapped_page)
1465                 kunmap(kmapped_page);
1466         return ret;
1467 }
1468
1469 #ifdef CONFIG_MMU
1470
1471 #define free_arg_pages(bprm) do { } while (0)
1472
1473 #else
1474
1475 static inline void free_arg_pages(struct linux_binprm *bprm)
1476 {
1477         int i;
1478
1479         for (i = 0; i < MAX_ARG_PAGES; i++) {
1480                 if (bprm->page[i])
1481                         __free_page(bprm->page[i]);
1482                 bprm->page[i] = NULL;
1483         }
1484 }
1485
1486 #endif /* CONFIG_MMU */
1487
1488 /*
1489  * compat_do_execve() is mostly a copy of do_execve(), with the exception
1490  * that it processes 32 bit argv and envp pointers.
1491  */
1492 int compat_do_execve(char * filename,
1493         compat_uptr_t __user *argv,
1494         compat_uptr_t __user *envp,
1495         struct pt_regs * regs)
1496 {
1497         struct linux_binprm *bprm;
1498         struct file *file;
1499         int retval;
1500         int i;
1501
1502         retval = -ENOMEM;
1503         bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1504         if (!bprm)
1505                 goto out_ret;
1506
1507         file = open_exec(filename);
1508         retval = PTR_ERR(file);
1509         if (IS_ERR(file))
1510                 goto out_kfree;
1511
1512         sched_exec();
1513
1514         bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1515         bprm->file = file;
1516         bprm->filename = filename;
1517         bprm->interp = filename;
1518         bprm->mm = mm_alloc();
1519         retval = -ENOMEM;
1520         if (!bprm->mm)
1521                 goto out_file;
1522
1523         retval = init_new_context(current, bprm->mm);
1524         if (retval < 0)
1525                 goto out_mm;
1526
1527         bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1528         if ((retval = bprm->argc) < 0)
1529                 goto out_mm;
1530
1531         bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1532         if ((retval = bprm->envc) < 0)
1533                 goto out_mm;
1534
1535         retval = security_bprm_alloc(bprm);
1536         if (retval)
1537                 goto out;
1538
1539         retval = prepare_binprm(bprm);
1540         if (retval < 0)
1541                 goto out;
1542
1543         retval = copy_strings_kernel(1, &bprm->filename, bprm);
1544         if (retval < 0)
1545                 goto out;
1546
1547         bprm->exec = bprm->p;
1548         retval = compat_copy_strings(bprm->envc, envp, bprm);
1549         if (retval < 0)
1550                 goto out;
1551
1552         retval = compat_copy_strings(bprm->argc, argv, bprm);
1553         if (retval < 0)
1554                 goto out;
1555
1556         retval = search_binary_handler(bprm, regs);
1557         if (retval >= 0) {
1558                 free_arg_pages(bprm);
1559
1560                 /* execve success */
1561                 security_bprm_free(bprm);
1562                 acct_update_integrals(current);
1563                 kfree(bprm);
1564                 return retval;
1565         }
1566
1567 out:
1568         /* Something went wrong, return the inode and free the argument pages*/
1569         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1570                 struct page * page = bprm->page[i];
1571                 if (page)
1572                         __free_page(page);
1573         }
1574
1575         if (bprm->security)
1576                 security_bprm_free(bprm);
1577
1578 out_mm:
1579         if (bprm->mm)
1580                 mmdrop(bprm->mm);
1581
1582 out_file:
1583         if (bprm->file) {
1584                 allow_write_access(bprm->file);
1585                 fput(bprm->file);
1586         }
1587
1588 out_kfree:
1589         kfree(bprm);
1590
1591 out_ret:
1592         return retval;
1593 }
1594
1595 #define __COMPAT_NFDBITS       (8 * sizeof(compat_ulong_t))
1596
1597 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1598
1599 /*
1600  * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
1601  * 64-bit unsigned longs.
1602  */
1603 static
1604 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1605                         unsigned long *fdset)
1606 {
1607         nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1608         if (ufdset) {
1609                 unsigned long odd;
1610
1611                 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1612                         return -EFAULT;
1613
1614                 odd = nr & 1UL;
1615                 nr &= ~1UL;
1616                 while (nr) {
1617                         unsigned long h, l;
1618                         if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1619                                 return -EFAULT;
1620                         ufdset += 2;
1621                         *fdset++ = h << 32 | l;
1622                         nr -= 2;
1623                 }
1624                 if (odd && __get_user(*fdset, ufdset))
1625                         return -EFAULT;
1626         } else {
1627                 /* Tricky, must clear full unsigned long in the
1628                  * kernel fdset at the end, this makes sure that
1629                  * actually happens.
1630                  */
1631                 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1632         }
1633         return 0;
1634 }
1635
1636 static
1637 int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1638                       unsigned long *fdset)
1639 {
1640         unsigned long odd;
1641         nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1642
1643         if (!ufdset)
1644                 return 0;
1645
1646         odd = nr & 1UL;
1647         nr &= ~1UL;
1648         while (nr) {
1649                 unsigned long h, l;
1650                 l = *fdset++;
1651                 h = l >> 32;
1652                 if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1653                         return -EFAULT;
1654                 ufdset += 2;
1655                 nr -= 2;
1656         }
1657         if (odd && __put_user(*fdset, ufdset))
1658                 return -EFAULT;
1659         return 0;
1660 }
1661
1662
1663 /*
1664  * This is a virtual copy of sys_select from fs/select.c and probably
1665  * should be compared to it from time to time
1666  */
1667
1668 /*
1669  * We can actually return ERESTARTSYS instead of EINTR, but I'd
1670  * like to be certain this leads to no problems. So I return
1671  * EINTR just for safety.
1672  *
1673  * Update: ERESTARTSYS breaks at least the xview clock binary, so
1674  * I'm trying ERESTARTNOHAND which restart only when you want to.
1675  */
1676 #define MAX_SELECT_SECONDS \
1677         ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1678
1679 int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1680         compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout)
1681 {
1682         fd_set_bits fds;
1683         char *bits;
1684         int size, max_fds, ret = -EINVAL;
1685         struct fdtable *fdt;
1686
1687         if (n < 0)
1688                 goto out_nofds;
1689
1690         /* max_fds can increase, so grab it once to avoid race */
1691         rcu_read_lock();
1692         fdt = files_fdtable(current->files);
1693         max_fds = fdt->max_fds;
1694         rcu_read_unlock();
1695         if (n > max_fds)
1696                 n = max_fds;
1697
1698         /*
1699          * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1700          * since we used fdset we need to allocate memory in units of
1701          * long-words.
1702          */
1703         ret = -ENOMEM;
1704         size = FDS_BYTES(n);
1705         bits = kmalloc(6 * size, GFP_KERNEL);
1706         if (!bits)
1707                 goto out_nofds;
1708         fds.in      = (unsigned long *)  bits;
1709         fds.out     = (unsigned long *) (bits +   size);
1710         fds.ex      = (unsigned long *) (bits + 2*size);
1711         fds.res_in  = (unsigned long *) (bits + 3*size);
1712         fds.res_out = (unsigned long *) (bits + 4*size);
1713         fds.res_ex  = (unsigned long *) (bits + 5*size);
1714
1715         if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1716             (ret = compat_get_fd_set(n, outp, fds.out)) ||
1717             (ret = compat_get_fd_set(n, exp, fds.ex)))
1718                 goto out;
1719         zero_fd_set(n, fds.res_in);
1720         zero_fd_set(n, fds.res_out);
1721         zero_fd_set(n, fds.res_ex);
1722
1723         ret = do_select(n, &fds, timeout);
1724
1725         if (ret < 0)
1726                 goto out;
1727         if (!ret) {
1728                 ret = -ERESTARTNOHAND;
1729                 if (signal_pending(current))
1730                         goto out;
1731                 ret = 0;
1732         }
1733
1734         if (compat_set_fd_set(n, inp, fds.res_in) ||
1735             compat_set_fd_set(n, outp, fds.res_out) ||
1736             compat_set_fd_set(n, exp, fds.res_ex))
1737                 ret = -EFAULT;
1738 out:
1739         kfree(bits);
1740 out_nofds:
1741         return ret;
1742 }
1743
1744 asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1745         compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1746         struct compat_timeval __user *tvp)
1747 {
1748         s64 timeout = -1;
1749         struct compat_timeval tv;
1750         int ret;
1751
1752         if (tvp) {
1753                 if (copy_from_user(&tv, tvp, sizeof(tv)))
1754                         return -EFAULT;
1755
1756                 if (tv.tv_sec < 0 || tv.tv_usec < 0)
1757                         return -EINVAL;
1758
1759                 /* Cast to u64 to make GCC stop complaining */
1760                 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
1761                         timeout = -1;   /* infinite */
1762                 else {
1763                         timeout = ROUND_UP(tv.tv_usec, 1000000/HZ);
1764                         timeout += tv.tv_sec * HZ;
1765                 }
1766         }
1767
1768         ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1769
1770         if (tvp) {
1771                 struct compat_timeval rtv;
1772
1773                 if (current->personality & STICKY_TIMEOUTS)
1774                         goto sticky;
1775                 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
1776                 rtv.tv_sec = timeout;
1777                 if (compat_timeval_compare(&rtv, &tv) >= 0)
1778                         rtv = tv;
1779                 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
1780 sticky:
1781                         /*
1782                          * If an application puts its timeval in read-only
1783                          * memory, we don't want the Linux-specific update to
1784                          * the timeval to cause a fault after the select has
1785                          * completed successfully. However, because we're not
1786                          * updating the timeval, we can't restart the system
1787                          * call.
1788                          */
1789                         if (ret == -ERESTARTNOHAND)
1790                                 ret = -EINTR;
1791                 }
1792         }
1793
1794         return ret;
1795 }
1796
1797 #ifdef TIF_RESTORE_SIGMASK
1798 asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
1799         compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1800         struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1801         compat_size_t sigsetsize)
1802 {
1803         compat_sigset_t ss32;
1804         sigset_t ksigmask, sigsaved;
1805         s64 timeout = MAX_SCHEDULE_TIMEOUT;
1806         struct compat_timespec ts;
1807         int ret;
1808
1809         if (tsp) {
1810                 if (copy_from_user(&ts, tsp, sizeof(ts)))
1811                         return -EFAULT;
1812
1813                 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
1814                         return -EINVAL;
1815         }
1816
1817         if (sigmask) {
1818                 if (sigsetsize != sizeof(compat_sigset_t))
1819                         return -EINVAL;
1820                 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1821                         return -EFAULT;
1822                 sigset_from_compat(&ksigmask, &ss32);
1823
1824                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1825                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1826         }
1827
1828         do {
1829                 if (tsp) {
1830                         if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) {
1831                                 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
1832                                 timeout += ts.tv_sec * (unsigned long)HZ;
1833                                 ts.tv_sec = 0;
1834                                 ts.tv_nsec = 0;
1835                         } else {
1836                                 ts.tv_sec -= MAX_SELECT_SECONDS;
1837                                 timeout = MAX_SELECT_SECONDS * HZ;
1838                         }
1839                 }
1840
1841                 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1842
1843         } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec));
1844
1845         if (tsp) {
1846                 struct compat_timespec rts;
1847
1848                 if (current->personality & STICKY_TIMEOUTS)
1849                         goto sticky;
1850
1851                 rts.tv_sec = timeout / HZ;
1852                 rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
1853                 if (rts.tv_nsec >= NSEC_PER_SEC) {
1854                         rts.tv_sec++;
1855                         rts.tv_nsec -= NSEC_PER_SEC;
1856                 }
1857                 if (compat_timespec_compare(&rts, &ts) >= 0)
1858                         rts = ts;
1859                 if (copy_to_user(tsp, &rts, sizeof(rts))) {
1860 sticky:
1861                         /*
1862                          * If an application puts its timeval in read-only
1863                          * memory, we don't want the Linux-specific update to
1864                          * the timeval to cause a fault after the select has
1865                          * completed successfully. However, because we're not
1866                          * updating the timeval, we can't restart the system
1867                          * call.
1868                          */
1869                         if (ret == -ERESTARTNOHAND)
1870                                 ret = -EINTR;
1871                 }
1872         }
1873
1874         if (ret == -ERESTARTNOHAND) {
1875                 /*
1876                  * Don't restore the signal mask yet. Let do_signal() deliver
1877                  * the signal on the way back to userspace, before the signal
1878                  * mask is restored.
1879                  */
1880                 if (sigmask) {
1881                         memcpy(&current->saved_sigmask, &sigsaved,
1882                                         sizeof(sigsaved));
1883                         set_thread_flag(TIF_RESTORE_SIGMASK);
1884                 }
1885         } else if (sigmask)
1886                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1887
1888         return ret;
1889 }
1890
1891 asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1892         compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1893         struct compat_timespec __user *tsp, void __user *sig)
1894 {
1895         compat_size_t sigsetsize = 0;
1896         compat_uptr_t up = 0;
1897
1898         if (sig) {
1899                 if (!access_ok(VERIFY_READ, sig,
1900                                 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1901                         __get_user(up, (compat_uptr_t __user *)sig) ||
1902                         __get_user(sigsetsize,
1903                                 (compat_size_t __user *)(sig+sizeof(up))))
1904                         return -EFAULT;
1905         }
1906         return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up),
1907                                         sigsetsize);
1908 }
1909
1910 asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1911         unsigned int nfds, struct compat_timespec __user *tsp,
1912         const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1913 {
1914         compat_sigset_t ss32;
1915         sigset_t ksigmask, sigsaved;
1916         struct compat_timespec ts;
1917         s64 timeout = -1;
1918         int ret;
1919
1920         if (tsp) {
1921                 if (copy_from_user(&ts, tsp, sizeof(ts)))
1922                         return -EFAULT;
1923
1924                 /* We assume that ts.tv_sec is always lower than
1925                    the number of seconds that can be expressed in
1926                    an s64. Otherwise the compiler bitches at us */
1927                 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
1928                 timeout += ts.tv_sec * HZ;
1929         }
1930
1931         if (sigmask) {
1932                 if (sigsetsize != sizeof(compat_sigset_t))
1933                         return -EINVAL;
1934                 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1935                         return -EFAULT;
1936                 sigset_from_compat(&ksigmask, &ss32);
1937
1938                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1939                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1940         }
1941
1942         ret = do_sys_poll(ufds, nfds, &timeout);
1943
1944         /* We can restart this syscall, usually */
1945         if (ret == -EINTR) {
1946                 /*
1947                  * Don't restore the signal mask yet. Let do_signal() deliver
1948                  * the signal on the way back to userspace, before the signal
1949                  * mask is restored.
1950                  */
1951                 if (sigmask) {
1952                         memcpy(&current->saved_sigmask, &sigsaved,
1953                                 sizeof(sigsaved));
1954                         set_thread_flag(TIF_RESTORE_SIGMASK);
1955                 }
1956                 ret = -ERESTARTNOHAND;
1957         } else if (sigmask)
1958                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1959
1960         if (tsp && timeout >= 0) {
1961                 struct compat_timespec rts;
1962
1963                 if (current->personality & STICKY_TIMEOUTS)
1964                         goto sticky;
1965                 /* Yes, we know it's actually an s64, but it's also positive. */
1966                 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
1967                                         1000;
1968                 rts.tv_sec = timeout;
1969                 if (compat_timespec_compare(&rts, &ts) >= 0)
1970                         rts = ts;
1971                 if (copy_to_user(tsp, &rts, sizeof(rts))) {
1972 sticky:
1973                         /*
1974                          * If an application puts its timeval in read-only
1975                          * memory, we don't want the Linux-specific update to
1976                          * the timeval to cause a fault after the select has
1977                          * completed successfully. However, because we're not
1978                          * updating the timeval, we can't restart the system
1979                          * call.
1980                          */
1981                         if (ret == -ERESTARTNOHAND && timeout >= 0)
1982                                 ret = -EINTR;
1983                 }
1984         }
1985
1986         return ret;
1987 }
1988 #endif /* TIF_RESTORE_SIGMASK */
1989
1990 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1991 /* Stuff for NFS server syscalls... */
1992 struct compat_nfsctl_svc {
1993         u16                     svc32_port;
1994         s32                     svc32_nthreads;
1995 };
1996
1997 struct compat_nfsctl_client {
1998         s8                      cl32_ident[NFSCLNT_IDMAX+1];
1999         s32                     cl32_naddr;
2000         struct in_addr          cl32_addrlist[NFSCLNT_ADDRMAX];
2001         s32                     cl32_fhkeytype;
2002         s32                     cl32_fhkeylen;
2003         u8                      cl32_fhkey[NFSCLNT_KEYMAX];
2004 };
2005
2006 struct compat_nfsctl_export {
2007         char            ex32_client[NFSCLNT_IDMAX+1];
2008         char            ex32_path[NFS_MAXPATHLEN+1];
2009         compat_dev_t    ex32_dev;
2010         compat_ino_t    ex32_ino;
2011         compat_int_t    ex32_flags;
2012         __compat_uid_t  ex32_anon_uid;
2013         __compat_gid_t  ex32_anon_gid;
2014 };
2015
2016 struct compat_nfsctl_fdparm {
2017         struct sockaddr         gd32_addr;
2018         s8                      gd32_path[NFS_MAXPATHLEN+1];
2019         compat_int_t            gd32_version;
2020 };
2021
2022 struct compat_nfsctl_fsparm {
2023         struct sockaddr         gd32_addr;
2024         s8                      gd32_path[NFS_MAXPATHLEN+1];
2025         compat_int_t            gd32_maxlen;
2026 };
2027
2028 struct compat_nfsctl_arg {
2029         compat_int_t            ca32_version;   /* safeguard */
2030         union {
2031                 struct compat_nfsctl_svc        u32_svc;
2032                 struct compat_nfsctl_client     u32_client;
2033                 struct compat_nfsctl_export     u32_export;
2034                 struct compat_nfsctl_fdparm     u32_getfd;
2035                 struct compat_nfsctl_fsparm     u32_getfs;
2036         } u;
2037 #define ca32_svc        u.u32_svc
2038 #define ca32_client     u.u32_client
2039 #define ca32_export     u.u32_export
2040 #define ca32_getfd      u.u32_getfd
2041 #define ca32_getfs      u.u32_getfs
2042 };
2043
2044 union compat_nfsctl_res {
2045         __u8                    cr32_getfh[NFS_FHSIZE];
2046         struct knfsd_fh         cr32_getfs;
2047 };
2048
2049 static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
2050                                 struct compat_nfsctl_arg __user *arg)
2051 {
2052         if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) ||
2053                 get_user(karg->ca_version, &arg->ca32_version) ||
2054                 __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) ||
2055                 __get_user(karg->ca_svc.svc_nthreads,
2056                                 &arg->ca32_svc.svc32_nthreads))
2057                 return -EFAULT;
2058         return 0;
2059 }
2060
2061 static int compat_nfs_clnt_trans(struct nfsctl_arg *karg,
2062                                 struct compat_nfsctl_arg __user *arg)
2063 {
2064         if (!access_ok(VERIFY_READ, &arg->ca32_client,
2065                         sizeof(arg->ca32_client)) ||
2066                 get_user(karg->ca_version, &arg->ca32_version) ||
2067                 __copy_from_user(&karg->ca_client.cl_ident[0],
2068                                 &arg->ca32_client.cl32_ident[0],
2069                                 NFSCLNT_IDMAX) ||
2070                 __get_user(karg->ca_client.cl_naddr,
2071                                 &arg->ca32_client.cl32_naddr) ||
2072                 __copy_from_user(&karg->ca_client.cl_addrlist[0],
2073                                 &arg->ca32_client.cl32_addrlist[0],
2074                                 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) ||
2075                 __get_user(karg->ca_client.cl_fhkeytype,
2076                                 &arg->ca32_client.cl32_fhkeytype) ||
2077                 __get_user(karg->ca_client.cl_fhkeylen,
2078                                 &arg->ca32_client.cl32_fhkeylen) ||
2079                 __copy_from_user(&karg->ca_client.cl_fhkey[0],
2080                                 &arg->ca32_client.cl32_fhkey[0],
2081                                 NFSCLNT_KEYMAX))
2082                 return -EFAULT;
2083
2084         return 0;
2085 }
2086
2087 static int compat_nfs_exp_trans(struct nfsctl_arg *karg,
2088                                 struct compat_nfsctl_arg __user *arg)
2089 {
2090         if (!access_ok(VERIFY_READ, &arg->ca32_export,
2091                                 sizeof(arg->ca32_export)) ||
2092                 get_user(karg->ca_version, &arg->ca32_version) ||
2093                 __copy_from_user(&karg->ca_export.ex_client[0],
2094                                 &arg->ca32_export.ex32_client[0],
2095                                 NFSCLNT_IDMAX) ||
2096                 __copy_from_user(&karg->ca_export.ex_path[0],
2097                                 &arg->ca32_export.ex32_path[0],
2098                                 NFS_MAXPATHLEN) ||
2099                 __get_user(karg->ca_export.ex_dev,
2100                                 &arg->ca32_export.ex32_dev) ||
2101                 __get_user(karg->ca_export.ex_ino,
2102                                 &arg->ca32_export.ex32_ino) ||
2103                 __get_user(karg->ca_export.ex_flags,
2104                                 &arg->ca32_export.ex32_flags) ||
2105                 __get_user(karg->ca_export.ex_anon_uid,
2106                                 &arg->ca32_export.ex32_anon_uid) ||
2107                 __get_user(karg->ca_export.ex_anon_gid,
2108                                 &arg->ca32_export.ex32_anon_gid))
2109                 return -EFAULT;
2110         SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
2111         SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
2112
2113         return 0;
2114 }
2115
2116 static int compat_nfs_getfd_trans(struct nfsctl_arg *karg,
2117                                 struct compat_nfsctl_arg __user *arg)
2118 {
2119         if (!access_ok(VERIFY_READ, &arg->ca32_getfd,
2120                         sizeof(arg->ca32_getfd)) ||
2121                 get_user(karg->ca_version, &arg->ca32_version) ||
2122                 __copy_from_user(&karg->ca_getfd.gd_addr,
2123                                 &arg->ca32_getfd.gd32_addr,
2124                                 (sizeof(struct sockaddr))) ||
2125                 __copy_from_user(&karg->ca_getfd.gd_path,
2126                                 &arg->ca32_getfd.gd32_path,
2127                                 (NFS_MAXPATHLEN+1)) ||
2128                 __get_user(karg->ca_getfd.gd_version,
2129                                 &arg->ca32_getfd.gd32_version))
2130                 return -EFAULT;
2131
2132         return 0;
2133 }
2134
2135 static int compat_nfs_getfs_trans(struct nfsctl_arg *karg,
2136                                 struct compat_nfsctl_arg __user *arg)
2137 {
2138         if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) ||
2139                 get_user(karg->ca_version, &arg->ca32_version) ||
2140                 __copy_from_user(&karg->ca_getfs.gd_addr,
2141                                 &arg->ca32_getfs.gd32_addr,
2142                                 (sizeof(struct sockaddr))) ||
2143                 __copy_from_user(&karg->ca_getfs.gd_path,
2144                                 &arg->ca32_getfs.gd32_path,
2145                                 (NFS_MAXPATHLEN+1)) ||
2146                 __get_user(karg->ca_getfs.gd_maxlen,
2147                                 &arg->ca32_getfs.gd32_maxlen))
2148                 return -EFAULT;
2149
2150         return 0;
2151 }
2152
2153 /* This really doesn't need translations, we are only passing
2154  * back a union which contains opaque nfs file handle data.
2155  */
2156 static int compat_nfs_getfh_res_trans(union nfsctl_res *kres,
2157                                 union compat_nfsctl_res __user *res)
2158 {
2159         int err;
2160
2161         err = copy_to_user(res, kres, sizeof(*res));
2162
2163         return (err) ? -EFAULT : 0;
2164 }
2165
2166 asmlinkage long compat_sys_nfsservctl(int cmd,
2167                                 struct compat_nfsctl_arg __user *arg,
2168                                 union compat_nfsctl_res __user *res)
2169 {
2170         struct nfsctl_arg *karg;
2171         union nfsctl_res *kres;
2172         mm_segment_t oldfs;
2173         int err;
2174
2175         karg = kmalloc(sizeof(*karg), GFP_USER);
2176         kres = kmalloc(sizeof(*kres), GFP_USER);
2177         if(!karg || !kres) {
2178                 err = -ENOMEM;
2179                 goto done;
2180         }
2181
2182         switch(cmd) {
2183         case NFSCTL_SVC:
2184                 err = compat_nfs_svc_trans(karg, arg);
2185                 break;
2186
2187         case NFSCTL_ADDCLIENT:
2188                 err = compat_nfs_clnt_trans(karg, arg);
2189                 break;
2190
2191         case NFSCTL_DELCLIENT:
2192                 err = compat_nfs_clnt_trans(karg, arg);
2193                 break;
2194
2195         case NFSCTL_EXPORT:
2196         case NFSCTL_UNEXPORT:
2197                 err = compat_nfs_exp_trans(karg, arg);
2198                 break;
2199
2200         case NFSCTL_GETFD:
2201                 err = compat_nfs_getfd_trans(karg, arg);
2202                 break;
2203
2204         case NFSCTL_GETFS:
2205                 err = compat_nfs_getfs_trans(karg, arg);
2206                 break;
2207
2208         default:
2209                 err = -EINVAL;
2210                 break;
2211         }
2212
2213         if (err)
2214                 goto done;
2215
2216         oldfs = get_fs();
2217         set_fs(KERNEL_DS);
2218         /* The __user pointer casts are valid because of the set_fs() */
2219         err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
2220         set_fs(oldfs);
2221
2222         if (err)
2223                 goto done;
2224
2225         if((cmd == NFSCTL_GETFD) ||
2226            (cmd == NFSCTL_GETFS))
2227                 err = compat_nfs_getfh_res_trans(kres, res);
2228
2229 done:
2230         kfree(karg);
2231         kfree(kres);
2232         return err;
2233 }
2234 #else /* !NFSD */
2235 long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
2236 {
2237         return sys_ni_syscall();
2238 }
2239 #endif
2240
2241 #ifdef CONFIG_EPOLL
2242
2243 #ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2244 asmlinkage long compat_sys_epoll_ctl(int epfd, int op, int fd,
2245                         struct compat_epoll_event __user *event)
2246 {
2247         long err = 0;
2248         struct compat_epoll_event user;
2249         struct epoll_event __user *kernel = NULL;
2250
2251         if (event) {
2252                 if (copy_from_user(&user, event, sizeof(user)))
2253                         return -EFAULT;
2254                 kernel = compat_alloc_user_space(sizeof(struct epoll_event));
2255                 err |= __put_user(user.events, &kernel->events);
2256                 err |= __put_user(user.data, &kernel->data);
2257         }
2258
2259         return err ? err : sys_epoll_ctl(epfd, op, fd, kernel);
2260 }
2261
2262
2263 asmlinkage long compat_sys_epoll_wait(int epfd,
2264                         struct compat_epoll_event __user *events,
2265                         int maxevents, int timeout)
2266 {
2267         long i, ret, err = 0;
2268         struct epoll_event __user *kbuf;
2269         struct epoll_event ev;
2270
2271         if ((maxevents <= 0) ||
2272                         (maxevents > (INT_MAX / sizeof(struct epoll_event))))
2273                 return -EINVAL;
2274         kbuf = compat_alloc_user_space(sizeof(struct epoll_event) * maxevents);
2275         ret = sys_epoll_wait(epfd, kbuf, maxevents, timeout);
2276         for (i = 0; i < ret; i++) {
2277                 err |= __get_user(ev.events, &kbuf[i].events);
2278                 err |= __get_user(ev.data, &kbuf[i].data);
2279                 err |= __put_user(ev.events, &events->events);
2280                 err |= __put_user_unaligned(ev.data, &events->data);
2281                 events++;
2282         }
2283
2284         return err ? -EFAULT: ret;
2285 }
2286 #endif  /* CONFIG_HAS_COMPAT_EPOLL_EVENT */
2287
2288 #ifdef TIF_RESTORE_SIGMASK
2289 asmlinkage long compat_sys_epoll_pwait(int epfd,
2290                         struct compat_epoll_event __user *events,
2291                         int maxevents, int timeout,
2292                         const compat_sigset_t __user *sigmask,
2293                         compat_size_t sigsetsize)
2294 {
2295         long err;
2296         compat_sigset_t csigmask;
2297         sigset_t ksigmask, sigsaved;
2298
2299         /*
2300          * If the caller wants a certain signal mask to be set during the wait,
2301          * we apply it here.
2302          */
2303         if (sigmask) {
2304                 if (sigsetsize != sizeof(compat_sigset_t))
2305                         return -EINVAL;
2306                 if (copy_from_user(&csigmask, sigmask, sizeof(csigmask)))
2307                         return -EFAULT;
2308                 sigset_from_compat(&ksigmask, &csigmask);
2309                 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2310                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
2311         }
2312
2313 #ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2314         err = compat_sys_epoll_wait(epfd, events, maxevents, timeout);
2315 #else
2316         err = sys_epoll_wait(epfd, events, maxevents, timeout);
2317 #endif
2318
2319         /*
2320          * If we changed the signal mask, we need to restore the original one.
2321          * In case we've got a signal while waiting, we do not restore the
2322          * signal mask yet, and we allow do_signal() to deliver the signal on
2323          * the way back to userspace, before the signal mask is restored.
2324          */
2325         if (sigmask) {
2326                 if (err == -EINTR) {
2327                         memcpy(&current->saved_sigmask, &sigsaved,
2328                                sizeof(sigsaved));
2329                         set_thread_flag(TIF_RESTORE_SIGMASK);
2330                 } else
2331                         sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2332         }
2333
2334         return err;
2335 }
2336 #endif /* TIF_RESTORE_SIGMASK */
2337
2338 #endif /* CONFIG_EPOLL */