1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contains functions assisting in mapping VFS to 9P2000
5 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
12 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/cred.h>
16 #include <linux/parser.h>
17 #include <linux/slab.h>
18 #include <linux/seq_file.h>
19 #include <net/9p/9p.h>
20 #include <net/9p/client.h>
21 #include <net/9p/transport.h>
26 static DEFINE_SPINLOCK(v9fs_sessionlist_lock);
27 static LIST_HEAD(v9fs_sessionlist);
28 struct kmem_cache *v9fs_inode_cache;
31 * Option Parsing (code inspired by NFS code)
32 * NOTE: each transport will parse its own options
36 /* Options that take integer arguments */
37 Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
39 Opt_uname, Opt_remotename, Opt_cache, Opt_cachetag,
40 /* Options that take no arguments */
41 Opt_nodevmap, Opt_noxattr, Opt_directio, Opt_ignoreqv,
43 Opt_access, Opt_posixacl,
44 /* Lock timeout option */
50 static const match_table_t tokens = {
51 {Opt_debug, "debug=%x"},
52 {Opt_dfltuid, "dfltuid=%u"},
53 {Opt_dfltgid, "dfltgid=%u"},
54 {Opt_afid, "afid=%u"},
55 {Opt_uname, "uname=%s"},
56 {Opt_remotename, "aname=%s"},
57 {Opt_nodevmap, "nodevmap"},
58 {Opt_noxattr, "noxattr"},
59 {Opt_directio, "directio"},
60 {Opt_ignoreqv, "ignoreqv"},
61 {Opt_cache, "cache=%s"},
62 {Opt_cachetag, "cachetag=%s"},
63 {Opt_access, "access=%s"},
64 {Opt_posixacl, "posixacl"},
65 {Opt_locktimeout, "locktimeout=%u"},
69 /* Interpret mount options for cache mode */
70 static int get_cache_mode(char *s)
72 int version = -EINVAL;
74 if (!strcmp(s, "loose")) {
75 version = CACHE_SC_LOOSE;
76 p9_debug(P9_DEBUG_9P, "Cache mode: loose\n");
77 } else if (!strcmp(s, "fscache")) {
78 version = CACHE_SC_FSCACHE;
79 p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n");
80 } else if (!strcmp(s, "mmap")) {
81 version = CACHE_SC_MMAP;
82 p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n");
83 } else if (!strcmp(s, "readahead")) {
84 version = CACHE_SC_READAHEAD;
85 p9_debug(P9_DEBUG_9P, "Cache mode: readahead\n");
86 } else if (!strcmp(s, "none")) {
87 version = CACHE_SC_NONE;
88 p9_debug(P9_DEBUG_9P, "Cache mode: none\n");
89 } else if (kstrtoint(s, 0, &version) != 0) {
91 pr_info("Unknown Cache mode or invalid value %s\n", s);
97 * Display the mount options in /proc/mounts.
99 int v9fs_show_options(struct seq_file *m, struct dentry *root)
101 struct v9fs_session_info *v9ses = root->d_sb->s_fs_info;
104 seq_printf(m, ",debug=%x", v9ses->debug);
105 if (!uid_eq(v9ses->dfltuid, V9FS_DEFUID))
106 seq_printf(m, ",dfltuid=%u",
107 from_kuid_munged(&init_user_ns, v9ses->dfltuid));
108 if (!gid_eq(v9ses->dfltgid, V9FS_DEFGID))
109 seq_printf(m, ",dfltgid=%u",
110 from_kgid_munged(&init_user_ns, v9ses->dfltgid));
111 if (v9ses->afid != ~0)
112 seq_printf(m, ",afid=%u", v9ses->afid);
113 if (strcmp(v9ses->uname, V9FS_DEFUSER) != 0)
114 seq_printf(m, ",uname=%s", v9ses->uname);
115 if (strcmp(v9ses->aname, V9FS_DEFANAME) != 0)
116 seq_printf(m, ",aname=%s", v9ses->aname);
118 seq_puts(m, ",nodevmap");
120 seq_printf(m, ",cache=%x", v9ses->cache);
121 #ifdef CONFIG_9P_FSCACHE
122 if (v9ses->cachetag && (v9ses->cache & CACHE_FSCACHE))
123 seq_printf(m, ",cachetag=%s", v9ses->cachetag);
126 switch (v9ses->flags & V9FS_ACCESS_MASK) {
127 case V9FS_ACCESS_USER:
128 seq_puts(m, ",access=user");
130 case V9FS_ACCESS_ANY:
131 seq_puts(m, ",access=any");
133 case V9FS_ACCESS_CLIENT:
134 seq_puts(m, ",access=client");
136 case V9FS_ACCESS_SINGLE:
137 seq_printf(m, ",access=%u",
138 from_kuid_munged(&init_user_ns, v9ses->uid));
142 if (v9ses->flags & V9FS_IGNORE_QV)
143 seq_puts(m, ",ignoreqv");
144 if (v9ses->flags & V9FS_DIRECT_IO)
145 seq_puts(m, ",directio");
146 if (v9ses->flags & V9FS_POSIX_ACL)
147 seq_puts(m, ",posixacl");
149 if (v9ses->flags & V9FS_NO_XATTR)
150 seq_puts(m, ",noxattr");
152 return p9_show_client_options(m, v9ses->clnt);
156 * v9fs_parse_options - parse mount options into session structure
157 * @v9ses: existing v9fs session information
158 * @opts: The mount option string
160 * Return 0 upon success, -ERRNO upon failure.
163 static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
165 char *options, *tmp_options;
166 substring_t args[MAX_OPT_ARGS];
175 v9ses->cache = CACHE_NONE;
176 #ifdef CONFIG_9P_FSCACHE
177 v9ses->cachetag = NULL;
179 v9ses->session_lock_timeout = P9_LOCK_TIMEOUT;
184 tmp_options = kstrdup(opts, GFP_KERNEL);
187 goto fail_option_alloc;
189 options = tmp_options;
191 while ((p = strsep(&options, ",")) != NULL) {
197 token = match_token(p, tokens, args);
200 r = match_int(&args[0], &option);
202 p9_debug(P9_DEBUG_ERROR,
203 "integer field, but no integer?\n");
206 v9ses->debug = option;
207 #ifdef CONFIG_NET_9P_DEBUG
208 p9_debug_level = option;
214 r = match_int(&args[0], &option);
216 p9_debug(P9_DEBUG_ERROR,
217 "integer field, but no integer?\n");
221 v9ses->dfltuid = make_kuid(current_user_ns(), option);
222 if (!uid_valid(v9ses->dfltuid)) {
223 p9_debug(P9_DEBUG_ERROR,
224 "uid field, but not a uid?\n");
229 r = match_int(&args[0], &option);
231 p9_debug(P9_DEBUG_ERROR,
232 "integer field, but no integer?\n");
236 v9ses->dfltgid = make_kgid(current_user_ns(), option);
237 if (!gid_valid(v9ses->dfltgid)) {
238 p9_debug(P9_DEBUG_ERROR,
239 "gid field, but not a gid?\n");
244 r = match_int(&args[0], &option);
246 p9_debug(P9_DEBUG_ERROR,
247 "integer field, but no integer?\n");
250 v9ses->afid = option;
255 v9ses->uname = match_strdup(&args[0]);
258 goto free_and_return;
263 v9ses->aname = match_strdup(&args[0]);
266 goto free_and_return;
273 v9ses->flags |= V9FS_NO_XATTR;
276 v9ses->flags |= V9FS_DIRECT_IO;
279 v9ses->flags |= V9FS_IGNORE_QV;
282 #ifdef CONFIG_9P_FSCACHE
283 kfree(v9ses->cachetag);
284 v9ses->cachetag = match_strdup(&args[0]);
285 if (!v9ses->cachetag) {
287 goto free_and_return;
292 s = match_strdup(&args[0]);
295 p9_debug(P9_DEBUG_ERROR,
296 "problem allocating copy of cache arg\n");
297 goto free_and_return;
299 r = get_cache_mode(s);
309 s = match_strdup(&args[0]);
312 p9_debug(P9_DEBUG_ERROR,
313 "problem allocating copy of access arg\n");
314 goto free_and_return;
317 v9ses->flags &= ~V9FS_ACCESS_MASK;
318 if (strcmp(s, "user") == 0)
319 v9ses->flags |= V9FS_ACCESS_USER;
320 else if (strcmp(s, "any") == 0)
321 v9ses->flags |= V9FS_ACCESS_ANY;
322 else if (strcmp(s, "client") == 0) {
323 v9ses->flags |= V9FS_ACCESS_CLIENT;
327 v9ses->flags |= V9FS_ACCESS_SINGLE;
328 r = kstrtouint(s, 10, &uid);
331 pr_info("Unknown access argument %s: %d\n",
336 v9ses->uid = make_kuid(current_user_ns(), uid);
337 if (!uid_valid(v9ses->uid)) {
339 pr_info("Unknown uid %s\n", s);
347 #ifdef CONFIG_9P_FS_POSIX_ACL
348 v9ses->flags |= V9FS_POSIX_ACL;
350 p9_debug(P9_DEBUG_ERROR,
351 "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
355 case Opt_locktimeout:
356 r = match_int(&args[0], &option);
358 p9_debug(P9_DEBUG_ERROR,
359 "integer field, but no integer?\n");
364 p9_debug(P9_DEBUG_ERROR,
365 "locktimeout must be a greater than zero integer.\n");
369 v9ses->session_lock_timeout = (long)option * HZ;
384 * v9fs_session_init - initialize session
385 * @v9ses: session information structure
386 * @dev_name: device being mounted
391 struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
392 const char *dev_name, char *data)
397 v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
401 v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
404 init_rwsem(&v9ses->rename_sem);
406 v9ses->uid = INVALID_UID;
407 v9ses->dfltuid = V9FS_DEFUID;
408 v9ses->dfltgid = V9FS_DEFGID;
410 v9ses->clnt = p9_client_create(dev_name, data);
411 if (IS_ERR(v9ses->clnt)) {
412 rc = PTR_ERR(v9ses->clnt);
413 p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n");
417 v9ses->flags = V9FS_ACCESS_USER;
419 if (p9_is_proto_dotl(v9ses->clnt)) {
420 v9ses->flags = V9FS_ACCESS_CLIENT;
421 v9ses->flags |= V9FS_PROTO_2000L;
422 } else if (p9_is_proto_dotu(v9ses->clnt)) {
423 v9ses->flags |= V9FS_PROTO_2000U;
426 rc = v9fs_parse_options(v9ses, data);
430 v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
432 if (!v9fs_proto_dotl(v9ses) &&
433 ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
435 * We support ACCESS_CLIENT only for dotl.
436 * Fall back to ACCESS_USER
438 v9ses->flags &= ~V9FS_ACCESS_MASK;
439 v9ses->flags |= V9FS_ACCESS_USER;
442 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
443 if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
444 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
446 v9ses->flags &= ~V9FS_ACCESS_MASK;
447 v9ses->flags |= V9FS_ACCESS_ANY;
448 v9ses->uid = INVALID_UID;
450 if (!v9fs_proto_dotl(v9ses) ||
451 !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
453 * We support ACL checks on clinet only if the protocol is
454 * 9P2000.L and access is V9FS_ACCESS_CLIENT.
456 v9ses->flags &= ~V9FS_ACL_MASK;
459 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, INVALID_UID,
463 p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
467 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
468 fid->uid = v9ses->uid;
470 fid->uid = INVALID_UID;
472 #ifdef CONFIG_9P_FSCACHE
473 /* register the session for caching */
474 if (v9ses->cache & CACHE_FSCACHE) {
475 rc = v9fs_cache_session_get_cookie(v9ses, dev_name);
480 spin_lock(&v9fs_sessionlist_lock);
481 list_add(&v9ses->slist, &v9fs_sessionlist);
482 spin_unlock(&v9fs_sessionlist_lock);
487 #ifdef CONFIG_9P_FSCACHE
488 kfree(v9ses->cachetag);
490 p9_client_destroy(v9ses->clnt);
498 * v9fs_session_close - shutdown a session
499 * @v9ses: session information structure
503 void v9fs_session_close(struct v9fs_session_info *v9ses)
506 p9_client_destroy(v9ses->clnt);
510 #ifdef CONFIG_9P_FSCACHE
511 fscache_relinquish_volume(v9fs_session_cache(v9ses), NULL, false);
512 kfree(v9ses->cachetag);
517 spin_lock(&v9fs_sessionlist_lock);
518 list_del(&v9ses->slist);
519 spin_unlock(&v9fs_sessionlist_lock);
523 * v9fs_session_cancel - terminate a session
524 * @v9ses: session to terminate
526 * mark transport as disconnected and cancel all pending requests.
529 void v9fs_session_cancel(struct v9fs_session_info *v9ses)
531 p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
532 p9_client_disconnect(v9ses->clnt);
536 * v9fs_session_begin_cancel - Begin terminate of a session
537 * @v9ses: session to terminate
539 * After this call we don't allow any request other than clunk.
542 void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
544 p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
545 p9_client_begin_disconnect(v9ses->clnt);
548 static struct kobject *v9fs_kobj;
550 #ifdef CONFIG_9P_FSCACHE
552 * List caches associated with a session
554 static ssize_t caches_show(struct kobject *kobj,
555 struct kobj_attribute *attr,
558 ssize_t n = 0, count = 0, limit = PAGE_SIZE;
559 struct v9fs_session_info *v9ses;
561 spin_lock(&v9fs_sessionlist_lock);
562 list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
563 if (v9ses->cachetag) {
564 n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
575 spin_unlock(&v9fs_sessionlist_lock);
579 static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
580 #endif /* CONFIG_9P_FSCACHE */
582 static struct attribute *v9fs_attrs[] = {
583 #ifdef CONFIG_9P_FSCACHE
584 &v9fs_attr_cache.attr,
589 static const struct attribute_group v9fs_attr_group = {
594 * v9fs_sysfs_init - Initialize the v9fs sysfs interface
598 static int __init v9fs_sysfs_init(void)
600 v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
604 if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
605 kobject_put(v9fs_kobj);
613 * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
617 static void v9fs_sysfs_cleanup(void)
619 sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
620 kobject_put(v9fs_kobj);
623 static void v9fs_inode_init_once(void *foo)
625 struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
627 memset(&v9inode->qid, 0, sizeof(v9inode->qid));
628 inode_init_once(&v9inode->netfs.inode);
632 * v9fs_init_inode_cache - initialize a cache for 9P
633 * Returns 0 on success.
635 static int v9fs_init_inode_cache(void)
637 v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
638 sizeof(struct v9fs_inode),
639 0, (SLAB_RECLAIM_ACCOUNT|
640 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
641 v9fs_inode_init_once);
642 if (!v9fs_inode_cache)
649 * v9fs_destroy_inode_cache - destroy the cache of 9P inode
652 static void v9fs_destroy_inode_cache(void)
655 * Make sure all delayed rcu free inodes are flushed before we
659 kmem_cache_destroy(v9fs_inode_cache);
662 static int v9fs_cache_register(void)
666 ret = v9fs_init_inode_cache();
672 static void v9fs_cache_unregister(void)
674 v9fs_destroy_inode_cache();
678 * init_v9fs - Initialize module
682 static int __init init_v9fs(void)
686 pr_info("Installing v9fs 9p2000 file system support\n");
687 /* TODO: Setup list of registered trasnport modules */
689 err = v9fs_cache_register();
691 pr_err("Failed to register v9fs for caching\n");
695 err = v9fs_sysfs_init();
697 pr_err("Failed to register with sysfs\n");
700 err = register_filesystem(&v9fs_fs_type);
702 pr_err("Failed to register filesystem\n");
703 goto out_sysfs_cleanup;
709 v9fs_sysfs_cleanup();
712 v9fs_cache_unregister();
718 * exit_v9fs - shutdown module
722 static void __exit exit_v9fs(void)
724 v9fs_sysfs_cleanup();
725 v9fs_cache_unregister();
726 unregister_filesystem(&v9fs_fs_type);
729 module_init(init_v9fs)
730 module_exit(exit_v9fs)
732 MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
733 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
734 MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
735 MODULE_LICENSE("GPL");