v9fs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/sched.h>
  30. #include <linux/parser.h>
  31. #include <linux/idr.h>
  32. #include <linux/slab.h>
  33. #include <net/9p/9p.h>
  34. #include <net/9p/client.h>
  35. #include <net/9p/transport.h>
  36. #include "v9fs.h"
  37. #include "v9fs_vfs.h"
  38. #include "cache.h"
  39. static DEFINE_SPINLOCK(v9fs_sessionlist_lock);
  40. static LIST_HEAD(v9fs_sessionlist);
  41. struct kmem_cache *v9fs_inode_cache;
  42. /*
  43. * Option Parsing (code inspired by NFS code)
  44. * NOTE: each transport will parse its own options
  45. */
  46. enum {
  47. /* Options that take integer arguments */
  48. Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
  49. /* String options */
  50. Opt_uname, Opt_remotename, Opt_trans, Opt_cache, Opt_cachetag,
  51. /* Options that take no arguments */
  52. Opt_nodevmap,
  53. /* Cache options */
  54. Opt_cache_loose, Opt_fscache,
  55. /* Access options */
  56. Opt_access, Opt_posixacl,
  57. /* Error token */
  58. Opt_err
  59. };
  60. static const match_table_t tokens = {
  61. {Opt_debug, "debug=%x"},
  62. {Opt_dfltuid, "dfltuid=%u"},
  63. {Opt_dfltgid, "dfltgid=%u"},
  64. {Opt_afid, "afid=%u"},
  65. {Opt_uname, "uname=%s"},
  66. {Opt_remotename, "aname=%s"},
  67. {Opt_nodevmap, "nodevmap"},
  68. {Opt_cache, "cache=%s"},
  69. {Opt_cache_loose, "loose"},
  70. {Opt_fscache, "fscache"},
  71. {Opt_cachetag, "cachetag=%s"},
  72. {Opt_access, "access=%s"},
  73. {Opt_posixacl, "posixacl"},
  74. {Opt_err, NULL}
  75. };
  76. /* Interpret mount options for cache mode */
  77. static int get_cache_mode(char *s)
  78. {
  79. int version = -EINVAL;
  80. if (!strcmp(s, "loose")) {
  81. version = CACHE_LOOSE;
  82. p9_debug(P9_DEBUG_9P, "Cache mode: loose\n");
  83. } else if (!strcmp(s, "fscache")) {
  84. version = CACHE_FSCACHE;
  85. p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n");
  86. } else if (!strcmp(s, "none")) {
  87. version = CACHE_NONE;
  88. p9_debug(P9_DEBUG_9P, "Cache mode: none\n");
  89. } else
  90. pr_info("Unknown Cache mode %s\n", s);
  91. return version;
  92. }
  93. /**
  94. * v9fs_parse_options - parse mount options into session structure
  95. * @v9ses: existing v9fs session information
  96. *
  97. * Return 0 upon success, -ERRNO upon failure.
  98. */
  99. static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
  100. {
  101. char *options, *tmp_options;
  102. substring_t args[MAX_OPT_ARGS];
  103. char *p;
  104. int option = 0;
  105. char *s, *e;
  106. int ret = 0;
  107. /* setup defaults */
  108. v9ses->afid = ~0;
  109. v9ses->debug = 0;
  110. v9ses->cache = CACHE_NONE;
  111. #ifdef CONFIG_9P_FSCACHE
  112. v9ses->cachetag = NULL;
  113. #endif
  114. if (!opts)
  115. return 0;
  116. tmp_options = kstrdup(opts, GFP_KERNEL);
  117. if (!tmp_options) {
  118. ret = -ENOMEM;
  119. goto fail_option_alloc;
  120. }
  121. options = tmp_options;
  122. while ((p = strsep(&options, ",")) != NULL) {
  123. int token, r;
  124. if (!*p)
  125. continue;
  126. token = match_token(p, tokens, args);
  127. switch (token) {
  128. case Opt_debug:
  129. r = match_int(&args[0], &option);
  130. if (r < 0) {
  131. p9_debug(P9_DEBUG_ERROR,
  132. "integer field, but no integer?\n");
  133. ret = r;
  134. continue;
  135. }
  136. v9ses->debug = option;
  137. #ifdef CONFIG_NET_9P_DEBUG
  138. p9_debug_level = option;
  139. #endif
  140. break;
  141. case Opt_dfltuid:
  142. r = match_int(&args[0], &option);
  143. if (r < 0) {
  144. p9_debug(P9_DEBUG_ERROR,
  145. "integer field, but no integer?\n");
  146. ret = r;
  147. continue;
  148. }
  149. v9ses->dfltuid = option;
  150. break;
  151. case Opt_dfltgid:
  152. r = match_int(&args[0], &option);
  153. if (r < 0) {
  154. p9_debug(P9_DEBUG_ERROR,
  155. "integer field, but no integer?\n");
  156. ret = r;
  157. continue;
  158. }
  159. v9ses->dfltgid = option;
  160. break;
  161. case Opt_afid:
  162. r = match_int(&args[0], &option);
  163. if (r < 0) {
  164. p9_debug(P9_DEBUG_ERROR,
  165. "integer field, but no integer?\n");
  166. ret = r;
  167. continue;
  168. }
  169. v9ses->afid = option;
  170. break;
  171. case Opt_uname:
  172. match_strlcpy(v9ses->uname, &args[0], PATH_MAX);
  173. break;
  174. case Opt_remotename:
  175. match_strlcpy(v9ses->aname, &args[0], PATH_MAX);
  176. break;
  177. case Opt_nodevmap:
  178. v9ses->nodev = 1;
  179. break;
  180. case Opt_cache_loose:
  181. v9ses->cache = CACHE_LOOSE;
  182. break;
  183. case Opt_fscache:
  184. v9ses->cache = CACHE_FSCACHE;
  185. break;
  186. case Opt_cachetag:
  187. #ifdef CONFIG_9P_FSCACHE
  188. v9ses->cachetag = match_strdup(&args[0]);
  189. #endif
  190. break;
  191. case Opt_cache:
  192. s = match_strdup(&args[0]);
  193. if (!s) {
  194. ret = -ENOMEM;
  195. p9_debug(P9_DEBUG_ERROR,
  196. "problem allocating copy of cache arg\n");
  197. goto free_and_return;
  198. }
  199. ret = get_cache_mode(s);
  200. if (ret == -EINVAL) {
  201. kfree(s);
  202. goto free_and_return;
  203. }
  204. v9ses->cache = ret;
  205. kfree(s);
  206. break;
  207. case Opt_access:
  208. s = match_strdup(&args[0]);
  209. if (!s) {
  210. ret = -ENOMEM;
  211. p9_debug(P9_DEBUG_ERROR,
  212. "problem allocating copy of access arg\n");
  213. goto free_and_return;
  214. }
  215. v9ses->flags &= ~V9FS_ACCESS_MASK;
  216. if (strcmp(s, "user") == 0)
  217. v9ses->flags |= V9FS_ACCESS_USER;
  218. else if (strcmp(s, "any") == 0)
  219. v9ses->flags |= V9FS_ACCESS_ANY;
  220. else if (strcmp(s, "client") == 0) {
  221. v9ses->flags |= V9FS_ACCESS_CLIENT;
  222. } else {
  223. v9ses->flags |= V9FS_ACCESS_SINGLE;
  224. v9ses->uid = simple_strtoul(s, &e, 10);
  225. if (*e != '\0') {
  226. ret = -EINVAL;
  227. pr_info("Unknown access argument %s\n",
  228. s);
  229. kfree(s);
  230. goto free_and_return;
  231. }
  232. }
  233. kfree(s);
  234. break;
  235. case Opt_posixacl:
  236. #ifdef CONFIG_9P_FS_POSIX_ACL
  237. v9ses->flags |= V9FS_POSIX_ACL;
  238. #else
  239. p9_debug(P9_DEBUG_ERROR,
  240. "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
  241. #endif
  242. break;
  243. default:
  244. continue;
  245. }
  246. }
  247. free_and_return:
  248. kfree(tmp_options);
  249. fail_option_alloc:
  250. return ret;
  251. }
  252. /**
  253. * v9fs_session_init - initialize session
  254. * @v9ses: session information structure
  255. * @dev_name: device being mounted
  256. * @data: options
  257. *
  258. */
  259. struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
  260. const char *dev_name, char *data)
  261. {
  262. int retval = -EINVAL;
  263. struct p9_fid *fid;
  264. int rc;
  265. v9ses->uname = __getname();
  266. if (!v9ses->uname)
  267. return ERR_PTR(-ENOMEM);
  268. v9ses->aname = __getname();
  269. if (!v9ses->aname) {
  270. __putname(v9ses->uname);
  271. return ERR_PTR(-ENOMEM);
  272. }
  273. init_rwsem(&v9ses->rename_sem);
  274. rc = bdi_setup_and_register(&v9ses->bdi, "9p", BDI_CAP_MAP_COPY);
  275. if (rc) {
  276. __putname(v9ses->aname);
  277. __putname(v9ses->uname);
  278. return ERR_PTR(rc);
  279. }
  280. spin_lock(&v9fs_sessionlist_lock);
  281. list_add(&v9ses->slist, &v9fs_sessionlist);
  282. spin_unlock(&v9fs_sessionlist_lock);
  283. strcpy(v9ses->uname, V9FS_DEFUSER);
  284. strcpy(v9ses->aname, V9FS_DEFANAME);
  285. v9ses->uid = ~0;
  286. v9ses->dfltuid = V9FS_DEFUID;
  287. v9ses->dfltgid = V9FS_DEFGID;
  288. v9ses->clnt = p9_client_create(dev_name, data);
  289. if (IS_ERR(v9ses->clnt)) {
  290. retval = PTR_ERR(v9ses->clnt);
  291. v9ses->clnt = NULL;
  292. p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n");
  293. goto error;
  294. }
  295. v9ses->flags = V9FS_ACCESS_USER;
  296. if (p9_is_proto_dotl(v9ses->clnt)) {
  297. v9ses->flags = V9FS_ACCESS_CLIENT;
  298. v9ses->flags |= V9FS_PROTO_2000L;
  299. } else if (p9_is_proto_dotu(v9ses->clnt)) {
  300. v9ses->flags |= V9FS_PROTO_2000U;
  301. }
  302. rc = v9fs_parse_options(v9ses, data);
  303. if (rc < 0) {
  304. retval = rc;
  305. goto error;
  306. }
  307. v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
  308. if (!v9fs_proto_dotl(v9ses) &&
  309. ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
  310. /*
  311. * We support ACCESS_CLIENT only for dotl.
  312. * Fall back to ACCESS_USER
  313. */
  314. v9ses->flags &= ~V9FS_ACCESS_MASK;
  315. v9ses->flags |= V9FS_ACCESS_USER;
  316. }
  317. /*FIXME !! */
  318. /* for legacy mode, fall back to V9FS_ACCESS_ANY */
  319. if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
  320. ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
  321. v9ses->flags &= ~V9FS_ACCESS_MASK;
  322. v9ses->flags |= V9FS_ACCESS_ANY;
  323. v9ses->uid = ~0;
  324. }
  325. if (!v9fs_proto_dotl(v9ses) ||
  326. !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
  327. /*
  328. * We support ACL checks on clinet only if the protocol is
  329. * 9P2000.L and access is V9FS_ACCESS_CLIENT.
  330. */
  331. v9ses->flags &= ~V9FS_ACL_MASK;
  332. }
  333. fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0,
  334. v9ses->aname);
  335. if (IS_ERR(fid)) {
  336. retval = PTR_ERR(fid);
  337. fid = NULL;
  338. p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
  339. goto error;
  340. }
  341. if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
  342. fid->uid = v9ses->uid;
  343. else
  344. fid->uid = ~0;
  345. #ifdef CONFIG_9P_FSCACHE
  346. /* register the session for caching */
  347. v9fs_cache_session_get_cookie(v9ses);
  348. #endif
  349. return fid;
  350. error:
  351. bdi_destroy(&v9ses->bdi);
  352. return ERR_PTR(retval);
  353. }
  354. /**
  355. * v9fs_session_close - shutdown a session
  356. * @v9ses: session information structure
  357. *
  358. */
  359. void v9fs_session_close(struct v9fs_session_info *v9ses)
  360. {
  361. if (v9ses->clnt) {
  362. p9_client_destroy(v9ses->clnt);
  363. v9ses->clnt = NULL;
  364. }
  365. #ifdef CONFIG_9P_FSCACHE
  366. if (v9ses->fscache) {
  367. v9fs_cache_session_put_cookie(v9ses);
  368. kfree(v9ses->cachetag);
  369. }
  370. #endif
  371. __putname(v9ses->uname);
  372. __putname(v9ses->aname);
  373. bdi_destroy(&v9ses->bdi);
  374. spin_lock(&v9fs_sessionlist_lock);
  375. list_del(&v9ses->slist);
  376. spin_unlock(&v9fs_sessionlist_lock);
  377. }
  378. /**
  379. * v9fs_session_cancel - terminate a session
  380. * @v9ses: session to terminate
  381. *
  382. * mark transport as disconnected and cancel all pending requests.
  383. */
  384. void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
  385. p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
  386. p9_client_disconnect(v9ses->clnt);
  387. }
  388. /**
  389. * v9fs_session_begin_cancel - Begin terminate of a session
  390. * @v9ses: session to terminate
  391. *
  392. * After this call we don't allow any request other than clunk.
  393. */
  394. void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
  395. {
  396. p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
  397. p9_client_begin_disconnect(v9ses->clnt);
  398. }
  399. extern int v9fs_error_init(void);
  400. static struct kobject *v9fs_kobj;
  401. #ifdef CONFIG_9P_FSCACHE
  402. /**
  403. * caches_show - list caches associated with a session
  404. *
  405. * Returns the size of buffer written.
  406. */
  407. static ssize_t caches_show(struct kobject *kobj,
  408. struct kobj_attribute *attr,
  409. char *buf)
  410. {
  411. ssize_t n = 0, count = 0, limit = PAGE_SIZE;
  412. struct v9fs_session_info *v9ses;
  413. spin_lock(&v9fs_sessionlist_lock);
  414. list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
  415. if (v9ses->cachetag) {
  416. n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
  417. if (n < 0) {
  418. count = n;
  419. break;
  420. }
  421. count += n;
  422. limit -= n;
  423. }
  424. }
  425. spin_unlock(&v9fs_sessionlist_lock);
  426. return count;
  427. }
  428. static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
  429. #endif /* CONFIG_9P_FSCACHE */
  430. static struct attribute *v9fs_attrs[] = {
  431. #ifdef CONFIG_9P_FSCACHE
  432. &v9fs_attr_cache.attr,
  433. #endif
  434. NULL,
  435. };
  436. static struct attribute_group v9fs_attr_group = {
  437. .attrs = v9fs_attrs,
  438. };
  439. /**
  440. * v9fs_sysfs_init - Initialize the v9fs sysfs interface
  441. *
  442. */
  443. static int v9fs_sysfs_init(void)
  444. {
  445. v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
  446. if (!v9fs_kobj)
  447. return -ENOMEM;
  448. if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
  449. kobject_put(v9fs_kobj);
  450. return -ENOMEM;
  451. }
  452. return 0;
  453. }
  454. /**
  455. * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
  456. *
  457. */
  458. static void v9fs_sysfs_cleanup(void)
  459. {
  460. sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
  461. kobject_put(v9fs_kobj);
  462. }
  463. static void v9fs_inode_init_once(void *foo)
  464. {
  465. struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
  466. #ifdef CONFIG_9P_FSCACHE
  467. v9inode->fscache = NULL;
  468. #endif
  469. memset(&v9inode->qid, 0, sizeof(v9inode->qid));
  470. inode_init_once(&v9inode->vfs_inode);
  471. }
  472. /**
  473. * v9fs_init_inode_cache - initialize a cache for 9P
  474. * Returns 0 on success.
  475. */
  476. static int v9fs_init_inode_cache(void)
  477. {
  478. v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
  479. sizeof(struct v9fs_inode),
  480. 0, (SLAB_RECLAIM_ACCOUNT|
  481. SLAB_MEM_SPREAD),
  482. v9fs_inode_init_once);
  483. if (!v9fs_inode_cache)
  484. return -ENOMEM;
  485. return 0;
  486. }
  487. /**
  488. * v9fs_destroy_inode_cache - destroy the cache of 9P inode
  489. *
  490. */
  491. static void v9fs_destroy_inode_cache(void)
  492. {
  493. /*
  494. * Make sure all delayed rcu free inodes are flushed before we
  495. * destroy cache.
  496. */
  497. rcu_barrier();
  498. kmem_cache_destroy(v9fs_inode_cache);
  499. }
  500. static int v9fs_cache_register(void)
  501. {
  502. int ret;
  503. ret = v9fs_init_inode_cache();
  504. if (ret < 0)
  505. return ret;
  506. #ifdef CONFIG_9P_FSCACHE
  507. return fscache_register_netfs(&v9fs_cache_netfs);
  508. #else
  509. return ret;
  510. #endif
  511. }
  512. static void v9fs_cache_unregister(void)
  513. {
  514. v9fs_destroy_inode_cache();
  515. #ifdef CONFIG_9P_FSCACHE
  516. fscache_unregister_netfs(&v9fs_cache_netfs);
  517. #endif
  518. }
  519. /**
  520. * init_v9fs - Initialize module
  521. *
  522. */
  523. static int __init init_v9fs(void)
  524. {
  525. int err;
  526. pr_info("Installing v9fs 9p2000 file system support\n");
  527. /* TODO: Setup list of registered trasnport modules */
  528. err = v9fs_cache_register();
  529. if (err < 0) {
  530. pr_err("Failed to register v9fs for caching\n");
  531. return err;
  532. }
  533. err = v9fs_sysfs_init();
  534. if (err < 0) {
  535. pr_err("Failed to register with sysfs\n");
  536. goto out_cache;
  537. }
  538. err = register_filesystem(&v9fs_fs_type);
  539. if (err < 0) {
  540. pr_err("Failed to register filesystem\n");
  541. goto out_sysfs_cleanup;
  542. }
  543. return 0;
  544. out_sysfs_cleanup:
  545. v9fs_sysfs_cleanup();
  546. out_cache:
  547. v9fs_cache_unregister();
  548. return err;
  549. }
  550. /**
  551. * exit_v9fs - shutdown module
  552. *
  553. */
  554. static void __exit exit_v9fs(void)
  555. {
  556. v9fs_sysfs_cleanup();
  557. v9fs_cache_unregister();
  558. unregister_filesystem(&v9fs_fs_type);
  559. }
  560. module_init(init_v9fs)
  561. module_exit(exit_v9fs)
  562. MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
  563. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  564. MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
  565. MODULE_LICENSE("GPL");