v9fs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. *
  40. */
  41. enum {
  42. /* Options that take integer arguments */
  43. Opt_debug, Opt_port, Opt_msize, Opt_uid, Opt_gid, Opt_afid,
  44. Opt_rfdno, Opt_wfdno,
  45. /* String options */
  46. Opt_uname, Opt_remotename,
  47. /* Options that take no arguments */
  48. Opt_legacy, Opt_nodevmap, Opt_unix, Opt_tcp, Opt_fd, Opt_pci,
  49. /* Cache options */
  50. Opt_cache_loose,
  51. /* Error token */
  52. Opt_err
  53. };
  54. static match_table_t tokens = {
  55. {Opt_debug, "debug=%x"},
  56. {Opt_port, "port=%u"},
  57. {Opt_msize, "msize=%u"},
  58. {Opt_uid, "uid=%u"},
  59. {Opt_gid, "gid=%u"},
  60. {Opt_afid, "afid=%u"},
  61. {Opt_rfdno, "rfdno=%u"},
  62. {Opt_wfdno, "wfdno=%u"},
  63. {Opt_uname, "uname=%s"},
  64. {Opt_remotename, "aname=%s"},
  65. {Opt_unix, "proto=unix"},
  66. {Opt_tcp, "proto=tcp"},
  67. {Opt_fd, "proto=fd"},
  68. #ifdef CONFIG_PCI_9P
  69. {Opt_pci, "proto=pci"},
  70. #endif
  71. {Opt_tcp, "tcp"},
  72. {Opt_unix, "unix"},
  73. {Opt_fd, "fd"},
  74. {Opt_legacy, "noextend"},
  75. {Opt_nodevmap, "nodevmap"},
  76. {Opt_cache_loose, "cache=loose"},
  77. {Opt_cache_loose, "loose"},
  78. {Opt_err, NULL}
  79. };
  80. extern struct p9_transport *p9pci_trans_create(void);
  81. /*
  82. * Parse option string.
  83. */
  84. /**
  85. * v9fs_parse_options - parse mount options into session structure
  86. * @options: options string passed from mount
  87. * @v9ses: existing v9fs session information
  88. *
  89. */
  90. static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses)
  91. {
  92. char *p;
  93. substring_t args[MAX_OPT_ARGS];
  94. int option;
  95. int ret;
  96. /* setup defaults */
  97. v9ses->port = V9FS_PORT;
  98. v9ses->maxdata = 9000;
  99. v9ses->proto = PROTO_TCP;
  100. v9ses->extended = 1;
  101. v9ses->afid = ~0;
  102. v9ses->debug = 0;
  103. v9ses->rfdno = ~0;
  104. v9ses->wfdno = ~0;
  105. v9ses->cache = 0;
  106. if (!options)
  107. return;
  108. while ((p = strsep(&options, ",")) != NULL) {
  109. int token;
  110. if (!*p)
  111. continue;
  112. token = match_token(p, tokens, args);
  113. if (token < Opt_uname) {
  114. if ((ret = match_int(&args[0], &option)) < 0) {
  115. P9_DPRINTK(P9_DEBUG_ERROR,
  116. "integer field, but no integer?\n");
  117. continue;
  118. }
  119. }
  120. switch (token) {
  121. case Opt_debug:
  122. v9ses->debug = option;
  123. #ifdef CONFIG_NET_9P_DEBUG
  124. p9_debug_level = option;
  125. #endif
  126. break;
  127. case Opt_port:
  128. v9ses->port = option;
  129. break;
  130. case Opt_msize:
  131. v9ses->maxdata = option;
  132. break;
  133. case Opt_uid:
  134. v9ses->uid = option;
  135. break;
  136. case Opt_gid:
  137. v9ses->gid = option;
  138. break;
  139. case Opt_afid:
  140. v9ses->afid = option;
  141. break;
  142. case Opt_rfdno:
  143. v9ses->rfdno = option;
  144. break;
  145. case Opt_wfdno:
  146. v9ses->wfdno = option;
  147. break;
  148. case Opt_tcp:
  149. v9ses->proto = PROTO_TCP;
  150. break;
  151. case Opt_unix:
  152. v9ses->proto = PROTO_UNIX;
  153. break;
  154. case Opt_pci:
  155. v9ses->proto = PROTO_PCI;
  156. break;
  157. case Opt_fd:
  158. v9ses->proto = PROTO_FD;
  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_transport *trans;
  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. v9fs_parse_options(data, v9ses);
  204. switch (v9ses->proto) {
  205. case PROTO_TCP:
  206. trans = p9_trans_create_tcp(dev_name, v9ses->port);
  207. break;
  208. case PROTO_UNIX:
  209. trans = p9_trans_create_unix(dev_name);
  210. *v9ses->remotename = 0;
  211. break;
  212. case PROTO_FD:
  213. trans = p9_trans_create_fd(v9ses->rfdno, v9ses->wfdno);
  214. *v9ses->remotename = 0;
  215. break;
  216. #ifdef CONFIG_PCI_9P
  217. case PROTO_PCI:
  218. trans = p9pci_trans_create();
  219. *v9ses->remotename = 0;
  220. break;
  221. #endif
  222. default:
  223. printk(KERN_ERR "v9fs: Bad mount protocol %d\n", v9ses->proto);
  224. retval = -ENOPROTOOPT;
  225. goto error;
  226. };
  227. if (IS_ERR(trans)) {
  228. retval = PTR_ERR(trans);
  229. trans = NULL;
  230. goto error;
  231. }
  232. v9ses->clnt = p9_client_create(trans, v9ses->maxdata + P9_IOHDRSZ,
  233. v9ses->extended);
  234. if (IS_ERR(v9ses->clnt)) {
  235. retval = PTR_ERR(v9ses->clnt);
  236. v9ses->clnt = NULL;
  237. P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
  238. goto error;
  239. }
  240. fid = p9_client_attach(v9ses->clnt, NULL, v9ses->name,
  241. v9ses->remotename);
  242. if (IS_ERR(fid)) {
  243. retval = PTR_ERR(fid);
  244. fid = NULL;
  245. P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
  246. goto error;
  247. }
  248. return fid;
  249. error:
  250. v9fs_session_close(v9ses);
  251. return ERR_PTR(retval);
  252. }
  253. /**
  254. * v9fs_session_close - shutdown a session
  255. * @v9ses: session information structure
  256. *
  257. */
  258. void v9fs_session_close(struct v9fs_session_info *v9ses)
  259. {
  260. if (v9ses->clnt) {
  261. p9_client_destroy(v9ses->clnt);
  262. v9ses->clnt = NULL;
  263. }
  264. __putname(v9ses->name);
  265. __putname(v9ses->remotename);
  266. }
  267. /**
  268. * v9fs_session_cancel - mark transport as disconnected
  269. * and cancel all pending requests.
  270. */
  271. void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
  272. P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
  273. p9_client_disconnect(v9ses->clnt);
  274. }
  275. extern int v9fs_error_init(void);
  276. /**
  277. * v9fs_init - Initialize module
  278. *
  279. */
  280. static int __init init_v9fs(void)
  281. {
  282. printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
  283. return register_filesystem(&v9fs_fs_type);
  284. }
  285. /**
  286. * v9fs_init - shutdown module
  287. *
  288. */
  289. static void __exit exit_v9fs(void)
  290. {
  291. unregister_filesystem(&v9fs_fs_type);
  292. }
  293. module_init(init_v9fs)
  294. module_exit(exit_v9fs)
  295. MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
  296. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  297. MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
  298. MODULE_LICENSE("GPL");