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