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@suse.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/linkage.h>
19 #include <linux/compat.h>
20 #include <linux/errno.h>
21 #include <linux/time.h>
23 #include <linux/fcntl.h>
24 #include <linux/namei.h>
25 #include <linux/file.h>
26 #include <linux/vfs.h>
27 #include <linux/ioctl32.h>
28 #include <linux/ioctl.h>
29 #include <linux/init.h>
30 #include <linux/sockios.h> /* for SIOCDEVPRIVATE */
31 #include <linux/smb.h>
32 #include <linux/smb_mount.h>
33 #include <linux/ncp_mount.h>
34 #include <linux/nfs4_mount.h>
35 #include <linux/smp_lock.h>
36 #include <linux/syscalls.h>
37 #include <linux/ctype.h>
38 #include <linux/module.h>
39 #include <linux/dirent.h>
40 #include <linux/fsnotify.h>
41 #include <linux/highuid.h>
42 #include <linux/sunrpc/svc.h>
43 #include <linux/nfsd/nfsd.h>
44 #include <linux/nfsd/syscall.h>
45 #include <linux/personality.h>
46 #include <linux/rwsem.h>
47 #include <linux/acct.h>
50 #include <net/sock.h> /* siocdevprivate_ioctl */
52 #include <asm/uaccess.h>
53 #include <asm/mmu_context.h>
54 #include <asm/ioctls.h>
56 extern void sigset_from_compat(sigset_t *set, compat_sigset_t *compat);
59 * Not all architectures have sys_utime, so implement this in terms
62 asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
67 if (get_user(tv[0].tv_sec, &t->actime) ||
68 get_user(tv[1].tv_sec, &t->modtime))
73 return do_utimes(AT_FDCWD, filename, t ? tv : NULL);
76 asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t)
81 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
82 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
83 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
84 get_user(tv[1].tv_usec, &t[1].tv_usec))
87 return do_utimes(dfd, filename, t ? tv : NULL);
90 asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
92 return compat_sys_futimesat(AT_FDCWD, filename, t);
95 asmlinkage long compat_sys_newstat(char __user * filename,
96 struct compat_stat __user *statbuf)
99 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
102 error = cp_compat_stat(&stat, statbuf);
106 asmlinkage long compat_sys_newlstat(char __user * filename,
107 struct compat_stat __user *statbuf)
110 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
113 error = cp_compat_stat(&stat, statbuf);
117 #ifndef __ARCH_WANT_STAT64
118 asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user *filename,
119 struct compat_stat __user *statbuf, int flag)
124 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
127 if (flag & AT_SYMLINK_NOFOLLOW)
128 error = vfs_lstat_fd(dfd, filename, &stat);
130 error = vfs_stat_fd(dfd, filename, &stat);
133 error = cp_compat_stat(&stat, statbuf);
140 asmlinkage long compat_sys_newfstat(unsigned int fd,
141 struct compat_stat __user * statbuf)
144 int error = vfs_fstat(fd, &stat);
147 error = cp_compat_stat(&stat, statbuf);
151 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
154 if (sizeof ubuf->f_blocks == 4) {
155 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
156 0xffffffff00000000ULL)
158 /* f_files and f_ffree may be -1; it's okay
159 * to stuff that into 32 bits */
160 if (kbuf->f_files != 0xffffffffffffffffULL
161 && (kbuf->f_files & 0xffffffff00000000ULL))
163 if (kbuf->f_ffree != 0xffffffffffffffffULL
164 && (kbuf->f_ffree & 0xffffffff00000000ULL))
167 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
168 __put_user(kbuf->f_type, &ubuf->f_type) ||
169 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
170 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
171 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
172 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
173 __put_user(kbuf->f_files, &ubuf->f_files) ||
174 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
175 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
176 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
177 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
178 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
179 __put_user(0, &ubuf->f_spare[0]) ||
180 __put_user(0, &ubuf->f_spare[1]) ||
181 __put_user(0, &ubuf->f_spare[2]) ||
182 __put_user(0, &ubuf->f_spare[3]) ||
183 __put_user(0, &ubuf->f_spare[4]))
189 * The following statfs calls are copies of code from fs/open.c and
190 * should be checked against those from time to time
192 asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
197 error = user_path_walk(path, &nd);
200 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
202 error = put_compat_statfs(buf, &tmp);
208 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
218 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
220 error = put_compat_statfs(buf, &tmp);
226 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
228 if (sizeof ubuf->f_blocks == 4) {
229 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
230 0xffffffff00000000ULL)
232 /* f_files and f_ffree may be -1; it's okay
233 * to stuff that into 32 bits */
234 if (kbuf->f_files != 0xffffffffffffffffULL
235 && (kbuf->f_files & 0xffffffff00000000ULL))
237 if (kbuf->f_ffree != 0xffffffffffffffffULL
238 && (kbuf->f_ffree & 0xffffffff00000000ULL))
241 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
242 __put_user(kbuf->f_type, &ubuf->f_type) ||
243 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
244 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
245 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
246 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
247 __put_user(kbuf->f_files, &ubuf->f_files) ||
248 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
249 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
250 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
251 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
252 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
257 asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
262 if (sz != sizeof(*buf))
265 error = user_path_walk(path, &nd);
268 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
270 error = put_compat_statfs64(buf, &tmp);
276 asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
282 if (sz != sizeof(*buf))
289 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
291 error = put_compat_statfs64(buf, &tmp);
297 /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
299 #define IOCTL_HASHSIZE 256
300 static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
302 extern struct ioctl_trans ioctl_start[];
303 extern int ioctl_table_size;
305 static inline unsigned long ioctl32_hash(unsigned long cmd)
307 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
310 static void ioctl32_insert_translation(struct ioctl_trans *trans)
313 struct ioctl_trans *t;
315 hash = ioctl32_hash (trans->cmd);
316 if (!ioctl32_hash_table[hash])
317 ioctl32_hash_table[hash] = trans;
319 t = ioctl32_hash_table[hash];
327 static int __init init_sys32_ioctl(void)
331 for (i = 0; i < ioctl_table_size; i++) {
332 if (ioctl_start[i].next != 0) {
333 printk("ioctl translation %d bad\n",i);
337 ioctl32_insert_translation(&ioctl_start[i]);
342 __initcall(init_sys32_ioctl);
344 static void compat_ioctl_error(struct file *filp, unsigned int fd,
345 unsigned int cmd, unsigned long arg)
351 /* find the name of the device. */
352 path = (char *)__get_free_page(GFP_KERNEL);
354 fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE);
359 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
360 if (!isprint(buf[1]))
361 sprintf(buf, "%02x", buf[1]);
362 printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
363 "cmd(%08x){%s} arg(%08x) on %s\n",
364 current->comm, current->pid,
365 (int)fd, (unsigned int)cmd, buf,
366 (unsigned int)arg, fn);
369 free_page((unsigned long)path);
372 asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
377 struct ioctl_trans *t;
380 filp = fget_light(fd, &fput_needed);
384 /* RED-PEN how should LSM module know it's handling 32bit? */
385 error = security_file_ioctl(filp, cmd, arg);
390 * To allow the compat_ioctl handlers to be self contained
391 * we need to check the common ioctls here first.
392 * Just handle them with the standard handlers below.
405 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
410 if (filp->f_op && filp->f_op->compat_ioctl) {
411 error = filp->f_op->compat_ioctl(filp, cmd, arg);
412 if (error != -ENOIOCTLCMD)
417 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
422 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
427 if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) &&
428 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
429 error = siocdevprivate_ioctl(fd, cmd, arg);
434 compat_ioctl_error(filp, fd, cmd, arg);
443 error = t->handler(fd, cmd, arg, filp);
449 error = vfs_ioctl(filp, fd, cmd, arg);
451 fput_light(filp, fput_needed);
456 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
458 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
459 __get_user(kfl->l_type, &ufl->l_type) ||
460 __get_user(kfl->l_whence, &ufl->l_whence) ||
461 __get_user(kfl->l_start, &ufl->l_start) ||
462 __get_user(kfl->l_len, &ufl->l_len) ||
463 __get_user(kfl->l_pid, &ufl->l_pid))
468 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
470 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
471 __put_user(kfl->l_type, &ufl->l_type) ||
472 __put_user(kfl->l_whence, &ufl->l_whence) ||
473 __put_user(kfl->l_start, &ufl->l_start) ||
474 __put_user(kfl->l_len, &ufl->l_len) ||
475 __put_user(kfl->l_pid, &ufl->l_pid))
480 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
481 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
483 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
484 __get_user(kfl->l_type, &ufl->l_type) ||
485 __get_user(kfl->l_whence, &ufl->l_whence) ||
486 __get_user(kfl->l_start, &ufl->l_start) ||
487 __get_user(kfl->l_len, &ufl->l_len) ||
488 __get_user(kfl->l_pid, &ufl->l_pid))
494 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
495 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
497 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
498 __put_user(kfl->l_type, &ufl->l_type) ||
499 __put_user(kfl->l_whence, &ufl->l_whence) ||
500 __put_user(kfl->l_start, &ufl->l_start) ||
501 __put_user(kfl->l_len, &ufl->l_len) ||
502 __put_user(kfl->l_pid, &ufl->l_pid))
508 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
519 ret = get_compat_flock(&f, compat_ptr(arg));
524 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
526 if (cmd == F_GETLK && ret == 0) {
527 /* GETLK was successfule and we need to return the data...
528 * but it needs to fit in the compat structure.
529 * l_start shouldn't be too big, unless the original
530 * start + end is greater than COMPAT_OFF_T_MAX, in which
531 * case the app was asking for trouble, so we return
532 * -EOVERFLOW in that case.
533 * l_len could be too big, in which case we just truncate it,
534 * and only allow the app to see that part of the conflicting
535 * lock that might make sense to it anyway
538 if (f.l_start > COMPAT_OFF_T_MAX)
540 if (f.l_len > COMPAT_OFF_T_MAX)
541 f.l_len = COMPAT_OFF_T_MAX;
543 ret = put_compat_flock(&f, compat_ptr(arg));
550 ret = get_compat_flock64(&f, compat_ptr(arg));
555 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
556 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
559 if (cmd == F_GETLK64 && ret == 0) {
560 /* need to return lock information - see above for commentary */
561 if (f.l_start > COMPAT_LOFF_T_MAX)
563 if (f.l_len > COMPAT_LOFF_T_MAX)
564 f.l_len = COMPAT_LOFF_T_MAX;
566 ret = put_compat_flock64(&f, compat_ptr(arg));
571 ret = sys_fcntl(fd, cmd, arg);
577 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
580 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
582 return compat_sys_fcntl64(fd, cmd, arg);
586 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
591 mm_segment_t oldfs = get_fs();
592 if (unlikely(get_user(ctx64, ctx32p)))
596 /* The __user pointer cast is valid because of the set_fs() */
597 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
599 /* truncating is ok because it's a user address */
601 ret = put_user((u32) ctx64, ctx32p);
606 compat_sys_io_getevents(aio_context_t ctx_id,
607 unsigned long min_nr,
609 struct io_event __user *events,
610 struct compat_timespec __user *timeout)
614 struct timespec __user *ut = NULL;
617 if (unlikely(!access_ok(VERIFY_WRITE, events,
618 nr * sizeof(struct io_event))))
621 if (get_compat_timespec(&t, timeout))
624 ut = compat_alloc_user_space(sizeof(*ut));
625 if (copy_to_user(ut, &t, sizeof(t)) )
628 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
634 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
639 for (i = 0; i < nr; ++i) {
640 if (get_user(uptr, ptr32 + i))
642 if (put_user(compat_ptr(uptr), ptr64 + i))
648 #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
651 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
653 struct iocb __user * __user *iocb64;
656 if (unlikely(nr < 0))
659 if (nr > MAX_AIO_SUBMITS)
660 nr = MAX_AIO_SUBMITS;
662 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
663 ret = copy_iocb(nr, iocb, iocb64);
665 ret = sys_io_submit(ctx_id, nr, iocb64);
669 struct compat_ncp_mount_data {
670 compat_int_t version;
671 compat_uint_t ncp_fd;
672 __compat_uid_t mounted_uid;
673 compat_pid_t wdog_pid;
674 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
675 compat_uint_t time_out;
676 compat_uint_t retry_count;
680 compat_mode_t file_mode;
681 compat_mode_t dir_mode;
684 struct compat_ncp_mount_data_v4 {
685 compat_int_t version;
686 compat_ulong_t flags;
687 compat_ulong_t mounted_uid;
688 compat_long_t wdog_pid;
689 compat_uint_t ncp_fd;
690 compat_uint_t time_out;
691 compat_uint_t retry_count;
694 compat_ulong_t file_mode;
695 compat_ulong_t dir_mode;
698 static void *do_ncp_super_data_conv(void *raw_data)
700 int version = *(unsigned int *)raw_data;
703 struct compat_ncp_mount_data *c_n = raw_data;
704 struct ncp_mount_data *n = raw_data;
706 n->dir_mode = c_n->dir_mode;
707 n->file_mode = c_n->file_mode;
710 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
711 n->wdog_pid = c_n->wdog_pid;
712 n->mounted_uid = c_n->mounted_uid;
713 } else if (version == 4) {
714 struct compat_ncp_mount_data_v4 *c_n = raw_data;
715 struct ncp_mount_data_v4 *n = raw_data;
717 n->dir_mode = c_n->dir_mode;
718 n->file_mode = c_n->file_mode;
721 n->retry_count = c_n->retry_count;
722 n->time_out = c_n->time_out;
723 n->ncp_fd = c_n->ncp_fd;
724 n->wdog_pid = c_n->wdog_pid;
725 n->mounted_uid = c_n->mounted_uid;
726 n->flags = c_n->flags;
727 } else if (version != 5) {
734 struct compat_smb_mount_data {
735 compat_int_t version;
736 __compat_uid_t mounted_uid;
739 compat_mode_t file_mode;
740 compat_mode_t dir_mode;
743 static void *do_smb_super_data_conv(void *raw_data)
745 struct smb_mount_data *s = raw_data;
746 struct compat_smb_mount_data *c_s = raw_data;
748 if (c_s->version != SMB_MOUNT_OLDVERSION)
750 s->dir_mode = c_s->dir_mode;
751 s->file_mode = c_s->file_mode;
754 s->mounted_uid = c_s->mounted_uid;
759 struct compat_nfs_string {
764 static inline void compat_nfs_string(struct nfs_string *dst,
765 struct compat_nfs_string *src)
767 dst->data = compat_ptr(src->data);
771 struct compat_nfs4_mount_data_v1 {
772 compat_int_t version;
777 compat_int_t retrans;
778 compat_int_t acregmin;
779 compat_int_t acregmax;
780 compat_int_t acdirmin;
781 compat_int_t acdirmax;
782 struct compat_nfs_string client_addr;
783 struct compat_nfs_string mnt_path;
784 struct compat_nfs_string hostname;
785 compat_uint_t host_addrlen;
786 compat_uptr_t host_addr;
788 compat_int_t auth_flavourlen;
789 compat_uptr_t auth_flavours;
792 static int do_nfs4_super_data_conv(void *raw_data)
794 int version = *(compat_uint_t *) raw_data;
797 struct compat_nfs4_mount_data_v1 *raw = raw_data;
798 struct nfs4_mount_data *real = raw_data;
800 /* copy the fields backwards */
801 real->auth_flavours = compat_ptr(raw->auth_flavours);
802 real->auth_flavourlen = raw->auth_flavourlen;
803 real->proto = raw->proto;
804 real->host_addr = compat_ptr(raw->host_addr);
805 real->host_addrlen = raw->host_addrlen;
806 compat_nfs_string(&real->hostname, &raw->hostname);
807 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
808 compat_nfs_string(&real->client_addr, &raw->client_addr);
809 real->acdirmax = raw->acdirmax;
810 real->acdirmin = raw->acdirmin;
811 real->acregmax = raw->acregmax;
812 real->acregmin = raw->acregmin;
813 real->retrans = raw->retrans;
814 real->timeo = raw->timeo;
815 real->wsize = raw->wsize;
816 real->rsize = raw->rsize;
817 real->flags = raw->flags;
818 real->version = raw->version;
827 extern int copy_mount_options (const void __user *, unsigned long *);
829 #define SMBFS_NAME "smbfs"
830 #define NCPFS_NAME "ncpfs"
831 #define NFS4_NAME "nfs4"
833 asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
834 char __user * type, unsigned long flags,
837 unsigned long type_page;
838 unsigned long data_page;
839 unsigned long dev_page;
843 retval = copy_mount_options (type, &type_page);
847 dir_page = getname(dir_name);
848 retval = PTR_ERR(dir_page);
849 if (IS_ERR(dir_page))
852 retval = copy_mount_options (dev_name, &dev_page);
856 retval = copy_mount_options (data, &data_page);
863 if (!strcmp((char *)type_page, SMBFS_NAME)) {
864 do_smb_super_data_conv((void *)data_page);
865 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
866 do_ncp_super_data_conv((void *)data_page);
867 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
868 if (do_nfs4_super_data_conv((void *) data_page))
874 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
875 flags, (void*)data_page);
879 free_page(data_page);
885 free_page(type_page);
890 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
891 #define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
892 ~(sizeof(compat_long_t)-1))
894 struct compat_old_linux_dirent {
895 compat_ulong_t d_ino;
896 compat_ulong_t d_offset;
897 unsigned short d_namlen;
901 struct compat_readdir_callback {
902 struct compat_old_linux_dirent __user *dirent;
906 static int compat_fillonedir(void *__buf, const char *name, int namlen,
907 loff_t offset, ino_t ino, unsigned int d_type)
909 struct compat_readdir_callback *buf = __buf;
910 struct compat_old_linux_dirent __user *dirent;
915 dirent = buf->dirent;
916 if (!access_ok(VERIFY_WRITE, dirent,
917 (unsigned long)(dirent->d_name + namlen + 1) -
918 (unsigned long)dirent))
920 if ( __put_user(ino, &dirent->d_ino) ||
921 __put_user(offset, &dirent->d_offset) ||
922 __put_user(namlen, &dirent->d_namlen) ||
923 __copy_to_user(dirent->d_name, name, namlen) ||
924 __put_user(0, dirent->d_name + namlen))
928 buf->result = -EFAULT;
932 asmlinkage long compat_sys_old_readdir(unsigned int fd,
933 struct compat_old_linux_dirent __user *dirent, unsigned int count)
937 struct compat_readdir_callback buf;
947 error = vfs_readdir(file, compat_fillonedir, &buf);
956 struct compat_linux_dirent {
957 compat_ulong_t d_ino;
958 compat_ulong_t d_off;
959 unsigned short d_reclen;
963 struct compat_getdents_callback {
964 struct compat_linux_dirent __user *current_dir;
965 struct compat_linux_dirent __user *previous;
970 static int compat_filldir(void *__buf, const char *name, int namlen,
971 loff_t offset, ino_t ino, unsigned int d_type)
973 struct compat_linux_dirent __user * dirent;
974 struct compat_getdents_callback *buf = __buf;
975 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
977 buf->error = -EINVAL; /* only used if we fail.. */
978 if (reclen > buf->count)
980 dirent = buf->previous;
982 if (__put_user(offset, &dirent->d_off))
985 dirent = buf->current_dir;
986 if (__put_user(ino, &dirent->d_ino))
988 if (__put_user(reclen, &dirent->d_reclen))
990 if (copy_to_user(dirent->d_name, name, namlen))
992 if (__put_user(0, dirent->d_name + namlen))
994 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
996 buf->previous = dirent;
997 dirent = (void __user *)dirent + reclen;
998 buf->current_dir = dirent;
999 buf->count -= reclen;
1002 buf->error = -EFAULT;
1006 asmlinkage long compat_sys_getdents(unsigned int fd,
1007 struct compat_linux_dirent __user *dirent, unsigned int count)
1010 struct compat_linux_dirent __user * lastdirent;
1011 struct compat_getdents_callback buf;
1015 if (!access_ok(VERIFY_WRITE, dirent, count))
1023 buf.current_dir = dirent;
1024 buf.previous = NULL;
1028 error = vfs_readdir(file, compat_filldir, &buf);
1032 lastdirent = buf.previous;
1034 if (put_user(file->f_pos, &lastdirent->d_off))
1037 error = count - buf.count;
1046 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1047 #define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1049 struct compat_getdents_callback64 {
1050 struct linux_dirent64 __user *current_dir;
1051 struct linux_dirent64 __user *previous;
1056 static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1057 ino_t ino, unsigned int d_type)
1059 struct linux_dirent64 __user *dirent;
1060 struct compat_getdents_callback64 *buf = __buf;
1061 int jj = NAME_OFFSET(dirent);
1062 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1065 buf->error = -EINVAL; /* only used if we fail.. */
1066 if (reclen > buf->count)
1068 dirent = buf->previous;
1071 if (__put_user_unaligned(offset, &dirent->d_off))
1074 dirent = buf->current_dir;
1075 if (__put_user_unaligned(ino, &dirent->d_ino))
1078 if (__put_user_unaligned(off, &dirent->d_off))
1080 if (__put_user(reclen, &dirent->d_reclen))
1082 if (__put_user(d_type, &dirent->d_type))
1084 if (copy_to_user(dirent->d_name, name, namlen))
1086 if (__put_user(0, dirent->d_name + namlen))
1088 buf->previous = dirent;
1089 dirent = (void __user *)dirent + reclen;
1090 buf->current_dir = dirent;
1091 buf->count -= reclen;
1094 buf->error = -EFAULT;
1098 asmlinkage long compat_sys_getdents64(unsigned int fd,
1099 struct linux_dirent64 __user * dirent, unsigned int count)
1102 struct linux_dirent64 __user * lastdirent;
1103 struct compat_getdents_callback64 buf;
1107 if (!access_ok(VERIFY_WRITE, dirent, count))
1115 buf.current_dir = dirent;
1116 buf.previous = NULL;
1120 error = vfs_readdir(file, compat_filldir64, &buf);
1124 lastdirent = buf.previous;
1126 typeof(lastdirent->d_off) d_off = file->f_pos;
1127 __put_user_unaligned(d_off, &lastdirent->d_off);
1128 error = count - buf.count;
1136 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1138 static ssize_t compat_do_readv_writev(int type, struct file *file,
1139 const struct compat_iovec __user *uvector,
1140 unsigned long nr_segs, loff_t *pos)
1142 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
1143 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
1145 compat_ssize_t tot_len;
1146 struct iovec iovstack[UIO_FASTIOV];
1147 struct iovec *iov=iovstack, *vector;
1154 * SuS says "The readv() function *may* fail if the iovcnt argument
1155 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1156 * traditionally returned zero for zero segments, so...
1163 * First get the "struct iovec" from user memory and
1164 * verify all the pointers
1167 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1171 if (nr_segs > UIO_FASTIOV) {
1173 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1178 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1182 * Single unix specification:
1183 * We should -EINVAL if an element length is not >= 0 and fitting an
1184 * ssize_t. The total length is fitting an ssize_t
1186 * Be careful here because iov_len is a size_t not an ssize_t
1191 for (seg = 0 ; seg < nr_segs; seg++) {
1192 compat_ssize_t tmp = tot_len;
1196 if (__get_user(len, &uvector->iov_len) ||
1197 __get_user(buf, &uvector->iov_base)) {
1201 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1204 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1206 vector->iov_base = compat_ptr(buf);
1207 vector->iov_len = (compat_size_t) len;
1216 ret = rw_verify_area(type, file, pos, tot_len);
1222 fn = file->f_op->read;
1223 fnv = file->f_op->readv;
1225 fn = (io_fn_t)file->f_op->write;
1226 fnv = file->f_op->writev;
1229 ret = fnv(file, iov, nr_segs, pos);
1233 /* Do it by hand, with file-ops */
1236 while (nr_segs > 0) {
1241 base = vector->iov_base;
1242 len = vector->iov_len;
1246 nr = fn(file, base, len, pos);
1257 if (iov != iovstack)
1259 if ((ret + (type == READ)) > 0) {
1260 struct dentry *dentry = file->f_dentry;
1262 fsnotify_access(dentry);
1264 fsnotify_modify(dentry);
1270 compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1273 ssize_t ret = -EBADF;
1279 if (!(file->f_mode & FMODE_READ))
1283 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
1286 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1294 compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1297 ssize_t ret = -EBADF;
1302 if (!(file->f_mode & FMODE_WRITE))
1306 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
1309 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1317 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1321 compat_sys_open(const char __user *filename, int flags, int mode)
1323 return do_sys_open(AT_FDCWD, filename, flags, mode);
1327 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1331 compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
1333 return do_sys_open(dfd, filename, flags, mode);
1337 * compat_count() counts the number of arguments/envelopes. It is basically
1338 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1339 * and envp pointers.
1341 static int compat_count(compat_uptr_t __user *argv, int max)
1349 if (get_user(p, argv))
1362 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1363 * except that it works with 32 bit argv and envp pointers.
1365 static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1366 struct linux_binprm *bprm)
1368 struct page *kmapped_page = NULL;
1372 while (argc-- > 0) {
1377 if (get_user(str, argv+argc) ||
1378 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1383 if (bprm->p < len) {
1389 /* XXX: add architecture specific overflow check here. */
1394 int offset, bytes_to_copy;
1397 offset = pos % PAGE_SIZE;
1399 page = bprm->page[i];
1402 page = alloc_page(GFP_HIGHUSER);
1403 bprm->page[i] = page;
1411 if (page != kmapped_page) {
1413 kunmap(kmapped_page);
1414 kmapped_page = page;
1415 kaddr = kmap(kmapped_page);
1418 memset(kaddr, 0, offset);
1419 bytes_to_copy = PAGE_SIZE - offset;
1420 if (bytes_to_copy > len) {
1421 bytes_to_copy = len;
1423 memset(kaddr+offset+len, 0,
1424 PAGE_SIZE-offset-len);
1426 err = copy_from_user(kaddr+offset, compat_ptr(str),
1433 pos += bytes_to_copy;
1434 str += bytes_to_copy;
1435 len -= bytes_to_copy;
1441 kunmap(kmapped_page);
1447 #define free_arg_pages(bprm) do { } while (0)
1451 static inline void free_arg_pages(struct linux_binprm *bprm)
1455 for (i = 0; i < MAX_ARG_PAGES; i++) {
1457 __free_page(bprm->page[i]);
1458 bprm->page[i] = NULL;
1462 #endif /* CONFIG_MMU */
1465 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1466 * that it processes 32 bit argv and envp pointers.
1468 int compat_do_execve(char * filename,
1469 compat_uptr_t __user *argv,
1470 compat_uptr_t __user *envp,
1471 struct pt_regs * regs)
1473 struct linux_binprm *bprm;
1479 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1482 memset(bprm, 0, sizeof(*bprm));
1484 file = open_exec(filename);
1485 retval = PTR_ERR(file);
1491 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1493 bprm->filename = filename;
1494 bprm->interp = filename;
1495 bprm->mm = mm_alloc();
1500 retval = init_new_context(current, bprm->mm);
1504 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1505 if ((retval = bprm->argc) < 0)
1508 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1509 if ((retval = bprm->envc) < 0)
1512 retval = security_bprm_alloc(bprm);
1516 retval = prepare_binprm(bprm);
1520 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1524 bprm->exec = bprm->p;
1525 retval = compat_copy_strings(bprm->envc, envp, bprm);
1529 retval = compat_copy_strings(bprm->argc, argv, bprm);
1533 retval = search_binary_handler(bprm, regs);
1535 free_arg_pages(bprm);
1537 /* execve success */
1538 security_bprm_free(bprm);
1539 acct_update_integrals(current);
1545 /* Something went wrong, return the inode and free the argument pages*/
1546 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1547 struct page * page = bprm->page[i];
1553 security_bprm_free(bprm);
1561 allow_write_access(bprm->file);
1572 #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1574 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1577 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1578 * 64-bit unsigned longs.
1581 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1582 unsigned long *fdset)
1584 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1588 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1595 __get_user(l, ufdset);
1596 __get_user(h, ufdset+1);
1598 *fdset++ = h << 32 | l;
1602 __get_user(*fdset, ufdset);
1604 /* Tricky, must clear full unsigned long in the
1605 * kernel fdset at the end, this makes sure that
1608 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1614 void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1615 unsigned long *fdset)
1618 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1629 __put_user(l, ufdset);
1630 __put_user(h, ufdset+1);
1635 __put_user(*fdset, ufdset);
1640 * This is a virtual copy of sys_select from fs/select.c and probably
1641 * should be compared to it from time to time
1643 static void *select_bits_alloc(int size)
1645 return kmalloc(6 * size, GFP_KERNEL);
1648 static void select_bits_free(void *bits, int size)
1654 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1655 * like to be certain this leads to no problems. So I return
1656 * EINTR just for safety.
1658 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1659 * I'm trying ERESTARTNOHAND which restart only when you want to.
1661 #define MAX_SELECT_SECONDS \
1662 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1664 int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1665 compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout)
1669 int size, max_fdset, ret = -EINVAL;
1670 struct fdtable *fdt;
1675 /* max_fdset can increase, so grab it once to avoid race */
1677 fdt = files_fdtable(current->files);
1678 max_fdset = fdt->max_fdset;
1684 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1685 * since we used fdset we need to allocate memory in units of
1689 size = FDS_BYTES(n);
1690 bits = select_bits_alloc(size);
1693 fds.in = (unsigned long *) bits;
1694 fds.out = (unsigned long *) (bits + size);
1695 fds.ex = (unsigned long *) (bits + 2*size);
1696 fds.res_in = (unsigned long *) (bits + 3*size);
1697 fds.res_out = (unsigned long *) (bits + 4*size);
1698 fds.res_ex = (unsigned long *) (bits + 5*size);
1700 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1701 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1702 (ret = compat_get_fd_set(n, exp, fds.ex)))
1704 zero_fd_set(n, fds.res_in);
1705 zero_fd_set(n, fds.res_out);
1706 zero_fd_set(n, fds.res_ex);
1708 ret = do_select(n, &fds, timeout);
1713 ret = -ERESTARTNOHAND;
1714 if (signal_pending(current))
1719 compat_set_fd_set(n, inp, fds.res_in);
1720 compat_set_fd_set(n, outp, fds.res_out);
1721 compat_set_fd_set(n, exp, fds.res_ex);
1724 select_bits_free(bits, size);
1729 asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1730 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1731 struct compat_timeval __user *tvp)
1734 struct compat_timeval tv;
1738 if (copy_from_user(&tv, tvp, sizeof(tv)))
1741 if (tv.tv_sec < 0 || tv.tv_usec < 0)
1744 /* Cast to u64 to make GCC stop complaining */
1745 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
1746 timeout = -1; /* infinite */
1748 timeout = ROUND_UP(tv.tv_usec, 1000000/HZ);
1749 timeout += tv.tv_sec * HZ;
1753 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1756 struct compat_timeval rtv;
1758 if (current->personality & STICKY_TIMEOUTS)
1760 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
1761 rtv.tv_sec = timeout;
1762 if (compat_timeval_compare(&rtv, &tv) >= 0)
1764 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
1767 * If an application puts its timeval in read-only
1768 * memory, we don't want the Linux-specific update to
1769 * the timeval to cause a fault after the select has
1770 * completed successfully. However, because we're not
1771 * updating the timeval, we can't restart the system
1774 if (ret == -ERESTARTNOHAND)
1782 #ifdef TIF_RESTORE_SIGMASK
1783 asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
1784 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1785 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1786 compat_size_t sigsetsize)
1788 compat_sigset_t ss32;
1789 sigset_t ksigmask, sigsaved;
1790 s64 timeout = MAX_SCHEDULE_TIMEOUT;
1791 struct compat_timespec ts;
1795 if (copy_from_user(&ts, tsp, sizeof(ts)))
1798 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
1803 if (sigsetsize != sizeof(compat_sigset_t))
1805 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1807 sigset_from_compat(&ksigmask, &ss32);
1809 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1810 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1815 if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) {
1816 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
1817 timeout += ts.tv_sec * (unsigned long)HZ;
1821 ts.tv_sec -= MAX_SELECT_SECONDS;
1822 timeout = MAX_SELECT_SECONDS * HZ;
1826 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1828 } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec));
1830 if (tsp && !(current->personality & STICKY_TIMEOUTS)) {
1831 struct compat_timespec rts;
1833 rts.tv_sec = timeout / HZ;
1834 rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
1835 if (rts.tv_nsec >= NSEC_PER_SEC) {
1837 rts.tv_nsec -= NSEC_PER_SEC;
1839 if (compat_timespec_compare(&rts, &ts) >= 0)
1841 copy_to_user(tsp, &rts, sizeof(rts));
1844 if (ret == -ERESTARTNOHAND) {
1846 * Don't restore the signal mask yet. Let do_signal() deliver
1847 * the signal on the way back to userspace, before the signal
1851 memcpy(¤t->saved_sigmask, &sigsaved,
1853 set_thread_flag(TIF_RESTORE_SIGMASK);
1856 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1861 asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1862 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1863 struct compat_timespec __user *tsp, void __user *sig)
1865 compat_size_t sigsetsize = 0;
1866 compat_uptr_t up = 0;
1869 if (!access_ok(VERIFY_READ, sig,
1870 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1871 __get_user(up, (compat_uptr_t __user *)sig) ||
1872 __get_user(sigsetsize,
1873 (compat_size_t __user *)(sig+sizeof(up))))
1876 return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up),
1880 asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1881 unsigned int nfds, struct compat_timespec __user *tsp,
1882 const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1884 compat_sigset_t ss32;
1885 sigset_t ksigmask, sigsaved;
1886 struct compat_timespec ts;
1891 if (copy_from_user(&ts, tsp, sizeof(ts)))
1894 /* We assume that ts.tv_sec is always lower than
1895 the number of seconds that can be expressed in
1896 an s64. Otherwise the compiler bitches at us */
1897 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
1898 timeout += ts.tv_sec * HZ;
1902 if (sigsetsize |= sizeof(compat_sigset_t))
1904 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1906 sigset_from_compat(&ksigmask, &ss32);
1908 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1909 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1912 ret = do_sys_poll(ufds, nfds, &timeout);
1914 /* We can restart this syscall, usually */
1915 if (ret == -EINTR) {
1917 * Don't restore the signal mask yet. Let do_signal() deliver
1918 * the signal on the way back to userspace, before the signal
1922 memcpy(¤t->saved_sigmask, &sigsaved,
1924 set_thread_flag(TIF_RESTORE_SIGMASK);
1926 ret = -ERESTARTNOHAND;
1928 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1930 if (tsp && timeout >= 0) {
1931 struct compat_timespec rts;
1933 if (current->personality & STICKY_TIMEOUTS)
1935 /* Yes, we know it's actually an s64, but it's also positive. */
1936 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
1938 rts.tv_sec = timeout;
1939 if (compat_timespec_compare(&rts, &ts) >= 0)
1941 if (copy_to_user(tsp, &rts, sizeof(rts))) {
1944 * If an application puts its timeval in read-only
1945 * memory, we don't want the Linux-specific update to
1946 * the timeval to cause a fault after the select has
1947 * completed successfully. However, because we're not
1948 * updating the timeval, we can't restart the system
1951 if (ret == -ERESTARTNOHAND && timeout >= 0)
1958 #endif /* TIF_RESTORE_SIGMASK */
1960 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1961 /* Stuff for NFS server syscalls... */
1962 struct compat_nfsctl_svc {
1967 struct compat_nfsctl_client {
1968 s8 cl32_ident[NFSCLNT_IDMAX+1];
1970 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1973 u8 cl32_fhkey[NFSCLNT_KEYMAX];
1976 struct compat_nfsctl_export {
1977 char ex32_client[NFSCLNT_IDMAX+1];
1978 char ex32_path[NFS_MAXPATHLEN+1];
1979 compat_dev_t ex32_dev;
1980 compat_ino_t ex32_ino;
1981 compat_int_t ex32_flags;
1982 __compat_uid_t ex32_anon_uid;
1983 __compat_gid_t ex32_anon_gid;
1986 struct compat_nfsctl_fdparm {
1987 struct sockaddr gd32_addr;
1988 s8 gd32_path[NFS_MAXPATHLEN+1];
1989 compat_int_t gd32_version;
1992 struct compat_nfsctl_fsparm {
1993 struct sockaddr gd32_addr;
1994 s8 gd32_path[NFS_MAXPATHLEN+1];
1995 compat_int_t gd32_maxlen;
1998 struct compat_nfsctl_arg {
1999 compat_int_t ca32_version; /* safeguard */
2001 struct compat_nfsctl_svc u32_svc;
2002 struct compat_nfsctl_client u32_client;
2003 struct compat_nfsctl_export u32_export;
2004 struct compat_nfsctl_fdparm u32_getfd;
2005 struct compat_nfsctl_fsparm u32_getfs;
2007 #define ca32_svc u.u32_svc
2008 #define ca32_client u.u32_client
2009 #define ca32_export u.u32_export
2010 #define ca32_getfd u.u32_getfd
2011 #define ca32_getfs u.u32_getfs
2014 union compat_nfsctl_res {
2015 __u8 cr32_getfh[NFS_FHSIZE];
2016 struct knfsd_fh cr32_getfs;
2019 static int compat_nfs_svc_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
2023 err = access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc));
2024 err |= get_user(karg->ca_version, &arg->ca32_version);
2025 err |= __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port);
2026 err |= __get_user(karg->ca_svc.svc_nthreads, &arg->ca32_svc.svc32_nthreads);
2027 return (err) ? -EFAULT : 0;
2030 static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
2034 err = access_ok(VERIFY_READ, &arg->ca32_client, sizeof(arg->ca32_client));
2035 err |= get_user(karg->ca_version, &arg->ca32_version);
2036 err |= __copy_from_user(&karg->ca_client.cl_ident[0],
2037 &arg->ca32_client.cl32_ident[0],
2039 err |= __get_user(karg->ca_client.cl_naddr, &arg->ca32_client.cl32_naddr);
2040 err |= __copy_from_user(&karg->ca_client.cl_addrlist[0],
2041 &arg->ca32_client.cl32_addrlist[0],
2042 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX));
2043 err |= __get_user(karg->ca_client.cl_fhkeytype,
2044 &arg->ca32_client.cl32_fhkeytype);
2045 err |= __get_user(karg->ca_client.cl_fhkeylen,
2046 &arg->ca32_client.cl32_fhkeylen);
2047 err |= __copy_from_user(&karg->ca_client.cl_fhkey[0],
2048 &arg->ca32_client.cl32_fhkey[0],
2051 return (err) ? -EFAULT : 0;
2054 static int compat_nfs_exp_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
2058 err = access_ok(VERIFY_READ, &arg->ca32_export, sizeof(arg->ca32_export));
2059 err |= get_user(karg->ca_version, &arg->ca32_version);
2060 err |= __copy_from_user(&karg->ca_export.ex_client[0],
2061 &arg->ca32_export.ex32_client[0],
2063 err |= __copy_from_user(&karg->ca_export.ex_path[0],
2064 &arg->ca32_export.ex32_path[0],
2066 err |= __get_user(karg->ca_export.ex_dev,
2067 &arg->ca32_export.ex32_dev);
2068 err |= __get_user(karg->ca_export.ex_ino,
2069 &arg->ca32_export.ex32_ino);
2070 err |= __get_user(karg->ca_export.ex_flags,
2071 &arg->ca32_export.ex32_flags);
2072 err |= __get_user(karg->ca_export.ex_anon_uid,
2073 &arg->ca32_export.ex32_anon_uid);
2074 err |= __get_user(karg->ca_export.ex_anon_gid,
2075 &arg->ca32_export.ex32_anon_gid);
2076 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
2077 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
2079 return (err) ? -EFAULT : 0;
2082 static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
2086 err = access_ok(VERIFY_READ, &arg->ca32_getfd, sizeof(arg->ca32_getfd));
2087 err |= get_user(karg->ca_version, &arg->ca32_version);
2088 err |= __copy_from_user(&karg->ca_getfd.gd_addr,
2089 &arg->ca32_getfd.gd32_addr,
2090 (sizeof(struct sockaddr)));
2091 err |= __copy_from_user(&karg->ca_getfd.gd_path,
2092 &arg->ca32_getfd.gd32_path,
2093 (NFS_MAXPATHLEN+1));
2094 err |= __get_user(karg->ca_getfd.gd_version,
2095 &arg->ca32_getfd.gd32_version);
2097 return (err) ? -EFAULT : 0;
2100 static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
2104 err = access_ok(VERIFY_READ, &arg->ca32_getfs, sizeof(arg->ca32_getfs));
2105 err |= get_user(karg->ca_version, &arg->ca32_version);
2106 err |= __copy_from_user(&karg->ca_getfs.gd_addr,
2107 &arg->ca32_getfs.gd32_addr,
2108 (sizeof(struct sockaddr)));
2109 err |= __copy_from_user(&karg->ca_getfs.gd_path,
2110 &arg->ca32_getfs.gd32_path,
2111 (NFS_MAXPATHLEN+1));
2112 err |= __get_user(karg->ca_getfs.gd_maxlen,
2113 &arg->ca32_getfs.gd32_maxlen);
2115 return (err) ? -EFAULT : 0;
2118 /* This really doesn't need translations, we are only passing
2119 * back a union which contains opaque nfs file handle data.
2121 static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, union compat_nfsctl_res __user *res)
2125 err = copy_to_user(res, kres, sizeof(*res));
2127 return (err) ? -EFAULT : 0;
2130 asmlinkage long compat_sys_nfsservctl(int cmd, struct compat_nfsctl_arg __user *arg,
2131 union compat_nfsctl_res __user *res)
2133 struct nfsctl_arg *karg;
2134 union nfsctl_res *kres;
2138 karg = kmalloc(sizeof(*karg), GFP_USER);
2139 kres = kmalloc(sizeof(*kres), GFP_USER);
2140 if(!karg || !kres) {
2147 err = compat_nfs_svc_trans(karg, arg);
2150 case NFSCTL_ADDCLIENT:
2151 err = compat_nfs_clnt_trans(karg, arg);
2154 case NFSCTL_DELCLIENT:
2155 err = compat_nfs_clnt_trans(karg, arg);
2159 case NFSCTL_UNEXPORT:
2160 err = compat_nfs_exp_trans(karg, arg);
2164 err = compat_nfs_getfd_trans(karg, arg);
2168 err = compat_nfs_getfs_trans(karg, arg);
2178 /* The __user pointer casts are valid because of the set_fs() */
2179 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
2185 if((cmd == NFSCTL_GETFD) ||
2186 (cmd == NFSCTL_GETFS))
2187 err = compat_nfs_getfh_res_trans(kres, res);
2195 long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
2197 return sys_ni_syscall();