blob: fbb12dadba8389a77f7cfece24fc452d86732640 [file] [log] [blame]
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -07001/*
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 Hensbergen42e8c502006-03-25 03:07:28 -080010 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070012 *
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 Hensbergen9e82cf62005-09-09 13:04:20 -070026#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
Al Viro914e2632006-10-18 13:55:46 -040029#include <linux/sched.h>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070030#include <linux/parser.h>
31#include <linux/idr.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050032#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 Hensbergen9e82cf62005-09-09 13:04:20 -070036#include "v9fs.h"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070037#include "v9fs_vfs.h"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070038
39/*
40 * Option Parsing (code inspired by NFS code)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050041 * NOTE: each transport will parse its own options
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070042 */
43
44enum {
45 /* Options that take integer arguments */
Latchesar Ionkovbd32b822007-10-17 14:31:07 -050046 Opt_debug, Opt_msize, Opt_dfltuid, Opt_dfltgid, Opt_afid,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070047 /* String options */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050048 Opt_uname, Opt_remotename, Opt_trans,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070049 /* Options that take no arguments */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050050 Opt_legacy, Opt_nodevmap,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060051 /* Cache options */
52 Opt_cache_loose,
Latchesar Ionkovba176742007-10-17 14:31:07 -050053 /* Access options */
54 Opt_access,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070055 /* Error token */
56 Opt_err
57};
58
59static match_table_t tokens = {
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -050060 {Opt_debug, "debug=%x"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070061 {Opt_msize, "msize=%u"},
Latchesar Ionkovbd32b822007-10-17 14:31:07 -050062 {Opt_dfltuid, "dfltuid=%u"},
63 {Opt_dfltgid, "dfltgid=%u"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070064 {Opt_afid, "afid=%u"},
Eric Van Hensbergen67543e52006-03-25 03:07:29 -080065 {Opt_uname, "uname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070066 {Opt_remotename, "aname=%s"},
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050067 {Opt_trans, "trans=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070068 {Opt_legacy, "noextend"},
69 {Opt_nodevmap, "nodevmap"},
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060070 {Opt_cache_loose, "cache=loose"},
71 {Opt_cache_loose, "loose"},
Latchesar Ionkovba176742007-10-17 14:31:07 -050072 {Opt_access, "access=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070073 {Opt_err, NULL}
74};
75
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070076/**
77 * v9fs_parse_options - parse mount options into session structure
78 * @options: options string passed from mount
79 * @v9ses: existing v9fs session information
80 *
81 */
82
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050083static void v9fs_parse_options(struct v9fs_session_info *v9ses)
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070084{
Latchesar Ionkov8999e042007-11-06 08:02:53 -060085 char *options;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070086 substring_t args[MAX_OPT_ARGS];
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050087 char *p;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070088 int option;
89 int ret;
Latchesar Ionkovba176742007-10-17 14:31:07 -050090 char *s, *e;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070091
92 /* setup defaults */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050093 v9ses->maxdata = 8192;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070094 v9ses->afid = ~0;
95 v9ses->debug = 0;
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060096 v9ses->cache = 0;
Eric Van Hensbergenfb0466c2007-10-17 14:31:07 -050097 v9ses->trans = v9fs_default_trans();
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070098
Latchesar Ionkov8999e042007-11-06 08:02:53 -060099 if (!v9ses->options)
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700100 return;
101
Latchesar Ionkov8999e042007-11-06 08:02:53 -0600102 options = kstrdup(v9ses->options, GFP_KERNEL);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700103 while ((p = strsep(&options, ",")) != NULL) {
104 int token;
105 if (!*p)
106 continue;
107 token = match_token(p, tokens, args);
Eric Van Hensbergen67543e52006-03-25 03:07:29 -0800108 if (token < Opt_uname) {
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700109 if ((ret = match_int(&args[0], &option)) < 0) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500110 P9_DPRINTK(P9_DEBUG_ERROR,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700111 "integer field, but no integer?\n");
112 continue;
113 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700114 }
115 switch (token) {
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500116 case Opt_debug:
117 v9ses->debug = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500118#ifdef CONFIG_NET_9P_DEBUG
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500119 p9_debug_level = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500120#endif
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500121 break;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700122 case Opt_msize:
123 v9ses->maxdata = option;
124 break;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500125 case Opt_dfltuid:
126 v9ses->dfltuid = option;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700127 break;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500128 case Opt_dfltgid:
129 v9ses->dfltgid = option;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700130 break;
131 case Opt_afid:
132 v9ses->afid = option;
133 break;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500134 case Opt_trans:
135 v9ses->trans = v9fs_match_trans(&args[0]);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700136 break;
Eric Van Hensbergen67543e52006-03-25 03:07:29 -0800137 case Opt_uname:
Latchesar Ionkovba176742007-10-17 14:31:07 -0500138 match_strcpy(v9ses->uname, &args[0]);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700139 break;
140 case Opt_remotename:
Latchesar Ionkovba176742007-10-17 14:31:07 -0500141 match_strcpy(v9ses->aname, &args[0]);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700142 break;
143 case Opt_legacy:
Latchesar Ionkov24056692007-10-17 14:31:07 -0500144 v9ses->flags &= ~V9FS_EXTENDED;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700145 break;
146 case Opt_nodevmap:
147 v9ses->nodev = 1;
148 break;
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600149 case Opt_cache_loose:
150 v9ses->cache = CACHE_LOOSE;
151 break;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500152
153 case Opt_access:
154 s = match_strdup(&args[0]);
155 v9ses->flags &= ~V9FS_ACCESS_MASK;
156 if (strcmp(s, "user") == 0)
157 v9ses->flags |= V9FS_ACCESS_USER;
158 else if (strcmp(s, "any") == 0)
159 v9ses->flags |= V9FS_ACCESS_ANY;
160 else {
161 v9ses->flags |= V9FS_ACCESS_SINGLE;
162 v9ses->uid = simple_strtol(s, &e, 10);
163 if (*e != '\0')
164 v9ses->uid = ~0;
165 }
Adrian Bunk0a976292007-10-23 13:48:50 -0500166 kfree(s);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500167 break;
168
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700169 default:
170 continue;
171 }
172 }
Latchesar Ionkov8999e042007-11-06 08:02:53 -0600173 kfree(options);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700174}
175
176/**
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700177 * v9fs_session_init - initialize session
178 * @v9ses: session information structure
179 * @dev_name: device being mounted
180 * @data: options
181 *
182 */
183
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500184struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700185 const char *dev_name, char *data)
186{
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700187 int retval = -EINVAL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500188 struct p9_trans *trans = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500189 struct p9_fid *fid;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700190
Latchesar Ionkovba176742007-10-17 14:31:07 -0500191 v9ses->uname = __getname();
192 if (!v9ses->uname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500193 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700194
Latchesar Ionkovba176742007-10-17 14:31:07 -0500195 v9ses->aname = __getname();
196 if (!v9ses->aname) {
197 __putname(v9ses->uname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500198 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700199 }
200
Latchesar Ionkovba176742007-10-17 14:31:07 -0500201 v9ses->flags = V9FS_EXTENDED | V9FS_ACCESS_USER;
202 strcpy(v9ses->uname, V9FS_DEFUSER);
203 strcpy(v9ses->aname, V9FS_DEFANAME);
204 v9ses->uid = ~0;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500205 v9ses->dfltuid = V9FS_DEFUID;
206 v9ses->dfltgid = V9FS_DEFGID;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500207 v9ses->options = kstrdup(data, GFP_KERNEL);
208 v9fs_parse_options(v9ses);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700209
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500210 if (v9ses->trans == NULL) {
211 retval = -EPROTONOSUPPORT;
212 P9_DPRINTK(P9_DEBUG_ERROR,
213 "No transport defined or default transport\n");
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500214 goto error;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500215 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700216
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500217 trans = v9ses->trans->create(dev_name, v9ses->options);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500218 if (IS_ERR(trans)) {
219 retval = PTR_ERR(trans);
220 trans = NULL;
221 goto error;
Latchesar Ionkova8e63bf2005-09-22 21:43:51 -0700222 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500223 if ((v9ses->maxdata+P9_IOHDRSZ) > v9ses->trans->maxsize)
224 v9ses->maxdata = v9ses->trans->maxsize-P9_IOHDRSZ;
Latchesar Ionkova8e63bf2005-09-22 21:43:51 -0700225
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500226 v9ses->clnt = p9_client_create(trans, v9ses->maxdata+P9_IOHDRSZ,
Latchesar Ionkov24056692007-10-17 14:31:07 -0500227 v9fs_extended(v9ses));
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700228
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500229 if (IS_ERR(v9ses->clnt)) {
230 retval = PTR_ERR(v9ses->clnt);
231 v9ses->clnt = NULL;
232 P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
233 goto error;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700234 }
235
Latchesar Ionkovba176742007-10-17 14:31:07 -0500236 if (!v9ses->clnt->dotu)
237 v9ses->flags &= ~V9FS_EXTENDED;
238
239 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
240 if (!v9fs_extended(v9ses) &&
241 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
242
243 v9ses->flags &= ~V9FS_ACCESS_MASK;
244 v9ses->flags |= V9FS_ACCESS_ANY;
245 v9ses->uid = ~0;
246 }
247
248 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0,
249 v9ses->aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500250 if (IS_ERR(fid)) {
251 retval = PTR_ERR(fid);
252 fid = NULL;
253 P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
254 goto error;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700255 }
256
Latchesar Ionkovba176742007-10-17 14:31:07 -0500257 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
258 fid->uid = v9ses->uid;
259 else
260 fid->uid = ~0;
261
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500262 return fid;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700263
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500264error:
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700265 v9fs_session_close(v9ses);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500266 return ERR_PTR(retval);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700267}
268
269/**
270 * v9fs_session_close - shutdown a session
271 * @v9ses: session information structure
272 *
273 */
274
275void v9fs_session_close(struct v9fs_session_info *v9ses)
276{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500277 if (v9ses->clnt) {
278 p9_client_destroy(v9ses->clnt);
279 v9ses->clnt = NULL;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800280 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700281
Latchesar Ionkovba176742007-10-17 14:31:07 -0500282 __putname(v9ses->uname);
283 __putname(v9ses->aname);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500284 kfree(v9ses->options);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700285}
286
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700287/**
288 * v9fs_session_cancel - mark transport as disconnected
289 * and cancel all pending requests.
290 */
291void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500292 P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
293 p9_client_disconnect(v9ses->clnt);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700294}
295
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700296extern int v9fs_error_init(void);
297
298/**
299 * v9fs_init - Initialize module
300 *
301 */
302
303static int __init init_v9fs(void)
304{
Eric Van Hensbergenf94b3472007-01-26 00:57:04 -0800305 printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500306 /* TODO: Setup list of registered trasnport modules */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500307 return register_filesystem(&v9fs_fs_type);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700308}
309
310/**
311 * v9fs_init - shutdown module
312 *
313 */
314
315static void __exit exit_v9fs(void)
316{
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700317 unregister_filesystem(&v9fs_fs_type);
318}
319
320module_init(init_v9fs)
321module_exit(exit_v9fs)
322
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500323MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700324MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
325MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
326MODULE_LICENSE("GPL");