xref: /openbmc/linux/fs/9p/v9fs.c (revision ee443996)
19e82cf6aSEric Van Hensbergen /*
29e82cf6aSEric Van Hensbergen  *  linux/fs/9p/v9fs.c
39e82cf6aSEric Van Hensbergen  *
49e82cf6aSEric Van Hensbergen  *  This file contains functions assisting in mapping VFS to 9P2000
59e82cf6aSEric Van Hensbergen  *
68a0dc95fSEric Van Hensbergen  *  Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
79e82cf6aSEric Van Hensbergen  *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
89e82cf6aSEric Van Hensbergen  *
99e82cf6aSEric Van Hensbergen  *  This program is free software; you can redistribute it and/or modify
1042e8c509SEric Van Hensbergen  *  it under the terms of the GNU General Public License version 2
1142e8c509SEric Van Hensbergen  *  as published by the Free Software Foundation.
129e82cf6aSEric Van Hensbergen  *
139e82cf6aSEric Van Hensbergen  *  This program is distributed in the hope that it will be useful,
149e82cf6aSEric Van Hensbergen  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
159e82cf6aSEric Van Hensbergen  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
169e82cf6aSEric Van Hensbergen  *  GNU General Public License for more details.
179e82cf6aSEric Van Hensbergen  *
189e82cf6aSEric Van Hensbergen  *  You should have received a copy of the GNU General Public License
199e82cf6aSEric Van Hensbergen  *  along with this program; if not, write to:
209e82cf6aSEric Van Hensbergen  *  Free Software Foundation
219e82cf6aSEric Van Hensbergen  *  51 Franklin Street, Fifth Floor
229e82cf6aSEric Van Hensbergen  *  Boston, MA  02111-1301  USA
239e82cf6aSEric Van Hensbergen  *
249e82cf6aSEric Van Hensbergen  */
259e82cf6aSEric Van Hensbergen 
269e82cf6aSEric Van Hensbergen #include <linux/module.h>
279e82cf6aSEric Van Hensbergen #include <linux/errno.h>
289e82cf6aSEric Van Hensbergen #include <linux/fs.h>
29914e2637SAl Viro #include <linux/sched.h>
309e82cf6aSEric Van Hensbergen #include <linux/parser.h>
319e82cf6aSEric Van Hensbergen #include <linux/idr.h>
32bd238fb4SLatchesar Ionkov #include <net/9p/9p.h>
33bd238fb4SLatchesar Ionkov #include <net/9p/transport.h>
34bd238fb4SLatchesar Ionkov #include <net/9p/client.h>
359e82cf6aSEric Van Hensbergen #include "v9fs.h"
369e82cf6aSEric Van Hensbergen #include "v9fs_vfs.h"
379e82cf6aSEric Van Hensbergen 
389e82cf6aSEric Van Hensbergen /*
39a80d923eSEric Van Hensbergen   * Option Parsing (code inspired by NFS code)
40a80d923eSEric Van Hensbergen   *  NOTE: each transport will parse its own options
41a80d923eSEric Van Hensbergen   */
42a80d923eSEric Van Hensbergen 
439e82cf6aSEric Van Hensbergen enum {
449e82cf6aSEric Van Hensbergen 	/* Options that take integer arguments */
458a0dc95fSEric Van Hensbergen 	Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
469e82cf6aSEric Van Hensbergen 	/* String options */
47a80d923eSEric Van Hensbergen 	Opt_uname, Opt_remotename, Opt_trans,
489e82cf6aSEric Van Hensbergen 	/* Options that take no arguments */
498a0dc95fSEric Van Hensbergen 	Opt_nodevmap,
50e03abc0cSEric Van Hensbergen 	/* Cache options */
51e03abc0cSEric Van Hensbergen 	Opt_cache_loose,
52ba17674fSLatchesar Ionkov 	/* Access options */
53ba17674fSLatchesar Ionkov 	Opt_access,
549e82cf6aSEric Van Hensbergen 	/* Error token */
559e82cf6aSEric Van Hensbergen 	Opt_err
569e82cf6aSEric Van Hensbergen };
579e82cf6aSEric Van Hensbergen 
589e82cf6aSEric Van Hensbergen static match_table_t tokens = {
599e2f6688SEric Van Hensbergen 	{Opt_debug, "debug=%x"},
60bd32b82dSLatchesar Ionkov 	{Opt_dfltuid, "dfltuid=%u"},
61bd32b82dSLatchesar Ionkov 	{Opt_dfltgid, "dfltgid=%u"},
629e82cf6aSEric Van Hensbergen 	{Opt_afid, "afid=%u"},
6367543e50SEric Van Hensbergen 	{Opt_uname, "uname=%s"},
649e82cf6aSEric Van Hensbergen 	{Opt_remotename, "aname=%s"},
659e82cf6aSEric Van Hensbergen 	{Opt_nodevmap, "nodevmap"},
66e03abc0cSEric Van Hensbergen 	{Opt_cache_loose, "cache=loose"},
67e03abc0cSEric Van Hensbergen 	{Opt_cache_loose, "loose"},
68ba17674fSLatchesar Ionkov 	{Opt_access, "access=%s"},
699e82cf6aSEric Van Hensbergen 	{Opt_err, NULL}
709e82cf6aSEric Van Hensbergen };
719e82cf6aSEric Van Hensbergen 
729e82cf6aSEric Van Hensbergen /**
739e82cf6aSEric Van Hensbergen  * v9fs_parse_options - parse mount options into session structure
749e82cf6aSEric Van Hensbergen  * @v9ses: existing v9fs session information
759e82cf6aSEric Van Hensbergen  *
769e82cf6aSEric Van Hensbergen  */
779e82cf6aSEric Van Hensbergen 
78a80d923eSEric Van Hensbergen static void v9fs_parse_options(struct v9fs_session_info *v9ses)
799e82cf6aSEric Van Hensbergen {
808999e04fSLatchesar Ionkov 	char *options;
819e82cf6aSEric Van Hensbergen 	substring_t args[MAX_OPT_ARGS];
82a80d923eSEric Van Hensbergen 	char *p;
838a0dc95fSEric Van Hensbergen 	int option = 0;
84ba17674fSLatchesar Ionkov 	char *s, *e;
858a0dc95fSEric Van Hensbergen 	int ret;
869e82cf6aSEric Van Hensbergen 
879e82cf6aSEric Van Hensbergen 	/* setup defaults */
889e82cf6aSEric Van Hensbergen 	v9ses->afid = ~0;
899e82cf6aSEric Van Hensbergen 	v9ses->debug = 0;
90e03abc0cSEric Van Hensbergen 	v9ses->cache = 0;
919e82cf6aSEric Van Hensbergen 
928999e04fSLatchesar Ionkov 	if (!v9ses->options)
939e82cf6aSEric Van Hensbergen 		return;
949e82cf6aSEric Van Hensbergen 
958999e04fSLatchesar Ionkov 	options = kstrdup(v9ses->options, GFP_KERNEL);
969e82cf6aSEric Van Hensbergen 	while ((p = strsep(&options, ",")) != NULL) {
979e82cf6aSEric Van Hensbergen 		int token;
989e82cf6aSEric Van Hensbergen 		if (!*p)
999e82cf6aSEric Van Hensbergen 			continue;
1009e82cf6aSEric Van Hensbergen 		token = match_token(p, tokens, args);
10167543e50SEric Van Hensbergen 		if (token < Opt_uname) {
1028a0dc95fSEric Van Hensbergen 			ret = match_int(&args[0], &option);
1038a0dc95fSEric Van Hensbergen 			if (ret < 0) {
104bd238fb4SLatchesar Ionkov 				P9_DPRINTK(P9_DEBUG_ERROR,
1059e82cf6aSEric Van Hensbergen 					"integer field, but no integer?\n");
1069e82cf6aSEric Van Hensbergen 				continue;
1079e82cf6aSEric Van Hensbergen 			}
1089e82cf6aSEric Van Hensbergen 		}
1099e82cf6aSEric Van Hensbergen 		switch (token) {
1109e2f6688SEric Van Hensbergen 		case Opt_debug:
1119e2f6688SEric Van Hensbergen 			v9ses->debug = option;
11210fa16e7SEric Van Hensbergen #ifdef CONFIG_NET_9P_DEBUG
1139e2f6688SEric Van Hensbergen 			p9_debug_level = option;
11410fa16e7SEric Van Hensbergen #endif
1159e2f6688SEric Van Hensbergen 			break;
1168a0dc95fSEric Van Hensbergen 
117bd32b82dSLatchesar Ionkov 		case Opt_dfltuid:
118bd32b82dSLatchesar Ionkov 			v9ses->dfltuid = option;
1199e82cf6aSEric Van Hensbergen 			break;
120bd32b82dSLatchesar Ionkov 		case Opt_dfltgid:
121bd32b82dSLatchesar Ionkov 			v9ses->dfltgid = option;
1229e82cf6aSEric Van Hensbergen 			break;
1239e82cf6aSEric Van Hensbergen 		case Opt_afid:
1249e82cf6aSEric Van Hensbergen 			v9ses->afid = option;
1259e82cf6aSEric Van Hensbergen 			break;
12667543e50SEric Van Hensbergen 		case Opt_uname:
127b32a09dbSMarkus Armbruster 			match_strlcpy(v9ses->uname, &args[0], PATH_MAX);
1289e82cf6aSEric Van Hensbergen 			break;
1299e82cf6aSEric Van Hensbergen 		case Opt_remotename:
130b32a09dbSMarkus Armbruster 			match_strlcpy(v9ses->aname, &args[0], PATH_MAX);
1319e82cf6aSEric Van Hensbergen 			break;
1329e82cf6aSEric Van Hensbergen 		case Opt_nodevmap:
1339e82cf6aSEric Van Hensbergen 			v9ses->nodev = 1;
1349e82cf6aSEric Van Hensbergen 			break;
135e03abc0cSEric Van Hensbergen 		case Opt_cache_loose:
136e03abc0cSEric Van Hensbergen 			v9ses->cache = CACHE_LOOSE;
137e03abc0cSEric Van Hensbergen 			break;
138ba17674fSLatchesar Ionkov 
139ba17674fSLatchesar Ionkov 		case Opt_access:
140ba17674fSLatchesar Ionkov 			s = match_strdup(&args[0]);
141ba17674fSLatchesar Ionkov 			v9ses->flags &= ~V9FS_ACCESS_MASK;
142ba17674fSLatchesar Ionkov 			if (strcmp(s, "user") == 0)
143ba17674fSLatchesar Ionkov 				v9ses->flags |= V9FS_ACCESS_USER;
144ba17674fSLatchesar Ionkov 			else if (strcmp(s, "any") == 0)
145ba17674fSLatchesar Ionkov 				v9ses->flags |= V9FS_ACCESS_ANY;
146ba17674fSLatchesar Ionkov 			else {
147ba17674fSLatchesar Ionkov 				v9ses->flags |= V9FS_ACCESS_SINGLE;
148ba17674fSLatchesar Ionkov 				v9ses->uid = simple_strtol(s, &e, 10);
149ba17674fSLatchesar Ionkov 				if (*e != '\0')
150ba17674fSLatchesar Ionkov 					v9ses->uid = ~0;
151ba17674fSLatchesar Ionkov 			}
1520a976297SAdrian Bunk 			kfree(s);
153ba17674fSLatchesar Ionkov 			break;
154ba17674fSLatchesar Ionkov 
1559e82cf6aSEric Van Hensbergen 		default:
1569e82cf6aSEric Van Hensbergen 			continue;
1579e82cf6aSEric Van Hensbergen 		}
1589e82cf6aSEric Van Hensbergen 	}
1598999e04fSLatchesar Ionkov 	kfree(options);
1609e82cf6aSEric Van Hensbergen }
1619e82cf6aSEric Van Hensbergen 
1629e82cf6aSEric Van Hensbergen /**
1639e82cf6aSEric Van Hensbergen  * v9fs_session_init - initialize session
1649e82cf6aSEric Van Hensbergen  * @v9ses: session information structure
1659e82cf6aSEric Van Hensbergen  * @dev_name: device being mounted
1669e82cf6aSEric Van Hensbergen  * @data: options
1679e82cf6aSEric Van Hensbergen  *
1689e82cf6aSEric Van Hensbergen  */
1699e82cf6aSEric Van Hensbergen 
170bd238fb4SLatchesar Ionkov struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
1719e82cf6aSEric Van Hensbergen 		  const char *dev_name, char *data)
1729e82cf6aSEric Van Hensbergen {
1739e82cf6aSEric Van Hensbergen 	int retval = -EINVAL;
174bd238fb4SLatchesar Ionkov 	struct p9_fid *fid;
1759e82cf6aSEric Van Hensbergen 
176ba17674fSLatchesar Ionkov 	v9ses->uname = __getname();
177ba17674fSLatchesar Ionkov 	if (!v9ses->uname)
178bd238fb4SLatchesar Ionkov 		return ERR_PTR(-ENOMEM);
1799e82cf6aSEric Van Hensbergen 
180ba17674fSLatchesar Ionkov 	v9ses->aname = __getname();
181ba17674fSLatchesar Ionkov 	if (!v9ses->aname) {
182ba17674fSLatchesar Ionkov 		__putname(v9ses->uname);
183bd238fb4SLatchesar Ionkov 		return ERR_PTR(-ENOMEM);
1849e82cf6aSEric Van Hensbergen 	}
1859e82cf6aSEric Van Hensbergen 
186ba17674fSLatchesar Ionkov 	v9ses->flags = V9FS_EXTENDED | V9FS_ACCESS_USER;
187ba17674fSLatchesar Ionkov 	strcpy(v9ses->uname, V9FS_DEFUSER);
188ba17674fSLatchesar Ionkov 	strcpy(v9ses->aname, V9FS_DEFANAME);
189ba17674fSLatchesar Ionkov 	v9ses->uid = ~0;
190bd32b82dSLatchesar Ionkov 	v9ses->dfltuid = V9FS_DEFUID;
191bd32b82dSLatchesar Ionkov 	v9ses->dfltgid = V9FS_DEFGID;
192a80d923eSEric Van Hensbergen 	v9ses->options = kstrdup(data, GFP_KERNEL);
193a80d923eSEric Van Hensbergen 	v9fs_parse_options(v9ses);
1949e82cf6aSEric Van Hensbergen 
1958a0dc95fSEric Van Hensbergen 	v9ses->clnt = p9_client_create(dev_name, v9ses->options);
1969e82cf6aSEric Van Hensbergen 
197bd238fb4SLatchesar Ionkov 	if (IS_ERR(v9ses->clnt)) {
198bd238fb4SLatchesar Ionkov 		retval = PTR_ERR(v9ses->clnt);
199bd238fb4SLatchesar Ionkov 		v9ses->clnt = NULL;
200bd238fb4SLatchesar Ionkov 		P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
201bd238fb4SLatchesar Ionkov 		goto error;
2029e82cf6aSEric Van Hensbergen 	}
2039e82cf6aSEric Van Hensbergen 
204ba17674fSLatchesar Ionkov 	if (!v9ses->clnt->dotu)
205ba17674fSLatchesar Ionkov 		v9ses->flags &= ~V9FS_EXTENDED;
206ba17674fSLatchesar Ionkov 
2078a0dc95fSEric Van Hensbergen 	v9ses->maxdata = v9ses->clnt->msize;
2088a0dc95fSEric Van Hensbergen 
209ba17674fSLatchesar Ionkov 	/* for legacy mode, fall back to V9FS_ACCESS_ANY */
210ba17674fSLatchesar Ionkov 	if (!v9fs_extended(v9ses) &&
211ba17674fSLatchesar Ionkov 		((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
212ba17674fSLatchesar Ionkov 
213ba17674fSLatchesar Ionkov 		v9ses->flags &= ~V9FS_ACCESS_MASK;
214ba17674fSLatchesar Ionkov 		v9ses->flags |= V9FS_ACCESS_ANY;
215ba17674fSLatchesar Ionkov 		v9ses->uid = ~0;
216ba17674fSLatchesar Ionkov 	}
217ba17674fSLatchesar Ionkov 
218ba17674fSLatchesar Ionkov 	fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0,
219ba17674fSLatchesar Ionkov 							v9ses->aname);
220bd238fb4SLatchesar Ionkov 	if (IS_ERR(fid)) {
221bd238fb4SLatchesar Ionkov 		retval = PTR_ERR(fid);
222bd238fb4SLatchesar Ionkov 		fid = NULL;
223bd238fb4SLatchesar Ionkov 		P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
224bd238fb4SLatchesar Ionkov 		goto error;
2259e82cf6aSEric Van Hensbergen 	}
2269e82cf6aSEric Van Hensbergen 
227ba17674fSLatchesar Ionkov 	if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
228ba17674fSLatchesar Ionkov 		fid->uid = v9ses->uid;
229ba17674fSLatchesar Ionkov 	else
230ba17674fSLatchesar Ionkov 		fid->uid = ~0;
231ba17674fSLatchesar Ionkov 
232bd238fb4SLatchesar Ionkov 	return fid;
2339e82cf6aSEric Van Hensbergen 
234bd238fb4SLatchesar Ionkov error:
2359e82cf6aSEric Van Hensbergen 	v9fs_session_close(v9ses);
236bd238fb4SLatchesar Ionkov 	return ERR_PTR(retval);
2379e82cf6aSEric Van Hensbergen }
2389e82cf6aSEric Van Hensbergen 
2399e82cf6aSEric Van Hensbergen /**
2409e82cf6aSEric Van Hensbergen  * v9fs_session_close - shutdown a session
2419e82cf6aSEric Van Hensbergen  * @v9ses: session information structure
2429e82cf6aSEric Van Hensbergen  *
2439e82cf6aSEric Van Hensbergen  */
2449e82cf6aSEric Van Hensbergen 
2459e82cf6aSEric Van Hensbergen void v9fs_session_close(struct v9fs_session_info *v9ses)
2469e82cf6aSEric Van Hensbergen {
247bd238fb4SLatchesar Ionkov 	if (v9ses->clnt) {
248bd238fb4SLatchesar Ionkov 		p9_client_destroy(v9ses->clnt);
249bd238fb4SLatchesar Ionkov 		v9ses->clnt = NULL;
2503cf6429aSLatchesar Ionkov 	}
2519e82cf6aSEric Van Hensbergen 
252ba17674fSLatchesar Ionkov 	__putname(v9ses->uname);
253ba17674fSLatchesar Ionkov 	__putname(v9ses->aname);
254a80d923eSEric Van Hensbergen 	kfree(v9ses->options);
2559e82cf6aSEric Van Hensbergen }
2569e82cf6aSEric Van Hensbergen 
257322b329aSEric Van Hensbergen /**
258ee443996SEric Van Hensbergen  * v9fs_session_cancel - terminate a session
259ee443996SEric Van Hensbergen  * @v9ses: session to terminate
260ee443996SEric Van Hensbergen  *
261ee443996SEric Van Hensbergen  * mark transport as disconnected and cancel all pending requests.
262322b329aSEric Van Hensbergen  */
263ee443996SEric Van Hensbergen 
264322b329aSEric Van Hensbergen void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
265bd238fb4SLatchesar Ionkov 	P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
266bd238fb4SLatchesar Ionkov 	p9_client_disconnect(v9ses->clnt);
267322b329aSEric Van Hensbergen }
268322b329aSEric Van Hensbergen 
2699e82cf6aSEric Van Hensbergen extern int v9fs_error_init(void);
2709e82cf6aSEric Van Hensbergen 
2719e82cf6aSEric Van Hensbergen /**
2729e82cf6aSEric Van Hensbergen  * v9fs_init - Initialize module
2739e82cf6aSEric Van Hensbergen  *
2749e82cf6aSEric Van Hensbergen  */
2759e82cf6aSEric Van Hensbergen 
2769e82cf6aSEric Van Hensbergen static int __init init_v9fs(void)
2779e82cf6aSEric Van Hensbergen {
278f94b3470SEric Van Hensbergen 	printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
279a80d923eSEric Van Hensbergen 	/* TODO: Setup list of registered trasnport modules */
280bd238fb4SLatchesar Ionkov 	return register_filesystem(&v9fs_fs_type);
2819e82cf6aSEric Van Hensbergen }
2829e82cf6aSEric Van Hensbergen 
2839e82cf6aSEric Van Hensbergen /**
2849e82cf6aSEric Van Hensbergen  * v9fs_init - shutdown module
2859e82cf6aSEric Van Hensbergen  *
2869e82cf6aSEric Van Hensbergen  */
2879e82cf6aSEric Van Hensbergen 
2889e82cf6aSEric Van Hensbergen static void __exit exit_v9fs(void)
2899e82cf6aSEric Van Hensbergen {
2909e82cf6aSEric Van Hensbergen 	unregister_filesystem(&v9fs_fs_type);
2919e82cf6aSEric Van Hensbergen }
2929e82cf6aSEric Van Hensbergen 
2939e82cf6aSEric Van Hensbergen module_init(init_v9fs)
2949e82cf6aSEric Van Hensbergen module_exit(exit_v9fs)
2959e82cf6aSEric Van Hensbergen 
296bd238fb4SLatchesar Ionkov MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
2979e82cf6aSEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
2989e82cf6aSEric Van Hensbergen MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
2999e82cf6aSEric Van Hensbergen MODULE_LICENSE("GPL");
300