Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/9p/v9fs.c |
| 3 | * |
| 4 | * This file contains functions assisting in mapping VFS to 9P2000 |
| 5 | * |
| 6 | * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> |
| 7 | * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Eric Van Hensbergen | 42e8c50 | 2006-03-25 03:07:28 -0800 | [diff] [blame] | 10 | * it under the terms of the GNU General Public License version 2 |
| 11 | * as published by the Free Software Foundation. |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to: |
| 20 | * Free Software Foundation |
| 21 | * 51 Franklin Street, Fifth Floor |
| 22 | * Boston, MA 02111-1301 USA |
| 23 | * |
| 24 | */ |
| 25 | |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/fs.h> |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 29 | #include <linux/sched.h> |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 30 | #include <linux/parser.h> |
| 31 | #include <linux/idr.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 32 | #include <net/9p/9p.h> |
| 33 | #include <net/9p/transport.h> |
| 34 | #include <net/9p/conn.h> |
| 35 | #include <net/9p/client.h> |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 36 | #include "v9fs.h" |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 37 | #include "v9fs_vfs.h" |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Option Parsing (code inspired by NFS code) |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | enum { |
| 45 | /* Options that take integer arguments */ |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 46 | Opt_port, Opt_msize, Opt_uid, Opt_gid, Opt_afid, |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 47 | Opt_rfdno, Opt_wfdno, |
| 48 | /* String options */ |
Eric Van Hensbergen | 67543e5 | 2006-03-25 03:07:29 -0800 | [diff] [blame] | 49 | Opt_uname, Opt_remotename, |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 50 | /* Options that take no arguments */ |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 51 | Opt_legacy, Opt_nodevmap, Opt_unix, Opt_tcp, Opt_fd, Opt_pci, |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 52 | /* Cache options */ |
| 53 | Opt_cache_loose, |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 54 | /* Error token */ |
| 55 | Opt_err |
| 56 | }; |
| 57 | |
| 58 | static match_table_t tokens = { |
| 59 | {Opt_port, "port=%u"}, |
| 60 | {Opt_msize, "msize=%u"}, |
| 61 | {Opt_uid, "uid=%u"}, |
| 62 | {Opt_gid, "gid=%u"}, |
| 63 | {Opt_afid, "afid=%u"}, |
| 64 | {Opt_rfdno, "rfdno=%u"}, |
| 65 | {Opt_wfdno, "wfdno=%u"}, |
Eric Van Hensbergen | 67543e5 | 2006-03-25 03:07:29 -0800 | [diff] [blame] | 66 | {Opt_uname, "uname=%s"}, |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 67 | {Opt_remotename, "aname=%s"}, |
| 68 | {Opt_unix, "proto=unix"}, |
| 69 | {Opt_tcp, "proto=tcp"}, |
| 70 | {Opt_fd, "proto=fd"}, |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 71 | #ifdef CONFIG_PCI_9P |
| 72 | {Opt_pci, "proto=pci"}, |
| 73 | #endif |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 74 | {Opt_tcp, "tcp"}, |
| 75 | {Opt_unix, "unix"}, |
| 76 | {Opt_fd, "fd"}, |
| 77 | {Opt_legacy, "noextend"}, |
| 78 | {Opt_nodevmap, "nodevmap"}, |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 79 | {Opt_cache_loose, "cache=loose"}, |
| 80 | {Opt_cache_loose, "loose"}, |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 81 | {Opt_err, NULL} |
| 82 | }; |
| 83 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 84 | extern struct p9_transport *p9pci_trans_create(void); |
| 85 | |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 86 | /* |
| 87 | * Parse option string. |
| 88 | */ |
| 89 | |
| 90 | /** |
| 91 | * v9fs_parse_options - parse mount options into session structure |
| 92 | * @options: options string passed from mount |
| 93 | * @v9ses: existing v9fs session information |
| 94 | * |
| 95 | */ |
| 96 | |
| 97 | static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses) |
| 98 | { |
| 99 | char *p; |
| 100 | substring_t args[MAX_OPT_ARGS]; |
| 101 | int option; |
| 102 | int ret; |
| 103 | |
| 104 | /* setup defaults */ |
| 105 | v9ses->port = V9FS_PORT; |
| 106 | v9ses->maxdata = 9000; |
| 107 | v9ses->proto = PROTO_TCP; |
| 108 | v9ses->extended = 1; |
| 109 | v9ses->afid = ~0; |
| 110 | v9ses->debug = 0; |
| 111 | v9ses->rfdno = ~0; |
| 112 | v9ses->wfdno = ~0; |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 113 | v9ses->cache = 0; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 114 | |
| 115 | if (!options) |
| 116 | return; |
| 117 | |
| 118 | while ((p = strsep(&options, ",")) != NULL) { |
| 119 | int token; |
| 120 | if (!*p) |
| 121 | continue; |
| 122 | token = match_token(p, tokens, args); |
Eric Van Hensbergen | 67543e5 | 2006-03-25 03:07:29 -0800 | [diff] [blame] | 123 | if (token < Opt_uname) { |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 124 | if ((ret = match_int(&args[0], &option)) < 0) { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 125 | P9_DPRINTK(P9_DEBUG_ERROR, |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 126 | "integer field, but no integer?\n"); |
| 127 | continue; |
| 128 | } |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 129 | } |
| 130 | switch (token) { |
| 131 | case Opt_port: |
| 132 | v9ses->port = option; |
| 133 | break; |
| 134 | case Opt_msize: |
| 135 | v9ses->maxdata = option; |
| 136 | break; |
| 137 | case Opt_uid: |
| 138 | v9ses->uid = option; |
| 139 | break; |
| 140 | case Opt_gid: |
| 141 | v9ses->gid = option; |
| 142 | break; |
| 143 | case Opt_afid: |
| 144 | v9ses->afid = option; |
| 145 | break; |
| 146 | case Opt_rfdno: |
| 147 | v9ses->rfdno = option; |
| 148 | break; |
| 149 | case Opt_wfdno: |
| 150 | v9ses->wfdno = option; |
| 151 | break; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 152 | case Opt_tcp: |
| 153 | v9ses->proto = PROTO_TCP; |
| 154 | break; |
| 155 | case Opt_unix: |
| 156 | v9ses->proto = PROTO_UNIX; |
| 157 | break; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 158 | case Opt_pci: |
| 159 | v9ses->proto = PROTO_PCI; |
| 160 | break; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 161 | case Opt_fd: |
| 162 | v9ses->proto = PROTO_FD; |
| 163 | break; |
Eric Van Hensbergen | 67543e5 | 2006-03-25 03:07:29 -0800 | [diff] [blame] | 164 | case Opt_uname: |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 165 | match_strcpy(v9ses->name, &args[0]); |
| 166 | break; |
| 167 | case Opt_remotename: |
| 168 | match_strcpy(v9ses->remotename, &args[0]); |
| 169 | break; |
| 170 | case Opt_legacy: |
| 171 | v9ses->extended = 0; |
| 172 | break; |
| 173 | case Opt_nodevmap: |
| 174 | v9ses->nodev = 1; |
| 175 | break; |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 176 | case Opt_cache_loose: |
| 177 | v9ses->cache = CACHE_LOOSE; |
| 178 | break; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 179 | default: |
| 180 | continue; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 186 | * v9fs_session_init - initialize session |
| 187 | * @v9ses: session information structure |
| 188 | * @dev_name: device being mounted |
| 189 | * @data: options |
| 190 | * |
| 191 | */ |
| 192 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 193 | struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses, |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 194 | const char *dev_name, char *data) |
| 195 | { |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 196 | int retval = -EINVAL; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 197 | struct p9_transport *trans; |
| 198 | struct p9_fid *fid; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 199 | |
| 200 | v9ses->name = __getname(); |
| 201 | if (!v9ses->name) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 202 | return ERR_PTR(-ENOMEM); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 203 | |
| 204 | v9ses->remotename = __getname(); |
| 205 | if (!v9ses->remotename) { |
Davi Arnaut | ce44eeb | 2005-11-07 00:59:36 -0800 | [diff] [blame] | 206 | __putname(v9ses->name); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 207 | return ERR_PTR(-ENOMEM); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | strcpy(v9ses->name, V9FS_DEFUSER); |
| 211 | strcpy(v9ses->remotename, V9FS_DEFANAME); |
| 212 | |
| 213 | v9fs_parse_options(data, v9ses); |
| 214 | |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 215 | switch (v9ses->proto) { |
| 216 | case PROTO_TCP: |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 217 | trans = p9_trans_create_tcp(dev_name, v9ses->port); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 218 | break; |
| 219 | case PROTO_UNIX: |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 220 | trans = p9_trans_create_unix(dev_name); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 221 | *v9ses->remotename = 0; |
| 222 | break; |
| 223 | case PROTO_FD: |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 224 | trans = p9_trans_create_fd(v9ses->rfdno, v9ses->wfdno); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 225 | *v9ses->remotename = 0; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 226 | break; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 227 | #ifdef CONFIG_PCI_9P |
| 228 | case PROTO_PCI: |
| 229 | trans = p9pci_trans_create(); |
| 230 | *v9ses->remotename = 0; |
| 231 | break; |
| 232 | #endif |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 233 | default: |
| 234 | printk(KERN_ERR "v9fs: Bad mount protocol %d\n", v9ses->proto); |
| 235 | retval = -ENOPROTOOPT; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 236 | goto error; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 239 | if (IS_ERR(trans)) { |
| 240 | retval = PTR_ERR(trans); |
| 241 | trans = NULL; |
| 242 | goto error; |
Latchesar Ionkov | a8e63bf | 2005-09-22 21:43:51 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 245 | v9ses->clnt = p9_client_create(trans, v9ses->maxdata + P9_IOHDRSZ, |
| 246 | v9ses->extended); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 247 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 248 | if (IS_ERR(v9ses->clnt)) { |
| 249 | retval = PTR_ERR(v9ses->clnt); |
| 250 | v9ses->clnt = NULL; |
| 251 | P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n"); |
| 252 | goto error; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 255 | fid = p9_client_attach(v9ses->clnt, NULL, v9ses->name, |
| 256 | v9ses->remotename); |
| 257 | if (IS_ERR(fid)) { |
| 258 | retval = PTR_ERR(fid); |
| 259 | fid = NULL; |
| 260 | P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n"); |
| 261 | goto error; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 264 | return fid; |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 265 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 266 | error: |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 267 | v9fs_session_close(v9ses); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 268 | return ERR_PTR(retval); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * v9fs_session_close - shutdown a session |
| 273 | * @v9ses: session information structure |
| 274 | * |
| 275 | */ |
| 276 | |
| 277 | void v9fs_session_close(struct v9fs_session_info *v9ses) |
| 278 | { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 279 | if (v9ses->clnt) { |
| 280 | p9_client_destroy(v9ses->clnt); |
| 281 | v9ses->clnt = NULL; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 282 | } |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 283 | |
Davi Arnaut | ce44eeb | 2005-11-07 00:59:36 -0800 | [diff] [blame] | 284 | __putname(v9ses->name); |
| 285 | __putname(v9ses->remotename); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Eric Van Hensbergen | 322b329 | 2005-09-09 13:04:23 -0700 | [diff] [blame] | 288 | /** |
| 289 | * v9fs_session_cancel - mark transport as disconnected |
| 290 | * and cancel all pending requests. |
| 291 | */ |
| 292 | void v9fs_session_cancel(struct v9fs_session_info *v9ses) { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 293 | P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses); |
| 294 | p9_client_disconnect(v9ses->clnt); |
Eric Van Hensbergen | 322b329 | 2005-09-09 13:04:23 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 297 | extern int v9fs_error_init(void); |
| 298 | |
| 299 | /** |
| 300 | * v9fs_init - Initialize module |
| 301 | * |
| 302 | */ |
| 303 | |
| 304 | static int __init init_v9fs(void) |
| 305 | { |
Eric Van Hensbergen | f94b347 | 2007-01-26 00:57:04 -0800 | [diff] [blame] | 306 | printk(KERN_INFO "Installing v9fs 9p2000 file system support\n"); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 307 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 308 | return register_filesystem(&v9fs_fs_type); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
| 312 | * v9fs_init - shutdown module |
| 313 | * |
| 314 | */ |
| 315 | |
| 316 | static void __exit exit_v9fs(void) |
| 317 | { |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 318 | unregister_filesystem(&v9fs_fs_type); |
| 319 | } |
| 320 | |
| 321 | module_init(init_v9fs) |
| 322 | module_exit(exit_v9fs) |
| 323 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame^] | 324 | MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>"); |
Eric Van Hensbergen | 9e82cf6 | 2005-09-09 13:04:20 -0700 | [diff] [blame] | 325 | MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>"); |
| 326 | MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>"); |
| 327 | MODULE_LICENSE("GPL"); |