blob: 61352491ba36c0798cf6632de1c3e9e8cdb4e00a [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"
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070040
41/* TODO: sysfs or debugfs interface */
42int v9fs_debug_level = 0; /* feature-rific global debug level */
43
44/*
45 * Option Parsing (code inspired by NFS code)
46 *
47 */
48
49enum {
50 /* Options that take integer arguments */
51 Opt_port, Opt_msize, Opt_uid, Opt_gid, Opt_afid, Opt_debug,
52 Opt_rfdno, Opt_wfdno,
53 /* String options */
54 Opt_name, Opt_remotename,
55 /* Options that take no arguments */
56 Opt_legacy, Opt_nodevmap, Opt_unix, Opt_tcp, Opt_fd,
57 /* Error token */
58 Opt_err
59};
60
61static match_table_t tokens = {
62 {Opt_port, "port=%u"},
63 {Opt_msize, "msize=%u"},
64 {Opt_uid, "uid=%u"},
65 {Opt_gid, "gid=%u"},
66 {Opt_afid, "afid=%u"},
67 {Opt_rfdno, "rfdno=%u"},
68 {Opt_wfdno, "wfdno=%u"},
Eric Van Hensbergene1c92112006-02-20 18:28:05 -080069 {Opt_debug, "debug=%x"},
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -070070 {Opt_name, "name=%s"},
71 {Opt_remotename, "aname=%s"},
72 {Opt_unix, "proto=unix"},
73 {Opt_tcp, "proto=tcp"},
74 {Opt_fd, "proto=fd"},
75 {Opt_tcp, "tcp"},
76 {Opt_unix, "unix"},
77 {Opt_fd, "fd"},
78 {Opt_legacy, "noextend"},
79 {Opt_nodevmap, "nodevmap"},
80 {Opt_err, NULL}
81};
82
83/*
84 * Parse option string.
85 */
86
87/**
88 * v9fs_parse_options - parse mount options into session structure
89 * @options: options string passed from mount
90 * @v9ses: existing v9fs session information
91 *
92 */
93
94static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses)
95{
96 char *p;
97 substring_t args[MAX_OPT_ARGS];
98 int option;
99 int ret;
100
101 /* setup defaults */
102 v9ses->port = V9FS_PORT;
103 v9ses->maxdata = 9000;
104 v9ses->proto = PROTO_TCP;
105 v9ses->extended = 1;
106 v9ses->afid = ~0;
107 v9ses->debug = 0;
108 v9ses->rfdno = ~0;
109 v9ses->wfdno = ~0;
110
111 if (!options)
112 return;
113
114 while ((p = strsep(&options, ",")) != NULL) {
115 int token;
116 if (!*p)
117 continue;
118 token = match_token(p, tokens, args);
119 if (token < Opt_name) {
120 if ((ret = match_int(&args[0], &option)) < 0) {
121 dprintk(DEBUG_ERROR,
122 "integer field, but no integer?\n");
123 continue;
124 }
125
126 }
127 switch (token) {
128 case Opt_port:
129 v9ses->port = option;
130 break;
131 case Opt_msize:
132 v9ses->maxdata = option;
133 break;
134 case Opt_uid:
135 v9ses->uid = option;
136 break;
137 case Opt_gid:
138 v9ses->gid = option;
139 break;
140 case Opt_afid:
141 v9ses->afid = option;
142 break;
143 case Opt_rfdno:
144 v9ses->rfdno = option;
145 break;
146 case Opt_wfdno:
147 v9ses->wfdno = option;
148 break;
149 case Opt_debug:
150 v9ses->debug = option;
151 break;
152 case Opt_tcp:
153 v9ses->proto = PROTO_TCP;
154 break;
155 case Opt_unix:
156 v9ses->proto = PROTO_UNIX;
157 break;
158 case Opt_fd:
159 v9ses->proto = PROTO_FD;
160 break;
161 case Opt_name:
162 match_strcpy(v9ses->name, &args[0]);
163 break;
164 case Opt_remotename:
165 match_strcpy(v9ses->remotename, &args[0]);
166 break;
167 case Opt_legacy:
168 v9ses->extended = 0;
169 break;
170 case Opt_nodevmap:
171 v9ses->nodev = 1;
172 break;
173 default:
174 continue;
175 }
176 }
177}
178
179/**
180 * v9fs_inode2v9ses - safely extract v9fs session info from super block
181 * @inode: inode to extract information from
182 *
183 * Paranoid function to extract v9ses information from superblock,
184 * if anything is missing it will report an error.
185 *
186 */
187
188struct v9fs_session_info *v9fs_inode2v9ses(struct inode *inode)
189{
190 return (inode->i_sb->s_fs_info);
191}
192
193/**
194 * v9fs_get_idpool - allocate numeric id from pool
195 * @p - pool to allocate from
196 *
197 * XXX - This seems to be an awful generic function, should it be in idr.c with
198 * the lock included in struct idr?
199 */
200
201int v9fs_get_idpool(struct v9fs_idpool *p)
202{
203 int i = 0;
204 int error;
205
206retry:
207 if (idr_pre_get(&p->pool, GFP_KERNEL) == 0)
208 return 0;
209
210 if (down_interruptible(&p->lock) == -EINTR) {
211 eprintk(KERN_WARNING, "Interrupted while locking\n");
212 return -1;
213 }
214
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800215 /* no need to store exactly p, we just need something non-null */
216 error = idr_get_new(&p->pool, p, &i);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700217 up(&p->lock);
218
219 if (error == -EAGAIN)
220 goto retry;
221 else if (error)
222 return -1;
223
224 return i;
225}
226
227/**
228 * v9fs_put_idpool - release numeric id from pool
229 * @p - pool to allocate from
230 *
231 * XXX - This seems to be an awful generic function, should it be in idr.c with
232 * the lock included in struct idr?
233 */
234
235void v9fs_put_idpool(int id, struct v9fs_idpool *p)
236{
237 if (down_interruptible(&p->lock) == -EINTR) {
238 eprintk(KERN_WARNING, "Interrupted while locking\n");
239 return;
240 }
241 idr_remove(&p->pool, id);
242 up(&p->lock);
243}
244
245/**
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800246 * v9fs_check_idpool - check if the specified id is available
247 * @id - id to check
248 * @p - pool
249 */
250int v9fs_check_idpool(int id, struct v9fs_idpool *p)
251{
252 return idr_find(&p->pool, id) != NULL;
253}
254
255/**
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700256 * v9fs_session_init - initialize session
257 * @v9ses: session information structure
258 * @dev_name: device being mounted
259 * @data: options
260 *
261 */
262
263int
264v9fs_session_init(struct v9fs_session_info *v9ses,
265 const char *dev_name, char *data)
266{
267 struct v9fs_fcall *fcall = NULL;
268 struct v9fs_transport *trans_proto;
269 int n = 0;
270 int newfid = -1;
271 int retval = -EINVAL;
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800272 struct v9fs_str *version;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700273
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
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800355 version = &fcall->params.rversion.version;
356 if (version->len==8 && !memcmp(version->str, "9P2000.u", 8)) {
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700357 dprintk(DEBUG_9P, "9P2000 UNIX extensions enabled\n");
358 v9ses->extended = 1;
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800359 } else if (version->len==6 && !memcmp(version->str, "9P2000", 6)) {
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700360 dprintk(DEBUG_9P, "9P2000 legacy mode enabled\n");
361 v9ses->extended = 0;
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800362 } else {
363 retval = -EREMOTEIO;
364 goto FreeFcall;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700365 }
366
367 n = fcall->params.rversion.msize;
368 kfree(fcall);
369
370 if (n < v9ses->maxdata)
371 v9ses->maxdata = n;
372 }
373
374 newfid = v9fs_get_idpool(&v9ses->fidpool);
375 if (newfid < 0) {
376 eprintk(KERN_WARNING, "couldn't allocate FID\n");
377 retval = -ENOMEM;
378 goto SessCleanUp;
379 }
380 /* it is a little bit ugly, but we have to prevent newfid */
381 /* being the same as afid, so if it is, get a new fid */
382 if (v9ses->afid != ~0 && newfid == v9ses->afid) {
383 newfid = v9fs_get_idpool(&v9ses->fidpool);
384 if (newfid < 0) {
385 eprintk(KERN_WARNING, "couldn't allocate FID\n");
386 retval = -ENOMEM;
387 goto SessCleanUp;
388 }
389 }
390
391 if ((retval =
392 v9fs_t_attach(v9ses, v9ses->name, v9ses->remotename, newfid,
393 v9ses->afid, NULL))
394 < 0) {
395 dprintk(DEBUG_ERROR, "cannot attach\n");
396 goto SessCleanUp;
397 }
398
399 if (v9ses->afid != ~0) {
Eric Van Hensbergen46f6dac2006-03-02 02:54:33 -0800400 dprintk(DEBUG_ERROR, "afid not equal to ~0\n");
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800401 if (v9fs_t_clunk(v9ses, v9ses->afid))
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700402 dprintk(DEBUG_ERROR, "clunk failed\n");
403 }
404
405 return newfid;
406
407 FreeFcall:
408 kfree(fcall);
409
410 SessCleanUp:
411 v9fs_session_close(v9ses);
412 return retval;
413}
414
415/**
416 * v9fs_session_close - shutdown a session
417 * @v9ses: session information structure
418 *
419 */
420
421void v9fs_session_close(struct v9fs_session_info *v9ses)
422{
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800423 if (v9ses->mux) {
424 v9fs_mux_destroy(v9ses->mux);
425 v9ses->mux = NULL;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700426 }
427
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800428 if (v9ses->transport) {
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700429 v9ses->transport->close(v9ses->transport);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800430 kfree(v9ses->transport);
431 v9ses->transport = NULL;
432 }
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700433
Davi Arnautce44eeb2005-11-07 00:59:36 -0800434 __putname(v9ses->name);
435 __putname(v9ses->remotename);
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700436}
437
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700438/**
439 * v9fs_session_cancel - mark transport as disconnected
440 * and cancel all pending requests.
441 */
442void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800443 dprintk(DEBUG_ERROR, "cancel session %p\n", v9ses);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700444 v9ses->transport->status = Disconnected;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800445 v9fs_mux_cancel(v9ses->mux, -EIO);
Eric Van Hensbergen322b3292005-09-09 13:04:23 -0700446}
447
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700448extern int v9fs_error_init(void);
449
450/**
451 * v9fs_init - Initialize module
452 *
453 */
454
455static int __init init_v9fs(void)
456{
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800457 int ret;
458
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700459 v9fs_error_init();
460
461 printk(KERN_INFO "Installing v9fs 9P2000 file system support\n");
462
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800463 ret = v9fs_mux_global_init();
464 if (!ret)
465 ret = register_filesystem(&v9fs_fs_type);
466
467 return ret;
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700468}
469
470/**
471 * v9fs_init - shutdown module
472 *
473 */
474
475static void __exit exit_v9fs(void)
476{
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800477 v9fs_mux_global_exit();
Eric Van Hensbergen9e82cf62005-09-09 13:04:20 -0700478 unregister_filesystem(&v9fs_fs_type);
479}
480
481module_init(init_v9fs)
482module_exit(exit_v9fs)
483
484MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
485MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
486MODULE_LICENSE("GPL");