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 as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
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.
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
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/errno.h>
31 #include <linux/parser.h>
32 #include <linux/idr.h>
38 #include "transport.h"
42 /* TODO: sysfs or debugfs interface */
43 int v9fs_debug_level
= 0; /* feature-rific global debug level */
46 * Option Parsing (code inspired by NFS code)
51 /* Options that take integer arguments */
52 Opt_port
, Opt_msize
, Opt_uid
, Opt_gid
, Opt_afid
, Opt_debug
,
55 Opt_name
, Opt_remotename
,
56 /* Options that take no arguments */
57 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=%u"},
71 {Opt_name
, "name=%s"},
72 {Opt_remotename
, "aname=%s"},
73 {Opt_unix
, "proto=unix"},
74 {Opt_tcp
, "proto=tcp"},
79 {Opt_legacy
, "noextend"},
80 {Opt_nodevmap
, "nodevmap"},
85 * Parse option string.
89 * v9fs_parse_options - parse mount options into session structure
90 * @options: options string passed from mount
91 * @v9ses: existing v9fs session information
95 static void v9fs_parse_options(char *options
, struct v9fs_session_info
*v9ses
)
98 substring_t args
[MAX_OPT_ARGS
];
103 v9ses
->port
= V9FS_PORT
;
104 v9ses
->maxdata
= 9000;
105 v9ses
->proto
= PROTO_TCP
;
115 while ((p
= strsep(&options
, ",")) != NULL
) {
119 token
= match_token(p
, tokens
, args
);
120 if (token
< Opt_name
) {
121 if ((ret
= match_int(&args
[0], &option
)) < 0) {
123 "integer field, but no integer?\n");
130 v9ses
->port
= option
;
133 v9ses
->maxdata
= option
;
142 v9ses
->afid
= option
;
145 v9ses
->rfdno
= option
;
148 v9ses
->wfdno
= option
;
151 v9ses
->debug
= option
;
154 v9ses
->proto
= PROTO_TCP
;
157 v9ses
->proto
= PROTO_UNIX
;
160 v9ses
->proto
= PROTO_FD
;
163 match_strcpy(v9ses
->name
, &args
[0]);
166 match_strcpy(v9ses
->remotename
, &args
[0]);
181 * v9fs_inode2v9ses - safely extract v9fs session info from super block
182 * @inode: inode to extract information from
184 * Paranoid function to extract v9ses information from superblock,
185 * if anything is missing it will report an error.
189 struct v9fs_session_info
*v9fs_inode2v9ses(struct inode
*inode
)
191 return (inode
->i_sb
->s_fs_info
);
195 * v9fs_get_idpool - allocate numeric id from pool
196 * @p - pool to allocate from
198 * XXX - This seems to be an awful generic function, should it be in idr.c with
199 * the lock included in struct idr?
202 int v9fs_get_idpool(struct v9fs_idpool
*p
)
208 if (idr_pre_get(&p
->pool
, GFP_KERNEL
) == 0)
211 if (down_interruptible(&p
->lock
) == -EINTR
) {
212 eprintk(KERN_WARNING
, "Interrupted while locking\n");
216 error
= idr_get_new(&p
->pool
, NULL
, &i
);
219 if (error
== -EAGAIN
)
228 * v9fs_put_idpool - release numeric id from pool
229 * @p - pool to allocate from
231 * XXX - This seems to be an awful generic function, should it be in idr.c with
232 * the lock included in struct idr?
235 void v9fs_put_idpool(int id
, struct v9fs_idpool
*p
)
237 if (down_interruptible(&p
->lock
) == -EINTR
) {
238 eprintk(KERN_WARNING
, "Interrupted while locking\n");
241 idr_remove(&p
->pool
, id
);
246 * v9fs_session_init - initialize session
247 * @v9ses: session information structure
248 * @dev_name: device being mounted
254 v9fs_session_init(struct v9fs_session_info
*v9ses
,
255 const char *dev_name
, char *data
)
257 struct v9fs_fcall
*fcall
= NULL
;
258 struct v9fs_transport
*trans_proto
;
261 int retval
= -EINVAL
;
263 v9ses
->name
= __getname();
267 v9ses
->remotename
= __getname();
268 if (!v9ses
->remotename
) {
269 __putname(v9ses
->name
);
273 strcpy(v9ses
->name
, V9FS_DEFUSER
);
274 strcpy(v9ses
->remotename
, V9FS_DEFANAME
);
276 v9fs_parse_options(data
, v9ses
);
278 /* set global debug level */
279 v9fs_debug_level
= v9ses
->debug
;
281 /* id pools that are session-dependent: FIDs and TIDs */
282 idr_init(&v9ses
->fidpool
.pool
);
283 init_MUTEX(&v9ses
->fidpool
.lock
);
284 idr_init(&v9ses
->tidpool
.pool
);
285 init_MUTEX(&v9ses
->tidpool
.lock
);
288 switch (v9ses
->proto
) {
290 trans_proto
= &v9fs_trans_tcp
;
293 trans_proto
= &v9fs_trans_unix
;
294 *v9ses
->remotename
= 0;
297 trans_proto
= &v9fs_trans_fd
;
298 *v9ses
->remotename
= 0;
301 printk(KERN_ERR
"v9fs: Bad mount protocol %d\n", v9ses
->proto
);
302 retval
= -ENOPROTOOPT
;
306 v9ses
->transport
= kmalloc(sizeof(*v9ses
->transport
), GFP_KERNEL
);
307 if (!v9ses
->transport
) {
312 memmove(v9ses
->transport
, trans_proto
, sizeof(*v9ses
->transport
));
314 if ((retval
= v9ses
->transport
->init(v9ses
, dev_name
, data
)) < 0) {
315 eprintk(KERN_ERR
, "problem initializing transport\n");
319 v9ses
->inprogress
= 0;
321 v9ses
->session_hung
= 0;
323 if ((retval
= v9fs_mux_init(v9ses
, dev_name
)) < 0) {
324 dprintk(DEBUG_ERROR
, "problem initializing mux\n");
328 if (v9ses
->afid
== ~0) {
331 v9fs_t_version(v9ses
, v9ses
->maxdata
, "9P2000.u",
334 retval
= v9fs_t_version(v9ses
, v9ses
->maxdata
, "9P2000",
338 dprintk(DEBUG_ERROR
, "v9fs_t_version failed\n");
342 /* Really should check for 9P1 and report error */
343 if (!strcmp(fcall
->params
.rversion
.version
, "9P2000.u")) {
344 dprintk(DEBUG_9P
, "9P2000 UNIX extensions enabled\n");
347 dprintk(DEBUG_9P
, "9P2000 legacy mode enabled\n");
351 n
= fcall
->params
.rversion
.msize
;
354 if (n
< v9ses
->maxdata
)
358 newfid
= v9fs_get_idpool(&v9ses
->fidpool
);
360 eprintk(KERN_WARNING
, "couldn't allocate FID\n");
364 /* it is a little bit ugly, but we have to prevent newfid */
365 /* being the same as afid, so if it is, get a new fid */
366 if (v9ses
->afid
!= ~0 && newfid
== v9ses
->afid
) {
367 newfid
= v9fs_get_idpool(&v9ses
->fidpool
);
369 eprintk(KERN_WARNING
, "couldn't allocate FID\n");
376 v9fs_t_attach(v9ses
, v9ses
->name
, v9ses
->remotename
, newfid
,
379 dprintk(DEBUG_ERROR
, "cannot attach\n");
383 if (v9ses
->afid
!= ~0) {
384 if (v9fs_t_clunk(v9ses
, v9ses
->afid
, NULL
))
385 dprintk(DEBUG_ERROR
, "clunk failed\n");
394 v9fs_session_close(v9ses
);
399 * v9fs_session_close - shutdown a session
400 * @v9ses: session information structure
404 void v9fs_session_close(struct v9fs_session_info
*v9ses
)
406 if (v9ses
->recvproc
) {
407 send_sig(SIGKILL
, v9ses
->recvproc
, 1);
408 wait_for_completion(&v9ses
->proccmpl
);
411 if (v9ses
->transport
)
412 v9ses
->transport
->close(v9ses
->transport
);
414 __putname(v9ses
->name
);
415 __putname(v9ses
->remotename
);
419 * v9fs_session_cancel - mark transport as disconnected
420 * and cancel all pending requests.
422 void v9fs_session_cancel(struct v9fs_session_info
*v9ses
) {
423 v9ses
->transport
->status
= Disconnected
;
424 v9fs_mux_cancel_requests(v9ses
, -EIO
);
427 extern int v9fs_error_init(void);
430 * v9fs_init - Initialize module
434 static int __init
init_v9fs(void)
438 printk(KERN_INFO
"Installing v9fs 9P2000 file system support\n");
440 return register_filesystem(&v9fs_fs_type
);
444 * v9fs_init - shutdown module
448 static void __exit
exit_v9fs(void)
450 unregister_filesystem(&v9fs_fs_type
);
453 module_init(init_v9fs
)
454 module_exit(exit_v9fs
)
456 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
457 MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
458 MODULE_LICENSE("GPL");