v9fs.c (16347B)
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * This file contains functions assisting in mapping VFS to 9P2000 4 * 5 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com> 6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov> 7 */ 8 9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11#include <linux/module.h> 12#include <linux/errno.h> 13#include <linux/fs.h> 14#include <linux/sched.h> 15#include <linux/cred.h> 16#include <linux/parser.h> 17#include <linux/idr.h> 18#include <linux/slab.h> 19#include <linux/seq_file.h> 20#include <net/9p/9p.h> 21#include <net/9p/client.h> 22#include <net/9p/transport.h> 23#include "v9fs.h" 24#include "v9fs_vfs.h" 25#include "cache.h" 26 27static DEFINE_SPINLOCK(v9fs_sessionlist_lock); 28static LIST_HEAD(v9fs_sessionlist); 29struct kmem_cache *v9fs_inode_cache; 30 31/* 32 * Option Parsing (code inspired by NFS code) 33 * NOTE: each transport will parse its own options 34 */ 35 36enum { 37 /* Options that take integer arguments */ 38 Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid, 39 /* String options */ 40 Opt_uname, Opt_remotename, Opt_cache, Opt_cachetag, 41 /* Options that take no arguments */ 42 Opt_nodevmap, 43 /* Cache options */ 44 Opt_cache_loose, Opt_fscache, Opt_mmap, 45 /* Access options */ 46 Opt_access, Opt_posixacl, 47 /* Lock timeout option */ 48 Opt_locktimeout, 49 /* Error token */ 50 Opt_err 51}; 52 53static const match_table_t tokens = { 54 {Opt_debug, "debug=%x"}, 55 {Opt_dfltuid, "dfltuid=%u"}, 56 {Opt_dfltgid, "dfltgid=%u"}, 57 {Opt_afid, "afid=%u"}, 58 {Opt_uname, "uname=%s"}, 59 {Opt_remotename, "aname=%s"}, 60 {Opt_nodevmap, "nodevmap"}, 61 {Opt_cache, "cache=%s"}, 62 {Opt_cache_loose, "loose"}, 63 {Opt_fscache, "fscache"}, 64 {Opt_mmap, "mmap"}, 65 {Opt_cachetag, "cachetag=%s"}, 66 {Opt_access, "access=%s"}, 67 {Opt_posixacl, "posixacl"}, 68 {Opt_locktimeout, "locktimeout=%u"}, 69 {Opt_err, NULL} 70}; 71 72static const char *const v9fs_cache_modes[nr__p9_cache_modes] = { 73 [CACHE_NONE] = "none", 74 [CACHE_MMAP] = "mmap", 75 [CACHE_LOOSE] = "loose", 76 [CACHE_FSCACHE] = "fscache", 77}; 78 79/* Interpret mount options for cache mode */ 80static int get_cache_mode(char *s) 81{ 82 int version = -EINVAL; 83 84 if (!strcmp(s, "loose")) { 85 version = CACHE_LOOSE; 86 p9_debug(P9_DEBUG_9P, "Cache mode: loose\n"); 87 } else if (!strcmp(s, "fscache")) { 88 version = CACHE_FSCACHE; 89 p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n"); 90 } else if (!strcmp(s, "mmap")) { 91 version = CACHE_MMAP; 92 p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n"); 93 } else if (!strcmp(s, "none")) { 94 version = CACHE_NONE; 95 p9_debug(P9_DEBUG_9P, "Cache mode: none\n"); 96 } else 97 pr_info("Unknown Cache mode %s\n", s); 98 return version; 99} 100 101/* 102 * Display the mount options in /proc/mounts. 103 */ 104int v9fs_show_options(struct seq_file *m, struct dentry *root) 105{ 106 struct v9fs_session_info *v9ses = root->d_sb->s_fs_info; 107 108 if (v9ses->debug) 109 seq_printf(m, ",debug=%x", v9ses->debug); 110 if (!uid_eq(v9ses->dfltuid, V9FS_DEFUID)) 111 seq_printf(m, ",dfltuid=%u", 112 from_kuid_munged(&init_user_ns, v9ses->dfltuid)); 113 if (!gid_eq(v9ses->dfltgid, V9FS_DEFGID)) 114 seq_printf(m, ",dfltgid=%u", 115 from_kgid_munged(&init_user_ns, v9ses->dfltgid)); 116 if (v9ses->afid != ~0) 117 seq_printf(m, ",afid=%u", v9ses->afid); 118 if (strcmp(v9ses->uname, V9FS_DEFUSER) != 0) 119 seq_printf(m, ",uname=%s", v9ses->uname); 120 if (strcmp(v9ses->aname, V9FS_DEFANAME) != 0) 121 seq_printf(m, ",aname=%s", v9ses->aname); 122 if (v9ses->nodev) 123 seq_puts(m, ",nodevmap"); 124 if (v9ses->cache) 125 seq_printf(m, ",%s", v9fs_cache_modes[v9ses->cache]); 126#ifdef CONFIG_9P_FSCACHE 127 if (v9ses->cachetag && v9ses->cache == CACHE_FSCACHE) 128 seq_printf(m, ",cachetag=%s", v9ses->cachetag); 129#endif 130 131 switch (v9ses->flags & V9FS_ACCESS_MASK) { 132 case V9FS_ACCESS_USER: 133 seq_puts(m, ",access=user"); 134 break; 135 case V9FS_ACCESS_ANY: 136 seq_puts(m, ",access=any"); 137 break; 138 case V9FS_ACCESS_CLIENT: 139 seq_puts(m, ",access=client"); 140 break; 141 case V9FS_ACCESS_SINGLE: 142 seq_printf(m, ",access=%u", 143 from_kuid_munged(&init_user_ns, v9ses->uid)); 144 break; 145 } 146 147 if (v9ses->flags & V9FS_POSIX_ACL) 148 seq_puts(m, ",posixacl"); 149 150 return p9_show_client_options(m, v9ses->clnt); 151} 152 153/** 154 * v9fs_parse_options - parse mount options into session structure 155 * @v9ses: existing v9fs session information 156 * @opts: The mount option string 157 * 158 * Return 0 upon success, -ERRNO upon failure. 159 */ 160 161static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts) 162{ 163 char *options, *tmp_options; 164 substring_t args[MAX_OPT_ARGS]; 165 char *p; 166 int option = 0; 167 char *s; 168 int ret = 0; 169 170 /* setup defaults */ 171 v9ses->afid = ~0; 172 v9ses->debug = 0; 173 v9ses->cache = CACHE_NONE; 174#ifdef CONFIG_9P_FSCACHE 175 v9ses->cachetag = NULL; 176#endif 177 v9ses->session_lock_timeout = P9_LOCK_TIMEOUT; 178 179 if (!opts) 180 return 0; 181 182 tmp_options = kstrdup(opts, GFP_KERNEL); 183 if (!tmp_options) { 184 ret = -ENOMEM; 185 goto fail_option_alloc; 186 } 187 options = tmp_options; 188 189 while ((p = strsep(&options, ",")) != NULL) { 190 int token, r; 191 192 if (!*p) 193 continue; 194 195 token = match_token(p, tokens, args); 196 switch (token) { 197 case Opt_debug: 198 r = match_int(&args[0], &option); 199 if (r < 0) { 200 p9_debug(P9_DEBUG_ERROR, 201 "integer field, but no integer?\n"); 202 ret = r; 203 } else { 204 v9ses->debug = option; 205#ifdef CONFIG_NET_9P_DEBUG 206 p9_debug_level = option; 207#endif 208 } 209 break; 210 211 case Opt_dfltuid: 212 r = match_int(&args[0], &option); 213 if (r < 0) { 214 p9_debug(P9_DEBUG_ERROR, 215 "integer field, but no integer?\n"); 216 ret = r; 217 continue; 218 } 219 v9ses->dfltuid = make_kuid(current_user_ns(), option); 220 if (!uid_valid(v9ses->dfltuid)) { 221 p9_debug(P9_DEBUG_ERROR, 222 "uid field, but not a uid?\n"); 223 ret = -EINVAL; 224 } 225 break; 226 case Opt_dfltgid: 227 r = match_int(&args[0], &option); 228 if (r < 0) { 229 p9_debug(P9_DEBUG_ERROR, 230 "integer field, but no integer?\n"); 231 ret = r; 232 continue; 233 } 234 v9ses->dfltgid = make_kgid(current_user_ns(), option); 235 if (!gid_valid(v9ses->dfltgid)) { 236 p9_debug(P9_DEBUG_ERROR, 237 "gid field, but not a gid?\n"); 238 ret = -EINVAL; 239 } 240 break; 241 case Opt_afid: 242 r = match_int(&args[0], &option); 243 if (r < 0) { 244 p9_debug(P9_DEBUG_ERROR, 245 "integer field, but no integer?\n"); 246 ret = r; 247 } else { 248 v9ses->afid = option; 249 } 250 break; 251 case Opt_uname: 252 kfree(v9ses->uname); 253 v9ses->uname = match_strdup(&args[0]); 254 if (!v9ses->uname) { 255 ret = -ENOMEM; 256 goto free_and_return; 257 } 258 break; 259 case Opt_remotename: 260 kfree(v9ses->aname); 261 v9ses->aname = match_strdup(&args[0]); 262 if (!v9ses->aname) { 263 ret = -ENOMEM; 264 goto free_and_return; 265 } 266 break; 267 case Opt_nodevmap: 268 v9ses->nodev = 1; 269 break; 270 case Opt_cache_loose: 271 v9ses->cache = CACHE_LOOSE; 272 break; 273 case Opt_fscache: 274 v9ses->cache = CACHE_FSCACHE; 275 break; 276 case Opt_mmap: 277 v9ses->cache = CACHE_MMAP; 278 break; 279 case Opt_cachetag: 280#ifdef CONFIG_9P_FSCACHE 281 kfree(v9ses->cachetag); 282 v9ses->cachetag = match_strdup(&args[0]); 283 if (!v9ses->cachetag) { 284 ret = -ENOMEM; 285 goto free_and_return; 286 } 287#endif 288 break; 289 case Opt_cache: 290 s = match_strdup(&args[0]); 291 if (!s) { 292 ret = -ENOMEM; 293 p9_debug(P9_DEBUG_ERROR, 294 "problem allocating copy of cache arg\n"); 295 goto free_and_return; 296 } 297 r = get_cache_mode(s); 298 if (r < 0) 299 ret = r; 300 else 301 v9ses->cache = r; 302 303 kfree(s); 304 break; 305 306 case Opt_access: 307 s = match_strdup(&args[0]); 308 if (!s) { 309 ret = -ENOMEM; 310 p9_debug(P9_DEBUG_ERROR, 311 "problem allocating copy of access arg\n"); 312 goto free_and_return; 313 } 314 315 v9ses->flags &= ~V9FS_ACCESS_MASK; 316 if (strcmp(s, "user") == 0) 317 v9ses->flags |= V9FS_ACCESS_USER; 318 else if (strcmp(s, "any") == 0) 319 v9ses->flags |= V9FS_ACCESS_ANY; 320 else if (strcmp(s, "client") == 0) { 321 v9ses->flags |= V9FS_ACCESS_CLIENT; 322 } else { 323 uid_t uid; 324 325 v9ses->flags |= V9FS_ACCESS_SINGLE; 326 r = kstrtouint(s, 10, &uid); 327 if (r) { 328 ret = r; 329 pr_info("Unknown access argument %s: %d\n", 330 s, r); 331 kfree(s); 332 continue; 333 } 334 v9ses->uid = make_kuid(current_user_ns(), uid); 335 if (!uid_valid(v9ses->uid)) { 336 ret = -EINVAL; 337 pr_info("Unknown uid %s\n", s); 338 } 339 } 340 341 kfree(s); 342 break; 343 344 case Opt_posixacl: 345#ifdef CONFIG_9P_FS_POSIX_ACL 346 v9ses->flags |= V9FS_POSIX_ACL; 347#else 348 p9_debug(P9_DEBUG_ERROR, 349 "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n"); 350#endif 351 break; 352 353 case Opt_locktimeout: 354 r = match_int(&args[0], &option); 355 if (r < 0) { 356 p9_debug(P9_DEBUG_ERROR, 357 "integer field, but no integer?\n"); 358 ret = r; 359 continue; 360 } 361 if (option < 1) { 362 p9_debug(P9_DEBUG_ERROR, 363 "locktimeout must be a greater than zero integer.\n"); 364 ret = -EINVAL; 365 continue; 366 } 367 v9ses->session_lock_timeout = (long)option * HZ; 368 break; 369 370 default: 371 continue; 372 } 373 } 374 375free_and_return: 376 kfree(tmp_options); 377fail_option_alloc: 378 return ret; 379} 380 381/** 382 * v9fs_session_init - initialize session 383 * @v9ses: session information structure 384 * @dev_name: device being mounted 385 * @data: options 386 * 387 */ 388 389struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses, 390 const char *dev_name, char *data) 391{ 392 struct p9_fid *fid; 393 int rc = -ENOMEM; 394 395 v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL); 396 if (!v9ses->uname) 397 goto err_names; 398 399 v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL); 400 if (!v9ses->aname) 401 goto err_names; 402 init_rwsem(&v9ses->rename_sem); 403 404 v9ses->uid = INVALID_UID; 405 v9ses->dfltuid = V9FS_DEFUID; 406 v9ses->dfltgid = V9FS_DEFGID; 407 408 v9ses->clnt = p9_client_create(dev_name, data); 409 if (IS_ERR(v9ses->clnt)) { 410 rc = PTR_ERR(v9ses->clnt); 411 p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n"); 412 goto err_names; 413 } 414 415 v9ses->flags = V9FS_ACCESS_USER; 416 417 if (p9_is_proto_dotl(v9ses->clnt)) { 418 v9ses->flags = V9FS_ACCESS_CLIENT; 419 v9ses->flags |= V9FS_PROTO_2000L; 420 } else if (p9_is_proto_dotu(v9ses->clnt)) { 421 v9ses->flags |= V9FS_PROTO_2000U; 422 } 423 424 rc = v9fs_parse_options(v9ses, data); 425 if (rc < 0) 426 goto err_clnt; 427 428 v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ; 429 430 if (!v9fs_proto_dotl(v9ses) && 431 ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) { 432 /* 433 * We support ACCESS_CLIENT only for dotl. 434 * Fall back to ACCESS_USER 435 */ 436 v9ses->flags &= ~V9FS_ACCESS_MASK; 437 v9ses->flags |= V9FS_ACCESS_USER; 438 } 439 /*FIXME !! */ 440 /* for legacy mode, fall back to V9FS_ACCESS_ANY */ 441 if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) && 442 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) { 443 444 v9ses->flags &= ~V9FS_ACCESS_MASK; 445 v9ses->flags |= V9FS_ACCESS_ANY; 446 v9ses->uid = INVALID_UID; 447 } 448 if (!v9fs_proto_dotl(v9ses) || 449 !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) { 450 /* 451 * We support ACL checks on clinet only if the protocol is 452 * 9P2000.L and access is V9FS_ACCESS_CLIENT. 453 */ 454 v9ses->flags &= ~V9FS_ACL_MASK; 455 } 456 457 fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, INVALID_UID, 458 v9ses->aname); 459 if (IS_ERR(fid)) { 460 rc = PTR_ERR(fid); 461 p9_debug(P9_DEBUG_ERROR, "cannot attach\n"); 462 goto err_clnt; 463 } 464 465 if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE) 466 fid->uid = v9ses->uid; 467 else 468 fid->uid = INVALID_UID; 469 470#ifdef CONFIG_9P_FSCACHE 471 /* register the session for caching */ 472 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) { 473 rc = v9fs_cache_session_get_cookie(v9ses, dev_name); 474 if (rc < 0) 475 goto err_clnt; 476 } 477#endif 478 spin_lock(&v9fs_sessionlist_lock); 479 list_add(&v9ses->slist, &v9fs_sessionlist); 480 spin_unlock(&v9fs_sessionlist_lock); 481 482 return fid; 483 484err_clnt: 485#ifdef CONFIG_9P_FSCACHE 486 kfree(v9ses->cachetag); 487#endif 488 p9_client_destroy(v9ses->clnt); 489err_names: 490 kfree(v9ses->uname); 491 kfree(v9ses->aname); 492 return ERR_PTR(rc); 493} 494 495/** 496 * v9fs_session_close - shutdown a session 497 * @v9ses: session information structure 498 * 499 */ 500 501void v9fs_session_close(struct v9fs_session_info *v9ses) 502{ 503 if (v9ses->clnt) { 504 p9_client_destroy(v9ses->clnt); 505 v9ses->clnt = NULL; 506 } 507 508#ifdef CONFIG_9P_FSCACHE 509 fscache_relinquish_volume(v9fs_session_cache(v9ses), NULL, false); 510 kfree(v9ses->cachetag); 511#endif 512 kfree(v9ses->uname); 513 kfree(v9ses->aname); 514 515 spin_lock(&v9fs_sessionlist_lock); 516 list_del(&v9ses->slist); 517 spin_unlock(&v9fs_sessionlist_lock); 518} 519 520/** 521 * v9fs_session_cancel - terminate a session 522 * @v9ses: session to terminate 523 * 524 * mark transport as disconnected and cancel all pending requests. 525 */ 526 527void v9fs_session_cancel(struct v9fs_session_info *v9ses) 528{ 529 p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses); 530 p9_client_disconnect(v9ses->clnt); 531} 532 533/** 534 * v9fs_session_begin_cancel - Begin terminate of a session 535 * @v9ses: session to terminate 536 * 537 * After this call we don't allow any request other than clunk. 538 */ 539 540void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses) 541{ 542 p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses); 543 p9_client_begin_disconnect(v9ses->clnt); 544} 545 546extern int v9fs_error_init(void); 547 548static struct kobject *v9fs_kobj; 549 550#ifdef CONFIG_9P_FSCACHE 551/* 552 * List caches associated with a session 553 */ 554static ssize_t caches_show(struct kobject *kobj, 555 struct kobj_attribute *attr, 556 char *buf) 557{ 558 ssize_t n = 0, count = 0, limit = PAGE_SIZE; 559 struct v9fs_session_info *v9ses; 560 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); 565 if (n < 0) { 566 count = n; 567 break; 568 } 569 570 count += n; 571 limit -= n; 572 } 573 } 574 575 spin_unlock(&v9fs_sessionlist_lock); 576 return count; 577} 578 579static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches); 580#endif /* CONFIG_9P_FSCACHE */ 581 582static struct attribute *v9fs_attrs[] = { 583#ifdef CONFIG_9P_FSCACHE 584 &v9fs_attr_cache.attr, 585#endif 586 NULL, 587}; 588 589static const struct attribute_group v9fs_attr_group = { 590 .attrs = v9fs_attrs, 591}; 592 593/** 594 * v9fs_sysfs_init - Initialize the v9fs sysfs interface 595 * 596 */ 597 598static int __init v9fs_sysfs_init(void) 599{ 600 v9fs_kobj = kobject_create_and_add("9p", fs_kobj); 601 if (!v9fs_kobj) 602 return -ENOMEM; 603 604 if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) { 605 kobject_put(v9fs_kobj); 606 return -ENOMEM; 607 } 608 609 return 0; 610} 611 612/** 613 * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface 614 * 615 */ 616 617static void v9fs_sysfs_cleanup(void) 618{ 619 sysfs_remove_group(v9fs_kobj, &v9fs_attr_group); 620 kobject_put(v9fs_kobj); 621} 622 623static void v9fs_inode_init_once(void *foo) 624{ 625 struct v9fs_inode *v9inode = (struct v9fs_inode *)foo; 626 627 memset(&v9inode->qid, 0, sizeof(v9inode->qid)); 628 inode_init_once(&v9inode->netfs.inode); 629} 630 631/** 632 * v9fs_init_inode_cache - initialize a cache for 9P 633 * Returns 0 on success. 634 */ 635static int v9fs_init_inode_cache(void) 636{ 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) 643 return -ENOMEM; 644 645 return 0; 646} 647 648/** 649 * v9fs_destroy_inode_cache - destroy the cache of 9P inode 650 * 651 */ 652static void v9fs_destroy_inode_cache(void) 653{ 654 /* 655 * Make sure all delayed rcu free inodes are flushed before we 656 * destroy cache. 657 */ 658 rcu_barrier(); 659 kmem_cache_destroy(v9fs_inode_cache); 660} 661 662static int v9fs_cache_register(void) 663{ 664 int ret; 665 666 ret = v9fs_init_inode_cache(); 667 if (ret < 0) 668 return ret; 669 return ret; 670} 671 672static void v9fs_cache_unregister(void) 673{ 674 v9fs_destroy_inode_cache(); 675} 676 677/** 678 * init_v9fs - Initialize module 679 * 680 */ 681 682static int __init init_v9fs(void) 683{ 684 int err; 685 686 pr_info("Installing v9fs 9p2000 file system support\n"); 687 /* TODO: Setup list of registered trasnport modules */ 688 689 err = v9fs_cache_register(); 690 if (err < 0) { 691 pr_err("Failed to register v9fs for caching\n"); 692 return err; 693 } 694 695 err = v9fs_sysfs_init(); 696 if (err < 0) { 697 pr_err("Failed to register with sysfs\n"); 698 goto out_cache; 699 } 700 err = register_filesystem(&v9fs_fs_type); 701 if (err < 0) { 702 pr_err("Failed to register filesystem\n"); 703 goto out_sysfs_cleanup; 704 } 705 706 return 0; 707 708out_sysfs_cleanup: 709 v9fs_sysfs_cleanup(); 710 711out_cache: 712 v9fs_cache_unregister(); 713 714 return err; 715} 716 717/** 718 * exit_v9fs - shutdown module 719 * 720 */ 721 722static void __exit exit_v9fs(void) 723{ 724 v9fs_sysfs_cleanup(); 725 v9fs_cache_unregister(); 726 unregister_filesystem(&v9fs_fs_type); 727} 728 729module_init(init_v9fs) 730module_exit(exit_v9fs) 731 732MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>"); 733MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>"); 734MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>"); 735MODULE_LICENSE("GPL");