v9fs.c 10 KB

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