| 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/nfsd/syscall.h> | 
| Randy Dunlap | 9789cfe | 2008-11-19 11:46:46 -0800 | [diff] [blame] | 11 | #include <linux/cred.h> | 
 | 12 | #include <linux/sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { | 
| Josef 'Jeff' Sipek | 16b6287 | 2007-07-19 01:48:21 -0700 | [diff] [blame] | 25 | 	struct vfsmount *mnt; | 
| Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 26 | 	struct file *file; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  | 
| Josef 'Jeff' Sipek | 16b6287 | 2007-07-19 01:48:21 -0700 | [diff] [blame] | 28 | 	mnt = do_kern_mount("nfsd", 0, "nfsd", NULL); | 
 | 29 | 	if (IS_ERR(mnt)) | 
 | 30 | 		return (struct file *)mnt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 |  | 
| Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 32 | 	file = file_open_root(mnt->mnt_root, mnt, name, flags); | 
 | 33 |  | 
| Josef 'Jeff' Sipek | 16b6287 | 2007-07-19 01:48:21 -0700 | [diff] [blame] | 34 | 	mntput(mnt);	/* drop do_kern_mount reference */ | 
| Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 35 | 	return file; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } | 
 | 37 |  | 
 | 38 | static struct { | 
 | 39 | 	char *name; int wsize; int rsize; | 
 | 40 | } map[] = { | 
 | 41 | 	[NFSCTL_SVC] = { | 
 | 42 | 		.name	= ".svc", | 
 | 43 | 		.wsize	= sizeof(struct nfsctl_svc) | 
 | 44 | 	}, | 
 | 45 | 	[NFSCTL_ADDCLIENT] = { | 
 | 46 | 		.name	= ".add", | 
 | 47 | 		.wsize	= sizeof(struct nfsctl_client) | 
 | 48 | 	}, | 
 | 49 | 	[NFSCTL_DELCLIENT] = { | 
 | 50 | 		.name	= ".del", | 
 | 51 | 		.wsize	= sizeof(struct nfsctl_client) | 
 | 52 | 	}, | 
 | 53 | 	[NFSCTL_EXPORT] = { | 
 | 54 | 		.name	= ".export", | 
 | 55 | 		.wsize	= sizeof(struct nfsctl_export) | 
 | 56 | 	}, | 
 | 57 | 	[NFSCTL_UNEXPORT] = { | 
 | 58 | 		.name	= ".unexport", | 
 | 59 | 		.wsize	= sizeof(struct nfsctl_export) | 
 | 60 | 	}, | 
 | 61 | 	[NFSCTL_GETFD] = { | 
 | 62 | 		.name	= ".getfd", | 
 | 63 | 		.wsize	= sizeof(struct nfsctl_fdparm), | 
 | 64 | 		.rsize	= NFS_FHSIZE | 
 | 65 | 	}, | 
 | 66 | 	[NFSCTL_GETFS] = { | 
 | 67 | 		.name	= ".getfs", | 
 | 68 | 		.wsize	= sizeof(struct nfsctl_fsparm), | 
 | 69 | 		.rsize	= sizeof(struct knfsd_fh) | 
 | 70 | 	}, | 
 | 71 | }; | 
 | 72 |  | 
| Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 73 | SYSCALL_DEFINE3(nfsservctl, int, cmd, struct nfsctl_arg __user *, arg, | 
 | 74 | 		void __user *, res) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { | 
 | 76 | 	struct file *file; | 
 | 77 | 	void __user *p = &arg->u; | 
 | 78 | 	int version; | 
 | 79 | 	int err; | 
 | 80 |  | 
 | 81 | 	if (copy_from_user(&version, &arg->ca_version, sizeof(int))) | 
 | 82 | 		return -EFAULT; | 
 | 83 |  | 
| Peter Staubach | 85c6932 | 2006-03-16 23:04:02 -0800 | [diff] [blame] | 84 | 	if (version != NFSCTL_VERSION) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
| Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 87 | 	if (cmd < 0 || cmd >= ARRAY_SIZE(map) || !map[cmd].name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | 		return -EINVAL; | 
 | 89 |  | 
 | 90 | 	file = do_open(map[cmd].name, map[cmd].rsize ? O_RDWR : O_WRONLY);	 | 
 | 91 | 	if (IS_ERR(file)) | 
 | 92 | 		return PTR_ERR(file); | 
 | 93 | 	err = file->f_op->write(file, p, map[cmd].wsize, &file->f_pos); | 
 | 94 | 	if (err >= 0 && map[cmd].rsize) | 
 | 95 | 		err = file->f_op->read(file, res, map[cmd].rsize, &file->f_pos); | 
 | 96 | 	if (err >= 0) | 
 | 97 | 		err = 0; | 
 | 98 | 	fput(file); | 
 | 99 | 	return err; | 
 | 100 | } |