1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contains functions assisting in mapping VFS to 9P2000
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 */
43 Opt_cache_loose, Opt_fscache, Opt_mmap,
45 Opt_access, Opt_posixacl,
46 /* Lock timeout option */
52 static const match_table_t tokens = {
53 {Opt_debug, "debug=%x"},
54 {Opt_dfltuid, "dfltuid=%u"},
55 {Opt_dfltgid, "dfltgid=%u"},
56 {Opt_afid, "afid=%u"},
57 {Opt_uname, "uname=%s"},
58 {Opt_remotename, "aname=%s"},
59 {Opt_nodevmap, "nodevmap"},
60 {Opt_cache, "cache=%s"},
61 {Opt_cache_loose, "loose"},
62 {Opt_fscache, "fscache"},
64 {Opt_cachetag, "cachetag=%s"},
65 {Opt_access, "access=%s"},
66 {Opt_posixacl, "posixacl"},
67 {Opt_locktimeout, "locktimeout=%u"},
71 static const char *const v9fs_cache_modes[nr__p9_cache_modes] = {
72 [CACHE_NONE] = "none",
73 [CACHE_MMAP] = "mmap",
74 [CACHE_LOOSE] = "loose",
75 [CACHE_FSCACHE] = "fscache",
78 /* Interpret mount options for cache mode */
79 static int get_cache_mode(char *s)
81 int version = -EINVAL;
83 if (!strcmp(s, "loose")) {
84 version = CACHE_LOOSE;
85 p9_debug(P9_DEBUG_9P, "Cache mode: loose\n");
86 } else if (!strcmp(s, "fscache")) {
87 version = CACHE_FSCACHE;
88 p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n");
89 } else if (!strcmp(s, "mmap")) {
91 p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n");
92 } else if (!strcmp(s, "none")) {
94 p9_debug(P9_DEBUG_9P, "Cache mode: none\n");
96 pr_info("Unknown Cache mode %s\n", s);
101 * Display the mount options in /proc/mounts.
103 int v9fs_show_options(struct seq_file *m, struct dentry *root)
105 struct v9fs_session_info *v9ses = root->d_sb->s_fs_info;
108 seq_printf(m, ",debug=%x", v9ses->debug);
109 if (!uid_eq(v9ses->dfltuid, V9FS_DEFUID))
110 seq_printf(m, ",dfltuid=%u",
111 from_kuid_munged(&init_user_ns, v9ses->dfltuid));
112 if (!gid_eq(v9ses->dfltgid, V9FS_DEFGID))
113 seq_printf(m, ",dfltgid=%u",
114 from_kgid_munged(&init_user_ns, v9ses->dfltgid));
115 if (v9ses->afid != ~0)
116 seq_printf(m, ",afid=%u", v9ses->afid);
117 if (strcmp(v9ses->uname, V9FS_DEFUSER) != 0)
118 seq_printf(m, ",uname=%s", v9ses->uname);
119 if (strcmp(v9ses->aname, V9FS_DEFANAME) != 0)
120 seq_printf(m, ",aname=%s", v9ses->aname);
122 seq_puts(m, ",nodevmap");
124 seq_printf(m, ",%s", v9fs_cache_modes[v9ses->cache]);
125 #ifdef CONFIG_9P_FSCACHE
126 if (v9ses->cachetag && v9ses->cache == CACHE_FSCACHE)
127 seq_printf(m, ",cachetag=%s", v9ses->cachetag);
130 switch (v9ses->flags & V9FS_ACCESS_MASK) {
131 case V9FS_ACCESS_USER:
132 seq_puts(m, ",access=user");
134 case V9FS_ACCESS_ANY:
135 seq_puts(m, ",access=any");
137 case V9FS_ACCESS_CLIENT:
138 seq_puts(m, ",access=client");
140 case V9FS_ACCESS_SINGLE:
141 seq_printf(m, ",access=%u",
142 from_kuid_munged(&init_user_ns, v9ses->uid));
146 if (v9ses->flags & V9FS_POSIX_ACL)
147 seq_puts(m, ",posixacl");
149 return p9_show_client_options(m, v9ses->clnt);
153 * v9fs_parse_options - parse mount options into session structure
154 * @v9ses: existing v9fs session information
155 * @opts: The mount option string
157 * Return 0 upon success, -ERRNO upon failure.
160 static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
162 char *options, *tmp_options;
163 substring_t args[MAX_OPT_ARGS];
172 v9ses->cache = CACHE_NONE;
173 #ifdef CONFIG_9P_FSCACHE
174 v9ses->cachetag = NULL;
176 v9ses->session_lock_timeout = P9_LOCK_TIMEOUT;
181 tmp_options = kstrdup(opts, GFP_KERNEL);
184 goto fail_option_alloc;
186 options = tmp_options;
188 while ((p = strsep(&options, ",")) != NULL) {
194 token = match_token(p, tokens, args);
197 r = match_int(&args[0], &option);
199 p9_debug(P9_DEBUG_ERROR,
200 "integer field, but no integer?\n");
203 v9ses->debug = option;
204 #ifdef CONFIG_NET_9P_DEBUG
205 p9_debug_level = option;
211 r = match_int(&args[0], &option);
213 p9_debug(P9_DEBUG_ERROR,
214 "integer field, but no integer?\n");
218 v9ses->dfltuid = make_kuid(current_user_ns(), option);
219 if (!uid_valid(v9ses->dfltuid)) {
220 p9_debug(P9_DEBUG_ERROR,
221 "uid field, but not a uid?\n");
226 r = match_int(&args[0], &option);
228 p9_debug(P9_DEBUG_ERROR,
229 "integer field, but no integer?\n");
233 v9ses->dfltgid = make_kgid(current_user_ns(), option);
234 if (!gid_valid(v9ses->dfltgid)) {
235 p9_debug(P9_DEBUG_ERROR,
236 "gid field, but not a gid?\n");
241 r = match_int(&args[0], &option);
243 p9_debug(P9_DEBUG_ERROR,
244 "integer field, but no integer?\n");
247 v9ses->afid = option;
252 v9ses->uname = match_strdup(&args[0]);
255 goto free_and_return;
260 v9ses->aname = match_strdup(&args[0]);
263 goto free_and_return;
269 case Opt_cache_loose:
270 v9ses->cache = CACHE_LOOSE;
273 v9ses->cache = CACHE_FSCACHE;
276 v9ses->cache = CACHE_MMAP;
279 #ifdef CONFIG_9P_FSCACHE
280 kfree(v9ses->cachetag);
281 v9ses->cachetag = match_strdup(&args[0]);
282 if (!v9ses->cachetag) {
284 goto free_and_return;
289 s = match_strdup(&args[0]);
292 p9_debug(P9_DEBUG_ERROR,
293 "problem allocating copy of cache arg\n");
294 goto free_and_return;
296 r = get_cache_mode(s);
306 s = match_strdup(&args[0]);
309 p9_debug(P9_DEBUG_ERROR,
310 "problem allocating copy of access arg\n");
311 goto free_and_return;
314 v9ses->flags &= ~V9FS_ACCESS_MASK;
315 if (strcmp(s, "user") == 0)
316 v9ses->flags |= V9FS_ACCESS_USER;
317 else if (strcmp(s, "any") == 0)
318 v9ses->flags |= V9FS_ACCESS_ANY;
319 else if (strcmp(s, "client") == 0) {
320 v9ses->flags |= V9FS_ACCESS_CLIENT;
324 v9ses->flags |= V9FS_ACCESS_SINGLE;
325 r = kstrtouint(s, 10, &uid);
328 pr_info("Unknown access argument %s: %d\n",
333 v9ses->uid = make_kuid(current_user_ns(), uid);
334 if (!uid_valid(v9ses->uid)) {
336 pr_info("Unknown uid %s\n", s);
344 #ifdef CONFIG_9P_FS_POSIX_ACL
345 v9ses->flags |= V9FS_POSIX_ACL;
347 p9_debug(P9_DEBUG_ERROR,
348 "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
352 case Opt_locktimeout:
353 r = match_int(&args[0], &option);
355 p9_debug(P9_DEBUG_ERROR,
356 "integer field, but no integer?\n");
361 p9_debug(P9_DEBUG_ERROR,
362 "locktimeout must be a greater than zero integer.\n");
366 v9ses->session_lock_timeout = (long)option * HZ;
381 * v9fs_session_init - initialize session
382 * @v9ses: session information structure
383 * @dev_name: device being mounted
388 struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
389 const char *dev_name, char *data)
394 v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
398 v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
401 init_rwsem(&v9ses->rename_sem);
403 v9ses->uid = INVALID_UID;
404 v9ses->dfltuid = V9FS_DEFUID;
405 v9ses->dfltgid = V9FS_DEFGID;
407 v9ses->clnt = p9_client_create(dev_name, data);
408 if (IS_ERR(v9ses->clnt)) {
409 rc = PTR_ERR(v9ses->clnt);
410 p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n");
414 v9ses->flags = V9FS_ACCESS_USER;
416 if (p9_is_proto_dotl(v9ses->clnt)) {
417 v9ses->flags = V9FS_ACCESS_CLIENT;
418 v9ses->flags |= V9FS_PROTO_2000L;
419 } else if (p9_is_proto_dotu(v9ses->clnt)) {
420 v9ses->flags |= V9FS_PROTO_2000U;
423 rc = v9fs_parse_options(v9ses, data);
427 v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
429 if (!v9fs_proto_dotl(v9ses) &&
430 ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
432 * We support ACCESS_CLIENT only for dotl.
433 * Fall back to ACCESS_USER
435 v9ses->flags &= ~V9FS_ACCESS_MASK;
436 v9ses->flags |= V9FS_ACCESS_USER;
439 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
440 if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
441 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
443 v9ses->flags &= ~V9FS_ACCESS_MASK;
444 v9ses->flags |= V9FS_ACCESS_ANY;
445 v9ses->uid = INVALID_UID;
447 if (!v9fs_proto_dotl(v9ses) ||
448 !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
450 * We support ACL checks on clinet only if the protocol is
451 * 9P2000.L and access is V9FS_ACCESS_CLIENT.
453 v9ses->flags &= ~V9FS_ACL_MASK;
456 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, INVALID_UID,
460 p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
464 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
465 fid->uid = v9ses->uid;
467 fid->uid = INVALID_UID;
469 #ifdef CONFIG_9P_FSCACHE
470 /* register the session for caching */
471 if (v9ses->cache == CACHE_FSCACHE) {
472 rc = v9fs_cache_session_get_cookie(v9ses, dev_name);
477 spin_lock(&v9fs_sessionlist_lock);
478 list_add(&v9ses->slist, &v9fs_sessionlist);
479 spin_unlock(&v9fs_sessionlist_lock);
484 #ifdef CONFIG_9P_FSCACHE
485 kfree(v9ses->cachetag);
487 p9_client_destroy(v9ses->clnt);
495 * v9fs_session_close - shutdown a session
496 * @v9ses: session information structure
500 void v9fs_session_close(struct v9fs_session_info *v9ses)
503 p9_client_destroy(v9ses->clnt);
507 #ifdef CONFIG_9P_FSCACHE
508 fscache_relinquish_volume(v9fs_session_cache(v9ses), NULL, false);
509 kfree(v9ses->cachetag);
514 spin_lock(&v9fs_sessionlist_lock);
515 list_del(&v9ses->slist);
516 spin_unlock(&v9fs_sessionlist_lock);
520 * v9fs_session_cancel - terminate a session
521 * @v9ses: session to terminate
523 * mark transport as disconnected and cancel all pending requests.
526 void v9fs_session_cancel(struct v9fs_session_info *v9ses)
528 p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
529 p9_client_disconnect(v9ses->clnt);
533 * v9fs_session_begin_cancel - Begin terminate of a session
534 * @v9ses: session to terminate
536 * After this call we don't allow any request other than clunk.
539 void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
541 p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
542 p9_client_begin_disconnect(v9ses->clnt);
545 extern int v9fs_error_init(void);
547 static struct kobject *v9fs_kobj;
549 #ifdef CONFIG_9P_FSCACHE
551 * List caches associated with a session
553 static ssize_t caches_show(struct kobject *kobj,
554 struct kobj_attribute *attr,
557 ssize_t n = 0, count = 0, limit = PAGE_SIZE;
558 struct v9fs_session_info *v9ses;
560 spin_lock(&v9fs_sessionlist_lock);
561 list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
562 if (v9ses->cachetag) {
563 n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
574 spin_unlock(&v9fs_sessionlist_lock);
578 static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
579 #endif /* CONFIG_9P_FSCACHE */
581 static struct attribute *v9fs_attrs[] = {
582 #ifdef CONFIG_9P_FSCACHE
583 &v9fs_attr_cache.attr,
588 static const struct attribute_group v9fs_attr_group = {
593 * v9fs_sysfs_init - Initialize the v9fs sysfs interface
597 static int __init v9fs_sysfs_init(void)
599 v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
603 if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
604 kobject_put(v9fs_kobj);
612 * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
616 static void v9fs_sysfs_cleanup(void)
618 sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
619 kobject_put(v9fs_kobj);
622 static void v9fs_inode_init_once(void *foo)
624 struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
626 memset(&v9inode->qid, 0, sizeof(v9inode->qid));
627 inode_init_once(&v9inode->netfs.inode);
631 * v9fs_init_inode_cache - initialize a cache for 9P
632 * Returns 0 on success.
634 static int v9fs_init_inode_cache(void)
636 v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
637 sizeof(struct v9fs_inode),
638 0, (SLAB_RECLAIM_ACCOUNT|
639 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
640 v9fs_inode_init_once);
641 if (!v9fs_inode_cache)
648 * v9fs_destroy_inode_cache - destroy the cache of 9P inode
651 static void v9fs_destroy_inode_cache(void)
654 * Make sure all delayed rcu free inodes are flushed before we
658 kmem_cache_destroy(v9fs_inode_cache);
661 static int v9fs_cache_register(void)
665 ret = v9fs_init_inode_cache();
671 static void v9fs_cache_unregister(void)
673 v9fs_destroy_inode_cache();
677 * init_v9fs - Initialize module
681 static int __init init_v9fs(void)
685 pr_info("Installing v9fs 9p2000 file system support\n");
686 /* TODO: Setup list of registered trasnport modules */
688 err = v9fs_cache_register();
690 pr_err("Failed to register v9fs for caching\n");
694 err = v9fs_sysfs_init();
696 pr_err("Failed to register with sysfs\n");
699 err = register_filesystem(&v9fs_fs_type);
701 pr_err("Failed to register filesystem\n");
702 goto out_sysfs_cleanup;
708 v9fs_sysfs_cleanup();
711 v9fs_cache_unregister();
717 * exit_v9fs - shutdown module
721 static void __exit exit_v9fs(void)
723 v9fs_sysfs_cleanup();
724 v9fs_cache_unregister();
725 unregister_filesystem(&v9fs_fs_type);
728 module_init(init_v9fs)
729 module_exit(exit_v9fs)
734 MODULE_LICENSE("GPL");