v9fs.c 6.9 KB

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