4 * This file contains functions assisting in mapping VFS to 9P2000
6 * Copyright (C) 2004-2008 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/errno.h>
31 #include <linux/sched.h>
32 #include <linux/parser.h>
33 #include <linux/idr.h>
34 #include <linux/slab.h>
35 #include <net/9p/9p.h>
36 #include <net/9p/client.h>
37 #include <net/9p/transport.h>
42 static DEFINE_SPINLOCK(v9fs_sessionlist_lock);
43 static LIST_HEAD(v9fs_sessionlist);
44 struct kmem_cache *v9fs_inode_cache;
47 * Option Parsing (code inspired by NFS code)
48 * NOTE: each transport will parse its own options
52 /* Options that take integer arguments */
53 Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
55 Opt_uname, Opt_remotename, Opt_cache, Opt_cachetag,
56 /* Options that take no arguments */
59 Opt_cache_loose, Opt_fscache, Opt_mmap,
61 Opt_access, Opt_posixacl,
62 /* Lock timeout option */
68 static const match_table_t tokens = {
69 {Opt_debug, "debug=%x"},
70 {Opt_dfltuid, "dfltuid=%u"},
71 {Opt_dfltgid, "dfltgid=%u"},
72 {Opt_afid, "afid=%u"},
73 {Opt_uname, "uname=%s"},
74 {Opt_remotename, "aname=%s"},
75 {Opt_nodevmap, "nodevmap"},
76 {Opt_cache, "cache=%s"},
77 {Opt_cache_loose, "loose"},
78 {Opt_fscache, "fscache"},
80 {Opt_cachetag, "cachetag=%s"},
81 {Opt_access, "access=%s"},
82 {Opt_posixacl, "posixacl"},
83 {Opt_locktimeout, "locktimeout=%u"},
87 /* Interpret mount options for cache mode */
88 static int get_cache_mode(char *s)
90 int version = -EINVAL;
92 if (!strcmp(s, "loose")) {
93 version = CACHE_LOOSE;
94 p9_debug(P9_DEBUG_9P, "Cache mode: loose\n");
95 } else if (!strcmp(s, "fscache")) {
96 version = CACHE_FSCACHE;
97 p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n");
98 } else if (!strcmp(s, "mmap")) {
100 p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n");
101 } else if (!strcmp(s, "none")) {
102 version = CACHE_NONE;
103 p9_debug(P9_DEBUG_9P, "Cache mode: none\n");
105 pr_info("Unknown Cache mode %s\n", s);
110 * v9fs_parse_options - parse mount options into session structure
111 * @v9ses: existing v9fs session information
113 * Return 0 upon success, -ERRNO upon failure.
116 static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
118 char *options, *tmp_options;
119 substring_t args[MAX_OPT_ARGS];
128 v9ses->cache = CACHE_NONE;
129 #ifdef CONFIG_9P_FSCACHE
130 v9ses->cachetag = NULL;
132 v9ses->session_lock_timeout = P9_LOCK_TIMEOUT;
137 tmp_options = kstrdup(opts, GFP_KERNEL);
140 goto fail_option_alloc;
142 options = tmp_options;
144 while ((p = strsep(&options, ",")) != NULL) {
148 token = match_token(p, tokens, args);
151 r = match_int(&args[0], &option);
153 p9_debug(P9_DEBUG_ERROR,
154 "integer field, but no integer?\n");
158 v9ses->debug = option;
159 #ifdef CONFIG_NET_9P_DEBUG
160 p9_debug_level = option;
165 r = match_int(&args[0], &option);
167 p9_debug(P9_DEBUG_ERROR,
168 "integer field, but no integer?\n");
172 v9ses->dfltuid = make_kuid(current_user_ns(), option);
173 if (!uid_valid(v9ses->dfltuid)) {
174 p9_debug(P9_DEBUG_ERROR,
175 "uid field, but not a uid?\n");
181 r = match_int(&args[0], &option);
183 p9_debug(P9_DEBUG_ERROR,
184 "integer field, but no integer?\n");
188 v9ses->dfltgid = make_kgid(current_user_ns(), option);
189 if (!gid_valid(v9ses->dfltgid)) {
190 p9_debug(P9_DEBUG_ERROR,
191 "gid field, but not a gid?\n");
197 r = match_int(&args[0], &option);
199 p9_debug(P9_DEBUG_ERROR,
200 "integer field, but no integer?\n");
204 v9ses->afid = option;
208 v9ses->uname = match_strdup(&args[0]);
211 goto free_and_return;
216 v9ses->aname = match_strdup(&args[0]);
219 goto free_and_return;
225 case Opt_cache_loose:
226 v9ses->cache = CACHE_LOOSE;
229 v9ses->cache = CACHE_FSCACHE;
232 v9ses->cache = CACHE_MMAP;
235 #ifdef CONFIG_9P_FSCACHE
236 v9ses->cachetag = match_strdup(&args[0]);
240 s = match_strdup(&args[0]);
243 p9_debug(P9_DEBUG_ERROR,
244 "problem allocating copy of cache arg\n");
245 goto free_and_return;
247 ret = get_cache_mode(s);
248 if (ret == -EINVAL) {
250 goto free_and_return;
258 s = match_strdup(&args[0]);
261 p9_debug(P9_DEBUG_ERROR,
262 "problem allocating copy of access arg\n");
263 goto free_and_return;
266 v9ses->flags &= ~V9FS_ACCESS_MASK;
267 if (strcmp(s, "user") == 0)
268 v9ses->flags |= V9FS_ACCESS_USER;
269 else if (strcmp(s, "any") == 0)
270 v9ses->flags |= V9FS_ACCESS_ANY;
271 else if (strcmp(s, "client") == 0) {
272 v9ses->flags |= V9FS_ACCESS_CLIENT;
275 v9ses->flags |= V9FS_ACCESS_SINGLE;
276 uid = simple_strtoul(s, &e, 10);
279 pr_info("Unknown access argument %s\n",
282 goto free_and_return;
284 v9ses->uid = make_kuid(current_user_ns(), uid);
285 if (!uid_valid(v9ses->uid)) {
287 pr_info("Uknown uid %s\n", s);
289 goto free_and_return;
297 #ifdef CONFIG_9P_FS_POSIX_ACL
298 v9ses->flags |= V9FS_POSIX_ACL;
300 p9_debug(P9_DEBUG_ERROR,
301 "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
305 case Opt_locktimeout:
306 r = match_int(&args[0], &option);
308 p9_debug(P9_DEBUG_ERROR,
309 "integer field, but no integer?\n");
314 p9_debug(P9_DEBUG_ERROR,
315 "locktimeout must be a greater than zero integer.\n");
319 v9ses->session_lock_timeout = (long)option * HZ;
334 * v9fs_session_init - initialize session
335 * @v9ses: session information structure
336 * @dev_name: device being mounted
341 struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
342 const char *dev_name, char *data)
347 v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
351 v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
354 init_rwsem(&v9ses->rename_sem);
356 rc = bdi_setup_and_register(&v9ses->bdi, "9p");
360 v9ses->uid = INVALID_UID;
361 v9ses->dfltuid = V9FS_DEFUID;
362 v9ses->dfltgid = V9FS_DEFGID;
364 v9ses->clnt = p9_client_create(dev_name, data);
365 if (IS_ERR(v9ses->clnt)) {
366 rc = PTR_ERR(v9ses->clnt);
367 p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n");
371 v9ses->flags = V9FS_ACCESS_USER;
373 if (p9_is_proto_dotl(v9ses->clnt)) {
374 v9ses->flags = V9FS_ACCESS_CLIENT;
375 v9ses->flags |= V9FS_PROTO_2000L;
376 } else if (p9_is_proto_dotu(v9ses->clnt)) {
377 v9ses->flags |= V9FS_PROTO_2000U;
380 rc = v9fs_parse_options(v9ses, data);
384 v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
386 if (!v9fs_proto_dotl(v9ses) &&
387 ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
389 * We support ACCESS_CLIENT only for dotl.
390 * Fall back to ACCESS_USER
392 v9ses->flags &= ~V9FS_ACCESS_MASK;
393 v9ses->flags |= V9FS_ACCESS_USER;
396 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
397 if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
398 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
400 v9ses->flags &= ~V9FS_ACCESS_MASK;
401 v9ses->flags |= V9FS_ACCESS_ANY;
402 v9ses->uid = INVALID_UID;
404 if (!v9fs_proto_dotl(v9ses) ||
405 !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
407 * We support ACL checks on clinet only if the protocol is
408 * 9P2000.L and access is V9FS_ACCESS_CLIENT.
410 v9ses->flags &= ~V9FS_ACL_MASK;
413 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, INVALID_UID,
417 p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
421 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
422 fid->uid = v9ses->uid;
424 fid->uid = INVALID_UID;
426 #ifdef CONFIG_9P_FSCACHE
427 /* register the session for caching */
428 v9fs_cache_session_get_cookie(v9ses);
430 spin_lock(&v9fs_sessionlist_lock);
431 list_add(&v9ses->slist, &v9fs_sessionlist);
432 spin_unlock(&v9fs_sessionlist_lock);
437 p9_client_destroy(v9ses->clnt);
439 bdi_destroy(&v9ses->bdi);
447 * v9fs_session_close - shutdown a session
448 * @v9ses: session information structure
452 void v9fs_session_close(struct v9fs_session_info *v9ses)
455 p9_client_destroy(v9ses->clnt);
459 #ifdef CONFIG_9P_FSCACHE
461 v9fs_cache_session_put_cookie(v9ses);
462 kfree(v9ses->cachetag);
467 bdi_destroy(&v9ses->bdi);
469 spin_lock(&v9fs_sessionlist_lock);
470 list_del(&v9ses->slist);
471 spin_unlock(&v9fs_sessionlist_lock);
475 * v9fs_session_cancel - terminate a session
476 * @v9ses: session to terminate
478 * mark transport as disconnected and cancel all pending requests.
481 void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
482 p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
483 p9_client_disconnect(v9ses->clnt);
487 * v9fs_session_begin_cancel - Begin terminate of a session
488 * @v9ses: session to terminate
490 * After this call we don't allow any request other than clunk.
493 void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
495 p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
496 p9_client_begin_disconnect(v9ses->clnt);
499 extern int v9fs_error_init(void);
501 static struct kobject *v9fs_kobj;
503 #ifdef CONFIG_9P_FSCACHE
505 * caches_show - list caches associated with a session
507 * Returns the size of buffer written.
510 static ssize_t caches_show(struct kobject *kobj,
511 struct kobj_attribute *attr,
514 ssize_t n = 0, count = 0, limit = PAGE_SIZE;
515 struct v9fs_session_info *v9ses;
517 spin_lock(&v9fs_sessionlist_lock);
518 list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
519 if (v9ses->cachetag) {
520 n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
531 spin_unlock(&v9fs_sessionlist_lock);
535 static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
536 #endif /* CONFIG_9P_FSCACHE */
538 static struct attribute *v9fs_attrs[] = {
539 #ifdef CONFIG_9P_FSCACHE
540 &v9fs_attr_cache.attr,
545 static struct attribute_group v9fs_attr_group = {
550 * v9fs_sysfs_init - Initialize the v9fs sysfs interface
554 static int __init v9fs_sysfs_init(void)
556 v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
560 if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
561 kobject_put(v9fs_kobj);
569 * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
573 static void v9fs_sysfs_cleanup(void)
575 sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
576 kobject_put(v9fs_kobj);
579 static void v9fs_inode_init_once(void *foo)
581 struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
582 #ifdef CONFIG_9P_FSCACHE
583 v9inode->fscache = NULL;
585 memset(&v9inode->qid, 0, sizeof(v9inode->qid));
586 inode_init_once(&v9inode->vfs_inode);
590 * v9fs_init_inode_cache - initialize a cache for 9P
591 * Returns 0 on success.
593 static int v9fs_init_inode_cache(void)
595 v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
596 sizeof(struct v9fs_inode),
597 0, (SLAB_RECLAIM_ACCOUNT|
598 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
599 v9fs_inode_init_once);
600 if (!v9fs_inode_cache)
607 * v9fs_destroy_inode_cache - destroy the cache of 9P inode
610 static void v9fs_destroy_inode_cache(void)
613 * Make sure all delayed rcu free inodes are flushed before we
617 kmem_cache_destroy(v9fs_inode_cache);
620 static int v9fs_cache_register(void)
623 ret = v9fs_init_inode_cache();
626 #ifdef CONFIG_9P_FSCACHE
627 ret = fscache_register_netfs(&v9fs_cache_netfs);
629 v9fs_destroy_inode_cache();
634 static void v9fs_cache_unregister(void)
636 v9fs_destroy_inode_cache();
637 #ifdef CONFIG_9P_FSCACHE
638 fscache_unregister_netfs(&v9fs_cache_netfs);
643 * init_v9fs - Initialize module
647 static int __init init_v9fs(void)
650 pr_info("Installing v9fs 9p2000 file system support\n");
651 /* TODO: Setup list of registered trasnport modules */
653 err = v9fs_cache_register();
655 pr_err("Failed to register v9fs for caching\n");
659 err = v9fs_sysfs_init();
661 pr_err("Failed to register with sysfs\n");
664 err = register_filesystem(&v9fs_fs_type);
666 pr_err("Failed to register filesystem\n");
667 goto out_sysfs_cleanup;
673 v9fs_sysfs_cleanup();
676 v9fs_cache_unregister();
682 * exit_v9fs - shutdown module
686 static void __exit exit_v9fs(void)
688 v9fs_sysfs_cleanup();
689 v9fs_cache_unregister();
690 unregister_filesystem(&v9fs_fs_type);
693 module_init(init_v9fs)
694 module_exit(exit_v9fs)
696 MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
697 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
698 MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
699 MODULE_LICENSE("GPL");