xref: /openbmc/linux/fs/9p/v9fs.c (revision b32a09db)
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  * @options: options string passed from mount
759e82cf6aSEric Van Hensbergen  * @v9ses: existing v9fs session information
769e82cf6aSEric Van Hensbergen  *
779e82cf6aSEric Van Hensbergen  */
789e82cf6aSEric Van Hensbergen 
79a80d923eSEric Van Hensbergen static void v9fs_parse_options(struct v9fs_session_info *v9ses)
809e82cf6aSEric Van Hensbergen {
818999e04fSLatchesar Ionkov 	char *options;
829e82cf6aSEric Van Hensbergen 	substring_t args[MAX_OPT_ARGS];
83a80d923eSEric Van Hensbergen 	char *p;
848a0dc95fSEric Van Hensbergen 	int option = 0;
85ba17674fSLatchesar Ionkov 	char *s, *e;
868a0dc95fSEric Van Hensbergen 	int ret;
879e82cf6aSEric Van Hensbergen 
889e82cf6aSEric Van Hensbergen 	/* setup defaults */
899e82cf6aSEric Van Hensbergen 	v9ses->afid = ~0;
909e82cf6aSEric Van Hensbergen 	v9ses->debug = 0;
91e03abc0cSEric Van Hensbergen 	v9ses->cache = 0;
929e82cf6aSEric Van Hensbergen 
938999e04fSLatchesar Ionkov 	if (!v9ses->options)
949e82cf6aSEric Van Hensbergen 		return;
959e82cf6aSEric Van Hensbergen 
968999e04fSLatchesar Ionkov 	options = kstrdup(v9ses->options, GFP_KERNEL);
979e82cf6aSEric Van Hensbergen 	while ((p = strsep(&options, ",")) != NULL) {
989e82cf6aSEric Van Hensbergen 		int token;
999e82cf6aSEric Van Hensbergen 		if (!*p)
1009e82cf6aSEric Van Hensbergen 			continue;
1019e82cf6aSEric Van Hensbergen 		token = match_token(p, tokens, args);
10267543e50SEric Van Hensbergen 		if (token < Opt_uname) {
1038a0dc95fSEric Van Hensbergen 			ret = match_int(&args[0], &option);
1048a0dc95fSEric Van Hensbergen 			if (ret < 0) {
105bd238fb4SLatchesar Ionkov 				P9_DPRINTK(P9_DEBUG_ERROR,
1069e82cf6aSEric Van Hensbergen 					"integer field, but no integer?\n");
1079e82cf6aSEric Van Hensbergen 				continue;
1089e82cf6aSEric Van Hensbergen 			}
1099e82cf6aSEric Van Hensbergen 		}
1109e82cf6aSEric Van Hensbergen 		switch (token) {
1119e2f6688SEric Van Hensbergen 		case Opt_debug:
1129e2f6688SEric Van Hensbergen 			v9ses->debug = option;
11310fa16e7SEric Van Hensbergen #ifdef CONFIG_NET_9P_DEBUG
1149e2f6688SEric Van Hensbergen 			p9_debug_level = option;
11510fa16e7SEric Van Hensbergen #endif
1169e2f6688SEric Van Hensbergen 			break;
1178a0dc95fSEric Van Hensbergen 
118bd32b82dSLatchesar Ionkov 		case Opt_dfltuid:
119bd32b82dSLatchesar Ionkov 			v9ses->dfltuid = option;
1209e82cf6aSEric Van Hensbergen 			break;
121bd32b82dSLatchesar Ionkov 		case Opt_dfltgid:
122bd32b82dSLatchesar Ionkov 			v9ses->dfltgid = option;
1239e82cf6aSEric Van Hensbergen 			break;
1249e82cf6aSEric Van Hensbergen 		case Opt_afid:
1259e82cf6aSEric Van Hensbergen 			v9ses->afid = option;
1269e82cf6aSEric Van Hensbergen 			break;
12767543e50SEric Van Hensbergen 		case Opt_uname:
128b32a09dbSMarkus Armbruster 			match_strlcpy(v9ses->uname, &args[0], PATH_MAX);
1299e82cf6aSEric Van Hensbergen 			break;
1309e82cf6aSEric Van Hensbergen 		case Opt_remotename:
131b32a09dbSMarkus Armbruster 			match_strlcpy(v9ses->aname, &args[0], PATH_MAX);
1329e82cf6aSEric Van Hensbergen 			break;
1339e82cf6aSEric Van Hensbergen 		case Opt_nodevmap:
1349e82cf6aSEric Van Hensbergen 			v9ses->nodev = 1;
1359e82cf6aSEric Van Hensbergen 			break;
136e03abc0cSEric Van Hensbergen 		case Opt_cache_loose:
137e03abc0cSEric Van Hensbergen 			v9ses->cache = CACHE_LOOSE;
138e03abc0cSEric Van Hensbergen 			break;
139ba17674fSLatchesar Ionkov 
140ba17674fSLatchesar Ionkov 		case Opt_access:
141ba17674fSLatchesar Ionkov 			s = match_strdup(&args[0]);
142ba17674fSLatchesar Ionkov 			v9ses->flags &= ~V9FS_ACCESS_MASK;
143ba17674fSLatchesar Ionkov 			if (strcmp(s, "user") == 0)
144ba17674fSLatchesar Ionkov 				v9ses->flags |= V9FS_ACCESS_USER;
145ba17674fSLatchesar Ionkov 			else if (strcmp(s, "any") == 0)
146ba17674fSLatchesar Ionkov 				v9ses->flags |= V9FS_ACCESS_ANY;
147ba17674fSLatchesar Ionkov 			else {
148ba17674fSLatchesar Ionkov 				v9ses->flags |= V9FS_ACCESS_SINGLE;
149ba17674fSLatchesar Ionkov 				v9ses->uid = simple_strtol(s, &e, 10);
150ba17674fSLatchesar Ionkov 				if (*e != '\0')
151ba17674fSLatchesar Ionkov 					v9ses->uid = ~0;
152ba17674fSLatchesar Ionkov 			}
1530a976297SAdrian Bunk 			kfree(s);
154ba17674fSLatchesar Ionkov 			break;
155ba17674fSLatchesar Ionkov 
1569e82cf6aSEric Van Hensbergen 		default:
1579e82cf6aSEric Van Hensbergen 			continue;
1589e82cf6aSEric Van Hensbergen 		}
1599e82cf6aSEric Van Hensbergen 	}
1608999e04fSLatchesar Ionkov 	kfree(options);
1619e82cf6aSEric Van Hensbergen }
1629e82cf6aSEric Van Hensbergen 
1639e82cf6aSEric Van Hensbergen /**
1649e82cf6aSEric Van Hensbergen  * v9fs_session_init - initialize session
1659e82cf6aSEric Van Hensbergen  * @v9ses: session information structure
1669e82cf6aSEric Van Hensbergen  * @dev_name: device being mounted
1679e82cf6aSEric Van Hensbergen  * @data: options
1689e82cf6aSEric Van Hensbergen  *
1699e82cf6aSEric Van Hensbergen  */
1709e82cf6aSEric Van Hensbergen 
171bd238fb4SLatchesar Ionkov struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
1729e82cf6aSEric Van Hensbergen 		  const char *dev_name, char *data)
1739e82cf6aSEric Van Hensbergen {
1749e82cf6aSEric Van Hensbergen 	int retval = -EINVAL;
175bd238fb4SLatchesar Ionkov 	struct p9_fid *fid;
1769e82cf6aSEric Van Hensbergen 
177ba17674fSLatchesar Ionkov 	v9ses->uname = __getname();
178ba17674fSLatchesar Ionkov 	if (!v9ses->uname)
179bd238fb4SLatchesar Ionkov 		return ERR_PTR(-ENOMEM);
1809e82cf6aSEric Van Hensbergen 
181ba17674fSLatchesar Ionkov 	v9ses->aname = __getname();
182ba17674fSLatchesar Ionkov 	if (!v9ses->aname) {
183ba17674fSLatchesar Ionkov 		__putname(v9ses->uname);
184bd238fb4SLatchesar Ionkov 		return ERR_PTR(-ENOMEM);
1859e82cf6aSEric Van Hensbergen 	}
1869e82cf6aSEric Van Hensbergen 
187ba17674fSLatchesar Ionkov 	v9ses->flags = V9FS_EXTENDED | V9FS_ACCESS_USER;
188ba17674fSLatchesar Ionkov 	strcpy(v9ses->uname, V9FS_DEFUSER);
189ba17674fSLatchesar Ionkov 	strcpy(v9ses->aname, V9FS_DEFANAME);
190ba17674fSLatchesar Ionkov 	v9ses->uid = ~0;
191bd32b82dSLatchesar Ionkov 	v9ses->dfltuid = V9FS_DEFUID;
192bd32b82dSLatchesar Ionkov 	v9ses->dfltgid = V9FS_DEFGID;
193a80d923eSEric Van Hensbergen 	v9ses->options = kstrdup(data, GFP_KERNEL);
194a80d923eSEric Van Hensbergen 	v9fs_parse_options(v9ses);
1959e82cf6aSEric Van Hensbergen 
1968a0dc95fSEric Van Hensbergen 	v9ses->clnt = p9_client_create(dev_name, v9ses->options);
1979e82cf6aSEric Van Hensbergen 
198bd238fb4SLatchesar Ionkov 	if (IS_ERR(v9ses->clnt)) {
199bd238fb4SLatchesar Ionkov 		retval = PTR_ERR(v9ses->clnt);
200bd238fb4SLatchesar Ionkov 		v9ses->clnt = NULL;
201bd238fb4SLatchesar Ionkov 		P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
202bd238fb4SLatchesar Ionkov 		goto error;
2039e82cf6aSEric Van Hensbergen 	}
2049e82cf6aSEric Van Hensbergen 
205ba17674fSLatchesar Ionkov 	if (!v9ses->clnt->dotu)
206ba17674fSLatchesar Ionkov 		v9ses->flags &= ~V9FS_EXTENDED;
207ba17674fSLatchesar Ionkov 
2088a0dc95fSEric Van Hensbergen 	v9ses->maxdata = v9ses->clnt->msize;
2098a0dc95fSEric Van Hensbergen 
210ba17674fSLatchesar Ionkov 	/* for legacy mode, fall back to V9FS_ACCESS_ANY */
211ba17674fSLatchesar Ionkov 	if (!v9fs_extended(v9ses) &&
212ba17674fSLatchesar Ionkov 		((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
213ba17674fSLatchesar Ionkov 
214ba17674fSLatchesar Ionkov 		v9ses->flags &= ~V9FS_ACCESS_MASK;
215ba17674fSLatchesar Ionkov 		v9ses->flags |= V9FS_ACCESS_ANY;
216ba17674fSLatchesar Ionkov 		v9ses->uid = ~0;
217ba17674fSLatchesar Ionkov 	}
218ba17674fSLatchesar Ionkov 
219ba17674fSLatchesar Ionkov 	fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0,
220ba17674fSLatchesar Ionkov 							v9ses->aname);
221bd238fb4SLatchesar Ionkov 	if (IS_ERR(fid)) {
222bd238fb4SLatchesar Ionkov 		retval = PTR_ERR(fid);
223bd238fb4SLatchesar Ionkov 		fid = NULL;
224bd238fb4SLatchesar Ionkov 		P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
225bd238fb4SLatchesar Ionkov 		goto error;
2269e82cf6aSEric Van Hensbergen 	}
2279e82cf6aSEric Van Hensbergen 
228ba17674fSLatchesar Ionkov 	if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
229ba17674fSLatchesar Ionkov 		fid->uid = v9ses->uid;
230ba17674fSLatchesar Ionkov 	else
231ba17674fSLatchesar Ionkov 		fid->uid = ~0;
232ba17674fSLatchesar Ionkov 
233bd238fb4SLatchesar Ionkov 	return fid;
2349e82cf6aSEric Van Hensbergen 
235bd238fb4SLatchesar Ionkov error:
2369e82cf6aSEric Van Hensbergen 	v9fs_session_close(v9ses);
237bd238fb4SLatchesar Ionkov 	return ERR_PTR(retval);
2389e82cf6aSEric Van Hensbergen }
2399e82cf6aSEric Van Hensbergen 
2409e82cf6aSEric Van Hensbergen /**
2419e82cf6aSEric Van Hensbergen  * v9fs_session_close - shutdown a session
2429e82cf6aSEric Van Hensbergen  * @v9ses: session information structure
2439e82cf6aSEric Van Hensbergen  *
2449e82cf6aSEric Van Hensbergen  */
2459e82cf6aSEric Van Hensbergen 
2469e82cf6aSEric Van Hensbergen void v9fs_session_close(struct v9fs_session_info *v9ses)
2479e82cf6aSEric Van Hensbergen {
248bd238fb4SLatchesar Ionkov 	if (v9ses->clnt) {
249bd238fb4SLatchesar Ionkov 		p9_client_destroy(v9ses->clnt);
250bd238fb4SLatchesar Ionkov 		v9ses->clnt = NULL;
2513cf6429aSLatchesar Ionkov 	}
2529e82cf6aSEric Van Hensbergen 
253ba17674fSLatchesar Ionkov 	__putname(v9ses->uname);
254ba17674fSLatchesar Ionkov 	__putname(v9ses->aname);
255a80d923eSEric Van Hensbergen 	kfree(v9ses->options);
2569e82cf6aSEric Van Hensbergen }
2579e82cf6aSEric Van Hensbergen 
258322b329aSEric Van Hensbergen /**
259322b329aSEric Van Hensbergen  * v9fs_session_cancel - mark transport as disconnected
260322b329aSEric Van Hensbergen  * 	and cancel all pending requests.
261322b329aSEric Van Hensbergen  */
262322b329aSEric Van Hensbergen void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
263bd238fb4SLatchesar Ionkov 	P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
264bd238fb4SLatchesar Ionkov 	p9_client_disconnect(v9ses->clnt);
265322b329aSEric Van Hensbergen }
266322b329aSEric Van Hensbergen 
2679e82cf6aSEric Van Hensbergen extern int v9fs_error_init(void);
2689e82cf6aSEric Van Hensbergen 
2699e82cf6aSEric Van Hensbergen /**
2709e82cf6aSEric Van Hensbergen  * v9fs_init - Initialize module
2719e82cf6aSEric Van Hensbergen  *
2729e82cf6aSEric Van Hensbergen  */
2739e82cf6aSEric Van Hensbergen 
2749e82cf6aSEric Van Hensbergen static int __init init_v9fs(void)
2759e82cf6aSEric Van Hensbergen {
276f94b3470SEric Van Hensbergen 	printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
277a80d923eSEric Van Hensbergen 	/* TODO: Setup list of registered trasnport modules */
278bd238fb4SLatchesar Ionkov 	return register_filesystem(&v9fs_fs_type);
2799e82cf6aSEric Van Hensbergen }
2809e82cf6aSEric Van Hensbergen 
2819e82cf6aSEric Van Hensbergen /**
2829e82cf6aSEric Van Hensbergen  * v9fs_init - shutdown module
2839e82cf6aSEric Van Hensbergen  *
2849e82cf6aSEric Van Hensbergen  */
2859e82cf6aSEric Van Hensbergen 
2869e82cf6aSEric Van Hensbergen static void __exit exit_v9fs(void)
2879e82cf6aSEric Van Hensbergen {
2889e82cf6aSEric Van Hensbergen 	unregister_filesystem(&v9fs_fs_type);
2899e82cf6aSEric Van Hensbergen }
2909e82cf6aSEric Van Hensbergen 
2919e82cf6aSEric Van Hensbergen module_init(init_v9fs)
2929e82cf6aSEric Van Hensbergen module_exit(exit_v9fs)
2939e82cf6aSEric Van Hensbergen 
294bd238fb4SLatchesar Ionkov MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
2959e82cf6aSEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
2969e82cf6aSEric Van Hensbergen MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
2979e82cf6aSEric Van Hensbergen MODULE_LICENSE("GPL");
298