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 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. /* TODO: sysfs or debugfs interface */
  39. int v9fs_debug_level = 0; /* feature-rific global debug level */
  40. /*
  41. * Option Parsing (code inspired by NFS code)
  42. *
  43. */
  44. enum {
  45. /* Options that take integer arguments */
  46. Opt_port, Opt_msize, Opt_uid, Opt_gid, Opt_afid, Opt_debug,
  47. Opt_rfdno, Opt_wfdno,
  48. /* String options */
  49. Opt_name, Opt_remotename,
  50. /* Options that take no arguments */
  51. Opt_legacy, Opt_nodevmap, Opt_unix, Opt_tcp, Opt_fd,
  52. /* Error token */
  53. Opt_err
  54. };
  55. static match_table_t tokens = {
  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_debug, "debug=%u"},
  64. {Opt_name, "name=%s"},
  65. {Opt_remotename, "aname=%s"},
  66. {Opt_unix, "proto=unix"},
  67. {Opt_tcp, "proto=tcp"},
  68. {Opt_fd, "proto=fd"},
  69. {Opt_tcp, "tcp"},
  70. {Opt_unix, "unix"},
  71. {Opt_fd, "fd"},
  72. {Opt_legacy, "noextend"},
  73. {Opt_nodevmap, "nodevmap"},
  74. {Opt_err, NULL}
  75. };
  76. /*
  77. * Parse option string.
  78. */
  79. /**
  80. * v9fs_parse_options - parse mount options into session structure
  81. * @options: options string passed from mount
  82. * @v9ses: existing v9fs session information
  83. *
  84. */
  85. static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses)
  86. {
  87. char *p;
  88. substring_t args[MAX_OPT_ARGS];
  89. int option;
  90. int ret;
  91. /* setup defaults */
  92. v9ses->port = V9FS_PORT;
  93. v9ses->maxdata = 9000;
  94. v9ses->proto = PROTO_TCP;
  95. v9ses->extended = 1;
  96. v9ses->afid = ~0;
  97. v9ses->debug = 0;
  98. v9ses->rfdno = ~0;
  99. v9ses->wfdno = ~0;
  100. if (!options)
  101. return;
  102. while ((p = strsep(&options, ",")) != NULL) {
  103. int token;
  104. if (!*p)
  105. continue;
  106. token = match_token(p, tokens, args);
  107. if (token < Opt_name) {
  108. if ((ret = match_int(&args[0], &option)) < 0) {
  109. dprintk(DEBUG_ERROR,
  110. "integer field, but no integer?\n");
  111. continue;
  112. }
  113. }
  114. switch (token) {
  115. case Opt_port:
  116. v9ses->port = option;
  117. break;
  118. case Opt_msize:
  119. v9ses->maxdata = option;
  120. break;
  121. case Opt_uid:
  122. v9ses->uid = option;
  123. break;
  124. case Opt_gid:
  125. v9ses->gid = option;
  126. break;
  127. case Opt_afid:
  128. v9ses->afid = option;
  129. break;
  130. case Opt_rfdno:
  131. v9ses->rfdno = option;
  132. break;
  133. case Opt_wfdno:
  134. v9ses->wfdno = option;
  135. break;
  136. case Opt_debug:
  137. v9ses->debug = option;
  138. break;
  139. case Opt_tcp:
  140. v9ses->proto = PROTO_TCP;
  141. break;
  142. case Opt_unix:
  143. v9ses->proto = PROTO_UNIX;
  144. break;
  145. case Opt_fd:
  146. v9ses->proto = PROTO_FD;
  147. break;
  148. case Opt_name:
  149. match_strcpy(v9ses->name, &args[0]);
  150. break;
  151. case Opt_remotename:
  152. match_strcpy(v9ses->remotename, &args[0]);
  153. break;
  154. case Opt_legacy:
  155. v9ses->extended = 0;
  156. break;
  157. case Opt_nodevmap:
  158. v9ses->nodev = 1;
  159. break;
  160. default:
  161. continue;
  162. }
  163. }
  164. }
  165. /**
  166. * v9fs_inode2v9ses - safely extract v9fs session info from super block
  167. * @inode: inode to extract information from
  168. *
  169. * Paranoid function to extract v9ses information from superblock,
  170. * if anything is missing it will report an error.
  171. *
  172. */
  173. struct v9fs_session_info *v9fs_inode2v9ses(struct inode *inode)
  174. {
  175. return (inode->i_sb->s_fs_info);
  176. }
  177. /**
  178. * v9fs_get_idpool - allocate numeric id from pool
  179. * @p - pool to allocate from
  180. *
  181. * XXX - This seems to be an awful generic function, should it be in idr.c with
  182. * the lock included in struct idr?
  183. */
  184. int v9fs_get_idpool(struct v9fs_idpool *p)
  185. {
  186. int i = 0;
  187. int error;
  188. retry:
  189. if (idr_pre_get(&p->pool, GFP_KERNEL) == 0)
  190. return 0;
  191. if (down_interruptible(&p->lock) == -EINTR) {
  192. eprintk(KERN_WARNING, "Interrupted while locking\n");
  193. return -1;
  194. }
  195. /* no need to store exactly p, we just need something non-null */
  196. error = idr_get_new(&p->pool, p, &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_check_idpool - check if the specified id is available
  222. * @id - id to check
  223. * @p - pool
  224. */
  225. int v9fs_check_idpool(int id, struct v9fs_idpool *p)
  226. {
  227. return idr_find(&p->pool, id) != NULL;
  228. }
  229. /**
  230. * v9fs_session_init - initialize session
  231. * @v9ses: session information structure
  232. * @dev_name: device being mounted
  233. * @data: options
  234. *
  235. */
  236. int
  237. v9fs_session_init(struct v9fs_session_info *v9ses,
  238. const char *dev_name, char *data)
  239. {
  240. struct v9fs_fcall *fcall = NULL;
  241. struct v9fs_transport *trans_proto;
  242. int n = 0;
  243. int newfid = -1;
  244. int retval = -EINVAL;
  245. struct v9fs_str *version;
  246. v9ses->name = __getname();
  247. if (!v9ses->name)
  248. return -ENOMEM;
  249. v9ses->remotename = __getname();
  250. if (!v9ses->remotename) {
  251. __putname(v9ses->name);
  252. return -ENOMEM;
  253. }
  254. strcpy(v9ses->name, V9FS_DEFUSER);
  255. strcpy(v9ses->remotename, V9FS_DEFANAME);
  256. v9fs_parse_options(data, v9ses);
  257. /* set global debug level */
  258. v9fs_debug_level = v9ses->debug;
  259. /* id pools that are session-dependent: FIDs and TIDs */
  260. idr_init(&v9ses->fidpool.pool);
  261. init_MUTEX(&v9ses->fidpool.lock);
  262. switch (v9ses->proto) {
  263. case PROTO_TCP:
  264. trans_proto = &v9fs_trans_tcp;
  265. break;
  266. case PROTO_UNIX:
  267. trans_proto = &v9fs_trans_unix;
  268. *v9ses->remotename = 0;
  269. break;
  270. case PROTO_FD:
  271. trans_proto = &v9fs_trans_fd;
  272. *v9ses->remotename = 0;
  273. break;
  274. default:
  275. printk(KERN_ERR "v9fs: Bad mount protocol %d\n", v9ses->proto);
  276. retval = -ENOPROTOOPT;
  277. goto SessCleanUp;
  278. };
  279. v9ses->transport = kmalloc(sizeof(*v9ses->transport), GFP_KERNEL);
  280. if (!v9ses->transport) {
  281. retval = -ENOMEM;
  282. goto SessCleanUp;
  283. }
  284. memmove(v9ses->transport, trans_proto, sizeof(*v9ses->transport));
  285. if ((retval = v9ses->transport->init(v9ses, dev_name, data)) < 0) {
  286. eprintk(KERN_ERR, "problem initializing transport\n");
  287. goto SessCleanUp;
  288. }
  289. v9ses->inprogress = 0;
  290. v9ses->shutdown = 0;
  291. v9ses->session_hung = 0;
  292. v9ses->mux = v9fs_mux_init(v9ses->transport, v9ses->maxdata + V9FS_IOHDRSZ,
  293. &v9ses->extended);
  294. if (IS_ERR(v9ses->mux)) {
  295. retval = PTR_ERR(v9ses->mux);
  296. v9ses->mux = NULL;
  297. dprintk(DEBUG_ERROR, "problem initializing mux\n");
  298. goto SessCleanUp;
  299. }
  300. if (v9ses->afid == ~0) {
  301. if (v9ses->extended)
  302. retval =
  303. v9fs_t_version(v9ses, v9ses->maxdata, "9P2000.u",
  304. &fcall);
  305. else
  306. retval = v9fs_t_version(v9ses, v9ses->maxdata, "9P2000",
  307. &fcall);
  308. if (retval < 0) {
  309. dprintk(DEBUG_ERROR, "v9fs_t_version failed\n");
  310. goto FreeFcall;
  311. }
  312. version = &fcall->params.rversion.version;
  313. if (version->len==8 && !memcmp(version->str, "9P2000.u", 8)) {
  314. dprintk(DEBUG_9P, "9P2000 UNIX extensions enabled\n");
  315. v9ses->extended = 1;
  316. } else if (version->len==6 && !memcmp(version->str, "9P2000", 6)) {
  317. dprintk(DEBUG_9P, "9P2000 legacy mode enabled\n");
  318. v9ses->extended = 0;
  319. } else {
  320. retval = -EREMOTEIO;
  321. goto FreeFcall;
  322. }
  323. n = fcall->params.rversion.msize;
  324. kfree(fcall);
  325. if (n < v9ses->maxdata)
  326. v9ses->maxdata = n;
  327. }
  328. newfid = v9fs_get_idpool(&v9ses->fidpool);
  329. if (newfid < 0) {
  330. eprintk(KERN_WARNING, "couldn't allocate FID\n");
  331. retval = -ENOMEM;
  332. goto SessCleanUp;
  333. }
  334. /* it is a little bit ugly, but we have to prevent newfid */
  335. /* being the same as afid, so if it is, get a new fid */
  336. if (v9ses->afid != ~0 && newfid == v9ses->afid) {
  337. newfid = v9fs_get_idpool(&v9ses->fidpool);
  338. if (newfid < 0) {
  339. eprintk(KERN_WARNING, "couldn't allocate FID\n");
  340. retval = -ENOMEM;
  341. goto SessCleanUp;
  342. }
  343. }
  344. if ((retval =
  345. v9fs_t_attach(v9ses, v9ses->name, v9ses->remotename, newfid,
  346. v9ses->afid, NULL))
  347. < 0) {
  348. dprintk(DEBUG_ERROR, "cannot attach\n");
  349. goto SessCleanUp;
  350. }
  351. if (v9ses->afid != ~0) {
  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");