v9fs.c 7.3 KB

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