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