switch epoll_pwait to COMPAT_SYSCALL_DEFINE
[linux-2.6-block.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@ucw.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/stddef.h>
19 #include <linux/kernel.h>
20 #include <linux/linkage.h>
21 #include <linux/compat.h>
22 #include <linux/errno.h>
23 #include <linux/time.h>
24 #include <linux/fs.h>
25 #include <linux/fcntl.h>
26 #include <linux/namei.h>
27 #include <linux/file.h>
28 #include <linux/fdtable.h>
29 #include <linux/vfs.h>
30 #include <linux/ioctl.h>
31 #include <linux/init.h>
32 #include <linux/ncp_mount.h>
33 #include <linux/nfs4_mount.h>
34 #include <linux/syscalls.h>
35 #include <linux/ctype.h>
36 #include <linux/dirent.h>
37 #include <linux/fsnotify.h>
38 #include <linux/highuid.h>
39 #include <linux/personality.h>
40 #include <linux/rwsem.h>
41 #include <linux/tsacct_kern.h>
42 #include <linux/security.h>
43 #include <linux/highmem.h>
44 #include <linux/signal.h>
45 #include <linux/poll.h>
46 #include <linux/mm.h>
47 #include <linux/fs_struct.h>
48 #include <linux/slab.h>
49 #include <linux/pagemap.h>
50
51 #include <asm/uaccess.h>
52 #include <asm/mmu_context.h>
53 #include <asm/ioctls.h>
54 #include "internal.h"
55
56 int compat_log = 1;
57
58 int compat_printk(const char *fmt, ...)
59 {
60         va_list ap;
61         int ret;
62         if (!compat_log)
63                 return 0;
64         va_start(ap, fmt);
65         ret = vprintk(fmt, ap);
66         va_end(ap);
67         return ret;
68 }
69
70 #include "read_write.h"
71
72 /*
73  * Not all architectures have sys_utime, so implement this in terms
74  * of sys_utimes.
75  */
76 asmlinkage long compat_sys_utime(const char __user *filename,
77                                  struct compat_utimbuf __user *t)
78 {
79         struct timespec tv[2];
80
81         if (t) {
82                 if (get_user(tv[0].tv_sec, &t->actime) ||
83                     get_user(tv[1].tv_sec, &t->modtime))
84                         return -EFAULT;
85                 tv[0].tv_nsec = 0;
86                 tv[1].tv_nsec = 0;
87         }
88         return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
89 }
90
91 asmlinkage long compat_sys_utimensat(unsigned int dfd, const char __user *filename, struct compat_timespec __user *t, int flags)
92 {
93         struct timespec tv[2];
94
95         if  (t) {
96                 if (get_compat_timespec(&tv[0], &t[0]) ||
97                     get_compat_timespec(&tv[1], &t[1]))
98                         return -EFAULT;
99
100                 if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
101                         return 0;
102         }
103         return do_utimes(dfd, filename, t ? tv : NULL, flags);
104 }
105
106 asmlinkage long compat_sys_futimesat(unsigned int dfd, const char __user *filename, struct compat_timeval __user *t)
107 {
108         struct timespec tv[2];
109
110         if (t) {
111                 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
112                     get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
113                     get_user(tv[1].tv_sec, &t[1].tv_sec) ||
114                     get_user(tv[1].tv_nsec, &t[1].tv_usec))
115                         return -EFAULT;
116                 if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
117                     tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
118                         return -EINVAL;
119                 tv[0].tv_nsec *= 1000;
120                 tv[1].tv_nsec *= 1000;
121         }
122         return do_utimes(dfd, filename, t ? tv : NULL, 0);
123 }
124
125 asmlinkage long compat_sys_utimes(const char __user *filename, struct compat_timeval __user *t)
126 {
127         return compat_sys_futimesat(AT_FDCWD, filename, t);
128 }
129
130 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
131 {
132         struct compat_stat tmp;
133
134         if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
135                 return -EOVERFLOW;
136
137         memset(&tmp, 0, sizeof(tmp));
138         tmp.st_dev = old_encode_dev(stat->dev);
139         tmp.st_ino = stat->ino;
140         if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
141                 return -EOVERFLOW;
142         tmp.st_mode = stat->mode;
143         tmp.st_nlink = stat->nlink;
144         if (tmp.st_nlink != stat->nlink)
145                 return -EOVERFLOW;
146         SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
147         SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
148         tmp.st_rdev = old_encode_dev(stat->rdev);
149         if ((u64) stat->size > MAX_NON_LFS)
150                 return -EOVERFLOW;
151         tmp.st_size = stat->size;
152         tmp.st_atime = stat->atime.tv_sec;
153         tmp.st_atime_nsec = stat->atime.tv_nsec;
154         tmp.st_mtime = stat->mtime.tv_sec;
155         tmp.st_mtime_nsec = stat->mtime.tv_nsec;
156         tmp.st_ctime = stat->ctime.tv_sec;
157         tmp.st_ctime_nsec = stat->ctime.tv_nsec;
158         tmp.st_blocks = stat->blocks;
159         tmp.st_blksize = stat->blksize;
160         return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
161 }
162
163 asmlinkage long compat_sys_newstat(const char __user * filename,
164                 struct compat_stat __user *statbuf)
165 {
166         struct kstat stat;
167         int error;
168
169         error = vfs_stat(filename, &stat);
170         if (error)
171                 return error;
172         return cp_compat_stat(&stat, statbuf);
173 }
174
175 asmlinkage long compat_sys_newlstat(const char __user * filename,
176                 struct compat_stat __user *statbuf)
177 {
178         struct kstat stat;
179         int error;
180
181         error = vfs_lstat(filename, &stat);
182         if (error)
183                 return error;
184         return cp_compat_stat(&stat, statbuf);
185 }
186
187 #ifndef __ARCH_WANT_STAT64
188 asmlinkage long compat_sys_newfstatat(unsigned int dfd,
189                 const char __user *filename,
190                 struct compat_stat __user *statbuf, int flag)
191 {
192         struct kstat stat;
193         int error;
194
195         error = vfs_fstatat(dfd, filename, &stat, flag);
196         if (error)
197                 return error;
198         return cp_compat_stat(&stat, statbuf);
199 }
200 #endif
201
202 asmlinkage long compat_sys_newfstat(unsigned int fd,
203                 struct compat_stat __user * statbuf)
204 {
205         struct kstat stat;
206         int error = vfs_fstat(fd, &stat);
207
208         if (!error)
209                 error = cp_compat_stat(&stat, statbuf);
210         return error;
211 }
212
213 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
214 {
215         
216         if (sizeof ubuf->f_blocks == 4) {
217                 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
218                      kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
219                         return -EOVERFLOW;
220                 /* f_files and f_ffree may be -1; it's okay
221                  * to stuff that into 32 bits */
222                 if (kbuf->f_files != 0xffffffffffffffffULL
223                  && (kbuf->f_files & 0xffffffff00000000ULL))
224                         return -EOVERFLOW;
225                 if (kbuf->f_ffree != 0xffffffffffffffffULL
226                  && (kbuf->f_ffree & 0xffffffff00000000ULL))
227                         return -EOVERFLOW;
228         }
229         if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
230             __put_user(kbuf->f_type, &ubuf->f_type) ||
231             __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
232             __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
233             __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
234             __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
235             __put_user(kbuf->f_files, &ubuf->f_files) ||
236             __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
237             __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
238             __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
239             __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
240             __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
241             __put_user(kbuf->f_flags, &ubuf->f_flags) ||
242             __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
243                 return -EFAULT;
244         return 0;
245 }
246
247 /*
248  * The following statfs calls are copies of code from fs/statfs.c and
249  * should be checked against those from time to time
250  */
251 asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf)
252 {
253         struct kstatfs tmp;
254         int error = user_statfs(pathname, &tmp);
255         if (!error)
256                 error = put_compat_statfs(buf, &tmp);
257         return error;
258 }
259
260 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
261 {
262         struct kstatfs tmp;
263         int error = fd_statfs(fd, &tmp);
264         if (!error)
265                 error = put_compat_statfs(buf, &tmp);
266         return error;
267 }
268
269 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
270 {
271         if (sizeof ubuf->f_blocks == 4) {
272                 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
273                      kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
274                         return -EOVERFLOW;
275                 /* f_files and f_ffree may be -1; it's okay
276                  * to stuff that into 32 bits */
277                 if (kbuf->f_files != 0xffffffffffffffffULL
278                  && (kbuf->f_files & 0xffffffff00000000ULL))
279                         return -EOVERFLOW;
280                 if (kbuf->f_ffree != 0xffffffffffffffffULL
281                  && (kbuf->f_ffree & 0xffffffff00000000ULL))
282                         return -EOVERFLOW;
283         }
284         if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
285             __put_user(kbuf->f_type, &ubuf->f_type) ||
286             __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
287             __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
288             __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
289             __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
290             __put_user(kbuf->f_files, &ubuf->f_files) ||
291             __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
292             __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
293             __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
294             __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
295             __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
296             __put_user(kbuf->f_flags, &ubuf->f_flags) ||
297             __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
298                 return -EFAULT;
299         return 0;
300 }
301
302 asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf)
303 {
304         struct kstatfs tmp;
305         int error;
306
307         if (sz != sizeof(*buf))
308                 return -EINVAL;
309
310         error = user_statfs(pathname, &tmp);
311         if (!error)
312                 error = put_compat_statfs64(buf, &tmp);
313         return error;
314 }
315
316 asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
317 {
318         struct kstatfs tmp;
319         int error;
320
321         if (sz != sizeof(*buf))
322                 return -EINVAL;
323
324         error = fd_statfs(fd, &tmp);
325         if (!error)
326                 error = put_compat_statfs64(buf, &tmp);
327         return error;
328 }
329
330 /*
331  * This is a copy of sys_ustat, just dealing with a structure layout.
332  * Given how simple this syscall is that apporach is more maintainable
333  * than the various conversion hacks.
334  */
335 asmlinkage long compat_sys_ustat(unsigned dev, struct compat_ustat __user *u)
336 {
337         struct compat_ustat tmp;
338         struct kstatfs sbuf;
339         int err = vfs_ustat(new_decode_dev(dev), &sbuf);
340         if (err)
341                 return err;
342
343         memset(&tmp, 0, sizeof(struct compat_ustat));
344         tmp.f_tfree = sbuf.f_bfree;
345         tmp.f_tinode = sbuf.f_ffree;
346         if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
347                 return -EFAULT;
348         return 0;
349 }
350
351 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
352 {
353         if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
354             __get_user(kfl->l_type, &ufl->l_type) ||
355             __get_user(kfl->l_whence, &ufl->l_whence) ||
356             __get_user(kfl->l_start, &ufl->l_start) ||
357             __get_user(kfl->l_len, &ufl->l_len) ||
358             __get_user(kfl->l_pid, &ufl->l_pid))
359                 return -EFAULT;
360         return 0;
361 }
362
363 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
364 {
365         if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
366             __put_user(kfl->l_type, &ufl->l_type) ||
367             __put_user(kfl->l_whence, &ufl->l_whence) ||
368             __put_user(kfl->l_start, &ufl->l_start) ||
369             __put_user(kfl->l_len, &ufl->l_len) ||
370             __put_user(kfl->l_pid, &ufl->l_pid))
371                 return -EFAULT;
372         return 0;
373 }
374
375 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
376 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
377 {
378         if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
379             __get_user(kfl->l_type, &ufl->l_type) ||
380             __get_user(kfl->l_whence, &ufl->l_whence) ||
381             __get_user(kfl->l_start, &ufl->l_start) ||
382             __get_user(kfl->l_len, &ufl->l_len) ||
383             __get_user(kfl->l_pid, &ufl->l_pid))
384                 return -EFAULT;
385         return 0;
386 }
387 #endif
388
389 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
390 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
391 {
392         if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
393             __put_user(kfl->l_type, &ufl->l_type) ||
394             __put_user(kfl->l_whence, &ufl->l_whence) ||
395             __put_user(kfl->l_start, &ufl->l_start) ||
396             __put_user(kfl->l_len, &ufl->l_len) ||
397             __put_user(kfl->l_pid, &ufl->l_pid))
398                 return -EFAULT;
399         return 0;
400 }
401 #endif
402
403 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
404                 unsigned long arg)
405 {
406         mm_segment_t old_fs;
407         struct flock f;
408         long ret;
409
410         switch (cmd) {
411         case F_GETLK:
412         case F_SETLK:
413         case F_SETLKW:
414                 ret = get_compat_flock(&f, compat_ptr(arg));
415                 if (ret != 0)
416                         break;
417                 old_fs = get_fs();
418                 set_fs(KERNEL_DS);
419                 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
420                 set_fs(old_fs);
421                 if (cmd == F_GETLK && ret == 0) {
422                         /* GETLK was successful and we need to return the data...
423                          * but it needs to fit in the compat structure.
424                          * l_start shouldn't be too big, unless the original
425                          * start + end is greater than COMPAT_OFF_T_MAX, in which
426                          * case the app was asking for trouble, so we return
427                          * -EOVERFLOW in that case.
428                          * l_len could be too big, in which case we just truncate it,
429                          * and only allow the app to see that part of the conflicting
430                          * lock that might make sense to it anyway
431                          */
432
433                         if (f.l_start > COMPAT_OFF_T_MAX)
434                                 ret = -EOVERFLOW;
435                         if (f.l_len > COMPAT_OFF_T_MAX)
436                                 f.l_len = COMPAT_OFF_T_MAX;
437                         if (ret == 0)
438                                 ret = put_compat_flock(&f, compat_ptr(arg));
439                 }
440                 break;
441
442         case F_GETLK64:
443         case F_SETLK64:
444         case F_SETLKW64:
445                 ret = get_compat_flock64(&f, compat_ptr(arg));
446                 if (ret != 0)
447                         break;
448                 old_fs = get_fs();
449                 set_fs(KERNEL_DS);
450                 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
451                                 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
452                                 (unsigned long)&f);
453                 set_fs(old_fs);
454                 if (cmd == F_GETLK64 && ret == 0) {
455                         /* need to return lock information - see above for commentary */
456                         if (f.l_start > COMPAT_LOFF_T_MAX)
457                                 ret = -EOVERFLOW;
458                         if (f.l_len > COMPAT_LOFF_T_MAX)
459                                 f.l_len = COMPAT_LOFF_T_MAX;
460                         if (ret == 0)
461                                 ret = put_compat_flock64(&f, compat_ptr(arg));
462                 }
463                 break;
464
465         default:
466                 ret = sys_fcntl(fd, cmd, arg);
467                 break;
468         }
469         return ret;
470 }
471
472 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
473                 unsigned long arg)
474 {
475         if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
476                 return -EINVAL;
477         return compat_sys_fcntl64(fd, cmd, arg);
478 }
479
480 asmlinkage long
481 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
482 {
483         long ret;
484         aio_context_t ctx64;
485
486         mm_segment_t oldfs = get_fs();
487         if (unlikely(get_user(ctx64, ctx32p)))
488                 return -EFAULT;
489
490         set_fs(KERNEL_DS);
491         /* The __user pointer cast is valid because of the set_fs() */
492         ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
493         set_fs(oldfs);
494         /* truncating is ok because it's a user address */
495         if (!ret)
496                 ret = put_user((u32) ctx64, ctx32p);
497         return ret;
498 }
499
500 asmlinkage long
501 compat_sys_io_getevents(aio_context_t ctx_id,
502                                  unsigned long min_nr,
503                                  unsigned long nr,
504                                  struct io_event __user *events,
505                                  struct compat_timespec __user *timeout)
506 {
507         long ret;
508         struct timespec t;
509         struct timespec __user *ut = NULL;
510
511         ret = -EFAULT;
512         if (unlikely(!access_ok(VERIFY_WRITE, events, 
513                                 nr * sizeof(struct io_event))))
514                 goto out;
515         if (timeout) {
516                 if (get_compat_timespec(&t, timeout))
517                         goto out;
518
519                 ut = compat_alloc_user_space(sizeof(*ut));
520                 if (copy_to_user(ut, &t, sizeof(t)) )
521                         goto out;
522         } 
523         ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
524 out:
525         return ret;
526 }
527
528 /* A write operation does a read from user space and vice versa */
529 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
530
531 ssize_t compat_rw_copy_check_uvector(int type,
532                 const struct compat_iovec __user *uvector, unsigned long nr_segs,
533                 unsigned long fast_segs, struct iovec *fast_pointer,
534                 struct iovec **ret_pointer)
535 {
536         compat_ssize_t tot_len;
537         struct iovec *iov = *ret_pointer = fast_pointer;
538         ssize_t ret = 0;
539         int seg;
540
541         /*
542          * SuS says "The readv() function *may* fail if the iovcnt argument
543          * was less than or equal to 0, or greater than {IOV_MAX}.  Linux has
544          * traditionally returned zero for zero segments, so...
545          */
546         if (nr_segs == 0)
547                 goto out;
548
549         ret = -EINVAL;
550         if (nr_segs > UIO_MAXIOV || nr_segs < 0)
551                 goto out;
552         if (nr_segs > fast_segs) {
553                 ret = -ENOMEM;
554                 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
555                 if (iov == NULL)
556                         goto out;
557         }
558         *ret_pointer = iov;
559
560         /*
561          * Single unix specification:
562          * We should -EINVAL if an element length is not >= 0 and fitting an
563          * ssize_t.
564          *
565          * In Linux, the total length is limited to MAX_RW_COUNT, there is
566          * no overflow possibility.
567          */
568         tot_len = 0;
569         ret = -EINVAL;
570         for (seg = 0; seg < nr_segs; seg++) {
571                 compat_uptr_t buf;
572                 compat_ssize_t len;
573
574                 if (__get_user(len, &uvector->iov_len) ||
575                    __get_user(buf, &uvector->iov_base)) {
576                         ret = -EFAULT;
577                         goto out;
578                 }
579                 if (len < 0)    /* size_t not fitting in compat_ssize_t .. */
580                         goto out;
581                 if (type >= 0 &&
582                     !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
583                         ret = -EFAULT;
584                         goto out;
585                 }
586                 if (len > MAX_RW_COUNT - tot_len)
587                         len = MAX_RW_COUNT - tot_len;
588                 tot_len += len;
589                 iov->iov_base = compat_ptr(buf);
590                 iov->iov_len = (compat_size_t) len;
591                 uvector++;
592                 iov++;
593         }
594         ret = tot_len;
595
596 out:
597         return ret;
598 }
599
600 static inline long
601 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
602 {
603         compat_uptr_t uptr;
604         int i;
605
606         for (i = 0; i < nr; ++i) {
607                 if (get_user(uptr, ptr32 + i))
608                         return -EFAULT;
609                 if (put_user(compat_ptr(uptr), ptr64 + i))
610                         return -EFAULT;
611         }
612         return 0;
613 }
614
615 #define MAX_AIO_SUBMITS         (PAGE_SIZE/sizeof(struct iocb *))
616
617 asmlinkage long
618 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
619 {
620         struct iocb __user * __user *iocb64; 
621         long ret;
622
623         if (unlikely(nr < 0))
624                 return -EINVAL;
625
626         if (nr > MAX_AIO_SUBMITS)
627                 nr = MAX_AIO_SUBMITS;
628         
629         iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
630         ret = copy_iocb(nr, iocb, iocb64);
631         if (!ret)
632                 ret = do_io_submit(ctx_id, nr, iocb64, 1);
633         return ret;
634 }
635
636 struct compat_ncp_mount_data {
637         compat_int_t version;
638         compat_uint_t ncp_fd;
639         __compat_uid_t mounted_uid;
640         compat_pid_t wdog_pid;
641         unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
642         compat_uint_t time_out;
643         compat_uint_t retry_count;
644         compat_uint_t flags;
645         __compat_uid_t uid;
646         __compat_gid_t gid;
647         compat_mode_t file_mode;
648         compat_mode_t dir_mode;
649 };
650
651 struct compat_ncp_mount_data_v4 {
652         compat_int_t version;
653         compat_ulong_t flags;
654         compat_ulong_t mounted_uid;
655         compat_long_t wdog_pid;
656         compat_uint_t ncp_fd;
657         compat_uint_t time_out;
658         compat_uint_t retry_count;
659         compat_ulong_t uid;
660         compat_ulong_t gid;
661         compat_ulong_t file_mode;
662         compat_ulong_t dir_mode;
663 };
664
665 static void *do_ncp_super_data_conv(void *raw_data)
666 {
667         int version = *(unsigned int *)raw_data;
668
669         if (version == 3) {
670                 struct compat_ncp_mount_data *c_n = raw_data;
671                 struct ncp_mount_data *n = raw_data;
672
673                 n->dir_mode = c_n->dir_mode;
674                 n->file_mode = c_n->file_mode;
675                 n->gid = c_n->gid;
676                 n->uid = c_n->uid;
677                 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
678                 n->wdog_pid = c_n->wdog_pid;
679                 n->mounted_uid = c_n->mounted_uid;
680         } else if (version == 4) {
681                 struct compat_ncp_mount_data_v4 *c_n = raw_data;
682                 struct ncp_mount_data_v4 *n = raw_data;
683
684                 n->dir_mode = c_n->dir_mode;
685                 n->file_mode = c_n->file_mode;
686                 n->gid = c_n->gid;
687                 n->uid = c_n->uid;
688                 n->retry_count = c_n->retry_count;
689                 n->time_out = c_n->time_out;
690                 n->ncp_fd = c_n->ncp_fd;
691                 n->wdog_pid = c_n->wdog_pid;
692                 n->mounted_uid = c_n->mounted_uid;
693                 n->flags = c_n->flags;
694         } else if (version != 5) {
695                 return NULL;
696         }
697
698         return raw_data;
699 }
700
701
702 struct compat_nfs_string {
703         compat_uint_t len;
704         compat_uptr_t data;
705 };
706
707 static inline void compat_nfs_string(struct nfs_string *dst,
708                                      struct compat_nfs_string *src)
709 {
710         dst->data = compat_ptr(src->data);
711         dst->len = src->len;
712 }
713
714 struct compat_nfs4_mount_data_v1 {
715         compat_int_t version;
716         compat_int_t flags;
717         compat_int_t rsize;
718         compat_int_t wsize;
719         compat_int_t timeo;
720         compat_int_t retrans;
721         compat_int_t acregmin;
722         compat_int_t acregmax;
723         compat_int_t acdirmin;
724         compat_int_t acdirmax;
725         struct compat_nfs_string client_addr;
726         struct compat_nfs_string mnt_path;
727         struct compat_nfs_string hostname;
728         compat_uint_t host_addrlen;
729         compat_uptr_t host_addr;
730         compat_int_t proto;
731         compat_int_t auth_flavourlen;
732         compat_uptr_t auth_flavours;
733 };
734
735 static int do_nfs4_super_data_conv(void *raw_data)
736 {
737         int version = *(compat_uint_t *) raw_data;
738
739         if (version == 1) {
740                 struct compat_nfs4_mount_data_v1 *raw = raw_data;
741                 struct nfs4_mount_data *real = raw_data;
742
743                 /* copy the fields backwards */
744                 real->auth_flavours = compat_ptr(raw->auth_flavours);
745                 real->auth_flavourlen = raw->auth_flavourlen;
746                 real->proto = raw->proto;
747                 real->host_addr = compat_ptr(raw->host_addr);
748                 real->host_addrlen = raw->host_addrlen;
749                 compat_nfs_string(&real->hostname, &raw->hostname);
750                 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
751                 compat_nfs_string(&real->client_addr, &raw->client_addr);
752                 real->acdirmax = raw->acdirmax;
753                 real->acdirmin = raw->acdirmin;
754                 real->acregmax = raw->acregmax;
755                 real->acregmin = raw->acregmin;
756                 real->retrans = raw->retrans;
757                 real->timeo = raw->timeo;
758                 real->wsize = raw->wsize;
759                 real->rsize = raw->rsize;
760                 real->flags = raw->flags;
761                 real->version = raw->version;
762         }
763
764         return 0;
765 }
766
767 #define NCPFS_NAME      "ncpfs"
768 #define NFS4_NAME       "nfs4"
769
770 asmlinkage long compat_sys_mount(const char __user * dev_name,
771                                  const char __user * dir_name,
772                                  const char __user * type, unsigned long flags,
773                                  const void __user * data)
774 {
775         char *kernel_type;
776         unsigned long data_page;
777         char *kernel_dev;
778         struct filename *dir;
779         int retval;
780
781         retval = copy_mount_string(type, &kernel_type);
782         if (retval < 0)
783                 goto out;
784
785         dir = getname(dir_name);
786         retval = PTR_ERR(dir);
787         if (IS_ERR(dir))
788                 goto out1;
789
790         retval = copy_mount_string(dev_name, &kernel_dev);
791         if (retval < 0)
792                 goto out2;
793
794         retval = copy_mount_options(data, &data_page);
795         if (retval < 0)
796                 goto out3;
797
798         retval = -EINVAL;
799
800         if (kernel_type && data_page) {
801                 if (!strcmp(kernel_type, NCPFS_NAME)) {
802                         do_ncp_super_data_conv((void *)data_page);
803                 } else if (!strcmp(kernel_type, NFS4_NAME)) {
804                         if (do_nfs4_super_data_conv((void *) data_page))
805                                 goto out4;
806                 }
807         }
808
809         retval = do_mount(kernel_dev, dir->name, kernel_type,
810                         flags, (void*)data_page);
811
812  out4:
813         free_page(data_page);
814  out3:
815         kfree(kernel_dev);
816  out2:
817         putname(dir);
818  out1:
819         kfree(kernel_type);
820  out:
821         return retval;
822 }
823
824 struct compat_old_linux_dirent {
825         compat_ulong_t  d_ino;
826         compat_ulong_t  d_offset;
827         unsigned short  d_namlen;
828         char            d_name[1];
829 };
830
831 struct compat_readdir_callback {
832         struct compat_old_linux_dirent __user *dirent;
833         int result;
834 };
835
836 static int compat_fillonedir(void *__buf, const char *name, int namlen,
837                         loff_t offset, u64 ino, unsigned int d_type)
838 {
839         struct compat_readdir_callback *buf = __buf;
840         struct compat_old_linux_dirent __user *dirent;
841         compat_ulong_t d_ino;
842
843         if (buf->result)
844                 return -EINVAL;
845         d_ino = ino;
846         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
847                 buf->result = -EOVERFLOW;
848                 return -EOVERFLOW;
849         }
850         buf->result++;
851         dirent = buf->dirent;
852         if (!access_ok(VERIFY_WRITE, dirent,
853                         (unsigned long)(dirent->d_name + namlen + 1) -
854                                 (unsigned long)dirent))
855                 goto efault;
856         if (    __put_user(d_ino, &dirent->d_ino) ||
857                 __put_user(offset, &dirent->d_offset) ||
858                 __put_user(namlen, &dirent->d_namlen) ||
859                 __copy_to_user(dirent->d_name, name, namlen) ||
860                 __put_user(0, dirent->d_name + namlen))
861                 goto efault;
862         return 0;
863 efault:
864         buf->result = -EFAULT;
865         return -EFAULT;
866 }
867
868 asmlinkage long compat_sys_old_readdir(unsigned int fd,
869         struct compat_old_linux_dirent __user *dirent, unsigned int count)
870 {
871         int error;
872         struct fd f = fdget(fd);
873         struct compat_readdir_callback buf;
874
875         if (!f.file)
876                 return -EBADF;
877
878         buf.result = 0;
879         buf.dirent = dirent;
880
881         error = vfs_readdir(f.file, compat_fillonedir, &buf);
882         if (buf.result)
883                 error = buf.result;
884
885         fdput(f);
886         return error;
887 }
888
889 struct compat_linux_dirent {
890         compat_ulong_t  d_ino;
891         compat_ulong_t  d_off;
892         unsigned short  d_reclen;
893         char            d_name[1];
894 };
895
896 struct compat_getdents_callback {
897         struct compat_linux_dirent __user *current_dir;
898         struct compat_linux_dirent __user *previous;
899         int count;
900         int error;
901 };
902
903 static int compat_filldir(void *__buf, const char *name, int namlen,
904                 loff_t offset, u64 ino, unsigned int d_type)
905 {
906         struct compat_linux_dirent __user * dirent;
907         struct compat_getdents_callback *buf = __buf;
908         compat_ulong_t d_ino;
909         int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
910                 namlen + 2, sizeof(compat_long_t));
911
912         buf->error = -EINVAL;   /* only used if we fail.. */
913         if (reclen > buf->count)
914                 return -EINVAL;
915         d_ino = ino;
916         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
917                 buf->error = -EOVERFLOW;
918                 return -EOVERFLOW;
919         }
920         dirent = buf->previous;
921         if (dirent) {
922                 if (__put_user(offset, &dirent->d_off))
923                         goto efault;
924         }
925         dirent = buf->current_dir;
926         if (__put_user(d_ino, &dirent->d_ino))
927                 goto efault;
928         if (__put_user(reclen, &dirent->d_reclen))
929                 goto efault;
930         if (copy_to_user(dirent->d_name, name, namlen))
931                 goto efault;
932         if (__put_user(0, dirent->d_name + namlen))
933                 goto efault;
934         if (__put_user(d_type, (char  __user *) dirent + reclen - 1))
935                 goto efault;
936         buf->previous = dirent;
937         dirent = (void __user *)dirent + reclen;
938         buf->current_dir = dirent;
939         buf->count -= reclen;
940         return 0;
941 efault:
942         buf->error = -EFAULT;
943         return -EFAULT;
944 }
945
946 asmlinkage long compat_sys_getdents(unsigned int fd,
947                 struct compat_linux_dirent __user *dirent, unsigned int count)
948 {
949         struct fd f;
950         struct compat_linux_dirent __user * lastdirent;
951         struct compat_getdents_callback buf;
952         int error;
953
954         if (!access_ok(VERIFY_WRITE, dirent, count))
955                 return -EFAULT;
956
957         f = fdget(fd);
958         if (!f.file)
959                 return -EBADF;
960
961         buf.current_dir = dirent;
962         buf.previous = NULL;
963         buf.count = count;
964         buf.error = 0;
965
966         error = vfs_readdir(f.file, compat_filldir, &buf);
967         if (error >= 0)
968                 error = buf.error;
969         lastdirent = buf.previous;
970         if (lastdirent) {
971                 if (put_user(f.file->f_pos, &lastdirent->d_off))
972                         error = -EFAULT;
973                 else
974                         error = count - buf.count;
975         }
976         fdput(f);
977         return error;
978 }
979
980 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
981
982 struct compat_getdents_callback64 {
983         struct linux_dirent64 __user *current_dir;
984         struct linux_dirent64 __user *previous;
985         int count;
986         int error;
987 };
988
989 static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
990                      u64 ino, unsigned int d_type)
991 {
992         struct linux_dirent64 __user *dirent;
993         struct compat_getdents_callback64 *buf = __buf;
994         int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
995                 sizeof(u64));
996         u64 off;
997
998         buf->error = -EINVAL;   /* only used if we fail.. */
999         if (reclen > buf->count)
1000                 return -EINVAL;
1001         dirent = buf->previous;
1002
1003         if (dirent) {
1004                 if (__put_user_unaligned(offset, &dirent->d_off))
1005                         goto efault;
1006         }
1007         dirent = buf->current_dir;
1008         if (__put_user_unaligned(ino, &dirent->d_ino))
1009                 goto efault;
1010         off = 0;
1011         if (__put_user_unaligned(off, &dirent->d_off))
1012                 goto efault;
1013         if (__put_user(reclen, &dirent->d_reclen))
1014                 goto efault;
1015         if (__put_user(d_type, &dirent->d_type))
1016                 goto efault;
1017         if (copy_to_user(dirent->d_name, name, namlen))
1018                 goto efault;
1019         if (__put_user(0, dirent->d_name + namlen))
1020                 goto efault;
1021         buf->previous = dirent;
1022         dirent = (void __user *)dirent + reclen;
1023         buf->current_dir = dirent;
1024         buf->count -= reclen;
1025         return 0;
1026 efault:
1027         buf->error = -EFAULT;
1028         return -EFAULT;
1029 }
1030
1031 asmlinkage long compat_sys_getdents64(unsigned int fd,
1032                 struct linux_dirent64 __user * dirent, unsigned int count)
1033 {
1034         struct fd f;
1035         struct linux_dirent64 __user * lastdirent;
1036         struct compat_getdents_callback64 buf;
1037         int error;
1038
1039         if (!access_ok(VERIFY_WRITE, dirent, count))
1040                 return -EFAULT;
1041
1042         f = fdget(fd);
1043         if (!f.file)
1044                 return -EBADF;
1045
1046         buf.current_dir = dirent;
1047         buf.previous = NULL;
1048         buf.count = count;
1049         buf.error = 0;
1050
1051         error = vfs_readdir(f.file, compat_filldir64, &buf);
1052         if (error >= 0)
1053                 error = buf.error;
1054         lastdirent = buf.previous;
1055         if (lastdirent) {
1056                 typeof(lastdirent->d_off) d_off = f.file->f_pos;
1057                 if (__put_user_unaligned(d_off, &lastdirent->d_off))
1058                         error = -EFAULT;
1059                 else
1060                         error = count - buf.count;
1061         }
1062         fdput(f);
1063         return error;
1064 }
1065 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1066
1067 static ssize_t compat_do_readv_writev(int type, struct file *file,
1068                                const struct compat_iovec __user *uvector,
1069                                unsigned long nr_segs, loff_t *pos)
1070 {
1071         compat_ssize_t tot_len;
1072         struct iovec iovstack[UIO_FASTIOV];
1073         struct iovec *iov = iovstack;
1074         ssize_t ret;
1075         io_fn_t fn;
1076         iov_fn_t fnv;
1077
1078         ret = -EINVAL;
1079         if (!file->f_op)
1080                 goto out;
1081
1082         ret = -EFAULT;
1083         if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1084                 goto out;
1085
1086         tot_len = compat_rw_copy_check_uvector(type, uvector, nr_segs,
1087                                                UIO_FASTIOV, iovstack, &iov);
1088         if (tot_len == 0) {
1089                 ret = 0;
1090                 goto out;
1091         }
1092
1093         ret = rw_verify_area(type, file, pos, tot_len);
1094         if (ret < 0)
1095                 goto out;
1096
1097         fnv = NULL;
1098         if (type == READ) {
1099                 fn = file->f_op->read;
1100                 fnv = file->f_op->aio_read;
1101         } else {
1102                 fn = (io_fn_t)file->f_op->write;
1103                 fnv = file->f_op->aio_write;
1104         }
1105
1106         if (fnv)
1107                 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1108                                                 pos, fnv);
1109         else
1110                 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
1111
1112 out:
1113         if (iov != iovstack)
1114                 kfree(iov);
1115         if ((ret + (type == READ)) > 0) {
1116                 if (type == READ)
1117                         fsnotify_access(file);
1118                 else
1119                         fsnotify_modify(file);
1120         }
1121         return ret;
1122 }
1123
1124 static size_t compat_readv(struct file *file,
1125                            const struct compat_iovec __user *vec,
1126                            unsigned long vlen, loff_t *pos)
1127 {
1128         ssize_t ret = -EBADF;
1129
1130         if (!(file->f_mode & FMODE_READ))
1131                 goto out;
1132
1133         ret = -EINVAL;
1134         if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
1135                 goto out;
1136
1137         ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
1138
1139 out:
1140         if (ret > 0)
1141                 add_rchar(current, ret);
1142         inc_syscr(current);
1143         return ret;
1144 }
1145
1146 asmlinkage ssize_t
1147 compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec,
1148                  unsigned long vlen)
1149 {
1150         struct fd f = fdget(fd);
1151         ssize_t ret;
1152         loff_t pos;
1153
1154         if (!f.file)
1155                 return -EBADF;
1156         pos = f.file->f_pos;
1157         ret = compat_readv(f.file, vec, vlen, &pos);
1158         f.file->f_pos = pos;
1159         fdput(f);
1160         return ret;
1161 }
1162
1163 asmlinkage ssize_t
1164 compat_sys_preadv64(unsigned long fd, const struct compat_iovec __user *vec,
1165                     unsigned long vlen, loff_t pos)
1166 {
1167         struct fd f;
1168         ssize_t ret;
1169
1170         if (pos < 0)
1171                 return -EINVAL;
1172         f = fdget(fd);
1173         if (!f.file)
1174                 return -EBADF;
1175         ret = -ESPIPE;
1176         if (f.file->f_mode & FMODE_PREAD)
1177                 ret = compat_readv(f.file, vec, vlen, &pos);
1178         fdput(f);
1179         return ret;
1180 }
1181
1182 asmlinkage ssize_t
1183 compat_sys_preadv(unsigned long fd, const struct compat_iovec __user *vec,
1184                   unsigned long vlen, u32 pos_low, u32 pos_high)
1185 {
1186         loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1187         return compat_sys_preadv64(fd, vec, vlen, pos);
1188 }
1189
1190 static size_t compat_writev(struct file *file,
1191                             const struct compat_iovec __user *vec,
1192                             unsigned long vlen, loff_t *pos)
1193 {
1194         ssize_t ret = -EBADF;
1195
1196         if (!(file->f_mode & FMODE_WRITE))
1197                 goto out;
1198
1199         ret = -EINVAL;
1200         if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
1201                 goto out;
1202
1203         ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1204
1205 out:
1206         if (ret > 0)
1207                 add_wchar(current, ret);
1208         inc_syscw(current);
1209         return ret;
1210 }
1211
1212 asmlinkage ssize_t
1213 compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec,
1214                   unsigned long vlen)
1215 {
1216         struct fd f = fdget(fd);
1217         ssize_t ret;
1218         loff_t pos;
1219
1220         if (!f.file)
1221                 return -EBADF;
1222         pos = f.file->f_pos;
1223         ret = compat_writev(f.file, vec, vlen, &pos);
1224         f.file->f_pos = pos;
1225         fdput(f);
1226         return ret;
1227 }
1228
1229 asmlinkage ssize_t
1230 compat_sys_pwritev64(unsigned long fd, const struct compat_iovec __user *vec,
1231                      unsigned long vlen, loff_t pos)
1232 {
1233         struct fd f;
1234         ssize_t ret;
1235
1236         if (pos < 0)
1237                 return -EINVAL;
1238         f = fdget(fd);
1239         if (!f.file)
1240                 return -EBADF;
1241         ret = -ESPIPE;
1242         if (f.file->f_mode & FMODE_PWRITE)
1243                 ret = compat_writev(f.file, vec, vlen, &pos);
1244         fdput(f);
1245         return ret;
1246 }
1247
1248 asmlinkage ssize_t
1249 compat_sys_pwritev(unsigned long fd, const struct compat_iovec __user *vec,
1250                    unsigned long vlen, u32 pos_low, u32 pos_high)
1251 {
1252         loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1253         return compat_sys_pwritev64(fd, vec, vlen, pos);
1254 }
1255
1256 asmlinkage long
1257 compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
1258                     unsigned int nr_segs, unsigned int flags)
1259 {
1260         unsigned i;
1261         struct iovec __user *iov;
1262         if (nr_segs > UIO_MAXIOV)
1263                 return -EINVAL;
1264         iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1265         for (i = 0; i < nr_segs; i++) {
1266                 struct compat_iovec v;
1267                 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1268                     get_user(v.iov_len, &iov32[i].iov_len) ||
1269                     put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1270                     put_user(v.iov_len, &iov[i].iov_len))
1271                         return -EFAULT;
1272         }
1273         return sys_vmsplice(fd, iov, nr_segs, flags);
1274 }
1275
1276 /*
1277  * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1278  * O_LARGEFILE flag.
1279  */
1280 COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1281 {
1282         return do_sys_open(AT_FDCWD, filename, flags, mode);
1283 }
1284
1285 /*
1286  * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1287  * O_LARGEFILE flag.
1288  */
1289 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
1290 {
1291         return do_sys_open(dfd, filename, flags, mode);
1292 }
1293
1294 #define __COMPAT_NFDBITS       (8 * sizeof(compat_ulong_t))
1295
1296 static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
1297                                       int timeval, int ret)
1298 {
1299         struct timespec ts;
1300
1301         if (!p)
1302                 return ret;
1303
1304         if (current->personality & STICKY_TIMEOUTS)
1305                 goto sticky;
1306
1307         /* No update for zero timeout */
1308         if (!end_time->tv_sec && !end_time->tv_nsec)
1309                 return ret;
1310
1311         ktime_get_ts(&ts);
1312         ts = timespec_sub(*end_time, ts);
1313         if (ts.tv_sec < 0)
1314                 ts.tv_sec = ts.tv_nsec = 0;
1315
1316         if (timeval) {
1317                 struct compat_timeval rtv;
1318
1319                 rtv.tv_sec = ts.tv_sec;
1320                 rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
1321
1322                 if (!copy_to_user(p, &rtv, sizeof(rtv)))
1323                         return ret;
1324         } else {
1325                 struct compat_timespec rts;
1326
1327                 rts.tv_sec = ts.tv_sec;
1328                 rts.tv_nsec = ts.tv_nsec;
1329
1330                 if (!copy_to_user(p, &rts, sizeof(rts)))
1331                         return ret;
1332         }
1333         /*
1334          * If an application puts its timeval in read-only memory, we
1335          * don't want the Linux-specific update to the timeval to
1336          * cause a fault after the select has completed
1337          * successfully. However, because we're not updating the
1338          * timeval, we can't restart the system call.
1339          */
1340
1341 sticky:
1342         if (ret == -ERESTARTNOHAND)
1343                 ret = -EINTR;
1344         return ret;
1345 }
1346
1347 /*
1348  * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
1349  * 64-bit unsigned longs.
1350  */
1351 static
1352 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1353                         unsigned long *fdset)
1354 {
1355         nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
1356         if (ufdset) {
1357                 unsigned long odd;
1358
1359                 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1360                         return -EFAULT;
1361
1362                 odd = nr & 1UL;
1363                 nr &= ~1UL;
1364                 while (nr) {
1365                         unsigned long h, l;
1366                         if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1367                                 return -EFAULT;
1368                         ufdset += 2;
1369                         *fdset++ = h << 32 | l;
1370                         nr -= 2;
1371                 }
1372                 if (odd && __get_user(*fdset, ufdset))
1373                         return -EFAULT;
1374         } else {
1375                 /* Tricky, must clear full unsigned long in the
1376                  * kernel fdset at the end, this makes sure that
1377                  * actually happens.
1378                  */
1379                 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1380         }
1381         return 0;
1382 }
1383
1384 static
1385 int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1386                       unsigned long *fdset)
1387 {
1388         unsigned long odd;
1389         nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
1390
1391         if (!ufdset)
1392                 return 0;
1393
1394         odd = nr & 1UL;
1395         nr &= ~1UL;
1396         while (nr) {
1397                 unsigned long h, l;
1398                 l = *fdset++;
1399                 h = l >> 32;
1400                 if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1401                         return -EFAULT;
1402                 ufdset += 2;
1403                 nr -= 2;
1404         }
1405         if (odd && __put_user(*fdset, ufdset))
1406                 return -EFAULT;
1407         return 0;
1408 }
1409
1410
1411 /*
1412  * This is a virtual copy of sys_select from fs/select.c and probably
1413  * should be compared to it from time to time
1414  */
1415
1416 /*
1417  * We can actually return ERESTARTSYS instead of EINTR, but I'd
1418  * like to be certain this leads to no problems. So I return
1419  * EINTR just for safety.
1420  *
1421  * Update: ERESTARTSYS breaks at least the xview clock binary, so
1422  * I'm trying ERESTARTNOHAND which restart only when you want to.
1423  */
1424 int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1425         compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1426         struct timespec *end_time)
1427 {
1428         fd_set_bits fds;
1429         void *bits;
1430         int size, max_fds, ret = -EINVAL;
1431         struct fdtable *fdt;
1432         long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
1433
1434         if (n < 0)
1435                 goto out_nofds;
1436
1437         /* max_fds can increase, so grab it once to avoid race */
1438         rcu_read_lock();
1439         fdt = files_fdtable(current->files);
1440         max_fds = fdt->max_fds;
1441         rcu_read_unlock();
1442         if (n > max_fds)
1443                 n = max_fds;
1444
1445         /*
1446          * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1447          * since we used fdset we need to allocate memory in units of
1448          * long-words.
1449          */
1450         size = FDS_BYTES(n);
1451         bits = stack_fds;
1452         if (size > sizeof(stack_fds) / 6) {
1453                 bits = kmalloc(6 * size, GFP_KERNEL);
1454                 ret = -ENOMEM;
1455                 if (!bits)
1456                         goto out_nofds;
1457         }
1458         fds.in      = (unsigned long *)  bits;
1459         fds.out     = (unsigned long *) (bits +   size);
1460         fds.ex      = (unsigned long *) (bits + 2*size);
1461         fds.res_in  = (unsigned long *) (bits + 3*size);
1462         fds.res_out = (unsigned long *) (bits + 4*size);
1463         fds.res_ex  = (unsigned long *) (bits + 5*size);
1464
1465         if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1466             (ret = compat_get_fd_set(n, outp, fds.out)) ||
1467             (ret = compat_get_fd_set(n, exp, fds.ex)))
1468                 goto out;
1469         zero_fd_set(n, fds.res_in);
1470         zero_fd_set(n, fds.res_out);
1471         zero_fd_set(n, fds.res_ex);
1472
1473         ret = do_select(n, &fds, end_time);
1474
1475         if (ret < 0)
1476                 goto out;
1477         if (!ret) {
1478                 ret = -ERESTARTNOHAND;
1479                 if (signal_pending(current))
1480                         goto out;
1481                 ret = 0;
1482         }
1483
1484         if (compat_set_fd_set(n, inp, fds.res_in) ||
1485             compat_set_fd_set(n, outp, fds.res_out) ||
1486             compat_set_fd_set(n, exp, fds.res_ex))
1487                 ret = -EFAULT;
1488 out:
1489         if (bits != stack_fds)
1490                 kfree(bits);
1491 out_nofds:
1492         return ret;
1493 }
1494
1495 asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1496         compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1497         struct compat_timeval __user *tvp)
1498 {
1499         struct timespec end_time, *to = NULL;
1500         struct compat_timeval tv;
1501         int ret;
1502
1503         if (tvp) {
1504                 if (copy_from_user(&tv, tvp, sizeof(tv)))
1505                         return -EFAULT;
1506
1507                 to = &end_time;
1508                 if (poll_select_set_timeout(to,
1509                                 tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
1510                                 (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
1511                         return -EINVAL;
1512         }
1513
1514         ret = compat_core_sys_select(n, inp, outp, exp, to);
1515         ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
1516
1517         return ret;
1518 }
1519
1520 struct compat_sel_arg_struct {
1521         compat_ulong_t n;
1522         compat_uptr_t inp;
1523         compat_uptr_t outp;
1524         compat_uptr_t exp;
1525         compat_uptr_t tvp;
1526 };
1527
1528 asmlinkage long compat_sys_old_select(struct compat_sel_arg_struct __user *arg)
1529 {
1530         struct compat_sel_arg_struct a;
1531
1532         if (copy_from_user(&a, arg, sizeof(a)))
1533                 return -EFAULT;
1534         return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1535                                  compat_ptr(a.exp), compat_ptr(a.tvp));
1536 }
1537
1538 static long do_compat_pselect(int n, compat_ulong_t __user *inp,
1539         compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1540         struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1541         compat_size_t sigsetsize)
1542 {
1543         compat_sigset_t ss32;
1544         sigset_t ksigmask, sigsaved;
1545         struct compat_timespec ts;
1546         struct timespec end_time, *to = NULL;
1547         int ret;
1548
1549         if (tsp) {
1550                 if (copy_from_user(&ts, tsp, sizeof(ts)))
1551                         return -EFAULT;
1552
1553                 to = &end_time;
1554                 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1555                         return -EINVAL;
1556         }
1557
1558         if (sigmask) {
1559                 if (sigsetsize != sizeof(compat_sigset_t))
1560                         return -EINVAL;
1561                 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1562                         return -EFAULT;
1563                 sigset_from_compat(&ksigmask, &ss32);
1564
1565                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1566                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1567         }
1568
1569         ret = compat_core_sys_select(n, inp, outp, exp, to);
1570         ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
1571
1572         if (ret == -ERESTARTNOHAND) {
1573                 /*
1574                  * Don't restore the signal mask yet. Let do_signal() deliver
1575                  * the signal on the way back to userspace, before the signal
1576                  * mask is restored.
1577                  */
1578                 if (sigmask) {
1579                         memcpy(&current->saved_sigmask, &sigsaved,
1580                                         sizeof(sigsaved));
1581                         set_restore_sigmask();
1582                 }
1583         } else if (sigmask)
1584                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1585
1586         return ret;
1587 }
1588
1589 asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1590         compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1591         struct compat_timespec __user *tsp, void __user *sig)
1592 {
1593         compat_size_t sigsetsize = 0;
1594         compat_uptr_t up = 0;
1595
1596         if (sig) {
1597                 if (!access_ok(VERIFY_READ, sig,
1598                                 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1599                         __get_user(up, (compat_uptr_t __user *)sig) ||
1600                         __get_user(sigsetsize,
1601                                 (compat_size_t __user *)(sig+sizeof(up))))
1602                         return -EFAULT;
1603         }
1604         return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
1605                                  sigsetsize);
1606 }
1607
1608 asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1609         unsigned int nfds, struct compat_timespec __user *tsp,
1610         const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1611 {
1612         compat_sigset_t ss32;
1613         sigset_t ksigmask, sigsaved;
1614         struct compat_timespec ts;
1615         struct timespec end_time, *to = NULL;
1616         int ret;
1617
1618         if (tsp) {
1619                 if (copy_from_user(&ts, tsp, sizeof(ts)))
1620                         return -EFAULT;
1621
1622                 to = &end_time;
1623                 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1624                         return -EINVAL;
1625         }
1626
1627         if (sigmask) {
1628                 if (sigsetsize != sizeof(compat_sigset_t))
1629                         return -EINVAL;
1630                 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1631                         return -EFAULT;
1632                 sigset_from_compat(&ksigmask, &ss32);
1633
1634                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1635                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1636         }
1637
1638         ret = do_sys_poll(ufds, nfds, to);
1639
1640         /* We can restart this syscall, usually */
1641         if (ret == -EINTR) {
1642                 /*
1643                  * Don't restore the signal mask yet. Let do_signal() deliver
1644                  * the signal on the way back to userspace, before the signal
1645                  * mask is restored.
1646                  */
1647                 if (sigmask) {
1648                         memcpy(&current->saved_sigmask, &sigsaved,
1649                                 sizeof(sigsaved));
1650                         set_restore_sigmask();
1651                 }
1652                 ret = -ERESTARTNOHAND;
1653         } else if (sigmask)
1654                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1655
1656         ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
1657
1658         return ret;
1659 }
1660
1661 #ifdef CONFIG_FHANDLE
1662 /*
1663  * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
1664  * doesn't set the O_LARGEFILE flag.
1665  */
1666 COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
1667                              struct file_handle __user *, handle, int, flags)
1668 {
1669         return do_handle_open(mountdirfd, handle, flags);
1670 }
1671 #endif