v9fs.c 11 KB

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