GNU Linux-libre 4.9.308-gnu1
[releases.git] / fs / 9p / v9fs.c
1 /*
2  *  linux/fs/9p/v9fs.c
3  *
4  *  This file contains functions assisting in mapping VFS to 9P2000
5  *
6  *  Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
7  *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8  *
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.
12  *
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.
17  *
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
23  *
24  */
25
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
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"
41
42 static DEFINE_SPINLOCK(v9fs_sessionlist_lock);
43 static LIST_HEAD(v9fs_sessionlist);
44 struct kmem_cache *v9fs_inode_cache;
45
46 /*
47  * Option Parsing (code inspired by NFS code)
48  *  NOTE: each transport will parse its own options
49  */
50
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
66 };
67
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}
85 };
86
87 /* Interpret mount options for cache mode */
88 static int get_cache_mode(char *s)
89 {
90         int version = -EINVAL;
91
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;
107 }
108
109 /**
110  * v9fs_parse_options - parse mount options into session structure
111  * @v9ses: existing v9fs session information
112  *
113  * Return 0 upon success, -ERRNO upon failure.
114  */
115
116 static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
117 {
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;
124
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;
133
134         if (!opts)
135                 return 0;
136
137         tmp_options = kstrdup(opts, GFP_KERNEL);
138         if (!tmp_options) {
139                 ret = -ENOMEM;
140                 goto fail_option_alloc;
141         }
142         options = tmp_options;
143
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;
157                         }
158                         v9ses->debug = option;
159 #ifdef CONFIG_NET_9P_DEBUG
160                         p9_debug_level = option;
161 #endif
162                         break;
163
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;
171                         }
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;
178                         }
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;
187                         }
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;
194                         }
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;
203                         }
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;
212                         }
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;
220                         }
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;
246                         }
247                         ret = get_cache_mode(s);
248                         if (ret == -EINVAL) {
249                                 kfree(s);
250                                 goto free_and_return;
251                         }
252
253                         v9ses->cache = ret;
254                         kfree(s);
255                         break;
256
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;
264                         }
265
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",
280                                                 s);
281                                         kfree(s);
282                                         goto free_and_return;
283                                 }
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;
290                                 }
291                         }
292
293                         kfree(s);
294                         break;
295
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;
304
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;
312                         }
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;
318                         }
319                         v9ses->session_lock_timeout = (long)option * HZ;
320                         break;
321
322                 default:
323                         continue;
324                 }
325         }
326
327 free_and_return:
328         kfree(tmp_options);
329 fail_option_alloc:
330         return ret;
331 }
332
333 /**
334  * v9fs_session_init - initialize session
335  * @v9ses: session information structure
336  * @dev_name: device being mounted
337  * @data: options
338  *
339  */
340
341 struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
342                   const char *dev_name, char *data)
343 {
344         struct p9_fid *fid;
345         int rc = -ENOMEM;
346
347         v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
348         if (!v9ses->uname)
349                 goto err_names;
350
351         v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
352         if (!v9ses->aname)
353                 goto err_names;
354         init_rwsem(&v9ses->rename_sem);
355
356         rc = bdi_setup_and_register(&v9ses->bdi, "9p");
357         if (rc)
358                 goto err_names;
359
360         v9ses->uid = INVALID_UID;
361         v9ses->dfltuid = V9FS_DEFUID;
362         v9ses->dfltgid = V9FS_DEFGID;
363
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;
369         }
370
371         v9ses->flags = V9FS_ACCESS_USER;
372
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;
378         }
379
380         rc = v9fs_parse_options(v9ses, data);
381         if (rc < 0)
382                 goto err_clnt;
383
384         v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
385
386         if (!v9fs_proto_dotl(v9ses) &&
387             ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
388                 /*
389                  * We support ACCESS_CLIENT only for dotl.
390                  * Fall back to ACCESS_USER
391                  */
392                 v9ses->flags &= ~V9FS_ACCESS_MASK;
393                 v9ses->flags |= V9FS_ACCESS_USER;
394         }
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)) {
399
400                 v9ses->flags &= ~V9FS_ACCESS_MASK;
401                 v9ses->flags |= V9FS_ACCESS_ANY;
402                 v9ses->uid = INVALID_UID;
403         }
404         if (!v9fs_proto_dotl(v9ses) ||
405                 !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
406                 /*
407                  * We support ACL checks on clinet only if the protocol is
408                  * 9P2000.L and access is V9FS_ACCESS_CLIENT.
409                  */
410                 v9ses->flags &= ~V9FS_ACL_MASK;
411         }
412
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;
419         }
420
421         if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
422                 fid->uid = v9ses->uid;
423         else
424                 fid->uid = INVALID_UID;
425
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);
433
434         return fid;
435
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);
444 }
445
446 /**
447  * v9fs_session_close - shutdown a session
448  * @v9ses: session information structure
449  *
450  */
451
452 void v9fs_session_close(struct v9fs_session_info *v9ses)
453 {
454         if (v9ses->clnt) {
455                 p9_client_destroy(v9ses->clnt);
456                 v9ses->clnt = NULL;
457         }
458
459 #ifdef CONFIG_9P_FSCACHE
460         if (v9ses->fscache)
461                 v9fs_cache_session_put_cookie(v9ses);
462         kfree(v9ses->cachetag);
463 #endif
464         kfree(v9ses->uname);
465         kfree(v9ses->aname);
466
467         bdi_destroy(&v9ses->bdi);
468
469         spin_lock(&v9fs_sessionlist_lock);
470         list_del(&v9ses->slist);
471         spin_unlock(&v9fs_sessionlist_lock);
472 }
473
474 /**
475  * v9fs_session_cancel - terminate a session
476  * @v9ses: session to terminate
477  *
478  * mark transport as disconnected and cancel all pending requests.
479  */
480
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);
484 }
485
486 /**
487  * v9fs_session_begin_cancel - Begin terminate of a session
488  * @v9ses: session to terminate
489  *
490  * After this call we don't allow any request other than clunk.
491  */
492
493 void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
494 {
495         p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
496         p9_client_begin_disconnect(v9ses->clnt);
497 }
498
499 extern int v9fs_error_init(void);
500
501 static struct kobject *v9fs_kobj;
502
503 #ifdef CONFIG_9P_FSCACHE
504 /**
505  * caches_show - list caches associated with a session
506  *
507  * Returns the size of buffer written.
508  */
509
510 static ssize_t caches_show(struct kobject *kobj,
511                            struct kobj_attribute *attr,
512                            char *buf)
513 {
514         ssize_t n = 0, count = 0, limit = PAGE_SIZE;
515         struct v9fs_session_info *v9ses;
516
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);
521                         if (n < 0) {
522                                 count = n;
523                                 break;
524                         }
525
526                         count += n;
527                         limit -= n;
528                 }
529         }
530
531         spin_unlock(&v9fs_sessionlist_lock);
532         return count;
533 }
534
535 static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
536 #endif /* CONFIG_9P_FSCACHE */
537
538 static struct attribute *v9fs_attrs[] = {
539 #ifdef CONFIG_9P_FSCACHE
540         &v9fs_attr_cache.attr,
541 #endif
542         NULL,
543 };
544
545 static struct attribute_group v9fs_attr_group = {
546         .attrs = v9fs_attrs,
547 };
548
549 /**
550  * v9fs_sysfs_init - Initialize the v9fs sysfs interface
551  *
552  */
553
554 static int __init v9fs_sysfs_init(void)
555 {
556         v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
557         if (!v9fs_kobj)
558                 return -ENOMEM;
559
560         if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
561                 kobject_put(v9fs_kobj);
562                 return -ENOMEM;
563         }
564
565         return 0;
566 }
567
568 /**
569  * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
570  *
571  */
572
573 static void v9fs_sysfs_cleanup(void)
574 {
575         sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
576         kobject_put(v9fs_kobj);
577 }
578
579 static void v9fs_inode_init_once(void *foo)
580 {
581         struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
582 #ifdef CONFIG_9P_FSCACHE
583         v9inode->fscache = NULL;
584 #endif
585         memset(&v9inode->qid, 0, sizeof(v9inode->qid));
586         inode_init_once(&v9inode->vfs_inode);
587 }
588
589 /**
590  * v9fs_init_inode_cache - initialize a cache for 9P
591  * Returns 0 on success.
592  */
593 static int v9fs_init_inode_cache(void)
594 {
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)
601                 return -ENOMEM;
602
603         return 0;
604 }
605
606 /**
607  * v9fs_destroy_inode_cache - destroy the cache of 9P inode
608  *
609  */
610 static void v9fs_destroy_inode_cache(void)
611 {
612         /*
613          * Make sure all delayed rcu free inodes are flushed before we
614          * destroy cache.
615          */
616         rcu_barrier();
617         kmem_cache_destroy(v9fs_inode_cache);
618 }
619
620 static int v9fs_cache_register(void)
621 {
622         int ret;
623         ret = v9fs_init_inode_cache();
624         if (ret < 0)
625                 return ret;
626 #ifdef CONFIG_9P_FSCACHE
627         ret = fscache_register_netfs(&v9fs_cache_netfs);
628         if (ret < 0)
629                 v9fs_destroy_inode_cache();
630 #endif
631         return ret;
632 }
633
634 static void v9fs_cache_unregister(void)
635 {
636         v9fs_destroy_inode_cache();
637 #ifdef CONFIG_9P_FSCACHE
638         fscache_unregister_netfs(&v9fs_cache_netfs);
639 #endif
640 }
641
642 /**
643  * init_v9fs - Initialize module
644  *
645  */
646
647 static int __init init_v9fs(void)
648 {
649         int err;
650         pr_info("Installing v9fs 9p2000 file system support\n");
651         /* TODO: Setup list of registered trasnport modules */
652
653         err = v9fs_cache_register();
654         if (err < 0) {
655                 pr_err("Failed to register v9fs for caching\n");
656                 return err;
657         }
658
659         err = v9fs_sysfs_init();
660         if (err < 0) {
661                 pr_err("Failed to register with sysfs\n");
662                 goto out_cache;
663         }
664         err = register_filesystem(&v9fs_fs_type);
665         if (err < 0) {
666                 pr_err("Failed to register filesystem\n");
667                 goto out_sysfs_cleanup;
668         }
669
670         return 0;
671
672 out_sysfs_cleanup:
673         v9fs_sysfs_cleanup();
674
675 out_cache:
676         v9fs_cache_unregister();
677
678         return err;
679 }
680
681 /**
682  * exit_v9fs - shutdown module
683  *
684  */
685
686 static void __exit exit_v9fs(void)
687 {
688         v9fs_sysfs_cleanup();
689         v9fs_cache_unregister();
690         unregister_filesystem(&v9fs_fs_type);
691 }
692
693 module_init(init_v9fs)
694 module_exit(exit_v9fs)
695
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");