]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/9p/v9fs.c
9p: attach-per-user
[net-next-2.6.git] / fs / 9p / v9fs.c
CommitLineData
9e82cf6a
EVH
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
42e8c509
EVH
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
9e82cf6a
EVH
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
9e82cf6a
EVH
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
914e2637 29#include <linux/sched.h>
9e82cf6a
EVH
30#include <linux/parser.h>
31#include <linux/idr.h>
bd238fb4
LI
32#include <net/9p/9p.h>
33#include <net/9p/transport.h>
34#include <net/9p/conn.h>
35#include <net/9p/client.h>
9e82cf6a 36#include "v9fs.h"
9e82cf6a 37#include "v9fs_vfs.h"
9e82cf6a 38
a80d923e
EVH
39/*
40 * Dynamic Transport Registration Routines
41 *
42 */
43
44static LIST_HEAD(v9fs_trans_list);
45static struct p9_trans_module *v9fs_default_trans;
46
47/**
48 * v9fs_register_trans - register a new transport with 9p
49 * @m - structure describing the transport module and entry points
50 *
51 */
52void v9fs_register_trans(struct p9_trans_module *m)
53{
54 list_add_tail(&m->list, &v9fs_trans_list);
55 if (m->def)
56 v9fs_default_trans = m;
57}
58EXPORT_SYMBOL(v9fs_register_trans);
59
60/**
61 * v9fs_match_trans - match transport versus registered transports
62 * @arg: string identifying transport
63 *
64 */
65static struct p9_trans_module *v9fs_match_trans(const substring_t *name)
66{
67 struct list_head *p;
68 struct p9_trans_module *t = NULL;
69
70 list_for_each(p, &v9fs_trans_list) {
71 t = list_entry(p, struct p9_trans_module, list);
72 if (strncmp(t->name, name->from, name->to-name->from) == 0) {
73 P9_DPRINTK(P9_DEBUG_TRANS, "trans=%s\n", t->name);
74 break;
75 }
76 }
77 return t;
78}
79
9e82cf6a
EVH
80/*
81 * Option Parsing (code inspired by NFS code)
a80d923e 82 * NOTE: each transport will parse its own options
9e82cf6a
EVH
83 */
84
85enum {
86 /* Options that take integer arguments */
bd32b82d 87 Opt_debug, Opt_msize, Opt_dfltuid, Opt_dfltgid, Opt_afid,
9e82cf6a 88 /* String options */
a80d923e 89 Opt_uname, Opt_remotename, Opt_trans,
9e82cf6a 90 /* Options that take no arguments */
a80d923e 91 Opt_legacy, Opt_nodevmap,
e03abc0c
EVH
92 /* Cache options */
93 Opt_cache_loose,
ba17674f
LI
94 /* Access options */
95 Opt_access,
9e82cf6a
EVH
96 /* Error token */
97 Opt_err
98};
99
100static match_table_t tokens = {
9e2f6688 101 {Opt_debug, "debug=%x"},
9e82cf6a 102 {Opt_msize, "msize=%u"},
bd32b82d
LI
103 {Opt_dfltuid, "dfltuid=%u"},
104 {Opt_dfltgid, "dfltgid=%u"},
9e82cf6a 105 {Opt_afid, "afid=%u"},
67543e50 106 {Opt_uname, "uname=%s"},
9e82cf6a 107 {Opt_remotename, "aname=%s"},
a80d923e 108 {Opt_trans, "trans=%s"},
9e82cf6a
EVH
109 {Opt_legacy, "noextend"},
110 {Opt_nodevmap, "nodevmap"},
e03abc0c
EVH
111 {Opt_cache_loose, "cache=loose"},
112 {Opt_cache_loose, "loose"},
ba17674f 113 {Opt_access, "access=%s"},
9e82cf6a
EVH
114 {Opt_err, NULL}
115};
116
9e82cf6a
EVH
117/**
118 * v9fs_parse_options - parse mount options into session structure
119 * @options: options string passed from mount
120 * @v9ses: existing v9fs session information
121 *
122 */
123
a80d923e 124static void v9fs_parse_options(struct v9fs_session_info *v9ses)
9e82cf6a 125{
a80d923e 126 char *options = v9ses->options;
9e82cf6a 127 substring_t args[MAX_OPT_ARGS];
a80d923e 128 char *p;
9e82cf6a
EVH
129 int option;
130 int ret;
ba17674f 131 char *s, *e;
9e82cf6a
EVH
132
133 /* setup defaults */
a80d923e 134 v9ses->maxdata = 8192;
9e82cf6a
EVH
135 v9ses->afid = ~0;
136 v9ses->debug = 0;
e03abc0c 137 v9ses->cache = 0;
a80d923e 138 v9ses->trans = v9fs_default_trans;
9e82cf6a
EVH
139
140 if (!options)
141 return;
142
143 while ((p = strsep(&options, ",")) != NULL) {
144 int token;
145 if (!*p)
146 continue;
147 token = match_token(p, tokens, args);
67543e50 148 if (token < Opt_uname) {
9e82cf6a 149 if ((ret = match_int(&args[0], &option)) < 0) {
bd238fb4 150 P9_DPRINTK(P9_DEBUG_ERROR,
9e82cf6a
EVH
151 "integer field, but no integer?\n");
152 continue;
153 }
9e82cf6a
EVH
154 }
155 switch (token) {
9e2f6688
EVH
156 case Opt_debug:
157 v9ses->debug = option;
10fa16e7 158#ifdef CONFIG_NET_9P_DEBUG
9e2f6688 159 p9_debug_level = option;
10fa16e7 160#endif
9e2f6688 161 break;
9e82cf6a
EVH
162 case Opt_msize:
163 v9ses->maxdata = option;
164 break;
bd32b82d
LI
165 case Opt_dfltuid:
166 v9ses->dfltuid = option;
9e82cf6a 167 break;
bd32b82d
LI
168 case Opt_dfltgid:
169 v9ses->dfltgid = option;
9e82cf6a
EVH
170 break;
171 case Opt_afid:
172 v9ses->afid = option;
173 break;
a80d923e
EVH
174 case Opt_trans:
175 v9ses->trans = v9fs_match_trans(&args[0]);
9e82cf6a 176 break;
67543e50 177 case Opt_uname:
ba17674f 178 match_strcpy(v9ses->uname, &args[0]);
9e82cf6a
EVH
179 break;
180 case Opt_remotename:
ba17674f 181 match_strcpy(v9ses->aname, &args[0]);
9e82cf6a
EVH
182 break;
183 case Opt_legacy:
2405669b 184 v9ses->flags &= ~V9FS_EXTENDED;
9e82cf6a
EVH
185 break;
186 case Opt_nodevmap:
187 v9ses->nodev = 1;
188 break;
e03abc0c
EVH
189 case Opt_cache_loose:
190 v9ses->cache = CACHE_LOOSE;
191 break;
ba17674f
LI
192
193 case Opt_access:
194 s = match_strdup(&args[0]);
195 v9ses->flags &= ~V9FS_ACCESS_MASK;
196 if (strcmp(s, "user") == 0)
197 v9ses->flags |= V9FS_ACCESS_USER;
198 else if (strcmp(s, "any") == 0)
199 v9ses->flags |= V9FS_ACCESS_ANY;
200 else {
201 v9ses->flags |= V9FS_ACCESS_SINGLE;
202 v9ses->uid = simple_strtol(s, &e, 10);
203 if (*e != '\0')
204 v9ses->uid = ~0;
205 }
206 break;
207
9e82cf6a
EVH
208 default:
209 continue;
210 }
211 }
212}
213
9e82cf6a
EVH
214/**
215 * v9fs_session_init - initialize session
216 * @v9ses: session information structure
217 * @dev_name: device being mounted
218 * @data: options
219 *
220 */
221
bd238fb4 222struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
9e82cf6a
EVH
223 const char *dev_name, char *data)
224{
9e82cf6a 225 int retval = -EINVAL;
a80d923e 226 struct p9_trans *trans = NULL;
bd238fb4 227 struct p9_fid *fid;
9e82cf6a 228
ba17674f
LI
229 v9ses->uname = __getname();
230 if (!v9ses->uname)
bd238fb4 231 return ERR_PTR(-ENOMEM);
9e82cf6a 232
ba17674f
LI
233 v9ses->aname = __getname();
234 if (!v9ses->aname) {
235 __putname(v9ses->uname);
bd238fb4 236 return ERR_PTR(-ENOMEM);
9e82cf6a
EVH
237 }
238
ba17674f
LI
239 v9ses->flags = V9FS_EXTENDED | V9FS_ACCESS_USER;
240 strcpy(v9ses->uname, V9FS_DEFUSER);
241 strcpy(v9ses->aname, V9FS_DEFANAME);
242 v9ses->uid = ~0;
bd32b82d
LI
243 v9ses->dfltuid = V9FS_DEFUID;
244 v9ses->dfltgid = V9FS_DEFGID;
a80d923e
EVH
245 v9ses->options = kstrdup(data, GFP_KERNEL);
246 v9fs_parse_options(v9ses);
247
248 if ((v9ses->trans == NULL) && !list_empty(&v9fs_trans_list))
249 v9ses->trans = list_first_entry(&v9fs_trans_list,
250 struct p9_trans_module, list);
251
252 if (v9ses->trans == NULL) {
253 retval = -EPROTONOSUPPORT;
254 P9_DPRINTK(P9_DEBUG_ERROR,
255 "No transport defined or default transport\n");
bd238fb4 256 goto error;
a80d923e 257 }
9e82cf6a 258
a80d923e 259 trans = v9ses->trans->create(dev_name, v9ses->options);
bd238fb4
LI
260 if (IS_ERR(trans)) {
261 retval = PTR_ERR(trans);
262 trans = NULL;
263 goto error;
a8e63bff 264 }
a80d923e
EVH
265 if ((v9ses->maxdata+P9_IOHDRSZ) > v9ses->trans->maxsize)
266 v9ses->maxdata = v9ses->trans->maxsize-P9_IOHDRSZ;
a8e63bff 267
a80d923e 268 v9ses->clnt = p9_client_create(trans, v9ses->maxdata+P9_IOHDRSZ,
2405669b 269 v9fs_extended(v9ses));
9e82cf6a 270
bd238fb4
LI
271 if (IS_ERR(v9ses->clnt)) {
272 retval = PTR_ERR(v9ses->clnt);
273 v9ses->clnt = NULL;
274 P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
275 goto error;
9e82cf6a
EVH
276 }
277
ba17674f
LI
278 if (!v9ses->clnt->dotu)
279 v9ses->flags &= ~V9FS_EXTENDED;
280
281 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
282 if (!v9fs_extended(v9ses) &&
283 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
284
285 v9ses->flags &= ~V9FS_ACCESS_MASK;
286 v9ses->flags |= V9FS_ACCESS_ANY;
287 v9ses->uid = ~0;
288 }
289
290 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0,
291 v9ses->aname);
bd238fb4
LI
292 if (IS_ERR(fid)) {
293 retval = PTR_ERR(fid);
294 fid = NULL;
295 P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
296 goto error;
9e82cf6a
EVH
297 }
298
ba17674f
LI
299 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
300 fid->uid = v9ses->uid;
301 else
302 fid->uid = ~0;
303
bd238fb4 304 return fid;
9e82cf6a 305
bd238fb4 306error:
9e82cf6a 307 v9fs_session_close(v9ses);
bd238fb4 308 return ERR_PTR(retval);
9e82cf6a
EVH
309}
310
311/**
312 * v9fs_session_close - shutdown a session
313 * @v9ses: session information structure
314 *
315 */
316
317void v9fs_session_close(struct v9fs_session_info *v9ses)
318{
bd238fb4
LI
319 if (v9ses->clnt) {
320 p9_client_destroy(v9ses->clnt);
321 v9ses->clnt = NULL;
3cf6429a 322 }
9e82cf6a 323
ba17674f
LI
324 __putname(v9ses->uname);
325 __putname(v9ses->aname);
a80d923e 326 kfree(v9ses->options);
9e82cf6a
EVH
327}
328
322b329a
EVH
329/**
330 * v9fs_session_cancel - mark transport as disconnected
331 * and cancel all pending requests.
332 */
333void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
bd238fb4
LI
334 P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
335 p9_client_disconnect(v9ses->clnt);
322b329a
EVH
336}
337
9e82cf6a
EVH
338extern int v9fs_error_init(void);
339
340/**
341 * v9fs_init - Initialize module
342 *
343 */
344
345static int __init init_v9fs(void)
346{
f94b3470 347 printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
a80d923e 348 /* TODO: Setup list of registered trasnport modules */
bd238fb4 349 return register_filesystem(&v9fs_fs_type);
9e82cf6a
EVH
350}
351
352/**
353 * v9fs_init - shutdown module
354 *
355 */
356
357static void __exit exit_v9fs(void)
358{
359 unregister_filesystem(&v9fs_fs_type);
360}
361
362module_init(init_v9fs)
363module_exit(exit_v9fs)
364
bd238fb4 365MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
9e82cf6a
EVH
366MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
367MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
368MODULE_LICENSE("GPL");