v9fs.c 13 KB

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