arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / 9p / v9fs.c
bloba8ff430686196078b2b39e3fa38eb531eca93a17
1 /*
2 * linux/fs/9p/v9fs.c
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>
30 #include <linux/fs.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>
38 #include "v9fs.h"
39 #include "v9fs_vfs.h"
40 #include "cache.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
51 enum {
52 /* Options that take integer arguments */
53 Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
54 /* String options */
55 Opt_uname, Opt_remotename, Opt_cache, Opt_cachetag,
56 /* Options that take no arguments */
57 Opt_nodevmap,
58 /* Cache options */
59 Opt_cache_loose, Opt_fscache, Opt_mmap,
60 /* Access options */
61 Opt_access, Opt_posixacl,
62 /* Lock timeout option */
63 Opt_locktimeout,
64 /* Error token */
65 Opt_err
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"},
79 {Opt_mmap, "mmap"},
80 {Opt_cachetag, "cachetag=%s"},
81 {Opt_access, "access=%s"},
82 {Opt_posixacl, "posixacl"},
83 {Opt_locktimeout, "locktimeout=%u"},
84 {Opt_err, NULL}
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")) {
99 version = CACHE_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");
104 } else
105 pr_info("Unknown Cache mode %s\n", s);
106 return version;
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];
120 char *p;
121 int option = 0;
122 char *s, *e;
123 int ret = 0;
125 /* setup defaults */
126 v9ses->afid = ~0;
127 v9ses->debug = 0;
128 v9ses->cache = CACHE_NONE;
129 #ifdef CONFIG_9P_FSCACHE
130 v9ses->cachetag = NULL;
131 #endif
132 v9ses->session_lock_timeout = P9_LOCK_TIMEOUT;
134 if (!opts)
135 return 0;
137 tmp_options = kstrdup(opts, GFP_KERNEL);
138 if (!tmp_options) {
139 ret = -ENOMEM;
140 goto fail_option_alloc;
142 options = tmp_options;
144 while ((p = strsep(&options, ",")) != NULL) {
145 int token, r;
146 if (!*p)
147 continue;
148 token = match_token(p, tokens, args);
149 switch (token) {
150 case Opt_debug:
151 r = match_int(&args[0], &option);
152 if (r < 0) {
153 p9_debug(P9_DEBUG_ERROR,
154 "integer field, but no integer?\n");
155 ret = r;
156 continue;
158 v9ses->debug = option;
159 #ifdef CONFIG_NET_9P_DEBUG
160 p9_debug_level = option;
161 #endif
162 break;
164 case Opt_dfltuid:
165 r = match_int(&args[0], &option);
166 if (r < 0) {
167 p9_debug(P9_DEBUG_ERROR,
168 "integer field, but no integer?\n");
169 ret = r;
170 continue;
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");
176 ret = -EINVAL;
177 continue;
179 break;
180 case Opt_dfltgid:
181 r = match_int(&args[0], &option);
182 if (r < 0) {
183 p9_debug(P9_DEBUG_ERROR,
184 "integer field, but no integer?\n");
185 ret = r;
186 continue;
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");
192 ret = -EINVAL;
193 continue;
195 break;
196 case Opt_afid:
197 r = match_int(&args[0], &option);
198 if (r < 0) {
199 p9_debug(P9_DEBUG_ERROR,
200 "integer field, but no integer?\n");
201 ret = r;
202 continue;
204 v9ses->afid = option;
205 break;
206 case Opt_uname:
207 kfree(v9ses->uname);
208 v9ses->uname = match_strdup(&args[0]);
209 if (!v9ses->uname) {
210 ret = -ENOMEM;
211 goto free_and_return;
213 break;
214 case Opt_remotename:
215 kfree(v9ses->aname);
216 v9ses->aname = match_strdup(&args[0]);
217 if (!v9ses->aname) {
218 ret = -ENOMEM;
219 goto free_and_return;
221 break;
222 case Opt_nodevmap:
223 v9ses->nodev = 1;
224 break;
225 case Opt_cache_loose:
226 v9ses->cache = CACHE_LOOSE;
227 break;
228 case Opt_fscache:
229 v9ses->cache = CACHE_FSCACHE;
230 break;
231 case Opt_mmap:
232 v9ses->cache = CACHE_MMAP;
233 break;
234 case Opt_cachetag:
235 #ifdef CONFIG_9P_FSCACHE
236 v9ses->cachetag = match_strdup(&args[0]);
237 #endif
238 break;
239 case Opt_cache:
240 s = match_strdup(&args[0]);
241 if (!s) {
242 ret = -ENOMEM;
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) {
249 kfree(s);
250 goto free_and_return;
253 v9ses->cache = ret;
254 kfree(s);
255 break;
257 case Opt_access:
258 s = match_strdup(&args[0]);
259 if (!s) {
260 ret = -ENOMEM;
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;
273 } else {
274 uid_t uid;
275 v9ses->flags |= V9FS_ACCESS_SINGLE;
276 uid = simple_strtoul(s, &e, 10);
277 if (*e != '\0') {
278 ret = -EINVAL;
279 pr_info("Unknown access argument %s\n",
281 kfree(s);
282 goto free_and_return;
284 v9ses->uid = make_kuid(current_user_ns(), uid);
285 if (!uid_valid(v9ses->uid)) {
286 ret = -EINVAL;
287 pr_info("Uknown uid %s\n", s);
288 kfree(s);
289 goto free_and_return;
293 kfree(s);
294 break;
296 case Opt_posixacl:
297 #ifdef CONFIG_9P_FS_POSIX_ACL
298 v9ses->flags |= V9FS_POSIX_ACL;
299 #else
300 p9_debug(P9_DEBUG_ERROR,
301 "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
302 #endif
303 break;
305 case Opt_locktimeout:
306 r = match_int(&args[0], &option);
307 if (r < 0) {
308 p9_debug(P9_DEBUG_ERROR,
309 "integer field, but no integer?\n");
310 ret = r;
311 continue;
313 if (option < 1) {
314 p9_debug(P9_DEBUG_ERROR,
315 "locktimeout must be a greater than zero integer.\n");
316 ret = -EINVAL;
317 continue;
319 v9ses->session_lock_timeout = (long)option * HZ;
320 break;
322 default:
323 continue;
327 free_and_return:
328 kfree(tmp_options);
329 fail_option_alloc:
330 return ret;
334 * v9fs_session_init - initialize session
335 * @v9ses: session information structure
336 * @dev_name: device being mounted
337 * @data: options
341 struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
342 const char *dev_name, char *data)
344 struct p9_fid *fid;
345 int rc = -ENOMEM;
347 v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
348 if (!v9ses->uname)
349 goto err_names;
351 v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
352 if (!v9ses->aname)
353 goto err_names;
354 init_rwsem(&v9ses->rename_sem);
356 rc = bdi_setup_and_register(&v9ses->bdi, "9p");
357 if (rc)
358 goto err_names;
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");
368 goto err_bdi;
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);
381 if (rc < 0)
382 goto err_clnt;
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;
395 /*FIXME !! */
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,
414 v9ses->aname);
415 if (IS_ERR(fid)) {
416 rc = PTR_ERR(fid);
417 p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
418 goto err_clnt;
421 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
422 fid->uid = v9ses->uid;
423 else
424 fid->uid = INVALID_UID;
426 #ifdef CONFIG_9P_FSCACHE
427 /* register the session for caching */
428 v9fs_cache_session_get_cookie(v9ses);
429 #endif
430 spin_lock(&v9fs_sessionlist_lock);
431 list_add(&v9ses->slist, &v9fs_sessionlist);
432 spin_unlock(&v9fs_sessionlist_lock);
434 return fid;
436 err_clnt:
437 p9_client_destroy(v9ses->clnt);
438 err_bdi:
439 bdi_destroy(&v9ses->bdi);
440 err_names:
441 kfree(v9ses->uname);
442 kfree(v9ses->aname);
443 return ERR_PTR(rc);
447 * v9fs_session_close - shutdown a session
448 * @v9ses: session information structure
452 void v9fs_session_close(struct v9fs_session_info *v9ses)
454 if (v9ses->clnt) {
455 p9_client_destroy(v9ses->clnt);
456 v9ses->clnt = NULL;
459 #ifdef CONFIG_9P_FSCACHE
460 if (v9ses->fscache) {
461 v9fs_cache_session_put_cookie(v9ses);
462 kfree(v9ses->cachetag);
464 #endif
465 kfree(v9ses->uname);
466 kfree(v9ses->aname);
468 bdi_destroy(&v9ses->bdi);
470 spin_lock(&v9fs_sessionlist_lock);
471 list_del(&v9ses->slist);
472 spin_unlock(&v9fs_sessionlist_lock);
476 * v9fs_session_cancel - terminate a session
477 * @v9ses: session to terminate
479 * mark transport as disconnected and cancel all pending requests.
482 void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
483 p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
484 p9_client_disconnect(v9ses->clnt);
488 * v9fs_session_begin_cancel - Begin terminate of a session
489 * @v9ses: session to terminate
491 * After this call we don't allow any request other than clunk.
494 void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
496 p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
497 p9_client_begin_disconnect(v9ses->clnt);
500 extern int v9fs_error_init(void);
502 static struct kobject *v9fs_kobj;
504 #ifdef CONFIG_9P_FSCACHE
506 * caches_show - list caches associated with a session
508 * Returns the size of buffer written.
511 static ssize_t caches_show(struct kobject *kobj,
512 struct kobj_attribute *attr,
513 char *buf)
515 ssize_t n = 0, count = 0, limit = PAGE_SIZE;
516 struct v9fs_session_info *v9ses;
518 spin_lock(&v9fs_sessionlist_lock);
519 list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
520 if (v9ses->cachetag) {
521 n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
522 if (n < 0) {
523 count = n;
524 break;
527 count += n;
528 limit -= n;
532 spin_unlock(&v9fs_sessionlist_lock);
533 return count;
536 static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
537 #endif /* CONFIG_9P_FSCACHE */
539 static struct attribute *v9fs_attrs[] = {
540 #ifdef CONFIG_9P_FSCACHE
541 &v9fs_attr_cache.attr,
542 #endif
543 NULL,
546 static struct attribute_group v9fs_attr_group = {
547 .attrs = v9fs_attrs,
551 * v9fs_sysfs_init - Initialize the v9fs sysfs interface
555 static int __init v9fs_sysfs_init(void)
557 v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
558 if (!v9fs_kobj)
559 return -ENOMEM;
561 if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
562 kobject_put(v9fs_kobj);
563 return -ENOMEM;
566 return 0;
570 * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
574 static void v9fs_sysfs_cleanup(void)
576 sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
577 kobject_put(v9fs_kobj);
580 static void v9fs_inode_init_once(void *foo)
582 struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
583 #ifdef CONFIG_9P_FSCACHE
584 v9inode->fscache = NULL;
585 #endif
586 memset(&v9inode->qid, 0, sizeof(v9inode->qid));
587 inode_init_once(&v9inode->vfs_inode);
591 * v9fs_init_inode_cache - initialize a cache for 9P
592 * Returns 0 on success.
594 static int v9fs_init_inode_cache(void)
596 v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
597 sizeof(struct v9fs_inode),
598 0, (SLAB_RECLAIM_ACCOUNT|
599 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
600 v9fs_inode_init_once);
601 if (!v9fs_inode_cache)
602 return -ENOMEM;
604 return 0;
608 * v9fs_destroy_inode_cache - destroy the cache of 9P inode
611 static void v9fs_destroy_inode_cache(void)
614 * Make sure all delayed rcu free inodes are flushed before we
615 * destroy cache.
617 rcu_barrier();
618 kmem_cache_destroy(v9fs_inode_cache);
621 static int v9fs_cache_register(void)
623 int ret;
624 ret = v9fs_init_inode_cache();
625 if (ret < 0)
626 return ret;
627 #ifdef CONFIG_9P_FSCACHE
628 ret = fscache_register_netfs(&v9fs_cache_netfs);
629 if (ret < 0)
630 v9fs_destroy_inode_cache();
631 #endif
632 return ret;
635 static void v9fs_cache_unregister(void)
637 v9fs_destroy_inode_cache();
638 #ifdef CONFIG_9P_FSCACHE
639 fscache_unregister_netfs(&v9fs_cache_netfs);
640 #endif
644 * init_v9fs - Initialize module
648 static int __init init_v9fs(void)
650 int err;
651 pr_info("Installing v9fs 9p2000 file system support\n");
652 /* TODO: Setup list of registered trasnport modules */
654 err = v9fs_cache_register();
655 if (err < 0) {
656 pr_err("Failed to register v9fs for caching\n");
657 return err;
660 err = v9fs_sysfs_init();
661 if (err < 0) {
662 pr_err("Failed to register with sysfs\n");
663 goto out_cache;
665 err = register_filesystem(&v9fs_fs_type);
666 if (err < 0) {
667 pr_err("Failed to register filesystem\n");
668 goto out_sysfs_cleanup;
671 return 0;
673 out_sysfs_cleanup:
674 v9fs_sysfs_cleanup();
676 out_cache:
677 v9fs_cache_unregister();
679 return err;
683 * exit_v9fs - shutdown module
687 static void __exit exit_v9fs(void)
689 v9fs_sysfs_cleanup();
690 v9fs_cache_unregister();
691 unregister_filesystem(&v9fs_fs_type);
694 module_init(init_v9fs)
695 module_exit(exit_v9fs)
697 MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
698 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
699 MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
700 MODULE_LICENSE("GPL");