Card with WEP enabled and using shared-key auth will have firmware
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / compat.c
blob8e71cdbecc7c78cc18f2e14f758b8493e767a07c
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>
47 #include <linux/acct.h>
48 #include <linux/mm.h>
50 #include <net/sock.h> /* siocdevprivate_ioctl */
52 #include <asm/uaccess.h>
53 #include <asm/mmu_context.h>
54 #include <asm/ioctls.h>
57 * Not all architectures have sys_utime, so implement this in terms
58 * of sys_utimes.
60 asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
62 struct timeval tv[2];
64 if (t) {
65 if (get_user(tv[0].tv_sec, &t->actime) ||
66 get_user(tv[1].tv_sec, &t->modtime))
67 return -EFAULT;
68 tv[0].tv_usec = 0;
69 tv[1].tv_usec = 0;
71 return do_utimes(filename, t ? tv : NULL);
74 asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
76 struct timeval tv[2];
78 if (t) {
79 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
80 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
81 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
82 get_user(tv[1].tv_usec, &t[1].tv_usec))
83 return -EFAULT;
85 return do_utimes(filename, t ? tv : NULL);
88 asmlinkage long compat_sys_newstat(char __user * filename,
89 struct compat_stat __user *statbuf)
91 struct kstat stat;
92 int error = vfs_stat(filename, &stat);
94 if (!error)
95 error = cp_compat_stat(&stat, statbuf);
96 return error;
99 asmlinkage long compat_sys_newlstat(char __user * filename,
100 struct compat_stat __user *statbuf)
102 struct kstat stat;
103 int error = vfs_lstat(filename, &stat);
105 if (!error)
106 error = cp_compat_stat(&stat, statbuf);
107 return error;
110 asmlinkage long compat_sys_newfstat(unsigned int fd,
111 struct compat_stat __user * statbuf)
113 struct kstat stat;
114 int error = vfs_fstat(fd, &stat);
116 if (!error)
117 error = cp_compat_stat(&stat, statbuf);
118 return error;
121 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
124 if (sizeof ubuf->f_blocks == 4) {
125 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
126 0xffffffff00000000ULL)
127 return -EOVERFLOW;
128 /* f_files and f_ffree may be -1; it's okay
129 * to stuff that into 32 bits */
130 if (kbuf->f_files != 0xffffffffffffffffULL
131 && (kbuf->f_files & 0xffffffff00000000ULL))
132 return -EOVERFLOW;
133 if (kbuf->f_ffree != 0xffffffffffffffffULL
134 && (kbuf->f_ffree & 0xffffffff00000000ULL))
135 return -EOVERFLOW;
137 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
138 __put_user(kbuf->f_type, &ubuf->f_type) ||
139 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
140 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
141 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
142 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
143 __put_user(kbuf->f_files, &ubuf->f_files) ||
144 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
145 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
146 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
147 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
148 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
149 __put_user(0, &ubuf->f_spare[0]) ||
150 __put_user(0, &ubuf->f_spare[1]) ||
151 __put_user(0, &ubuf->f_spare[2]) ||
152 __put_user(0, &ubuf->f_spare[3]) ||
153 __put_user(0, &ubuf->f_spare[4]))
154 return -EFAULT;
155 return 0;
159 * The following statfs calls are copies of code from fs/open.c and
160 * should be checked against those from time to time
162 asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
164 struct nameidata nd;
165 int error;
167 error = user_path_walk(path, &nd);
168 if (!error) {
169 struct kstatfs tmp;
170 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
171 if (!error && put_compat_statfs(buf, &tmp))
172 error = -EFAULT;
173 path_release(&nd);
175 return error;
178 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
180 struct file * file;
181 struct kstatfs tmp;
182 int error;
184 error = -EBADF;
185 file = fget(fd);
186 if (!file)
187 goto out;
188 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
189 if (!error && put_compat_statfs(buf, &tmp))
190 error = -EFAULT;
191 fput(file);
192 out:
193 return error;
196 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
198 if (sizeof ubuf->f_blocks == 4) {
199 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
200 0xffffffff00000000ULL)
201 return -EOVERFLOW;
202 /* f_files and f_ffree may be -1; it's okay
203 * to stuff that into 32 bits */
204 if (kbuf->f_files != 0xffffffffffffffffULL
205 && (kbuf->f_files & 0xffffffff00000000ULL))
206 return -EOVERFLOW;
207 if (kbuf->f_ffree != 0xffffffffffffffffULL
208 && (kbuf->f_ffree & 0xffffffff00000000ULL))
209 return -EOVERFLOW;
211 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
212 __put_user(kbuf->f_type, &ubuf->f_type) ||
213 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
214 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
215 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
216 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
217 __put_user(kbuf->f_files, &ubuf->f_files) ||
218 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
219 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
220 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
221 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
222 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
223 return -EFAULT;
224 return 0;
227 asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
229 struct nameidata nd;
230 int error;
232 if (sz != sizeof(*buf))
233 return -EINVAL;
235 error = user_path_walk(path, &nd);
236 if (!error) {
237 struct kstatfs tmp;
238 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
239 if (!error && put_compat_statfs64(buf, &tmp))
240 error = -EFAULT;
241 path_release(&nd);
243 return error;
246 asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
248 struct file * file;
249 struct kstatfs tmp;
250 int error;
252 if (sz != sizeof(*buf))
253 return -EINVAL;
255 error = -EBADF;
256 file = fget(fd);
257 if (!file)
258 goto out;
259 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
260 if (!error && put_compat_statfs64(buf, &tmp))
261 error = -EFAULT;
262 fput(file);
263 out:
264 return error;
267 /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
269 #define IOCTL_HASHSIZE 256
270 static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
271 static DECLARE_RWSEM(ioctl32_sem);
273 extern struct ioctl_trans ioctl_start[];
274 extern int ioctl_table_size;
276 static inline unsigned long ioctl32_hash(unsigned long cmd)
278 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
281 static void ioctl32_insert_translation(struct ioctl_trans *trans)
283 unsigned long hash;
284 struct ioctl_trans *t;
286 hash = ioctl32_hash (trans->cmd);
287 if (!ioctl32_hash_table[hash])
288 ioctl32_hash_table[hash] = trans;
289 else {
290 t = ioctl32_hash_table[hash];
291 while (t->next)
292 t = t->next;
293 trans->next = NULL;
294 t->next = trans;
298 static int __init init_sys32_ioctl(void)
300 int i;
302 for (i = 0; i < ioctl_table_size; i++) {
303 if (ioctl_start[i].next != 0) {
304 printk("ioctl translation %d bad\n",i);
305 return -1;
308 ioctl32_insert_translation(&ioctl_start[i]);
310 return 0;
313 __initcall(init_sys32_ioctl);
315 static void compat_ioctl_error(struct file *filp, unsigned int fd,
316 unsigned int cmd, unsigned long arg)
318 char buf[10];
319 char *fn = "?";
320 char *path;
322 /* find the name of the device. */
323 path = (char *)__get_free_page(GFP_KERNEL);
324 if (path) {
325 fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE);
326 if (IS_ERR(fn))
327 fn = "?";
330 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
331 if (!isprint(buf[1]))
332 sprintf(buf, "%02x", buf[1]);
333 printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
334 "cmd(%08x){%s} arg(%08x) on %s\n",
335 current->comm, current->pid,
336 (int)fd, (unsigned int)cmd, buf,
337 (unsigned int)arg, fn);
339 if (path)
340 free_page((unsigned long)path);
343 asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
344 unsigned long arg)
346 struct file *filp;
347 int error = -EBADF;
348 struct ioctl_trans *t;
349 int fput_needed;
351 filp = fget_light(fd, &fput_needed);
352 if (!filp)
353 goto out;
355 /* RED-PEN how should LSM module know it's handling 32bit? */
356 error = security_file_ioctl(filp, cmd, arg);
357 if (error)
358 goto out_fput;
361 * To allow the compat_ioctl handlers to be self contained
362 * we need to check the common ioctls here first.
363 * Just handle them with the standard handlers below.
365 switch (cmd) {
366 case FIOCLEX:
367 case FIONCLEX:
368 case FIONBIO:
369 case FIOASYNC:
370 case FIOQSIZE:
371 break;
373 case FIBMAP:
374 case FIGETBSZ:
375 case FIONREAD:
376 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
377 break;
378 /*FALL THROUGH*/
380 default:
381 if (filp->f_op && filp->f_op->compat_ioctl) {
382 error = filp->f_op->compat_ioctl(filp, cmd, arg);
383 if (error != -ENOIOCTLCMD)
384 goto out_fput;
387 if (!filp->f_op ||
388 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
389 goto do_ioctl;
390 break;
393 /* When register_ioctl32_conversion is finally gone remove
394 this lock! -AK */
395 down_read(&ioctl32_sem);
396 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
397 if (t->cmd == cmd)
398 goto found_handler;
400 up_read(&ioctl32_sem);
402 if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) &&
403 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
404 error = siocdevprivate_ioctl(fd, cmd, arg);
405 } else {
406 static int count;
408 if (++count <= 50)
409 compat_ioctl_error(filp, fd, cmd, arg);
410 error = -EINVAL;
413 goto out_fput;
415 found_handler:
416 if (t->handler) {
417 lock_kernel();
418 error = t->handler(fd, cmd, arg, filp);
419 unlock_kernel();
420 up_read(&ioctl32_sem);
421 goto out_fput;
424 up_read(&ioctl32_sem);
425 do_ioctl:
426 error = vfs_ioctl(filp, fd, cmd, arg);
427 out_fput:
428 fput_light(filp, fput_needed);
429 out:
430 return error;
433 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
435 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
436 __get_user(kfl->l_type, &ufl->l_type) ||
437 __get_user(kfl->l_whence, &ufl->l_whence) ||
438 __get_user(kfl->l_start, &ufl->l_start) ||
439 __get_user(kfl->l_len, &ufl->l_len) ||
440 __get_user(kfl->l_pid, &ufl->l_pid))
441 return -EFAULT;
442 return 0;
445 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
447 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
448 __put_user(kfl->l_type, &ufl->l_type) ||
449 __put_user(kfl->l_whence, &ufl->l_whence) ||
450 __put_user(kfl->l_start, &ufl->l_start) ||
451 __put_user(kfl->l_len, &ufl->l_len) ||
452 __put_user(kfl->l_pid, &ufl->l_pid))
453 return -EFAULT;
454 return 0;
457 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
458 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
460 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
461 __get_user(kfl->l_type, &ufl->l_type) ||
462 __get_user(kfl->l_whence, &ufl->l_whence) ||
463 __get_user(kfl->l_start, &ufl->l_start) ||
464 __get_user(kfl->l_len, &ufl->l_len) ||
465 __get_user(kfl->l_pid, &ufl->l_pid))
466 return -EFAULT;
467 return 0;
469 #endif
471 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
472 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
474 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
475 __put_user(kfl->l_type, &ufl->l_type) ||
476 __put_user(kfl->l_whence, &ufl->l_whence) ||
477 __put_user(kfl->l_start, &ufl->l_start) ||
478 __put_user(kfl->l_len, &ufl->l_len) ||
479 __put_user(kfl->l_pid, &ufl->l_pid))
480 return -EFAULT;
481 return 0;
483 #endif
485 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
486 unsigned long arg)
488 mm_segment_t old_fs;
489 struct flock f;
490 long ret;
492 switch (cmd) {
493 case F_GETLK:
494 case F_SETLK:
495 case F_SETLKW:
496 ret = get_compat_flock(&f, compat_ptr(arg));
497 if (ret != 0)
498 break;
499 old_fs = get_fs();
500 set_fs(KERNEL_DS);
501 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
502 set_fs(old_fs);
503 if (cmd == F_GETLK && ret == 0) {
504 if ((f.l_start >= COMPAT_OFF_T_MAX) ||
505 ((f.l_start + f.l_len) > COMPAT_OFF_T_MAX))
506 ret = -EOVERFLOW;
507 if (ret == 0)
508 ret = put_compat_flock(&f, compat_ptr(arg));
510 break;
512 case F_GETLK64:
513 case F_SETLK64:
514 case F_SETLKW64:
515 ret = get_compat_flock64(&f, compat_ptr(arg));
516 if (ret != 0)
517 break;
518 old_fs = get_fs();
519 set_fs(KERNEL_DS);
520 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
521 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
522 (unsigned long)&f);
523 set_fs(old_fs);
524 if (cmd == F_GETLK64 && ret == 0) {
525 if ((f.l_start >= COMPAT_LOFF_T_MAX) ||
526 ((f.l_start + f.l_len) > COMPAT_LOFF_T_MAX))
527 ret = -EOVERFLOW;
528 if (ret == 0)
529 ret = put_compat_flock64(&f, compat_ptr(arg));
531 break;
533 default:
534 ret = sys_fcntl(fd, cmd, arg);
535 break;
537 return ret;
540 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
541 unsigned long arg)
543 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
544 return -EINVAL;
545 return compat_sys_fcntl64(fd, cmd, arg);
548 asmlinkage long
549 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
551 long ret;
552 aio_context_t ctx64;
554 mm_segment_t oldfs = get_fs();
555 if (unlikely(get_user(ctx64, ctx32p)))
556 return -EFAULT;
558 set_fs(KERNEL_DS);
559 /* The __user pointer cast is valid because of the set_fs() */
560 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
561 set_fs(oldfs);
562 /* truncating is ok because it's a user address */
563 if (!ret)
564 ret = put_user((u32) ctx64, ctx32p);
565 return ret;
568 asmlinkage long
569 compat_sys_io_getevents(aio_context_t ctx_id,
570 unsigned long min_nr,
571 unsigned long nr,
572 struct io_event __user *events,
573 struct compat_timespec __user *timeout)
575 long ret;
576 struct timespec t;
577 struct timespec __user *ut = NULL;
579 ret = -EFAULT;
580 if (unlikely(!access_ok(VERIFY_WRITE, events,
581 nr * sizeof(struct io_event))))
582 goto out;
583 if (timeout) {
584 if (get_compat_timespec(&t, timeout))
585 goto out;
587 ut = compat_alloc_user_space(sizeof(*ut));
588 if (copy_to_user(ut, &t, sizeof(t)) )
589 goto out;
591 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
592 out:
593 return ret;
596 static inline long
597 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
599 compat_uptr_t uptr;
600 int i;
602 for (i = 0; i < nr; ++i) {
603 if (get_user(uptr, ptr32 + i))
604 return -EFAULT;
605 if (put_user(compat_ptr(uptr), ptr64 + i))
606 return -EFAULT;
608 return 0;
611 #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
613 asmlinkage long
614 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
616 struct iocb __user * __user *iocb64;
617 long ret;
619 if (unlikely(nr < 0))
620 return -EINVAL;
622 if (nr > MAX_AIO_SUBMITS)
623 nr = MAX_AIO_SUBMITS;
625 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
626 ret = copy_iocb(nr, iocb, iocb64);
627 if (!ret)
628 ret = sys_io_submit(ctx_id, nr, iocb64);
629 return ret;
632 struct compat_ncp_mount_data {
633 compat_int_t version;
634 compat_uint_t ncp_fd;
635 __compat_uid_t mounted_uid;
636 compat_pid_t wdog_pid;
637 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
638 compat_uint_t time_out;
639 compat_uint_t retry_count;
640 compat_uint_t flags;
641 __compat_uid_t uid;
642 __compat_gid_t gid;
643 compat_mode_t file_mode;
644 compat_mode_t dir_mode;
647 struct compat_ncp_mount_data_v4 {
648 compat_int_t version;
649 compat_ulong_t flags;
650 compat_ulong_t mounted_uid;
651 compat_long_t wdog_pid;
652 compat_uint_t ncp_fd;
653 compat_uint_t time_out;
654 compat_uint_t retry_count;
655 compat_ulong_t uid;
656 compat_ulong_t gid;
657 compat_ulong_t file_mode;
658 compat_ulong_t dir_mode;
661 static void *do_ncp_super_data_conv(void *raw_data)
663 int version = *(unsigned int *)raw_data;
665 if (version == 3) {
666 struct compat_ncp_mount_data *c_n = raw_data;
667 struct ncp_mount_data *n = raw_data;
669 n->dir_mode = c_n->dir_mode;
670 n->file_mode = c_n->file_mode;
671 n->gid = c_n->gid;
672 n->uid = c_n->uid;
673 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
674 n->wdog_pid = c_n->wdog_pid;
675 n->mounted_uid = c_n->mounted_uid;
676 } else if (version == 4) {
677 struct compat_ncp_mount_data_v4 *c_n = raw_data;
678 struct ncp_mount_data_v4 *n = raw_data;
680 n->dir_mode = c_n->dir_mode;
681 n->file_mode = c_n->file_mode;
682 n->gid = c_n->gid;
683 n->uid = c_n->uid;
684 n->retry_count = c_n->retry_count;
685 n->time_out = c_n->time_out;
686 n->ncp_fd = c_n->ncp_fd;
687 n->wdog_pid = c_n->wdog_pid;
688 n->mounted_uid = c_n->mounted_uid;
689 n->flags = c_n->flags;
690 } else if (version != 5) {
691 return NULL;
694 return raw_data;
697 struct compat_smb_mount_data {
698 compat_int_t version;
699 __compat_uid_t mounted_uid;
700 __compat_uid_t uid;
701 __compat_gid_t gid;
702 compat_mode_t file_mode;
703 compat_mode_t dir_mode;
706 static void *do_smb_super_data_conv(void *raw_data)
708 struct smb_mount_data *s = raw_data;
709 struct compat_smb_mount_data *c_s = raw_data;
711 if (c_s->version != SMB_MOUNT_OLDVERSION)
712 goto out;
713 s->dir_mode = c_s->dir_mode;
714 s->file_mode = c_s->file_mode;
715 s->gid = c_s->gid;
716 s->uid = c_s->uid;
717 s->mounted_uid = c_s->mounted_uid;
718 out:
719 return raw_data;
722 struct compat_nfs_string {
723 compat_uint_t len;
724 compat_uptr_t data;
727 static inline void compat_nfs_string(struct nfs_string *dst,
728 struct compat_nfs_string *src)
730 dst->data = compat_ptr(src->data);
731 dst->len = src->len;
734 struct compat_nfs4_mount_data_v1 {
735 compat_int_t version;
736 compat_int_t flags;
737 compat_int_t rsize;
738 compat_int_t wsize;
739 compat_int_t timeo;
740 compat_int_t retrans;
741 compat_int_t acregmin;
742 compat_int_t acregmax;
743 compat_int_t acdirmin;
744 compat_int_t acdirmax;
745 struct compat_nfs_string client_addr;
746 struct compat_nfs_string mnt_path;
747 struct compat_nfs_string hostname;
748 compat_uint_t host_addrlen;
749 compat_uptr_t host_addr;
750 compat_int_t proto;
751 compat_int_t auth_flavourlen;
752 compat_uptr_t auth_flavours;
755 static int do_nfs4_super_data_conv(void *raw_data)
757 int version = *(compat_uint_t *) raw_data;
759 if (version == 1) {
760 struct compat_nfs4_mount_data_v1 *raw = raw_data;
761 struct nfs4_mount_data *real = raw_data;
763 /* copy the fields backwards */
764 real->auth_flavours = compat_ptr(raw->auth_flavours);
765 real->auth_flavourlen = raw->auth_flavourlen;
766 real->proto = raw->proto;
767 real->host_addr = compat_ptr(raw->host_addr);
768 real->host_addrlen = raw->host_addrlen;
769 compat_nfs_string(&real->hostname, &raw->hostname);
770 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
771 compat_nfs_string(&real->client_addr, &raw->client_addr);
772 real->acdirmax = raw->acdirmax;
773 real->acdirmin = raw->acdirmin;
774 real->acregmax = raw->acregmax;
775 real->acregmin = raw->acregmin;
776 real->retrans = raw->retrans;
777 real->timeo = raw->timeo;
778 real->wsize = raw->wsize;
779 real->rsize = raw->rsize;
780 real->flags = raw->flags;
781 real->version = raw->version;
783 else {
784 return -EINVAL;
787 return 0;
790 extern int copy_mount_options (const void __user *, unsigned long *);
792 #define SMBFS_NAME "smbfs"
793 #define NCPFS_NAME "ncpfs"
794 #define NFS4_NAME "nfs4"
796 asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
797 char __user * type, unsigned long flags,
798 void __user * data)
800 unsigned long type_page;
801 unsigned long data_page;
802 unsigned long dev_page;
803 char *dir_page;
804 int retval;
806 retval = copy_mount_options (type, &type_page);
807 if (retval < 0)
808 goto out;
810 dir_page = getname(dir_name);
811 retval = PTR_ERR(dir_page);
812 if (IS_ERR(dir_page))
813 goto out1;
815 retval = copy_mount_options (dev_name, &dev_page);
816 if (retval < 0)
817 goto out2;
819 retval = copy_mount_options (data, &data_page);
820 if (retval < 0)
821 goto out3;
823 retval = -EINVAL;
825 if (type_page) {
826 if (!strcmp((char *)type_page, SMBFS_NAME)) {
827 do_smb_super_data_conv((void *)data_page);
828 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
829 do_ncp_super_data_conv((void *)data_page);
830 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
831 if (do_nfs4_super_data_conv((void *) data_page))
832 goto out4;
836 lock_kernel();
837 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
838 flags, (void*)data_page);
839 unlock_kernel();
841 out4:
842 free_page(data_page);
843 out3:
844 free_page(dev_page);
845 out2:
846 putname(dir_page);
847 out1:
848 free_page(type_page);
849 out:
850 return retval;
853 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
854 #define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
855 ~(sizeof(compat_long_t)-1))
857 struct compat_old_linux_dirent {
858 compat_ulong_t d_ino;
859 compat_ulong_t d_offset;
860 unsigned short d_namlen;
861 char d_name[1];
864 struct compat_readdir_callback {
865 struct compat_old_linux_dirent __user *dirent;
866 int result;
869 static int compat_fillonedir(void *__buf, const char *name, int namlen,
870 loff_t offset, ino_t ino, unsigned int d_type)
872 struct compat_readdir_callback *buf = __buf;
873 struct compat_old_linux_dirent __user *dirent;
875 if (buf->result)
876 return -EINVAL;
877 buf->result++;
878 dirent = buf->dirent;
879 if (!access_ok(VERIFY_WRITE, dirent,
880 (unsigned long)(dirent->d_name + namlen + 1) -
881 (unsigned long)dirent))
882 goto efault;
883 if ( __put_user(ino, &dirent->d_ino) ||
884 __put_user(offset, &dirent->d_offset) ||
885 __put_user(namlen, &dirent->d_namlen) ||
886 __copy_to_user(dirent->d_name, name, namlen) ||
887 __put_user(0, dirent->d_name + namlen))
888 goto efault;
889 return 0;
890 efault:
891 buf->result = -EFAULT;
892 return -EFAULT;
895 asmlinkage long compat_sys_old_readdir(unsigned int fd,
896 struct compat_old_linux_dirent __user *dirent, unsigned int count)
898 int error;
899 struct file *file;
900 struct compat_readdir_callback buf;
902 error = -EBADF;
903 file = fget(fd);
904 if (!file)
905 goto out;
907 buf.result = 0;
908 buf.dirent = dirent;
910 error = vfs_readdir(file, compat_fillonedir, &buf);
911 if (error >= 0)
912 error = buf.result;
914 fput(file);
915 out:
916 return error;
919 struct compat_linux_dirent {
920 compat_ulong_t d_ino;
921 compat_ulong_t d_off;
922 unsigned short d_reclen;
923 char d_name[1];
926 struct compat_getdents_callback {
927 struct compat_linux_dirent __user *current_dir;
928 struct compat_linux_dirent __user *previous;
929 int count;
930 int error;
933 static int compat_filldir(void *__buf, const char *name, int namlen,
934 loff_t offset, ino_t ino, unsigned int d_type)
936 struct compat_linux_dirent __user * dirent;
937 struct compat_getdents_callback *buf = __buf;
938 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
940 buf->error = -EINVAL; /* only used if we fail.. */
941 if (reclen > buf->count)
942 return -EINVAL;
943 dirent = buf->previous;
944 if (dirent) {
945 if (__put_user(offset, &dirent->d_off))
946 goto efault;
948 dirent = buf->current_dir;
949 if (__put_user(ino, &dirent->d_ino))
950 goto efault;
951 if (__put_user(reclen, &dirent->d_reclen))
952 goto efault;
953 if (copy_to_user(dirent->d_name, name, namlen))
954 goto efault;
955 if (__put_user(0, dirent->d_name + namlen))
956 goto efault;
957 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
958 goto efault;
959 buf->previous = dirent;
960 dirent = (void __user *)dirent + reclen;
961 buf->current_dir = dirent;
962 buf->count -= reclen;
963 return 0;
964 efault:
965 buf->error = -EFAULT;
966 return -EFAULT;
969 asmlinkage long compat_sys_getdents(unsigned int fd,
970 struct compat_linux_dirent __user *dirent, unsigned int count)
972 struct file * file;
973 struct compat_linux_dirent __user * lastdirent;
974 struct compat_getdents_callback buf;
975 int error;
977 error = -EFAULT;
978 if (!access_ok(VERIFY_WRITE, dirent, count))
979 goto out;
981 error = -EBADF;
982 file = fget(fd);
983 if (!file)
984 goto out;
986 buf.current_dir = dirent;
987 buf.previous = NULL;
988 buf.count = count;
989 buf.error = 0;
991 error = vfs_readdir(file, compat_filldir, &buf);
992 if (error < 0)
993 goto out_putf;
994 error = buf.error;
995 lastdirent = buf.previous;
996 if (lastdirent) {
997 if (put_user(file->f_pos, &lastdirent->d_off))
998 error = -EFAULT;
999 else
1000 error = count - buf.count;
1003 out_putf:
1004 fput(file);
1005 out:
1006 return error;
1009 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1010 #define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1012 struct compat_getdents_callback64 {
1013 struct linux_dirent64 __user *current_dir;
1014 struct linux_dirent64 __user *previous;
1015 int count;
1016 int error;
1019 static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1020 ino_t ino, unsigned int d_type)
1022 struct linux_dirent64 __user *dirent;
1023 struct compat_getdents_callback64 *buf = __buf;
1024 int jj = NAME_OFFSET(dirent);
1025 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1026 u64 off;
1028 buf->error = -EINVAL; /* only used if we fail.. */
1029 if (reclen > buf->count)
1030 return -EINVAL;
1031 dirent = buf->previous;
1033 if (dirent) {
1034 if (__put_user_unaligned(offset, &dirent->d_off))
1035 goto efault;
1037 dirent = buf->current_dir;
1038 if (__put_user_unaligned(ino, &dirent->d_ino))
1039 goto efault;
1040 off = 0;
1041 if (__put_user_unaligned(off, &dirent->d_off))
1042 goto efault;
1043 if (__put_user(reclen, &dirent->d_reclen))
1044 goto efault;
1045 if (__put_user(d_type, &dirent->d_type))
1046 goto efault;
1047 if (copy_to_user(dirent->d_name, name, namlen))
1048 goto efault;
1049 if (__put_user(0, dirent->d_name + namlen))
1050 goto efault;
1051 buf->previous = dirent;
1052 dirent = (void __user *)dirent + reclen;
1053 buf->current_dir = dirent;
1054 buf->count -= reclen;
1055 return 0;
1056 efault:
1057 buf->error = -EFAULT;
1058 return -EFAULT;
1061 asmlinkage long compat_sys_getdents64(unsigned int fd,
1062 struct linux_dirent64 __user * dirent, unsigned int count)
1064 struct file * file;
1065 struct linux_dirent64 __user * lastdirent;
1066 struct compat_getdents_callback64 buf;
1067 int error;
1069 error = -EFAULT;
1070 if (!access_ok(VERIFY_WRITE, dirent, count))
1071 goto out;
1073 error = -EBADF;
1074 file = fget(fd);
1075 if (!file)
1076 goto out;
1078 buf.current_dir = dirent;
1079 buf.previous = NULL;
1080 buf.count = count;
1081 buf.error = 0;
1083 error = vfs_readdir(file, compat_filldir64, &buf);
1084 if (error < 0)
1085 goto out_putf;
1086 error = buf.error;
1087 lastdirent = buf.previous;
1088 if (lastdirent) {
1089 typeof(lastdirent->d_off) d_off = file->f_pos;
1090 __put_user_unaligned(d_off, &lastdirent->d_off);
1091 error = count - buf.count;
1094 out_putf:
1095 fput(file);
1096 out:
1097 return error;
1099 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1101 static ssize_t compat_do_readv_writev(int type, struct file *file,
1102 const struct compat_iovec __user *uvector,
1103 unsigned long nr_segs, loff_t *pos)
1105 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
1106 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
1108 compat_ssize_t tot_len;
1109 struct iovec iovstack[UIO_FASTIOV];
1110 struct iovec *iov=iovstack, *vector;
1111 ssize_t ret;
1112 int seg;
1113 io_fn_t fn;
1114 iov_fn_t fnv;
1117 * SuS says "The readv() function *may* fail if the iovcnt argument
1118 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1119 * traditionally returned zero for zero segments, so...
1121 ret = 0;
1122 if (nr_segs == 0)
1123 goto out;
1126 * First get the "struct iovec" from user memory and
1127 * verify all the pointers
1129 ret = -EINVAL;
1130 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1131 goto out;
1132 if (!file->f_op)
1133 goto out;
1134 if (nr_segs > UIO_FASTIOV) {
1135 ret = -ENOMEM;
1136 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1137 if (!iov)
1138 goto out;
1140 ret = -EFAULT;
1141 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1142 goto out;
1145 * Single unix specification:
1146 * We should -EINVAL if an element length is not >= 0 and fitting an
1147 * ssize_t. The total length is fitting an ssize_t
1149 * Be careful here because iov_len is a size_t not an ssize_t
1151 tot_len = 0;
1152 vector = iov;
1153 ret = -EINVAL;
1154 for (seg = 0 ; seg < nr_segs; seg++) {
1155 compat_ssize_t tmp = tot_len;
1156 compat_ssize_t len;
1157 compat_uptr_t buf;
1159 if (__get_user(len, &uvector->iov_len) ||
1160 __get_user(buf, &uvector->iov_base)) {
1161 ret = -EFAULT;
1162 goto out;
1164 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1165 goto out;
1166 tot_len += len;
1167 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1168 goto out;
1169 vector->iov_base = compat_ptr(buf);
1170 vector->iov_len = (compat_size_t) len;
1171 uvector++;
1172 vector++;
1174 if (tot_len == 0) {
1175 ret = 0;
1176 goto out;
1179 ret = rw_verify_area(type, file, pos, tot_len);
1180 if (ret)
1181 goto out;
1183 fnv = NULL;
1184 if (type == READ) {
1185 fn = file->f_op->read;
1186 fnv = file->f_op->readv;
1187 } else {
1188 fn = (io_fn_t)file->f_op->write;
1189 fnv = file->f_op->writev;
1191 if (fnv) {
1192 ret = fnv(file, iov, nr_segs, pos);
1193 goto out;
1196 /* Do it by hand, with file-ops */
1197 ret = 0;
1198 vector = iov;
1199 while (nr_segs > 0) {
1200 void __user * base;
1201 size_t len;
1202 ssize_t nr;
1204 base = vector->iov_base;
1205 len = vector->iov_len;
1206 vector++;
1207 nr_segs--;
1209 nr = fn(file, base, len, pos);
1211 if (nr < 0) {
1212 if (!ret) ret = nr;
1213 break;
1215 ret += nr;
1216 if (nr != len)
1217 break;
1219 out:
1220 if (iov != iovstack)
1221 kfree(iov);
1222 if ((ret + (type == READ)) > 0) {
1223 struct dentry *dentry = file->f_dentry;
1224 if (type == READ)
1225 fsnotify_access(dentry);
1226 else
1227 fsnotify_modify(dentry);
1229 return ret;
1232 asmlinkage ssize_t
1233 compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1235 struct file *file;
1236 ssize_t ret = -EBADF;
1238 file = fget(fd);
1239 if (!file)
1240 return -EBADF;
1242 if (!(file->f_mode & FMODE_READ))
1243 goto out;
1245 ret = -EINVAL;
1246 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
1247 goto out;
1249 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1251 out:
1252 fput(file);
1253 return ret;
1256 asmlinkage ssize_t
1257 compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1259 struct file *file;
1260 ssize_t ret = -EBADF;
1262 file = fget(fd);
1263 if (!file)
1264 return -EBADF;
1265 if (!(file->f_mode & FMODE_WRITE))
1266 goto out;
1268 ret = -EINVAL;
1269 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
1270 goto out;
1272 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1274 out:
1275 fput(file);
1276 return ret;
1280 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1281 * O_LARGEFILE flag.
1283 asmlinkage long
1284 compat_sys_open(const char __user *filename, int flags, int mode)
1286 return do_sys_open(filename, flags, mode);
1290 * compat_count() counts the number of arguments/envelopes. It is basically
1291 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1292 * and envp pointers.
1294 static int compat_count(compat_uptr_t __user *argv, int max)
1296 int i = 0;
1298 if (argv != NULL) {
1299 for (;;) {
1300 compat_uptr_t p;
1302 if (get_user(p, argv))
1303 return -EFAULT;
1304 if (!p)
1305 break;
1306 argv++;
1307 if(++i > max)
1308 return -E2BIG;
1311 return i;
1315 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1316 * except that it works with 32 bit argv and envp pointers.
1318 static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1319 struct linux_binprm *bprm)
1321 struct page *kmapped_page = NULL;
1322 char *kaddr = NULL;
1323 int ret;
1325 while (argc-- > 0) {
1326 compat_uptr_t str;
1327 int len;
1328 unsigned long pos;
1330 if (get_user(str, argv+argc) ||
1331 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1332 ret = -EFAULT;
1333 goto out;
1336 if (bprm->p < len) {
1337 ret = -E2BIG;
1338 goto out;
1341 bprm->p -= len;
1342 /* XXX: add architecture specific overflow check here. */
1343 pos = bprm->p;
1345 while (len > 0) {
1346 int i, new, err;
1347 int offset, bytes_to_copy;
1348 struct page *page;
1350 offset = pos % PAGE_SIZE;
1351 i = pos/PAGE_SIZE;
1352 page = bprm->page[i];
1353 new = 0;
1354 if (!page) {
1355 page = alloc_page(GFP_HIGHUSER);
1356 bprm->page[i] = page;
1357 if (!page) {
1358 ret = -ENOMEM;
1359 goto out;
1361 new = 1;
1364 if (page != kmapped_page) {
1365 if (kmapped_page)
1366 kunmap(kmapped_page);
1367 kmapped_page = page;
1368 kaddr = kmap(kmapped_page);
1370 if (new && offset)
1371 memset(kaddr, 0, offset);
1372 bytes_to_copy = PAGE_SIZE - offset;
1373 if (bytes_to_copy > len) {
1374 bytes_to_copy = len;
1375 if (new)
1376 memset(kaddr+offset+len, 0,
1377 PAGE_SIZE-offset-len);
1379 err = copy_from_user(kaddr+offset, compat_ptr(str),
1380 bytes_to_copy);
1381 if (err) {
1382 ret = -EFAULT;
1383 goto out;
1386 pos += bytes_to_copy;
1387 str += bytes_to_copy;
1388 len -= bytes_to_copy;
1391 ret = 0;
1392 out:
1393 if (kmapped_page)
1394 kunmap(kmapped_page);
1395 return ret;
1398 #ifdef CONFIG_MMU
1400 #define free_arg_pages(bprm) do { } while (0)
1402 #else
1404 static inline void free_arg_pages(struct linux_binprm *bprm)
1406 int i;
1408 for (i = 0; i < MAX_ARG_PAGES; i++) {
1409 if (bprm->page[i])
1410 __free_page(bprm->page[i]);
1411 bprm->page[i] = NULL;
1415 #endif /* CONFIG_MMU */
1418 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1419 * that it processes 32 bit argv and envp pointers.
1421 int compat_do_execve(char * filename,
1422 compat_uptr_t __user *argv,
1423 compat_uptr_t __user *envp,
1424 struct pt_regs * regs)
1426 struct linux_binprm *bprm;
1427 struct file *file;
1428 int retval;
1429 int i;
1431 retval = -ENOMEM;
1432 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1433 if (!bprm)
1434 goto out_ret;
1435 memset(bprm, 0, sizeof(*bprm));
1437 file = open_exec(filename);
1438 retval = PTR_ERR(file);
1439 if (IS_ERR(file))
1440 goto out_kfree;
1442 sched_exec();
1444 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1445 bprm->file = file;
1446 bprm->filename = filename;
1447 bprm->interp = filename;
1448 bprm->mm = mm_alloc();
1449 retval = -ENOMEM;
1450 if (!bprm->mm)
1451 goto out_file;
1453 retval = init_new_context(current, bprm->mm);
1454 if (retval < 0)
1455 goto out_mm;
1457 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1458 if ((retval = bprm->argc) < 0)
1459 goto out_mm;
1461 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1462 if ((retval = bprm->envc) < 0)
1463 goto out_mm;
1465 retval = security_bprm_alloc(bprm);
1466 if (retval)
1467 goto out;
1469 retval = prepare_binprm(bprm);
1470 if (retval < 0)
1471 goto out;
1473 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1474 if (retval < 0)
1475 goto out;
1477 bprm->exec = bprm->p;
1478 retval = compat_copy_strings(bprm->envc, envp, bprm);
1479 if (retval < 0)
1480 goto out;
1482 retval = compat_copy_strings(bprm->argc, argv, bprm);
1483 if (retval < 0)
1484 goto out;
1486 retval = search_binary_handler(bprm, regs);
1487 if (retval >= 0) {
1488 free_arg_pages(bprm);
1490 /* execve success */
1491 security_bprm_free(bprm);
1492 acct_update_integrals(current);
1493 kfree(bprm);
1494 return retval;
1497 out:
1498 /* Something went wrong, return the inode and free the argument pages*/
1499 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1500 struct page * page = bprm->page[i];
1501 if (page)
1502 __free_page(page);
1505 if (bprm->security)
1506 security_bprm_free(bprm);
1508 out_mm:
1509 if (bprm->mm)
1510 mmdrop(bprm->mm);
1512 out_file:
1513 if (bprm->file) {
1514 allow_write_access(bprm->file);
1515 fput(bprm->file);
1518 out_kfree:
1519 kfree(bprm);
1521 out_ret:
1522 return retval;
1525 #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1527 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1530 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1531 * 64-bit unsigned longs.
1533 static inline
1534 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1535 unsigned long *fdset)
1537 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1538 if (ufdset) {
1539 unsigned long odd;
1541 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1542 return -EFAULT;
1544 odd = nr & 1UL;
1545 nr &= ~1UL;
1546 while (nr) {
1547 unsigned long h, l;
1548 __get_user(l, ufdset);
1549 __get_user(h, ufdset+1);
1550 ufdset += 2;
1551 *fdset++ = h << 32 | l;
1552 nr -= 2;
1554 if (odd)
1555 __get_user(*fdset, ufdset);
1556 } else {
1557 /* Tricky, must clear full unsigned long in the
1558 * kernel fdset at the end, this makes sure that
1559 * actually happens.
1561 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1563 return 0;
1566 static inline
1567 void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1568 unsigned long *fdset)
1570 unsigned long odd;
1571 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1573 if (!ufdset)
1574 return;
1576 odd = nr & 1UL;
1577 nr &= ~1UL;
1578 while (nr) {
1579 unsigned long h, l;
1580 l = *fdset++;
1581 h = l >> 32;
1582 __put_user(l, ufdset);
1583 __put_user(h, ufdset+1);
1584 ufdset += 2;
1585 nr -= 2;
1587 if (odd)
1588 __put_user(*fdset, ufdset);
1593 * This is a virtual copy of sys_select from fs/select.c and probably
1594 * should be compared to it from time to time
1596 static void *select_bits_alloc(int size)
1598 return kmalloc(6 * size, GFP_KERNEL);
1601 static void select_bits_free(void *bits, int size)
1603 kfree(bits);
1607 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1608 * like to be certain this leads to no problems. So I return
1609 * EINTR just for safety.
1611 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1612 * I'm trying ERESTARTNOHAND which restart only when you want to.
1614 #define MAX_SELECT_SECONDS \
1615 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1617 asmlinkage long
1618 compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp,
1619 compat_ulong_t __user *exp, struct compat_timeval __user *tvp)
1621 fd_set_bits fds;
1622 char *bits;
1623 long timeout;
1624 int size, max_fdset, ret = -EINVAL;
1625 struct fdtable *fdt;
1627 timeout = MAX_SCHEDULE_TIMEOUT;
1628 if (tvp) {
1629 time_t sec, usec;
1631 if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
1632 || __get_user(sec, &tvp->tv_sec)
1633 || __get_user(usec, &tvp->tv_usec)) {
1634 ret = -EFAULT;
1635 goto out_nofds;
1638 if (sec < 0 || usec < 0)
1639 goto out_nofds;
1641 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
1642 timeout = ROUND_UP(usec, 1000000/HZ);
1643 timeout += sec * (unsigned long) HZ;
1647 if (n < 0)
1648 goto out_nofds;
1650 /* max_fdset can increase, so grab it once to avoid race */
1651 rcu_read_lock();
1652 fdt = files_fdtable(current->files);
1653 max_fdset = fdt->max_fdset;
1654 rcu_read_unlock();
1655 if (n > max_fdset)
1656 n = max_fdset;
1659 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1660 * since we used fdset we need to allocate memory in units of
1661 * long-words.
1663 ret = -ENOMEM;
1664 size = FDS_BYTES(n);
1665 bits = select_bits_alloc(size);
1666 if (!bits)
1667 goto out_nofds;
1668 fds.in = (unsigned long *) bits;
1669 fds.out = (unsigned long *) (bits + size);
1670 fds.ex = (unsigned long *) (bits + 2*size);
1671 fds.res_in = (unsigned long *) (bits + 3*size);
1672 fds.res_out = (unsigned long *) (bits + 4*size);
1673 fds.res_ex = (unsigned long *) (bits + 5*size);
1675 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1676 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1677 (ret = compat_get_fd_set(n, exp, fds.ex)))
1678 goto out;
1679 zero_fd_set(n, fds.res_in);
1680 zero_fd_set(n, fds.res_out);
1681 zero_fd_set(n, fds.res_ex);
1683 ret = do_select(n, &fds, &timeout);
1685 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
1686 time_t sec = 0, usec = 0;
1687 if (timeout) {
1688 sec = timeout / HZ;
1689 usec = timeout % HZ;
1690 usec *= (1000000/HZ);
1692 if (put_user(sec, &tvp->tv_sec) ||
1693 put_user(usec, &tvp->tv_usec))
1694 ret = -EFAULT;
1697 if (ret < 0)
1698 goto out;
1699 if (!ret) {
1700 ret = -ERESTARTNOHAND;
1701 if (signal_pending(current))
1702 goto out;
1703 ret = 0;
1706 compat_set_fd_set(n, inp, fds.res_in);
1707 compat_set_fd_set(n, outp, fds.res_out);
1708 compat_set_fd_set(n, exp, fds.res_ex);
1710 out:
1711 select_bits_free(bits, size);
1712 out_nofds:
1713 return ret;
1716 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1717 /* Stuff for NFS server syscalls... */
1718 struct compat_nfsctl_svc {
1719 u16 svc32_port;
1720 s32 svc32_nthreads;
1723 struct compat_nfsctl_client {
1724 s8 cl32_ident[NFSCLNT_IDMAX+1];
1725 s32 cl32_naddr;
1726 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1727 s32 cl32_fhkeytype;
1728 s32 cl32_fhkeylen;
1729 u8 cl32_fhkey[NFSCLNT_KEYMAX];
1732 struct compat_nfsctl_export {
1733 char ex32_client[NFSCLNT_IDMAX+1];
1734 char ex32_path[NFS_MAXPATHLEN+1];
1735 compat_dev_t ex32_dev;
1736 compat_ino_t ex32_ino;
1737 compat_int_t ex32_flags;
1738 __compat_uid_t ex32_anon_uid;
1739 __compat_gid_t ex32_anon_gid;
1742 struct compat_nfsctl_fdparm {
1743 struct sockaddr gd32_addr;
1744 s8 gd32_path[NFS_MAXPATHLEN+1];
1745 compat_int_t gd32_version;
1748 struct compat_nfsctl_fsparm {
1749 struct sockaddr gd32_addr;
1750 s8 gd32_path[NFS_MAXPATHLEN+1];
1751 compat_int_t gd32_maxlen;
1754 struct compat_nfsctl_arg {
1755 compat_int_t ca32_version; /* safeguard */
1756 union {
1757 struct compat_nfsctl_svc u32_svc;
1758 struct compat_nfsctl_client u32_client;
1759 struct compat_nfsctl_export u32_export;
1760 struct compat_nfsctl_fdparm u32_getfd;
1761 struct compat_nfsctl_fsparm u32_getfs;
1762 } u;
1763 #define ca32_svc u.u32_svc
1764 #define ca32_client u.u32_client
1765 #define ca32_export u.u32_export
1766 #define ca32_getfd u.u32_getfd
1767 #define ca32_getfs u.u32_getfs
1770 union compat_nfsctl_res {
1771 __u8 cr32_getfh[NFS_FHSIZE];
1772 struct knfsd_fh cr32_getfs;
1775 static int compat_nfs_svc_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1777 int err;
1779 err = access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc));
1780 err |= get_user(karg->ca_version, &arg->ca32_version);
1781 err |= __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port);
1782 err |= __get_user(karg->ca_svc.svc_nthreads, &arg->ca32_svc.svc32_nthreads);
1783 return (err) ? -EFAULT : 0;
1786 static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1788 int err;
1790 err = access_ok(VERIFY_READ, &arg->ca32_client, sizeof(arg->ca32_client));
1791 err |= get_user(karg->ca_version, &arg->ca32_version);
1792 err |= __copy_from_user(&karg->ca_client.cl_ident[0],
1793 &arg->ca32_client.cl32_ident[0],
1794 NFSCLNT_IDMAX);
1795 err |= __get_user(karg->ca_client.cl_naddr, &arg->ca32_client.cl32_naddr);
1796 err |= __copy_from_user(&karg->ca_client.cl_addrlist[0],
1797 &arg->ca32_client.cl32_addrlist[0],
1798 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX));
1799 err |= __get_user(karg->ca_client.cl_fhkeytype,
1800 &arg->ca32_client.cl32_fhkeytype);
1801 err |= __get_user(karg->ca_client.cl_fhkeylen,
1802 &arg->ca32_client.cl32_fhkeylen);
1803 err |= __copy_from_user(&karg->ca_client.cl_fhkey[0],
1804 &arg->ca32_client.cl32_fhkey[0],
1805 NFSCLNT_KEYMAX);
1807 return (err) ? -EFAULT : 0;
1810 static int compat_nfs_exp_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1812 int err;
1814 err = access_ok(VERIFY_READ, &arg->ca32_export, sizeof(arg->ca32_export));
1815 err |= get_user(karg->ca_version, &arg->ca32_version);
1816 err |= __copy_from_user(&karg->ca_export.ex_client[0],
1817 &arg->ca32_export.ex32_client[0],
1818 NFSCLNT_IDMAX);
1819 err |= __copy_from_user(&karg->ca_export.ex_path[0],
1820 &arg->ca32_export.ex32_path[0],
1821 NFS_MAXPATHLEN);
1822 err |= __get_user(karg->ca_export.ex_dev,
1823 &arg->ca32_export.ex32_dev);
1824 err |= __get_user(karg->ca_export.ex_ino,
1825 &arg->ca32_export.ex32_ino);
1826 err |= __get_user(karg->ca_export.ex_flags,
1827 &arg->ca32_export.ex32_flags);
1828 err |= __get_user(karg->ca_export.ex_anon_uid,
1829 &arg->ca32_export.ex32_anon_uid);
1830 err |= __get_user(karg->ca_export.ex_anon_gid,
1831 &arg->ca32_export.ex32_anon_gid);
1832 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
1833 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
1835 return (err) ? -EFAULT : 0;
1838 static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1840 int err;
1842 err = access_ok(VERIFY_READ, &arg->ca32_getfd, sizeof(arg->ca32_getfd));
1843 err |= get_user(karg->ca_version, &arg->ca32_version);
1844 err |= __copy_from_user(&karg->ca_getfd.gd_addr,
1845 &arg->ca32_getfd.gd32_addr,
1846 (sizeof(struct sockaddr)));
1847 err |= __copy_from_user(&karg->ca_getfd.gd_path,
1848 &arg->ca32_getfd.gd32_path,
1849 (NFS_MAXPATHLEN+1));
1850 err |= __get_user(karg->ca_getfd.gd_version,
1851 &arg->ca32_getfd.gd32_version);
1853 return (err) ? -EFAULT : 0;
1856 static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1858 int err;
1860 err = access_ok(VERIFY_READ, &arg->ca32_getfs, sizeof(arg->ca32_getfs));
1861 err |= get_user(karg->ca_version, &arg->ca32_version);
1862 err |= __copy_from_user(&karg->ca_getfs.gd_addr,
1863 &arg->ca32_getfs.gd32_addr,
1864 (sizeof(struct sockaddr)));
1865 err |= __copy_from_user(&karg->ca_getfs.gd_path,
1866 &arg->ca32_getfs.gd32_path,
1867 (NFS_MAXPATHLEN+1));
1868 err |= __get_user(karg->ca_getfs.gd_maxlen,
1869 &arg->ca32_getfs.gd32_maxlen);
1871 return (err) ? -EFAULT : 0;
1874 /* This really doesn't need translations, we are only passing
1875 * back a union which contains opaque nfs file handle data.
1877 static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, union compat_nfsctl_res __user *res)
1879 int err;
1881 err = copy_to_user(res, kres, sizeof(*res));
1883 return (err) ? -EFAULT : 0;
1886 asmlinkage long compat_sys_nfsservctl(int cmd, struct compat_nfsctl_arg __user *arg,
1887 union compat_nfsctl_res __user *res)
1889 struct nfsctl_arg *karg;
1890 union nfsctl_res *kres;
1891 mm_segment_t oldfs;
1892 int err;
1894 karg = kmalloc(sizeof(*karg), GFP_USER);
1895 kres = kmalloc(sizeof(*kres), GFP_USER);
1896 if(!karg || !kres) {
1897 err = -ENOMEM;
1898 goto done;
1901 switch(cmd) {
1902 case NFSCTL_SVC:
1903 err = compat_nfs_svc_trans(karg, arg);
1904 break;
1906 case NFSCTL_ADDCLIENT:
1907 err = compat_nfs_clnt_trans(karg, arg);
1908 break;
1910 case NFSCTL_DELCLIENT:
1911 err = compat_nfs_clnt_trans(karg, arg);
1912 break;
1914 case NFSCTL_EXPORT:
1915 case NFSCTL_UNEXPORT:
1916 err = compat_nfs_exp_trans(karg, arg);
1917 break;
1919 case NFSCTL_GETFD:
1920 err = compat_nfs_getfd_trans(karg, arg);
1921 break;
1923 case NFSCTL_GETFS:
1924 err = compat_nfs_getfs_trans(karg, arg);
1925 break;
1927 default:
1928 err = -EINVAL;
1929 goto done;
1932 oldfs = get_fs();
1933 set_fs(KERNEL_DS);
1934 /* The __user pointer casts are valid because of the set_fs() */
1935 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
1936 set_fs(oldfs);
1938 if (err)
1939 goto done;
1941 if((cmd == NFSCTL_GETFD) ||
1942 (cmd == NFSCTL_GETFS))
1943 err = compat_nfs_getfh_res_trans(kres, res);
1945 done:
1946 kfree(karg);
1947 kfree(kres);
1948 return err;
1950 #else /* !NFSD */
1951 long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
1953 return sys_ni_syscall();
1955 #endif