4 * This file contains functions assisting in mapping VFS to 9P2000
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
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.
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
26 #include <linux/module.h>
27 #include <linux/errno.h>
29 #include <linux/sched.h>
30 #include <linux/parser.h>
31 #include <linux/idr.h>
37 #include "transport.h"
40 /* TODO: sysfs or debugfs interface */
41 int v9fs_debug_level
= 0; /* feature-rific global debug level */
44 * Option Parsing (code inspired by NFS code)
49 /* Options that take integer arguments */
50 Opt_port
, Opt_msize
, Opt_uid
, Opt_gid
, Opt_afid
, Opt_debug
,
53 Opt_uname
, Opt_remotename
,
54 /* Options that take no arguments */
55 Opt_legacy
, Opt_nodevmap
, Opt_unix
, Opt_tcp
, Opt_fd
,
62 static match_table_t tokens
= {
63 {Opt_port
, "port=%u"},
64 {Opt_msize
, "msize=%u"},
67 {Opt_afid
, "afid=%u"},
68 {Opt_rfdno
, "rfdno=%u"},
69 {Opt_wfdno
, "wfdno=%u"},
70 {Opt_debug
, "debug=%x"},
71 {Opt_uname
, "uname=%s"},
72 {Opt_remotename
, "aname=%s"},
73 {Opt_unix
, "proto=unix"},
74 {Opt_tcp
, "proto=tcp"},
79 {Opt_legacy
, "noextend"},
80 {Opt_nodevmap
, "nodevmap"},
81 {Opt_cache_loose
, "cache=loose"},
82 {Opt_cache_loose
, "loose"},
87 * Parse option string.
91 * v9fs_parse_options - parse mount options into session structure
92 * @options: options string passed from mount
93 * @v9ses: existing v9fs session information
97 static void v9fs_parse_options(char *options
, struct v9fs_session_info
*v9ses
)
100 substring_t args
[MAX_OPT_ARGS
];
105 v9ses
->port
= V9FS_PORT
;
106 v9ses
->maxdata
= 9000;
107 v9ses
->proto
= PROTO_TCP
;
118 while ((p
= strsep(&options
, ",")) != NULL
) {
122 token
= match_token(p
, tokens
, args
);
123 if (token
< Opt_uname
) {
124 if ((ret
= match_int(&args
[0], &option
)) < 0) {
126 "integer field, but no integer?\n");
132 v9ses
->port
= option
;
135 v9ses
->maxdata
= option
;
144 v9ses
->afid
= option
;
147 v9ses
->rfdno
= option
;
150 v9ses
->wfdno
= option
;
153 v9ses
->debug
= option
;
156 v9ses
->proto
= PROTO_TCP
;
159 v9ses
->proto
= PROTO_UNIX
;
162 v9ses
->proto
= PROTO_FD
;
165 match_strcpy(v9ses
->name
, &args
[0]);
168 match_strcpy(v9ses
->remotename
, &args
[0]);
176 case Opt_cache_loose
:
177 v9ses
->cache
= CACHE_LOOSE
;
186 * v9fs_inode2v9ses - safely extract v9fs session info from super block
187 * @inode: inode to extract information from
189 * Paranoid function to extract v9ses information from superblock,
190 * if anything is missing it will report an error.
194 struct v9fs_session_info
*v9fs_inode2v9ses(struct inode
*inode
)
196 return (inode
->i_sb
->s_fs_info
);
200 * v9fs_get_idpool - allocate numeric id from pool
201 * @p - pool to allocate from
203 * XXX - This seems to be an awful generic function, should it be in idr.c with
204 * the lock included in struct idr?
207 int v9fs_get_idpool(struct v9fs_idpool
*p
)
213 if (idr_pre_get(&p
->pool
, GFP_KERNEL
) == 0)
216 if (down_interruptible(&p
->lock
) == -EINTR
) {
217 eprintk(KERN_WARNING
, "Interrupted while locking\n");
221 /* no need to store exactly p, we just need something non-null */
222 error
= idr_get_new(&p
->pool
, p
, &i
);
225 if (error
== -EAGAIN
)
234 * v9fs_put_idpool - release numeric id from pool
235 * @p - pool to allocate from
237 * XXX - This seems to be an awful generic function, should it be in idr.c with
238 * the lock included in struct idr?
241 void v9fs_put_idpool(int id
, struct v9fs_idpool
*p
)
243 if (down_interruptible(&p
->lock
) == -EINTR
) {
244 eprintk(KERN_WARNING
, "Interrupted while locking\n");
247 idr_remove(&p
->pool
, id
);
252 * v9fs_check_idpool - check if the specified id is available
256 int v9fs_check_idpool(int id
, struct v9fs_idpool
*p
)
258 return idr_find(&p
->pool
, id
) != NULL
;
262 * v9fs_session_init - initialize session
263 * @v9ses: session information structure
264 * @dev_name: device being mounted
270 v9fs_session_init(struct v9fs_session_info
*v9ses
,
271 const char *dev_name
, char *data
)
273 struct v9fs_fcall
*fcall
= NULL
;
274 struct v9fs_transport
*trans_proto
;
277 int retval
= -EINVAL
;
278 struct v9fs_str
*version
;
280 v9ses
->name
= __getname();
284 v9ses
->remotename
= __getname();
285 if (!v9ses
->remotename
) {
286 __putname(v9ses
->name
);
290 strcpy(v9ses
->name
, V9FS_DEFUSER
);
291 strcpy(v9ses
->remotename
, V9FS_DEFANAME
);
293 v9fs_parse_options(data
, v9ses
);
295 /* set global debug level */
296 v9fs_debug_level
= v9ses
->debug
;
298 /* id pools that are session-dependent: fids and tags */
299 idr_init(&v9ses
->fidpool
.pool
);
300 init_MUTEX(&v9ses
->fidpool
.lock
);
302 switch (v9ses
->proto
) {
304 trans_proto
= &v9fs_trans_tcp
;
307 trans_proto
= &v9fs_trans_unix
;
308 *v9ses
->remotename
= 0;
311 trans_proto
= &v9fs_trans_fd
;
312 *v9ses
->remotename
= 0;
315 printk(KERN_ERR
"v9fs: Bad mount protocol %d\n", v9ses
->proto
);
316 retval
= -ENOPROTOOPT
;
320 v9ses
->transport
= kmalloc(sizeof(*v9ses
->transport
), GFP_KERNEL
);
321 if (!v9ses
->transport
) {
326 memmove(v9ses
->transport
, trans_proto
, sizeof(*v9ses
->transport
));
328 if ((retval
= v9ses
->transport
->init(v9ses
, dev_name
, data
)) < 0) {
329 eprintk(KERN_ERR
, "problem initializing transport\n");
333 v9ses
->inprogress
= 0;
335 v9ses
->session_hung
= 0;
337 v9ses
->mux
= v9fs_mux_init(v9ses
->transport
, v9ses
->maxdata
+ V9FS_IOHDRSZ
,
340 if (IS_ERR(v9ses
->mux
)) {
341 retval
= PTR_ERR(v9ses
->mux
);
343 dprintk(DEBUG_ERROR
, "problem initializing mux\n");
347 if (v9ses
->afid
== ~0) {
350 v9fs_t_version(v9ses
, v9ses
->maxdata
, "9P2000.u",
353 retval
= v9fs_t_version(v9ses
, v9ses
->maxdata
, "9P2000",
357 dprintk(DEBUG_ERROR
, "v9fs_t_version failed\n");
361 version
= &fcall
->params
.rversion
.version
;
362 if (version
->len
==8 && !memcmp(version
->str
, "9P2000.u", 8)) {
363 dprintk(DEBUG_9P
, "9P2000 UNIX extensions enabled\n");
365 } else if (version
->len
==6 && !memcmp(version
->str
, "9P2000", 6)) {
366 dprintk(DEBUG_9P
, "9P2000 legacy mode enabled\n");
373 n
= fcall
->params
.rversion
.msize
;
376 if (n
< v9ses
->maxdata
)
380 newfid
= v9fs_get_idpool(&v9ses
->fidpool
);
382 eprintk(KERN_WARNING
, "couldn't allocate FID\n");
386 /* it is a little bit ugly, but we have to prevent newfid */
387 /* being the same as afid, so if it is, get a new fid */
388 if (v9ses
->afid
!= ~0 && newfid
== v9ses
->afid
) {
389 newfid
= v9fs_get_idpool(&v9ses
->fidpool
);
391 eprintk(KERN_WARNING
, "couldn't allocate FID\n");
398 v9fs_t_attach(v9ses
, v9ses
->name
, v9ses
->remotename
, newfid
,
401 dprintk(DEBUG_ERROR
, "cannot attach\n");
405 if (v9ses
->afid
!= ~0) {
406 dprintk(DEBUG_ERROR
, "afid not equal to ~0\n");
407 if (v9fs_t_clunk(v9ses
, v9ses
->afid
))
408 dprintk(DEBUG_ERROR
, "clunk failed\n");
417 v9fs_session_close(v9ses
);
422 * v9fs_session_close - shutdown a session
423 * @v9ses: session information structure
427 void v9fs_session_close(struct v9fs_session_info
*v9ses
)
430 v9fs_mux_destroy(v9ses
->mux
);
434 if (v9ses
->transport
) {
435 v9ses
->transport
->close(v9ses
->transport
);
436 kfree(v9ses
->transport
);
437 v9ses
->transport
= NULL
;
440 __putname(v9ses
->name
);
441 __putname(v9ses
->remotename
);
445 * v9fs_session_cancel - mark transport as disconnected
446 * and cancel all pending requests.
448 void v9fs_session_cancel(struct v9fs_session_info
*v9ses
) {
449 dprintk(DEBUG_ERROR
, "cancel session %p\n", v9ses
);
450 v9ses
->transport
->status
= Disconnected
;
451 v9fs_mux_cancel(v9ses
->mux
, -EIO
);
454 extern int v9fs_error_init(void);
457 * v9fs_init - Initialize module
461 static int __init
init_v9fs(void)
467 printk(KERN_INFO
"Installing v9fs 9p2000 file system support\n");
469 ret
= v9fs_mux_global_init();
471 printk(KERN_WARNING
"v9fs: starting mux failed\n");
474 ret
= register_filesystem(&v9fs_fs_type
);
476 printk(KERN_WARNING
"v9fs: registering file system failed\n");
477 v9fs_mux_global_exit();
484 * v9fs_init - shutdown module
488 static void __exit
exit_v9fs(void)
490 v9fs_mux_global_exit();
491 unregister_filesystem(&v9fs_fs_type
);
494 module_init(init_v9fs
)
495 module_exit(exit_v9fs
)
497 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
498 MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
499 MODULE_LICENSE("GPL");