v9fs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. p9_debug_level = option;
  124. break;
  125. case Opt_port:
  126. v9ses->port = option;
  127. break;
  128. case Opt_msize:
  129. v9ses->maxdata = option;
  130. break;
  131. case Opt_uid:
  132. v9ses->uid = option;
  133. break;
  134. case Opt_gid:
  135. v9ses->gid = option;
  136. break;
  137. case Opt_afid:
  138. v9ses->afid = option;
  139. break;
  140. case Opt_rfdno:
  141. v9ses->rfdno = option;
  142. break;
  143. case Opt_wfdno:
  144. v9ses->wfdno = option;
  145. break;
  146. case Opt_tcp:
  147. v9ses->proto = PROTO_TCP;
  148. break;
  149. case Opt_unix:
  150. v9ses->proto = PROTO_UNIX;
  151. break;
  152. case Opt_pci:
  153. v9ses->proto = PROTO_PCI;
  154. break;
  155. case Opt_fd:
  156. v9ses->proto = PROTO_FD;
  157. break;
  158. case Opt_uname:
  159. match_strcpy(v9ses->name, &args[0]);
  160. break;
  161. case Opt_remotename:
  162. match_strcpy(v9ses->remotename, &args[0]);
  163. break;
  164. case Opt_legacy:
  165. v9ses->extended = 0;
  166. break;
  167. case Opt_nodevmap:
  168. v9ses->nodev = 1;
  169. break;
  170. case Opt_cache_loose:
  171. v9ses->cache = CACHE_LOOSE;
  172. break;
  173. default:
  174. continue;
  175. }
  176. }
  177. }
  178. /**
  179. * v9fs_session_init - initialize session
  180. * @v9ses: session information structure
  181. * @dev_name: device being mounted
  182. * @data: options
  183. *
  184. */
  185. struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
  186. const char *dev_name, char *data)
  187. {
  188. int retval = -EINVAL;
  189. struct p9_transport *trans;
  190. struct p9_fid *fid;
  191. v9ses->name = __getname();
  192. if (!v9ses->name)
  193. return ERR_PTR(-ENOMEM);
  194. v9ses->remotename = __getname();
  195. if (!v9ses->remotename) {
  196. __putname(v9ses->name);
  197. return ERR_PTR(-ENOMEM);
  198. }
  199. strcpy(v9ses->name, V9FS_DEFUSER);
  200. strcpy(v9ses->remotename, V9FS_DEFANAME);
  201. v9fs_parse_options(data, v9ses);
  202. switch (v9ses->proto) {
  203. case PROTO_TCP:
  204. trans = p9_trans_create_tcp(dev_name, v9ses->port);
  205. break;
  206. case PROTO_UNIX:
  207. trans = p9_trans_create_unix(dev_name);
  208. *v9ses->remotename = 0;
  209. break;
  210. case PROTO_FD:
  211. trans = p9_trans_create_fd(v9ses->rfdno, v9ses->wfdno);
  212. *v9ses->remotename = 0;
  213. break;
  214. #ifdef CONFIG_PCI_9P
  215. case PROTO_PCI:
  216. trans = p9pci_trans_create();
  217. *v9ses->remotename = 0;
  218. break;
  219. #endif
  220. default:
  221. printk(KERN_ERR "v9fs: Bad mount protocol %d\n", v9ses->proto);
  222. retval = -ENOPROTOOPT;
  223. goto error;
  224. };
  225. if (IS_ERR(trans)) {
  226. retval = PTR_ERR(trans);
  227. trans = NULL;
  228. goto error;
  229. }
  230. v9ses->clnt = p9_client_create(trans, v9ses->maxdata + P9_IOHDRSZ,
  231. v9ses->extended);
  232. if (IS_ERR(v9ses->clnt)) {
  233. retval = PTR_ERR(v9ses->clnt);
  234. v9ses->clnt = NULL;
  235. P9_DPRINTK(P9_DEBUG_ERROR, "problem initializing 9p client\n");
  236. goto error;
  237. }
  238. fid = p9_client_attach(v9ses->clnt, NULL, v9ses->name,
  239. v9ses->remotename);
  240. if (IS_ERR(fid)) {
  241. retval = PTR_ERR(fid);
  242. fid = NULL;
  243. P9_DPRINTK(P9_DEBUG_ERROR, "cannot attach\n");
  244. goto error;
  245. }
  246. return fid;
  247. error:
  248. v9fs_session_close(v9ses);
  249. return ERR_PTR(retval);
  250. }
  251. /**
  252. * v9fs_session_close - shutdown a session
  253. * @v9ses: session information structure
  254. *
  255. */
  256. void v9fs_session_close(struct v9fs_session_info *v9ses)
  257. {
  258. if (v9ses->clnt) {
  259. p9_client_destroy(v9ses->clnt);
  260. v9ses->clnt = NULL;
  261. }
  262. __putname(v9ses->name);
  263. __putname(v9ses->remotename);
  264. }
  265. /**
  266. * v9fs_session_cancel - mark transport as disconnected
  267. * and cancel all pending requests.
  268. */
  269. void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
  270. P9_DPRINTK(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
  271. p9_client_disconnect(v9ses->clnt);
  272. }
  273. extern int v9fs_error_init(void);
  274. /**
  275. * v9fs_init - Initialize module
  276. *
  277. */
  278. static int __init init_v9fs(void)
  279. {
  280. printk(KERN_INFO "Installing v9fs 9p2000 file system support\n");
  281. return register_filesystem(&v9fs_fs_type);
  282. }
  283. /**
  284. * v9fs_init - shutdown module
  285. *
  286. */
  287. static void __exit exit_v9fs(void)
  288. {
  289. unregister_filesystem(&v9fs_fs_type);
  290. }
  291. module_init(init_v9fs)
  292. module_exit(exit_v9fs)
  293. MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
  294. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  295. MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
  296. MODULE_LICENSE("GPL");