blob: c6ba9deabada4a54f201fee076f9b28ab2915e54 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/compat.c
3 *
4 * Kernel compatibililty routines for e.g. 32 bit syscall support
5 * on 64 bit kernels.
6 *
7 * Copyright (C) 2002 Stephen Rothwell, IBM Corporation
8 * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
9 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
10 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
11 * Copyright (C) 2003 Pavel Machek (pavel@suse.cz)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#include <linux/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>
David Howells9a9947b2005-04-18 10:54:51 -070034#include <linux/nfs4_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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>
Robert Love0eeca282005-07-12 17:06:03 -040040#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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>
David S. Miller4a805e82005-09-14 21:40:00 -070047#include <linux/acct.h>
48#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <net/sock.h> /* siocdevprivate_ioctl */
51
52#include <asm/uaccess.h>
53#include <asm/mmu_context.h>
54#include <asm/ioctls.h>
55
56/*
57 * Not all architectures have sys_utime, so implement this in terms
58 * of sys_utimes.
59 */
60asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
61{
62 struct timeval tv[2];
63
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;
70 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -080071 return do_utimes(AT_FDCWD, filename, t ? tv : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Ulrich Drepper5590ff02006-01-18 17:43:53 -080074asmlinkage long compat_sys_futimesat(int dfd, char __user *filename, struct compat_timeval __user *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 struct timeval tv[2];
77
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;
84 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -080085 return do_utimes(dfd, filename, t ? tv : NULL);
86}
87
88asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
89{
90 return compat_sys_futimesat(AT_FDCWD, filename, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93asmlinkage long compat_sys_newstat(char __user * filename,
94 struct compat_stat __user *statbuf)
95{
96 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -080097 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 if (!error)
100 error = cp_compat_stat(&stat, statbuf);
101 return error;
102}
103
104asmlinkage long compat_sys_newlstat(char __user * filename,
105 struct compat_stat __user *statbuf)
106{
107 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800108 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (!error)
111 error = cp_compat_stat(&stat, statbuf);
112 return error;
113}
114
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800115asmlinkage long compat_sys_newfstatat(int dfd, char __user *filename,
116 struct compat_stat __user *statbuf, int flag)
117{
118 struct kstat stat;
119 int error = -EINVAL;
120
121 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
122 goto out;
123
124 if (flag & AT_SYMLINK_NOFOLLOW)
125 error = vfs_lstat_fd(dfd, filename, &stat);
126 else
127 error = vfs_stat_fd(dfd, filename, &stat);
128
129 if (!error)
130 error = cp_compat_stat(&stat, statbuf);
131
132out:
133 return error;
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136asmlinkage long compat_sys_newfstat(unsigned int fd,
137 struct compat_stat __user * statbuf)
138{
139 struct kstat stat;
140 int error = vfs_fstat(fd, &stat);
141
142 if (!error)
143 error = cp_compat_stat(&stat, statbuf);
144 return error;
145}
146
147static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
148{
149
150 if (sizeof ubuf->f_blocks == 4) {
151 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
152 0xffffffff00000000ULL)
153 return -EOVERFLOW;
154 /* f_files and f_ffree may be -1; it's okay
155 * to stuff that into 32 bits */
156 if (kbuf->f_files != 0xffffffffffffffffULL
157 && (kbuf->f_files & 0xffffffff00000000ULL))
158 return -EOVERFLOW;
159 if (kbuf->f_ffree != 0xffffffffffffffffULL
160 && (kbuf->f_ffree & 0xffffffff00000000ULL))
161 return -EOVERFLOW;
162 }
163 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
164 __put_user(kbuf->f_type, &ubuf->f_type) ||
165 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
166 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
167 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
168 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
169 __put_user(kbuf->f_files, &ubuf->f_files) ||
170 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
171 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
172 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
173 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
174 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
175 __put_user(0, &ubuf->f_spare[0]) ||
176 __put_user(0, &ubuf->f_spare[1]) ||
177 __put_user(0, &ubuf->f_spare[2]) ||
178 __put_user(0, &ubuf->f_spare[3]) ||
179 __put_user(0, &ubuf->f_spare[4]))
180 return -EFAULT;
181 return 0;
182}
183
184/*
185 * The following statfs calls are copies of code from fs/open.c and
186 * should be checked against those from time to time
187 */
188asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
189{
190 struct nameidata nd;
191 int error;
192
193 error = user_path_walk(path, &nd);
194 if (!error) {
195 struct kstatfs tmp;
196 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800197 if (!error)
198 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 path_release(&nd);
200 }
201 return error;
202}
203
204asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
205{
206 struct file * file;
207 struct kstatfs tmp;
208 int error;
209
210 error = -EBADF;
211 file = fget(fd);
212 if (!file)
213 goto out;
214 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800215 if (!error)
216 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 fput(file);
218out:
219 return error;
220}
221
222static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
223{
224 if (sizeof ubuf->f_blocks == 4) {
225 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
226 0xffffffff00000000ULL)
227 return -EOVERFLOW;
228 /* f_files and f_ffree may be -1; it's okay
229 * to stuff that into 32 bits */
230 if (kbuf->f_files != 0xffffffffffffffffULL
231 && (kbuf->f_files & 0xffffffff00000000ULL))
232 return -EOVERFLOW;
233 if (kbuf->f_ffree != 0xffffffffffffffffULL
234 && (kbuf->f_ffree & 0xffffffff00000000ULL))
235 return -EOVERFLOW;
236 }
237 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
238 __put_user(kbuf->f_type, &ubuf->f_type) ||
239 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
240 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
241 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
242 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
243 __put_user(kbuf->f_files, &ubuf->f_files) ||
244 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
245 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
246 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
247 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
248 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
249 return -EFAULT;
250 return 0;
251}
252
253asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
254{
255 struct nameidata nd;
256 int error;
257
258 if (sz != sizeof(*buf))
259 return -EINVAL;
260
261 error = user_path_walk(path, &nd);
262 if (!error) {
263 struct kstatfs tmp;
264 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800265 if (!error)
266 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 path_release(&nd);
268 }
269 return error;
270}
271
272asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
273{
274 struct file * file;
275 struct kstatfs tmp;
276 int error;
277
278 if (sz != sizeof(*buf))
279 return -EINVAL;
280
281 error = -EBADF;
282 file = fget(fd);
283 if (!file)
284 goto out;
285 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800286 if (!error)
287 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 fput(file);
289out:
290 return error;
291}
292
293/* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
294
295#define IOCTL_HASHSIZE 256
296static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298extern struct ioctl_trans ioctl_start[];
299extern int ioctl_table_size;
300
301static inline unsigned long ioctl32_hash(unsigned long cmd)
302{
303 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
304}
305
306static void ioctl32_insert_translation(struct ioctl_trans *trans)
307{
308 unsigned long hash;
309 struct ioctl_trans *t;
310
311 hash = ioctl32_hash (trans->cmd);
312 if (!ioctl32_hash_table[hash])
313 ioctl32_hash_table[hash] = trans;
314 else {
315 t = ioctl32_hash_table[hash];
316 while (t->next)
317 t = t->next;
318 trans->next = NULL;
319 t->next = trans;
320 }
321}
322
323static int __init init_sys32_ioctl(void)
324{
325 int i;
326
327 for (i = 0; i < ioctl_table_size; i++) {
328 if (ioctl_start[i].next != 0) {
329 printk("ioctl translation %d bad\n",i);
330 return -1;
331 }
332
333 ioctl32_insert_translation(&ioctl_start[i]);
334 }
335 return 0;
336}
337
338__initcall(init_sys32_ioctl);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static void compat_ioctl_error(struct file *filp, unsigned int fd,
341 unsigned int cmd, unsigned long arg)
342{
343 char buf[10];
344 char *fn = "?";
345 char *path;
346
347 /* find the name of the device. */
348 path = (char *)__get_free_page(GFP_KERNEL);
349 if (path) {
350 fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE);
351 if (IS_ERR(fn))
352 fn = "?";
353 }
354
355 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
356 if (!isprint(buf[1]))
357 sprintf(buf, "%02x", buf[1]);
358 printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
359 "cmd(%08x){%s} arg(%08x) on %s\n",
360 current->comm, current->pid,
361 (int)fd, (unsigned int)cmd, buf,
362 (unsigned int)arg, fn);
363
364 if (path)
365 free_page((unsigned long)path);
366}
367
368asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
369 unsigned long arg)
370{
371 struct file *filp;
372 int error = -EBADF;
373 struct ioctl_trans *t;
374 int fput_needed;
375
376 filp = fget_light(fd, &fput_needed);
377 if (!filp)
378 goto out;
379
380 /* RED-PEN how should LSM module know it's handling 32bit? */
381 error = security_file_ioctl(filp, cmd, arg);
382 if (error)
383 goto out_fput;
384
385 /*
386 * To allow the compat_ioctl handlers to be self contained
387 * we need to check the common ioctls here first.
388 * Just handle them with the standard handlers below.
389 */
390 switch (cmd) {
391 case FIOCLEX:
392 case FIONCLEX:
393 case FIONBIO:
394 case FIOASYNC:
395 case FIOQSIZE:
396 break;
397
398 case FIBMAP:
399 case FIGETBSZ:
400 case FIONREAD:
401 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
402 break;
403 /*FALL THROUGH*/
404
405 default:
406 if (filp->f_op && filp->f_op->compat_ioctl) {
407 error = filp->f_op->compat_ioctl(filp, cmd, arg);
408 if (error != -ENOIOCTLCMD)
409 goto out_fput;
410 }
411
412 if (!filp->f_op ||
413 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
414 goto do_ioctl;
415 break;
416 }
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
419 if (t->cmd == cmd)
420 goto found_handler;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) &&
424 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
425 error = siocdevprivate_ioctl(fd, cmd, arg);
426 } else {
427 static int count;
428
429 if (++count <= 50)
430 compat_ioctl_error(filp, fd, cmd, arg);
431 error = -EINVAL;
432 }
433
434 goto out_fput;
435
436 found_handler:
437 if (t->handler) {
438 lock_kernel();
439 error = t->handler(fd, cmd, arg, filp);
440 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 goto out_fput;
442 }
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 do_ioctl:
445 error = vfs_ioctl(filp, fd, cmd, arg);
446 out_fput:
447 fput_light(filp, fput_needed);
448 out:
449 return error;
450}
451
452static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
453{
454 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
455 __get_user(kfl->l_type, &ufl->l_type) ||
456 __get_user(kfl->l_whence, &ufl->l_whence) ||
457 __get_user(kfl->l_start, &ufl->l_start) ||
458 __get_user(kfl->l_len, &ufl->l_len) ||
459 __get_user(kfl->l_pid, &ufl->l_pid))
460 return -EFAULT;
461 return 0;
462}
463
464static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
465{
466 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
467 __put_user(kfl->l_type, &ufl->l_type) ||
468 __put_user(kfl->l_whence, &ufl->l_whence) ||
469 __put_user(kfl->l_start, &ufl->l_start) ||
470 __put_user(kfl->l_len, &ufl->l_len) ||
471 __put_user(kfl->l_pid, &ufl->l_pid))
472 return -EFAULT;
473 return 0;
474}
475
476#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
477static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
478{
479 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
480 __get_user(kfl->l_type, &ufl->l_type) ||
481 __get_user(kfl->l_whence, &ufl->l_whence) ||
482 __get_user(kfl->l_start, &ufl->l_start) ||
483 __get_user(kfl->l_len, &ufl->l_len) ||
484 __get_user(kfl->l_pid, &ufl->l_pid))
485 return -EFAULT;
486 return 0;
487}
488#endif
489
490#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
491static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
492{
493 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
494 __put_user(kfl->l_type, &ufl->l_type) ||
495 __put_user(kfl->l_whence, &ufl->l_whence) ||
496 __put_user(kfl->l_start, &ufl->l_start) ||
497 __put_user(kfl->l_len, &ufl->l_len) ||
498 __put_user(kfl->l_pid, &ufl->l_pid))
499 return -EFAULT;
500 return 0;
501}
502#endif
503
504asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
505 unsigned long arg)
506{
507 mm_segment_t old_fs;
508 struct flock f;
509 long ret;
510
511 switch (cmd) {
512 case F_GETLK:
513 case F_SETLK:
514 case F_SETLKW:
515 ret = get_compat_flock(&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, (unsigned long)&f);
521 set_fs(old_fs);
522 if (cmd == F_GETLK && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800523 /* GETLK was successfule and we need to return the data...
524 * but it needs to fit in the compat structure.
525 * l_start shouldn't be too big, unless the original
526 * start + end is greater than COMPAT_OFF_T_MAX, in which
527 * case the app was asking for trouble, so we return
528 * -EOVERFLOW in that case.
529 * l_len could be too big, in which case we just truncate it,
530 * and only allow the app to see that part of the conflicting
531 * lock that might make sense to it anyway
532 */
533
534 if (f.l_start > COMPAT_OFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800536 if (f.l_len > COMPAT_OFF_T_MAX)
537 f.l_len = COMPAT_OFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (ret == 0)
539 ret = put_compat_flock(&f, compat_ptr(arg));
540 }
541 break;
542
543 case F_GETLK64:
544 case F_SETLK64:
545 case F_SETLKW64:
546 ret = get_compat_flock64(&f, compat_ptr(arg));
547 if (ret != 0)
548 break;
549 old_fs = get_fs();
550 set_fs(KERNEL_DS);
551 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
552 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
553 (unsigned long)&f);
554 set_fs(old_fs);
555 if (cmd == F_GETLK64 && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800556 /* need to return lock information - see above for commentary */
557 if (f.l_start > COMPAT_LOFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800559 if (f.l_len > COMPAT_LOFF_T_MAX)
560 f.l_len = COMPAT_LOFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (ret == 0)
562 ret = put_compat_flock64(&f, compat_ptr(arg));
563 }
564 break;
565
566 default:
567 ret = sys_fcntl(fd, cmd, arg);
568 break;
569 }
570 return ret;
571}
572
573asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
574 unsigned long arg)
575{
576 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
577 return -EINVAL;
578 return compat_sys_fcntl64(fd, cmd, arg);
579}
580
581asmlinkage long
582compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
583{
584 long ret;
585 aio_context_t ctx64;
586
587 mm_segment_t oldfs = get_fs();
588 if (unlikely(get_user(ctx64, ctx32p)))
589 return -EFAULT;
590
591 set_fs(KERNEL_DS);
592 /* The __user pointer cast is valid because of the set_fs() */
593 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
594 set_fs(oldfs);
595 /* truncating is ok because it's a user address */
596 if (!ret)
597 ret = put_user((u32) ctx64, ctx32p);
598 return ret;
599}
600
601asmlinkage long
602compat_sys_io_getevents(aio_context_t ctx_id,
603 unsigned long min_nr,
604 unsigned long nr,
605 struct io_event __user *events,
606 struct compat_timespec __user *timeout)
607{
608 long ret;
609 struct timespec t;
610 struct timespec __user *ut = NULL;
611
612 ret = -EFAULT;
613 if (unlikely(!access_ok(VERIFY_WRITE, events,
614 nr * sizeof(struct io_event))))
615 goto out;
616 if (timeout) {
617 if (get_compat_timespec(&t, timeout))
618 goto out;
619
620 ut = compat_alloc_user_space(sizeof(*ut));
621 if (copy_to_user(ut, &t, sizeof(t)) )
622 goto out;
623 }
624 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
625out:
626 return ret;
627}
628
629static inline long
630copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
631{
632 compat_uptr_t uptr;
633 int i;
634
635 for (i = 0; i < nr; ++i) {
636 if (get_user(uptr, ptr32 + i))
637 return -EFAULT;
638 if (put_user(compat_ptr(uptr), ptr64 + i))
639 return -EFAULT;
640 }
641 return 0;
642}
643
644#define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
645
646asmlinkage long
647compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
648{
649 struct iocb __user * __user *iocb64;
650 long ret;
651
652 if (unlikely(nr < 0))
653 return -EINVAL;
654
655 if (nr > MAX_AIO_SUBMITS)
656 nr = MAX_AIO_SUBMITS;
657
658 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
659 ret = copy_iocb(nr, iocb, iocb64);
660 if (!ret)
661 ret = sys_io_submit(ctx_id, nr, iocb64);
662 return ret;
663}
664
665struct compat_ncp_mount_data {
666 compat_int_t version;
667 compat_uint_t ncp_fd;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700668 __compat_uid_t mounted_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 compat_pid_t wdog_pid;
670 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
671 compat_uint_t time_out;
672 compat_uint_t retry_count;
673 compat_uint_t flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700674 __compat_uid_t uid;
675 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 compat_mode_t file_mode;
677 compat_mode_t dir_mode;
678};
679
680struct compat_ncp_mount_data_v4 {
681 compat_int_t version;
682 compat_ulong_t flags;
683 compat_ulong_t mounted_uid;
684 compat_long_t wdog_pid;
685 compat_uint_t ncp_fd;
686 compat_uint_t time_out;
687 compat_uint_t retry_count;
688 compat_ulong_t uid;
689 compat_ulong_t gid;
690 compat_ulong_t file_mode;
691 compat_ulong_t dir_mode;
692};
693
694static void *do_ncp_super_data_conv(void *raw_data)
695{
696 int version = *(unsigned int *)raw_data;
697
698 if (version == 3) {
699 struct compat_ncp_mount_data *c_n = raw_data;
700 struct ncp_mount_data *n = raw_data;
701
702 n->dir_mode = c_n->dir_mode;
703 n->file_mode = c_n->file_mode;
704 n->gid = c_n->gid;
705 n->uid = c_n->uid;
706 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
707 n->wdog_pid = c_n->wdog_pid;
708 n->mounted_uid = c_n->mounted_uid;
709 } else if (version == 4) {
710 struct compat_ncp_mount_data_v4 *c_n = raw_data;
711 struct ncp_mount_data_v4 *n = raw_data;
712
713 n->dir_mode = c_n->dir_mode;
714 n->file_mode = c_n->file_mode;
715 n->gid = c_n->gid;
716 n->uid = c_n->uid;
717 n->retry_count = c_n->retry_count;
718 n->time_out = c_n->time_out;
719 n->ncp_fd = c_n->ncp_fd;
720 n->wdog_pid = c_n->wdog_pid;
721 n->mounted_uid = c_n->mounted_uid;
722 n->flags = c_n->flags;
723 } else if (version != 5) {
724 return NULL;
725 }
726
727 return raw_data;
728}
729
730struct compat_smb_mount_data {
731 compat_int_t version;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700732 __compat_uid_t mounted_uid;
733 __compat_uid_t uid;
734 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 compat_mode_t file_mode;
736 compat_mode_t dir_mode;
737};
738
739static void *do_smb_super_data_conv(void *raw_data)
740{
741 struct smb_mount_data *s = raw_data;
742 struct compat_smb_mount_data *c_s = raw_data;
743
744 if (c_s->version != SMB_MOUNT_OLDVERSION)
745 goto out;
746 s->dir_mode = c_s->dir_mode;
747 s->file_mode = c_s->file_mode;
748 s->gid = c_s->gid;
749 s->uid = c_s->uid;
750 s->mounted_uid = c_s->mounted_uid;
751 out:
752 return raw_data;
753}
754
David Howells9a9947b2005-04-18 10:54:51 -0700755struct compat_nfs_string {
756 compat_uint_t len;
David Howells5fc3e622005-04-27 15:39:03 -0700757 compat_uptr_t data;
David Howells9a9947b2005-04-18 10:54:51 -0700758};
759
760static inline void compat_nfs_string(struct nfs_string *dst,
761 struct compat_nfs_string *src)
762{
763 dst->data = compat_ptr(src->data);
764 dst->len = src->len;
765}
766
767struct compat_nfs4_mount_data_v1 {
768 compat_int_t version;
769 compat_int_t flags;
770 compat_int_t rsize;
771 compat_int_t wsize;
772 compat_int_t timeo;
773 compat_int_t retrans;
774 compat_int_t acregmin;
775 compat_int_t acregmax;
776 compat_int_t acdirmin;
777 compat_int_t acdirmax;
778 struct compat_nfs_string client_addr;
779 struct compat_nfs_string mnt_path;
780 struct compat_nfs_string hostname;
781 compat_uint_t host_addrlen;
David Howells5fc3e622005-04-27 15:39:03 -0700782 compat_uptr_t host_addr;
David Howells9a9947b2005-04-18 10:54:51 -0700783 compat_int_t proto;
784 compat_int_t auth_flavourlen;
David Howells5fc3e622005-04-27 15:39:03 -0700785 compat_uptr_t auth_flavours;
David Howells9a9947b2005-04-18 10:54:51 -0700786};
787
788static int do_nfs4_super_data_conv(void *raw_data)
789{
790 int version = *(compat_uint_t *) raw_data;
791
792 if (version == 1) {
793 struct compat_nfs4_mount_data_v1 *raw = raw_data;
794 struct nfs4_mount_data *real = raw_data;
795
796 /* copy the fields backwards */
797 real->auth_flavours = compat_ptr(raw->auth_flavours);
798 real->auth_flavourlen = raw->auth_flavourlen;
799 real->proto = raw->proto;
800 real->host_addr = compat_ptr(raw->host_addr);
801 real->host_addrlen = raw->host_addrlen;
802 compat_nfs_string(&real->hostname, &raw->hostname);
803 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
804 compat_nfs_string(&real->client_addr, &raw->client_addr);
805 real->acdirmax = raw->acdirmax;
806 real->acdirmin = raw->acdirmin;
807 real->acregmax = raw->acregmax;
808 real->acregmin = raw->acregmin;
809 real->retrans = raw->retrans;
810 real->timeo = raw->timeo;
811 real->wsize = raw->wsize;
812 real->rsize = raw->rsize;
813 real->flags = raw->flags;
814 real->version = raw->version;
815 }
816 else {
817 return -EINVAL;
818 }
819
820 return 0;
821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823extern int copy_mount_options (const void __user *, unsigned long *);
824
825#define SMBFS_NAME "smbfs"
826#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700827#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
830 char __user * type, unsigned long flags,
831 void __user * data)
832{
833 unsigned long type_page;
834 unsigned long data_page;
835 unsigned long dev_page;
836 char *dir_page;
837 int retval;
838
839 retval = copy_mount_options (type, &type_page);
840 if (retval < 0)
841 goto out;
842
843 dir_page = getname(dir_name);
844 retval = PTR_ERR(dir_page);
845 if (IS_ERR(dir_page))
846 goto out1;
847
848 retval = copy_mount_options (dev_name, &dev_page);
849 if (retval < 0)
850 goto out2;
851
852 retval = copy_mount_options (data, &data_page);
853 if (retval < 0)
854 goto out3;
855
856 retval = -EINVAL;
857
858 if (type_page) {
859 if (!strcmp((char *)type_page, SMBFS_NAME)) {
860 do_smb_super_data_conv((void *)data_page);
861 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
862 do_ncp_super_data_conv((void *)data_page);
David Howells9a9947b2005-04-18 10:54:51 -0700863 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
864 if (do_nfs4_super_data_conv((void *) data_page))
865 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 }
868
869 lock_kernel();
870 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
871 flags, (void*)data_page);
872 unlock_kernel();
873
David Howells9a9947b2005-04-18 10:54:51 -0700874 out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 free_page(data_page);
876 out3:
877 free_page(dev_page);
878 out2:
879 putname(dir_page);
880 out1:
881 free_page(type_page);
882 out:
883 return retval;
884}
885
886#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
887#define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
888 ~(sizeof(compat_long_t)-1))
889
890struct compat_old_linux_dirent {
891 compat_ulong_t d_ino;
892 compat_ulong_t d_offset;
893 unsigned short d_namlen;
894 char d_name[1];
895};
896
897struct compat_readdir_callback {
898 struct compat_old_linux_dirent __user *dirent;
899 int result;
900};
901
902static int compat_fillonedir(void *__buf, const char *name, int namlen,
903 loff_t offset, ino_t ino, unsigned int d_type)
904{
905 struct compat_readdir_callback *buf = __buf;
906 struct compat_old_linux_dirent __user *dirent;
907
908 if (buf->result)
909 return -EINVAL;
910 buf->result++;
911 dirent = buf->dirent;
912 if (!access_ok(VERIFY_WRITE, dirent,
913 (unsigned long)(dirent->d_name + namlen + 1) -
914 (unsigned long)dirent))
915 goto efault;
916 if ( __put_user(ino, &dirent->d_ino) ||
917 __put_user(offset, &dirent->d_offset) ||
918 __put_user(namlen, &dirent->d_namlen) ||
919 __copy_to_user(dirent->d_name, name, namlen) ||
920 __put_user(0, dirent->d_name + namlen))
921 goto efault;
922 return 0;
923efault:
924 buf->result = -EFAULT;
925 return -EFAULT;
926}
927
928asmlinkage long compat_sys_old_readdir(unsigned int fd,
929 struct compat_old_linux_dirent __user *dirent, unsigned int count)
930{
931 int error;
932 struct file *file;
933 struct compat_readdir_callback buf;
934
935 error = -EBADF;
936 file = fget(fd);
937 if (!file)
938 goto out;
939
940 buf.result = 0;
941 buf.dirent = dirent;
942
943 error = vfs_readdir(file, compat_fillonedir, &buf);
944 if (error >= 0)
945 error = buf.result;
946
947 fput(file);
948out:
949 return error;
950}
951
952struct compat_linux_dirent {
953 compat_ulong_t d_ino;
954 compat_ulong_t d_off;
955 unsigned short d_reclen;
956 char d_name[1];
957};
958
959struct compat_getdents_callback {
960 struct compat_linux_dirent __user *current_dir;
961 struct compat_linux_dirent __user *previous;
962 int count;
963 int error;
964};
965
966static int compat_filldir(void *__buf, const char *name, int namlen,
967 loff_t offset, ino_t ino, unsigned int d_type)
968{
969 struct compat_linux_dirent __user * dirent;
970 struct compat_getdents_callback *buf = __buf;
971 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
972
973 buf->error = -EINVAL; /* only used if we fail.. */
974 if (reclen > buf->count)
975 return -EINVAL;
976 dirent = buf->previous;
977 if (dirent) {
978 if (__put_user(offset, &dirent->d_off))
979 goto efault;
980 }
981 dirent = buf->current_dir;
982 if (__put_user(ino, &dirent->d_ino))
983 goto efault;
984 if (__put_user(reclen, &dirent->d_reclen))
985 goto efault;
986 if (copy_to_user(dirent->d_name, name, namlen))
987 goto efault;
988 if (__put_user(0, dirent->d_name + namlen))
989 goto efault;
990 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
991 goto efault;
992 buf->previous = dirent;
993 dirent = (void __user *)dirent + reclen;
994 buf->current_dir = dirent;
995 buf->count -= reclen;
996 return 0;
997efault:
998 buf->error = -EFAULT;
999 return -EFAULT;
1000}
1001
1002asmlinkage long compat_sys_getdents(unsigned int fd,
1003 struct compat_linux_dirent __user *dirent, unsigned int count)
1004{
1005 struct file * file;
1006 struct compat_linux_dirent __user * lastdirent;
1007 struct compat_getdents_callback buf;
1008 int error;
1009
1010 error = -EFAULT;
1011 if (!access_ok(VERIFY_WRITE, dirent, count))
1012 goto out;
1013
1014 error = -EBADF;
1015 file = fget(fd);
1016 if (!file)
1017 goto out;
1018
1019 buf.current_dir = dirent;
1020 buf.previous = NULL;
1021 buf.count = count;
1022 buf.error = 0;
1023
1024 error = vfs_readdir(file, compat_filldir, &buf);
1025 if (error < 0)
1026 goto out_putf;
1027 error = buf.error;
1028 lastdirent = buf.previous;
1029 if (lastdirent) {
1030 if (put_user(file->f_pos, &lastdirent->d_off))
1031 error = -EFAULT;
1032 else
1033 error = count - buf.count;
1034 }
1035
1036out_putf:
1037 fput(file);
1038out:
1039 return error;
1040}
1041
1042#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1043#define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1044
1045struct compat_getdents_callback64 {
1046 struct linux_dirent64 __user *current_dir;
1047 struct linux_dirent64 __user *previous;
1048 int count;
1049 int error;
1050};
1051
1052static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1053 ino_t ino, unsigned int d_type)
1054{
1055 struct linux_dirent64 __user *dirent;
1056 struct compat_getdents_callback64 *buf = __buf;
1057 int jj = NAME_OFFSET(dirent);
1058 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1059 u64 off;
1060
1061 buf->error = -EINVAL; /* only used if we fail.. */
1062 if (reclen > buf->count)
1063 return -EINVAL;
1064 dirent = buf->previous;
1065
1066 if (dirent) {
1067 if (__put_user_unaligned(offset, &dirent->d_off))
1068 goto efault;
1069 }
1070 dirent = buf->current_dir;
1071 if (__put_user_unaligned(ino, &dirent->d_ino))
1072 goto efault;
1073 off = 0;
1074 if (__put_user_unaligned(off, &dirent->d_off))
1075 goto efault;
1076 if (__put_user(reclen, &dirent->d_reclen))
1077 goto efault;
1078 if (__put_user(d_type, &dirent->d_type))
1079 goto efault;
1080 if (copy_to_user(dirent->d_name, name, namlen))
1081 goto efault;
1082 if (__put_user(0, dirent->d_name + namlen))
1083 goto efault;
1084 buf->previous = dirent;
1085 dirent = (void __user *)dirent + reclen;
1086 buf->current_dir = dirent;
1087 buf->count -= reclen;
1088 return 0;
1089efault:
1090 buf->error = -EFAULT;
1091 return -EFAULT;
1092}
1093
1094asmlinkage long compat_sys_getdents64(unsigned int fd,
1095 struct linux_dirent64 __user * dirent, unsigned int count)
1096{
1097 struct file * file;
1098 struct linux_dirent64 __user * lastdirent;
1099 struct compat_getdents_callback64 buf;
1100 int error;
1101
1102 error = -EFAULT;
1103 if (!access_ok(VERIFY_WRITE, dirent, count))
1104 goto out;
1105
1106 error = -EBADF;
1107 file = fget(fd);
1108 if (!file)
1109 goto out;
1110
1111 buf.current_dir = dirent;
1112 buf.previous = NULL;
1113 buf.count = count;
1114 buf.error = 0;
1115
1116 error = vfs_readdir(file, compat_filldir64, &buf);
1117 if (error < 0)
1118 goto out_putf;
1119 error = buf.error;
1120 lastdirent = buf.previous;
1121 if (lastdirent) {
1122 typeof(lastdirent->d_off) d_off = file->f_pos;
1123 __put_user_unaligned(d_off, &lastdirent->d_off);
1124 error = count - buf.count;
1125 }
1126
1127out_putf:
1128 fput(file);
1129out:
1130 return error;
1131}
1132#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1133
1134static ssize_t compat_do_readv_writev(int type, struct file *file,
1135 const struct compat_iovec __user *uvector,
1136 unsigned long nr_segs, loff_t *pos)
1137{
1138 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
1139 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
1140
1141 compat_ssize_t tot_len;
1142 struct iovec iovstack[UIO_FASTIOV];
1143 struct iovec *iov=iovstack, *vector;
1144 ssize_t ret;
1145 int seg;
1146 io_fn_t fn;
1147 iov_fn_t fnv;
1148
1149 /*
1150 * SuS says "The readv() function *may* fail if the iovcnt argument
1151 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1152 * traditionally returned zero for zero segments, so...
1153 */
1154 ret = 0;
1155 if (nr_segs == 0)
1156 goto out;
1157
1158 /*
1159 * First get the "struct iovec" from user memory and
1160 * verify all the pointers
1161 */
1162 ret = -EINVAL;
1163 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1164 goto out;
1165 if (!file->f_op)
1166 goto out;
1167 if (nr_segs > UIO_FASTIOV) {
1168 ret = -ENOMEM;
1169 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1170 if (!iov)
1171 goto out;
1172 }
1173 ret = -EFAULT;
1174 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1175 goto out;
1176
1177 /*
1178 * Single unix specification:
1179 * We should -EINVAL if an element length is not >= 0 and fitting an
1180 * ssize_t. The total length is fitting an ssize_t
1181 *
1182 * Be careful here because iov_len is a size_t not an ssize_t
1183 */
1184 tot_len = 0;
1185 vector = iov;
1186 ret = -EINVAL;
1187 for (seg = 0 ; seg < nr_segs; seg++) {
1188 compat_ssize_t tmp = tot_len;
1189 compat_ssize_t len;
1190 compat_uptr_t buf;
1191
1192 if (__get_user(len, &uvector->iov_len) ||
1193 __get_user(buf, &uvector->iov_base)) {
1194 ret = -EFAULT;
1195 goto out;
1196 }
1197 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1198 goto out;
1199 tot_len += len;
1200 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1201 goto out;
1202 vector->iov_base = compat_ptr(buf);
1203 vector->iov_len = (compat_size_t) len;
1204 uvector++;
1205 vector++;
1206 }
1207 if (tot_len == 0) {
1208 ret = 0;
1209 goto out;
1210 }
1211
1212 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001213 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto out;
1215
1216 fnv = NULL;
1217 if (type == READ) {
1218 fn = file->f_op->read;
1219 fnv = file->f_op->readv;
1220 } else {
1221 fn = (io_fn_t)file->f_op->write;
1222 fnv = file->f_op->writev;
1223 }
1224 if (fnv) {
1225 ret = fnv(file, iov, nr_segs, pos);
1226 goto out;
1227 }
1228
1229 /* Do it by hand, with file-ops */
1230 ret = 0;
1231 vector = iov;
1232 while (nr_segs > 0) {
1233 void __user * base;
1234 size_t len;
1235 ssize_t nr;
1236
1237 base = vector->iov_base;
1238 len = vector->iov_len;
1239 vector++;
1240 nr_segs--;
1241
1242 nr = fn(file, base, len, pos);
1243
1244 if (nr < 0) {
1245 if (!ret) ret = nr;
1246 break;
1247 }
1248 ret += nr;
1249 if (nr != len)
1250 break;
1251 }
1252out:
1253 if (iov != iovstack)
1254 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001255 if ((ret + (type == READ)) > 0) {
1256 struct dentry *dentry = file->f_dentry;
1257 if (type == READ)
1258 fsnotify_access(dentry);
1259 else
1260 fsnotify_modify(dentry);
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return ret;
1263}
1264
1265asmlinkage ssize_t
1266compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1267{
1268 struct file *file;
1269 ssize_t ret = -EBADF;
1270
1271 file = fget(fd);
1272 if (!file)
1273 return -EBADF;
1274
1275 if (!(file->f_mode & FMODE_READ))
1276 goto out;
1277
1278 ret = -EINVAL;
1279 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
1280 goto out;
1281
1282 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1283
1284out:
1285 fput(file);
1286 return ret;
1287}
1288
1289asmlinkage ssize_t
1290compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1291{
1292 struct file *file;
1293 ssize_t ret = -EBADF;
1294
1295 file = fget(fd);
1296 if (!file)
1297 return -EBADF;
1298 if (!(file->f_mode & FMODE_WRITE))
1299 goto out;
1300
1301 ret = -EINVAL;
1302 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
1303 goto out;
1304
1305 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1306
1307out:
1308 fput(file);
1309 return ret;
1310}
1311
1312/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001313 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1314 * O_LARGEFILE flag.
1315 */
1316asmlinkage long
1317compat_sys_open(const char __user *filename, int flags, int mode)
1318{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001319 return do_sys_open(AT_FDCWD, filename, flags, mode);
1320}
1321
1322/*
1323 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1324 * O_LARGEFILE flag.
1325 */
1326asmlinkage long
1327compat_sys_openat(int dfd, const char __user *filename, int flags, int mode)
1328{
1329 return do_sys_open(dfd, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001330}
1331
1332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * compat_count() counts the number of arguments/envelopes. It is basically
1334 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1335 * and envp pointers.
1336 */
1337static int compat_count(compat_uptr_t __user *argv, int max)
1338{
1339 int i = 0;
1340
1341 if (argv != NULL) {
1342 for (;;) {
1343 compat_uptr_t p;
1344
1345 if (get_user(p, argv))
1346 return -EFAULT;
1347 if (!p)
1348 break;
1349 argv++;
1350 if(++i > max)
1351 return -E2BIG;
1352 }
1353 }
1354 return i;
1355}
1356
1357/*
1358 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1359 * except that it works with 32 bit argv and envp pointers.
1360 */
1361static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1362 struct linux_binprm *bprm)
1363{
1364 struct page *kmapped_page = NULL;
1365 char *kaddr = NULL;
1366 int ret;
1367
1368 while (argc-- > 0) {
1369 compat_uptr_t str;
1370 int len;
1371 unsigned long pos;
1372
1373 if (get_user(str, argv+argc) ||
1374 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1375 ret = -EFAULT;
1376 goto out;
1377 }
1378
1379 if (bprm->p < len) {
1380 ret = -E2BIG;
1381 goto out;
1382 }
1383
1384 bprm->p -= len;
1385 /* XXX: add architecture specific overflow check here. */
1386 pos = bprm->p;
1387
1388 while (len > 0) {
1389 int i, new, err;
1390 int offset, bytes_to_copy;
1391 struct page *page;
1392
1393 offset = pos % PAGE_SIZE;
1394 i = pos/PAGE_SIZE;
1395 page = bprm->page[i];
1396 new = 0;
1397 if (!page) {
1398 page = alloc_page(GFP_HIGHUSER);
1399 bprm->page[i] = page;
1400 if (!page) {
1401 ret = -ENOMEM;
1402 goto out;
1403 }
1404 new = 1;
1405 }
1406
1407 if (page != kmapped_page) {
1408 if (kmapped_page)
1409 kunmap(kmapped_page);
1410 kmapped_page = page;
1411 kaddr = kmap(kmapped_page);
1412 }
1413 if (new && offset)
1414 memset(kaddr, 0, offset);
1415 bytes_to_copy = PAGE_SIZE - offset;
1416 if (bytes_to_copy > len) {
1417 bytes_to_copy = len;
1418 if (new)
1419 memset(kaddr+offset+len, 0,
1420 PAGE_SIZE-offset-len);
1421 }
1422 err = copy_from_user(kaddr+offset, compat_ptr(str),
1423 bytes_to_copy);
1424 if (err) {
1425 ret = -EFAULT;
1426 goto out;
1427 }
1428
1429 pos += bytes_to_copy;
1430 str += bytes_to_copy;
1431 len -= bytes_to_copy;
1432 }
1433 }
1434 ret = 0;
1435out:
1436 if (kmapped_page)
1437 kunmap(kmapped_page);
1438 return ret;
1439}
1440
1441#ifdef CONFIG_MMU
1442
1443#define free_arg_pages(bprm) do { } while (0)
1444
1445#else
1446
1447static inline void free_arg_pages(struct linux_binprm *bprm)
1448{
1449 int i;
1450
1451 for (i = 0; i < MAX_ARG_PAGES; i++) {
1452 if (bprm->page[i])
1453 __free_page(bprm->page[i]);
1454 bprm->page[i] = NULL;
1455 }
1456}
1457
1458#endif /* CONFIG_MMU */
1459
1460/*
1461 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1462 * that it processes 32 bit argv and envp pointers.
1463 */
1464int compat_do_execve(char * filename,
1465 compat_uptr_t __user *argv,
1466 compat_uptr_t __user *envp,
1467 struct pt_regs * regs)
1468{
1469 struct linux_binprm *bprm;
1470 struct file *file;
1471 int retval;
1472 int i;
1473
1474 retval = -ENOMEM;
1475 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1476 if (!bprm)
1477 goto out_ret;
1478 memset(bprm, 0, sizeof(*bprm));
1479
1480 file = open_exec(filename);
1481 retval = PTR_ERR(file);
1482 if (IS_ERR(file))
1483 goto out_kfree;
1484
1485 sched_exec();
1486
1487 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1488 bprm->file = file;
1489 bprm->filename = filename;
1490 bprm->interp = filename;
1491 bprm->mm = mm_alloc();
1492 retval = -ENOMEM;
1493 if (!bprm->mm)
1494 goto out_file;
1495
1496 retval = init_new_context(current, bprm->mm);
1497 if (retval < 0)
1498 goto out_mm;
1499
1500 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1501 if ((retval = bprm->argc) < 0)
1502 goto out_mm;
1503
1504 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1505 if ((retval = bprm->envc) < 0)
1506 goto out_mm;
1507
1508 retval = security_bprm_alloc(bprm);
1509 if (retval)
1510 goto out;
1511
1512 retval = prepare_binprm(bprm);
1513 if (retval < 0)
1514 goto out;
1515
1516 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1517 if (retval < 0)
1518 goto out;
1519
1520 bprm->exec = bprm->p;
1521 retval = compat_copy_strings(bprm->envc, envp, bprm);
1522 if (retval < 0)
1523 goto out;
1524
1525 retval = compat_copy_strings(bprm->argc, argv, bprm);
1526 if (retval < 0)
1527 goto out;
1528
1529 retval = search_binary_handler(bprm, regs);
1530 if (retval >= 0) {
1531 free_arg_pages(bprm);
1532
1533 /* execve success */
1534 security_bprm_free(bprm);
David S. Miller4a805e82005-09-14 21:40:00 -07001535 acct_update_integrals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 kfree(bprm);
1537 return retval;
1538 }
1539
1540out:
1541 /* Something went wrong, return the inode and free the argument pages*/
1542 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1543 struct page * page = bprm->page[i];
1544 if (page)
1545 __free_page(page);
1546 }
1547
1548 if (bprm->security)
1549 security_bprm_free(bprm);
1550
1551out_mm:
1552 if (bprm->mm)
1553 mmdrop(bprm->mm);
1554
1555out_file:
1556 if (bprm->file) {
1557 allow_write_access(bprm->file);
1558 fput(bprm->file);
1559 }
1560
1561out_kfree:
1562 kfree(bprm);
1563
1564out_ret:
1565 return retval;
1566}
1567
1568#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1569
1570#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1571
1572/*
1573 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1574 * 64-bit unsigned longs.
1575 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001576static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1578 unsigned long *fdset)
1579{
1580 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1581 if (ufdset) {
1582 unsigned long odd;
1583
1584 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1585 return -EFAULT;
1586
1587 odd = nr & 1UL;
1588 nr &= ~1UL;
1589 while (nr) {
1590 unsigned long h, l;
1591 __get_user(l, ufdset);
1592 __get_user(h, ufdset+1);
1593 ufdset += 2;
1594 *fdset++ = h << 32 | l;
1595 nr -= 2;
1596 }
1597 if (odd)
1598 __get_user(*fdset, ufdset);
1599 } else {
1600 /* Tricky, must clear full unsigned long in the
1601 * kernel fdset at the end, this makes sure that
1602 * actually happens.
1603 */
1604 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1605 }
1606 return 0;
1607}
1608
Arjan van de Ven858119e2006-01-14 13:20:43 -08001609static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1611 unsigned long *fdset)
1612{
1613 unsigned long odd;
1614 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1615
1616 if (!ufdset)
1617 return;
1618
1619 odd = nr & 1UL;
1620 nr &= ~1UL;
1621 while (nr) {
1622 unsigned long h, l;
1623 l = *fdset++;
1624 h = l >> 32;
1625 __put_user(l, ufdset);
1626 __put_user(h, ufdset+1);
1627 ufdset += 2;
1628 nr -= 2;
1629 }
1630 if (odd)
1631 __put_user(*fdset, ufdset);
1632}
1633
1634
1635/*
1636 * This is a virtual copy of sys_select from fs/select.c and probably
1637 * should be compared to it from time to time
1638 */
1639static void *select_bits_alloc(int size)
1640{
1641 return kmalloc(6 * size, GFP_KERNEL);
1642}
1643
1644static void select_bits_free(void *bits, int size)
1645{
1646 kfree(bits);
1647}
1648
1649/*
1650 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1651 * like to be certain this leads to no problems. So I return
1652 * EINTR just for safety.
1653 *
1654 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1655 * I'm trying ERESTARTNOHAND which restart only when you want to.
1656 */
1657#define MAX_SELECT_SECONDS \
1658 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1659
1660asmlinkage long
1661compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp,
1662 compat_ulong_t __user *exp, struct compat_timeval __user *tvp)
1663{
1664 fd_set_bits fds;
1665 char *bits;
1666 long timeout;
1667 int size, max_fdset, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001668 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 timeout = MAX_SCHEDULE_TIMEOUT;
1671 if (tvp) {
1672 time_t sec, usec;
1673
1674 if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
1675 || __get_user(sec, &tvp->tv_sec)
1676 || __get_user(usec, &tvp->tv_usec)) {
1677 ret = -EFAULT;
1678 goto out_nofds;
1679 }
1680
1681 if (sec < 0 || usec < 0)
1682 goto out_nofds;
1683
1684 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
1685 timeout = ROUND_UP(usec, 1000000/HZ);
1686 timeout += sec * (unsigned long) HZ;
1687 }
1688 }
1689
1690 if (n < 0)
1691 goto out_nofds;
1692
1693 /* max_fdset can increase, so grab it once to avoid race */
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001694 rcu_read_lock();
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001695 fdt = files_fdtable(current->files);
1696 max_fdset = fdt->max_fdset;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001697 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (n > max_fdset)
1699 n = max_fdset;
1700
1701 /*
1702 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1703 * since we used fdset we need to allocate memory in units of
1704 * long-words.
1705 */
1706 ret = -ENOMEM;
1707 size = FDS_BYTES(n);
1708 bits = select_bits_alloc(size);
1709 if (!bits)
1710 goto out_nofds;
1711 fds.in = (unsigned long *) bits;
1712 fds.out = (unsigned long *) (bits + size);
1713 fds.ex = (unsigned long *) (bits + 2*size);
1714 fds.res_in = (unsigned long *) (bits + 3*size);
1715 fds.res_out = (unsigned long *) (bits + 4*size);
1716 fds.res_ex = (unsigned long *) (bits + 5*size);
1717
1718 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1719 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1720 (ret = compat_get_fd_set(n, exp, fds.ex)))
1721 goto out;
1722 zero_fd_set(n, fds.res_in);
1723 zero_fd_set(n, fds.res_out);
1724 zero_fd_set(n, fds.res_ex);
1725
1726 ret = do_select(n, &fds, &timeout);
1727
1728 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
1729 time_t sec = 0, usec = 0;
1730 if (timeout) {
1731 sec = timeout / HZ;
1732 usec = timeout % HZ;
1733 usec *= (1000000/HZ);
1734 }
1735 if (put_user(sec, &tvp->tv_sec) ||
1736 put_user(usec, &tvp->tv_usec))
1737 ret = -EFAULT;
1738 }
1739
1740 if (ret < 0)
1741 goto out;
1742 if (!ret) {
1743 ret = -ERESTARTNOHAND;
1744 if (signal_pending(current))
1745 goto out;
1746 ret = 0;
1747 }
1748
1749 compat_set_fd_set(n, inp, fds.res_in);
1750 compat_set_fd_set(n, outp, fds.res_out);
1751 compat_set_fd_set(n, exp, fds.res_ex);
1752
1753out:
1754 select_bits_free(bits, size);
1755out_nofds:
1756 return ret;
1757}
1758
1759#if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1760/* Stuff for NFS server syscalls... */
1761struct compat_nfsctl_svc {
1762 u16 svc32_port;
1763 s32 svc32_nthreads;
1764};
1765
1766struct compat_nfsctl_client {
1767 s8 cl32_ident[NFSCLNT_IDMAX+1];
1768 s32 cl32_naddr;
1769 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1770 s32 cl32_fhkeytype;
1771 s32 cl32_fhkeylen;
1772 u8 cl32_fhkey[NFSCLNT_KEYMAX];
1773};
1774
1775struct compat_nfsctl_export {
1776 char ex32_client[NFSCLNT_IDMAX+1];
1777 char ex32_path[NFS_MAXPATHLEN+1];
1778 compat_dev_t ex32_dev;
1779 compat_ino_t ex32_ino;
1780 compat_int_t ex32_flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -07001781 __compat_uid_t ex32_anon_uid;
1782 __compat_gid_t ex32_anon_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783};
1784
1785struct compat_nfsctl_fdparm {
1786 struct sockaddr gd32_addr;
1787 s8 gd32_path[NFS_MAXPATHLEN+1];
1788 compat_int_t gd32_version;
1789};
1790
1791struct compat_nfsctl_fsparm {
1792 struct sockaddr gd32_addr;
1793 s8 gd32_path[NFS_MAXPATHLEN+1];
1794 compat_int_t gd32_maxlen;
1795};
1796
1797struct compat_nfsctl_arg {
1798 compat_int_t ca32_version; /* safeguard */
1799 union {
1800 struct compat_nfsctl_svc u32_svc;
1801 struct compat_nfsctl_client u32_client;
1802 struct compat_nfsctl_export u32_export;
1803 struct compat_nfsctl_fdparm u32_getfd;
1804 struct compat_nfsctl_fsparm u32_getfs;
1805 } u;
1806#define ca32_svc u.u32_svc
1807#define ca32_client u.u32_client
1808#define ca32_export u.u32_export
1809#define ca32_getfd u.u32_getfd
1810#define ca32_getfs u.u32_getfs
1811};
1812
1813union compat_nfsctl_res {
1814 __u8 cr32_getfh[NFS_FHSIZE];
1815 struct knfsd_fh cr32_getfs;
1816};
1817
1818static int compat_nfs_svc_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1819{
1820 int err;
1821
1822 err = access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc));
1823 err |= get_user(karg->ca_version, &arg->ca32_version);
1824 err |= __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port);
1825 err |= __get_user(karg->ca_svc.svc_nthreads, &arg->ca32_svc.svc32_nthreads);
1826 return (err) ? -EFAULT : 0;
1827}
1828
1829static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1830{
1831 int err;
1832
1833 err = access_ok(VERIFY_READ, &arg->ca32_client, sizeof(arg->ca32_client));
1834 err |= get_user(karg->ca_version, &arg->ca32_version);
1835 err |= __copy_from_user(&karg->ca_client.cl_ident[0],
1836 &arg->ca32_client.cl32_ident[0],
1837 NFSCLNT_IDMAX);
1838 err |= __get_user(karg->ca_client.cl_naddr, &arg->ca32_client.cl32_naddr);
1839 err |= __copy_from_user(&karg->ca_client.cl_addrlist[0],
1840 &arg->ca32_client.cl32_addrlist[0],
1841 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX));
1842 err |= __get_user(karg->ca_client.cl_fhkeytype,
1843 &arg->ca32_client.cl32_fhkeytype);
1844 err |= __get_user(karg->ca_client.cl_fhkeylen,
1845 &arg->ca32_client.cl32_fhkeylen);
1846 err |= __copy_from_user(&karg->ca_client.cl_fhkey[0],
1847 &arg->ca32_client.cl32_fhkey[0],
1848 NFSCLNT_KEYMAX);
1849
1850 return (err) ? -EFAULT : 0;
1851}
1852
1853static int compat_nfs_exp_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1854{
1855 int err;
1856
1857 err = access_ok(VERIFY_READ, &arg->ca32_export, sizeof(arg->ca32_export));
1858 err |= get_user(karg->ca_version, &arg->ca32_version);
1859 err |= __copy_from_user(&karg->ca_export.ex_client[0],
1860 &arg->ca32_export.ex32_client[0],
1861 NFSCLNT_IDMAX);
1862 err |= __copy_from_user(&karg->ca_export.ex_path[0],
1863 &arg->ca32_export.ex32_path[0],
1864 NFS_MAXPATHLEN);
1865 err |= __get_user(karg->ca_export.ex_dev,
1866 &arg->ca32_export.ex32_dev);
1867 err |= __get_user(karg->ca_export.ex_ino,
1868 &arg->ca32_export.ex32_ino);
1869 err |= __get_user(karg->ca_export.ex_flags,
1870 &arg->ca32_export.ex32_flags);
1871 err |= __get_user(karg->ca_export.ex_anon_uid,
1872 &arg->ca32_export.ex32_anon_uid);
1873 err |= __get_user(karg->ca_export.ex_anon_gid,
1874 &arg->ca32_export.ex32_anon_gid);
1875 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
1876 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
1877
1878 return (err) ? -EFAULT : 0;
1879}
1880
1881static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1882{
1883 int err;
1884
1885 err = access_ok(VERIFY_READ, &arg->ca32_getfd, sizeof(arg->ca32_getfd));
1886 err |= get_user(karg->ca_version, &arg->ca32_version);
1887 err |= __copy_from_user(&karg->ca_getfd.gd_addr,
1888 &arg->ca32_getfd.gd32_addr,
1889 (sizeof(struct sockaddr)));
1890 err |= __copy_from_user(&karg->ca_getfd.gd_path,
1891 &arg->ca32_getfd.gd32_path,
1892 (NFS_MAXPATHLEN+1));
1893 err |= __get_user(karg->ca_getfd.gd_version,
1894 &arg->ca32_getfd.gd32_version);
1895
1896 return (err) ? -EFAULT : 0;
1897}
1898
1899static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1900{
1901 int err;
1902
1903 err = access_ok(VERIFY_READ, &arg->ca32_getfs, sizeof(arg->ca32_getfs));
1904 err |= get_user(karg->ca_version, &arg->ca32_version);
1905 err |= __copy_from_user(&karg->ca_getfs.gd_addr,
1906 &arg->ca32_getfs.gd32_addr,
1907 (sizeof(struct sockaddr)));
1908 err |= __copy_from_user(&karg->ca_getfs.gd_path,
1909 &arg->ca32_getfs.gd32_path,
1910 (NFS_MAXPATHLEN+1));
1911 err |= __get_user(karg->ca_getfs.gd_maxlen,
1912 &arg->ca32_getfs.gd32_maxlen);
1913
1914 return (err) ? -EFAULT : 0;
1915}
1916
1917/* This really doesn't need translations, we are only passing
1918 * back a union which contains opaque nfs file handle data.
1919 */
1920static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, union compat_nfsctl_res __user *res)
1921{
1922 int err;
1923
1924 err = copy_to_user(res, kres, sizeof(*res));
1925
1926 return (err) ? -EFAULT : 0;
1927}
1928
1929asmlinkage long compat_sys_nfsservctl(int cmd, struct compat_nfsctl_arg __user *arg,
1930 union compat_nfsctl_res __user *res)
1931{
1932 struct nfsctl_arg *karg;
1933 union nfsctl_res *kres;
1934 mm_segment_t oldfs;
1935 int err;
1936
1937 karg = kmalloc(sizeof(*karg), GFP_USER);
1938 kres = kmalloc(sizeof(*kres), GFP_USER);
1939 if(!karg || !kres) {
1940 err = -ENOMEM;
1941 goto done;
1942 }
1943
1944 switch(cmd) {
1945 case NFSCTL_SVC:
1946 err = compat_nfs_svc_trans(karg, arg);
1947 break;
1948
1949 case NFSCTL_ADDCLIENT:
1950 err = compat_nfs_clnt_trans(karg, arg);
1951 break;
1952
1953 case NFSCTL_DELCLIENT:
1954 err = compat_nfs_clnt_trans(karg, arg);
1955 break;
1956
1957 case NFSCTL_EXPORT:
1958 case NFSCTL_UNEXPORT:
1959 err = compat_nfs_exp_trans(karg, arg);
1960 break;
1961
1962 case NFSCTL_GETFD:
1963 err = compat_nfs_getfd_trans(karg, arg);
1964 break;
1965
1966 case NFSCTL_GETFS:
1967 err = compat_nfs_getfs_trans(karg, arg);
1968 break;
1969
1970 default:
1971 err = -EINVAL;
1972 goto done;
1973 }
1974
1975 oldfs = get_fs();
1976 set_fs(KERNEL_DS);
1977 /* The __user pointer casts are valid because of the set_fs() */
1978 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
1979 set_fs(oldfs);
1980
1981 if (err)
1982 goto done;
1983
1984 if((cmd == NFSCTL_GETFD) ||
1985 (cmd == NFSCTL_GETFS))
1986 err = compat_nfs_getfh_res_trans(kres, res);
1987
1988done:
1989 kfree(karg);
1990 kfree(kres);
1991 return err;
1992}
1993#else /* !NFSD */
1994long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
1995{
1996 return sys_ni_syscall();
1997}
1998#endif