v9fs.c 10 KB

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