blob: a5a18d4aca40a89661ac5dc219c5041e83ac75bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfsctl.c
3 *
4 * This should eventually move to userland.
5 *
6 */
7#include <linux/config.h>
Alan Cox715b49e2006-01-18 17:44:07 -08008#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/file.h>
10#include <linux/fs.h>
11#include <linux/sunrpc/svc.h>
12#include <linux/nfsd/nfsd.h>
13#include <linux/nfsd/syscall.h>
14#include <linux/linkage.h>
15#include <linux/namei.h>
16#include <linux/mount.h>
17#include <linux/syscalls.h>
18#include <asm/uaccess.h>
19
20/*
21 * open a file on nfsd fs
22 */
23
24static struct file *do_open(char *name, int flags)
25{
26 struct nameidata nd;
27 int error;
28
29 nd.mnt = do_kern_mount("nfsd", 0, "nfsd", NULL);
30
31 if (IS_ERR(nd.mnt))
32 return (struct file *)nd.mnt;
33
34 nd.dentry = dget(nd.mnt->mnt_root);
35 nd.last_type = LAST_ROOT;
36 nd.flags = 0;
37 nd.depth = 0;
38
39 error = path_walk(name, &nd);
40 if (error)
41 return ERR_PTR(error);
42
43 if (flags == O_RDWR)
44 error = may_open(&nd,MAY_READ|MAY_WRITE,FMODE_READ|FMODE_WRITE);
45 else
46 error = may_open(&nd, MAY_WRITE, FMODE_WRITE);
47
48 if (!error)
49 return dentry_open(nd.dentry, nd.mnt, flags);
50
51 path_release(&nd);
52 return ERR_PTR(error);
53}
54
55static struct {
56 char *name; int wsize; int rsize;
57} map[] = {
58 [NFSCTL_SVC] = {
59 .name = ".svc",
60 .wsize = sizeof(struct nfsctl_svc)
61 },
62 [NFSCTL_ADDCLIENT] = {
63 .name = ".add",
64 .wsize = sizeof(struct nfsctl_client)
65 },
66 [NFSCTL_DELCLIENT] = {
67 .name = ".del",
68 .wsize = sizeof(struct nfsctl_client)
69 },
70 [NFSCTL_EXPORT] = {
71 .name = ".export",
72 .wsize = sizeof(struct nfsctl_export)
73 },
74 [NFSCTL_UNEXPORT] = {
75 .name = ".unexport",
76 .wsize = sizeof(struct nfsctl_export)
77 },
78 [NFSCTL_GETFD] = {
79 .name = ".getfd",
80 .wsize = sizeof(struct nfsctl_fdparm),
81 .rsize = NFS_FHSIZE
82 },
83 [NFSCTL_GETFS] = {
84 .name = ".getfs",
85 .wsize = sizeof(struct nfsctl_fsparm),
86 .rsize = sizeof(struct knfsd_fh)
87 },
88};
89
90long
91asmlinkage sys_nfsservctl(int cmd, struct nfsctl_arg __user *arg, void __user *res)
92{
93 struct file *file;
94 void __user *p = &arg->u;
95 int version;
96 int err;
97
98 if (copy_from_user(&version, &arg->ca_version, sizeof(int)))
99 return -EFAULT;
100
Peter Staubach85c69322006-03-16 23:04:02 -0800101 if (version != NFSCTL_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Tobias Klausere8c96f82006-03-24 03:15:34 -0800104 if (cmd < 0 || cmd >= ARRAY_SIZE(map) || !map[cmd].name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EINVAL;
106
107 file = do_open(map[cmd].name, map[cmd].rsize ? O_RDWR : O_WRONLY);
108 if (IS_ERR(file))
109 return PTR_ERR(file);
110 err = file->f_op->write(file, p, map[cmd].wsize, &file->f_pos);
111 if (err >= 0 && map[cmd].rsize)
112 err = file->f_op->read(file, res, map[cmd].rsize, &file->f_pos);
113 if (err >= 0)
114 err = 0;
115 fput(file);
116 return err;
117}