v9fs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. kfree(v9ses->uname);
  173. v9ses->uname = match_strdup(&args[0]);
  174. if (!v9ses->uname) {
  175. ret = -ENOMEM;
  176. goto free_and_return;
  177. }
  178. break;
  179. case Opt_remotename:
  180. kfree(v9ses->aname);
  181. v9ses->aname = match_strdup(&args[0]);
  182. if (!v9ses->aname) {
  183. ret = -ENOMEM;
  184. goto free_and_return;
  185. }
  186. break;
  187. case Opt_nodevmap:
  188. v9ses->nodev = 1;
  189. break;
  190. case Opt_cache_loose:
  191. v9ses->cache = CACHE_LOOSE;
  192. break;
  193. case Opt_fscache:
  194. v9ses->cache = CACHE_FSCACHE;
  195. break;
  196. case Opt_cachetag:
  197. #ifdef CONFIG_9P_FSCACHE
  198. v9ses->cachetag = match_strdup(&args[0]);
  199. #endif
  200. break;
  201. case Opt_cache:
  202. s = match_strdup(&args[0]);
  203. if (!s) {
  204. ret = -ENOMEM;
  205. p9_debug(P9_DEBUG_ERROR,
  206. "problem allocating copy of cache arg\n");
  207. goto free_and_return;
  208. }
  209. ret = get_cache_mode(s);
  210. if (ret == -EINVAL) {
  211. kfree(s);
  212. goto free_and_return;
  213. }
  214. v9ses->cache = ret;
  215. kfree(s);
  216. break;
  217. case Opt_access:
  218. s = match_strdup(&args[0]);
  219. if (!s) {
  220. ret = -ENOMEM;
  221. p9_debug(P9_DEBUG_ERROR,
  222. "problem allocating copy of access arg\n");
  223. goto free_and_return;
  224. }
  225. v9ses->flags &= ~V9FS_ACCESS_MASK;
  226. if (strcmp(s, "user") == 0)
  227. v9ses->flags |= V9FS_ACCESS_USER;
  228. else if (strcmp(s, "any") == 0)
  229. v9ses->flags |= V9FS_ACCESS_ANY;
  230. else if (strcmp(s, "client") == 0) {
  231. v9ses->flags |= V9FS_ACCESS_CLIENT;
  232. } else {
  233. v9ses->flags |= V9FS_ACCESS_SINGLE;
  234. v9ses->uid = simple_strtoul(s, &e, 10);
  235. if (*e != '\0') {
  236. ret = -EINVAL;
  237. pr_info("Unknown access argument %s\n",
  238. s);
  239. kfree(s);
  240. goto free_and_return;
  241. }
  242. }
  243. kfree(s);
  244. break;
  245. case Opt_posixacl:
  246. #ifdef CONFIG_9P_FS_POSIX_ACL
  247. v9ses->flags |= V9FS_POSIX_ACL;
  248. #else
  249. p9_debug(P9_DEBUG_ERROR,
  250. "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
  251. #endif
  252. break;
  253. default:
  254. continue;
  255. }
  256. }
  257. free_and_return:
  258. kfree(tmp_options);
  259. fail_option_alloc:
  260. return ret;
  261. }
  262. /**
  263. * v9fs_session_init - initialize session
  264. * @v9ses: session information structure
  265. * @dev_name: device being mounted
  266. * @data: options
  267. *
  268. */
  269. struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
  270. const char *dev_name, char *data)
  271. {
  272. int retval = -EINVAL;
  273. struct p9_fid *fid;
  274. int rc;
  275. v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
  276. if (!v9ses->uname)
  277. return ERR_PTR(-ENOMEM);
  278. v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
  279. if (!v9ses->aname) {
  280. kfree(v9ses->uname);
  281. return ERR_PTR(-ENOMEM);
  282. }
  283. init_rwsem(&v9ses->rename_sem);
  284. rc = bdi_setup_and_register(&v9ses->bdi, "9p", BDI_CAP_MAP_COPY);
  285. if (rc) {
  286. kfree(v9ses->aname);
  287. kfree(v9ses->uname);
  288. return ERR_PTR(rc);
  289. }
  290. spin_lock(&v9fs_sessionlist_lock);
  291. list_add(&v9ses->slist, &v9fs_sessionlist);
  292. spin_unlock(&v9fs_sessionlist_lock);
  293. v9ses->uid = ~0;
  294. v9ses->dfltuid = V9FS_DEFUID;
  295. v9ses->dfltgid = V9FS_DEFGID;
  296. v9ses->clnt = p9_client_create(dev_name, data);
  297. if (IS_ERR(v9ses->clnt)) {
  298. retval = PTR_ERR(v9ses->clnt);
  299. v9ses->clnt = NULL;
  300. p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n");
  301. goto error;
  302. }
  303. v9ses->flags = V9FS_ACCESS_USER;
  304. if (p9_is_proto_dotl(v9ses->clnt)) {
  305. v9ses->flags = V9FS_ACCESS_CLIENT;
  306. v9ses->flags |= V9FS_PROTO_2000L;
  307. } else if (p9_is_proto_dotu(v9ses->clnt)) {
  308. v9ses->flags |= V9FS_PROTO_2000U;
  309. }
  310. rc = v9fs_parse_options(v9ses, data);
  311. if (rc < 0) {
  312. retval = rc;
  313. goto error;
  314. }
  315. v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
  316. if (!v9fs_proto_dotl(v9ses) &&
  317. ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
  318. /*
  319. * We support ACCESS_CLIENT only for dotl.
  320. * Fall back to ACCESS_USER
  321. */
  322. v9ses->flags &= ~V9FS_ACCESS_MASK;
  323. v9ses->flags |= V9FS_ACCESS_USER;
  324. }
  325. /*FIXME !! */
  326. /* for legacy mode, fall back to V9FS_ACCESS_ANY */
  327. if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
  328. ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
  329. v9ses->flags &= ~V9FS_ACCESS_MASK;
  330. v9ses->flags |= V9FS_ACCESS_ANY;
  331. v9ses->uid = ~0;
  332. }
  333. if (!v9fs_proto_dotl(v9ses) ||
  334. !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
  335. /*
  336. * We support ACL checks on clinet only if the protocol is
  337. * 9P2000.L and access is V9FS_ACCESS_CLIENT.
  338. */
  339. v9ses->flags &= ~V9FS_ACL_MASK;
  340. }
  341. fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, ~0,
  342. v9ses->aname);
  343. if (IS_ERR(fid)) {
  344. retval = PTR_ERR(fid);
  345. fid = NULL;
  346. p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
  347. goto error;
  348. }
  349. if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
  350. fid->uid = v9ses->uid;
  351. else
  352. fid->uid = ~0;
  353. #ifdef CONFIG_9P_FSCACHE
  354. /* register the session for caching */
  355. v9fs_cache_session_get_cookie(v9ses);
  356. #endif
  357. return fid;
  358. error:
  359. bdi_destroy(&v9ses->bdi);
  360. return ERR_PTR(retval);
  361. }
  362. /**
  363. * v9fs_session_close - shutdown a session
  364. * @v9ses: session information structure
  365. *
  366. */
  367. void v9fs_session_close(struct v9fs_session_info *v9ses)
  368. {
  369. if (v9ses->clnt) {
  370. p9_client_destroy(v9ses->clnt);
  371. v9ses->clnt = NULL;
  372. }
  373. #ifdef CONFIG_9P_FSCACHE
  374. if (v9ses->fscache) {
  375. v9fs_cache_session_put_cookie(v9ses);
  376. kfree(v9ses->cachetag);
  377. }
  378. #endif
  379. kfree(v9ses->uname);
  380. kfree(v9ses->aname);
  381. bdi_destroy(&v9ses->bdi);
  382. spin_lock(&v9fs_sessionlist_lock);
  383. list_del(&v9ses->slist);
  384. spin_unlock(&v9fs_sessionlist_lock);
  385. }
  386. /**
  387. * v9fs_session_cancel - terminate a session
  388. * @v9ses: session to terminate
  389. *
  390. * mark transport as disconnected and cancel all pending requests.
  391. */
  392. void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
  393. p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
  394. p9_client_disconnect(v9ses->clnt);
  395. }
  396. /**
  397. * v9fs_session_begin_cancel - Begin terminate of a session
  398. * @v9ses: session to terminate
  399. *
  400. * After this call we don't allow any request other than clunk.
  401. */
  402. void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
  403. {
  404. p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
  405. p9_client_begin_disconnect(v9ses->clnt);
  406. }
  407. extern int v9fs_error_init(void);
  408. static struct kobject *v9fs_kobj;
  409. #ifdef CONFIG_9P_FSCACHE
  410. /**
  411. * caches_show - list caches associated with a session
  412. *
  413. * Returns the size of buffer written.
  414. */
  415. static ssize_t caches_show(struct kobject *kobj,
  416. struct kobj_attribute *attr,
  417. char *buf)
  418. {
  419. ssize_t n = 0, count = 0, limit = PAGE_SIZE;
  420. struct v9fs_session_info *v9ses;
  421. spin_lock(&v9fs_sessionlist_lock);
  422. list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
  423. if (v9ses->cachetag) {
  424. n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
  425. if (n < 0) {
  426. count = n;
  427. break;
  428. }
  429. count += n;
  430. limit -= n;
  431. }
  432. }
  433. spin_unlock(&v9fs_sessionlist_lock);
  434. return count;
  435. }
  436. static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
  437. #endif /* CONFIG_9P_FSCACHE */
  438. static struct attribute *v9fs_attrs[] = {
  439. #ifdef CONFIG_9P_FSCACHE
  440. &v9fs_attr_cache.attr,
  441. #endif
  442. NULL,
  443. };
  444. static struct attribute_group v9fs_attr_group = {
  445. .attrs = v9fs_attrs,
  446. };
  447. /**
  448. * v9fs_sysfs_init - Initialize the v9fs sysfs interface
  449. *
  450. */
  451. static int v9fs_sysfs_init(void)
  452. {
  453. v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
  454. if (!v9fs_kobj)
  455. return -ENOMEM;
  456. if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
  457. kobject_put(v9fs_kobj);
  458. return -ENOMEM;
  459. }
  460. return 0;
  461. }
  462. /**
  463. * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
  464. *
  465. */
  466. static void v9fs_sysfs_cleanup(void)
  467. {
  468. sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
  469. kobject_put(v9fs_kobj);
  470. }
  471. static void v9fs_inode_init_once(void *foo)
  472. {
  473. struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
  474. #ifdef CONFIG_9P_FSCACHE
  475. v9inode->fscache = NULL;
  476. #endif
  477. memset(&v9inode->qid, 0, sizeof(v9inode->qid));
  478. inode_init_once(&v9inode->vfs_inode);
  479. }
  480. /**
  481. * v9fs_init_inode_cache - initialize a cache for 9P
  482. * Returns 0 on success.
  483. */
  484. static int v9fs_init_inode_cache(void)
  485. {
  486. v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
  487. sizeof(struct v9fs_inode),
  488. 0, (SLAB_RECLAIM_ACCOUNT|
  489. SLAB_MEM_SPREAD),
  490. v9fs_inode_init_once);
  491. if (!v9fs_inode_cache)
  492. return -ENOMEM;
  493. return 0;
  494. }
  495. /**
  496. * v9fs_destroy_inode_cache - destroy the cache of 9P inode
  497. *
  498. */
  499. static void v9fs_destroy_inode_cache(void)
  500. {
  501. /*
  502. * Make sure all delayed rcu free inodes are flushed before we
  503. * destroy cache.
  504. */
  505. rcu_barrier();
  506. kmem_cache_destroy(v9fs_inode_cache);
  507. }
  508. static int v9fs_cache_register(void)
  509. {
  510. int ret;
  511. ret = v9fs_init_inode_cache();
  512. if (ret < 0)
  513. return ret;
  514. #ifdef CONFIG_9P_FSCACHE
  515. return fscache_register_netfs(&v9fs_cache_netfs);
  516. #else
  517. return ret;
  518. #endif
  519. }
  520. static void v9fs_cache_unregister(void)
  521. {
  522. v9fs_destroy_inode_cache();
  523. #ifdef CONFIG_9P_FSCACHE
  524. fscache_unregister_netfs(&v9fs_cache_netfs);
  525. #endif
  526. }
  527. /**
  528. * init_v9fs - Initialize module
  529. *
  530. */
  531. static int __init init_v9fs(void)
  532. {
  533. int err;
  534. pr_info("Installing v9fs 9p2000 file system support\n");
  535. /* TODO: Setup list of registered trasnport modules */
  536. err = v9fs_cache_register();
  537. if (err < 0) {
  538. pr_err("Failed to register v9fs for caching\n");
  539. return err;
  540. }
  541. err = v9fs_sysfs_init();
  542. if (err < 0) {
  543. pr_err("Failed to register with sysfs\n");
  544. goto out_cache;
  545. }
  546. err = register_filesystem(&v9fs_fs_type);
  547. if (err < 0) {
  548. pr_err("Failed to register filesystem\n");
  549. goto out_sysfs_cleanup;
  550. }
  551. return 0;
  552. out_sysfs_cleanup:
  553. v9fs_sysfs_cleanup();
  554. out_cache:
  555. v9fs_cache_unregister();
  556. return err;
  557. }
  558. /**
  559. * exit_v9fs - shutdown module
  560. *
  561. */
  562. static void __exit exit_v9fs(void)
  563. {
  564. v9fs_sysfs_cleanup();
  565. v9fs_cache_unregister();
  566. unregister_filesystem(&v9fs_fs_type);
  567. }
  568. module_init(init_v9fs)
  569. module_exit(exit_v9fs)
  570. MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
  571. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  572. MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
  573. MODULE_LICENSE("GPL");