v9fs.c 6.5 KB

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