v9fs.c 11 KB

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