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/parser.h>
30 #include <linux/idr.h>
36 #include "transport.h"
39 /* TODO: sysfs or debugfs interface */
40 int v9fs_debug_level
= 0; /* feature-rific global debug level */
43 * Option Parsing (code inspired by NFS code)
48 /* Options that take integer arguments */
49 Opt_port
, Opt_msize
, Opt_uid
, Opt_gid
, Opt_afid
, Opt_debug
,
52 Opt_uname
, Opt_remotename
,
53 /* Options that take no arguments */
54 Opt_legacy
, Opt_nodevmap
, Opt_unix
, Opt_tcp
, Opt_fd
,
59 static match_table_t tokens
= {
60 {Opt_port
, "port=%u"},
61 {Opt_msize
, "msize=%u"},
64 {Opt_afid
, "afid=%u"},
65 {Opt_rfdno
, "rfdno=%u"},
66 {Opt_wfdno
, "wfdno=%u"},
67 {Opt_debug
, "debug=%x"},
68 {Opt_uname
, "uname=%s"},
69 {Opt_remotename
, "aname=%s"},
70 {Opt_unix
, "proto=unix"},
71 {Opt_tcp
, "proto=tcp"},
76 {Opt_legacy
, "noextend"},
77 {Opt_nodevmap
, "nodevmap"},
82 * Parse option string.
86 * v9fs_parse_options - parse mount options into session structure
87 * @options: options string passed from mount
88 * @v9ses: existing v9fs session information
92 static void v9fs_parse_options(char *options
, struct v9fs_session_info
*v9ses
)
95 substring_t args
[MAX_OPT_ARGS
];
100 v9ses
->port
= V9FS_PORT
;
101 v9ses
->maxdata
= 9000;
102 v9ses
->proto
= PROTO_TCP
;
112 while ((p
= strsep(&options
, ",")) != NULL
) {
116 token
= match_token(p
, tokens
, args
);
117 if (token
< Opt_uname
) {
118 if ((ret
= match_int(&args
[0], &option
)) < 0) {
120 "integer field, but no integer?\n");
127 v9ses
->port
= option
;
130 v9ses
->maxdata
= option
;
139 v9ses
->afid
= option
;
142 v9ses
->rfdno
= option
;
145 v9ses
->wfdno
= option
;
148 v9ses
->debug
= option
;
151 v9ses
->proto
= PROTO_TCP
;
154 v9ses
->proto
= PROTO_UNIX
;
157 v9ses
->proto
= PROTO_FD
;
160 match_strcpy(v9ses
->name
, &args
[0]);
163 match_strcpy(v9ses
->remotename
, &args
[0]);
178 * v9fs_inode2v9ses - safely extract v9fs session info from super block
179 * @inode: inode to extract information from
181 * Paranoid function to extract v9ses information from superblock,
182 * if anything is missing it will report an error.
186 struct v9fs_session_info
*v9fs_inode2v9ses(struct inode
*inode
)
188 return (inode
->i_sb
->s_fs_info
);
192 * v9fs_get_idpool - allocate numeric id from pool
193 * @p - pool to allocate from
195 * XXX - This seems to be an awful generic function, should it be in idr.c with
196 * the lock included in struct idr?
199 int v9fs_get_idpool(struct v9fs_idpool
*p
)
205 if (idr_pre_get(&p
->pool
, GFP_KERNEL
) == 0)
208 if (down_interruptible(&p
->lock
) == -EINTR
) {
209 eprintk(KERN_WARNING
, "Interrupted while locking\n");
213 /* no need to store exactly p, we just need something non-null */
214 error
= idr_get_new(&p
->pool
, p
, &i
);
217 if (error
== -EAGAIN
)
226 * v9fs_put_idpool - release numeric id from pool
227 * @p - pool to allocate from
229 * XXX - This seems to be an awful generic function, should it be in idr.c with
230 * the lock included in struct idr?
233 void v9fs_put_idpool(int id
, struct v9fs_idpool
*p
)
235 if (down_interruptible(&p
->lock
) == -EINTR
) {
236 eprintk(KERN_WARNING
, "Interrupted while locking\n");
239 idr_remove(&p
->pool
, id
);
244 * v9fs_check_idpool - check if the specified id is available
248 int v9fs_check_idpool(int id
, struct v9fs_idpool
*p
)
250 return idr_find(&p
->pool
, id
) != NULL
;
254 * v9fs_session_init - initialize session
255 * @v9ses: session information structure
256 * @dev_name: device being mounted
262 v9fs_session_init(struct v9fs_session_info
*v9ses
,
263 const char *dev_name
, char *data
)
265 struct v9fs_fcall
*fcall
= NULL
;
266 struct v9fs_transport
*trans_proto
;
269 int retval
= -EINVAL
;
270 struct v9fs_str
*version
;
272 v9ses
->name
= __getname();
276 v9ses
->remotename
= __getname();
277 if (!v9ses
->remotename
) {
278 __putname(v9ses
->name
);
282 strcpy(v9ses
->name
, V9FS_DEFUSER
);
283 strcpy(v9ses
->remotename
, V9FS_DEFANAME
);
285 v9fs_parse_options(data
, v9ses
);
287 /* set global debug level */
288 v9fs_debug_level
= v9ses
->debug
;
290 /* id pools that are session-dependent: fids and tags */
291 idr_init(&v9ses
->fidpool
.pool
);
292 init_MUTEX(&v9ses
->fidpool
.lock
);
294 switch (v9ses
->proto
) {
296 trans_proto
= &v9fs_trans_tcp
;
299 trans_proto
= &v9fs_trans_unix
;
300 *v9ses
->remotename
= 0;
303 trans_proto
= &v9fs_trans_fd
;
304 *v9ses
->remotename
= 0;
307 printk(KERN_ERR
"v9fs: Bad mount protocol %d\n", v9ses
->proto
);
308 retval
= -ENOPROTOOPT
;
312 v9ses
->transport
= kmalloc(sizeof(*v9ses
->transport
), GFP_KERNEL
);
313 if (!v9ses
->transport
) {
318 memmove(v9ses
->transport
, trans_proto
, sizeof(*v9ses
->transport
));
320 if ((retval
= v9ses
->transport
->init(v9ses
, dev_name
, data
)) < 0) {
321 eprintk(KERN_ERR
, "problem initializing transport\n");
325 v9ses
->inprogress
= 0;
327 v9ses
->session_hung
= 0;
329 v9ses
->mux
= v9fs_mux_init(v9ses
->transport
, v9ses
->maxdata
+ V9FS_IOHDRSZ
,
332 if (IS_ERR(v9ses
->mux
)) {
333 retval
= PTR_ERR(v9ses
->mux
);
335 dprintk(DEBUG_ERROR
, "problem initializing mux\n");
339 if (v9ses
->afid
== ~0) {
342 v9fs_t_version(v9ses
, v9ses
->maxdata
, "9P2000.u",
345 retval
= v9fs_t_version(v9ses
, v9ses
->maxdata
, "9P2000",
349 dprintk(DEBUG_ERROR
, "v9fs_t_version failed\n");
353 version
= &fcall
->params
.rversion
.version
;
354 if (version
->len
==8 && !memcmp(version
->str
, "9P2000.u", 8)) {
355 dprintk(DEBUG_9P
, "9P2000 UNIX extensions enabled\n");
357 } else if (version
->len
==6 && !memcmp(version
->str
, "9P2000", 6)) {
358 dprintk(DEBUG_9P
, "9P2000 legacy mode enabled\n");
365 n
= fcall
->params
.rversion
.msize
;
368 if (n
< v9ses
->maxdata
)
372 newfid
= v9fs_get_idpool(&v9ses
->fidpool
);
374 eprintk(KERN_WARNING
, "couldn't allocate FID\n");
378 /* it is a little bit ugly, but we have to prevent newfid */
379 /* being the same as afid, so if it is, get a new fid */
380 if (v9ses
->afid
!= ~0 && newfid
== v9ses
->afid
) {
381 newfid
= v9fs_get_idpool(&v9ses
->fidpool
);
383 eprintk(KERN_WARNING
, "couldn't allocate FID\n");
390 v9fs_t_attach(v9ses
, v9ses
->name
, v9ses
->remotename
, newfid
,
393 dprintk(DEBUG_ERROR
, "cannot attach\n");
397 if (v9ses
->afid
!= ~0) {
398 dprintk(DEBUG_ERROR
, "afid not equal to ~0\n");
399 if (v9fs_t_clunk(v9ses
, v9ses
->afid
))
400 dprintk(DEBUG_ERROR
, "clunk failed\n");
409 v9fs_session_close(v9ses
);
414 * v9fs_session_close - shutdown a session
415 * @v9ses: session information structure
419 void v9fs_session_close(struct v9fs_session_info
*v9ses
)
422 v9fs_mux_destroy(v9ses
->mux
);
426 if (v9ses
->transport
) {
427 v9ses
->transport
->close(v9ses
->transport
);
428 kfree(v9ses
->transport
);
429 v9ses
->transport
= NULL
;
432 __putname(v9ses
->name
);
433 __putname(v9ses
->remotename
);
437 * v9fs_session_cancel - mark transport as disconnected
438 * and cancel all pending requests.
440 void v9fs_session_cancel(struct v9fs_session_info
*v9ses
) {
441 dprintk(DEBUG_ERROR
, "cancel session %p\n", v9ses
);
442 v9ses
->transport
->status
= Disconnected
;
443 v9fs_mux_cancel(v9ses
->mux
, -EIO
);
446 extern int v9fs_error_init(void);
449 * v9fs_init - Initialize module
453 static int __init
init_v9fs(void)
459 printk(KERN_INFO
"Installing v9fs 9P2000 file system support\n");
461 ret
= v9fs_mux_global_init();
463 ret
= register_filesystem(&v9fs_fs_type
);
469 * v9fs_init - shutdown module
473 static void __exit
exit_v9fs(void)
475 v9fs_mux_global_exit();
476 unregister_filesystem(&v9fs_fs_type
);
479 module_init(init_v9fs
)
480 module_exit(exit_v9fs
)
482 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
483 MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
484 MODULE_LICENSE("GPL");
This page took 0.045015 seconds and 5 git commands to generate.