blob: 5e0f79355fdf8ebcb19896ad3cbc304e7b71bb6a [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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
24 *
25 */
26
27#include <linux/config.h>
28#include <linux/module.h>
29#include <linux/errno.h>
30#include <linux/fs.h>
31#include <linux/parser.h>
32#include <linux/idr.h>
33
34#include "debug.h"
35#include "v9fs.h"
36#include "9p.h"
37#include "v9fs_vfs.h"
38#include "transport.h"
39#include "mux.h"
40#include "conv.h"
41
42/* TODO: sysfs or debugfs interface */
43int v9fs_debug_level = 0; /* feature-rific global debug level */
44
45/*
46 * Option Parsing (code inspired by NFS code)
47 *
48 */
49
50enum {
51 /* Options that take integer arguments */
52 Opt_port, Opt_msize, Opt_uid, Opt_gid, Opt_afid, Opt_debug,
53 Opt_rfdno, Opt_wfdno,
54 /* String options */
55 Opt_name, Opt_remotename,
56 /* Options that take no arguments */
57 Opt_legacy, Opt_nodevmap, Opt_unix, Opt_tcp, Opt_fd,
58 /* Error token */
59 Opt_err
60};
61
62static match_table_t tokens = {
63 {Opt_port, "port=%u"},
64 {Opt_msize, "msize=%u"},
65 {Opt_uid, "uid=%u"},
66 {Opt_gid, "gid=%u"},
67 {Opt_afid, "afid=%u"},
68 {Opt_rfdno, "rfdno=%u"},
69 {Opt_wfdno, "wfdno=%u"},
70 {Opt_debug, "debug=%u"},
71 {Opt_name, "name=%s"},
72 {Opt_remotename, "aname=%s"},
73 {Opt_unix, "proto=unix"},
74 {Opt_tcp, "proto=tcp"},
75 {Opt_fd, "proto=fd"},
76 {Opt_tcp, "tcp"},
77 {Opt_unix, "unix"},
78 {Opt_fd, "fd"},
79 {Opt_legacy, "noextend"},
80 {Opt_nodevmap, "nodevmap"},
81 {Opt_err, NULL}
82};
83
84/*
85 * Parse option string.
86 */
87
88/**
89 * v9fs_parse_options - parse mount options into session structure
90 * @options: options string passed from mount
91 * @v9ses: existing v9fs session information
92 *
93 */
94
95static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses)
96{
97 char *p;
98 substring_t args[MAX_OPT_ARGS];
99 int option;
100 int ret;
101
102 /* setup defaults */
103 v9ses->port = V9FS_PORT;
104 v9ses->maxdata = 9000;
105 v9ses->proto = PROTO_TCP;
106 v9ses->extended = 1;
107 v9ses->afid = ~0;
108 v9ses->debug = 0;
109 v9ses->rfdno = ~0;
110 v9ses->wfdno = ~0;
111
112 if (!options)
113 return;
114
115 while ((p = strsep(&options, ",")) != NULL) {
116 int token;
117 if (!*p)
118 continue;
119 token = match_token(p, tokens, args);
120 if (token < Opt_name) {
121 if ((ret = match_int(&args[0], &option)) < 0) {
122 dprintk(DEBUG_ERROR,
123 "integer field, but no integer?\n");
124 continue;
125 }
126
127 }
128 switch (token) {
129 case Opt_port:
130 v9ses->port = option;
131 break;
132 case Opt_msize:
133 v9ses->maxdata = option;
134 break;
135 case Opt_uid:
136 v9ses->uid = option;
137 break;
138 case Opt_gid:
139 v9ses->gid = option;
140 break;
141 case Opt_afid:
142 v9ses->afid = option;
143 break;
144 case Opt_rfdno:
145 v9ses->rfdno = option;
146 break;
147 case Opt_wfdno:
148 v9ses->wfdno = option;
149 break;
150 case Opt_debug:
151 v9ses->debug = option;
152 break;
153 case Opt_tcp:
154 v9ses->proto = PROTO_TCP;
155 break;
156 case Opt_unix:
157 v9ses->proto = PROTO_UNIX;
158 break;
159 case Opt_fd:
160 v9ses->proto = PROTO_FD;
161 break;
162 case Opt_name:
163 match_strcpy(v9ses->name, &args[0]);
164 break;
165 case Opt_remotename:
166 match_strcpy(v9ses->remotename, &args[0]);
167 break;
168 case Opt_legacy:
169 v9ses->extended = 0;
170 break;
171 case Opt_nodevmap:
172 v9ses->nodev = 1;
173 break;
174 default:
175 continue;
176 }
177 }
178}
179
180/**
181 * v9fs_inode2v9ses - safely extract v9fs session info from super block
182 * @inode: inode to extract information from
183 *
184 * Paranoid function to extract v9ses information from superblock,
185 * if anything is missing it will report an error.
186 *
187 */
188
189struct v9fs_session_info *v9fs_inode2v9ses(struct inode *inode)
190{
191 return (inode->i_sb->s_fs_info);
192}
193
194/**
195 * v9fs_get_idpool - allocate numeric id from pool
196 * @p - pool to allocate from
197 *
198 * XXX - This seems to be an awful generic function, should it be in idr.c with
199 * the lock included in struct idr?
200 */
201
202int v9fs_get_idpool(struct v9fs_idpool *p)
203{
204 int i = 0;
205 int error;
206
207retry:
208 if (idr_pre_get(&p->pool, GFP_KERNEL) == 0)
209 return 0;
210
211 if (down_interruptible(&p->lock) == -EINTR) {
212 eprintk(KERN_WARNING, "Interrupted while locking\n");
213 return -1;
214 }
215
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800216 /* no need to store exactly p, we just need something non-null */
217 error = idr_get_new(&p->pool, p, &i);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700218 up(&p->lock);
219
220 if (error == -EAGAIN)
221 goto retry;
222 else if (error)
223 return -1;
224
225 return i;
226}
227
228/**
229 * v9fs_put_idpool - release numeric id from pool
230 * @p - pool to allocate from
231 *
232 * XXX - This seems to be an awful generic function, should it be in idr.c with
233 * the lock included in struct idr?
234 */
235
236void v9fs_put_idpool(int id, struct v9fs_idpool *p)
237{
238 if (down_interruptible(&p->lock) == -EINTR) {
239 eprintk(KERN_WARNING, "Interrupted while locking\n");
240 return;
241 }
242 idr_remove(&p->pool, id);
243 up(&p->lock);
244}
245
246/**
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800247 * v9fs_check_idpool - check if the specified id is available
248 * @id - id to check
249 * @p - pool
250 */
251int v9fs_check_idpool(int id, struct v9fs_idpool *p)
252{
253 return idr_find(&p->pool, id) != NULL;
254}
255
256/**
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700257 * v9fs_session_init - initialize session
258 * @v9ses: session information structure
259 * @dev_name: device being mounted
260 * @data: options
261 *
262 */
263
264int
265v9fs_session_init(struct v9fs_session_info *v9ses,
266 const char *dev_name, char *data)
267{
268 struct v9fs_fcall *fcall = NULL;
269 struct v9fs_transport *trans_proto;
270 int n = 0;
271 int newfid = -1;
272 int retval = -EINVAL;
273
274 v9ses->name = __getname();
275 if (!v9ses->name)
276 return -ENOMEM;
277
278 v9ses->remotename = __getname();
279 if (!v9ses->remotename) {
Davi Arnautce44eeb2005-11-07 00:59:36 -0800280 __putname(v9ses->name);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700281 return -ENOMEM;
282 }
283
284 strcpy(v9ses->name, V9FS_DEFUSER);
285 strcpy(v9ses->remotename, V9FS_DEFANAME);
286
287 v9fs_parse_options(data, v9ses);
288
289 /* set global debug level */
290 v9fs_debug_level = v9ses->debug;
291
292 /* id pools that are session-dependent: FIDs and TIDs */
293 idr_init(&v9ses->fidpool.pool);
294 init_MUTEX(&v9ses->fidpool.lock);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700295
296 switch (v9ses->proto) {
297 case PROTO_TCP:
298 trans_proto = &v9fs_trans_tcp;
299 break;
300 case PROTO_UNIX:
301 trans_proto = &v9fs_trans_unix;
302 *v9ses->remotename = 0;
303 break;
304 case PROTO_FD:
305 trans_proto = &v9fs_trans_fd;
306 *v9ses->remotename = 0;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700307 break;
308 default:
309 printk(KERN_ERR "v9fs: Bad mount protocol %d\n", v9ses->proto);
310 retval = -ENOPROTOOPT;
311 goto SessCleanUp;
312 };
313
Latchesar Ionkova8e63bf2005-09-22 21:43:51 -0700314 v9ses->transport = kmalloc(sizeof(*v9ses->transport), GFP_KERNEL);
315 if (!v9ses->transport) {
316 retval = -ENOMEM;
317 goto SessCleanUp;
318 }
319
320 memmove(v9ses->transport, trans_proto, sizeof(*v9ses->transport));
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700321
322 if ((retval = v9ses->transport->init(v9ses, dev_name, data)) < 0) {
323 eprintk(KERN_ERR, "problem initializing transport\n");
324 goto SessCleanUp;
325 }
326
327 v9ses->inprogress = 0;
328 v9ses->shutdown = 0;
329 v9ses->session_hung = 0;
330
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800331 v9ses->mux = v9fs_mux_init(v9ses->transport, v9ses->maxdata + V9FS_IOHDRSZ,
332 &v9ses->extended);
333
334 if (IS_ERR(v9ses->mux)) {
335 retval = PTR_ERR(v9ses->mux);
336 v9ses->mux = NULL;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700337 dprintk(DEBUG_ERROR, "problem initializing mux\n");
338 goto SessCleanUp;
339 }
340
341 if (v9ses->afid == ~0) {
342 if (v9ses->extended)
343 retval =
344 v9fs_t_version(v9ses, v9ses->maxdata, "9P2000.u",
345 &fcall);
346 else
347 retval = v9fs_t_version(v9ses, v9ses->maxdata, "9P2000",
348 &fcall);
349
350 if (retval < 0) {
351 dprintk(DEBUG_ERROR, "v9fs_t_version failed\n");
352 goto FreeFcall;
353 }
354
355 /* Really should check for 9P1 and report error */
356 if (!strcmp(fcall->params.rversion.version, "9P2000.u")) {
357 dprintk(DEBUG_9P, "9P2000 UNIX extensions enabled\n");
358 v9ses->extended = 1;
359 } else {
360 dprintk(DEBUG_9P, "9P2000 legacy mode enabled\n");
361 v9ses->extended = 0;
362 }
363
364 n = fcall->params.rversion.msize;
365 kfree(fcall);
366
367 if (n < v9ses->maxdata)
368 v9ses->maxdata = n;
369 }
370
371 newfid = v9fs_get_idpool(&v9ses->fidpool);
372 if (newfid < 0) {
373 eprintk(KERN_WARNING, "couldn't allocate FID\n");
374 retval = -ENOMEM;
375 goto SessCleanUp;
376 }
377 /* it is a little bit ugly, but we have to prevent newfid */
378 /* being the same as afid, so if it is, get a new fid */
379 if (v9ses->afid != ~0 && newfid == v9ses->afid) {
380 newfid = v9fs_get_idpool(&v9ses->fidpool);
381 if (newfid < 0) {
382 eprintk(KERN_WARNING, "couldn't allocate FID\n");
383 retval = -ENOMEM;
384 goto SessCleanUp;
385 }
386 }
387
388 if ((retval =
389 v9fs_t_attach(v9ses, v9ses->name, v9ses->remotename, newfid,
390 v9ses->afid, NULL))
391 < 0) {
392 dprintk(DEBUG_ERROR, "cannot attach\n");
393 goto SessCleanUp;
394 }
395
396 if (v9ses->afid != ~0) {
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800397 if (v9fs_t_clunk(v9ses, v9ses->afid))
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700398 dprintk(DEBUG_ERROR, "clunk failed\n");
399 }
400
401 return newfid;
402
403 FreeFcall:
404 kfree(fcall);
405
406 SessCleanUp:
407 v9fs_session_close(v9ses);
408 return retval;
409}
410
411/**
412 * v9fs_session_close - shutdown a session
413 * @v9ses: session information structure
414 *
415 */
416
417void v9fs_session_close(struct v9fs_session_info *v9ses)
418{
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800419 if (v9ses->mux) {
420 v9fs_mux_destroy(v9ses->mux);
421 v9ses->mux = NULL;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700422 }
423
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800424 if (v9ses->transport) {
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700425 v9ses->transport->close(v9ses->transport);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800426 kfree(v9ses->transport);
427 v9ses->transport = NULL;
428 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700429
Davi Arnautce44eeb2005-11-07 00:59:36 -0800430 __putname(v9ses->name);
431 __putname(v9ses->remotename);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700432}
433
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700434/**
435 * v9fs_session_cancel - mark transport as disconnected
436 * and cancel all pending requests.
437 */
438void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800439 dprintk(DEBUG_ERROR, "cancel session %p\n", v9ses);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700440 v9ses->transport->status = Disconnected;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800441 v9fs_mux_cancel(v9ses->mux, -EIO);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700442}
443
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700444extern int v9fs_error_init(void);
445
446/**
447 * v9fs_init - Initialize module
448 *
449 */
450
451static int __init init_v9fs(void)
452{
453 v9fs_error_init();
454
455 printk(KERN_INFO "Installing v9fs 9P2000 file system support\n");
456
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800457 v9fs_mux_global_init();
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700458 return register_filesystem(&v9fs_fs_type);
459}
460
461/**
462 * v9fs_init - shutdown module
463 *
464 */
465
466static void __exit exit_v9fs(void)
467{
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800468 v9fs_mux_global_exit();
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700469 unregister_filesystem(&v9fs_fs_type);
470}
471
472module_init(init_v9fs)
473module_exit(exit_v9fs)
474
475MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
476MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
477MODULE_LICENSE("GPL");