| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *	fs/nfsctl.c | 
 | 3 |  * | 
 | 4 |  *	This should eventually move to userland. | 
 | 5 |  * | 
 | 6 |  */ | 
| Alan Cox | 715b49e | 2006-01-18 17:44:07 -0800 | [diff] [blame] | 7 | #include <linux/types.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 <linux/syscalls.h> | 
 | 17 | #include <asm/uaccess.h> | 
 | 18 |  | 
 | 19 | /* | 
 | 20 |  * open a file on nfsd fs | 
 | 21 |  */ | 
 | 22 |  | 
 | 23 | static struct file *do_open(char *name, int flags) | 
 | 24 | { | 
 | 25 | 	struct nameidata nd; | 
| Josef 'Jeff' Sipek | 16b6287 | 2007-07-19 01:48:21 -0700 | [diff] [blame] | 26 | 	struct vfsmount *mnt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | 	int error; | 
 | 28 |  | 
| Josef 'Jeff' Sipek | 16b6287 | 2007-07-19 01:48:21 -0700 | [diff] [blame] | 29 | 	mnt = do_kern_mount("nfsd", 0, "nfsd", NULL); | 
 | 30 | 	if (IS_ERR(mnt)) | 
 | 31 | 		return (struct file *)mnt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 |  | 
| Josef 'Jeff' Sipek | 16b6287 | 2007-07-19 01:48:21 -0700 | [diff] [blame] | 33 | 	error = vfs_path_lookup(mnt->mnt_root, mnt, name, 0, &nd); | 
 | 34 | 	mntput(mnt);	/* drop do_kern_mount reference */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | 	if (error) | 
 | 36 | 		return ERR_PTR(error); | 
 | 37 |  | 
 | 38 | 	if (flags == O_RDWR) | 
 | 39 | 		error = may_open(&nd,MAY_READ|MAY_WRITE,FMODE_READ|FMODE_WRITE); | 
 | 40 | 	else | 
 | 41 | 		error = may_open(&nd, MAY_WRITE, FMODE_WRITE); | 
 | 42 |  | 
 | 43 | 	if (!error) | 
| Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 44 | 		return dentry_open(nd.path.dentry, nd.path.mnt, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 |  | 
| Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 46 | 	path_put(&nd.path); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | 	return ERR_PTR(error); | 
 | 48 | } | 
 | 49 |  | 
 | 50 | static struct { | 
 | 51 | 	char *name; int wsize; int rsize; | 
 | 52 | } map[] = { | 
 | 53 | 	[NFSCTL_SVC] = { | 
 | 54 | 		.name	= ".svc", | 
 | 55 | 		.wsize	= sizeof(struct nfsctl_svc) | 
 | 56 | 	}, | 
 | 57 | 	[NFSCTL_ADDCLIENT] = { | 
 | 58 | 		.name	= ".add", | 
 | 59 | 		.wsize	= sizeof(struct nfsctl_client) | 
 | 60 | 	}, | 
 | 61 | 	[NFSCTL_DELCLIENT] = { | 
 | 62 | 		.name	= ".del", | 
 | 63 | 		.wsize	= sizeof(struct nfsctl_client) | 
 | 64 | 	}, | 
 | 65 | 	[NFSCTL_EXPORT] = { | 
 | 66 | 		.name	= ".export", | 
 | 67 | 		.wsize	= sizeof(struct nfsctl_export) | 
 | 68 | 	}, | 
 | 69 | 	[NFSCTL_UNEXPORT] = { | 
 | 70 | 		.name	= ".unexport", | 
 | 71 | 		.wsize	= sizeof(struct nfsctl_export) | 
 | 72 | 	}, | 
 | 73 | 	[NFSCTL_GETFD] = { | 
 | 74 | 		.name	= ".getfd", | 
 | 75 | 		.wsize	= sizeof(struct nfsctl_fdparm), | 
 | 76 | 		.rsize	= NFS_FHSIZE | 
 | 77 | 	}, | 
 | 78 | 	[NFSCTL_GETFS] = { | 
 | 79 | 		.name	= ".getfs", | 
 | 80 | 		.wsize	= sizeof(struct nfsctl_fsparm), | 
 | 81 | 		.rsize	= sizeof(struct knfsd_fh) | 
 | 82 | 	}, | 
 | 83 | }; | 
 | 84 |  | 
 | 85 | long | 
 | 86 | asmlinkage sys_nfsservctl(int cmd, struct nfsctl_arg __user *arg, void __user *res) | 
 | 87 | { | 
 | 88 | 	struct file *file; | 
 | 89 | 	void __user *p = &arg->u; | 
 | 90 | 	int version; | 
 | 91 | 	int err; | 
 | 92 |  | 
 | 93 | 	if (copy_from_user(&version, &arg->ca_version, sizeof(int))) | 
 | 94 | 		return -EFAULT; | 
 | 95 |  | 
| Peter Staubach | 85c6932 | 2006-03-16 23:04:02 -0800 | [diff] [blame] | 96 | 	if (version != NFSCTL_VERSION) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 |  | 
| Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 99 | 	if (cmd < 0 || cmd >= ARRAY_SIZE(map) || !map[cmd].name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | 		return -EINVAL; | 
 | 101 |  | 
 | 102 | 	file = do_open(map[cmd].name, map[cmd].rsize ? O_RDWR : O_WRONLY);	 | 
 | 103 | 	if (IS_ERR(file)) | 
 | 104 | 		return PTR_ERR(file); | 
 | 105 | 	err = file->f_op->write(file, p, map[cmd].wsize, &file->f_pos); | 
 | 106 | 	if (err >= 0 && map[cmd].rsize) | 
 | 107 | 		err = file->f_op->read(file, res, map[cmd].rsize, &file->f_pos); | 
 | 108 | 	if (err >= 0) | 
 | 109 | 		err = 0; | 
 | 110 | 	fput(file); | 
 | 111 | 	return err; | 
 | 112 | } |