blob: d3854d94b7cf367cad8a04b665f0c18271355368 [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 */
Alan Cox715b49e2006-01-18 17:44:07 -08007#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/file.h>
9#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/nfsd/syscall.h>
Randy Dunlap9789cfe2008-11-19 11:46:46 -080011#include <linux/cred.h>
12#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/linkage.h>
14#include <linux/namei.h>
15#include <linux/mount.h>
16#include <linux/syscalls.h>
17#include <asm/uaccess.h>
18
19/*
20 * open a file on nfsd fs
21 */
22
23static struct file *do_open(char *name, int flags)
24{
25 struct nameidata nd;
Josef 'Jeff' Sipek16b62872007-07-19 01:48:21 -070026 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 int error;
28
Josef 'Jeff' Sipek16b62872007-07-19 01:48:21 -070029 mnt = do_kern_mount("nfsd", 0, "nfsd", NULL);
30 if (IS_ERR(mnt))
31 return (struct file *)mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Josef 'Jeff' Sipek16b62872007-07-19 01:48:21 -070033 error = vfs_path_lookup(mnt->mnt_root, mnt, name, 0, &nd);
34 mntput(mnt); /* drop do_kern_mount reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (error)
36 return ERR_PTR(error);
37
38 if (flags == O_RDWR)
Christoph Hellwig3fb64192008-10-24 09:58:10 +020039 error = may_open(&nd.path, MAY_READ|MAY_WRITE,
40 FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 else
Christoph Hellwig3fb64192008-10-24 09:58:10 +020042 error = may_open(&nd.path, MAY_WRITE, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 if (!error)
David Howells745ca242008-11-14 10:39:22 +110045 return dentry_open(nd.path.dentry, nd.path.mnt, flags,
46 current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jan Blunck1d957f92008-02-14 19:34:35 -080048 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return ERR_PTR(error);
50}
51
52static 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
Heiko Carstens1e7bfb22009-01-14 14:14:29 +010087SYSCALL_DEFINE3(nfsservctl, int, cmd, struct nfsctl_arg __user *, arg,
88 void __user *, res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
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
Peter Staubach85c69322006-03-16 23:04:02 -080098 if (version != NFSCTL_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Tobias Klausere8c96f82006-03-24 03:15:34 -0800101 if (cmd < 0 || cmd >= ARRAY_SIZE(map) || !map[cmd].name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return -EINVAL;
103
104 file = do_open(map[cmd].name, map[cmd].rsize ? O_RDWR : O_WRONLY);
105 if (IS_ERR(file))
106 return PTR_ERR(file);
107 err = file->f_op->write(file, p, map[cmd].wsize, &file->f_pos);
108 if (err >= 0 && map[cmd].rsize)
109 err = file->f_op->read(file, res, map[cmd].rsize, &file->f_pos);
110 if (err >= 0)
111 err = 0;
112 fput(file);
113 return err;
114}