blob: 9b0f0222e8bbed0947f19f0baebdef0f52aabe56 [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 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06006 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -07007 * 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>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050034#include <net/9p/client.h>
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070035#include "v9fs.h"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070036#include "v9fs_vfs.h"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070037
38/*
39 * Option Parsing (code inspired by NFS code)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050040 * NOTE: each transport will parse its own options
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070041 */
42
43enum {
44 /* Options that take integer arguments */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060045 Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070046 /* String options */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050047 Opt_uname, Opt_remotename, Opt_trans,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070048 /* Options that take no arguments */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060049 Opt_nodevmap,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060050 /* Cache options */
51 Opt_cache_loose,
Latchesar Ionkovba176742007-10-17 14:31:07 -050052 /* Access options */
53 Opt_access,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070054 /* Error token */
55 Opt_err
56};
57
58static match_table_t tokens = {
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -050059 {Opt_debug, "debug=%x"},
Latchesar Ionkovbd32b822007-10-17 14:31:07 -050060 {Opt_dfltuid, "dfltuid=%u"},
61 {Opt_dfltgid, "dfltgid=%u"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070062 {Opt_afid, "afid=%u"},
Eric Van Hensbergen67543e52006-03-25 03:07:29 -080063 {Opt_uname, "uname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070064 {Opt_remotename, "aname=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070065 {Opt_nodevmap, "nodevmap"},
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060066 {Opt_cache_loose, "cache=loose"},
67 {Opt_cache_loose, "loose"},
Latchesar Ionkovba176742007-10-17 14:31:07 -050068 {Opt_access, "access=%s"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070069 {Opt_err, NULL}
70};
71
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070072/**
73 * v9fs_parse_options - parse mount options into session structure
74 * @options: options string passed from mount
75 * @v9ses: existing v9fs session information
76 *
77 */
78
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050079static void v9fs_parse_options(struct v9fs_session_info *v9ses)
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070080{
Latchesar Ionkov8999e042007-11-06 08:02:53 -060081 char *options;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070082 substring_t args[MAX_OPT_ARGS];
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050083 char *p;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060084 int option = 0;
Latchesar Ionkovba176742007-10-17 14:31:07 -050085 char *s, *e;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060086 int ret;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070087
88 /* setup defaults */
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070089 v9ses->afid = ~0;
90 v9ses->debug = 0;
Eric Van Hensbergene03abc02007-02-11 13:21:39 -060091 v9ses->cache = 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070092
Latchesar Ionkov8999e042007-11-06 08:02:53 -060093 if (!v9ses->options)
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070094 return;
95
Latchesar Ionkov8999e042007-11-06 08:02:53 -060096 options = kstrdup(v9ses->options, GFP_KERNEL);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070097 while ((p = strsep(&options, ",")) != NULL) {
98 int token;
99 if (!*p)
100 continue;
101 token = match_token(p, tokens, args);
Eric Van Hensbergen67543e52006-03-25 03:07:29 -0800102 if (token < Opt_uname) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600103 ret = match_int(&args[0], &option);
104 if (ret < 0) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500105 P9_DPRINTK(P9_DEBUG_ERROR,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700106 "integer field, but no integer?\n");
107 continue;
108 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700109 }
110 switch (token) {
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500111 case Opt_debug:
112 v9ses->debug = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500113#ifdef CONFIG_NET_9P_DEBUG
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500114 p9_debug_level = option;
Eric Van Hensbergen10fa16e2007-07-16 16:02:49 -0500115#endif
Eric Van Hensbergen9e2f6682007-07-13 13:05:21 -0500116 break;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600117
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500118 case Opt_dfltuid:
119 v9ses->dfltuid = option;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700120 break;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500121 case Opt_dfltgid:
122 v9ses->dfltgid = option;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700123 break;
124 case Opt_afid:
125 v9ses->afid = option;
126 break;
Eric Van Hensbergen67543e52006-03-25 03:07:29 -0800127 case Opt_uname:
Latchesar Ionkovba176742007-10-17 14:31:07 -0500128 match_strcpy(v9ses->uname, &args[0]);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700129 break;
130 case Opt_remotename:
Latchesar Ionkovba176742007-10-17 14:31:07 -0500131 match_strcpy(v9ses->aname, &args[0]);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700132 break;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700133 case Opt_nodevmap:
134 v9ses->nodev = 1;
135 break;
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600136 case Opt_cache_loose:
137 v9ses->cache = CACHE_LOOSE;
138 break;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500139
140 case Opt_access:
141 s = match_strdup(&args[0]);
142 v9ses->flags &= ~V9FS_ACCESS_MASK;
143 if (strcmp(s, "user") == 0)
144 v9ses->flags |= V9FS_ACCESS_USER;
145 else if (strcmp(s, "any") == 0)
146 v9ses->flags |= V9FS_ACCESS_ANY;
147 else {
148 v9ses->flags |= V9FS_ACCESS_SINGLE;
149 v9ses->uid = simple_strtol(s, &e, 10);
150 if (*e != '\0')
151 v9ses->uid = ~0;
152 }
Adrian Bunk0a976292007-10-23 13:48:50 -0500153 kfree(s);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500154 break;
155
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700156 default:
157 continue;
158 }
159 }
Latchesar Ionkov8999e042007-11-06 08:02:53 -0600160 kfree(options);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700161}
162
163/**
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700164 * v9fs_session_init - initialize session
165 * @v9ses: session information structure
166 * @dev_name: device being mounted
167 * @data: options
168 *
169 */
170
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500171struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700172 const char *dev_name, char *data)
173{
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700174 int retval = -EINVAL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500175 struct p9_fid *fid;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700176
Latchesar Ionkovba176742007-10-17 14:31:07 -0500177 v9ses->uname = __getname();
178 if (!v9ses->uname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500179 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700180
Latchesar Ionkovba176742007-10-17 14:31:07 -0500181 v9ses->aname = __getname();
182 if (!v9ses->aname) {
183 __putname(v9ses->uname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500184 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700185 }
186
Latchesar Ionkovba176742007-10-17 14:31:07 -0500187 v9ses->flags = V9FS_EXTENDED | V9FS_ACCESS_USER;
188 strcpy(v9ses->uname, V9FS_DEFUSER);
189 strcpy(v9ses->aname, V9FS_DEFANAME);
190 v9ses->uid = ~0;
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500191 v9ses->dfltuid = V9FS_DEFUID;
192 v9ses->dfltgid = V9FS_DEFGID;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500193 v9ses->options = kstrdup(data, GFP_KERNEL);
194 v9fs_parse_options(v9ses);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700195
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600196 v9ses->clnt = p9_client_create(dev_name, v9ses->options);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700197
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500198 if (IS_ERR(v9ses->clnt)) {
199 retval = PTR_ERR(v9ses->clnt);
200 v9ses->clnt = NULL;
201 P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
202 goto error;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700203 }
204
Latchesar Ionkovba176742007-10-17 14:31:07 -0500205 if (!v9ses->clnt->dotu)
206 v9ses->flags &= ~V9FS_EXTENDED;
207
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600208 v9ses->maxdata = v9ses->clnt->msize;
209
Latchesar Ionkovba176742007-10-17 14:31:07 -0500210 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
211 if (!v9fs_extended(v9ses) &&
212 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
213
214 v9ses->flags &= ~V9FS_ACCESS_MASK;
215 v9ses->flags |= V9FS_ACCESS_ANY;
216 v9ses->uid = ~0;
217 }
218
219 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0,
220 v9ses->aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500221 if (IS_ERR(fid)) {
222 retval = PTR_ERR(fid);
223 fid = NULL;
224 P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
225 goto error;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700226 }
227
Latchesar Ionkovba176742007-10-17 14:31:07 -0500228 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
229 fid->uid = v9ses->uid;
230 else
231 fid->uid = ~0;
232
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500233 return fid;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700234
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500235error:
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700236 v9fs_session_close(v9ses);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500237 return ERR_PTR(retval);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700238}
239
240/**
241 * v9fs_session_close - shutdown a session
242 * @v9ses: session information structure
243 *
244 */
245
246void v9fs_session_close(struct v9fs_session_info *v9ses)
247{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500248 if (v9ses->clnt) {
249 p9_client_destroy(v9ses->clnt);
250 v9ses->clnt = NULL;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800251 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700252
Latchesar Ionkovba176742007-10-17 14:31:07 -0500253 __putname(v9ses->uname);
254 __putname(v9ses->aname);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500255 kfree(v9ses->options);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700256}
257
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700258/**
259 * v9fs_session_cancel - mark transport as disconnected
260 * and cancel all pending requests.
261 */
262void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500263 P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
264 p9_client_disconnect(v9ses->clnt);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700265}
266
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700267extern int v9fs_error_init(void);
268
269/**
270 * v9fs_init - Initialize module
271 *
272 */
273
274static int __init init_v9fs(void)
275{
Eric Van Hensbergenf94b3472007-01-26 00:57:04 -0800276 printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500277 /* TODO: Setup list of registered trasnport modules */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500278 return register_filesystem(&v9fs_fs_type);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700279}
280
281/**
282 * v9fs_init - shutdown module
283 *
284 */
285
286static void __exit exit_v9fs(void)
287{
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700288 unregister_filesystem(&v9fs_fs_type);
289}
290
291module_init(init_v9fs)
292module_exit(exit_v9fs)
293
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500294MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700295MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
296MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
297MODULE_LICENSE("GPL");