v9fs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * linux/fs/9p/v9fs.c
  3. *
  4. * This file contains functions assisting in mapping VFS to 9P2000
  5. *
  6. * Copyright (C) 2004 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/transport.h>
  33. #include <net/9p/conn.h>
  34. #include <net/9p/client.h>
  35. #include "v9fs.h"
  36. #include "v9fs_vfs.h"
  37. /*
  38. * Dynamic Transport Registration Routines
  39. *
  40. */
  41. static LIST_HEAD(v9fs_trans_list);
  42. static struct p9_trans_module *v9fs_default_trans;
  43. /**
  44. * v9fs_register_trans - register a new transport with 9p
  45. * @m - structure describing the transport module and entry points
  46. *
  47. */
  48. void v9fs_register_trans(struct p9_trans_module *m)
  49. {
  50. list_add_tail(&m->list, &v9fs_trans_list);
  51. if (m->def)
  52. v9fs_default_trans = m;
  53. }
  54. EXPORT_SYMBOL(v9fs_register_trans);
  55. /**
  56. * v9fs_match_trans - match transport versus registered transports
  57. * @arg: string identifying transport
  58. *
  59. */
  60. static struct p9_trans_module *v9fs_match_trans(const substring_t *name)
  61. {
  62. struct list_head *p;
  63. struct p9_trans_module *t = NULL;
  64. list_for_each(p, &v9fs_trans_list) {
  65. t = list_entry(p, struct p9_trans_module, list);
  66. if (strncmp(t->name, name->from, name->to-name->from) == 0) {
  67. P9_DPRINTK(P9_DEBUG_TRANS, "trans=%s\n", t->name);
  68. break;
  69. }
  70. }
  71. return t;
  72. }
  73. /*
  74. * Option Parsing (code inspired by NFS code)
  75. * NOTE: each transport will parse its own options
  76. */
  77. enum {
  78. /* Options that take integer arguments */
  79. Opt_debug, Opt_msize, Opt_uid, Opt_gid, Opt_afid,
  80. /* String options */
  81. Opt_uname, Opt_remotename, Opt_trans,
  82. /* Options that take no arguments */
  83. Opt_legacy, Opt_nodevmap,
  84. /* Cache options */
  85. Opt_cache_loose,
  86. /* Error token */
  87. Opt_err
  88. };
  89. static match_table_t tokens = {
  90. {Opt_debug, "debug=%x"},
  91. {Opt_msize, "msize=%u"},
  92. {Opt_uid, "uid=%u"},
  93. {Opt_gid, "gid=%u"},
  94. {Opt_afid, "afid=%u"},
  95. {Opt_uname, "uname=%s"},
  96. {Opt_remotename, "aname=%s"},
  97. {Opt_trans, "trans=%s"},
  98. {Opt_legacy, "noextend"},
  99. {Opt_nodevmap, "nodevmap"},
  100. {Opt_cache_loose, "cache=loose"},
  101. {Opt_cache_loose, "loose"},
  102. {Opt_err, NULL}
  103. };
  104. /**
  105. * v9fs_parse_options - parse mount options into session structure
  106. * @options: options string passed from mount
  107. * @v9ses: existing v9fs session information
  108. *
  109. */
  110. static void v9fs_parse_options(struct v9fs_session_info *v9ses)
  111. {
  112. char *options = v9ses->options;
  113. substring_t args[MAX_OPT_ARGS];
  114. char *p;
  115. int option;
  116. int ret;
  117. /* setup defaults */
  118. v9ses->maxdata = 8192;
  119. v9ses->extended = 1;
  120. v9ses->afid = ~0;
  121. v9ses->debug = 0;
  122. v9ses->cache = 0;
  123. v9ses->trans = v9fs_default_trans;
  124. if (!options)
  125. return;
  126. while ((p = strsep(&options, ",")) != NULL) {
  127. int token;
  128. if (!*p)
  129. continue;
  130. token = match_token(p, tokens, args);
  131. if (token < Opt_uname) {
  132. if ((ret = match_int(&args[0], &option)) < 0) {
  133. P9_DPRINTK(P9_DEBUG_ERROR,
  134. "integer field, but no integer?\n");
  135. continue;
  136. }
  137. }
  138. switch (token) {
  139. case Opt_debug:
  140. v9ses->debug = option;
  141. #ifdef CONFIG_NET_9P_DEBUG
  142. p9_debug_level = option;
  143. #endif
  144. break;
  145. case Opt_msize:
  146. v9ses->maxdata = option;
  147. break;
  148. case Opt_uid:
  149. v9ses->uid = option;
  150. break;
  151. case Opt_gid:
  152. v9ses->gid = option;
  153. break;
  154. case Opt_afid:
  155. v9ses->afid = option;
  156. break;
  157. case Opt_trans:
  158. v9ses->trans = v9fs_match_trans(&args[0]);
  159. break;
  160. case Opt_uname:
  161. match_strcpy(v9ses->name, &args[0]);
  162. break;
  163. case Opt_remotename:
  164. match_strcpy(v9ses->remotename, &args[0]);
  165. break;
  166. case Opt_legacy:
  167. v9ses->extended = 0;
  168. break;
  169. case Opt_nodevmap:
  170. v9ses->nodev = 1;
  171. break;
  172. case Opt_cache_loose:
  173. v9ses->cache = CACHE_LOOSE;
  174. break;
  175. default:
  176. continue;
  177. }
  178. }
  179. }
  180. /**
  181. * v9fs_session_init - initialize session
  182. * @v9ses: session information structure
  183. * @dev_name: device being mounted
  184. * @data: options
  185. *
  186. */
  187. struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
  188. const char *dev_name, char *data)
  189. {
  190. int retval = -EINVAL;
  191. struct p9_trans *trans = NULL;
  192. struct p9_fid *fid;
  193. v9ses->name = __getname();
  194. if (!v9ses->name)
  195. return ERR_PTR(-ENOMEM);
  196. v9ses->remotename = __getname();
  197. if (!v9ses->remotename) {
  198. __putname(v9ses->name);
  199. return ERR_PTR(-ENOMEM);
  200. }
  201. strcpy(v9ses->name, V9FS_DEFUSER);
  202. strcpy(v9ses->remotename, V9FS_DEFANAME);
  203. v9ses->options = kstrdup(data, GFP_KERNEL);
  204. v9fs_parse_options(v9ses);
  205. if ((v9ses->trans == NULL) && !list_empty(&v9fs_trans_list))
  206. v9ses->trans = list_first_entry(&v9fs_trans_list,
  207. struct p9_trans_module, list);
  208. if (v9ses->trans == NULL) {
  209. retval = -EPROTONOSUPPORT;
  210. P9_DPRINTK(P9_DEBUG_ERROR,
  211. "No transport defined or default transport\n");
  212. goto error;
  213. }
  214. trans = v9ses->trans->create(dev_name, v9ses->options);
  215. if (IS_ERR(trans)) {
  216. retval = PTR_ERR(trans);
  217. trans = NULL;
  218. goto error;
  219. }
  220. if ((v9ses->maxdata+P9_IOHDRSZ) > v9ses->trans->maxsize)
  221. v9ses->maxdata = v9ses->trans->maxsize-P9_IOHDRSZ;
  222. v9ses->clnt = p9_client_create(trans, v9ses->maxdata+P9_IOHDRSZ,
  223. v9ses->extended);
  224. if (IS_ERR(v9ses->clnt)) {
  225. retval = PTR_ERR(v9ses->clnt);
  226. v9ses->clnt = NULL;
  227. P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
  228. goto error;
  229. }
  230. fid = p9_client_attach(v9ses->clnt, NULL, v9ses->name,
  231. v9ses->remotename);
  232. if (IS_ERR(fid)) {
  233. retval = PTR_ERR(fid);
  234. fid = NULL;
  235. P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
  236. goto error;
  237. }
  238. return fid;
  239. error:
  240. v9fs_session_close(v9ses);
  241. return ERR_PTR(retval);
  242. }
  243. /**
  244. * v9fs_session_close - shutdown a session
  245. * @v9ses: session information structure
  246. *
  247. */
  248. void v9fs_session_close(struct v9fs_session_info *v9ses)
  249. {
  250. if (v9ses->clnt) {
  251. p9_client_destroy(v9ses->clnt);
  252. v9ses->clnt = NULL;
  253. }
  254. __putname(v9ses->name);
  255. __putname(v9ses->remotename);
  256. kfree(v9ses->options);
  257. }
  258. /**
  259. * v9fs_session_cancel - mark transport as disconnected
  260. * and cancel all pending requests.
  261. */
  262. void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
  263. P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
  264. p9_client_disconnect(v9ses->clnt);
  265. }
  266. extern int v9fs_error_init(void);
  267. /**
  268. * v9fs_init - Initialize module
  269. *
  270. */
  271. static int __init init_v9fs(void)
  272. {
  273. printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
  274. /* TODO: Setup list of registered trasnport modules */
  275. return register_filesystem(&v9fs_fs_type);
  276. }
  277. /**
  278. * v9fs_init - shutdown module
  279. *
  280. */
  281. static void __exit exit_v9fs(void)
  282. {
  283. unregister_filesystem(&v9fs_fs_type);
  284. }
  285. module_init(init_v9fs)
  286. module_exit(exit_v9fs)
  287. MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
  288. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  289. MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
  290. MODULE_LICENSE("GPL");