v9fs.c 17 KB

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