v9fs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * linux/fs/9p/v9fs.c
  3. *
  4. * This file contains functions assisting in mapping VFS to 9P2000
  5. *
  6. * Copyright (C) 2004-2008 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/sched.h>
  30. #include <linux/cred.h>
  31. #include <linux/parser.h>
  32. #include <linux/idr.h>
  33. #include <linux/slab.h>
  34. #include <linux/seq_file.h>
  35. #include <net/9p/9p.h>
  36. #include <net/9p/client.h>
  37. #include <net/9p/transport.h>
  38. #include "v9fs.h"
  39. #include "v9fs_vfs.h"
  40. #include "cache.h"
  41. static DEFINE_SPINLOCK(v9fs_sessionlist_lock);
  42. static LIST_HEAD(v9fs_sessionlist);
  43. struct kmem_cache *v9fs_inode_cache;
  44. /*
  45. * Option Parsing (code inspired by NFS code)
  46. * NOTE: each transport will parse its own options
  47. */
  48. enum {
  49. /* Options that take integer arguments */
  50. Opt_debug, Opt_dfltuid, Opt_dfltgid, Opt_afid,
  51. /* String options */
  52. Opt_uname, Opt_remotename, Opt_cache, Opt_cachetag,
  53. /* Options that take no arguments */
  54. Opt_nodevmap,
  55. /* Cache options */
  56. Opt_cache_loose, Opt_fscache, Opt_mmap,
  57. /* Access options */
  58. Opt_access, Opt_posixacl,
  59. /* Error token */
  60. Opt_err
  61. };
  62. static const match_table_t tokens = {
  63. {Opt_debug, "debug=%x"},
  64. {Opt_dfltuid, "dfltuid=%u"},
  65. {Opt_dfltgid, "dfltgid=%u"},
  66. {Opt_afid, "afid=%u"},
  67. {Opt_uname, "uname=%s"},
  68. {Opt_remotename, "aname=%s"},
  69. {Opt_nodevmap, "nodevmap"},
  70. {Opt_cache, "cache=%s"},
  71. {Opt_cache_loose, "loose"},
  72. {Opt_fscache, "fscache"},
  73. {Opt_mmap, "mmap"},
  74. {Opt_cachetag, "cachetag=%s"},
  75. {Opt_access, "access=%s"},
  76. {Opt_posixacl, "posixacl"},
  77. {Opt_err, NULL}
  78. };
  79. static const char *const v9fs_cache_modes[nr__p9_cache_modes] = {
  80. [CACHE_NONE] = "none",
  81. [CACHE_MMAP] = "mmap",
  82. [CACHE_LOOSE] = "loose",
  83. [CACHE_FSCACHE] = "fscache",
  84. };
  85. /* Interpret mount options for cache mode */
  86. static int get_cache_mode(char *s)
  87. {
  88. int version = -EINVAL;
  89. if (!strcmp(s, "loose")) {
  90. version = CACHE_LOOSE;
  91. p9_debug(P9_DEBUG_9P, "Cache mode: loose\n");
  92. } else if (!strcmp(s, "fscache")) {
  93. version = CACHE_FSCACHE;
  94. p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n");
  95. } else if (!strcmp(s, "mmap")) {
  96. version = CACHE_MMAP;
  97. p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n");
  98. } else if (!strcmp(s, "none")) {
  99. version = CACHE_NONE;
  100. p9_debug(P9_DEBUG_9P, "Cache mode: none\n");
  101. } else
  102. pr_info("Unknown Cache mode %s\n", s);
  103. return version;
  104. }
  105. /*
  106. * Display the mount options in /proc/mounts.
  107. */
  108. int v9fs_show_options(struct seq_file *m, struct dentry *root)
  109. {
  110. struct v9fs_session_info *v9ses = root->d_sb->s_fs_info;
  111. if (v9ses->debug)
  112. seq_printf(m, ",debug=%x", v9ses->debug);
  113. if (!uid_eq(v9ses->dfltuid, V9FS_DEFUID))
  114. seq_printf(m, ",dfltuid=%u",
  115. from_kuid_munged(&init_user_ns, v9ses->dfltuid));
  116. if (!gid_eq(v9ses->dfltgid, V9FS_DEFGID))
  117. seq_printf(m, ",dfltgid=%u",
  118. from_kgid_munged(&init_user_ns, v9ses->dfltgid));
  119. if (v9ses->afid != ~0)
  120. seq_printf(m, ",afid=%u", v9ses->afid);
  121. if (strcmp(v9ses->uname, V9FS_DEFUSER) != 0)
  122. seq_printf(m, ",uname=%s", v9ses->uname);
  123. if (strcmp(v9ses->aname, V9FS_DEFANAME) != 0)
  124. seq_printf(m, ",aname=%s", v9ses->aname);
  125. if (v9ses->nodev)
  126. seq_puts(m, ",nodevmap");
  127. if (v9ses->cache)
  128. seq_printf(m, ",%s", v9fs_cache_modes[v9ses->cache]);
  129. #ifdef CONFIG_9P_FSCACHE
  130. if (v9ses->cachetag && v9ses->cache == CACHE_FSCACHE)
  131. seq_printf(m, ",cachetag=%s", v9ses->cachetag);
  132. #endif
  133. switch (v9ses->flags & V9FS_ACCESS_MASK) {
  134. case V9FS_ACCESS_USER:
  135. seq_puts(m, ",access=user");
  136. break;
  137. case V9FS_ACCESS_ANY:
  138. seq_puts(m, ",access=any");
  139. break;
  140. case V9FS_ACCESS_CLIENT:
  141. seq_puts(m, ",access=client");
  142. break;
  143. case V9FS_ACCESS_SINGLE:
  144. seq_printf(m, ",access=%u",
  145. from_kuid_munged(&init_user_ns, v9ses->uid));
  146. break;
  147. }
  148. if (v9ses->flags & V9FS_POSIX_ACL)
  149. seq_puts(m, ",posixacl");
  150. return p9_show_client_options(m, v9ses->clnt);
  151. }
  152. /**
  153. * v9fs_parse_options - parse mount options into session structure
  154. * @v9ses: existing v9fs session information
  155. *
  156. * Return 0 upon success, -ERRNO upon failure.
  157. */
  158. static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
  159. {
  160. char *options, *tmp_options;
  161. substring_t args[MAX_OPT_ARGS];
  162. char *p;
  163. int option = 0;
  164. char *s, *e;
  165. int ret = 0;
  166. /* setup defaults */
  167. v9ses->afid = ~0;
  168. v9ses->debug = 0;
  169. v9ses->cache = CACHE_NONE;
  170. #ifdef CONFIG_9P_FSCACHE
  171. v9ses->cachetag = NULL;
  172. #endif
  173. if (!opts)
  174. return 0;
  175. tmp_options = kstrdup(opts, GFP_KERNEL);
  176. if (!tmp_options) {
  177. ret = -ENOMEM;
  178. goto fail_option_alloc;
  179. }
  180. options = tmp_options;
  181. while ((p = strsep(&options, ",")) != NULL) {
  182. int token, r;
  183. if (!*p)
  184. continue;
  185. token = match_token(p, tokens, args);
  186. switch (token) {
  187. case Opt_debug:
  188. r = match_int(&args[0], &option);
  189. if (r < 0) {
  190. p9_debug(P9_DEBUG_ERROR,
  191. "integer field, but no integer?\n");
  192. ret = r;
  193. continue;
  194. }
  195. v9ses->debug = option;
  196. #ifdef CONFIG_NET_9P_DEBUG
  197. p9_debug_level = option;
  198. #endif
  199. break;
  200. case Opt_dfltuid:
  201. r = match_int(&args[0], &option);
  202. if (r < 0) {
  203. p9_debug(P9_DEBUG_ERROR,
  204. "integer field, but no integer?\n");
  205. ret = r;
  206. continue;
  207. }
  208. v9ses->dfltuid = make_kuid(current_user_ns(), option);
  209. if (!uid_valid(v9ses->dfltuid)) {
  210. p9_debug(P9_DEBUG_ERROR,
  211. "uid field, but not a uid?\n");
  212. ret = -EINVAL;
  213. continue;
  214. }
  215. break;
  216. case Opt_dfltgid:
  217. r = match_int(&args[0], &option);
  218. if (r < 0) {
  219. p9_debug(P9_DEBUG_ERROR,
  220. "integer field, but no integer?\n");
  221. ret = r;
  222. continue;
  223. }
  224. v9ses->dfltgid = make_kgid(current_user_ns(), option);
  225. if (!gid_valid(v9ses->dfltgid)) {
  226. p9_debug(P9_DEBUG_ERROR,
  227. "gid field, but not a gid?\n");
  228. ret = -EINVAL;
  229. continue;
  230. }
  231. break;
  232. case Opt_afid:
  233. r = match_int(&args[0], &option);
  234. if (r < 0) {
  235. p9_debug(P9_DEBUG_ERROR,
  236. "integer field, but no integer?\n");
  237. ret = r;
  238. continue;
  239. }
  240. v9ses->afid = option;
  241. break;
  242. case Opt_uname:
  243. kfree(v9ses->uname);
  244. v9ses->uname = match_strdup(&args[0]);
  245. if (!v9ses->uname) {
  246. ret = -ENOMEM;
  247. goto free_and_return;
  248. }
  249. break;
  250. case Opt_remotename:
  251. kfree(v9ses->aname);
  252. v9ses->aname = match_strdup(&args[0]);
  253. if (!v9ses->aname) {
  254. ret = -ENOMEM;
  255. goto free_and_return;
  256. }
  257. break;
  258. case Opt_nodevmap:
  259. v9ses->nodev = 1;
  260. break;
  261. case Opt_cache_loose:
  262. v9ses->cache = CACHE_LOOSE;
  263. break;
  264. case Opt_fscache:
  265. v9ses->cache = CACHE_FSCACHE;
  266. break;
  267. case Opt_mmap:
  268. v9ses->cache = CACHE_MMAP;
  269. break;
  270. case Opt_cachetag:
  271. #ifdef CONFIG_9P_FSCACHE
  272. kfree(v9ses->cachetag);
  273. v9ses->cachetag = match_strdup(&args[0]);
  274. #endif
  275. break;
  276. case Opt_cache:
  277. s = match_strdup(&args[0]);
  278. if (!s) {
  279. ret = -ENOMEM;
  280. p9_debug(P9_DEBUG_ERROR,
  281. "problem allocating copy of cache arg\n");
  282. goto free_and_return;
  283. }
  284. ret = get_cache_mode(s);
  285. if (ret == -EINVAL) {
  286. kfree(s);
  287. goto free_and_return;
  288. }
  289. v9ses->cache = ret;
  290. kfree(s);
  291. break;
  292. case Opt_access:
  293. s = match_strdup(&args[0]);
  294. if (!s) {
  295. ret = -ENOMEM;
  296. p9_debug(P9_DEBUG_ERROR,
  297. "problem allocating copy of access arg\n");
  298. goto free_and_return;
  299. }
  300. v9ses->flags &= ~V9FS_ACCESS_MASK;
  301. if (strcmp(s, "user") == 0)
  302. v9ses->flags |= V9FS_ACCESS_USER;
  303. else if (strcmp(s, "any") == 0)
  304. v9ses->flags |= V9FS_ACCESS_ANY;
  305. else if (strcmp(s, "client") == 0) {
  306. v9ses->flags |= V9FS_ACCESS_CLIENT;
  307. } else {
  308. uid_t uid;
  309. v9ses->flags |= V9FS_ACCESS_SINGLE;
  310. uid = simple_strtoul(s, &e, 10);
  311. if (*e != '\0') {
  312. ret = -EINVAL;
  313. pr_info("Unknown access argument %s\n",
  314. s);
  315. kfree(s);
  316. goto free_and_return;
  317. }
  318. v9ses->uid = make_kuid(current_user_ns(), uid);
  319. if (!uid_valid(v9ses->uid)) {
  320. ret = -EINVAL;
  321. pr_info("Uknown uid %s\n", s);
  322. kfree(s);
  323. goto free_and_return;
  324. }
  325. }
  326. kfree(s);
  327. break;
  328. case Opt_posixacl:
  329. #ifdef CONFIG_9P_FS_POSIX_ACL
  330. v9ses->flags |= V9FS_POSIX_ACL;
  331. #else
  332. p9_debug(P9_DEBUG_ERROR,
  333. "Not defined CONFIG_9P_FS_POSIX_ACL. Ignoring posixacl option\n");
  334. #endif
  335. break;
  336. default:
  337. continue;
  338. }
  339. }
  340. free_and_return:
  341. kfree(tmp_options);
  342. fail_option_alloc:
  343. return ret;
  344. }
  345. /**
  346. * v9fs_session_init - initialize session
  347. * @v9ses: session information structure
  348. * @dev_name: device being mounted
  349. * @data: options
  350. *
  351. */
  352. struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
  353. const char *dev_name, char *data)
  354. {
  355. struct p9_fid *fid;
  356. int rc = -ENOMEM;
  357. v9ses->uname = kstrdup(V9FS_DEFUSER, GFP_KERNEL);
  358. if (!v9ses->uname)
  359. goto err_names;
  360. v9ses->aname = kstrdup(V9FS_DEFANAME, GFP_KERNEL);
  361. if (!v9ses->aname)
  362. goto err_names;
  363. init_rwsem(&v9ses->rename_sem);
  364. v9ses->uid = INVALID_UID;
  365. v9ses->dfltuid = V9FS_DEFUID;
  366. v9ses->dfltgid = V9FS_DEFGID;
  367. v9ses->clnt = p9_client_create(dev_name, data);
  368. if (IS_ERR(v9ses->clnt)) {
  369. rc = PTR_ERR(v9ses->clnt);
  370. p9_debug(P9_DEBUG_ERROR, "problem initializing 9p client\n");
  371. goto err_names;
  372. }
  373. v9ses->flags = V9FS_ACCESS_USER;
  374. if (p9_is_proto_dotl(v9ses->clnt)) {
  375. v9ses->flags = V9FS_ACCESS_CLIENT;
  376. v9ses->flags |= V9FS_PROTO_2000L;
  377. } else if (p9_is_proto_dotu(v9ses->clnt)) {
  378. v9ses->flags |= V9FS_PROTO_2000U;
  379. }
  380. rc = v9fs_parse_options(v9ses, data);
  381. if (rc < 0)
  382. goto err_clnt;
  383. v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
  384. if (!v9fs_proto_dotl(v9ses) &&
  385. ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
  386. /*
  387. * We support ACCESS_CLIENT only for dotl.
  388. * Fall back to ACCESS_USER
  389. */
  390. v9ses->flags &= ~V9FS_ACCESS_MASK;
  391. v9ses->flags |= V9FS_ACCESS_USER;
  392. }
  393. /*FIXME !! */
  394. /* for legacy mode, fall back to V9FS_ACCESS_ANY */
  395. if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
  396. ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
  397. v9ses->flags &= ~V9FS_ACCESS_MASK;
  398. v9ses->flags |= V9FS_ACCESS_ANY;
  399. v9ses->uid = INVALID_UID;
  400. }
  401. if (!v9fs_proto_dotl(v9ses) ||
  402. !((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_CLIENT)) {
  403. /*
  404. * We support ACL checks on clinet only if the protocol is
  405. * 9P2000.L and access is V9FS_ACCESS_CLIENT.
  406. */
  407. v9ses->flags &= ~V9FS_ACL_MASK;
  408. }
  409. fid = p9_client_attach(v9ses->clnt, NULL, v9ses->uname, INVALID_UID,
  410. v9ses->aname);
  411. if (IS_ERR(fid)) {
  412. rc = PTR_ERR(fid);
  413. p9_debug(P9_DEBUG_ERROR, "cannot attach\n");
  414. goto err_clnt;
  415. }
  416. if ((v9ses->flags & V9FS_ACCESS_MASK) == V9FS_ACCESS_SINGLE)
  417. fid->uid = v9ses->uid;
  418. else
  419. fid->uid = INVALID_UID;
  420. #ifdef CONFIG_9P_FSCACHE
  421. /* register the session for caching */
  422. v9fs_cache_session_get_cookie(v9ses);
  423. #endif
  424. spin_lock(&v9fs_sessionlist_lock);
  425. list_add(&v9ses->slist, &v9fs_sessionlist);
  426. spin_unlock(&v9fs_sessionlist_lock);
  427. return fid;
  428. err_clnt:
  429. p9_client_destroy(v9ses->clnt);
  430. err_names:
  431. kfree(v9ses->uname);
  432. kfree(v9ses->aname);
  433. return ERR_PTR(rc);
  434. }
  435. /**
  436. * v9fs_session_close - shutdown a session
  437. * @v9ses: session information structure
  438. *
  439. */
  440. void v9fs_session_close(struct v9fs_session_info *v9ses)
  441. {
  442. if (v9ses->clnt) {
  443. p9_client_destroy(v9ses->clnt);
  444. v9ses->clnt = NULL;
  445. }
  446. #ifdef CONFIG_9P_FSCACHE
  447. if (v9ses->fscache) {
  448. v9fs_cache_session_put_cookie(v9ses);
  449. kfree(v9ses->cachetag);
  450. }
  451. #endif
  452. kfree(v9ses->uname);
  453. kfree(v9ses->aname);
  454. spin_lock(&v9fs_sessionlist_lock);
  455. list_del(&v9ses->slist);
  456. spin_unlock(&v9fs_sessionlist_lock);
  457. }
  458. /**
  459. * v9fs_session_cancel - terminate a session
  460. * @v9ses: session to terminate
  461. *
  462. * mark transport as disconnected and cancel all pending requests.
  463. */
  464. void v9fs_session_cancel(struct v9fs_session_info *v9ses) {
  465. p9_debug(P9_DEBUG_ERROR, "cancel session %p\n", v9ses);
  466. p9_client_disconnect(v9ses->clnt);
  467. }
  468. /**
  469. * v9fs_session_begin_cancel - Begin terminate of a session
  470. * @v9ses: session to terminate
  471. *
  472. * After this call we don't allow any request other than clunk.
  473. */
  474. void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses)
  475. {
  476. p9_debug(P9_DEBUG_ERROR, "begin cancel session %p\n", v9ses);
  477. p9_client_begin_disconnect(v9ses->clnt);
  478. }
  479. extern int v9fs_error_init(void);
  480. static struct kobject *v9fs_kobj;
  481. #ifdef CONFIG_9P_FSCACHE
  482. /**
  483. * caches_show - list caches associated with a session
  484. *
  485. * Returns the size of buffer written.
  486. */
  487. static ssize_t caches_show(struct kobject *kobj,
  488. struct kobj_attribute *attr,
  489. char *buf)
  490. {
  491. ssize_t n = 0, count = 0, limit = PAGE_SIZE;
  492. struct v9fs_session_info *v9ses;
  493. spin_lock(&v9fs_sessionlist_lock);
  494. list_for_each_entry(v9ses, &v9fs_sessionlist, slist) {
  495. if (v9ses->cachetag) {
  496. n = snprintf(buf, limit, "%s\n", v9ses->cachetag);
  497. if (n < 0) {
  498. count = n;
  499. break;
  500. }
  501. count += n;
  502. limit -= n;
  503. }
  504. }
  505. spin_unlock(&v9fs_sessionlist_lock);
  506. return count;
  507. }
  508. static struct kobj_attribute v9fs_attr_cache = __ATTR_RO(caches);
  509. #endif /* CONFIG_9P_FSCACHE */
  510. static struct attribute *v9fs_attrs[] = {
  511. #ifdef CONFIG_9P_FSCACHE
  512. &v9fs_attr_cache.attr,
  513. #endif
  514. NULL,
  515. };
  516. static struct attribute_group v9fs_attr_group = {
  517. .attrs = v9fs_attrs,
  518. };
  519. /**
  520. * v9fs_sysfs_init - Initialize the v9fs sysfs interface
  521. *
  522. */
  523. static int __init v9fs_sysfs_init(void)
  524. {
  525. v9fs_kobj = kobject_create_and_add("9p", fs_kobj);
  526. if (!v9fs_kobj)
  527. return -ENOMEM;
  528. if (sysfs_create_group(v9fs_kobj, &v9fs_attr_group)) {
  529. kobject_put(v9fs_kobj);
  530. return -ENOMEM;
  531. }
  532. return 0;
  533. }
  534. /**
  535. * v9fs_sysfs_cleanup - Unregister the v9fs sysfs interface
  536. *
  537. */
  538. static void v9fs_sysfs_cleanup(void)
  539. {
  540. sysfs_remove_group(v9fs_kobj, &v9fs_attr_group);
  541. kobject_put(v9fs_kobj);
  542. }
  543. static void v9fs_inode_init_once(void *foo)
  544. {
  545. struct v9fs_inode *v9inode = (struct v9fs_inode *)foo;
  546. #ifdef CONFIG_9P_FSCACHE
  547. v9inode->fscache = NULL;
  548. #endif
  549. memset(&v9inode->qid, 0, sizeof(v9inode->qid));
  550. inode_init_once(&v9inode->vfs_inode);
  551. }
  552. /**
  553. * v9fs_init_inode_cache - initialize a cache for 9P
  554. * Returns 0 on success.
  555. */
  556. static int v9fs_init_inode_cache(void)
  557. {
  558. v9fs_inode_cache = kmem_cache_create("v9fs_inode_cache",
  559. sizeof(struct v9fs_inode),
  560. 0, (SLAB_RECLAIM_ACCOUNT|
  561. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  562. v9fs_inode_init_once);
  563. if (!v9fs_inode_cache)
  564. return -ENOMEM;
  565. return 0;
  566. }
  567. /**
  568. * v9fs_destroy_inode_cache - destroy the cache of 9P inode
  569. *
  570. */
  571. static void v9fs_destroy_inode_cache(void)
  572. {
  573. /*
  574. * Make sure all delayed rcu free inodes are flushed before we
  575. * destroy cache.
  576. */
  577. rcu_barrier();
  578. kmem_cache_destroy(v9fs_inode_cache);
  579. }
  580. static int v9fs_cache_register(void)
  581. {
  582. int ret;
  583. ret = v9fs_init_inode_cache();
  584. if (ret < 0)
  585. return ret;
  586. #ifdef CONFIG_9P_FSCACHE
  587. ret = fscache_register_netfs(&v9fs_cache_netfs);
  588. if (ret < 0)
  589. v9fs_destroy_inode_cache();
  590. #endif
  591. return ret;
  592. }
  593. static void v9fs_cache_unregister(void)
  594. {
  595. v9fs_destroy_inode_cache();
  596. #ifdef CONFIG_9P_FSCACHE
  597. fscache_unregister_netfs(&v9fs_cache_netfs);
  598. #endif
  599. }
  600. /**
  601. * init_v9fs - Initialize module
  602. *
  603. */
  604. static int __init init_v9fs(void)
  605. {
  606. int err;
  607. pr_info("Installing v9fs 9p2000 file system support\n");
  608. /* TODO: Setup list of registered trasnport modules */
  609. err = v9fs_cache_register();
  610. if (err < 0) {
  611. pr_err("Failed to register v9fs for caching\n");
  612. return err;
  613. }
  614. err = v9fs_sysfs_init();
  615. if (err < 0) {
  616. pr_err("Failed to register with sysfs\n");
  617. goto out_cache;
  618. }
  619. err = register_filesystem(&v9fs_fs_type);
  620. if (err < 0) {
  621. pr_err("Failed to register filesystem\n");
  622. goto out_sysfs_cleanup;
  623. }
  624. return 0;
  625. out_sysfs_cleanup:
  626. v9fs_sysfs_cleanup();
  627. out_cache:
  628. v9fs_cache_unregister();
  629. return err;
  630. }
  631. /**
  632. * exit_v9fs - shutdown module
  633. *
  634. */
  635. static void __exit exit_v9fs(void)
  636. {
  637. v9fs_sysfs_cleanup();
  638. v9fs_cache_unregister();
  639. unregister_filesystem(&v9fs_fs_type);
  640. }
  641. module_init(init_v9fs)
  642. module_exit(exit_v9fs)
  643. MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
  644. MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
  645. MODULE_AUTHOR("Ron Minnich <rminnich@lanl.gov>");
  646. MODULE_LICENSE("GPL");