v9fs.c 13 KB

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