This commit was manufactured by cvs2svn to create tag
[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/init.h>
29 #include <linux/sockios.h>      /* for SIOCDEVPRIVATE */
30 #include <linux/smb.h>
31 #include <linux/smb_mount.h>
32 #include <linux/ncp_mount.h>
33 #include <linux/smp_lock.h>
34 #include <linux/syscalls.h>
35 #include <linux/ctype.h>
36 #include <linux/module.h>
37 #include <linux/dirent.h>
38 #include <linux/dnotify.h>
39 #include <linux/highuid.h>
40 #include <linux/sunrpc/svc.h>
41 #include <linux/nfsd/nfsd.h>
42 #include <linux/nfsd/syscall.h>
43 #include <linux/personality.h>
44 #include <linux/rwsem.h>
45
46 #include <net/sock.h>           /* siocdevprivate_ioctl */
47
48 #include <asm/uaccess.h>
49 #include <asm/mmu_context.h>
50
51 /*
52  * Not all architectures have sys_utime, so implement this in terms
53  * of sys_utimes.
54  */
55 asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
56 {
57         struct timeval tv[2];
58
59         if (t) {
60                 if (get_user(tv[0].tv_sec, &t->actime) ||
61                     get_user(tv[1].tv_sec, &t->modtime))
62                         return -EFAULT;
63                 tv[0].tv_usec = 0;
64                 tv[1].tv_usec = 0;
65         }
66         return do_utimes(filename, t ? tv : NULL);
67 }
68
69 asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
70 {
71         struct timeval tv[2];
72
73         if (t) { 
74                 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
75                     get_user(tv[0].tv_usec, &t[0].tv_usec) ||
76                     get_user(tv[1].tv_sec, &t[1].tv_sec) ||
77                     get_user(tv[1].tv_usec, &t[1].tv_usec))
78                         return -EFAULT; 
79         } 
80         return do_utimes(filename, t ? tv : NULL);
81 }
82
83 asmlinkage long compat_sys_newstat(char __user * filename,
84                 struct compat_stat __user *statbuf)
85 {
86         struct kstat stat;
87         int error = vfs_stat(filename, &stat);
88
89         if (!error)
90                 error = cp_compat_stat(&stat, statbuf);
91         return error;
92 }
93
94 asmlinkage long compat_sys_newlstat(char __user * filename,
95                 struct compat_stat __user *statbuf)
96 {
97         struct kstat stat;
98         int error = vfs_lstat(filename, &stat);
99
100         if (!error)
101                 error = cp_compat_stat(&stat, statbuf);
102         return error;
103 }
104
105 asmlinkage long compat_sys_newfstat(unsigned int fd,
106                 struct compat_stat __user * statbuf)
107 {
108         struct kstat stat;
109         int error = vfs_fstat(fd, &stat);
110
111         if (!error)
112                 error = cp_compat_stat(&stat, statbuf);
113         return error;
114 }
115
116 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
117 {
118         
119         if (sizeof ubuf->f_blocks == 4) {
120                 if ((kbuf->f_blocks | kbuf->f_bfree |
121                      kbuf->f_bavail | kbuf->f_files | kbuf->f_ffree) &
122                     0xffffffff00000000ULL)
123                         return -EOVERFLOW;
124         }
125         if (verify_area(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
126             __put_user(kbuf->f_type, &ubuf->f_type) ||
127             __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
128             __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
129             __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
130             __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
131             __put_user(kbuf->f_files, &ubuf->f_files) ||
132             __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
133             __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
134             __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
135             __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
136             __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
137             __put_user(0, &ubuf->f_spare[0]) || 
138             __put_user(0, &ubuf->f_spare[1]) || 
139             __put_user(0, &ubuf->f_spare[2]) || 
140             __put_user(0, &ubuf->f_spare[3]) || 
141             __put_user(0, &ubuf->f_spare[4]))
142                 return -EFAULT;
143         return 0;
144 }
145
146 /*
147  * The following statfs calls are copies of code from fs/open.c and
148  * should be checked against those from time to time
149  */
150 asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
151 {
152         struct nameidata nd;
153         int error;
154
155         error = user_path_walk(path, &nd);
156         if (!error) {
157                 struct kstatfs tmp;
158                 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
159                 if (!error && put_compat_statfs(buf, &tmp))
160                         error = -EFAULT;
161                 path_release(&nd);
162         }
163         return error;
164 }
165
166 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
167 {
168         struct file * file;
169         struct kstatfs tmp;
170         int error;
171
172         error = -EBADF;
173         file = fget(fd);
174         if (!file)
175                 goto out;
176         error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
177         if (!error && put_compat_statfs(buf, &tmp))
178                 error = -EFAULT;
179         fput(file);
180 out:
181         return error;
182 }
183
184 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
185 {
186         if (sizeof ubuf->f_blocks == 4) {
187                 if ((kbuf->f_blocks | kbuf->f_bfree |
188                      kbuf->f_bavail | kbuf->f_files | kbuf->f_ffree) &
189                     0xffffffff00000000ULL)
190                         return -EOVERFLOW;
191         }
192         if (verify_area(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
193             __put_user(kbuf->f_type, &ubuf->f_type) ||
194             __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
195             __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
196             __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
197             __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
198             __put_user(kbuf->f_files, &ubuf->f_files) ||
199             __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
200             __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
201             __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
202             __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
203             __put_user(kbuf->f_frsize, &ubuf->f_frsize))
204                 return -EFAULT;
205         return 0;
206 }
207
208 asmlinkage long compat_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
209 {
210         struct nameidata nd;
211         int error;
212
213         if (sz != sizeof(*buf))
214                 return -EINVAL;
215
216         error = user_path_walk(path, &nd);
217         if (!error) {
218                 struct kstatfs tmp;
219                 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
220                 if (!error && put_compat_statfs64(buf, &tmp))
221                         error = -EFAULT;
222                 path_release(&nd);
223         }
224         return error;
225 }
226
227 asmlinkage long compat_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
228 {
229         struct file * file;
230         struct kstatfs tmp;
231         int error;
232
233         if (sz != sizeof(*buf))
234                 return -EINVAL;
235
236         error = -EBADF;
237         file = fget(fd);
238         if (!file)
239                 goto out;
240         error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
241         if (!error && put_compat_statfs64(buf, &tmp))
242                 error = -EFAULT;
243         fput(file);
244 out:
245         return error;
246 }
247
248 /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
249
250 #define IOCTL_HASHSIZE 256
251 static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
252 static DECLARE_RWSEM(ioctl32_sem);
253
254 extern struct ioctl_trans ioctl_start[];
255 extern int ioctl_table_size;
256
257 static inline unsigned long ioctl32_hash(unsigned long cmd)
258 {
259         return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
260 }
261
262 static void ioctl32_insert_translation(struct ioctl_trans *trans)
263 {
264         unsigned long hash;
265         struct ioctl_trans *t;
266
267         hash = ioctl32_hash (trans->cmd);
268         if (!ioctl32_hash_table[hash])
269                 ioctl32_hash_table[hash] = trans;
270         else {
271                 t = ioctl32_hash_table[hash];
272                 while (t->next)
273                         t = t->next;
274                 trans->next = NULL;
275                 t->next = trans;
276         }
277 }
278
279 static int __init init_sys32_ioctl(void)
280 {
281         int i;
282
283         for (i = 0; i < ioctl_table_size; i++) {
284                 if (ioctl_start[i].next != 0) { 
285                         printk("ioctl translation %d bad\n",i); 
286                         return -1;
287                 }
288
289                 ioctl32_insert_translation(&ioctl_start[i]);
290         }
291         return 0;
292 }
293
294 __initcall(init_sys32_ioctl);
295
296 int register_ioctl32_conversion(unsigned int cmd,
297                                 ioctl_trans_handler_t handler)
298 {
299         struct ioctl_trans *t;
300         struct ioctl_trans *new_t;
301         unsigned long hash = ioctl32_hash(cmd);
302
303         new_t = kmalloc(sizeof(*new_t), GFP_KERNEL);
304         if (!new_t)
305                 return -ENOMEM;
306
307         down_write(&ioctl32_sem);
308         for (t = ioctl32_hash_table[hash]; t; t = t->next) {
309                 if (t->cmd == cmd) {
310                         printk(KERN_ERR "Trying to register duplicated ioctl32 "
311                                         "handler %x\n", cmd);
312                         up_write(&ioctl32_sem);
313                         kfree(new_t);
314                         return -EINVAL; 
315                 }
316         }
317         new_t->next = NULL;
318         new_t->cmd = cmd;
319         new_t->handler = handler;
320         ioctl32_insert_translation(new_t);
321
322         up_write(&ioctl32_sem);
323         return 0;
324 }
325 EXPORT_SYMBOL(register_ioctl32_conversion);
326
327 static inline int builtin_ioctl(struct ioctl_trans *t)
328
329         return t >= ioctl_start && t < (ioctl_start + ioctl_table_size);
330
331
332 /* Problem: 
333    This function cannot unregister duplicate ioctls, because they are not
334    unique.
335    When they happen we need to extend the prototype to pass the handler too. */
336
337 int unregister_ioctl32_conversion(unsigned int cmd)
338 {
339         unsigned long hash = ioctl32_hash(cmd);
340         struct ioctl_trans *t, *t1;
341
342         down_write(&ioctl32_sem);
343
344         t = ioctl32_hash_table[hash];
345         if (!t) { 
346                 up_write(&ioctl32_sem);
347                 return -EINVAL;
348         } 
349
350         if (t->cmd == cmd) { 
351                 if (builtin_ioctl(t)) {
352                         printk("%p tried to unregister builtin ioctl %x\n",
353                                __builtin_return_address(0), cmd);
354                 } else { 
355                         ioctl32_hash_table[hash] = t->next;
356                         up_write(&ioctl32_sem);
357                         kfree(t);
358                         return 0;
359                 }
360         } 
361         while (t->next) {
362                 t1 = t->next;
363                 if (t1->cmd == cmd) { 
364                         if (builtin_ioctl(t1)) {
365                                 printk("%p tried to unregister builtin "
366                                         "ioctl %x\n",
367                                         __builtin_return_address(0), cmd);
368                                 goto out;
369                         } else { 
370                                 t->next = t1->next;
371                                 up_write(&ioctl32_sem);
372                                 kfree(t1);
373                                 return 0;
374                         }
375                 }
376                 t = t1;
377         }
378         printk(KERN_ERR "Trying to free unknown 32bit ioctl handler %x\n",
379                                 cmd);
380 out:
381         up_write(&ioctl32_sem);
382         return -EINVAL;
383 }
384 EXPORT_SYMBOL(unregister_ioctl32_conversion); 
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
393         filp = fget(fd);
394         if(!filp)
395                 goto out2;
396
397         if (!filp->f_op || !filp->f_op->ioctl) {
398                 error = sys_ioctl (fd, cmd, arg);
399                 goto out;
400         }
401
402         down_read(&ioctl32_sem);
403
404         t = ioctl32_hash_table[ioctl32_hash (cmd)];
405
406         while (t && t->cmd != cmd)
407                 t = t->next;
408         if (t) {
409                 if (t->handler) { 
410                         lock_kernel();
411                         error = t->handler(fd, cmd, arg, filp);
412                         unlock_kernel();
413                         up_read(&ioctl32_sem);
414                 } else {
415                         up_read(&ioctl32_sem);
416                         error = sys_ioctl(fd, cmd, arg);
417                 }
418         } else {
419                 up_read(&ioctl32_sem);
420                 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
421                         error = siocdevprivate_ioctl(fd, cmd, arg);
422                 } else {
423                         static int count;
424                         if (++count <= 50) {
425                                 char buf[10];
426                                 char *fn = "?";
427                                 char *path;
428
429                                 path = (char *)__get_free_page(GFP_KERNEL);
430
431                                 /* find the name of the device. */
432                                 if (path) {
433                                         fn = d_path(filp->f_dentry,
434                                                 filp->f_vfsmnt, path,
435                                                 PAGE_SIZE);
436                                         if (IS_ERR(fn))
437                                                 fn = "?";
438                                 }
439
440                                 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
441                                 if (!isprint(buf[1]))
442                                     sprintf(buf, "%02x", buf[1]);
443                                 printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
444                                         "cmd(%08x){%s} arg(%08x) on %s\n",
445                                         current->comm, current->pid,
446                                         (int)fd, (unsigned int)cmd, buf,
447                                         (unsigned int)arg, fn);
448                                 if (path)
449                                         free_page((unsigned long)path);
450                         }
451                         error = -EINVAL;
452                 }
453         }
454 out:
455         fput(filp);
456 out2:
457         return error;
458 }
459
460 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
461 {
462         if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
463             __get_user(kfl->l_type, &ufl->l_type) ||
464             __get_user(kfl->l_whence, &ufl->l_whence) ||
465             __get_user(kfl->l_start, &ufl->l_start) ||
466             __get_user(kfl->l_len, &ufl->l_len) ||
467             __get_user(kfl->l_pid, &ufl->l_pid))
468                 return -EFAULT;
469         return 0;
470 }
471
472 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
473 {
474         if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
475             __put_user(kfl->l_type, &ufl->l_type) ||
476             __put_user(kfl->l_whence, &ufl->l_whence) ||
477             __put_user(kfl->l_start, &ufl->l_start) ||
478             __put_user(kfl->l_len, &ufl->l_len) ||
479             __put_user(kfl->l_pid, &ufl->l_pid))
480                 return -EFAULT;
481         return 0;
482 }
483
484 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
485 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
486 {
487         if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
488             __get_user(kfl->l_type, &ufl->l_type) ||
489             __get_user(kfl->l_whence, &ufl->l_whence) ||
490             __get_user(kfl->l_start, &ufl->l_start) ||
491             __get_user(kfl->l_len, &ufl->l_len) ||
492             __get_user(kfl->l_pid, &ufl->l_pid))
493                 return -EFAULT;
494         return 0;
495 }
496 #endif
497
498 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
499 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
500 {
501         if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
502             __put_user(kfl->l_type, &ufl->l_type) ||
503             __put_user(kfl->l_whence, &ufl->l_whence) ||
504             __put_user(kfl->l_start, &ufl->l_start) ||
505             __put_user(kfl->l_len, &ufl->l_len) ||
506             __put_user(kfl->l_pid, &ufl->l_pid))
507                 return -EFAULT;
508         return 0;
509 }
510 #endif
511
512 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
513                 unsigned long arg)
514 {
515         mm_segment_t old_fs;
516         struct flock f;
517         long ret;
518
519         switch (cmd) {
520         case F_GETLK:
521         case F_SETLK:
522         case F_SETLKW:
523                 ret = get_compat_flock(&f, compat_ptr(arg));
524                 if (ret != 0)
525                         break;
526                 old_fs = get_fs();
527                 set_fs(KERNEL_DS);
528                 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
529                 set_fs(old_fs);
530                 if ((cmd == F_GETLK) && (ret == 0)) {
531                         if ((compat_off_t) f.l_start != f.l_start ||
532                             (compat_off_t) f.l_len != f.l_len)
533                                 ret = -EOVERFLOW;
534                         else
535                                 ret = put_compat_flock(&f, compat_ptr(arg));
536                 }
537                 break;
538
539         case F_GETLK64:
540         case F_SETLK64:
541         case F_SETLKW64:
542                 ret = get_compat_flock64(&f, compat_ptr(arg));
543                 if (ret != 0)
544                         break;
545                 old_fs = get_fs();
546                 set_fs(KERNEL_DS);
547                 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
548                                 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
549                                 (unsigned long)&f);
550                 set_fs(old_fs);
551                 if ((cmd == F_GETLK64) && (ret == 0)) {
552                         if ((compat_loff_t) f.l_start != f.l_start ||
553                             (compat_loff_t) f.l_len != f.l_len)
554                                 ret = -EOVERFLOW;
555                         else
556                                 ret = put_compat_flock64(&f, compat_ptr(arg));
557                 }
558                 break;
559
560         default:
561                 ret = sys_fcntl(fd, cmd, arg);
562                 break;
563         }
564         return ret;
565 }
566
567 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
568                 unsigned long arg)
569 {
570         if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
571                 return -EINVAL;
572         return compat_sys_fcntl64(fd, cmd, arg);
573 }
574
575 asmlinkage long
576 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
577 {
578         long ret;
579         aio_context_t ctx64;
580
581         mm_segment_t oldfs = get_fs();
582         if (unlikely(get_user(ctx64, ctx32p)))
583                 return -EFAULT;
584
585         set_fs(KERNEL_DS);
586         /* The __user pointer cast is valid because of the set_fs() */
587         ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
588         set_fs(oldfs);
589         /* truncating is ok because it's a user address */
590         if (!ret)
591                 ret = put_user((u32) ctx64, ctx32p);
592         return ret;
593 }
594
595 asmlinkage long
596 compat_sys_io_getevents(aio_context_t ctx_id,
597                                  unsigned long min_nr,
598                                  unsigned long nr,
599                                  struct io_event __user *events,
600                                  struct compat_timespec __user *timeout)
601 {
602         long ret;
603         struct timespec t;
604         struct timespec __user *ut = NULL;
605
606         ret = -EFAULT;
607         if (unlikely(!access_ok(VERIFY_WRITE, events, 
608                                 nr * sizeof(struct io_event))))
609                 goto out;
610         if (timeout) {
611                 if (get_compat_timespec(&t, timeout))
612                         goto out;
613
614                 ut = compat_alloc_user_space(sizeof(*ut));
615                 if (copy_to_user(ut, &t, sizeof(t)) )
616                         goto out;
617         } 
618         ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
619 out:
620         return ret;
621 }
622
623 static inline long
624 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
625 {
626         compat_uptr_t uptr;
627         int i;
628
629         for (i = 0; i < nr; ++i) {
630                 if (get_user(uptr, ptr32 + i))
631                         return -EFAULT;
632                 if (put_user(compat_ptr(uptr), ptr64 + i))
633                         return -EFAULT;
634         }
635         return 0;
636 }
637
638 #define MAX_AIO_SUBMITS         (PAGE_SIZE/sizeof(struct iocb *))
639
640 asmlinkage long
641 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
642 {
643         struct iocb __user * __user *iocb64; 
644         long ret;
645
646         if (unlikely(nr < 0))
647                 return -EINVAL;
648
649         if (nr > MAX_AIO_SUBMITS)
650                 nr = MAX_AIO_SUBMITS;
651         
652         iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
653         ret = copy_iocb(nr, iocb, iocb64);
654         if (!ret)
655                 ret = sys_io_submit(ctx_id, nr, iocb64);
656         return ret;
657 }
658
659 struct compat_ncp_mount_data {
660         compat_int_t version;
661         compat_uint_t ncp_fd;
662         compat_uid_t mounted_uid;
663         compat_pid_t wdog_pid;
664         unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
665         compat_uint_t time_out;
666         compat_uint_t retry_count;
667         compat_uint_t flags;
668         compat_uid_t uid;
669         compat_gid_t gid;
670         compat_mode_t file_mode;
671         compat_mode_t dir_mode;
672 };
673
674 struct compat_ncp_mount_data_v4 {
675         compat_int_t version;
676         compat_ulong_t flags;
677         compat_ulong_t mounted_uid;
678         compat_long_t wdog_pid;
679         compat_uint_t ncp_fd;
680         compat_uint_t time_out;
681         compat_uint_t retry_count;
682         compat_ulong_t uid;
683         compat_ulong_t gid;
684         compat_ulong_t file_mode;
685         compat_ulong_t dir_mode;
686 };
687
688 static void *do_ncp_super_data_conv(void *raw_data)
689 {
690         int version = *(unsigned int *)raw_data;
691
692         if (version == 3) {
693                 struct compat_ncp_mount_data *c_n = raw_data;
694                 struct ncp_mount_data *n = raw_data;
695
696                 n->dir_mode = c_n->dir_mode;
697                 n->file_mode = c_n->file_mode;
698                 n->gid = c_n->gid;
699                 n->uid = c_n->uid;
700                 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
701                 n->wdog_pid = c_n->wdog_pid;
702                 n->mounted_uid = c_n->mounted_uid;
703         } else if (version == 4) {
704                 struct compat_ncp_mount_data_v4 *c_n = raw_data;
705                 struct ncp_mount_data_v4 *n = raw_data;
706
707                 n->dir_mode = c_n->dir_mode;
708                 n->file_mode = c_n->file_mode;
709                 n->gid = c_n->gid;
710                 n->uid = c_n->uid;
711                 n->retry_count = c_n->retry_count;
712                 n->time_out = c_n->time_out;
713                 n->ncp_fd = c_n->ncp_fd;
714                 n->wdog_pid = c_n->wdog_pid;
715                 n->mounted_uid = c_n->mounted_uid;
716                 n->flags = c_n->flags;
717         } else if (version != 5) {
718                 return NULL;
719         }
720
721         return raw_data;
722 }
723
724 struct compat_smb_mount_data {
725         compat_int_t version;
726         compat_uid_t mounted_uid;
727         compat_uid_t uid;
728         compat_gid_t gid;
729         compat_mode_t file_mode;
730         compat_mode_t dir_mode;
731 };
732
733 static void *do_smb_super_data_conv(void *raw_data)
734 {
735         struct smb_mount_data *s = raw_data;
736         struct compat_smb_mount_data *c_s = raw_data;
737
738         if (c_s->version != SMB_MOUNT_OLDVERSION)
739                 goto out;
740         s->dir_mode = c_s->dir_mode;
741         s->file_mode = c_s->file_mode;
742         s->gid = c_s->gid;
743         s->uid = c_s->uid;
744         s->mounted_uid = c_s->mounted_uid;
745  out:
746         return raw_data;
747 }
748
749 extern int copy_mount_options (const void __user *, unsigned long *);
750
751 #define SMBFS_NAME      "smbfs"
752 #define NCPFS_NAME      "ncpfs"
753
754 asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
755                                  char __user * type, unsigned long flags,
756                                  void __user * data)
757 {
758         unsigned long type_page;
759         unsigned long data_page;
760         unsigned long dev_page;
761         char *dir_page;
762         int retval;
763
764         retval = copy_mount_options (type, &type_page);
765         if (retval < 0)
766                 goto out;
767
768         dir_page = getname(dir_name);
769         retval = PTR_ERR(dir_page);
770         if (IS_ERR(dir_page))
771                 goto out1;
772
773         retval = copy_mount_options (dev_name, &dev_page);
774         if (retval < 0)
775                 goto out2;
776
777         retval = copy_mount_options (data, &data_page);
778         if (retval < 0)
779                 goto out3;
780
781         retval = -EINVAL;
782
783         if (type_page) {
784                 if (!strcmp((char *)type_page, SMBFS_NAME)) {
785                         do_smb_super_data_conv((void *)data_page);
786                 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
787                         do_ncp_super_data_conv((void *)data_page);
788                 }
789         }
790
791         lock_kernel();
792         retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
793                         flags, (void*)data_page);
794         unlock_kernel();
795
796         free_page(data_page);
797  out3:
798         free_page(dev_page);
799  out2:
800         putname(dir_page);
801  out1:
802         free_page(type_page);
803  out:
804         return retval;
805 }
806
807 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
808 #define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
809                                 ~(sizeof(compat_long_t)-1))
810
811 struct compat_old_linux_dirent {
812         compat_ulong_t  d_ino;
813         compat_ulong_t  d_offset;
814         unsigned short  d_namlen;
815         char            d_name[1];
816 };
817
818 struct compat_readdir_callback {
819         struct compat_old_linux_dirent __user *dirent;
820         int result;
821 };
822
823 static int compat_fillonedir(void *__buf, const char *name, int namlen,
824                         loff_t offset, ino_t ino, unsigned int d_type)
825 {
826         struct compat_readdir_callback *buf = __buf;
827         struct compat_old_linux_dirent __user *dirent;
828
829         if (buf->result)
830                 return -EINVAL;
831         buf->result++;
832         dirent = buf->dirent;
833         if (!access_ok(VERIFY_WRITE, dirent,
834                         (unsigned long)(dirent->d_name + namlen + 1) -
835                                 (unsigned long)dirent))
836                 goto efault;
837         if (    __put_user(ino, &dirent->d_ino) ||
838                 __put_user(offset, &dirent->d_offset) ||
839                 __put_user(namlen, &dirent->d_namlen) ||
840                 __copy_to_user(dirent->d_name, name, namlen) ||
841                 __put_user(0, dirent->d_name + namlen))
842                 goto efault;
843         return 0;
844 efault:
845         buf->result = -EFAULT;
846         return -EFAULT;
847 }
848
849 asmlinkage long compat_old_readdir(unsigned int fd,
850         struct compat_old_linux_dirent __user *dirent, unsigned int count)
851 {
852         int error;
853         struct file *file;
854         struct compat_readdir_callback buf;
855
856         error = -EBADF;
857         file = fget(fd);
858         if (!file)
859                 goto out;
860
861         buf.result = 0;
862         buf.dirent = dirent;
863
864         error = vfs_readdir(file, compat_fillonedir, &buf);
865         if (error >= 0)
866                 error = buf.result;
867
868         fput(file);
869 out:
870         return error;
871 }
872
873 struct compat_linux_dirent {
874         compat_ulong_t  d_ino;
875         compat_ulong_t  d_off;
876         unsigned short  d_reclen;
877         char            d_name[1];
878 };
879
880 struct compat_getdents_callback {
881         struct compat_linux_dirent __user *current_dir;
882         struct compat_linux_dirent __user *previous;
883         int count;
884         int error;
885 };
886
887 static int compat_filldir(void *__buf, const char *name, int namlen,
888                 loff_t offset, ino_t ino, unsigned int d_type)
889 {
890         struct compat_linux_dirent __user * dirent;
891         struct compat_getdents_callback *buf = __buf;
892         int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
893
894         buf->error = -EINVAL;   /* only used if we fail.. */
895         if (reclen > buf->count)
896                 return -EINVAL;
897         dirent = buf->previous;
898         if (dirent) {
899                 if (__put_user(offset, &dirent->d_off))
900                         goto efault;
901         }
902         dirent = buf->current_dir;
903         if (__put_user(ino, &dirent->d_ino))
904                 goto efault;
905         if (__put_user(reclen, &dirent->d_reclen))
906                 goto efault;
907         if (copy_to_user(dirent->d_name, name, namlen))
908                 goto efault;
909         if (__put_user(0, dirent->d_name + namlen))
910                 goto efault;
911         if (__put_user(d_type, (char  __user *) dirent + reclen - 1))
912                 goto efault;
913         buf->previous = dirent;
914         dirent = (void __user *)dirent + reclen;
915         buf->current_dir = dirent;
916         buf->count -= reclen;
917         return 0;
918 efault:
919         buf->error = -EFAULT;
920         return -EFAULT;
921 }
922
923 asmlinkage long compat_sys_getdents(unsigned int fd,
924                 struct compat_linux_dirent __user *dirent, unsigned int count)
925 {
926         struct file * file;
927         struct compat_linux_dirent __user * lastdirent;
928         struct compat_getdents_callback buf;
929         int error;
930
931         error = -EFAULT;
932         if (!access_ok(VERIFY_WRITE, dirent, count))
933                 goto out;
934
935         error = -EBADF;
936         file = fget(fd);
937         if (!file)
938                 goto out;
939
940         buf.current_dir = dirent;
941         buf.previous = NULL;
942         buf.count = count;
943         buf.error = 0;
944
945         error = vfs_readdir(file, compat_filldir, &buf);
946         if (error < 0)
947                 goto out_putf;
948         error = buf.error;
949         lastdirent = buf.previous;
950         if (lastdirent) {
951                 if (put_user(file->f_pos, &lastdirent->d_off))
952                         error = -EFAULT;
953                 else
954                         error = count - buf.count;
955         }
956
957 out_putf:
958         fput(file);
959 out:
960         return error;
961 }
962
963 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
964 #define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
965
966 struct compat_getdents_callback64 {
967         struct linux_dirent64 __user *current_dir;
968         struct linux_dirent64 __user *previous;
969         int count;
970         int error;
971 };
972
973 static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
974                      ino_t ino, unsigned int d_type)
975 {
976         struct linux_dirent64 __user *dirent;
977         struct compat_getdents_callback64 *buf = __buf;
978         int jj = NAME_OFFSET(dirent);
979         int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
980         u64 off;
981
982         buf->error = -EINVAL;   /* only used if we fail.. */
983         if (reclen > buf->count)
984                 return -EINVAL;
985         dirent = buf->previous;
986
987         if (dirent) {
988                 if (__put_user_unaligned(offset, &dirent->d_off))
989                         goto efault;
990         }
991         dirent = buf->current_dir;
992         if (__put_user_unaligned(ino, &dirent->d_ino))
993                 goto efault;
994         off = 0;
995         if (__put_user_unaligned(off, &dirent->d_off))
996                 goto efault;
997         if (__put_user(reclen, &dirent->d_reclen))
998                 goto efault;
999         if (__put_user(d_type, &dirent->d_type))
1000                 goto efault;
1001         if (copy_to_user(dirent->d_name, name, namlen))
1002                 goto efault;
1003         if (__put_user(0, dirent->d_name + namlen))
1004                 goto efault;
1005         buf->previous = dirent;
1006         dirent = (void __user *)dirent + reclen;
1007         buf->current_dir = dirent;
1008         buf->count -= reclen;
1009         return 0;
1010 efault:
1011         buf->error = -EFAULT;
1012         return -EFAULT;
1013 }
1014
1015 asmlinkage long compat_sys_getdents64(unsigned int fd,
1016                 struct linux_dirent64 __user * dirent, unsigned int count)
1017 {
1018         struct file * file;
1019         struct linux_dirent64 __user * lastdirent;
1020         struct compat_getdents_callback64 buf;
1021         int error;
1022
1023         error = -EFAULT;
1024         if (!access_ok(VERIFY_WRITE, dirent, count))
1025                 goto out;
1026
1027         error = -EBADF;
1028         file = fget(fd);
1029         if (!file)
1030                 goto out;
1031
1032         buf.current_dir = dirent;
1033         buf.previous = NULL;
1034         buf.count = count;
1035         buf.error = 0;
1036
1037         error = vfs_readdir(file, compat_filldir64, &buf);
1038         if (error < 0)
1039                 goto out_putf;
1040         error = buf.error;
1041         lastdirent = buf.previous;
1042         if (lastdirent) {
1043                 typeof(lastdirent->d_off) d_off = file->f_pos;
1044                 __put_user_unaligned(d_off, &lastdirent->d_off);
1045                 error = count - buf.count;
1046         }
1047
1048 out_putf:
1049         fput(file);
1050 out:
1051         return error;
1052 }
1053 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1054
1055 static ssize_t compat_do_readv_writev(int type, struct file *file,
1056                                const struct compat_iovec __user *uvector,
1057                                unsigned long nr_segs, loff_t *pos)
1058 {
1059         typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
1060         typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
1061
1062         compat_ssize_t tot_len;
1063         struct iovec iovstack[UIO_FASTIOV];
1064         struct iovec *iov=iovstack, *vector;
1065         ssize_t ret;
1066         int seg;
1067         io_fn_t fn;
1068         iov_fn_t fnv;
1069         struct inode *inode;
1070
1071         /*
1072          * SuS says "The readv() function *may* fail if the iovcnt argument
1073          * was less than or equal to 0, or greater than {IOV_MAX}.  Linux has
1074          * traditionally returned zero for zero segments, so...
1075          */
1076         ret = 0;
1077         if (nr_segs == 0)
1078                 goto out;
1079
1080         /*
1081          * First get the "struct iovec" from user memory and
1082          * verify all the pointers
1083          */
1084         ret = -EINVAL;
1085         if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1086                 goto out;
1087         if (!file->f_op)
1088                 goto out;
1089         if (nr_segs > UIO_FASTIOV) {
1090                 ret = -ENOMEM;
1091                 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1092                 if (!iov)
1093                         goto out;
1094         }
1095         ret = -EFAULT;
1096         if (verify_area(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1097                 goto out;
1098
1099         /*
1100          * Single unix specification:
1101          * We should -EINVAL if an element length is not >= 0 and fitting an
1102          * ssize_t.  The total length is fitting an ssize_t
1103          *
1104          * Be careful here because iov_len is a size_t not an ssize_t
1105          */
1106         tot_len = 0;
1107         vector = iov;
1108         ret = -EINVAL;
1109         for (seg = 0 ; seg < nr_segs; seg++) {
1110                 compat_ssize_t tmp = tot_len;
1111                 compat_ssize_t len;
1112                 compat_uptr_t buf;
1113
1114                 if (__get_user(len, &uvector->iov_len) ||
1115                     __get_user(buf, &uvector->iov_base)) {
1116                         ret = -EFAULT;
1117                         goto out;
1118                 }
1119                 if (len < 0)    /* size_t not fitting an compat_ssize_t .. */
1120                         goto out;
1121                 tot_len += len;
1122                 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1123                         goto out;
1124                 vector->iov_base = compat_ptr(buf);
1125                 vector->iov_len = (compat_size_t) len;
1126                 uvector++;
1127                 vector++;
1128         }
1129         if (tot_len == 0) {
1130                 ret = 0;
1131                 goto out;
1132         }
1133
1134         inode = file->f_dentry->d_inode;
1135         /* VERIFY_WRITE actually means a read, as we write to user space */
1136         ret = locks_verify_area((type == READ
1137                                  ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE),
1138                                 inode, file, *pos, tot_len);
1139         if (ret)
1140                 goto out;
1141
1142         fnv = NULL;
1143         if (type == READ) {
1144                 fn = file->f_op->read;
1145                 fnv = file->f_op->readv;
1146         } else {
1147                 fn = (io_fn_t)file->f_op->write;
1148                 fnv = file->f_op->writev;
1149         }
1150         if (fnv) {
1151                 ret = fnv(file, iov, nr_segs, pos);
1152                 goto out;
1153         }
1154
1155         /* Do it by hand, with file-ops */
1156         ret = 0;
1157         vector = iov;
1158         while (nr_segs > 0) {
1159                 void __user * base;
1160                 size_t len;
1161                 ssize_t nr;
1162
1163                 base = vector->iov_base;
1164                 len = vector->iov_len;
1165                 vector++;
1166                 nr_segs--;
1167
1168                 nr = fn(file, base, len, pos);
1169
1170                 if (nr < 0) {
1171                         if (!ret) ret = nr;
1172                         break;
1173                 }
1174                 ret += nr;
1175                 if (nr != len)
1176                         break;
1177         }
1178 out:
1179         if (iov != iovstack)
1180                 kfree(iov);
1181         if ((ret + (type == READ)) > 0)
1182                 dnotify_parent(file->f_dentry,
1183                                 (type == READ) ? DN_ACCESS : DN_MODIFY);
1184         return ret;
1185 }
1186
1187 asmlinkage ssize_t
1188 compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1189 {
1190         struct file *file;
1191         ssize_t ret = -EBADF;
1192
1193         file = fget(fd);
1194         if (!file)
1195                 return -EBADF;
1196
1197         if (!(file->f_mode & FMODE_READ))
1198                 goto out;
1199
1200         ret = -EINVAL;
1201         if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
1202                 goto out;
1203
1204         ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1205
1206 out:
1207         fput(file);
1208         return ret;
1209 }
1210
1211 asmlinkage ssize_t
1212 compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1213 {
1214         struct file *file;
1215         ssize_t ret = -EBADF;
1216
1217         file = fget(fd);
1218         if (!file)
1219                 return -EBADF;
1220         if (!(file->f_mode & FMODE_WRITE))
1221                 goto out;
1222
1223         ret = -EINVAL;
1224         if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
1225                 goto out;
1226
1227         ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1228
1229 out:
1230         fput(file);
1231         return ret;
1232 }
1233
1234 /*
1235  * compat_count() counts the number of arguments/envelopes. It is basically
1236  * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1237  * and envp pointers.
1238  */
1239 static int compat_count(compat_uptr_t __user *argv, int max)
1240 {
1241         int i = 0;
1242
1243         if (argv != NULL) {
1244                 for (;;) {
1245                         compat_uptr_t p;
1246
1247                         if (get_user(p, argv))
1248                                 return -EFAULT;
1249                         if (!p)
1250                                 break;
1251                         argv++;
1252                         if(++i > max)
1253                                 return -E2BIG;
1254                 }
1255         }
1256         return i;
1257 }
1258
1259 /*
1260  * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1261  * except that it works with 32 bit argv and envp pointers.
1262  */
1263 static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1264                                 struct linux_binprm *bprm)
1265 {
1266         struct page *kmapped_page = NULL;
1267         char *kaddr = NULL;
1268         int ret;
1269
1270         while (argc-- > 0) {
1271                 compat_uptr_t str;
1272                 int len;
1273                 unsigned long pos;
1274
1275                 if (get_user(str, argv+argc) ||
1276                         !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1277                         ret = -EFAULT;
1278                         goto out;
1279                 }
1280
1281                 if (bprm->p < len)  {
1282                         ret = -E2BIG;
1283                         goto out;
1284                 }
1285
1286                 bprm->p -= len;
1287                 /* XXX: add architecture specific overflow check here. */
1288                 pos = bprm->p;
1289
1290                 while (len > 0) {
1291                         int i, new, err;
1292                         int offset, bytes_to_copy;
1293                         struct page *page;
1294
1295                         offset = pos % PAGE_SIZE;
1296                         i = pos/PAGE_SIZE;
1297                         page = bprm->page[i];
1298                         new = 0;
1299                         if (!page) {
1300                                 page = alloc_page(GFP_HIGHUSER);
1301                                 bprm->page[i] = page;
1302                                 if (!page) {
1303                                         ret = -ENOMEM;
1304                                         goto out;
1305                                 }
1306                                 new = 1;
1307                         }
1308
1309                         if (page != kmapped_page) {
1310                                 if (kmapped_page)
1311                                         kunmap(kmapped_page);
1312                                 kmapped_page = page;
1313                                 kaddr = kmap(kmapped_page);
1314                         }
1315                         if (new && offset)
1316                                 memset(kaddr, 0, offset);
1317                         bytes_to_copy = PAGE_SIZE - offset;
1318                         if (bytes_to_copy > len) {
1319                                 bytes_to_copy = len;
1320                                 if (new)
1321                                         memset(kaddr+offset+len, 0,
1322                                                 PAGE_SIZE-offset-len);
1323                         }
1324                         err = copy_from_user(kaddr+offset, compat_ptr(str),
1325                                                 bytes_to_copy);
1326                         if (err) {
1327                                 ret = -EFAULT;
1328                                 goto out;
1329                         }
1330
1331                         pos += bytes_to_copy;
1332                         str += bytes_to_copy;
1333                         len -= bytes_to_copy;
1334                 }
1335         }
1336         ret = 0;
1337 out:
1338         if (kmapped_page)
1339                 kunmap(kmapped_page);
1340         return ret;
1341 }
1342
1343 #ifdef CONFIG_MMU
1344
1345 #define free_arg_pages(bprm) do { } while (0)
1346
1347 #else
1348
1349 static inline void free_arg_pages(struct linux_binprm *bprm)
1350 {
1351         int i;
1352
1353         for (i = 0; i < MAX_ARG_PAGES; i++) {
1354                 if (bprm->page[i])
1355                         __free_page(bprm->page[i]);
1356                 bprm->page[i] = NULL;
1357         }
1358 }
1359
1360 #endif /* CONFIG_MMU */
1361
1362 /*
1363  * compat_do_execve() is mostly a copy of do_execve(), with the exception
1364  * that it processes 32 bit argv and envp pointers.
1365  */
1366 int compat_do_execve(char * filename,
1367         compat_uptr_t __user *argv,
1368         compat_uptr_t __user *envp,
1369         struct pt_regs * regs)
1370 {
1371         struct linux_binprm *bprm;
1372         struct file *file;
1373         int retval;
1374         int i;
1375
1376         file = open_exec(filename);
1377
1378         retval = PTR_ERR(file);
1379         if (IS_ERR(file))
1380                 return retval;
1381
1382         sched_exec();
1383
1384         retval = -ENOMEM;
1385         bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1386         if (!bprm)
1387                 goto out_ret;
1388         memset(bprm, 0, sizeof(*bprm));
1389
1390         bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1391         bprm->file = file;
1392         bprm->filename = filename;
1393         bprm->interp = filename;
1394         bprm->mm = mm_alloc();
1395         if (!bprm->mm)
1396                 goto out_file;
1397
1398         retval = init_new_context(current, bprm->mm);
1399         if (retval < 0)
1400                 goto out_mm;
1401
1402         bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1403         if ((retval = bprm->argc) < 0)
1404                 goto out_mm;
1405
1406         bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1407         if ((retval = bprm->envc) < 0)
1408                 goto out_mm;
1409
1410         retval = security_bprm_alloc(bprm);
1411         if (retval)
1412                 goto out;
1413
1414         retval = prepare_binprm(bprm);
1415         if (retval < 0)
1416                 goto out;
1417
1418         retval = copy_strings_kernel(1, &bprm->filename, bprm);
1419         if (retval < 0)
1420                 goto out;
1421
1422         bprm->exec = bprm->p;
1423         retval = compat_copy_strings(bprm->envc, envp, bprm);
1424         if (retval < 0)
1425                 goto out;
1426
1427         retval = compat_copy_strings(bprm->argc, argv, bprm);
1428         if (retval < 0)
1429                 goto out;
1430
1431         retval = search_binary_handler(bprm, regs);
1432         if (retval >= 0) {
1433                 free_arg_pages(bprm);
1434
1435                 /* execve success */
1436                 security_bprm_free(bprm);
1437                 kfree(bprm);
1438                 return retval;
1439         }
1440
1441 out:
1442         /* Something went wrong, return the inode and free the argument pages*/
1443         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1444                 struct page * page = bprm->page[i];
1445                 if (page)
1446                         __free_page(page);
1447         }
1448
1449         if (bprm->security)
1450                 security_bprm_free(bprm);
1451
1452 out_mm:
1453         if (bprm->mm)
1454                 mmdrop(bprm->mm);
1455
1456 out_file:
1457         if (bprm->file) {
1458                 allow_write_access(bprm->file);
1459                 fput(bprm->file);
1460         }
1461         kfree(bprm);
1462
1463 out_ret:
1464         return retval;
1465 }
1466
1467 #define __COMPAT_NFDBITS       (8 * sizeof(compat_ulong_t))
1468
1469 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1470
1471 /*
1472  * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
1473  * 64-bit unsigned longs.
1474  */
1475 static inline
1476 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1477                         unsigned long *fdset)
1478 {
1479         nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1480         if (ufdset) {
1481                 unsigned long odd;
1482
1483                 if (verify_area(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1484                         return -EFAULT;
1485
1486                 odd = nr & 1UL;
1487                 nr &= ~1UL;
1488                 while (nr) {
1489                         unsigned long h, l;
1490                         __get_user(l, ufdset);
1491                         __get_user(h, ufdset+1);
1492                         ufdset += 2;
1493                         *fdset++ = h << 32 | l;
1494                         nr -= 2;
1495                 }
1496                 if (odd)
1497                         __get_user(*fdset, ufdset);
1498         } else {
1499                 /* Tricky, must clear full unsigned long in the
1500                  * kernel fdset at the end, this makes sure that
1501                  * actually happens.
1502                  */
1503                 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1504         }
1505         return 0;
1506 }
1507
1508 static inline
1509 void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1510                         unsigned long *fdset)
1511 {
1512         unsigned long odd;
1513         nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1514
1515         if (!ufdset)
1516                 return;
1517
1518         odd = nr & 1UL;
1519         nr &= ~1UL;
1520         while (nr) {
1521                 unsigned long h, l;
1522                 l = *fdset++;
1523                 h = l >> 32;
1524                 __put_user(l, ufdset);
1525                 __put_user(h, ufdset+1);
1526                 ufdset += 2;
1527                 nr -= 2;
1528         }
1529         if (odd)
1530                 __put_user(*fdset, ufdset);
1531 }
1532
1533
1534 /*
1535  * This is a virtual copy of sys_select from fs/select.c and probably
1536  * should be compared to it from time to time
1537  */
1538 static void *select_bits_alloc(int size)
1539 {
1540         return kmalloc(6 * size, GFP_KERNEL);
1541 }
1542
1543 static void select_bits_free(void *bits, int size)
1544 {
1545         kfree(bits);
1546 }
1547
1548 /*
1549  * We can actually return ERESTARTSYS instead of EINTR, but I'd
1550  * like to be certain this leads to no problems. So I return
1551  * EINTR just for safety.
1552  *
1553  * Update: ERESTARTSYS breaks at least the xview clock binary, so
1554  * I'm trying ERESTARTNOHAND which restart only when you want to.
1555  */
1556 #define MAX_SELECT_SECONDS \
1557         ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1558
1559 asmlinkage long
1560 compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp,
1561                 compat_ulong_t __user *exp, struct compat_timeval __user *tvp)
1562 {
1563         fd_set_bits fds;
1564         char *bits;
1565         long timeout;
1566         int ret, size, max_fdset;
1567
1568         timeout = MAX_SCHEDULE_TIMEOUT;
1569         if (tvp) {
1570                 time_t sec, usec;
1571
1572                 if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp)))
1573                     || (ret = __get_user(sec, &tvp->tv_sec))
1574                     || (ret = __get_user(usec, &tvp->tv_usec)))
1575                         goto out_nofds;
1576
1577                 ret = -EINVAL;
1578                 if (sec < 0 || usec < 0)
1579                         goto out_nofds;
1580
1581                 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
1582                         timeout = ROUND_UP(usec, 1000000/HZ);
1583                         timeout += sec * (unsigned long) HZ;
1584                 }
1585         }
1586
1587         ret = -EINVAL;
1588         if (n < 0)
1589                 goto out_nofds;
1590
1591         /* max_fdset can increase, so grab it once to avoid race */
1592         max_fdset = current->files->max_fdset;
1593         if (n > max_fdset)
1594                 n = max_fdset;
1595
1596         /*
1597          * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1598          * since we used fdset we need to allocate memory in units of
1599          * long-words.
1600          */
1601         ret = -ENOMEM;
1602         size = FDS_BYTES(n);
1603         bits = select_bits_alloc(size);
1604         if (!bits)
1605                 goto out_nofds;
1606         fds.in      = (unsigned long *)  bits;
1607         fds.out     = (unsigned long *) (bits +   size);
1608         fds.ex      = (unsigned long *) (bits + 2*size);
1609         fds.res_in  = (unsigned long *) (bits + 3*size);
1610         fds.res_out = (unsigned long *) (bits + 4*size);
1611         fds.res_ex  = (unsigned long *) (bits + 5*size);
1612
1613         if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1614             (ret = compat_get_fd_set(n, outp, fds.out)) ||
1615             (ret = compat_get_fd_set(n, exp, fds.ex)))
1616                 goto out;
1617         zero_fd_set(n, fds.res_in);
1618         zero_fd_set(n, fds.res_out);
1619         zero_fd_set(n, fds.res_ex);
1620
1621         ret = do_select(n, &fds, &timeout);
1622
1623         if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
1624                 time_t sec = 0, usec = 0;
1625                 if (timeout) {
1626                         sec = timeout / HZ;
1627                         usec = timeout % HZ;
1628                         usec *= (1000000/HZ);
1629                 }
1630                 if (put_user(sec, &tvp->tv_sec) ||
1631                     put_user(usec, &tvp->tv_usec))
1632                         ret = -EFAULT;
1633         }
1634
1635         if (ret < 0)
1636                 goto out;
1637         if (!ret) {
1638                 ret = -ERESTARTNOHAND;
1639                 if (signal_pending(current))
1640                         goto out;
1641                 ret = 0;
1642         }
1643
1644         compat_set_fd_set(n, inp, fds.res_in);
1645         compat_set_fd_set(n, outp, fds.res_out);
1646         compat_set_fd_set(n, exp, fds.res_ex);
1647
1648 out:
1649         select_bits_free(bits, size);
1650 out_nofds:
1651         return ret;
1652 }
1653
1654 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1655 /* Stuff for NFS server syscalls... */
1656 struct compat_nfsctl_svc {
1657         u16                     svc32_port;
1658         s32                     svc32_nthreads;
1659 };
1660
1661 struct compat_nfsctl_client {
1662         s8                      cl32_ident[NFSCLNT_IDMAX+1];
1663         s32                     cl32_naddr;
1664         struct in_addr          cl32_addrlist[NFSCLNT_ADDRMAX];
1665         s32                     cl32_fhkeytype;
1666         s32                     cl32_fhkeylen;
1667         u8                      cl32_fhkey[NFSCLNT_KEYMAX];
1668 };
1669
1670 struct compat_nfsctl_export {
1671         char            ex32_client[NFSCLNT_IDMAX+1];
1672         char            ex32_path[NFS_MAXPATHLEN+1];
1673         compat_dev_t    ex32_dev;
1674         compat_ino_t    ex32_ino;
1675         compat_int_t    ex32_flags;
1676         compat_uid_t    ex32_anon_uid;
1677         compat_gid_t    ex32_anon_gid;
1678 };
1679
1680 struct compat_nfsctl_fdparm {
1681         struct sockaddr         gd32_addr;
1682         s8                      gd32_path[NFS_MAXPATHLEN+1];
1683         compat_int_t            gd32_version;
1684 };
1685
1686 struct compat_nfsctl_fsparm {
1687         struct sockaddr         gd32_addr;
1688         s8                      gd32_path[NFS_MAXPATHLEN+1];
1689         compat_int_t            gd32_maxlen;
1690 };
1691
1692 struct compat_nfsctl_arg {
1693         compat_int_t            ca32_version;   /* safeguard */
1694         union {
1695                 struct compat_nfsctl_svc        u32_svc;
1696                 struct compat_nfsctl_client     u32_client;
1697                 struct compat_nfsctl_export     u32_export;
1698                 struct compat_nfsctl_fdparm     u32_getfd;
1699                 struct compat_nfsctl_fsparm     u32_getfs;
1700         } u;
1701 #define ca32_svc        u.u32_svc
1702 #define ca32_client     u.u32_client
1703 #define ca32_export     u.u32_export
1704 #define ca32_getfd      u.u32_getfd
1705 #define ca32_getfs      u.u32_getfs
1706 };
1707
1708 union compat_nfsctl_res {
1709         __u8                    cr32_getfh[NFS_FHSIZE];
1710         struct knfsd_fh         cr32_getfs;
1711 };
1712
1713 static int compat_nfs_svc_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1714 {
1715         int err;
1716
1717         err = access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc));
1718         err |= get_user(karg->ca_version, &arg->ca32_version);
1719         err |= __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port);
1720         err |= __get_user(karg->ca_svc.svc_nthreads, &arg->ca32_svc.svc32_nthreads);
1721         return (err) ? -EFAULT : 0;
1722 }
1723
1724 static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1725 {
1726         int err;
1727
1728         err = access_ok(VERIFY_READ, &arg->ca32_client, sizeof(arg->ca32_client));
1729         err |= get_user(karg->ca_version, &arg->ca32_version);
1730         err |= __copy_from_user(&karg->ca_client.cl_ident[0],
1731                           &arg->ca32_client.cl32_ident[0],
1732                           NFSCLNT_IDMAX);
1733         err |= __get_user(karg->ca_client.cl_naddr, &arg->ca32_client.cl32_naddr);
1734         err |= __copy_from_user(&karg->ca_client.cl_addrlist[0],
1735                           &arg->ca32_client.cl32_addrlist[0],
1736                           (sizeof(struct in_addr) * NFSCLNT_ADDRMAX));
1737         err |= __get_user(karg->ca_client.cl_fhkeytype,
1738                       &arg->ca32_client.cl32_fhkeytype);
1739         err |= __get_user(karg->ca_client.cl_fhkeylen,
1740                       &arg->ca32_client.cl32_fhkeylen);
1741         err |= __copy_from_user(&karg->ca_client.cl_fhkey[0],
1742                           &arg->ca32_client.cl32_fhkey[0],
1743                           NFSCLNT_KEYMAX);
1744
1745         return (err) ? -EFAULT : 0;
1746 }
1747
1748 static int compat_nfs_exp_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1749 {
1750         int err;
1751
1752         err = access_ok(VERIFY_READ, &arg->ca32_export, sizeof(arg->ca32_export));
1753         err |= get_user(karg->ca_version, &arg->ca32_version);
1754         err |= __copy_from_user(&karg->ca_export.ex_client[0],
1755                           &arg->ca32_export.ex32_client[0],
1756                           NFSCLNT_IDMAX);
1757         err |= __copy_from_user(&karg->ca_export.ex_path[0],
1758                           &arg->ca32_export.ex32_path[0],
1759                           NFS_MAXPATHLEN);
1760         err |= __get_user(karg->ca_export.ex_dev,
1761                       &arg->ca32_export.ex32_dev);
1762         err |= __get_user(karg->ca_export.ex_ino,
1763                       &arg->ca32_export.ex32_ino);
1764         err |= __get_user(karg->ca_export.ex_flags,
1765                       &arg->ca32_export.ex32_flags);
1766         err |= __get_user(karg->ca_export.ex_anon_uid,
1767                       &arg->ca32_export.ex32_anon_uid);
1768         err |= __get_user(karg->ca_export.ex_anon_gid,
1769                       &arg->ca32_export.ex32_anon_gid);
1770         SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
1771         SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
1772
1773         return (err) ? -EFAULT : 0;
1774 }
1775
1776 static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1777 {
1778         int err;
1779
1780         err = access_ok(VERIFY_READ, &arg->ca32_getfd, sizeof(arg->ca32_getfd));
1781         err |= get_user(karg->ca_version, &arg->ca32_version);
1782         err |= __copy_from_user(&karg->ca_getfd.gd_addr,
1783                           &arg->ca32_getfd.gd32_addr,
1784                           (sizeof(struct sockaddr)));
1785         err |= __copy_from_user(&karg->ca_getfd.gd_path,
1786                           &arg->ca32_getfd.gd32_path,
1787                           (NFS_MAXPATHLEN+1));
1788         err |= __get_user(karg->ca_getfd.gd_version,
1789                       &arg->ca32_getfd.gd32_version);
1790
1791         return (err) ? -EFAULT : 0;
1792 }
1793
1794 static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1795 {
1796         int err;
1797
1798         err = access_ok(VERIFY_READ, &arg->ca32_getfs, sizeof(arg->ca32_getfs));
1799         err |= get_user(karg->ca_version, &arg->ca32_version);
1800         err |= __copy_from_user(&karg->ca_getfs.gd_addr,
1801                           &arg->ca32_getfs.gd32_addr,
1802                           (sizeof(struct sockaddr)));
1803         err |= __copy_from_user(&karg->ca_getfs.gd_path,
1804                           &arg->ca32_getfs.gd32_path,
1805                           (NFS_MAXPATHLEN+1));
1806         err |= __get_user(karg->ca_getfs.gd_maxlen,
1807                       &arg->ca32_getfs.gd32_maxlen);
1808
1809         return (err) ? -EFAULT : 0;
1810 }
1811
1812 /* This really doesn't need translations, we are only passing
1813  * back a union which contains opaque nfs file handle data.
1814  */
1815 static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, union compat_nfsctl_res __user *res)
1816 {
1817         int err;
1818
1819         err = copy_to_user(res, kres, sizeof(*res));
1820
1821         return (err) ? -EFAULT : 0;
1822 }
1823
1824 asmlinkage long compat_sys_nfsservctl(int cmd, struct compat_nfsctl_arg __user *arg,
1825                                         union compat_nfsctl_res __user *res)
1826 {
1827         struct nfsctl_arg *karg;
1828         union nfsctl_res *kres;
1829         mm_segment_t oldfs;
1830         int err;
1831
1832         karg = kmalloc(sizeof(*karg), GFP_USER);
1833         kres = kmalloc(sizeof(*kres), GFP_USER);
1834         if(!karg || !kres) {
1835                 err = -ENOMEM;
1836                 goto done;
1837         }
1838
1839         switch(cmd) {
1840         case NFSCTL_SVC:
1841                 err = compat_nfs_svc_trans(karg, arg);
1842                 break;
1843
1844         case NFSCTL_ADDCLIENT:
1845                 err = compat_nfs_clnt_trans(karg, arg);
1846                 break;
1847
1848         case NFSCTL_DELCLIENT:
1849                 err = compat_nfs_clnt_trans(karg, arg);
1850                 break;
1851
1852         case NFSCTL_EXPORT:
1853         case NFSCTL_UNEXPORT:
1854                 err = compat_nfs_exp_trans(karg, arg);
1855                 break;
1856
1857         case NFSCTL_GETFD:
1858                 err = compat_nfs_getfd_trans(karg, arg);
1859                 break;
1860
1861         case NFSCTL_GETFS:
1862                 err = compat_nfs_getfs_trans(karg, arg);
1863                 break;
1864
1865         default:
1866                 err = -EINVAL;
1867                 goto done;
1868         }
1869
1870         oldfs = get_fs();
1871         set_fs(KERNEL_DS);
1872         /* The __user pointer casts are valid because of the set_fs() */
1873         err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
1874         set_fs(oldfs);
1875
1876         if (err)
1877                 goto done;
1878
1879         if((cmd == NFSCTL_GETFD) ||
1880            (cmd == NFSCTL_GETFS))
1881                 err = compat_nfs_getfh_res_trans(kres, res);
1882
1883 done:
1884         kfree(karg);
1885         kfree(kres);
1886         return err;
1887 }
1888 #else /* !NFSD */
1889 long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
1890 {
1891         return sys_ni_syscall();
1892 }
1893 #endif