4 * Kernel compatibililty routines for e.g. 32 bit syscall support
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)
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.
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>
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>
47 #include <linux/fs_struct.h>
48 #include <linux/slab.h>
49 #include <linux/pagemap.h>
51 #include <asm/uaccess.h>
52 #include <asm/mmu_context.h>
53 #include <asm/ioctls.h>
58 int compat_printk(const char *fmt, ...)
65 ret = vprintk(fmt, ap);
70 #include "read_write.h"
73 * Not all architectures have sys_utime, so implement this in terms
76 asmlinkage long compat_sys_utime(const char __user *filename,
77 struct compat_utimbuf __user *t)
79 struct timespec tv[2];
82 if (get_user(tv[0].tv_sec, &t->actime) ||
83 get_user(tv[1].tv_sec, &t->modtime))
88 return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
91 asmlinkage long compat_sys_utimensat(unsigned int dfd, const char __user *filename, struct compat_timespec __user *t, int flags)
93 struct timespec tv[2];
96 if (get_compat_timespec(&tv[0], &t[0]) ||
97 get_compat_timespec(&tv[1], &t[1]))
100 if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
103 return do_utimes(dfd, filename, t ? tv : NULL, flags);
106 asmlinkage long compat_sys_futimesat(unsigned int dfd, const char __user *filename, struct compat_timeval __user *t)
108 struct timespec tv[2];
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))
116 if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
117 tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
119 tv[0].tv_nsec *= 1000;
120 tv[1].tv_nsec *= 1000;
122 return do_utimes(dfd, filename, t ? tv : NULL, 0);
125 asmlinkage long compat_sys_utimes(const char __user *filename, struct compat_timeval __user *t)
127 return compat_sys_futimesat(AT_FDCWD, filename, t);
130 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
132 struct compat_stat tmp;
134 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
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)
142 tmp.st_mode = stat->mode;
143 tmp.st_nlink = stat->nlink;
144 if (tmp.st_nlink != stat->nlink)
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)
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;
163 asmlinkage long compat_sys_newstat(const char __user * filename,
164 struct compat_stat __user *statbuf)
169 error = vfs_stat(filename, &stat);
172 return cp_compat_stat(&stat, statbuf);
175 asmlinkage long compat_sys_newlstat(const char __user * filename,
176 struct compat_stat __user *statbuf)
181 error = vfs_lstat(filename, &stat);
184 return cp_compat_stat(&stat, statbuf);
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)
195 error = vfs_fstatat(dfd, filename, &stat, flag);
198 return cp_compat_stat(&stat, statbuf);
202 asmlinkage long compat_sys_newfstat(unsigned int fd,
203 struct compat_stat __user * statbuf)
206 int error = vfs_fstat(fd, &stat);
209 error = cp_compat_stat(&stat, statbuf);
213 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
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)
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))
225 if (kbuf->f_ffree != 0xffffffffffffffffULL
226 && (kbuf->f_ffree & 0xffffffff00000000ULL))
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)))
248 * The following statfs calls are copies of code from fs/statfs.c and
249 * should be checked against those from time to time
251 asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf)
254 int error = user_statfs(pathname, &tmp);
256 error = put_compat_statfs(buf, &tmp);
260 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
263 int error = fd_statfs(fd, &tmp);
265 error = put_compat_statfs(buf, &tmp);
269 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
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)
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))
280 if (kbuf->f_ffree != 0xffffffffffffffffULL
281 && (kbuf->f_ffree & 0xffffffff00000000ULL))
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)))
302 asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf)
307 if (sz != sizeof(*buf))
310 error = user_statfs(pathname, &tmp);
312 error = put_compat_statfs64(buf, &tmp);
316 asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
321 if (sz != sizeof(*buf))
324 error = fd_statfs(fd, &tmp);
326 error = put_compat_statfs64(buf, &tmp);
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.
335 asmlinkage long compat_sys_ustat(unsigned dev, struct compat_ustat __user *u)
337 struct compat_ustat tmp;
339 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
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)))
351 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
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))
363 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
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))
375 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
376 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
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))
389 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
390 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
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))
403 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
414 ret = get_compat_flock(&f, compat_ptr(arg));
419 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
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
433 if (f.l_start > COMPAT_OFF_T_MAX)
435 if (f.l_len > COMPAT_OFF_T_MAX)
436 f.l_len = COMPAT_OFF_T_MAX;
438 ret = put_compat_flock(&f, compat_ptr(arg));
445 ret = get_compat_flock64(&f, compat_ptr(arg));
450 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
451 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
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)
458 if (f.l_len > COMPAT_LOFF_T_MAX)
459 f.l_len = COMPAT_LOFF_T_MAX;
461 ret = put_compat_flock64(&f, compat_ptr(arg));
466 ret = sys_fcntl(fd, cmd, arg);
472 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
475 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
477 return compat_sys_fcntl64(fd, cmd, arg);
481 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
486 mm_segment_t oldfs = get_fs();
487 if (unlikely(get_user(ctx64, ctx32p)))
491 /* The __user pointer cast is valid because of the set_fs() */
492 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
494 /* truncating is ok because it's a user address */
496 ret = put_user((u32) ctx64, ctx32p);
501 compat_sys_io_getevents(aio_context_t ctx_id,
502 unsigned long min_nr,
504 struct io_event __user *events,
505 struct compat_timespec __user *timeout)
509 struct timespec __user *ut = NULL;
512 if (unlikely(!access_ok(VERIFY_WRITE, events,
513 nr * sizeof(struct io_event))))
516 if (get_compat_timespec(&t, timeout))
519 ut = compat_alloc_user_space(sizeof(*ut));
520 if (copy_to_user(ut, &t, sizeof(t)) )
523 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
528 /* A write operation does a read from user space and vice versa */
529 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
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)
536 compat_ssize_t tot_len;
537 struct iovec *iov = *ret_pointer = fast_pointer;
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...
550 if (nr_segs > UIO_MAXIOV || nr_segs < 0)
552 if (nr_segs > fast_segs) {
554 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
561 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
565 * Single unix specification:
566 * We should -EINVAL if an element length is not >= 0 and fitting an
569 * In Linux, the total length is limited to MAX_RW_COUNT, there is
570 * no overflow possibility.
574 for (seg = 0; seg < nr_segs; seg++) {
578 if (__get_user(len, &uvector->iov_len) ||
579 __get_user(buf, &uvector->iov_base)) {
583 if (len < 0) /* size_t not fitting in compat_ssize_t .. */
586 !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
590 if (len > MAX_RW_COUNT - tot_len)
591 len = MAX_RW_COUNT - tot_len;
593 iov->iov_base = compat_ptr(buf);
594 iov->iov_len = (compat_size_t) len;
605 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
610 for (i = 0; i < nr; ++i) {
611 if (get_user(uptr, ptr32 + i))
613 if (put_user(compat_ptr(uptr), ptr64 + i))
619 #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
622 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
624 struct iocb __user * __user *iocb64;
627 if (unlikely(nr < 0))
630 if (nr > MAX_AIO_SUBMITS)
631 nr = MAX_AIO_SUBMITS;
633 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
634 ret = copy_iocb(nr, iocb, iocb64);
636 ret = do_io_submit(ctx_id, nr, iocb64, 1);
640 struct compat_ncp_mount_data {
641 compat_int_t version;
642 compat_uint_t ncp_fd;
643 __compat_uid_t mounted_uid;
644 compat_pid_t wdog_pid;
645 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
646 compat_uint_t time_out;
647 compat_uint_t retry_count;
651 compat_mode_t file_mode;
652 compat_mode_t dir_mode;
655 struct compat_ncp_mount_data_v4 {
656 compat_int_t version;
657 compat_ulong_t flags;
658 compat_ulong_t mounted_uid;
659 compat_long_t wdog_pid;
660 compat_uint_t ncp_fd;
661 compat_uint_t time_out;
662 compat_uint_t retry_count;
665 compat_ulong_t file_mode;
666 compat_ulong_t dir_mode;
669 static void *do_ncp_super_data_conv(void *raw_data)
671 int version = *(unsigned int *)raw_data;
674 struct compat_ncp_mount_data *c_n = raw_data;
675 struct ncp_mount_data *n = raw_data;
677 n->dir_mode = c_n->dir_mode;
678 n->file_mode = c_n->file_mode;
681 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
682 n->wdog_pid = c_n->wdog_pid;
683 n->mounted_uid = c_n->mounted_uid;
684 } else if (version == 4) {
685 struct compat_ncp_mount_data_v4 *c_n = raw_data;
686 struct ncp_mount_data_v4 *n = raw_data;
688 n->dir_mode = c_n->dir_mode;
689 n->file_mode = c_n->file_mode;
692 n->retry_count = c_n->retry_count;
693 n->time_out = c_n->time_out;
694 n->ncp_fd = c_n->ncp_fd;
695 n->wdog_pid = c_n->wdog_pid;
696 n->mounted_uid = c_n->mounted_uid;
697 n->flags = c_n->flags;
698 } else if (version != 5) {
706 struct compat_nfs_string {
711 static inline void compat_nfs_string(struct nfs_string *dst,
712 struct compat_nfs_string *src)
714 dst->data = compat_ptr(src->data);
718 struct compat_nfs4_mount_data_v1 {
719 compat_int_t version;
724 compat_int_t retrans;
725 compat_int_t acregmin;
726 compat_int_t acregmax;
727 compat_int_t acdirmin;
728 compat_int_t acdirmax;
729 struct compat_nfs_string client_addr;
730 struct compat_nfs_string mnt_path;
731 struct compat_nfs_string hostname;
732 compat_uint_t host_addrlen;
733 compat_uptr_t host_addr;
735 compat_int_t auth_flavourlen;
736 compat_uptr_t auth_flavours;
739 static int do_nfs4_super_data_conv(void *raw_data)
741 int version = *(compat_uint_t *) raw_data;
744 struct compat_nfs4_mount_data_v1 *raw = raw_data;
745 struct nfs4_mount_data *real = raw_data;
747 /* copy the fields backwards */
748 real->auth_flavours = compat_ptr(raw->auth_flavours);
749 real->auth_flavourlen = raw->auth_flavourlen;
750 real->proto = raw->proto;
751 real->host_addr = compat_ptr(raw->host_addr);
752 real->host_addrlen = raw->host_addrlen;
753 compat_nfs_string(&real->hostname, &raw->hostname);
754 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
755 compat_nfs_string(&real->client_addr, &raw->client_addr);
756 real->acdirmax = raw->acdirmax;
757 real->acdirmin = raw->acdirmin;
758 real->acregmax = raw->acregmax;
759 real->acregmin = raw->acregmin;
760 real->retrans = raw->retrans;
761 real->timeo = raw->timeo;
762 real->wsize = raw->wsize;
763 real->rsize = raw->rsize;
764 real->flags = raw->flags;
765 real->version = raw->version;
771 #define NCPFS_NAME "ncpfs"
772 #define NFS4_NAME "nfs4"
774 asmlinkage long compat_sys_mount(const char __user * dev_name,
775 const char __user * dir_name,
776 const char __user * type, unsigned long flags,
777 const void __user * data)
780 unsigned long data_page;
782 struct filename *dir;
785 retval = copy_mount_string(type, &kernel_type);
789 dir = getname(dir_name);
790 retval = PTR_ERR(dir);
794 retval = copy_mount_string(dev_name, &kernel_dev);
798 retval = copy_mount_options(data, &data_page);
804 if (kernel_type && data_page) {
805 if (!strcmp(kernel_type, NCPFS_NAME)) {
806 do_ncp_super_data_conv((void *)data_page);
807 } else if (!strcmp(kernel_type, NFS4_NAME)) {
808 if (do_nfs4_super_data_conv((void *) data_page))
813 retval = do_mount(kernel_dev, dir->name, kernel_type,
814 flags, (void*)data_page);
817 free_page(data_page);
828 struct compat_old_linux_dirent {
829 compat_ulong_t d_ino;
830 compat_ulong_t d_offset;
831 unsigned short d_namlen;
835 struct compat_readdir_callback {
836 struct compat_old_linux_dirent __user *dirent;
840 static int compat_fillonedir(void *__buf, const char *name, int namlen,
841 loff_t offset, u64 ino, unsigned int d_type)
843 struct compat_readdir_callback *buf = __buf;
844 struct compat_old_linux_dirent __user *dirent;
845 compat_ulong_t d_ino;
850 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
851 buf->result = -EOVERFLOW;
855 dirent = buf->dirent;
856 if (!access_ok(VERIFY_WRITE, dirent,
857 (unsigned long)(dirent->d_name + namlen + 1) -
858 (unsigned long)dirent))
860 if ( __put_user(d_ino, &dirent->d_ino) ||
861 __put_user(offset, &dirent->d_offset) ||
862 __put_user(namlen, &dirent->d_namlen) ||
863 __copy_to_user(dirent->d_name, name, namlen) ||
864 __put_user(0, dirent->d_name + namlen))
868 buf->result = -EFAULT;
872 asmlinkage long compat_sys_old_readdir(unsigned int fd,
873 struct compat_old_linux_dirent __user *dirent, unsigned int count)
876 struct fd f = fdget(fd);
877 struct compat_readdir_callback buf;
885 error = vfs_readdir(f.file, compat_fillonedir, &buf);
893 struct compat_linux_dirent {
894 compat_ulong_t d_ino;
895 compat_ulong_t d_off;
896 unsigned short d_reclen;
900 struct compat_getdents_callback {
901 struct compat_linux_dirent __user *current_dir;
902 struct compat_linux_dirent __user *previous;
907 static int compat_filldir(void *__buf, const char *name, int namlen,
908 loff_t offset, u64 ino, unsigned int d_type)
910 struct compat_linux_dirent __user * dirent;
911 struct compat_getdents_callback *buf = __buf;
912 compat_ulong_t d_ino;
913 int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
914 namlen + 2, sizeof(compat_long_t));
916 buf->error = -EINVAL; /* only used if we fail.. */
917 if (reclen > buf->count)
920 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
921 buf->error = -EOVERFLOW;
924 dirent = buf->previous;
926 if (__put_user(offset, &dirent->d_off))
929 dirent = buf->current_dir;
930 if (__put_user(d_ino, &dirent->d_ino))
932 if (__put_user(reclen, &dirent->d_reclen))
934 if (copy_to_user(dirent->d_name, name, namlen))
936 if (__put_user(0, dirent->d_name + namlen))
938 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
940 buf->previous = dirent;
941 dirent = (void __user *)dirent + reclen;
942 buf->current_dir = dirent;
943 buf->count -= reclen;
946 buf->error = -EFAULT;
950 asmlinkage long compat_sys_getdents(unsigned int fd,
951 struct compat_linux_dirent __user *dirent, unsigned int count)
954 struct compat_linux_dirent __user * lastdirent;
955 struct compat_getdents_callback buf;
958 if (!access_ok(VERIFY_WRITE, dirent, count))
965 buf.current_dir = dirent;
970 error = vfs_readdir(f.file, compat_filldir, &buf);
973 lastdirent = buf.previous;
975 if (put_user(f.file->f_pos, &lastdirent->d_off))
978 error = count - buf.count;
984 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
986 struct compat_getdents_callback64 {
987 struct linux_dirent64 __user *current_dir;
988 struct linux_dirent64 __user *previous;
993 static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
994 u64 ino, unsigned int d_type)
996 struct linux_dirent64 __user *dirent;
997 struct compat_getdents_callback64 *buf = __buf;
998 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
1002 buf->error = -EINVAL; /* only used if we fail.. */
1003 if (reclen > buf->count)
1005 dirent = buf->previous;
1008 if (__put_user_unaligned(offset, &dirent->d_off))
1011 dirent = buf->current_dir;
1012 if (__put_user_unaligned(ino, &dirent->d_ino))
1015 if (__put_user_unaligned(off, &dirent->d_off))
1017 if (__put_user(reclen, &dirent->d_reclen))
1019 if (__put_user(d_type, &dirent->d_type))
1021 if (copy_to_user(dirent->d_name, name, namlen))
1023 if (__put_user(0, dirent->d_name + namlen))
1025 buf->previous = dirent;
1026 dirent = (void __user *)dirent + reclen;
1027 buf->current_dir = dirent;
1028 buf->count -= reclen;
1031 buf->error = -EFAULT;
1035 asmlinkage long compat_sys_getdents64(unsigned int fd,
1036 struct linux_dirent64 __user * dirent, unsigned int count)
1039 struct linux_dirent64 __user * lastdirent;
1040 struct compat_getdents_callback64 buf;
1043 if (!access_ok(VERIFY_WRITE, dirent, count))
1050 buf.current_dir = dirent;
1051 buf.previous = NULL;
1055 error = vfs_readdir(f.file, compat_filldir64, &buf);
1058 lastdirent = buf.previous;
1060 typeof(lastdirent->d_off) d_off = f.file->f_pos;
1061 if (__put_user_unaligned(d_off, &lastdirent->d_off))
1064 error = count - buf.count;
1069 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1071 static ssize_t compat_do_readv_writev(int type, struct file *file,
1072 const struct compat_iovec __user *uvector,
1073 unsigned long nr_segs, loff_t *pos)
1075 compat_ssize_t tot_len;
1076 struct iovec iovstack[UIO_FASTIOV];
1077 struct iovec *iov = iovstack;
1086 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
1087 UIO_FASTIOV, iovstack, &iov);
1092 ret = rw_verify_area(type, file, pos, tot_len);
1098 fn = file->f_op->read;
1099 fnv = file->f_op->aio_read;
1101 fn = (io_fn_t)file->f_op->write;
1102 fnv = file->f_op->aio_write;
1106 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1109 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
1112 if (iov != iovstack)
1114 if ((ret + (type == READ)) > 0) {
1116 fsnotify_access(file);
1118 fsnotify_modify(file);
1123 static size_t compat_readv(struct file *file,
1124 const struct compat_iovec __user *vec,
1125 unsigned long vlen, loff_t *pos)
1127 ssize_t ret = -EBADF;
1129 if (!(file->f_mode & FMODE_READ))
1133 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
1136 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
1140 add_rchar(current, ret);
1146 compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec,
1149 struct fd f = fdget(fd);
1155 pos = f.file->f_pos;
1156 ret = compat_readv(f.file, vec, vlen, &pos);
1157 f.file->f_pos = pos;
1163 compat_sys_preadv64(unsigned long fd, const struct compat_iovec __user *vec,
1164 unsigned long vlen, loff_t pos)
1175 if (f.file->f_mode & FMODE_PREAD)
1176 ret = compat_readv(f.file, vec, vlen, &pos);
1182 compat_sys_preadv(unsigned long fd, const struct compat_iovec __user *vec,
1183 unsigned long vlen, u32 pos_low, u32 pos_high)
1185 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1186 return compat_sys_preadv64(fd, vec, vlen, pos);
1189 static size_t compat_writev(struct file *file,
1190 const struct compat_iovec __user *vec,
1191 unsigned long vlen, loff_t *pos)
1193 ssize_t ret = -EBADF;
1195 if (!(file->f_mode & FMODE_WRITE))
1199 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
1202 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1206 add_wchar(current, ret);
1212 compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec,
1215 struct fd f = fdget(fd);
1221 pos = f.file->f_pos;
1222 ret = compat_writev(f.file, vec, vlen, &pos);
1223 f.file->f_pos = pos;
1229 compat_sys_pwritev64(unsigned long fd, const struct compat_iovec __user *vec,
1230 unsigned long vlen, loff_t pos)
1241 if (f.file->f_mode & FMODE_PWRITE)
1242 ret = compat_writev(f.file, vec, vlen, &pos);
1248 compat_sys_pwritev(unsigned long fd, const struct compat_iovec __user *vec,
1249 unsigned long vlen, u32 pos_low, u32 pos_high)
1251 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1252 return compat_sys_pwritev64(fd, vec, vlen, pos);
1256 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1259 COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1261 return do_sys_open(AT_FDCWD, filename, flags, mode);
1265 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1268 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
1270 return do_sys_open(dfd, filename, flags, mode);
1273 #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1275 static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
1276 int timeval, int ret)
1283 if (current->personality & STICKY_TIMEOUTS)
1286 /* No update for zero timeout */
1287 if (!end_time->tv_sec && !end_time->tv_nsec)
1291 ts = timespec_sub(*end_time, ts);
1293 ts.tv_sec = ts.tv_nsec = 0;
1296 struct compat_timeval rtv;
1298 rtv.tv_sec = ts.tv_sec;
1299 rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
1301 if (!copy_to_user(p, &rtv, sizeof(rtv)))
1304 struct compat_timespec rts;
1306 rts.tv_sec = ts.tv_sec;
1307 rts.tv_nsec = ts.tv_nsec;
1309 if (!copy_to_user(p, &rts, sizeof(rts)))
1313 * If an application puts its timeval in read-only memory, we
1314 * don't want the Linux-specific update to the timeval to
1315 * cause a fault after the select has completed
1316 * successfully. However, because we're not updating the
1317 * timeval, we can't restart the system call.
1321 if (ret == -ERESTARTNOHAND)
1327 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1328 * 64-bit unsigned longs.
1331 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1332 unsigned long *fdset)
1334 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
1338 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1345 if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1348 *fdset++ = h << 32 | l;
1351 if (odd && __get_user(*fdset, ufdset))
1354 /* Tricky, must clear full unsigned long in the
1355 * kernel fdset at the end, this makes sure that
1358 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1364 int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1365 unsigned long *fdset)
1368 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
1379 if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1384 if (odd && __put_user(*fdset, ufdset))
1391 * This is a virtual copy of sys_select from fs/select.c and probably
1392 * should be compared to it from time to time
1396 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1397 * like to be certain this leads to no problems. So I return
1398 * EINTR just for safety.
1400 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1401 * I'm trying ERESTARTNOHAND which restart only when you want to.
1403 int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1404 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1405 struct timespec *end_time)
1409 int size, max_fds, ret = -EINVAL;
1410 struct fdtable *fdt;
1411 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
1416 /* max_fds can increase, so grab it once to avoid race */
1418 fdt = files_fdtable(current->files);
1419 max_fds = fdt->max_fds;
1425 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1426 * since we used fdset we need to allocate memory in units of
1429 size = FDS_BYTES(n);
1431 if (size > sizeof(stack_fds) / 6) {
1432 bits = kmalloc(6 * size, GFP_KERNEL);
1437 fds.in = (unsigned long *) bits;
1438 fds.out = (unsigned long *) (bits + size);
1439 fds.ex = (unsigned long *) (bits + 2*size);
1440 fds.res_in = (unsigned long *) (bits + 3*size);
1441 fds.res_out = (unsigned long *) (bits + 4*size);
1442 fds.res_ex = (unsigned long *) (bits + 5*size);
1444 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1445 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1446 (ret = compat_get_fd_set(n, exp, fds.ex)))
1448 zero_fd_set(n, fds.res_in);
1449 zero_fd_set(n, fds.res_out);
1450 zero_fd_set(n, fds.res_ex);
1452 ret = do_select(n, &fds, end_time);
1457 ret = -ERESTARTNOHAND;
1458 if (signal_pending(current))
1463 if (compat_set_fd_set(n, inp, fds.res_in) ||
1464 compat_set_fd_set(n, outp, fds.res_out) ||
1465 compat_set_fd_set(n, exp, fds.res_ex))
1468 if (bits != stack_fds)
1474 asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1475 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1476 struct compat_timeval __user *tvp)
1478 struct timespec end_time, *to = NULL;
1479 struct compat_timeval tv;
1483 if (copy_from_user(&tv, tvp, sizeof(tv)))
1487 if (poll_select_set_timeout(to,
1488 tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
1489 (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
1493 ret = compat_core_sys_select(n, inp, outp, exp, to);
1494 ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
1499 struct compat_sel_arg_struct {
1507 asmlinkage long compat_sys_old_select(struct compat_sel_arg_struct __user *arg)
1509 struct compat_sel_arg_struct a;
1511 if (copy_from_user(&a, arg, sizeof(a)))
1513 return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1514 compat_ptr(a.exp), compat_ptr(a.tvp));
1517 static long do_compat_pselect(int n, compat_ulong_t __user *inp,
1518 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1519 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1520 compat_size_t sigsetsize)
1522 compat_sigset_t ss32;
1523 sigset_t ksigmask, sigsaved;
1524 struct compat_timespec ts;
1525 struct timespec end_time, *to = NULL;
1529 if (copy_from_user(&ts, tsp, sizeof(ts)))
1533 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1538 if (sigsetsize != sizeof(compat_sigset_t))
1540 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1542 sigset_from_compat(&ksigmask, &ss32);
1544 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1545 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1548 ret = compat_core_sys_select(n, inp, outp, exp, to);
1549 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
1551 if (ret == -ERESTARTNOHAND) {
1553 * Don't restore the signal mask yet. Let do_signal() deliver
1554 * the signal on the way back to userspace, before the signal
1558 memcpy(¤t->saved_sigmask, &sigsaved,
1560 set_restore_sigmask();
1563 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1568 asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1569 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1570 struct compat_timespec __user *tsp, void __user *sig)
1572 compat_size_t sigsetsize = 0;
1573 compat_uptr_t up = 0;
1576 if (!access_ok(VERIFY_READ, sig,
1577 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1578 __get_user(up, (compat_uptr_t __user *)sig) ||
1579 __get_user(sigsetsize,
1580 (compat_size_t __user *)(sig+sizeof(up))))
1583 return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
1587 asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1588 unsigned int nfds, struct compat_timespec __user *tsp,
1589 const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1591 compat_sigset_t ss32;
1592 sigset_t ksigmask, sigsaved;
1593 struct compat_timespec ts;
1594 struct timespec end_time, *to = NULL;
1598 if (copy_from_user(&ts, tsp, sizeof(ts)))
1602 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1607 if (sigsetsize != sizeof(compat_sigset_t))
1609 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1611 sigset_from_compat(&ksigmask, &ss32);
1613 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1614 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1617 ret = do_sys_poll(ufds, nfds, to);
1619 /* We can restart this syscall, usually */
1620 if (ret == -EINTR) {
1622 * Don't restore the signal mask yet. Let do_signal() deliver
1623 * the signal on the way back to userspace, before the signal
1627 memcpy(¤t->saved_sigmask, &sigsaved,
1629 set_restore_sigmask();
1631 ret = -ERESTARTNOHAND;
1633 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1635 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
1640 #ifdef CONFIG_FHANDLE
1642 * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
1643 * doesn't set the O_LARGEFILE flag.
1645 COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
1646 struct file_handle __user *, handle, int, flags)
1648 return do_handle_open(mountdirfd, handle, flags);