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