[PATCH] w1: Added w1_reset_select_slave() - Resets the bus and then selects the slave by
[linux-2.6/verdex.git] / fs / compat.c
blob8c665705c6a07beea9a4ead55a2184524f7ed41f
1 /*
2 * linux/fs/compat.c
4 * Kernel compatibililty routines for e.g. 32 bit syscall support
5 * on 64 bit kernels.
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>
22 #include <linux/fs.h>
23 #include <linux/fcntl.h>
24 #include <linux/namei.h>
25 #include <linux/file.h>
26 #include <linux/vfs.h>
27 #include <linux/ioctl32.h>
28 #include <linux/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>
48 #include <net/sock.h> /* siocdevprivate_ioctl */
50 #include <asm/uaccess.h>
51 #include <asm/mmu_context.h>
52 #include <asm/ioctls.h>
55 * Not all architectures have sys_utime, so implement this in terms
56 * of sys_utimes.
58 asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
60 struct timeval tv[2];
62 if (t) {
63 if (get_user(tv[0].tv_sec, &t->actime) ||
64 get_user(tv[1].tv_sec, &t->modtime))
65 return -EFAULT;
66 tv[0].tv_usec = 0;
67 tv[1].tv_usec = 0;
69 return do_utimes(filename, t ? tv : NULL);
72 asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
74 struct timeval tv[2];
76 if (t) {
77 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
78 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
79 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
80 get_user(tv[1].tv_usec, &t[1].tv_usec))
81 return -EFAULT;
83 return do_utimes(filename, t ? tv : NULL);
86 asmlinkage long compat_sys_newstat(char __user * filename,
87 struct compat_stat __user *statbuf)
89 struct kstat stat;
90 int error = vfs_stat(filename, &stat);
92 if (!error)
93 error = cp_compat_stat(&stat, statbuf);
94 return error;
97 asmlinkage long compat_sys_newlstat(char __user * filename,
98 struct compat_stat __user *statbuf)
100 struct kstat stat;
101 int error = vfs_lstat(filename, &stat);
103 if (!error)
104 error = cp_compat_stat(&stat, statbuf);
105 return error;
108 asmlinkage long compat_sys_newfstat(unsigned int fd,
109 struct compat_stat __user * statbuf)
111 struct kstat stat;
112 int error = vfs_fstat(fd, &stat);
114 if (!error)
115 error = cp_compat_stat(&stat, statbuf);
116 return error;
119 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
122 if (sizeof ubuf->f_blocks == 4) {
123 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
124 0xffffffff00000000ULL)
125 return -EOVERFLOW;
126 /* f_files and f_ffree may be -1; it's okay
127 * to stuff that into 32 bits */
128 if (kbuf->f_files != 0xffffffffffffffffULL
129 && (kbuf->f_files & 0xffffffff00000000ULL))
130 return -EOVERFLOW;
131 if (kbuf->f_ffree != 0xffffffffffffffffULL
132 && (kbuf->f_ffree & 0xffffffff00000000ULL))
133 return -EOVERFLOW;
135 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
136 __put_user(kbuf->f_type, &ubuf->f_type) ||
137 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
138 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
139 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
140 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
141 __put_user(kbuf->f_files, &ubuf->f_files) ||
142 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
143 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
144 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
145 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
146 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
147 __put_user(0, &ubuf->f_spare[0]) ||
148 __put_user(0, &ubuf->f_spare[1]) ||
149 __put_user(0, &ubuf->f_spare[2]) ||
150 __put_user(0, &ubuf->f_spare[3]) ||
151 __put_user(0, &ubuf->f_spare[4]))
152 return -EFAULT;
153 return 0;
157 * The following statfs calls are copies of code from fs/open.c and
158 * should be checked against those from time to time
160 asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
162 struct nameidata nd;
163 int error;
165 error = user_path_walk(path, &nd);
166 if (!error) {
167 struct kstatfs tmp;
168 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
169 if (!error && put_compat_statfs(buf, &tmp))
170 error = -EFAULT;
171 path_release(&nd);
173 return error;
176 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
178 struct file * file;
179 struct kstatfs tmp;
180 int error;
182 error = -EBADF;
183 file = fget(fd);
184 if (!file)
185 goto out;
186 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
187 if (!error && put_compat_statfs(buf, &tmp))
188 error = -EFAULT;
189 fput(file);
190 out:
191 return error;
194 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
196 if (sizeof ubuf->f_blocks == 4) {
197 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
198 0xffffffff00000000ULL)
199 return -EOVERFLOW;
200 /* f_files and f_ffree may be -1; it's okay
201 * to stuff that into 32 bits */
202 if (kbuf->f_files != 0xffffffffffffffffULL
203 && (kbuf->f_files & 0xffffffff00000000ULL))
204 return -EOVERFLOW;
205 if (kbuf->f_ffree != 0xffffffffffffffffULL
206 && (kbuf->f_ffree & 0xffffffff00000000ULL))
207 return -EOVERFLOW;
209 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
210 __put_user(kbuf->f_type, &ubuf->f_type) ||
211 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
212 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
213 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
214 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
215 __put_user(kbuf->f_files, &ubuf->f_files) ||
216 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
217 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
218 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
219 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
220 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
221 return -EFAULT;
222 return 0;
225 asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
227 struct nameidata nd;
228 int error;
230 if (sz != sizeof(*buf))
231 return -EINVAL;
233 error = user_path_walk(path, &nd);
234 if (!error) {
235 struct kstatfs tmp;
236 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
237 if (!error && put_compat_statfs64(buf, &tmp))
238 error = -EFAULT;
239 path_release(&nd);
241 return error;
244 asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
246 struct file * file;
247 struct kstatfs tmp;
248 int error;
250 if (sz != sizeof(*buf))
251 return -EINVAL;
253 error = -EBADF;
254 file = fget(fd);
255 if (!file)
256 goto out;
257 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
258 if (!error && put_compat_statfs64(buf, &tmp))
259 error = -EFAULT;
260 fput(file);
261 out:
262 return error;
265 /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
267 #define IOCTL_HASHSIZE 256
268 static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
269 static DECLARE_RWSEM(ioctl32_sem);
271 extern struct ioctl_trans ioctl_start[];
272 extern int ioctl_table_size;
274 static inline unsigned long ioctl32_hash(unsigned long cmd)
276 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
279 static void ioctl32_insert_translation(struct ioctl_trans *trans)
281 unsigned long hash;
282 struct ioctl_trans *t;
284 hash = ioctl32_hash (trans->cmd);
285 if (!ioctl32_hash_table[hash])
286 ioctl32_hash_table[hash] = trans;
287 else {
288 t = ioctl32_hash_table[hash];
289 while (t->next)
290 t = t->next;
291 trans->next = NULL;
292 t->next = trans;
296 static int __init init_sys32_ioctl(void)
298 int i;
300 for (i = 0; i < ioctl_table_size; i++) {
301 if (ioctl_start[i].next != 0) {
302 printk("ioctl translation %d bad\n",i);
303 return -1;
306 ioctl32_insert_translation(&ioctl_start[i]);
308 return 0;
311 __initcall(init_sys32_ioctl);
313 static void compat_ioctl_error(struct file *filp, unsigned int fd,
314 unsigned int cmd, unsigned long arg)
316 char buf[10];
317 char *fn = "?";
318 char *path;
320 /* find the name of the device. */
321 path = (char *)__get_free_page(GFP_KERNEL);
322 if (path) {
323 fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE);
324 if (IS_ERR(fn))
325 fn = "?";
328 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
329 if (!isprint(buf[1]))
330 sprintf(buf, "%02x", buf[1]);
331 printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
332 "cmd(%08x){%s} arg(%08x) on %s\n",
333 current->comm, current->pid,
334 (int)fd, (unsigned int)cmd, buf,
335 (unsigned int)arg, fn);
337 if (path)
338 free_page((unsigned long)path);
341 asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
342 unsigned long arg)
344 struct file *filp;
345 int error = -EBADF;
346 struct ioctl_trans *t;
347 int fput_needed;
349 filp = fget_light(fd, &fput_needed);
350 if (!filp)
351 goto out;
353 /* RED-PEN how should LSM module know it's handling 32bit? */
354 error = security_file_ioctl(filp, cmd, arg);
355 if (error)
356 goto out_fput;
359 * To allow the compat_ioctl handlers to be self contained
360 * we need to check the common ioctls here first.
361 * Just handle them with the standard handlers below.
363 switch (cmd) {
364 case FIOCLEX:
365 case FIONCLEX:
366 case FIONBIO:
367 case FIOASYNC:
368 case FIOQSIZE:
369 break;
371 case FIBMAP:
372 case FIGETBSZ:
373 case FIONREAD:
374 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
375 break;
376 /*FALL THROUGH*/
378 default:
379 if (filp->f_op && filp->f_op->compat_ioctl) {
380 error = filp->f_op->compat_ioctl(filp, cmd, arg);
381 if (error != -ENOIOCTLCMD)
382 goto out_fput;
385 if (!filp->f_op ||
386 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
387 goto do_ioctl;
388 break;
391 /* When register_ioctl32_conversion is finally gone remove
392 this lock! -AK */
393 down_read(&ioctl32_sem);
394 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
395 if (t->cmd == cmd)
396 goto found_handler;
398 up_read(&ioctl32_sem);
400 if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) &&
401 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
402 error = siocdevprivate_ioctl(fd, cmd, arg);
403 } else {
404 static int count;
406 if (++count <= 50)
407 compat_ioctl_error(filp, fd, cmd, arg);
408 error = -EINVAL;
411 goto out_fput;
413 found_handler:
414 if (t->handler) {
415 lock_kernel();
416 error = t->handler(fd, cmd, arg, filp);
417 unlock_kernel();
418 up_read(&ioctl32_sem);
419 goto out_fput;
422 up_read(&ioctl32_sem);
423 do_ioctl:
424 error = vfs_ioctl(filp, fd, cmd, arg);
425 out_fput:
426 fput_light(filp, fput_needed);
427 out:
428 return error;
431 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
433 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
434 __get_user(kfl->l_type, &ufl->l_type) ||
435 __get_user(kfl->l_whence, &ufl->l_whence) ||
436 __get_user(kfl->l_start, &ufl->l_start) ||
437 __get_user(kfl->l_len, &ufl->l_len) ||
438 __get_user(kfl->l_pid, &ufl->l_pid))
439 return -EFAULT;
440 return 0;
443 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
445 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
446 __put_user(kfl->l_type, &ufl->l_type) ||
447 __put_user(kfl->l_whence, &ufl->l_whence) ||
448 __put_user(kfl->l_start, &ufl->l_start) ||
449 __put_user(kfl->l_len, &ufl->l_len) ||
450 __put_user(kfl->l_pid, &ufl->l_pid))
451 return -EFAULT;
452 return 0;
455 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
456 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __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))
464 return -EFAULT;
465 return 0;
467 #endif
469 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
470 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
472 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
473 __put_user(kfl->l_type, &ufl->l_type) ||
474 __put_user(kfl->l_whence, &ufl->l_whence) ||
475 __put_user(kfl->l_start, &ufl->l_start) ||
476 __put_user(kfl->l_len, &ufl->l_len) ||
477 __put_user(kfl->l_pid, &ufl->l_pid))
478 return -EFAULT;
479 return 0;
481 #endif
483 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
484 unsigned long arg)
486 mm_segment_t old_fs;
487 struct flock f;
488 long ret;
490 switch (cmd) {
491 case F_GETLK:
492 case F_SETLK:
493 case F_SETLKW:
494 ret = get_compat_flock(&f, compat_ptr(arg));
495 if (ret != 0)
496 break;
497 old_fs = get_fs();
498 set_fs(KERNEL_DS);
499 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
500 set_fs(old_fs);
501 if (cmd == F_GETLK && ret == 0) {
502 if ((f.l_start >= COMPAT_OFF_T_MAX) ||
503 ((f.l_start + f.l_len) > COMPAT_OFF_T_MAX))
504 ret = -EOVERFLOW;
505 if (ret == 0)
506 ret = put_compat_flock(&f, compat_ptr(arg));
508 break;
510 case F_GETLK64:
511 case F_SETLK64:
512 case F_SETLKW64:
513 ret = get_compat_flock64(&f, compat_ptr(arg));
514 if (ret != 0)
515 break;
516 old_fs = get_fs();
517 set_fs(KERNEL_DS);
518 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
519 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
520 (unsigned long)&f);
521 set_fs(old_fs);
522 if (cmd == F_GETLK64 && ret == 0) {
523 if ((f.l_start >= COMPAT_LOFF_T_MAX) ||
524 ((f.l_start + f.l_len) > COMPAT_LOFF_T_MAX))
525 ret = -EOVERFLOW;
526 if (ret == 0)
527 ret = put_compat_flock64(&f, compat_ptr(arg));
529 break;
531 default:
532 ret = sys_fcntl(fd, cmd, arg);
533 break;
535 return ret;
538 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
539 unsigned long arg)
541 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
542 return -EINVAL;
543 return compat_sys_fcntl64(fd, cmd, arg);
546 asmlinkage long
547 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
549 long ret;
550 aio_context_t ctx64;
552 mm_segment_t oldfs = get_fs();
553 if (unlikely(get_user(ctx64, ctx32p)))
554 return -EFAULT;
556 set_fs(KERNEL_DS);
557 /* The __user pointer cast is valid because of the set_fs() */
558 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
559 set_fs(oldfs);
560 /* truncating is ok because it's a user address */
561 if (!ret)
562 ret = put_user((u32) ctx64, ctx32p);
563 return ret;
566 asmlinkage long
567 compat_sys_io_getevents(aio_context_t ctx_id,
568 unsigned long min_nr,
569 unsigned long nr,
570 struct io_event __user *events,
571 struct compat_timespec __user *timeout)
573 long ret;
574 struct timespec t;
575 struct timespec __user *ut = NULL;
577 ret = -EFAULT;
578 if (unlikely(!access_ok(VERIFY_WRITE, events,
579 nr * sizeof(struct io_event))))
580 goto out;
581 if (timeout) {
582 if (get_compat_timespec(&t, timeout))
583 goto out;
585 ut = compat_alloc_user_space(sizeof(*ut));
586 if (copy_to_user(ut, &t, sizeof(t)) )
587 goto out;
589 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
590 out:
591 return ret;
594 static inline long
595 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
597 compat_uptr_t uptr;
598 int i;
600 for (i = 0; i < nr; ++i) {
601 if (get_user(uptr, ptr32 + i))
602 return -EFAULT;
603 if (put_user(compat_ptr(uptr), ptr64 + i))
604 return -EFAULT;
606 return 0;
609 #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
611 asmlinkage long
612 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
614 struct iocb __user * __user *iocb64;
615 long ret;
617 if (unlikely(nr < 0))
618 return -EINVAL;
620 if (nr > MAX_AIO_SUBMITS)
621 nr = MAX_AIO_SUBMITS;
623 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
624 ret = copy_iocb(nr, iocb, iocb64);
625 if (!ret)
626 ret = sys_io_submit(ctx_id, nr, iocb64);
627 return ret;
630 struct compat_ncp_mount_data {
631 compat_int_t version;
632 compat_uint_t ncp_fd;
633 __compat_uid_t mounted_uid;
634 compat_pid_t wdog_pid;
635 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
636 compat_uint_t time_out;
637 compat_uint_t retry_count;
638 compat_uint_t flags;
639 __compat_uid_t uid;
640 __compat_gid_t gid;
641 compat_mode_t file_mode;
642 compat_mode_t dir_mode;
645 struct compat_ncp_mount_data_v4 {
646 compat_int_t version;
647 compat_ulong_t flags;
648 compat_ulong_t mounted_uid;
649 compat_long_t wdog_pid;
650 compat_uint_t ncp_fd;
651 compat_uint_t time_out;
652 compat_uint_t retry_count;
653 compat_ulong_t uid;
654 compat_ulong_t gid;
655 compat_ulong_t file_mode;
656 compat_ulong_t dir_mode;
659 static void *do_ncp_super_data_conv(void *raw_data)
661 int version = *(unsigned int *)raw_data;
663 if (version == 3) {
664 struct compat_ncp_mount_data *c_n = raw_data;
665 struct ncp_mount_data *n = raw_data;
667 n->dir_mode = c_n->dir_mode;
668 n->file_mode = c_n->file_mode;
669 n->gid = c_n->gid;
670 n->uid = c_n->uid;
671 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
672 n->wdog_pid = c_n->wdog_pid;
673 n->mounted_uid = c_n->mounted_uid;
674 } else if (version == 4) {
675 struct compat_ncp_mount_data_v4 *c_n = raw_data;
676 struct ncp_mount_data_v4 *n = raw_data;
678 n->dir_mode = c_n->dir_mode;
679 n->file_mode = c_n->file_mode;
680 n->gid = c_n->gid;
681 n->uid = c_n->uid;
682 n->retry_count = c_n->retry_count;
683 n->time_out = c_n->time_out;
684 n->ncp_fd = c_n->ncp_fd;
685 n->wdog_pid = c_n->wdog_pid;
686 n->mounted_uid = c_n->mounted_uid;
687 n->flags = c_n->flags;
688 } else if (version != 5) {
689 return NULL;
692 return raw_data;
695 struct compat_smb_mount_data {
696 compat_int_t version;
697 __compat_uid_t mounted_uid;
698 __compat_uid_t uid;
699 __compat_gid_t gid;
700 compat_mode_t file_mode;
701 compat_mode_t dir_mode;
704 static void *do_smb_super_data_conv(void *raw_data)
706 struct smb_mount_data *s = raw_data;
707 struct compat_smb_mount_data *c_s = raw_data;
709 if (c_s->version != SMB_MOUNT_OLDVERSION)
710 goto out;
711 s->dir_mode = c_s->dir_mode;
712 s->file_mode = c_s->file_mode;
713 s->gid = c_s->gid;
714 s->uid = c_s->uid;
715 s->mounted_uid = c_s->mounted_uid;
716 out:
717 return raw_data;
720 struct compat_nfs_string {
721 compat_uint_t len;
722 compat_uptr_t data;
725 static inline void compat_nfs_string(struct nfs_string *dst,
726 struct compat_nfs_string *src)
728 dst->data = compat_ptr(src->data);
729 dst->len = src->len;
732 struct compat_nfs4_mount_data_v1 {
733 compat_int_t version;
734 compat_int_t flags;
735 compat_int_t rsize;
736 compat_int_t wsize;
737 compat_int_t timeo;
738 compat_int_t retrans;
739 compat_int_t acregmin;
740 compat_int_t acregmax;
741 compat_int_t acdirmin;
742 compat_int_t acdirmax;
743 struct compat_nfs_string client_addr;
744 struct compat_nfs_string mnt_path;
745 struct compat_nfs_string hostname;
746 compat_uint_t host_addrlen;
747 compat_uptr_t host_addr;
748 compat_int_t proto;
749 compat_int_t auth_flavourlen;
750 compat_uptr_t auth_flavours;
753 static int do_nfs4_super_data_conv(void *raw_data)
755 int version = *(compat_uint_t *) raw_data;
757 if (version == 1) {
758 struct compat_nfs4_mount_data_v1 *raw = raw_data;
759 struct nfs4_mount_data *real = raw_data;
761 /* copy the fields backwards */
762 real->auth_flavours = compat_ptr(raw->auth_flavours);
763 real->auth_flavourlen = raw->auth_flavourlen;
764 real->proto = raw->proto;
765 real->host_addr = compat_ptr(raw->host_addr);
766 real->host_addrlen = raw->host_addrlen;
767 compat_nfs_string(&real->hostname, &raw->hostname);
768 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
769 compat_nfs_string(&real->client_addr, &raw->client_addr);
770 real->acdirmax = raw->acdirmax;
771 real->acdirmin = raw->acdirmin;
772 real->acregmax = raw->acregmax;
773 real->acregmin = raw->acregmin;
774 real->retrans = raw->retrans;
775 real->timeo = raw->timeo;
776 real->wsize = raw->wsize;
777 real->rsize = raw->rsize;
778 real->flags = raw->flags;
779 real->version = raw->version;
781 else {
782 return -EINVAL;
785 return 0;
788 extern int copy_mount_options (const void __user *, unsigned long *);
790 #define SMBFS_NAME "smbfs"
791 #define NCPFS_NAME "ncpfs"
792 #define NFS4_NAME "nfs4"
794 asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
795 char __user * type, unsigned long flags,
796 void __user * data)
798 unsigned long type_page;
799 unsigned long data_page;
800 unsigned long dev_page;
801 char *dir_page;
802 int retval;
804 retval = copy_mount_options (type, &type_page);
805 if (retval < 0)
806 goto out;
808 dir_page = getname(dir_name);
809 retval = PTR_ERR(dir_page);
810 if (IS_ERR(dir_page))
811 goto out1;
813 retval = copy_mount_options (dev_name, &dev_page);
814 if (retval < 0)
815 goto out2;
817 retval = copy_mount_options (data, &data_page);
818 if (retval < 0)
819 goto out3;
821 retval = -EINVAL;
823 if (type_page) {
824 if (!strcmp((char *)type_page, SMBFS_NAME)) {
825 do_smb_super_data_conv((void *)data_page);
826 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
827 do_ncp_super_data_conv((void *)data_page);
828 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
829 if (do_nfs4_super_data_conv((void *) data_page))
830 goto out4;
834 lock_kernel();
835 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
836 flags, (void*)data_page);
837 unlock_kernel();
839 out4:
840 free_page(data_page);
841 out3:
842 free_page(dev_page);
843 out2:
844 putname(dir_page);
845 out1:
846 free_page(type_page);
847 out:
848 return retval;
851 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
852 #define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
853 ~(sizeof(compat_long_t)-1))
855 struct compat_old_linux_dirent {
856 compat_ulong_t d_ino;
857 compat_ulong_t d_offset;
858 unsigned short d_namlen;
859 char d_name[1];
862 struct compat_readdir_callback {
863 struct compat_old_linux_dirent __user *dirent;
864 int result;
867 static int compat_fillonedir(void *__buf, const char *name, int namlen,
868 loff_t offset, ino_t ino, unsigned int d_type)
870 struct compat_readdir_callback *buf = __buf;
871 struct compat_old_linux_dirent __user *dirent;
873 if (buf->result)
874 return -EINVAL;
875 buf->result++;
876 dirent = buf->dirent;
877 if (!access_ok(VERIFY_WRITE, dirent,
878 (unsigned long)(dirent->d_name + namlen + 1) -
879 (unsigned long)dirent))
880 goto efault;
881 if ( __put_user(ino, &dirent->d_ino) ||
882 __put_user(offset, &dirent->d_offset) ||
883 __put_user(namlen, &dirent->d_namlen) ||
884 __copy_to_user(dirent->d_name, name, namlen) ||
885 __put_user(0, dirent->d_name + namlen))
886 goto efault;
887 return 0;
888 efault:
889 buf->result = -EFAULT;
890 return -EFAULT;
893 asmlinkage long compat_sys_old_readdir(unsigned int fd,
894 struct compat_old_linux_dirent __user *dirent, unsigned int count)
896 int error;
897 struct file *file;
898 struct compat_readdir_callback buf;
900 error = -EBADF;
901 file = fget(fd);
902 if (!file)
903 goto out;
905 buf.result = 0;
906 buf.dirent = dirent;
908 error = vfs_readdir(file, compat_fillonedir, &buf);
909 if (error >= 0)
910 error = buf.result;
912 fput(file);
913 out:
914 return error;
917 struct compat_linux_dirent {
918 compat_ulong_t d_ino;
919 compat_ulong_t d_off;
920 unsigned short d_reclen;
921 char d_name[1];
924 struct compat_getdents_callback {
925 struct compat_linux_dirent __user *current_dir;
926 struct compat_linux_dirent __user *previous;
927 int count;
928 int error;
931 static int compat_filldir(void *__buf, const char *name, int namlen,
932 loff_t offset, ino_t ino, unsigned int d_type)
934 struct compat_linux_dirent __user * dirent;
935 struct compat_getdents_callback *buf = __buf;
936 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
938 buf->error = -EINVAL; /* only used if we fail.. */
939 if (reclen > buf->count)
940 return -EINVAL;
941 dirent = buf->previous;
942 if (dirent) {
943 if (__put_user(offset, &dirent->d_off))
944 goto efault;
946 dirent = buf->current_dir;
947 if (__put_user(ino, &dirent->d_ino))
948 goto efault;
949 if (__put_user(reclen, &dirent->d_reclen))
950 goto efault;
951 if (copy_to_user(dirent->d_name, name, namlen))
952 goto efault;
953 if (__put_user(0, dirent->d_name + namlen))
954 goto efault;
955 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
956 goto efault;
957 buf->previous = dirent;
958 dirent = (void __user *)dirent + reclen;
959 buf->current_dir = dirent;
960 buf->count -= reclen;
961 return 0;
962 efault:
963 buf->error = -EFAULT;
964 return -EFAULT;
967 asmlinkage long compat_sys_getdents(unsigned int fd,
968 struct compat_linux_dirent __user *dirent, unsigned int count)
970 struct file * file;
971 struct compat_linux_dirent __user * lastdirent;
972 struct compat_getdents_callback buf;
973 int error;
975 error = -EFAULT;
976 if (!access_ok(VERIFY_WRITE, dirent, count))
977 goto out;
979 error = -EBADF;
980 file = fget(fd);
981 if (!file)
982 goto out;
984 buf.current_dir = dirent;
985 buf.previous = NULL;
986 buf.count = count;
987 buf.error = 0;
989 error = vfs_readdir(file, compat_filldir, &buf);
990 if (error < 0)
991 goto out_putf;
992 error = buf.error;
993 lastdirent = buf.previous;
994 if (lastdirent) {
995 if (put_user(file->f_pos, &lastdirent->d_off))
996 error = -EFAULT;
997 else
998 error = count - buf.count;
1001 out_putf:
1002 fput(file);
1003 out:
1004 return error;
1007 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1008 #define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1010 struct compat_getdents_callback64 {
1011 struct linux_dirent64 __user *current_dir;
1012 struct linux_dirent64 __user *previous;
1013 int count;
1014 int error;
1017 static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1018 ino_t ino, unsigned int d_type)
1020 struct linux_dirent64 __user *dirent;
1021 struct compat_getdents_callback64 *buf = __buf;
1022 int jj = NAME_OFFSET(dirent);
1023 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1024 u64 off;
1026 buf->error = -EINVAL; /* only used if we fail.. */
1027 if (reclen > buf->count)
1028 return -EINVAL;
1029 dirent = buf->previous;
1031 if (dirent) {
1032 if (__put_user_unaligned(offset, &dirent->d_off))
1033 goto efault;
1035 dirent = buf->current_dir;
1036 if (__put_user_unaligned(ino, &dirent->d_ino))
1037 goto efault;
1038 off = 0;
1039 if (__put_user_unaligned(off, &dirent->d_off))
1040 goto efault;
1041 if (__put_user(reclen, &dirent->d_reclen))
1042 goto efault;
1043 if (__put_user(d_type, &dirent->d_type))
1044 goto efault;
1045 if (copy_to_user(dirent->d_name, name, namlen))
1046 goto efault;
1047 if (__put_user(0, dirent->d_name + namlen))
1048 goto efault;
1049 buf->previous = dirent;
1050 dirent = (void __user *)dirent + reclen;
1051 buf->current_dir = dirent;
1052 buf->count -= reclen;
1053 return 0;
1054 efault:
1055 buf->error = -EFAULT;
1056 return -EFAULT;
1059 asmlinkage long compat_sys_getdents64(unsigned int fd,
1060 struct linux_dirent64 __user * dirent, unsigned int count)
1062 struct file * file;
1063 struct linux_dirent64 __user * lastdirent;
1064 struct compat_getdents_callback64 buf;
1065 int error;
1067 error = -EFAULT;
1068 if (!access_ok(VERIFY_WRITE, dirent, count))
1069 goto out;
1071 error = -EBADF;
1072 file = fget(fd);
1073 if (!file)
1074 goto out;
1076 buf.current_dir = dirent;
1077 buf.previous = NULL;
1078 buf.count = count;
1079 buf.error = 0;
1081 error = vfs_readdir(file, compat_filldir64, &buf);
1082 if (error < 0)
1083 goto out_putf;
1084 error = buf.error;
1085 lastdirent = buf.previous;
1086 if (lastdirent) {
1087 typeof(lastdirent->d_off) d_off = file->f_pos;
1088 __put_user_unaligned(d_off, &lastdirent->d_off);
1089 error = count - buf.count;
1092 out_putf:
1093 fput(file);
1094 out:
1095 return error;
1097 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1099 static ssize_t compat_do_readv_writev(int type, struct file *file,
1100 const struct compat_iovec __user *uvector,
1101 unsigned long nr_segs, loff_t *pos)
1103 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
1104 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
1106 compat_ssize_t tot_len;
1107 struct iovec iovstack[UIO_FASTIOV];
1108 struct iovec *iov=iovstack, *vector;
1109 ssize_t ret;
1110 int seg;
1111 io_fn_t fn;
1112 iov_fn_t fnv;
1115 * SuS says "The readv() function *may* fail if the iovcnt argument
1116 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1117 * traditionally returned zero for zero segments, so...
1119 ret = 0;
1120 if (nr_segs == 0)
1121 goto out;
1124 * First get the "struct iovec" from user memory and
1125 * verify all the pointers
1127 ret = -EINVAL;
1128 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1129 goto out;
1130 if (!file->f_op)
1131 goto out;
1132 if (nr_segs > UIO_FASTIOV) {
1133 ret = -ENOMEM;
1134 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1135 if (!iov)
1136 goto out;
1138 ret = -EFAULT;
1139 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1140 goto out;
1143 * Single unix specification:
1144 * We should -EINVAL if an element length is not >= 0 and fitting an
1145 * ssize_t. The total length is fitting an ssize_t
1147 * Be careful here because iov_len is a size_t not an ssize_t
1149 tot_len = 0;
1150 vector = iov;
1151 ret = -EINVAL;
1152 for (seg = 0 ; seg < nr_segs; seg++) {
1153 compat_ssize_t tmp = tot_len;
1154 compat_ssize_t len;
1155 compat_uptr_t buf;
1157 if (__get_user(len, &uvector->iov_len) ||
1158 __get_user(buf, &uvector->iov_base)) {
1159 ret = -EFAULT;
1160 goto out;
1162 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1163 goto out;
1164 tot_len += len;
1165 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1166 goto out;
1167 vector->iov_base = compat_ptr(buf);
1168 vector->iov_len = (compat_size_t) len;
1169 uvector++;
1170 vector++;
1172 if (tot_len == 0) {
1173 ret = 0;
1174 goto out;
1177 ret = rw_verify_area(type, file, pos, tot_len);
1178 if (ret)
1179 goto out;
1181 fnv = NULL;
1182 if (type == READ) {
1183 fn = file->f_op->read;
1184 fnv = file->f_op->readv;
1185 } else {
1186 fn = (io_fn_t)file->f_op->write;
1187 fnv = file->f_op->writev;
1189 if (fnv) {
1190 ret = fnv(file, iov, nr_segs, pos);
1191 goto out;
1194 /* Do it by hand, with file-ops */
1195 ret = 0;
1196 vector = iov;
1197 while (nr_segs > 0) {
1198 void __user * base;
1199 size_t len;
1200 ssize_t nr;
1202 base = vector->iov_base;
1203 len = vector->iov_len;
1204 vector++;
1205 nr_segs--;
1207 nr = fn(file, base, len, pos);
1209 if (nr < 0) {
1210 if (!ret) ret = nr;
1211 break;
1213 ret += nr;
1214 if (nr != len)
1215 break;
1217 out:
1218 if (iov != iovstack)
1219 kfree(iov);
1220 if ((ret + (type == READ)) > 0) {
1221 struct dentry *dentry = file->f_dentry;
1222 if (type == READ)
1223 fsnotify_access(dentry);
1224 else
1225 fsnotify_modify(dentry);
1227 return ret;
1230 asmlinkage ssize_t
1231 compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1233 struct file *file;
1234 ssize_t ret = -EBADF;
1236 file = fget(fd);
1237 if (!file)
1238 return -EBADF;
1240 if (!(file->f_mode & FMODE_READ))
1241 goto out;
1243 ret = -EINVAL;
1244 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
1245 goto out;
1247 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1249 out:
1250 fput(file);
1251 return ret;
1254 asmlinkage ssize_t
1255 compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1257 struct file *file;
1258 ssize_t ret = -EBADF;
1260 file = fget(fd);
1261 if (!file)
1262 return -EBADF;
1263 if (!(file->f_mode & FMODE_WRITE))
1264 goto out;
1266 ret = -EINVAL;
1267 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
1268 goto out;
1270 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1272 out:
1273 fput(file);
1274 return ret;
1278 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1279 * O_LARGEFILE flag.
1281 asmlinkage long
1282 compat_sys_open(const char __user *filename, int flags, int mode)
1284 return do_sys_open(filename, flags, mode);
1288 * compat_count() counts the number of arguments/envelopes. It is basically
1289 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1290 * and envp pointers.
1292 static int compat_count(compat_uptr_t __user *argv, int max)
1294 int i = 0;
1296 if (argv != NULL) {
1297 for (;;) {
1298 compat_uptr_t p;
1300 if (get_user(p, argv))
1301 return -EFAULT;
1302 if (!p)
1303 break;
1304 argv++;
1305 if(++i > max)
1306 return -E2BIG;
1309 return i;
1313 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1314 * except that it works with 32 bit argv and envp pointers.
1316 static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1317 struct linux_binprm *bprm)
1319 struct page *kmapped_page = NULL;
1320 char *kaddr = NULL;
1321 int ret;
1323 while (argc-- > 0) {
1324 compat_uptr_t str;
1325 int len;
1326 unsigned long pos;
1328 if (get_user(str, argv+argc) ||
1329 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1330 ret = -EFAULT;
1331 goto out;
1334 if (bprm->p < len) {
1335 ret = -E2BIG;
1336 goto out;
1339 bprm->p -= len;
1340 /* XXX: add architecture specific overflow check here. */
1341 pos = bprm->p;
1343 while (len > 0) {
1344 int i, new, err;
1345 int offset, bytes_to_copy;
1346 struct page *page;
1348 offset = pos % PAGE_SIZE;
1349 i = pos/PAGE_SIZE;
1350 page = bprm->page[i];
1351 new = 0;
1352 if (!page) {
1353 page = alloc_page(GFP_HIGHUSER);
1354 bprm->page[i] = page;
1355 if (!page) {
1356 ret = -ENOMEM;
1357 goto out;
1359 new = 1;
1362 if (page != kmapped_page) {
1363 if (kmapped_page)
1364 kunmap(kmapped_page);
1365 kmapped_page = page;
1366 kaddr = kmap(kmapped_page);
1368 if (new && offset)
1369 memset(kaddr, 0, offset);
1370 bytes_to_copy = PAGE_SIZE - offset;
1371 if (bytes_to_copy > len) {
1372 bytes_to_copy = len;
1373 if (new)
1374 memset(kaddr+offset+len, 0,
1375 PAGE_SIZE-offset-len);
1377 err = copy_from_user(kaddr+offset, compat_ptr(str),
1378 bytes_to_copy);
1379 if (err) {
1380 ret = -EFAULT;
1381 goto out;
1384 pos += bytes_to_copy;
1385 str += bytes_to_copy;
1386 len -= bytes_to_copy;
1389 ret = 0;
1390 out:
1391 if (kmapped_page)
1392 kunmap(kmapped_page);
1393 return ret;
1396 #ifdef CONFIG_MMU
1398 #define free_arg_pages(bprm) do { } while (0)
1400 #else
1402 static inline void free_arg_pages(struct linux_binprm *bprm)
1404 int i;
1406 for (i = 0; i < MAX_ARG_PAGES; i++) {
1407 if (bprm->page[i])
1408 __free_page(bprm->page[i]);
1409 bprm->page[i] = NULL;
1413 #endif /* CONFIG_MMU */
1416 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1417 * that it processes 32 bit argv and envp pointers.
1419 int compat_do_execve(char * filename,
1420 compat_uptr_t __user *argv,
1421 compat_uptr_t __user *envp,
1422 struct pt_regs * regs)
1424 struct linux_binprm *bprm;
1425 struct file *file;
1426 int retval;
1427 int i;
1429 retval = -ENOMEM;
1430 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1431 if (!bprm)
1432 goto out_ret;
1433 memset(bprm, 0, sizeof(*bprm));
1435 file = open_exec(filename);
1436 retval = PTR_ERR(file);
1437 if (IS_ERR(file))
1438 goto out_kfree;
1440 sched_exec();
1442 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1443 bprm->file = file;
1444 bprm->filename = filename;
1445 bprm->interp = filename;
1446 bprm->mm = mm_alloc();
1447 retval = -ENOMEM;
1448 if (!bprm->mm)
1449 goto out_file;
1451 retval = init_new_context(current, bprm->mm);
1452 if (retval < 0)
1453 goto out_mm;
1455 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1456 if ((retval = bprm->argc) < 0)
1457 goto out_mm;
1459 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1460 if ((retval = bprm->envc) < 0)
1461 goto out_mm;
1463 retval = security_bprm_alloc(bprm);
1464 if (retval)
1465 goto out;
1467 retval = prepare_binprm(bprm);
1468 if (retval < 0)
1469 goto out;
1471 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1472 if (retval < 0)
1473 goto out;
1475 bprm->exec = bprm->p;
1476 retval = compat_copy_strings(bprm->envc, envp, bprm);
1477 if (retval < 0)
1478 goto out;
1480 retval = compat_copy_strings(bprm->argc, argv, bprm);
1481 if (retval < 0)
1482 goto out;
1484 retval = search_binary_handler(bprm, regs);
1485 if (retval >= 0) {
1486 free_arg_pages(bprm);
1488 /* execve success */
1489 security_bprm_free(bprm);
1490 kfree(bprm);
1491 return retval;
1494 out:
1495 /* Something went wrong, return the inode and free the argument pages*/
1496 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1497 struct page * page = bprm->page[i];
1498 if (page)
1499 __free_page(page);
1502 if (bprm->security)
1503 security_bprm_free(bprm);
1505 out_mm:
1506 if (bprm->mm)
1507 mmdrop(bprm->mm);
1509 out_file:
1510 if (bprm->file) {
1511 allow_write_access(bprm->file);
1512 fput(bprm->file);
1515 out_kfree:
1516 kfree(bprm);
1518 out_ret:
1519 return retval;
1522 #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1524 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1527 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1528 * 64-bit unsigned longs.
1530 static inline
1531 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1532 unsigned long *fdset)
1534 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1535 if (ufdset) {
1536 unsigned long odd;
1538 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1539 return -EFAULT;
1541 odd = nr & 1UL;
1542 nr &= ~1UL;
1543 while (nr) {
1544 unsigned long h, l;
1545 __get_user(l, ufdset);
1546 __get_user(h, ufdset+1);
1547 ufdset += 2;
1548 *fdset++ = h << 32 | l;
1549 nr -= 2;
1551 if (odd)
1552 __get_user(*fdset, ufdset);
1553 } else {
1554 /* Tricky, must clear full unsigned long in the
1555 * kernel fdset at the end, this makes sure that
1556 * actually happens.
1558 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1560 return 0;
1563 static inline
1564 void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1565 unsigned long *fdset)
1567 unsigned long odd;
1568 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1570 if (!ufdset)
1571 return;
1573 odd = nr & 1UL;
1574 nr &= ~1UL;
1575 while (nr) {
1576 unsigned long h, l;
1577 l = *fdset++;
1578 h = l >> 32;
1579 __put_user(l, ufdset);
1580 __put_user(h, ufdset+1);
1581 ufdset += 2;
1582 nr -= 2;
1584 if (odd)
1585 __put_user(*fdset, ufdset);
1590 * This is a virtual copy of sys_select from fs/select.c and probably
1591 * should be compared to it from time to time
1593 static void *select_bits_alloc(int size)
1595 return kmalloc(6 * size, GFP_KERNEL);
1598 static void select_bits_free(void *bits, int size)
1600 kfree(bits);
1604 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1605 * like to be certain this leads to no problems. So I return
1606 * EINTR just for safety.
1608 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1609 * I'm trying ERESTARTNOHAND which restart only when you want to.
1611 #define MAX_SELECT_SECONDS \
1612 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1614 asmlinkage long
1615 compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp,
1616 compat_ulong_t __user *exp, struct compat_timeval __user *tvp)
1618 fd_set_bits fds;
1619 char *bits;
1620 long timeout;
1621 int size, max_fdset, ret = -EINVAL;
1623 timeout = MAX_SCHEDULE_TIMEOUT;
1624 if (tvp) {
1625 time_t sec, usec;
1627 if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
1628 || __get_user(sec, &tvp->tv_sec)
1629 || __get_user(usec, &tvp->tv_usec)) {
1630 ret = -EFAULT;
1631 goto out_nofds;
1634 if (sec < 0 || usec < 0)
1635 goto out_nofds;
1637 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
1638 timeout = ROUND_UP(usec, 1000000/HZ);
1639 timeout += sec * (unsigned long) HZ;
1643 if (n < 0)
1644 goto out_nofds;
1646 /* max_fdset can increase, so grab it once to avoid race */
1647 max_fdset = current->files->max_fdset;
1648 if (n > max_fdset)
1649 n = max_fdset;
1652 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1653 * since we used fdset we need to allocate memory in units of
1654 * long-words.
1656 ret = -ENOMEM;
1657 size = FDS_BYTES(n);
1658 bits = select_bits_alloc(size);
1659 if (!bits)
1660 goto out_nofds;
1661 fds.in = (unsigned long *) bits;
1662 fds.out = (unsigned long *) (bits + size);
1663 fds.ex = (unsigned long *) (bits + 2*size);
1664 fds.res_in = (unsigned long *) (bits + 3*size);
1665 fds.res_out = (unsigned long *) (bits + 4*size);
1666 fds.res_ex = (unsigned long *) (bits + 5*size);
1668 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1669 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1670 (ret = compat_get_fd_set(n, exp, fds.ex)))
1671 goto out;
1672 zero_fd_set(n, fds.res_in);
1673 zero_fd_set(n, fds.res_out);
1674 zero_fd_set(n, fds.res_ex);
1676 ret = do_select(n, &fds, &timeout);
1678 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
1679 time_t sec = 0, usec = 0;
1680 if (timeout) {
1681 sec = timeout / HZ;
1682 usec = timeout % HZ;
1683 usec *= (1000000/HZ);
1685 if (put_user(sec, &tvp->tv_sec) ||
1686 put_user(usec, &tvp->tv_usec))
1687 ret = -EFAULT;
1690 if (ret < 0)
1691 goto out;
1692 if (!ret) {
1693 ret = -ERESTARTNOHAND;
1694 if (signal_pending(current))
1695 goto out;
1696 ret = 0;
1699 compat_set_fd_set(n, inp, fds.res_in);
1700 compat_set_fd_set(n, outp, fds.res_out);
1701 compat_set_fd_set(n, exp, fds.res_ex);
1703 out:
1704 select_bits_free(bits, size);
1705 out_nofds:
1706 return ret;
1709 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1710 /* Stuff for NFS server syscalls... */
1711 struct compat_nfsctl_svc {
1712 u16 svc32_port;
1713 s32 svc32_nthreads;
1716 struct compat_nfsctl_client {
1717 s8 cl32_ident[NFSCLNT_IDMAX+1];
1718 s32 cl32_naddr;
1719 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1720 s32 cl32_fhkeytype;
1721 s32 cl32_fhkeylen;
1722 u8 cl32_fhkey[NFSCLNT_KEYMAX];
1725 struct compat_nfsctl_export {
1726 char ex32_client[NFSCLNT_IDMAX+1];
1727 char ex32_path[NFS_MAXPATHLEN+1];
1728 compat_dev_t ex32_dev;
1729 compat_ino_t ex32_ino;
1730 compat_int_t ex32_flags;
1731 __compat_uid_t ex32_anon_uid;
1732 __compat_gid_t ex32_anon_gid;
1735 struct compat_nfsctl_fdparm {
1736 struct sockaddr gd32_addr;
1737 s8 gd32_path[NFS_MAXPATHLEN+1];
1738 compat_int_t gd32_version;
1741 struct compat_nfsctl_fsparm {
1742 struct sockaddr gd32_addr;
1743 s8 gd32_path[NFS_MAXPATHLEN+1];
1744 compat_int_t gd32_maxlen;
1747 struct compat_nfsctl_arg {
1748 compat_int_t ca32_version; /* safeguard */
1749 union {
1750 struct compat_nfsctl_svc u32_svc;
1751 struct compat_nfsctl_client u32_client;
1752 struct compat_nfsctl_export u32_export;
1753 struct compat_nfsctl_fdparm u32_getfd;
1754 struct compat_nfsctl_fsparm u32_getfs;
1755 } u;
1756 #define ca32_svc u.u32_svc
1757 #define ca32_client u.u32_client
1758 #define ca32_export u.u32_export
1759 #define ca32_getfd u.u32_getfd
1760 #define ca32_getfs u.u32_getfs
1763 union compat_nfsctl_res {
1764 __u8 cr32_getfh[NFS_FHSIZE];
1765 struct knfsd_fh cr32_getfs;
1768 static int compat_nfs_svc_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1770 int err;
1772 err = access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc));
1773 err |= get_user(karg->ca_version, &arg->ca32_version);
1774 err |= __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port);
1775 err |= __get_user(karg->ca_svc.svc_nthreads, &arg->ca32_svc.svc32_nthreads);
1776 return (err) ? -EFAULT : 0;
1779 static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1781 int err;
1783 err = access_ok(VERIFY_READ, &arg->ca32_client, sizeof(arg->ca32_client));
1784 err |= get_user(karg->ca_version, &arg->ca32_version);
1785 err |= __copy_from_user(&karg->ca_client.cl_ident[0],
1786 &arg->ca32_client.cl32_ident[0],
1787 NFSCLNT_IDMAX);
1788 err |= __get_user(karg->ca_client.cl_naddr, &arg->ca32_client.cl32_naddr);
1789 err |= __copy_from_user(&karg->ca_client.cl_addrlist[0],
1790 &arg->ca32_client.cl32_addrlist[0],
1791 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX));
1792 err |= __get_user(karg->ca_client.cl_fhkeytype,
1793 &arg->ca32_client.cl32_fhkeytype);
1794 err |= __get_user(karg->ca_client.cl_fhkeylen,
1795 &arg->ca32_client.cl32_fhkeylen);
1796 err |= __copy_from_user(&karg->ca_client.cl_fhkey[0],
1797 &arg->ca32_client.cl32_fhkey[0],
1798 NFSCLNT_KEYMAX);
1800 return (err) ? -EFAULT : 0;
1803 static int compat_nfs_exp_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1805 int err;
1807 err = access_ok(VERIFY_READ, &arg->ca32_export, sizeof(arg->ca32_export));
1808 err |= get_user(karg->ca_version, &arg->ca32_version);
1809 err |= __copy_from_user(&karg->ca_export.ex_client[0],
1810 &arg->ca32_export.ex32_client[0],
1811 NFSCLNT_IDMAX);
1812 err |= __copy_from_user(&karg->ca_export.ex_path[0],
1813 &arg->ca32_export.ex32_path[0],
1814 NFS_MAXPATHLEN);
1815 err |= __get_user(karg->ca_export.ex_dev,
1816 &arg->ca32_export.ex32_dev);
1817 err |= __get_user(karg->ca_export.ex_ino,
1818 &arg->ca32_export.ex32_ino);
1819 err |= __get_user(karg->ca_export.ex_flags,
1820 &arg->ca32_export.ex32_flags);
1821 err |= __get_user(karg->ca_export.ex_anon_uid,
1822 &arg->ca32_export.ex32_anon_uid);
1823 err |= __get_user(karg->ca_export.ex_anon_gid,
1824 &arg->ca32_export.ex32_anon_gid);
1825 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
1826 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
1828 return (err) ? -EFAULT : 0;
1831 static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1833 int err;
1835 err = access_ok(VERIFY_READ, &arg->ca32_getfd, sizeof(arg->ca32_getfd));
1836 err |= get_user(karg->ca_version, &arg->ca32_version);
1837 err |= __copy_from_user(&karg->ca_getfd.gd_addr,
1838 &arg->ca32_getfd.gd32_addr,
1839 (sizeof(struct sockaddr)));
1840 err |= __copy_from_user(&karg->ca_getfd.gd_path,
1841 &arg->ca32_getfd.gd32_path,
1842 (NFS_MAXPATHLEN+1));
1843 err |= __get_user(karg->ca_getfd.gd_version,
1844 &arg->ca32_getfd.gd32_version);
1846 return (err) ? -EFAULT : 0;
1849 static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1851 int err;
1853 err = access_ok(VERIFY_READ, &arg->ca32_getfs, sizeof(arg->ca32_getfs));
1854 err |= get_user(karg->ca_version, &arg->ca32_version);
1855 err |= __copy_from_user(&karg->ca_getfs.gd_addr,
1856 &arg->ca32_getfs.gd32_addr,
1857 (sizeof(struct sockaddr)));
1858 err |= __copy_from_user(&karg->ca_getfs.gd_path,
1859 &arg->ca32_getfs.gd32_path,
1860 (NFS_MAXPATHLEN+1));
1861 err |= __get_user(karg->ca_getfs.gd_maxlen,
1862 &arg->ca32_getfs.gd32_maxlen);
1864 return (err) ? -EFAULT : 0;
1867 /* This really doesn't need translations, we are only passing
1868 * back a union which contains opaque nfs file handle data.
1870 static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, union compat_nfsctl_res __user *res)
1872 int err;
1874 err = copy_to_user(res, kres, sizeof(*res));
1876 return (err) ? -EFAULT : 0;
1879 asmlinkage long compat_sys_nfsservctl(int cmd, struct compat_nfsctl_arg __user *arg,
1880 union compat_nfsctl_res __user *res)
1882 struct nfsctl_arg *karg;
1883 union nfsctl_res *kres;
1884 mm_segment_t oldfs;
1885 int err;
1887 karg = kmalloc(sizeof(*karg), GFP_USER);
1888 kres = kmalloc(sizeof(*kres), GFP_USER);
1889 if(!karg || !kres) {
1890 err = -ENOMEM;
1891 goto done;
1894 switch(cmd) {
1895 case NFSCTL_SVC:
1896 err = compat_nfs_svc_trans(karg, arg);
1897 break;
1899 case NFSCTL_ADDCLIENT:
1900 err = compat_nfs_clnt_trans(karg, arg);
1901 break;
1903 case NFSCTL_DELCLIENT:
1904 err = compat_nfs_clnt_trans(karg, arg);
1905 break;
1907 case NFSCTL_EXPORT:
1908 case NFSCTL_UNEXPORT:
1909 err = compat_nfs_exp_trans(karg, arg);
1910 break;
1912 case NFSCTL_GETFD:
1913 err = compat_nfs_getfd_trans(karg, arg);
1914 break;
1916 case NFSCTL_GETFS:
1917 err = compat_nfs_getfs_trans(karg, arg);
1918 break;
1920 default:
1921 err = -EINVAL;
1922 goto done;
1925 oldfs = get_fs();
1926 set_fs(KERNEL_DS);
1927 /* The __user pointer casts are valid because of the set_fs() */
1928 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
1929 set_fs(oldfs);
1931 if (err)
1932 goto done;
1934 if((cmd == NFSCTL_GETFD) ||
1935 (cmd == NFSCTL_GETFS))
1936 err = compat_nfs_getfh_res_trans(kres, res);
1938 done:
1939 kfree(karg);
1940 kfree(kres);
1941 return err;
1943 #else /* !NFSD */
1944 long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
1946 return sys_ni_syscall();
1948 #endif