VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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
45 #include <net/sock.h>           /* siocdevprivate_ioctl */
46
47 #include <asm/uaccess.h>
48 #include <asm/mmu_context.h>
49
50 /*
51  * Not all architectures have sys_utime, so implement this in terms
52  * of sys_utimes.
53  */
54 asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
55 {
56         struct timeval tv[2];
57
58         if (t) {
59                 if (get_user(tv[0].tv_sec, &t->actime) ||
60                     get_user(tv[1].tv_sec, &t->modtime))
61                         return -EFAULT;
62                 tv[0].tv_usec = 0;
63                 tv[1].tv_usec = 0;
64         }
65         return do_utimes(filename, t ? tv : NULL);
66 }
67
68 asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
69 {
70         struct timeval tv[2];
71
72         if (t) { 
73                 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
74                     get_user(tv[0].tv_usec, &t[0].tv_usec) ||
75                     get_user(tv[1].tv_sec, &t[1].tv_sec) ||
76                     get_user(tv[1].tv_usec, &t[1].tv_usec))
77                         return -EFAULT; 
78         } 
79         return do_utimes(filename, t ? tv : NULL);
80 }
81
82 asmlinkage long compat_sys_newstat(char __user * filename,
83                 struct compat_stat __user *statbuf)
84 {
85         struct kstat stat;
86         int error = vfs_stat(filename, &stat);
87
88         if (!error)
89                 error = cp_compat_stat(&stat, statbuf);
90         return error;
91 }
92
93 asmlinkage long compat_sys_newlstat(char __user * filename,
94                 struct compat_stat __user *statbuf)
95 {
96         struct kstat stat;
97         int error = vfs_lstat(filename, &stat);
98
99         if (!error)
100                 error = cp_compat_stat(&stat, statbuf);
101         return error;
102 }
103
104 asmlinkage long compat_sys_newfstat(unsigned int fd,
105                 struct compat_stat __user * statbuf)
106 {
107         struct kstat stat;
108         int error = vfs_fstat(fd, &stat);
109
110         if (!error)
111                 error = cp_compat_stat(&stat, statbuf);
112         return error;
113 }
114
115 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
116 {
117         
118         if (sizeof ubuf->f_blocks == 4) {
119                 if ((kbuf->f_blocks | kbuf->f_bfree |
120                      kbuf->f_bavail | kbuf->f_files | kbuf->f_ffree) &
121                     0xffffffff00000000ULL)
122                         return -EOVERFLOW;
123         }
124         if (verify_area(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
125             __put_user(kbuf->f_type, &ubuf->f_type) ||
126             __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
127             __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
128             __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
129             __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
130             __put_user(kbuf->f_files, &ubuf->f_files) ||
131             __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
132             __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
133             __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
134             __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
135             __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
136             __put_user(0, &ubuf->f_spare[0]) || 
137             __put_user(0, &ubuf->f_spare[1]) || 
138             __put_user(0, &ubuf->f_spare[2]) || 
139             __put_user(0, &ubuf->f_spare[3]) || 
140             __put_user(0, &ubuf->f_spare[4]))
141                 return -EFAULT;
142         return 0;
143 }
144
145 /*
146  * The following statfs calls are copies of code from fs/open.c and
147  * should be checked against those from time to time
148  */
149 asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
150 {
151         struct nameidata nd;
152         int error;
153
154         error = user_path_walk(path, &nd);
155         if (!error) {
156                 struct kstatfs tmp;
157                 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
158                 if (!error && put_compat_statfs(buf, &tmp))
159                         error = -EFAULT;
160                 path_release(&nd);
161         }
162         return error;
163 }
164
165 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
166 {
167         struct file * file;
168         struct kstatfs tmp;
169         int error;
170
171         error = -EBADF;
172         file = fget(fd);
173         if (!file)
174                 goto out;
175         error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
176         if (!error && put_compat_statfs(buf, &tmp))
177                 error = -EFAULT;
178         fput(file);
179 out:
180         return error;
181 }
182
183 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
184 {
185         if (sizeof ubuf->f_blocks == 4) {
186                 if ((kbuf->f_blocks | kbuf->f_bfree |
187                      kbuf->f_bavail | kbuf->f_files | kbuf->f_ffree) &
188                     0xffffffff00000000ULL)
189                         return -EOVERFLOW;
190         }
191         if (verify_area(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
192             __put_user(kbuf->f_type, &ubuf->f_type) ||
193             __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
194             __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
195             __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
196             __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
197             __put_user(kbuf->f_files, &ubuf->f_files) ||
198             __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
199             __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
200             __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
201             __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
202             __put_user(kbuf->f_frsize, &ubuf->f_frsize))
203                 return -EFAULT;
204         return 0;
205 }
206
207 asmlinkage long compat_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
208 {
209         struct nameidata nd;
210         int error;
211
212         if (sz != sizeof(*buf))
213                 return -EINVAL;
214
215         error = user_path_walk(path, &nd);
216         if (!error) {
217                 struct kstatfs tmp;
218                 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
219                 if (!error && put_compat_statfs64(buf, &tmp))
220                         error = -EFAULT;
221                 path_release(&nd);
222         }
223         return error;
224 }
225
226 asmlinkage long compat_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
227 {
228         struct file * file;
229         struct kstatfs tmp;
230         int error;
231
232         if (sz != sizeof(*buf))
233                 return -EINVAL;
234
235         error = -EBADF;
236         file = fget(fd);
237         if (!file)
238                 goto out;
239         error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
240         if (!error && put_compat_statfs64(buf, &tmp))
241                 error = -EFAULT;
242         fput(file);
243 out:
244         return error;
245 }
246
247 /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
248
249 #define IOCTL_HASHSIZE 256
250 struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
251
252 extern struct ioctl_trans ioctl_start[];
253 extern int ioctl_table_size;
254
255 static inline unsigned long ioctl32_hash(unsigned long cmd)
256 {
257         return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
258 }
259
260 static void ioctl32_insert_translation(struct ioctl_trans *trans)
261 {
262         unsigned long hash;
263         struct ioctl_trans *t;
264
265         hash = ioctl32_hash (trans->cmd);
266         if (!ioctl32_hash_table[hash])
267                 ioctl32_hash_table[hash] = trans;
268         else {
269                 t = ioctl32_hash_table[hash];
270                 while (t->next)
271                         t = t->next;
272                 trans->next = NULL;
273                 t->next = trans;
274         }
275 }
276
277 static int __init init_sys32_ioctl(void)
278 {
279         int i;
280
281         for (i = 0; i < ioctl_table_size; i++) {
282                 if (ioctl_start[i].next != 0) { 
283                         printk("ioctl translation %d bad\n",i); 
284                         return -1;
285                 }
286
287                 ioctl32_insert_translation(&ioctl_start[i]);
288         }
289         return 0;
290 }
291
292 __initcall(init_sys32_ioctl);
293
294 int register_ioctl32_conversion(unsigned int cmd, int (*handler)(unsigned int,
295                                 unsigned int, unsigned long, struct file *))
296 {
297         struct ioctl_trans *t;
298         struct ioctl_trans *new_t;
299         unsigned long hash = ioctl32_hash(cmd);
300
301         new_t = kmalloc(sizeof(*new_t), GFP_KERNEL);
302         if (!new_t)
303                 return -ENOMEM;
304
305         lock_kernel(); 
306         for (t = ioctl32_hash_table[hash]; t; t = t->next) {
307                 if (t->cmd == cmd) {
308                         printk(KERN_ERR "Trying to register duplicated ioctl32 "
309                                         "handler %x\n", cmd);
310                         unlock_kernel();
311                         kfree(new_t);
312                         return -EINVAL; 
313                 }
314         }
315         new_t->next = NULL;
316         new_t->cmd = cmd;
317         new_t->handler = handler;
318         ioctl32_insert_translation(new_t);
319
320         unlock_kernel();
321         return 0;
322 }
323 EXPORT_SYMBOL(register_ioctl32_conversion);
324
325 static inline int builtin_ioctl(struct ioctl_trans *t)
326
327         return t >= ioctl_start && t < (ioctl_start + ioctl_table_size);
328
329
330 /* Problem: 
331    This function cannot unregister duplicate ioctls, because they are not
332    unique.
333    When they happen we need to extend the prototype to pass the handler too. */
334
335 int unregister_ioctl32_conversion(unsigned int cmd)
336 {
337         unsigned long hash = ioctl32_hash(cmd);
338         struct ioctl_trans *t, *t1;
339
340         lock_kernel(); 
341
342         t = ioctl32_hash_table[hash];
343         if (!t) { 
344                 unlock_kernel();
345                 return -EINVAL;
346         } 
347
348         if (t->cmd == cmd) { 
349                 if (builtin_ioctl(t)) {
350                         printk("%p tried to unregister builtin ioctl %x\n",
351                                __builtin_return_address(0), cmd);
352                 } else { 
353                         ioctl32_hash_table[hash] = t->next;
354                         unlock_kernel();
355                         kfree(t);
356                         return 0;
357                 }
358         } 
359         while (t->next) {
360                 t1 = t->next;
361                 if (t1->cmd == cmd) { 
362                         if (builtin_ioctl(t1)) {
363                                 printk("%p tried to unregister builtin "
364                                         "ioctl %x\n",
365                                         __builtin_return_address(0), cmd);
366                                 goto out;
367                         } else { 
368                                 t->next = t1->next;
369                                 unlock_kernel();
370                                 kfree(t1);
371                                 return 0;
372                         }
373                 }
374                 t = t1;
375         }
376         printk(KERN_ERR "Trying to free unknown 32bit ioctl handler %x\n",
377                                 cmd);
378 out:
379         unlock_kernel();
380         return -EINVAL;
381 }
382 EXPORT_SYMBOL(unregister_ioctl32_conversion); 
383
384 asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
385                                 unsigned long arg)
386 {
387         struct file * filp;
388         int error = -EBADF;
389         struct ioctl_trans *t;
390
391         filp = fget(fd);
392         if(!filp)
393                 goto out2;
394
395         if (!filp->f_op || !filp->f_op->ioctl) {
396                 error = sys_ioctl (fd, cmd, arg);
397                 goto out;
398         }
399
400         lock_kernel();
401
402         t = ioctl32_hash_table[ioctl32_hash (cmd)];
403
404         while (t && t->cmd != cmd)
405                 t = t->next;
406         if (t) {
407                 if (t->handler) { 
408                         error = t->handler(fd, cmd, arg, filp);
409                         unlock_kernel();
410                 } else {
411                         unlock_kernel();
412                         error = sys_ioctl(fd, cmd, arg);
413                 }
414         } else {
415                 unlock_kernel();
416                 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
417                         error = siocdevprivate_ioctl(fd, cmd, arg);
418                 } else {
419                         static int count;
420                         if (++count <= 50) {
421                                 char buf[10];
422                                 char *fn = "?";
423                                 char *path;
424
425                                 path = (char *)__get_free_page(GFP_KERNEL);
426
427                                 /* find the name of the device. */
428                                 if (path) {
429                                         fn = d_path(filp->f_dentry,
430                                                 filp->f_vfsmnt, path,
431                                                 PAGE_SIZE);
432                                 }
433
434                                 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
435                                 if (!isprint(buf[1]))
436                                     sprintf(buf, "%02x", buf[1]);
437                                 printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
438                                         "cmd(%08x){%s} arg(%08x) on %s\n",
439                                         current->comm, current->pid,
440                                         (int)fd, (unsigned int)cmd, buf,
441                                         (unsigned int)arg, fn);
442                                 if (path)
443                                         free_page((unsigned long)path);
444                         }
445                         error = -EINVAL;
446                 }
447         }
448 out:
449         fput(filp);
450 out2:
451         return error;
452 }
453
454 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
455 {
456         if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
457             __get_user(kfl->l_type, &ufl->l_type) ||
458             __get_user(kfl->l_whence, &ufl->l_whence) ||
459             __get_user(kfl->l_start, &ufl->l_start) ||
460             __get_user(kfl->l_len, &ufl->l_len) ||
461             __get_user(kfl->l_pid, &ufl->l_pid))
462                 return -EFAULT;
463         return 0;
464 }
465
466 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
467 {
468         if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
469             __put_user(kfl->l_type, &ufl->l_type) ||
470             __put_user(kfl->l_whence, &ufl->l_whence) ||
471             __put_user(kfl->l_start, &ufl->l_start) ||
472             __put_user(kfl->l_len, &ufl->l_len) ||
473             __put_user(kfl->l_pid, &ufl->l_pid))
474                 return -EFAULT;
475         return 0;
476 }
477
478 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
479 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
480 {
481         if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
482             __get_user(kfl->l_type, &ufl->l_type) ||
483             __get_user(kfl->l_whence, &ufl->l_whence) ||
484             __get_user(kfl->l_start, &ufl->l_start) ||
485             __get_user(kfl->l_len, &ufl->l_len) ||
486             __get_user(kfl->l_pid, &ufl->l_pid))
487                 return -EFAULT;
488         return 0;
489 }
490 #endif
491
492 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
493 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
494 {
495         if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
496             __put_user(kfl->l_type, &ufl->l_type) ||
497             __put_user(kfl->l_whence, &ufl->l_whence) ||
498             __put_user(kfl->l_start, &ufl->l_start) ||
499             __put_user(kfl->l_len, &ufl->l_len) ||
500             __put_user(kfl->l_pid, &ufl->l_pid))
501                 return -EFAULT;
502         return 0;
503 }
504 #endif
505
506 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
507                 unsigned long arg)
508 {
509         mm_segment_t old_fs;
510         struct flock f;
511         long ret;
512
513         switch (cmd) {
514         case F_GETLK:
515         case F_SETLK:
516         case F_SETLKW:
517                 ret = get_compat_flock(&f, compat_ptr(arg));
518                 if (ret != 0)
519                         break;
520                 old_fs = get_fs();
521                 set_fs(KERNEL_DS);
522                 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
523                 set_fs(old_fs);
524                 if ((cmd == F_GETLK) && (ret == 0)) {
525                         if ((f.l_start >= COMPAT_OFF_T_MAX) ||
526                             ((f.l_start + f.l_len) >= COMPAT_OFF_T_MAX))
527                                 ret = -EOVERFLOW;
528                         if (ret == 0)
529                                 ret = put_compat_flock(&f, compat_ptr(arg));
530                 }
531                 break;
532
533         case F_GETLK64:
534         case F_SETLK64:
535         case F_SETLKW64:
536                 ret = get_compat_flock64(&f, compat_ptr(arg));
537                 if (ret != 0)
538                         break;
539                 old_fs = get_fs();
540                 set_fs(KERNEL_DS);
541                 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
542                                 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
543                                 (unsigned long)&f);
544                 set_fs(old_fs);
545                 if ((cmd == F_GETLK64) && (ret == 0)) {
546                         if ((f.l_start >= COMPAT_LOFF_T_MAX) ||
547                             ((f.l_start + f.l_len) >= COMPAT_LOFF_T_MAX))
548                                 ret = -EOVERFLOW;
549                         if (ret == 0)
550                                 ret = put_compat_flock64(&f, compat_ptr(arg));
551                 }
552                 break;
553
554         default:
555                 ret = sys_fcntl(fd, cmd, arg);
556                 break;
557         }
558         return ret;
559 }
560
561 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
562                 unsigned long arg)
563 {
564         if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
565                 return -EINVAL;
566         return compat_sys_fcntl64(fd, cmd, arg);
567 }
568
569 asmlinkage long
570 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
571 {
572         long ret;
573         aio_context_t ctx64;
574
575         mm_segment_t oldfs = get_fs();
576         if (unlikely(get_user(ctx64, ctx32p)))
577                 return -EFAULT;
578
579         set_fs(KERNEL_DS);
580         /* The __user pointer cast is valid because of the set_fs() */
581         ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
582         set_fs(oldfs);
583         /* truncating is ok because it's a user address */
584         if (!ret)
585                 ret = put_user((u32) ctx64, ctx32p);
586         return ret;
587 }
588
589 asmlinkage long
590 compat_sys_io_getevents(aio_context_t ctx_id,
591                                  unsigned long min_nr,
592                                  unsigned long nr,
593                                  struct io_event __user *events,
594                                  struct compat_timespec __user *timeout)
595 {
596         long ret;
597         struct timespec t;
598         struct timespec __user *ut = NULL;
599
600         ret = -EFAULT;
601         if (unlikely(!access_ok(VERIFY_WRITE, events, 
602                                 nr * sizeof(struct io_event))))
603                 goto out;
604         if (timeout) {
605                 if (get_compat_timespec(&t, timeout))
606                         goto out;
607
608                 ut = compat_alloc_user_space(sizeof(*ut));
609                 if (copy_to_user(ut, &t, sizeof(t)) )
610                         goto out;
611         } 
612         ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
613 out:
614         return ret;
615 }
616
617 static inline long
618 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
619 {
620         compat_uptr_t uptr;
621         int i;
622
623         for (i = 0; i < nr; ++i) {
624                 if (get_user(uptr, ptr32 + i))
625                         return -EFAULT;
626                 if (put_user(compat_ptr(uptr), ptr64 + i))
627                         return -EFAULT;
628         }
629         return 0;
630 }
631
632 #define MAX_AIO_SUBMITS         (PAGE_SIZE/sizeof(struct iocb *))
633
634 asmlinkage long
635 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
636 {
637         struct iocb __user * __user *iocb64; 
638         long ret;
639
640         if (unlikely(nr < 0))
641                 return -EINVAL;
642
643         if (nr > MAX_AIO_SUBMITS)
644                 nr = MAX_AIO_SUBMITS;
645         
646         iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
647         ret = copy_iocb(nr, iocb, iocb64);
648         if (!ret)
649                 ret = sys_io_submit(ctx_id, nr, iocb64);
650         return ret;
651 }
652
653 struct compat_ncp_mount_data {
654         compat_int_t version;
655         compat_uint_t ncp_fd;
656         compat_uid_t mounted_uid;
657         compat_pid_t wdog_pid;
658         unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
659         compat_uint_t time_out;
660         compat_uint_t retry_count;
661         compat_uint_t flags;
662         compat_uid_t uid;
663         compat_gid_t gid;
664         compat_mode_t file_mode;
665         compat_mode_t dir_mode;
666 };
667
668 struct compat_ncp_mount_data_v4 {
669         compat_int_t version;
670         compat_ulong_t flags;
671         compat_ulong_t mounted_uid;
672         compat_long_t wdog_pid;
673         compat_uint_t ncp_fd;
674         compat_uint_t time_out;
675         compat_uint_t retry_count;
676         compat_ulong_t uid;
677         compat_ulong_t gid;
678         compat_ulong_t file_mode;
679         compat_ulong_t dir_mode;
680 };
681
682 static void *do_ncp_super_data_conv(void *raw_data)
683 {
684         int version = *(unsigned int *)raw_data;
685
686         if (version == 3) {
687                 struct compat_ncp_mount_data *c_n = raw_data;
688                 struct ncp_mount_data *n = raw_data;
689
690                 n->dir_mode = c_n->dir_mode;
691                 n->file_mode = c_n->file_mode;
692                 n->gid = c_n->gid;
693                 n->uid = c_n->uid;
694                 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
695                 n->wdog_pid = c_n->wdog_pid;
696                 n->mounted_uid = c_n->mounted_uid;
697         } else if (version == 4) {
698                 struct compat_ncp_mount_data_v4 *c_n = raw_data;
699                 struct ncp_mount_data_v4 *n = raw_data;
700
701                 n->dir_mode = c_n->dir_mode;
702                 n->file_mode = c_n->file_mode;
703                 n->gid = c_n->gid;
704                 n->uid = c_n->uid;
705                 n->retry_count = c_n->retry_count;
706                 n->time_out = c_n->time_out;
707                 n->ncp_fd = c_n->ncp_fd;
708                 n->wdog_pid = c_n->wdog_pid;
709                 n->mounted_uid = c_n->mounted_uid;
710                 n->flags = c_n->flags;
711         } else if (version != 5) {
712                 return NULL;
713         }
714
715         return raw_data;
716 }
717
718 struct compat_smb_mount_data {
719         compat_int_t version;
720         compat_uid_t mounted_uid;
721         compat_uid_t uid;
722         compat_gid_t gid;
723         compat_mode_t file_mode;
724         compat_mode_t dir_mode;
725 };
726
727 static void *do_smb_super_data_conv(void *raw_data)
728 {
729         struct smb_mount_data *s = raw_data;
730         struct compat_smb_mount_data *c_s = raw_data;
731
732         if (c_s->version != SMB_MOUNT_OLDVERSION)
733                 goto out;
734         s->dir_mode = c_s->dir_mode;
735         s->file_mode = c_s->file_mode;
736         s->gid = c_s->gid;
737         s->uid = c_s->uid;
738         s->mounted_uid = c_s->mounted_uid;
739  out:
740         return raw_data;
741 }
742
743 extern int copy_mount_options (const void __user *, unsigned long *);
744
745 #define SMBFS_NAME      "smbfs"
746 #define NCPFS_NAME      "ncpfs"
747
748 asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
749                                  char __user * type, unsigned long flags,
750                                  void __user * data)
751 {
752         unsigned long type_page;
753         unsigned long data_page;
754         unsigned long dev_page;
755         char *dir_page;
756         int retval;
757
758         retval = copy_mount_options (type, &type_page);
759         if (retval < 0)
760                 goto out;
761
762         dir_page = getname(dir_name);
763         retval = PTR_ERR(dir_page);
764         if (IS_ERR(dir_page))
765                 goto out1;
766
767         retval = copy_mount_options (dev_name, &dev_page);
768         if (retval < 0)
769                 goto out2;
770
771         retval = copy_mount_options (data, &data_page);
772         if (retval < 0)
773                 goto out3;
774
775         retval = -EINVAL;
776
777         if (type_page) {
778                 if (!strcmp((char *)type_page, SMBFS_NAME)) {
779                         do_smb_super_data_conv((void *)data_page);
780                 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
781                         do_ncp_super_data_conv((void *)data_page);
782                 }
783         }
784
785         lock_kernel();
786         retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
787                         flags, (void*)data_page);
788         unlock_kernel();
789
790         free_page(data_page);
791  out3:
792         free_page(dev_page);
793  out2:
794         putname(dir_page);
795  out1:
796         free_page(type_page);
797  out:
798         return retval;
799 }
800
801 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
802 #define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
803                                 ~(sizeof(compat_long_t)-1))
804
805 struct compat_old_linux_dirent {
806         compat_ulong_t  d_ino;
807         compat_ulong_t  d_offset;
808         unsigned short  d_namlen;
809         char            d_name[1];
810 };
811
812 struct compat_readdir_callback {
813         struct compat_old_linux_dirent __user *dirent;
814         int result;
815 };
816
817 static int compat_fillonedir(void *__buf, const char *name, int namlen,
818                         loff_t offset, ino_t ino, unsigned int d_type)
819 {
820         struct compat_readdir_callback *buf = __buf;
821         struct compat_old_linux_dirent __user *dirent;
822
823         if (buf->result)
824                 return -EINVAL;
825         buf->result++;
826         dirent = buf->dirent;
827         if (!access_ok(VERIFY_WRITE, dirent,
828                         (unsigned long)(dirent->d_name + namlen + 1) -
829                                 (unsigned long)dirent))
830                 goto efault;
831         if (    __put_user(ino, &dirent->d_ino) ||
832                 __put_user(offset, &dirent->d_offset) ||
833                 __put_user(namlen, &dirent->d_namlen) ||
834                 __copy_to_user(dirent->d_name, name, namlen) ||
835                 __put_user(0, dirent->d_name + namlen))
836                 goto efault;
837         return 0;
838 efault:
839         buf->result = -EFAULT;
840         return -EFAULT;
841 }
842
843 asmlinkage long compat_old_readdir(unsigned int fd,
844         struct compat_old_linux_dirent __user *dirent, unsigned int count)
845 {
846         int error;
847         struct file *file;
848         struct compat_readdir_callback buf;
849
850         error = -EBADF;
851         file = fget(fd);
852         if (!file)
853                 goto out;
854
855         buf.result = 0;
856         buf.dirent = dirent;
857
858         error = vfs_readdir(file, compat_fillonedir, &buf);
859         if (error >= 0)
860                 error = buf.result;
861
862         fput(file);
863 out:
864         return error;
865 }
866
867 struct compat_linux_dirent {
868         compat_ulong_t  d_ino;
869         compat_ulong_t  d_off;
870         unsigned short  d_reclen;
871         char            d_name[1];
872 };
873
874 struct compat_getdents_callback {
875         struct compat_linux_dirent __user *current_dir;
876         struct compat_linux_dirent __user *previous;
877         int count;
878         int error;
879 };
880
881 static int compat_filldir(void *__buf, const char *name, int namlen,
882                 loff_t offset, ino_t ino, unsigned int d_type)
883 {
884         struct compat_linux_dirent __user * dirent;
885         struct compat_getdents_callback *buf = __buf;
886         int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
887
888         buf->error = -EINVAL;   /* only used if we fail.. */
889         if (reclen > buf->count)
890                 return -EINVAL;
891         dirent = buf->previous;
892         if (dirent) {
893                 if (__put_user(offset, &dirent->d_off))
894                         goto efault;
895         }
896         dirent = buf->current_dir;
897         if (__put_user(ino, &dirent->d_ino))
898                 goto efault;
899         if (__put_user(reclen, &dirent->d_reclen))
900                 goto efault;
901         if (copy_to_user(dirent->d_name, name, namlen))
902                 goto efault;
903         if (__put_user(0, dirent->d_name + namlen))
904                 goto efault;
905         if (__put_user(d_type, (char  __user *) dirent + reclen - 1))
906                 goto efault;
907         buf->previous = dirent;
908         dirent = (void __user *)dirent + reclen;
909         buf->current_dir = dirent;
910         buf->count -= reclen;
911         return 0;
912 efault:
913         buf->error = -EFAULT;
914         return -EFAULT;
915 }
916
917 asmlinkage long compat_sys_getdents(unsigned int fd,
918                 struct compat_linux_dirent __user *dirent, unsigned int count)
919 {
920         struct file * file;
921         struct compat_linux_dirent __user * lastdirent;
922         struct compat_getdents_callback buf;
923         int error;
924
925         error = -EFAULT;
926         if (!access_ok(VERIFY_WRITE, dirent, count))
927                 goto out;
928
929         error = -EBADF;
930         file = fget(fd);
931         if (!file)
932                 goto out;
933
934         buf.current_dir = dirent;
935         buf.previous = NULL;
936         buf.count = count;
937         buf.error = 0;
938
939         error = vfs_readdir(file, compat_filldir, &buf);
940         if (error < 0)
941                 goto out_putf;
942         error = buf.error;
943         lastdirent = buf.previous;
944         if (lastdirent) {
945                 if (put_user(file->f_pos, &lastdirent->d_off))
946                         error = -EFAULT;
947                 else
948                         error = count - buf.count;
949         }
950
951 out_putf:
952         fput(file);
953 out:
954         return error;
955 }
956
957 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
958 #define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
959
960 struct compat_getdents_callback64 {
961         struct linux_dirent64 __user *current_dir;
962         struct linux_dirent64 __user *previous;
963         int count;
964         int error;
965 };
966
967 static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
968                      ino_t ino, unsigned int d_type)
969 {
970         struct linux_dirent64 __user *dirent;
971         struct compat_getdents_callback64 *buf = __buf;
972         int jj = NAME_OFFSET(dirent);
973         int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
974         u64 off;
975
976         buf->error = -EINVAL;   /* only used if we fail.. */
977         if (reclen > buf->count)
978                 return -EINVAL;
979         dirent = buf->previous;
980
981         if (dirent) {
982                 if (__put_user(offset, (u32 __user *)&dirent->d_off))
983                         goto efault;
984                 if (__put_user(offset >> 32,
985                                 ((u32 __user *)&dirent->d_off) + 1))
986                         goto efault;
987         }
988         dirent = buf->current_dir;
989         if ((__put_user(ino, (u32 __user *)&dirent->d_ino))
990          || (__put_user(ino >> 32, ((u32 __user *)&dirent->d_ino) + 1)))
991                 goto efault;
992         off = 0;
993         if ((__put_user(off, (u32 __user *)&dirent->d_off))
994          || (__put_user(off >> 32, ((u32 __user *)&dirent->d_off) + 1)))
995                 goto efault;
996         if (__put_user(reclen, &dirent->d_reclen))
997                 goto efault;
998         if (__put_user(d_type, &dirent->d_type))
999                 goto efault;
1000         if (copy_to_user(dirent->d_name, name, namlen))
1001                 goto efault;
1002         if (__put_user(0, dirent->d_name + namlen))
1003                 goto efault;
1004         buf->previous = dirent;
1005         dirent = (void __user *)dirent + reclen;
1006         buf->current_dir = dirent;
1007         buf->count -= reclen;
1008         return 0;
1009 efault:
1010         buf->error = -EFAULT;
1011         return -EFAULT;
1012 }
1013
1014 asmlinkage long compat_sys_getdents64(unsigned int fd,
1015                 struct linux_dirent64 __user * dirent, unsigned int count)
1016 {
1017         struct file * file;
1018         struct linux_dirent64 __user * lastdirent;
1019         struct compat_getdents_callback64 buf;
1020         int error;
1021
1022         error = -EFAULT;
1023         if (!access_ok(VERIFY_WRITE, dirent, count))
1024                 goto out;
1025
1026         error = -EBADF;
1027         file = fget(fd);
1028         if (!file)
1029                 goto out;
1030
1031         buf.current_dir = dirent;
1032         buf.previous = NULL;
1033         buf.count = count;
1034         buf.error = 0;
1035
1036         error = vfs_readdir(file, compat_filldir64, &buf);
1037         if (error < 0)
1038                 goto out_putf;
1039         error = buf.error;
1040         lastdirent = buf.previous;
1041         if (lastdirent) {
1042                 typeof(lastdirent->d_off) d_off = file->f_pos;
1043                 __put_user(d_off, (u32 __user *)&lastdirent->d_off);
1044                 __put_user(d_off >> 32, ((u32 __user *)&lastdirent->d_off) + 1);
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         sched_balance_exec();
1377
1378         file = open_exec(filename);
1379
1380         retval = PTR_ERR(file);
1381         if (IS_ERR(file))
1382                 return retval;
1383
1384         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1385         memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1386
1387         bprm.file = file;
1388         bprm.filename = filename;
1389         bprm.interp = filename;
1390         bprm.sh_bang = 0;
1391         bprm.loader = 0;
1392         bprm.exec = 0;
1393         bprm.security = NULL;
1394         bprm.mm = mm_alloc();
1395         retval = -ENOMEM;
1396         if (!bprm.mm)
1397                 goto out_file;
1398
1399         retval = init_new_context(current, bprm.mm);
1400         if (retval < 0)
1401                 goto out_mm;
1402
1403         bprm.argc = compat_count(argv, bprm.p / sizeof(compat_uptr_t));
1404         if ((retval = bprm.argc) < 0)
1405                 goto out_mm;
1406
1407         bprm.envc = compat_count(envp, bprm.p / sizeof(compat_uptr_t));
1408         if ((retval = bprm.envc) < 0)
1409                 goto out_mm;
1410
1411         retval = security_bprm_alloc(&bprm);
1412         if (retval)
1413                 goto out;
1414
1415         retval = prepare_binprm(&bprm);
1416         if (retval < 0)
1417                 goto out;
1418
1419         retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1420         if (retval < 0)
1421                 goto out;
1422
1423         bprm.exec = bprm.p;
1424         retval = compat_copy_strings(bprm.envc, envp, &bprm);
1425         if (retval < 0)
1426                 goto out;
1427
1428         retval = compat_copy_strings(bprm.argc, argv, &bprm);
1429         if (retval < 0)
1430                 goto out;
1431
1432         retval = search_binary_handler(&bprm,regs);
1433         if (retval >= 0) {
1434                 free_arg_pages(&bprm);
1435
1436                 /* execve success */
1437                 security_bprm_free(&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
1462         return retval;
1463 }
1464
1465 #define __COMPAT_NFDBITS       (8 * sizeof(compat_ulong_t))
1466
1467 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1468
1469 /*
1470  * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
1471  * 64-bit unsigned longs.
1472  */
1473 static inline
1474 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1475                         unsigned long *fdset)
1476 {
1477         nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1478         if (ufdset) {
1479                 unsigned long odd;
1480
1481                 if (verify_area(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1482                         return -EFAULT;
1483
1484                 odd = nr & 1UL;
1485                 nr &= ~1UL;
1486                 while (nr) {
1487                         unsigned long h, l;
1488                         __get_user(l, ufdset);
1489                         __get_user(h, ufdset+1);
1490                         ufdset += 2;
1491                         *fdset++ = h << 32 | l;
1492                         nr -= 2;
1493                 }
1494                 if (odd)
1495                         __get_user(*fdset, ufdset);
1496         } else {
1497                 /* Tricky, must clear full unsigned long in the
1498                  * kernel fdset at the end, this makes sure that
1499                  * actually happens.
1500                  */
1501                 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1502         }
1503         return 0;
1504 }
1505
1506 static inline
1507 void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1508                         unsigned long *fdset)
1509 {
1510         unsigned long odd;
1511         nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1512
1513         if (!ufdset)
1514                 return;
1515
1516         odd = nr & 1UL;
1517         nr &= ~1UL;
1518         while (nr) {
1519                 unsigned long h, l;
1520                 l = *fdset++;
1521                 h = l >> 32;
1522                 __put_user(l, ufdset);
1523                 __put_user(h, ufdset+1);
1524                 ufdset += 2;
1525                 nr -= 2;
1526         }
1527         if (odd)
1528                 __put_user(*fdset, ufdset);
1529 }
1530
1531
1532 /*
1533  * This is a virtual copy of sys_select from fs/select.c and probably
1534  * should be compared to it from time to time
1535  */
1536 static void *select_bits_alloc(int size)
1537 {
1538         return kmalloc(6 * size, GFP_KERNEL);
1539 }
1540
1541 static void select_bits_free(void *bits, int size)
1542 {
1543         kfree(bits);
1544 }
1545
1546 /*
1547  * We can actually return ERESTARTSYS instead of EINTR, but I'd
1548  * like to be certain this leads to no problems. So I return
1549  * EINTR just for safety.
1550  *
1551  * Update: ERESTARTSYS breaks at least the xview clock binary, so
1552  * I'm trying ERESTARTNOHAND which restart only when you want to.
1553  */
1554 #define MAX_SELECT_SECONDS \
1555         ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1556
1557 asmlinkage long
1558 compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp,
1559                 compat_ulong_t __user *exp, struct compat_timeval __user *tvp)
1560 {
1561         fd_set_bits fds;
1562         char *bits;
1563         long timeout;
1564         int ret, size, max_fdset;
1565
1566         timeout = MAX_SCHEDULE_TIMEOUT;
1567         if (tvp) {
1568                 time_t sec, usec;
1569
1570                 if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp)))
1571                     || (ret = __get_user(sec, &tvp->tv_sec))
1572                     || (ret = __get_user(usec, &tvp->tv_usec)))
1573                         goto out_nofds;
1574
1575                 ret = -EINVAL;
1576                 if (sec < 0 || usec < 0)
1577                         goto out_nofds;
1578
1579                 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
1580                         timeout = ROUND_UP(usec, 1000000/HZ);
1581                         timeout += sec * (unsigned long) HZ;
1582                 }
1583         }
1584
1585         ret = -EINVAL;
1586         if (n < 0)
1587                 goto out_nofds;
1588
1589         /* max_fdset can increase, so grab it once to avoid race */
1590         max_fdset = current->files->max_fdset;
1591         if (n > max_fdset)
1592                 n = max_fdset;
1593
1594         /*
1595          * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1596          * since we used fdset we need to allocate memory in units of
1597          * long-words.
1598          */
1599         ret = -ENOMEM;
1600         size = FDS_BYTES(n);
1601         bits = select_bits_alloc(size);
1602         if (!bits)
1603                 goto out_nofds;
1604         fds.in      = (unsigned long *)  bits;
1605         fds.out     = (unsigned long *) (bits +   size);
1606         fds.ex      = (unsigned long *) (bits + 2*size);
1607         fds.res_in  = (unsigned long *) (bits + 3*size);
1608         fds.res_out = (unsigned long *) (bits + 4*size);
1609         fds.res_ex  = (unsigned long *) (bits + 5*size);
1610
1611         if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1612             (ret = compat_get_fd_set(n, outp, fds.out)) ||
1613             (ret = compat_get_fd_set(n, exp, fds.ex)))
1614                 goto out;
1615         zero_fd_set(n, fds.res_in);
1616         zero_fd_set(n, fds.res_out);
1617         zero_fd_set(n, fds.res_ex);
1618
1619         ret = do_select(n, &fds, &timeout);
1620
1621         if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
1622                 time_t sec = 0, usec = 0;
1623                 if (timeout) {
1624                         sec = timeout / HZ;
1625                         usec = timeout % HZ;
1626                         usec *= (1000000/HZ);
1627                 }
1628                 if (put_user(sec, &tvp->tv_sec) ||
1629                     put_user(usec, &tvp->tv_usec))
1630                         ret = -EFAULT;
1631         }
1632
1633         if (ret < 0)
1634                 goto out;
1635         if (!ret) {
1636                 ret = -ERESTARTNOHAND;
1637                 if (signal_pending(current))
1638                         goto out;
1639                 ret = 0;
1640         }
1641
1642         compat_set_fd_set(n, inp, fds.res_in);
1643         compat_set_fd_set(n, outp, fds.res_out);
1644         compat_set_fd_set(n, exp, fds.res_ex);
1645
1646 out:
1647         select_bits_free(bits, size);
1648 out_nofds:
1649         return ret;
1650 }
1651
1652 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1653 /* Stuff for NFS server syscalls... */
1654 struct compat_nfsctl_svc {
1655         u16                     svc32_port;
1656         s32                     svc32_nthreads;
1657 };
1658
1659 struct compat_nfsctl_client {
1660         s8                      cl32_ident[NFSCLNT_IDMAX+1];
1661         s32                     cl32_naddr;
1662         struct in_addr          cl32_addrlist[NFSCLNT_ADDRMAX];
1663         s32                     cl32_fhkeytype;
1664         s32                     cl32_fhkeylen;
1665         u8                      cl32_fhkey[NFSCLNT_KEYMAX];
1666 };
1667
1668 struct compat_nfsctl_export {
1669         char            ex32_client[NFSCLNT_IDMAX+1];
1670         char            ex32_path[NFS_MAXPATHLEN+1];
1671         compat_dev_t    ex32_dev;
1672         compat_ino_t    ex32_ino;
1673         compat_int_t    ex32_flags;
1674         compat_uid_t    ex32_anon_uid;
1675         compat_gid_t    ex32_anon_gid;
1676 };
1677
1678 struct compat_nfsctl_fdparm {
1679         struct sockaddr         gd32_addr;
1680         s8                      gd32_path[NFS_MAXPATHLEN+1];
1681         compat_int_t            gd32_version;
1682 };
1683
1684 struct compat_nfsctl_fsparm {
1685         struct sockaddr         gd32_addr;
1686         s8                      gd32_path[NFS_MAXPATHLEN+1];
1687         compat_int_t            gd32_maxlen;
1688 };
1689
1690 struct compat_nfsctl_arg {
1691         compat_int_t            ca32_version;   /* safeguard */
1692         union {
1693                 struct compat_nfsctl_svc        u32_svc;
1694                 struct compat_nfsctl_client     u32_client;
1695                 struct compat_nfsctl_export     u32_export;
1696                 struct compat_nfsctl_fdparm     u32_getfd;
1697                 struct compat_nfsctl_fsparm     u32_getfs;
1698         } u;
1699 #define ca32_svc        u.u32_svc
1700 #define ca32_client     u.u32_client
1701 #define ca32_export     u.u32_export
1702 #define ca32_getfd      u.u32_getfd
1703 #define ca32_getfs      u.u32_getfs
1704 };
1705
1706 union compat_nfsctl_res {
1707         __u8                    cr32_getfh[NFS_FHSIZE];
1708         struct knfsd_fh         cr32_getfs;
1709 };
1710
1711 static int compat_nfs_svc_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1712 {
1713         int err;
1714
1715         err = access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc));
1716         err |= get_user(karg->ca_version, &arg->ca32_version);
1717         err |= __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port);
1718         err |= __get_user(karg->ca_svc.svc_nthreads, &arg->ca32_svc.svc32_nthreads);
1719         return (err) ? -EFAULT : 0;
1720 }
1721
1722 static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1723 {
1724         int err;
1725
1726         err = access_ok(VERIFY_READ, &arg->ca32_client, sizeof(arg->ca32_client));
1727         err |= get_user(karg->ca_version, &arg->ca32_version);
1728         err |= __copy_from_user(&karg->ca_client.cl_ident[0],
1729                           &arg->ca32_client.cl32_ident[0],
1730                           NFSCLNT_IDMAX);
1731         err |= __get_user(karg->ca_client.cl_naddr, &arg->ca32_client.cl32_naddr);
1732         err |= __copy_from_user(&karg->ca_client.cl_addrlist[0],
1733                           &arg->ca32_client.cl32_addrlist[0],
1734                           (sizeof(struct in_addr) * NFSCLNT_ADDRMAX));
1735         err |= __get_user(karg->ca_client.cl_fhkeytype,
1736                       &arg->ca32_client.cl32_fhkeytype);
1737         err |= __get_user(karg->ca_client.cl_fhkeylen,
1738                       &arg->ca32_client.cl32_fhkeylen);
1739         err |= __copy_from_user(&karg->ca_client.cl_fhkey[0],
1740                           &arg->ca32_client.cl32_fhkey[0],
1741                           NFSCLNT_KEYMAX);
1742
1743         return (err) ? -EFAULT : 0;
1744 }
1745
1746 static int compat_nfs_exp_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1747 {
1748         int err;
1749
1750         err = access_ok(VERIFY_READ, &arg->ca32_export, sizeof(arg->ca32_export));
1751         err |= get_user(karg->ca_version, &arg->ca32_version);
1752         err |= __copy_from_user(&karg->ca_export.ex_client[0],
1753                           &arg->ca32_export.ex32_client[0],
1754                           NFSCLNT_IDMAX);
1755         err |= __copy_from_user(&karg->ca_export.ex_path[0],
1756                           &arg->ca32_export.ex32_path[0],
1757                           NFS_MAXPATHLEN);
1758         err |= __get_user(karg->ca_export.ex_dev,
1759                       &arg->ca32_export.ex32_dev);
1760         err |= __get_user(karg->ca_export.ex_ino,
1761                       &arg->ca32_export.ex32_ino);
1762         err |= __get_user(karg->ca_export.ex_flags,
1763                       &arg->ca32_export.ex32_flags);
1764         err |= __get_user(karg->ca_export.ex_anon_uid,
1765                       &arg->ca32_export.ex32_anon_uid);
1766         err |= __get_user(karg->ca_export.ex_anon_gid,
1767                       &arg->ca32_export.ex32_anon_gid);
1768         SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
1769         SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
1770
1771         return (err) ? -EFAULT : 0;
1772 }
1773
1774 static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1775 {
1776         int err;
1777
1778         err = access_ok(VERIFY_READ, &arg->ca32_getfd, sizeof(arg->ca32_getfd));
1779         err |= get_user(karg->ca_version, &arg->ca32_version);
1780         err |= __copy_from_user(&karg->ca_getfd.gd_addr,
1781                           &arg->ca32_getfd.gd32_addr,
1782                           (sizeof(struct sockaddr)));
1783         err |= __copy_from_user(&karg->ca_getfd.gd_path,
1784                           &arg->ca32_getfd.gd32_path,
1785                           (NFS_MAXPATHLEN+1));
1786         err |= __get_user(karg->ca_getfd.gd_version,
1787                       &arg->ca32_getfd.gd32_version);
1788
1789         return (err) ? -EFAULT : 0;
1790 }
1791
1792 static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1793 {
1794         int err;
1795
1796         err = access_ok(VERIFY_READ, &arg->ca32_getfs, sizeof(arg->ca32_getfs));
1797         err |= get_user(karg->ca_version, &arg->ca32_version);
1798         err |= __copy_from_user(&karg->ca_getfs.gd_addr,
1799                           &arg->ca32_getfs.gd32_addr,
1800                           (sizeof(struct sockaddr)));
1801         err |= __copy_from_user(&karg->ca_getfs.gd_path,
1802                           &arg->ca32_getfs.gd32_path,
1803                           (NFS_MAXPATHLEN+1));
1804         err |= __get_user(karg->ca_getfs.gd_maxlen,
1805                       &arg->ca32_getfs.gd32_maxlen);
1806
1807         return (err) ? -EFAULT : 0;
1808 }
1809
1810 /* This really doesn't need translations, we are only passing
1811  * back a union which contains opaque nfs file handle data.
1812  */
1813 static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, union compat_nfsctl_res __user *res)
1814 {
1815         int err;
1816
1817         err = copy_to_user(res, kres, sizeof(*res));
1818
1819         return (err) ? -EFAULT : 0;
1820 }
1821
1822 asmlinkage long compat_sys_nfsservctl(int cmd, struct compat_nfsctl_arg __user *arg,
1823                                         union compat_nfsctl_res __user *res)
1824 {
1825         struct nfsctl_arg *karg;
1826         union nfsctl_res *kres;
1827         mm_segment_t oldfs;
1828         int err;
1829
1830         karg = kmalloc(sizeof(*karg), GFP_USER);
1831         kres = kmalloc(sizeof(*kres), GFP_USER);
1832         if(!karg || !kres) {
1833                 err = -ENOMEM;
1834                 goto done;
1835         }
1836
1837         switch(cmd) {
1838         case NFSCTL_SVC:
1839                 err = compat_nfs_svc_trans(karg, arg);
1840                 break;
1841
1842         case NFSCTL_ADDCLIENT:
1843                 err = compat_nfs_clnt_trans(karg, arg);
1844                 break;
1845
1846         case NFSCTL_DELCLIENT:
1847                 err = compat_nfs_clnt_trans(karg, arg);
1848                 break;
1849
1850         case NFSCTL_EXPORT:
1851         case NFSCTL_UNEXPORT:
1852                 err = compat_nfs_exp_trans(karg, arg);
1853                 break;
1854
1855         case NFSCTL_GETFD:
1856                 err = compat_nfs_getfd_trans(karg, arg);
1857                 break;
1858
1859         case NFSCTL_GETFS:
1860                 err = compat_nfs_getfs_trans(karg, arg);
1861                 break;
1862
1863         default:
1864                 err = -EINVAL;
1865                 goto done;
1866         }
1867
1868         oldfs = get_fs();
1869         set_fs(KERNEL_DS);
1870         /* The __user pointer casts are valid because of the set_fs() */
1871         err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
1872         set_fs(oldfs);
1873
1874         if (err)
1875                 goto done;
1876
1877         if((cmd == NFSCTL_GETFD) ||
1878            (cmd == NFSCTL_GETFS))
1879                 err = compat_nfs_getfh_res_trans(kres, res);
1880
1881 done:
1882         kfree(karg);
1883         kfree(kres);
1884         return err;
1885 }
1886 #else /* !NFSD */
1887 long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
1888 {
1889         return sys_ni_syscall();
1890 }
1891 #endif