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 * 69e82cf6aSEric Van Hensbergen * Copyright (C) 2004 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/conn.h> 35bd238fb4SLatchesar Ionkov #include <net/9p/client.h> 369e82cf6aSEric Van Hensbergen #include "v9fs.h" 379e82cf6aSEric Van Hensbergen #include "v9fs_vfs.h" 389e82cf6aSEric Van Hensbergen 399e82cf6aSEric Van Hensbergen /* 40a80d923eSEric Van Hensbergen * Option Parsing (code inspired by NFS code) 41a80d923eSEric Van Hensbergen * NOTE: each transport will parse its own options 42a80d923eSEric Van Hensbergen */ 43a80d923eSEric Van Hensbergen 449e82cf6aSEric Van Hensbergen enum { 459e82cf6aSEric Van Hensbergen /* Options that take integer arguments */ 46bd32b82dSLatchesar Ionkov Opt_debug, Opt_msize, Opt_dfltuid, Opt_dfltgid, Opt_afid, 479e82cf6aSEric Van Hensbergen /* String options */ 48a80d923eSEric Van Hensbergen Opt_uname, Opt_remotename, Opt_trans, 499e82cf6aSEric Van Hensbergen /* Options that take no arguments */ 50a80d923eSEric Van Hensbergen Opt_legacy, Opt_nodevmap, 51e03abc0cSEric Van Hensbergen /* Cache options */ 52e03abc0cSEric Van Hensbergen Opt_cache_loose, 53ba17674fSLatchesar Ionkov /* Access options */ 54ba17674fSLatchesar Ionkov Opt_access, 559e82cf6aSEric Van Hensbergen /* Error token */ 569e82cf6aSEric Van Hensbergen Opt_err 579e82cf6aSEric Van Hensbergen }; 589e82cf6aSEric Van Hensbergen 599e82cf6aSEric Van Hensbergen static match_table_t tokens = { 609e2f6688SEric Van Hensbergen {Opt_debug, "debug=%x"}, 619e82cf6aSEric Van Hensbergen {Opt_msize, "msize=%u"}, 62bd32b82dSLatchesar Ionkov {Opt_dfltuid, "dfltuid=%u"}, 63bd32b82dSLatchesar Ionkov {Opt_dfltgid, "dfltgid=%u"}, 649e82cf6aSEric Van Hensbergen {Opt_afid, "afid=%u"}, 6567543e50SEric Van Hensbergen {Opt_uname, "uname=%s"}, 669e82cf6aSEric Van Hensbergen {Opt_remotename, "aname=%s"}, 67a80d923eSEric Van Hensbergen {Opt_trans, "trans=%s"}, 689e82cf6aSEric Van Hensbergen {Opt_legacy, "noextend"}, 699e82cf6aSEric Van Hensbergen {Opt_nodevmap, "nodevmap"}, 70e03abc0cSEric Van Hensbergen {Opt_cache_loose, "cache=loose"}, 71e03abc0cSEric Van Hensbergen {Opt_cache_loose, "loose"}, 72ba17674fSLatchesar Ionkov {Opt_access, "access=%s"}, 739e82cf6aSEric Van Hensbergen {Opt_err, NULL} 749e82cf6aSEric Van Hensbergen }; 759e82cf6aSEric Van Hensbergen 769e82cf6aSEric Van Hensbergen /** 779e82cf6aSEric Van Hensbergen * v9fs_parse_options - parse mount options into session structure 789e82cf6aSEric Van Hensbergen * @options: options string passed from mount 799e82cf6aSEric Van Hensbergen * @v9ses: existing v9fs session information 809e82cf6aSEric Van Hensbergen * 819e82cf6aSEric Van Hensbergen */ 829e82cf6aSEric Van Hensbergen 83a80d923eSEric Van Hensbergen static void v9fs_parse_options(struct v9fs_session_info *v9ses) 849e82cf6aSEric Van Hensbergen { 85a80d923eSEric Van Hensbergen char *options = v9ses->options; 869e82cf6aSEric Van Hensbergen substring_t args[MAX_OPT_ARGS]; 87a80d923eSEric Van Hensbergen char *p; 889e82cf6aSEric Van Hensbergen int option; 899e82cf6aSEric Van Hensbergen int ret; 90ba17674fSLatchesar Ionkov char *s, *e; 919e82cf6aSEric Van Hensbergen 929e82cf6aSEric Van Hensbergen /* setup defaults */ 93a80d923eSEric Van Hensbergen v9ses->maxdata = 8192; 949e82cf6aSEric Van Hensbergen v9ses->afid = ~0; 959e82cf6aSEric Van Hensbergen v9ses->debug = 0; 96e03abc0cSEric Van Hensbergen v9ses->cache = 0; 97fb0466c3SEric Van Hensbergen v9ses->trans = v9fs_default_trans(); 989e82cf6aSEric Van Hensbergen 999e82cf6aSEric Van Hensbergen if (!options) 1009e82cf6aSEric Van Hensbergen return; 1019e82cf6aSEric Van Hensbergen 1029e82cf6aSEric Van Hensbergen while ((p = strsep(&options, ",")) != NULL) { 1039e82cf6aSEric Van Hensbergen int token; 1049e82cf6aSEric Van Hensbergen if (!*p) 1059e82cf6aSEric Van Hensbergen continue; 1069e82cf6aSEric Van Hensbergen token = match_token(p, tokens, args); 10767543e50SEric Van Hensbergen if (token < Opt_uname) { 1089e82cf6aSEric Van Hensbergen if ((ret = match_int(&args[0], &option)) < 0) { 109bd238fb4SLatchesar Ionkov P9_DPRINTK(P9_DEBUG_ERROR, 1109e82cf6aSEric Van Hensbergen "integer field, but no integer?\n"); 1119e82cf6aSEric Van Hensbergen continue; 1129e82cf6aSEric Van Hensbergen } 1139e82cf6aSEric Van Hensbergen } 1149e82cf6aSEric Van Hensbergen switch (token) { 1159e2f6688SEric Van Hensbergen case Opt_debug: 1169e2f6688SEric Van Hensbergen v9ses->debug = option; 11710fa16e7SEric Van Hensbergen #ifdef CONFIG_NET_9P_DEBUG 1189e2f6688SEric Van Hensbergen p9_debug_level = option; 11910fa16e7SEric Van Hensbergen #endif 1209e2f6688SEric Van Hensbergen break; 1219e82cf6aSEric Van Hensbergen case Opt_msize: 1229e82cf6aSEric Van Hensbergen v9ses->maxdata = option; 1239e82cf6aSEric Van Hensbergen break; 124bd32b82dSLatchesar Ionkov case Opt_dfltuid: 125bd32b82dSLatchesar Ionkov v9ses->dfltuid = option; 1269e82cf6aSEric Van Hensbergen break; 127bd32b82dSLatchesar Ionkov case Opt_dfltgid: 128bd32b82dSLatchesar Ionkov v9ses->dfltgid = option; 1299e82cf6aSEric Van Hensbergen break; 1309e82cf6aSEric Van Hensbergen case Opt_afid: 1319e82cf6aSEric Van Hensbergen v9ses->afid = option; 1329e82cf6aSEric Van Hensbergen break; 133a80d923eSEric Van Hensbergen case Opt_trans: 134a80d923eSEric Van Hensbergen v9ses->trans = v9fs_match_trans(&args[0]); 1359e82cf6aSEric Van Hensbergen break; 13667543e50SEric Van Hensbergen case Opt_uname: 137ba17674fSLatchesar Ionkov match_strcpy(v9ses->uname, &args[0]); 1389e82cf6aSEric Van Hensbergen break; 1399e82cf6aSEric Van Hensbergen case Opt_remotename: 140ba17674fSLatchesar Ionkov match_strcpy(v9ses->aname, &args[0]); 1419e82cf6aSEric Van Hensbergen break; 1429e82cf6aSEric Van Hensbergen case Opt_legacy: 1432405669bSLatchesar Ionkov v9ses->flags &= ~V9FS_EXTENDED; 1449e82cf6aSEric Van Hensbergen break; 1459e82cf6aSEric Van Hensbergen case Opt_nodevmap: 1469e82cf6aSEric Van Hensbergen v9ses->nodev = 1; 1479e82cf6aSEric Van Hensbergen break; 148e03abc0cSEric Van Hensbergen case Opt_cache_loose: 149e03abc0cSEric Van Hensbergen v9ses->cache = CACHE_LOOSE; 150e03abc0cSEric Van Hensbergen break; 151ba17674fSLatchesar Ionkov 152ba17674fSLatchesar Ionkov case Opt_access: 153ba17674fSLatchesar Ionkov s = match_strdup(&args[0]); 154ba17674fSLatchesar Ionkov v9ses->flags &= ~V9FS_ACCESS_MASK; 155ba17674fSLatchesar Ionkov if (strcmp(s, "user") == 0) 156ba17674fSLatchesar Ionkov v9ses->flags |= V9FS_ACCESS_USER; 157ba17674fSLatchesar Ionkov else if (strcmp(s, "any") == 0) 158ba17674fSLatchesar Ionkov v9ses->flags |= V9FS_ACCESS_ANY; 159ba17674fSLatchesar Ionkov else { 160ba17674fSLatchesar Ionkov v9ses->flags |= V9FS_ACCESS_SINGLE; 161ba17674fSLatchesar Ionkov v9ses->uid = simple_strtol(s, &e, 10); 162ba17674fSLatchesar Ionkov if (*e != '\0') 163ba17674fSLatchesar Ionkov v9ses->uid = ~0; 164ba17674fSLatchesar Ionkov } 165ba17674fSLatchesar Ionkov break; 166ba17674fSLatchesar Ionkov 1679e82cf6aSEric Van Hensbergen default: 1689e82cf6aSEric Van Hensbergen continue; 1699e82cf6aSEric Van Hensbergen } 1709e82cf6aSEric Van Hensbergen } 1719e82cf6aSEric Van Hensbergen } 1729e82cf6aSEric Van Hensbergen 1739e82cf6aSEric Van Hensbergen /** 1749e82cf6aSEric Van Hensbergen * v9fs_session_init - initialize session 1759e82cf6aSEric Van Hensbergen * @v9ses: session information structure 1769e82cf6aSEric Van Hensbergen * @dev_name: device being mounted 1779e82cf6aSEric Van Hensbergen * @data: options 1789e82cf6aSEric Van Hensbergen * 1799e82cf6aSEric Van Hensbergen */ 1809e82cf6aSEric Van Hensbergen 181bd238fb4SLatchesar Ionkov struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses, 1829e82cf6aSEric Van Hensbergen const char *dev_name, char *data) 1839e82cf6aSEric Van Hensbergen { 1849e82cf6aSEric Van Hensbergen int retval = -EINVAL; 185a80d923eSEric Van Hensbergen struct p9_trans *trans = NULL; 186bd238fb4SLatchesar Ionkov struct p9_fid *fid; 1879e82cf6aSEric Van Hensbergen 188ba17674fSLatchesar Ionkov v9ses->uname = __getname(); 189ba17674fSLatchesar Ionkov if (!v9ses->uname) 190bd238fb4SLatchesar Ionkov return ERR_PTR(-ENOMEM); 1919e82cf6aSEric Van Hensbergen 192ba17674fSLatchesar Ionkov v9ses->aname = __getname(); 193ba17674fSLatchesar Ionkov if (!v9ses->aname) { 194ba17674fSLatchesar Ionkov __putname(v9ses->uname); 195bd238fb4SLatchesar Ionkov return ERR_PTR(-ENOMEM); 1969e82cf6aSEric Van Hensbergen } 1979e82cf6aSEric Van Hensbergen 198ba17674fSLatchesar Ionkov v9ses->flags = V9FS_EXTENDED | V9FS_ACCESS_USER; 199ba17674fSLatchesar Ionkov strcpy(v9ses->uname, V9FS_DEFUSER); 200ba17674fSLatchesar Ionkov strcpy(v9ses->aname, V9FS_DEFANAME); 201ba17674fSLatchesar Ionkov v9ses->uid = ~0; 202bd32b82dSLatchesar Ionkov v9ses->dfltuid = V9FS_DEFUID; 203bd32b82dSLatchesar Ionkov v9ses->dfltgid = V9FS_DEFGID; 204a80d923eSEric Van Hensbergen v9ses->options = kstrdup(data, GFP_KERNEL); 205a80d923eSEric Van Hensbergen v9fs_parse_options(v9ses); 2069e82cf6aSEric Van Hensbergen 207a80d923eSEric Van Hensbergen if (v9ses->trans == NULL) { 208a80d923eSEric Van Hensbergen retval = -EPROTONOSUPPORT; 209a80d923eSEric Van Hensbergen P9_DPRINTK(P9_DEBUG_ERROR, 210a80d923eSEric Van Hensbergen "No transport defined or default transport\n"); 211bd238fb4SLatchesar Ionkov goto error; 212a80d923eSEric Van Hensbergen } 2139e82cf6aSEric Van Hensbergen 214a80d923eSEric Van Hensbergen trans = v9ses->trans->create(dev_name, v9ses->options); 215bd238fb4SLatchesar Ionkov if (IS_ERR(trans)) { 216bd238fb4SLatchesar Ionkov retval = PTR_ERR(trans); 217bd238fb4SLatchesar Ionkov trans = NULL; 218bd238fb4SLatchesar Ionkov goto error; 219a8e63bffSLatchesar Ionkov } 220a80d923eSEric Van Hensbergen if ((v9ses->maxdata+P9_IOHDRSZ) > v9ses->trans->maxsize) 221a80d923eSEric Van Hensbergen v9ses->maxdata = v9ses->trans->maxsize-P9_IOHDRSZ; 222a8e63bffSLatchesar Ionkov 223bd238fb4SLatchesar Ionkov v9ses->clnt = p9_client_create(trans, v9ses->maxdata+P9_IOHDRSZ, 2242405669bSLatchesar Ionkov v9fs_extended(v9ses)); 2259e82cf6aSEric Van Hensbergen 226bd238fb4SLatchesar Ionkov if (IS_ERR(v9ses->clnt)) { 227bd238fb4SLatchesar Ionkov retval = PTR_ERR(v9ses->clnt); 228bd238fb4SLatchesar Ionkov v9ses->clnt = NULL; 229bd238fb4SLatchesar Ionkov P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n"); 230bd238fb4SLatchesar Ionkov goto error; 2319e82cf6aSEric Van Hensbergen } 2329e82cf6aSEric Van Hensbergen 233ba17674fSLatchesar Ionkov if (!v9ses->clnt->dotu) 234ba17674fSLatchesar Ionkov v9ses->flags &= ~V9FS_EXTENDED; 235ba17674fSLatchesar Ionkov 236ba17674fSLatchesar Ionkov /* for legacy mode, fall back to V9FS_ACCESS_ANY */ 237ba17674fSLatchesar Ionkov if (!v9fs_extended(v9ses) && 238ba17674fSLatchesar Ionkov ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) { 239ba17674fSLatchesar Ionkov 240ba17674fSLatchesar Ionkov v9ses->flags &= ~V9FS_ACCESS_MASK; 241ba17674fSLatchesar Ionkov v9ses->flags |= V9FS_ACCESS_ANY; 242ba17674fSLatchesar Ionkov v9ses->uid = ~0; 243ba17674fSLatchesar Ionkov } 244ba17674fSLatchesar Ionkov 245ba17674fSLatchesar Ionkov fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0, 246ba17674fSLatchesar Ionkov v9ses->aname); 247bd238fb4SLatchesar Ionkov if (IS_ERR(fid)) { 248bd238fb4SLatchesar Ionkov retval = PTR_ERR(fid); 249bd238fb4SLatchesar Ionkov fid = NULL; 250bd238fb4SLatchesar Ionkov P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n"); 251bd238fb4SLatchesar Ionkov goto error; 2529e82cf6aSEric Van Hensbergen } 2539e82cf6aSEric Van Hensbergen 254ba17674fSLatchesar Ionkov if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE) 255ba17674fSLatchesar Ionkov fid->uid = v9ses->uid; 256ba17674fSLatchesar Ionkov else 257ba17674fSLatchesar Ionkov fid->uid = ~0; 258ba17674fSLatchesar Ionkov 259bd238fb4SLatchesar Ionkov return fid; 2609e82cf6aSEric Van Hensbergen 261bd238fb4SLatchesar Ionkov error: 2629e82cf6aSEric Van Hensbergen v9fs_session_close(v9ses); 263bd238fb4SLatchesar Ionkov return ERR_PTR(retval); 2649e82cf6aSEric Van Hensbergen } 2659e82cf6aSEric Van Hensbergen 2669e82cf6aSEric Van Hensbergen /** 2679e82cf6aSEric Van Hensbergen * v9fs_session_close - shutdown a session 2689e82cf6aSEric Van Hensbergen * @v9ses: session information structure 2699e82cf6aSEric Van Hensbergen * 2709e82cf6aSEric Van Hensbergen */ 2719e82cf6aSEric Van Hensbergen 2729e82cf6aSEric Van Hensbergen void v9fs_session_close(struct v9fs_session_info *v9ses) 2739e82cf6aSEric Van Hensbergen { 274bd238fb4SLatchesar Ionkov if (v9ses->clnt) { 275bd238fb4SLatchesar Ionkov p9_client_destroy(v9ses->clnt); 276bd238fb4SLatchesar Ionkov v9ses->clnt = NULL; 2773cf6429aSLatchesar Ionkov } 2789e82cf6aSEric Van Hensbergen 279ba17674fSLatchesar Ionkov __putname(v9ses->uname); 280ba17674fSLatchesar Ionkov __putname(v9ses->aname); 281a80d923eSEric Van Hensbergen kfree(v9ses->options); 2829e82cf6aSEric Van Hensbergen } 2839e82cf6aSEric Van Hensbergen 284322b329aSEric Van Hensbergen /** 285322b329aSEric Van Hensbergen * v9fs_session_cancel - mark transport as disconnected 286322b329aSEric Van Hensbergen * and cancel all pending requests. 287322b329aSEric Van Hensbergen */ 288322b329aSEric Van Hensbergen void v9fs_session_cancel(struct v9fs_session_info *v9ses) { 289bd238fb4SLatchesar Ionkov P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses); 290bd238fb4SLatchesar Ionkov p9_client_disconnect(v9ses->clnt); 291322b329aSEric Van Hensbergen } 292322b329aSEric Van Hensbergen 2939e82cf6aSEric Van Hensbergen extern int v9fs_error_init(void); 2949e82cf6aSEric Van Hensbergen 2959e82cf6aSEric Van Hensbergen /** 2969e82cf6aSEric Van Hensbergen * v9fs_init - Initialize module 2979e82cf6aSEric Van Hensbergen * 2989e82cf6aSEric Van Hensbergen */ 2999e82cf6aSEric Van Hensbergen 3009e82cf6aSEric Van Hensbergen static int __init init_v9fs(void) 3019e82cf6aSEric Van Hensbergen { 302f94b3470SEric Van Hensbergen printk(KERN_INFO "Installing v9fs 9p2000 file system support\n"); 303a80d923eSEric Van Hensbergen /* TODO: Setup list of registered trasnport modules */ 304bd238fb4SLatchesar Ionkov return register_filesystem(&v9fs_fs_type); 3059e82cf6aSEric Van Hensbergen } 3069e82cf6aSEric Van Hensbergen 3079e82cf6aSEric Van Hensbergen /** 3089e82cf6aSEric Van Hensbergen * v9fs_init - shutdown module 3099e82cf6aSEric Van Hensbergen * 3109e82cf6aSEric Van Hensbergen */ 3119e82cf6aSEric Van Hensbergen 3129e82cf6aSEric Van Hensbergen static void __exit exit_v9fs(void) 3139e82cf6aSEric Van Hensbergen { 3149e82cf6aSEric Van Hensbergen unregister_filesystem(&v9fs_fs_type); 3159e82cf6aSEric Van Hensbergen } 3169e82cf6aSEric Van Hensbergen 3179e82cf6aSEric Van Hensbergen module_init(init_v9fs) 3189e82cf6aSEric Van Hensbergen module_exit(exit_v9fs) 3199e82cf6aSEric Van Hensbergen 320bd238fb4SLatchesar Ionkov MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>"); 3219e82cf6aSEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>"); 3229e82cf6aSEric Van Hensbergen MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>"); 3239e82cf6aSEric Van Hensbergen MODULE_LICENSE("GPL"); 324