v9fs.c 10 KB

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