v9fs.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to:
  21. * Free Software Foundation
  22. * 51 Franklin Street, Fifth Floor
  23. * Boston, MA 02111-1301 USA
  24. *
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/errno.h>
  29. #include <linux/fs.h>
  30. #include <linux/parser.h>
  31. #include <linux/idr.h>
  32. #include "debug.h"
  33. #include "v9fs.h"
  34. #include "9p.h"
  35. #include "v9fs_vfs.h"
  36. #include "transport.h"
  37. #include "mux.h"
  38. #include "conv.h"
  39. /* TODO: sysfs or debugfs interface */
  40. int v9fs_debug_level = 0; /* feature-rific global debug level */
  41. /*
  42. * Option Parsing (code inspired by NFS code)
  43. *
  44. */
  45. enum {
  46. /* Options that take integer arguments */
  47. Opt_port, Opt_msize, Opt_uid, Opt_gid, Opt_afid, Opt_debug,
  48. Opt_rfdno, Opt_wfdno,
  49. /* String options */
  50. Opt_name, Opt_remotename,
  51. /* Options that take no arguments */
  52. Opt_legacy, Opt_nodevmap, Opt_unix, Opt_tcp, Opt_fd,
  53. /* Error token */
  54. Opt_err
  55. };
  56. static match_table_t tokens = {
  57. {Opt_port, "port=%u"},
  58. {Opt_msize, "msize=%u"},
  59. {Opt_uid, "uid=%u"},
  60. {Opt_gid, "gid=%u"},
  61. {Opt_afid, "afid=%u"},
  62. {Opt_rfdno, "rfdno=%u"},
  63. {Opt_wfdno, "wfdno=%u"},
  64. {Opt_debug, "debug=%u"},
  65. {Opt_name, "name=%s"},
  66. {Opt_remotename, "aname=%s"},
  67. {Opt_unix, "proto=unix"},
  68. {Opt_tcp, "proto=tcp"},
  69. {Opt_fd, "proto=fd"},
  70. {Opt_tcp, "tcp"},
  71. {Opt_unix, "unix"},
  72. {Opt_fd, "fd"},
  73. {Opt_legacy, "noextend"},
  74. {Opt_nodevmap, "nodevmap"},
  75. {Opt_err, NULL}
  76. };
  77. /*
  78. * Parse option string.
  79. */
  80. /**
  81. * v9fs_parse_options - parse mount options into session structure
  82. * @options: options string passed from mount
  83. * @v9ses: existing v9fs session information
  84. *
  85. */
  86. static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses)
  87. {
  88. char *p;
  89. substring_t args[MAX_OPT_ARGS];
  90. int option;
  91. int ret;
  92. /* setup defaults */
  93. v9ses->port = V9FS_PORT;
  94. v9ses->maxdata = 9000;
  95. v9ses->proto = PROTO_TCP;
  96. v9ses->extended = 1;
  97. v9ses->afid = ~0;
  98. v9ses->debug = 0;
  99. v9ses->rfdno = ~0;
  100. v9ses->wfdno = ~0;
  101. if (!options)
  102. return;
  103. while ((p = strsep(&options, ",")) != NULL) {
  104. int token;
  105. if (!*p)
  106. continue;
  107. token = match_token(p, tokens, args);
  108. if (token < Opt_name) {
  109. if ((ret = match_int(&args[0], &option)) < 0) {
  110. dprintk(DEBUG_ERROR,
  111. "integer field, but no integer?\n");
  112. continue;
  113. }
  114. }
  115. switch (token) {
  116. case Opt_port:
  117. v9ses->port = option;
  118. break;
  119. case Opt_msize:
  120. v9ses->maxdata = option;
  121. break;
  122. case Opt_uid:
  123. v9ses->uid = option;
  124. break;
  125. case Opt_gid:
  126. v9ses->gid = option;
  127. break;
  128. case Opt_afid:
  129. v9ses->afid = option;
  130. break;
  131. case Opt_rfdno:
  132. v9ses->rfdno = option;
  133. break;
  134. case Opt_wfdno:
  135. v9ses->wfdno = option;
  136. break;
  137. case Opt_debug:
  138. v9ses->debug = option;
  139. break;
  140. case Opt_tcp:
  141. v9ses->proto = PROTO_TCP;
  142. break;
  143. case Opt_unix:
  144. v9ses->proto = PROTO_UNIX;
  145. break;
  146. case Opt_fd:
  147. v9ses->proto = PROTO_FD;
  148. break;
  149. case Opt_name:
  150. match_strcpy(v9ses->name, &args[0]);
  151. break;
  152. case Opt_remotename:
  153. match_strcpy(v9ses->remotename, &args[0]);
  154. break;
  155. case Opt_legacy:
  156. v9ses->extended = 0;
  157. break;
  158. case Opt_nodevmap:
  159. v9ses->nodev = 1;
  160. break;
  161. default:
  162. continue;
  163. }
  164. }
  165. }
  166. /**
  167. * v9fs_inode2v9ses - safely extract v9fs session info from super block
  168. * @inode: inode to extract information from
  169. *
  170. * Paranoid function to extract v9ses information from superblock,
  171. * if anything is missing it will report an error.
  172. *
  173. */
  174. struct v9fs_session_info *v9fs_inode2v9ses(struct inode *inode)
  175. {
  176. return (inode->i_sb->s_fs_info);
  177. }
  178. /**
  179. * v9fs_get_idpool - allocate numeric id from pool
  180. * @p - pool to allocate from
  181. *
  182. * XXX - This seems to be an awful generic function, should it be in idr.c with
  183. * the lock included in struct idr?
  184. */
  185. int v9fs_get_idpool(struct v9fs_idpool *p)
  186. {
  187. int i = 0;
  188. int error;
  189. retry:
  190. if (idr_pre_get(&p->pool, GFP_KERNEL) == 0)
  191. return 0;
  192. if (down_interruptible(&p->lock) == -EINTR) {
  193. eprintk(KERN_WARNING, "Interrupted while locking\n");
  194. return -1;
  195. }
  196. error = idr_get_new(&p->pool, NULL, &i);
  197. up(&p->lock);
  198. if (error == -EAGAIN)
  199. goto retry;
  200. else if (error)
  201. return -1;
  202. return i;
  203. }
  204. /**
  205. * v9fs_put_idpool - release numeric id from pool
  206. * @p - pool to allocate from
  207. *
  208. * XXX - This seems to be an awful generic function, should it be in idr.c with
  209. * the lock included in struct idr?
  210. */
  211. void v9fs_put_idpool(int id, struct v9fs_idpool *p)
  212. {
  213. if (down_interruptible(&p->lock) == -EINTR) {
  214. eprintk(KERN_WARNING, "Interrupted while locking\n");
  215. return;
  216. }
  217. idr_remove(&p->pool, id);
  218. up(&p->lock);
  219. }
  220. /**
  221. * v9fs_session_init - initialize session
  222. * @v9ses: session information structure
  223. * @dev_name: device being mounted
  224. * @data: options
  225. *
  226. */
  227. int
  228. v9fs_session_init(struct v9fs_session_info *v9ses,
  229. const char *dev_name, char *data)
  230. {
  231. struct v9fs_fcall *fcall = NULL;
  232. struct v9fs_transport *trans_proto;
  233. int n = 0;
  234. int newfid = -1;
  235. int retval = -EINVAL;
  236. v9ses->name = __getname();
  237. if (!v9ses->name)
  238. return -ENOMEM;
  239. v9ses->remotename = __getname();
  240. if (!v9ses->remotename) {
  241. putname(v9ses->name);
  242. return -ENOMEM;
  243. }
  244. strcpy(v9ses->name, V9FS_DEFUSER);
  245. strcpy(v9ses->remotename, V9FS_DEFANAME);
  246. v9fs_parse_options(data, v9ses);
  247. /* set global debug level */
  248. v9fs_debug_level = v9ses->debug;
  249. /* id pools that are session-dependent: FIDs and TIDs */
  250. idr_init(&v9ses->fidpool.pool);
  251. init_MUTEX(&v9ses->fidpool.lock);
  252. idr_init(&v9ses->tidpool.pool);
  253. init_MUTEX(&v9ses->tidpool.lock);
  254. switch (v9ses->proto) {
  255. case PROTO_TCP:
  256. trans_proto = &v9fs_trans_tcp;
  257. break;
  258. case PROTO_UNIX:
  259. trans_proto = &v9fs_trans_unix;
  260. *v9ses->remotename = 0;
  261. break;
  262. case PROTO_FD:
  263. trans_proto = &v9fs_trans_fd;
  264. *v9ses->remotename = 0;
  265. break;
  266. default:
  267. printk(KERN_ERR "v9fs: Bad mount protocol %d\n", v9ses->proto);
  268. retval = -ENOPROTOOPT;
  269. goto SessCleanUp;
  270. };
  271. v9ses->transport = trans_proto;
  272. if ((retval = v9ses->transport->init(v9ses, dev_name, data)) < 0) {
  273. eprintk(KERN_ERR, "problem initializing transport\n");
  274. goto SessCleanUp;
  275. }
  276. v9ses->inprogress = 0;
  277. v9ses->shutdown = 0;
  278. v9ses->session_hung = 0;
  279. if ((retval = v9fs_mux_init(v9ses, dev_name)) < 0) {
  280. dprintk(DEBUG_ERROR, "problem initializing mux\n");
  281. goto SessCleanUp;
  282. }
  283. if (v9ses->afid == ~0) {
  284. if (v9ses->extended)
  285. retval =
  286. v9fs_t_version(v9ses, v9ses->maxdata, "9P2000.u",
  287. &fcall);
  288. else
  289. retval = v9fs_t_version(v9ses, v9ses->maxdata, "9P2000",
  290. &fcall);
  291. if (retval < 0) {
  292. dprintk(DEBUG_ERROR, "v9fs_t_version failed\n");
  293. goto FreeFcall;
  294. }
  295. /* Really should check for 9P1 and report error */
  296. if (!strcmp(fcall->params.rversion.version, "9P2000.u")) {
  297. dprintk(DEBUG_9P, "9P2000 UNIX extensions enabled\n");
  298. v9ses->extended = 1;
  299. } else {
  300. dprintk(DEBUG_9P, "9P2000 legacy mode enabled\n");
  301. v9ses->extended = 0;
  302. }
  303. n = fcall->params.rversion.msize;
  304. kfree(fcall);
  305. if (n < v9ses->maxdata)
  306. v9ses->maxdata = n;
  307. }
  308. newfid = v9fs_get_idpool(&v9ses->fidpool);
  309. if (newfid < 0) {
  310. eprintk(KERN_WARNING, "couldn't allocate FID\n");
  311. retval = -ENOMEM;
  312. goto SessCleanUp;
  313. }
  314. /* it is a little bit ugly, but we have to prevent newfid */
  315. /* being the same as afid, so if it is, get a new fid */
  316. if (v9ses->afid != ~0 && newfid == v9ses->afid) {
  317. newfid = v9fs_get_idpool(&v9ses->fidpool);
  318. if (newfid < 0) {
  319. eprintk(KERN_WARNING, "couldn't allocate FID\n");
  320. retval = -ENOMEM;
  321. goto SessCleanUp;
  322. }
  323. }
  324. if ((retval =
  325. v9fs_t_attach(v9ses, v9ses->name, v9ses->remotename, newfid,
  326. v9ses->afid, NULL))
  327. < 0) {
  328. dprintk(DEBUG_ERROR, "cannot attach\n");
  329. goto SessCleanUp;
  330. }
  331. if (v9ses->afid != ~0) {
  332. if (v9fs_t_clunk(v9ses, v9ses->afid, NULL))
  333. dprintk(DEBUG_ERROR, "clunk failed\n");
  334. }
  335. return newfid;
  336. FreeFcall:
  337. kfree(fcall);
  338. SessCleanUp:
  339. v9fs_session_close(v9ses);
  340. return retval;
  341. }
  342. /**
  343. * v9fs_session_close - shutdown a session
  344. * @v9ses: session information structure
  345. *
  346. */
  347. void v9fs_session_close(struct v9fs_session_info *v9ses)
  348. {
  349. if (v9ses->recvproc) {
  350. send_sig(SIGKILL, v9ses->recvproc, 1);
  351. wait_for_completion(&v9ses->proccmpl);
  352. }
  353. if (v9ses->transport)
  354. v9ses->transport->close(v9ses->transport);
  355. putname(v9ses->name);
  356. putname(v9ses->remotename);
  357. }
  358. /**
  359. * v9fs_session_cancel - mark transport as disconnected
  360. * and cancel all pending requests.
  361. */
  362. void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
  363. v9ses->transport->status = Disconnected;
  364. v9fs_mux_cancel_requests(v9ses, -EIO);
  365. }
  366. extern int v9fs_error_init(void);
  367. /**
  368. * v9fs_init - Initialize module
  369. *
  370. */
  371. static int __init init_v9fs(void)
  372. {
  373. v9fs_error_init();
  374. printk(KERN_INFO "Installing v9fs 9P2000 file system support\n");
  375. return register_filesystem(&v9fs_fs_type);
  376. }
  377. /**
  378. * v9fs_init - shutdown module
  379. *
  380. */
  381. static void __exit exit_v9fs(void)
  382. {
  383. unregister_filesystem(&v9fs_fs_type);
  384. }
  385. module_init(init_v9fs)
  386. module_exit(exit_v9fs)
  387. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  388. MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
  389. MODULE_LICENSE("GPL");