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 { 858999e04fSLatchesar Ionkov char *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 998999e04fSLatchesar Ionkov if (!v9ses->options) 1009e82cf6aSEric Van Hensbergen return; 1019e82cf6aSEric Van Hensbergen 1028999e04fSLatchesar Ionkov options = kstrdup(v9ses->options, GFP_KERNEL); 1039e82cf6aSEric Van Hensbergen while ((p = strsep(&options, ",")) != NULL) { 1049e82cf6aSEric Van Hensbergen int token; 1059e82cf6aSEric Van Hensbergen if (!*p) 1069e82cf6aSEric Van Hensbergen continue; 1079e82cf6aSEric Van Hensbergen token = match_token(p, tokens, args); 10867543e50SEric Van Hensbergen if (token < Opt_uname) { 1099e82cf6aSEric Van Hensbergen if ((ret = match_int(&args[0], &option)) < 0) { 110bd238fb4SLatchesar Ionkov P9_DPRINTK(P9_DEBUG_ERROR, 1119e82cf6aSEric Van Hensbergen "integer field, but no integer?\n"); 1129e82cf6aSEric Van Hensbergen continue; 1139e82cf6aSEric Van Hensbergen } 1149e82cf6aSEric Van Hensbergen } 1159e82cf6aSEric Van Hensbergen switch (token) { 1169e2f6688SEric Van Hensbergen case Opt_debug: 1179e2f6688SEric Van Hensbergen v9ses->debug = option; 11810fa16e7SEric Van Hensbergen #ifdef CONFIG_NET_9P_DEBUG 1199e2f6688SEric Van Hensbergen p9_debug_level = option; 12010fa16e7SEric Van Hensbergen #endif 1219e2f6688SEric Van Hensbergen break; 1229e82cf6aSEric Van Hensbergen case Opt_msize: 1239e82cf6aSEric Van Hensbergen v9ses->maxdata = option; 1249e82cf6aSEric Van Hensbergen break; 125bd32b82dSLatchesar Ionkov case Opt_dfltuid: 126bd32b82dSLatchesar Ionkov v9ses->dfltuid = option; 1279e82cf6aSEric Van Hensbergen break; 128bd32b82dSLatchesar Ionkov case Opt_dfltgid: 129bd32b82dSLatchesar Ionkov v9ses->dfltgid = option; 1309e82cf6aSEric Van Hensbergen break; 1319e82cf6aSEric Van Hensbergen case Opt_afid: 1329e82cf6aSEric Van Hensbergen v9ses->afid = option; 1339e82cf6aSEric Van Hensbergen break; 134a80d923eSEric Van Hensbergen case Opt_trans: 135a80d923eSEric Van Hensbergen v9ses->trans = v9fs_match_trans(&args[0]); 1369e82cf6aSEric Van Hensbergen break; 13767543e50SEric Van Hensbergen case Opt_uname: 138ba17674fSLatchesar Ionkov match_strcpy(v9ses->uname, &args[0]); 1399e82cf6aSEric Van Hensbergen break; 1409e82cf6aSEric Van Hensbergen case Opt_remotename: 141ba17674fSLatchesar Ionkov match_strcpy(v9ses->aname, &args[0]); 1429e82cf6aSEric Van Hensbergen break; 1439e82cf6aSEric Van Hensbergen case Opt_legacy: 1442405669bSLatchesar Ionkov v9ses->flags &= ~V9FS_EXTENDED; 1459e82cf6aSEric Van Hensbergen break; 1469e82cf6aSEric Van Hensbergen case Opt_nodevmap: 1479e82cf6aSEric Van Hensbergen v9ses->nodev = 1; 1489e82cf6aSEric Van Hensbergen break; 149e03abc0cSEric Van Hensbergen case Opt_cache_loose: 150e03abc0cSEric Van Hensbergen v9ses->cache = CACHE_LOOSE; 151e03abc0cSEric Van Hensbergen break; 152ba17674fSLatchesar Ionkov 153ba17674fSLatchesar Ionkov case Opt_access: 154ba17674fSLatchesar Ionkov s = match_strdup(&args[0]); 155ba17674fSLatchesar Ionkov v9ses->flags &= ~V9FS_ACCESS_MASK; 156ba17674fSLatchesar Ionkov if (strcmp(s, "user") == 0) 157ba17674fSLatchesar Ionkov v9ses->flags |= V9FS_ACCESS_USER; 158ba17674fSLatchesar Ionkov else if (strcmp(s, "any") == 0) 159ba17674fSLatchesar Ionkov v9ses->flags |= V9FS_ACCESS_ANY; 160ba17674fSLatchesar Ionkov else { 161ba17674fSLatchesar Ionkov v9ses->flags |= V9FS_ACCESS_SINGLE; 162ba17674fSLatchesar Ionkov v9ses->uid = simple_strtol(s, &e, 10); 163ba17674fSLatchesar Ionkov if (*e != '\0') 164ba17674fSLatchesar Ionkov v9ses->uid = ~0; 165ba17674fSLatchesar Ionkov } 1660a976297SAdrian Bunk kfree(s); 167ba17674fSLatchesar Ionkov break; 168ba17674fSLatchesar Ionkov 1699e82cf6aSEric Van Hensbergen default: 1709e82cf6aSEric Van Hensbergen continue; 1719e82cf6aSEric Van Hensbergen } 1729e82cf6aSEric Van Hensbergen } 1738999e04fSLatchesar Ionkov kfree(options); 1749e82cf6aSEric Van Hensbergen } 1759e82cf6aSEric Van Hensbergen 1769e82cf6aSEric Van Hensbergen /** 1779e82cf6aSEric Van Hensbergen * v9fs_session_init - initialize session 1789e82cf6aSEric Van Hensbergen * @v9ses: session information structure 1799e82cf6aSEric Van Hensbergen * @dev_name: device being mounted 1809e82cf6aSEric Van Hensbergen * @data: options 1819e82cf6aSEric Van Hensbergen * 1829e82cf6aSEric Van Hensbergen */ 1839e82cf6aSEric Van Hensbergen 184bd238fb4SLatchesar Ionkov struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses, 1859e82cf6aSEric Van Hensbergen const char *dev_name, char *data) 1869e82cf6aSEric Van Hensbergen { 1879e82cf6aSEric Van Hensbergen int retval = -EINVAL; 188a80d923eSEric Van Hensbergen struct p9_trans *trans = NULL; 189bd238fb4SLatchesar Ionkov struct p9_fid *fid; 1909e82cf6aSEric Van Hensbergen 191ba17674fSLatchesar Ionkov v9ses->uname = __getname(); 192ba17674fSLatchesar Ionkov if (!v9ses->uname) 193bd238fb4SLatchesar Ionkov return ERR_PTR(-ENOMEM); 1949e82cf6aSEric Van Hensbergen 195ba17674fSLatchesar Ionkov v9ses->aname = __getname(); 196ba17674fSLatchesar Ionkov if (!v9ses->aname) { 197ba17674fSLatchesar Ionkov __putname(v9ses->uname); 198bd238fb4SLatchesar Ionkov return ERR_PTR(-ENOMEM); 1999e82cf6aSEric Van Hensbergen } 2009e82cf6aSEric Van Hensbergen 201ba17674fSLatchesar Ionkov v9ses->flags = V9FS_EXTENDED | V9FS_ACCESS_USER; 202ba17674fSLatchesar Ionkov strcpy(v9ses->uname, V9FS_DEFUSER); 203ba17674fSLatchesar Ionkov strcpy(v9ses->aname, V9FS_DEFANAME); 204ba17674fSLatchesar Ionkov v9ses->uid = ~0; 205bd32b82dSLatchesar Ionkov v9ses->dfltuid = V9FS_DEFUID; 206bd32b82dSLatchesar Ionkov v9ses->dfltgid = V9FS_DEFGID; 207a80d923eSEric Van Hensbergen v9ses->options = kstrdup(data, GFP_KERNEL); 208a80d923eSEric Van Hensbergen v9fs_parse_options(v9ses); 2099e82cf6aSEric Van Hensbergen 210a80d923eSEric Van Hensbergen if (v9ses->trans == NULL) { 211a80d923eSEric Van Hensbergen retval = -EPROTONOSUPPORT; 212a80d923eSEric Van Hensbergen P9_DPRINTK(P9_DEBUG_ERROR, 213a80d923eSEric Van Hensbergen "No transport defined or default transport\n"); 214bd238fb4SLatchesar Ionkov goto error; 215a80d923eSEric Van Hensbergen } 2169e82cf6aSEric Van Hensbergen 217a80d923eSEric Van Hensbergen trans = v9ses->trans->create(dev_name, v9ses->options); 218bd238fb4SLatchesar Ionkov if (IS_ERR(trans)) { 219bd238fb4SLatchesar Ionkov retval = PTR_ERR(trans); 220bd238fb4SLatchesar Ionkov trans = NULL; 221bd238fb4SLatchesar Ionkov goto error; 222a8e63bffSLatchesar Ionkov } 223a80d923eSEric Van Hensbergen if ((v9ses->maxdata+P9_IOHDRSZ) > v9ses->trans->maxsize) 224a80d923eSEric Van Hensbergen v9ses->maxdata = v9ses->trans->maxsize-P9_IOHDRSZ; 225a8e63bffSLatchesar Ionkov 226bd238fb4SLatchesar Ionkov v9ses->clnt = p9_client_create(trans, v9ses->maxdata+P9_IOHDRSZ, 2272405669bSLatchesar Ionkov v9fs_extended(v9ses)); 2289e82cf6aSEric Van Hensbergen 229bd238fb4SLatchesar Ionkov if (IS_ERR(v9ses->clnt)) { 230bd238fb4SLatchesar Ionkov retval = PTR_ERR(v9ses->clnt); 231bd238fb4SLatchesar Ionkov v9ses->clnt = NULL; 232bd238fb4SLatchesar Ionkov P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n"); 233bd238fb4SLatchesar Ionkov goto error; 2349e82cf6aSEric Van Hensbergen } 2359e82cf6aSEric Van Hensbergen 236ba17674fSLatchesar Ionkov if (!v9ses->clnt->dotu) 237ba17674fSLatchesar Ionkov v9ses->flags &= ~V9FS_EXTENDED; 238ba17674fSLatchesar Ionkov 239ba17674fSLatchesar Ionkov /* for legacy mode, fall back to V9FS_ACCESS_ANY */ 240ba17674fSLatchesar Ionkov if (!v9fs_extended(v9ses) && 241ba17674fSLatchesar Ionkov ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) { 242ba17674fSLatchesar Ionkov 243ba17674fSLatchesar Ionkov v9ses->flags &= ~V9FS_ACCESS_MASK; 244ba17674fSLatchesar Ionkov v9ses->flags |= V9FS_ACCESS_ANY; 245ba17674fSLatchesar Ionkov v9ses->uid = ~0; 246ba17674fSLatchesar Ionkov } 247ba17674fSLatchesar Ionkov 248ba17674fSLatchesar Ionkov fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0, 249ba17674fSLatchesar Ionkov v9ses->aname); 250bd238fb4SLatchesar Ionkov if (IS_ERR(fid)) { 251bd238fb4SLatchesar Ionkov retval = PTR_ERR(fid); 252bd238fb4SLatchesar Ionkov fid = NULL; 253bd238fb4SLatchesar Ionkov P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n"); 254bd238fb4SLatchesar Ionkov goto error; 2559e82cf6aSEric Van Hensbergen } 2569e82cf6aSEric Van Hensbergen 257ba17674fSLatchesar Ionkov if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE) 258ba17674fSLatchesar Ionkov fid->uid = v9ses->uid; 259ba17674fSLatchesar Ionkov else 260ba17674fSLatchesar Ionkov fid->uid = ~0; 261ba17674fSLatchesar Ionkov 262bd238fb4SLatchesar Ionkov return fid; 2639e82cf6aSEric Van Hensbergen 264bd238fb4SLatchesar Ionkov error: 2659e82cf6aSEric Van Hensbergen v9fs_session_close(v9ses); 266bd238fb4SLatchesar Ionkov return ERR_PTR(retval); 2679e82cf6aSEric Van Hensbergen } 2689e82cf6aSEric Van Hensbergen 2699e82cf6aSEric Van Hensbergen /** 2709e82cf6aSEric Van Hensbergen * v9fs_session_close - shutdown a session 2719e82cf6aSEric Van Hensbergen * @v9ses: session information structure 2729e82cf6aSEric Van Hensbergen * 2739e82cf6aSEric Van Hensbergen */ 2749e82cf6aSEric Van Hensbergen 2759e82cf6aSEric Van Hensbergen void v9fs_session_close(struct v9fs_session_info *v9ses) 2769e82cf6aSEric Van Hensbergen { 277bd238fb4SLatchesar Ionkov if (v9ses->clnt) { 278bd238fb4SLatchesar Ionkov p9_client_destroy(v9ses->clnt); 279bd238fb4SLatchesar Ionkov v9ses->clnt = NULL; 2803cf6429aSLatchesar Ionkov } 2819e82cf6aSEric Van Hensbergen 282ba17674fSLatchesar Ionkov __putname(v9ses->uname); 283ba17674fSLatchesar Ionkov __putname(v9ses->aname); 284a80d923eSEric Van Hensbergen kfree(v9ses->options); 2859e82cf6aSEric Van Hensbergen } 2869e82cf6aSEric Van Hensbergen 287322b329aSEric Van Hensbergen /** 288322b329aSEric Van Hensbergen * v9fs_session_cancel - mark transport as disconnected 289322b329aSEric Van Hensbergen * and cancel all pending requests. 290322b329aSEric Van Hensbergen */ 291322b329aSEric Van Hensbergen void v9fs_session_cancel(struct v9fs_session_info *v9ses) { 292bd238fb4SLatchesar Ionkov P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses); 293bd238fb4SLatchesar Ionkov p9_client_disconnect(v9ses->clnt); 294322b329aSEric Van Hensbergen } 295322b329aSEric Van Hensbergen 2969e82cf6aSEric Van Hensbergen extern int v9fs_error_init(void); 2979e82cf6aSEric Van Hensbergen 2989e82cf6aSEric Van Hensbergen /** 2999e82cf6aSEric Van Hensbergen * v9fs_init - Initialize module 3009e82cf6aSEric Van Hensbergen * 3019e82cf6aSEric Van Hensbergen */ 3029e82cf6aSEric Van Hensbergen 3039e82cf6aSEric Van Hensbergen static int __init init_v9fs(void) 3049e82cf6aSEric Van Hensbergen { 305f94b3470SEric Van Hensbergen printk(KERN_INFO "Installing v9fs 9p2000 file system support\n"); 306a80d923eSEric Van Hensbergen /* TODO: Setup list of registered trasnport modules */ 307bd238fb4SLatchesar Ionkov return register_filesystem(&v9fs_fs_type); 3089e82cf6aSEric Van Hensbergen } 3099e82cf6aSEric Van Hensbergen 3109e82cf6aSEric Van Hensbergen /** 3119e82cf6aSEric Van Hensbergen * v9fs_init - shutdown module 3129e82cf6aSEric Van Hensbergen * 3139e82cf6aSEric Van Hensbergen */ 3149e82cf6aSEric Van Hensbergen 3159e82cf6aSEric Van Hensbergen static void __exit exit_v9fs(void) 3169e82cf6aSEric Van Hensbergen { 3179e82cf6aSEric Van Hensbergen unregister_filesystem(&v9fs_fs_type); 3189e82cf6aSEric Van Hensbergen } 3199e82cf6aSEric Van Hensbergen 3209e82cf6aSEric Van Hensbergen module_init(init_v9fs) 3219e82cf6aSEric Van Hensbergen module_exit(exit_v9fs) 3229e82cf6aSEric Van Hensbergen 323bd238fb4SLatchesar Ionkov MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>"); 3249e82cf6aSEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>"); 3259e82cf6aSEric Van Hensbergen MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>"); 3269e82cf6aSEric Van Hensbergen MODULE_LICENSE("GPL"); 327