super.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /* AFS superblock handling
  2. *
  3. * Copyright (c) 2002, 2007, 2018 Red Hat, Inc. All rights reserved.
  4. *
  5. * This software may be freely redistributed under the terms of the
  6. * GNU General Public License.
  7. *
  8. * You should have received a copy of the GNU General Public License
  9. * along with this program; if not, write to the Free Software
  10. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  11. *
  12. * Authors: David Howells <dhowells@redhat.com>
  13. * David Woodhouse <dwmw2@infradead.org>
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/mount.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/fs.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/fs_parser.h>
  24. #include <linux/statfs.h>
  25. #include <linux/sched.h>
  26. #include <linux/nsproxy.h>
  27. #include <linux/magic.h>
  28. #include <net/net_namespace.h>
  29. #include "internal.h"
  30. static void afs_i_init_once(void *foo);
  31. static void afs_kill_super(struct super_block *sb);
  32. static struct inode *afs_alloc_inode(struct super_block *sb);
  33. static void afs_destroy_inode(struct inode *inode);
  34. static void afs_free_inode(struct inode *inode);
  35. static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
  36. static int afs_show_devname(struct seq_file *m, struct dentry *root);
  37. static int afs_show_options(struct seq_file *m, struct dentry *root);
  38. static int afs_init_fs_context(struct fs_context *fc);
  39. static const struct fs_parameter_spec afs_fs_parameters[];
  40. struct file_system_type afs_fs_type = {
  41. .owner = THIS_MODULE,
  42. .name = "afs",
  43. .init_fs_context = afs_init_fs_context,
  44. .parameters = afs_fs_parameters,
  45. .kill_sb = afs_kill_super,
  46. .fs_flags = FS_RENAME_DOES_D_MOVE,
  47. };
  48. MODULE_ALIAS_FS("afs");
  49. int afs_net_id;
  50. static const struct super_operations afs_super_ops = {
  51. .statfs = afs_statfs,
  52. .alloc_inode = afs_alloc_inode,
  53. .write_inode = netfs_unpin_writeback,
  54. .drop_inode = afs_drop_inode,
  55. .destroy_inode = afs_destroy_inode,
  56. .free_inode = afs_free_inode,
  57. .evict_inode = afs_evict_inode,
  58. .show_devname = afs_show_devname,
  59. .show_options = afs_show_options,
  60. };
  61. static struct kmem_cache *afs_inode_cachep;
  62. static atomic_t afs_count_active_inodes;
  63. enum afs_param {
  64. Opt_autocell,
  65. Opt_dyn,
  66. Opt_flock,
  67. Opt_source,
  68. };
  69. static const struct constant_table afs_param_flock[] = {
  70. {"local", afs_flock_mode_local },
  71. {"openafs", afs_flock_mode_openafs },
  72. {"strict", afs_flock_mode_strict },
  73. {"write", afs_flock_mode_write },
  74. {}
  75. };
  76. static const struct fs_parameter_spec afs_fs_parameters[] = {
  77. fsparam_flag ("autocell", Opt_autocell),
  78. fsparam_flag ("dyn", Opt_dyn),
  79. fsparam_enum ("flock", Opt_flock, afs_param_flock),
  80. fsparam_string("source", Opt_source),
  81. {}
  82. };
  83. /*
  84. * initialise the filesystem
  85. */
  86. int __init afs_fs_init(void)
  87. {
  88. int ret;
  89. _enter("");
  90. /* create ourselves an inode cache */
  91. atomic_set(&afs_count_active_inodes, 0);
  92. ret = -ENOMEM;
  93. afs_inode_cachep = kmem_cache_create("afs_inode_cache",
  94. sizeof(struct afs_vnode),
  95. 0,
  96. SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
  97. afs_i_init_once);
  98. if (!afs_inode_cachep) {
  99. printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
  100. return ret;
  101. }
  102. /* now export our filesystem to lesser mortals */
  103. ret = register_filesystem(&afs_fs_type);
  104. if (ret < 0) {
  105. kmem_cache_destroy(afs_inode_cachep);
  106. _leave(" = %d", ret);
  107. return ret;
  108. }
  109. _leave(" = 0");
  110. return 0;
  111. }
  112. /*
  113. * clean up the filesystem
  114. */
  115. void afs_fs_exit(void)
  116. {
  117. _enter("");
  118. afs_mntpt_kill_timer();
  119. unregister_filesystem(&afs_fs_type);
  120. if (atomic_read(&afs_count_active_inodes) != 0) {
  121. printk("kAFS: %d active inode objects still present\n",
  122. atomic_read(&afs_count_active_inodes));
  123. BUG();
  124. }
  125. /*
  126. * Make sure all delayed rcu free inodes are flushed before we
  127. * destroy cache.
  128. */
  129. rcu_barrier();
  130. kmem_cache_destroy(afs_inode_cachep);
  131. _leave("");
  132. }
  133. /*
  134. * Display the mount device name in /proc/mounts.
  135. */
  136. static int afs_show_devname(struct seq_file *m, struct dentry *root)
  137. {
  138. struct afs_super_info *as = AFS_FS_S(root->d_sb);
  139. struct afs_volume *volume = as->volume;
  140. struct afs_cell *cell = as->cell;
  141. const char *suf = "";
  142. char pref = '%';
  143. if (as->dyn_root) {
  144. seq_puts(m, "none");
  145. return 0;
  146. }
  147. switch (volume->type) {
  148. case AFSVL_RWVOL:
  149. break;
  150. case AFSVL_ROVOL:
  151. pref = '#';
  152. if (volume->type_force)
  153. suf = ".readonly";
  154. break;
  155. case AFSVL_BACKVOL:
  156. pref = '#';
  157. suf = ".backup";
  158. break;
  159. }
  160. seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->name, suf);
  161. return 0;
  162. }
  163. /*
  164. * Display the mount options in /proc/mounts.
  165. */
  166. static int afs_show_options(struct seq_file *m, struct dentry *root)
  167. {
  168. struct afs_super_info *as = AFS_FS_S(root->d_sb);
  169. const char *p = NULL;
  170. if (as->dyn_root)
  171. seq_puts(m, ",dyn");
  172. if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
  173. seq_puts(m, ",autocell");
  174. switch (as->flock_mode) {
  175. case afs_flock_mode_unset: break;
  176. case afs_flock_mode_local: p = "local"; break;
  177. case afs_flock_mode_openafs: p = "openafs"; break;
  178. case afs_flock_mode_strict: p = "strict"; break;
  179. case afs_flock_mode_write: p = "write"; break;
  180. }
  181. if (p)
  182. seq_printf(m, ",flock=%s", p);
  183. return 0;
  184. }
  185. /*
  186. * Parse the source name to get cell name, volume name, volume type and R/W
  187. * selector.
  188. *
  189. * This can be one of the following:
  190. * "%[cell:]volume[.]" R/W volume
  191. * "#[cell:]volume[.]" R/O or R/W volume (R/O parent),
  192. * or R/W (R/W parent) volume
  193. * "%[cell:]volume.readonly" R/O volume
  194. * "#[cell:]volume.readonly" R/O volume
  195. * "%[cell:]volume.backup" Backup volume
  196. * "#[cell:]volume.backup" Backup volume
  197. */
  198. static int afs_parse_source(struct fs_context *fc, struct fs_parameter *param)
  199. {
  200. struct afs_fs_context *ctx = fc->fs_private;
  201. struct afs_cell *cell;
  202. const char *cellname, *suffix, *name = param->string;
  203. int cellnamesz;
  204. _enter(",%s", name);
  205. if (fc->source)
  206. return invalf(fc, "kAFS: Multiple sources not supported");
  207. if (!name) {
  208. printk(KERN_ERR "kAFS: no volume name specified\n");
  209. return -EINVAL;
  210. }
  211. if ((name[0] != '%' && name[0] != '#') || !name[1]) {
  212. /* To use dynroot, we don't want to have to provide a source */
  213. if (strcmp(name, "none") == 0) {
  214. ctx->no_cell = true;
  215. return 0;
  216. }
  217. printk(KERN_ERR "kAFS: unparsable volume name\n");
  218. return -EINVAL;
  219. }
  220. /* determine the type of volume we're looking for */
  221. if (name[0] == '%') {
  222. ctx->type = AFSVL_RWVOL;
  223. ctx->force = true;
  224. }
  225. name++;
  226. /* split the cell name out if there is one */
  227. ctx->volname = strchr(name, ':');
  228. if (ctx->volname) {
  229. cellname = name;
  230. cellnamesz = ctx->volname - name;
  231. ctx->volname++;
  232. } else {
  233. ctx->volname = name;
  234. cellname = NULL;
  235. cellnamesz = 0;
  236. }
  237. /* the volume type is further affected by a possible suffix */
  238. suffix = strrchr(ctx->volname, '.');
  239. if (suffix) {
  240. if (strcmp(suffix, ".readonly") == 0) {
  241. ctx->type = AFSVL_ROVOL;
  242. ctx->force = true;
  243. } else if (strcmp(suffix, ".backup") == 0) {
  244. ctx->type = AFSVL_BACKVOL;
  245. ctx->force = true;
  246. } else if (suffix[1] == 0) {
  247. } else {
  248. suffix = NULL;
  249. }
  250. }
  251. ctx->volnamesz = suffix ?
  252. suffix - ctx->volname : strlen(ctx->volname);
  253. _debug("cell %*.*s [%p]",
  254. cellnamesz, cellnamesz, cellname ?: "", ctx->cell);
  255. /* lookup the cell record */
  256. if (cellname) {
  257. cell = afs_lookup_cell(ctx->net, cellname, cellnamesz,
  258. NULL, false);
  259. if (IS_ERR(cell)) {
  260. pr_err("kAFS: unable to lookup cell '%*.*s'\n",
  261. cellnamesz, cellnamesz, cellname ?: "");
  262. return PTR_ERR(cell);
  263. }
  264. afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_parse);
  265. afs_see_cell(cell, afs_cell_trace_see_source);
  266. ctx->cell = cell;
  267. }
  268. _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
  269. ctx->cell->name, ctx->cell,
  270. ctx->volnamesz, ctx->volnamesz, ctx->volname,
  271. suffix ?: "-", ctx->type, ctx->force ? " FORCE" : "");
  272. fc->source = param->string;
  273. param->string = NULL;
  274. return 0;
  275. }
  276. /*
  277. * Parse a single mount parameter.
  278. */
  279. static int afs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  280. {
  281. struct fs_parse_result result;
  282. struct afs_fs_context *ctx = fc->fs_private;
  283. int opt;
  284. opt = fs_parse(fc, afs_fs_parameters, param, &result);
  285. if (opt < 0)
  286. return opt;
  287. switch (opt) {
  288. case Opt_source:
  289. return afs_parse_source(fc, param);
  290. case Opt_autocell:
  291. ctx->autocell = true;
  292. break;
  293. case Opt_dyn:
  294. ctx->dyn_root = true;
  295. break;
  296. case Opt_flock:
  297. ctx->flock_mode = result.uint_32;
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. _leave(" = 0");
  303. return 0;
  304. }
  305. /*
  306. * Validate the options, get the cell key and look up the volume.
  307. */
  308. static int afs_validate_fc(struct fs_context *fc)
  309. {
  310. struct afs_fs_context *ctx = fc->fs_private;
  311. struct afs_volume *volume;
  312. struct afs_cell *cell;
  313. struct key *key;
  314. int ret;
  315. if (!ctx->dyn_root) {
  316. if (ctx->no_cell) {
  317. pr_warn("kAFS: Can only specify source 'none' with -o dyn\n");
  318. return -EINVAL;
  319. }
  320. if (!ctx->cell) {
  321. pr_warn("kAFS: No cell specified\n");
  322. return -EDESTADDRREQ;
  323. }
  324. reget_key:
  325. /* We try to do the mount securely. */
  326. key = afs_request_key(ctx->cell);
  327. if (IS_ERR(key))
  328. return PTR_ERR(key);
  329. ctx->key = key;
  330. if (ctx->volume) {
  331. afs_put_volume(ctx->volume, afs_volume_trace_put_validate_fc);
  332. ctx->volume = NULL;
  333. }
  334. if (test_bit(AFS_CELL_FL_CHECK_ALIAS, &ctx->cell->flags)) {
  335. ret = afs_cell_detect_alias(ctx->cell, key);
  336. if (ret < 0)
  337. return ret;
  338. if (ret == 1) {
  339. _debug("switch to alias");
  340. key_put(ctx->key);
  341. ctx->key = NULL;
  342. cell = afs_use_cell(ctx->cell->alias_of,
  343. afs_cell_trace_use_fc_alias);
  344. afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_fc);
  345. ctx->cell = cell;
  346. goto reget_key;
  347. }
  348. }
  349. volume = afs_create_volume(ctx);
  350. if (IS_ERR(volume))
  351. return PTR_ERR(volume);
  352. ctx->volume = volume;
  353. if (volume->type != AFSVL_RWVOL) {
  354. ctx->flock_mode = afs_flock_mode_local;
  355. fc->sb_flags |= SB_RDONLY;
  356. }
  357. }
  358. return 0;
  359. }
  360. /*
  361. * check a superblock to see if it's the one we're looking for
  362. */
  363. static int afs_test_super(struct super_block *sb, struct fs_context *fc)
  364. {
  365. struct afs_fs_context *ctx = fc->fs_private;
  366. struct afs_super_info *as = AFS_FS_S(sb);
  367. return (as->net_ns == fc->net_ns &&
  368. as->volume &&
  369. as->volume->vid == ctx->volume->vid &&
  370. as->cell == ctx->cell &&
  371. !as->dyn_root);
  372. }
  373. static int afs_dynroot_test_super(struct super_block *sb, struct fs_context *fc)
  374. {
  375. struct afs_super_info *as = AFS_FS_S(sb);
  376. return (as->net_ns == fc->net_ns &&
  377. as->dyn_root);
  378. }
  379. static int afs_set_super(struct super_block *sb, struct fs_context *fc)
  380. {
  381. return set_anon_super(sb, NULL);
  382. }
  383. /*
  384. * fill in the superblock
  385. */
  386. static int afs_fill_super(struct super_block *sb, struct afs_fs_context *ctx)
  387. {
  388. struct afs_super_info *as = AFS_FS_S(sb);
  389. struct inode *inode = NULL;
  390. int ret;
  391. _enter("");
  392. /* fill in the superblock */
  393. sb->s_blocksize = PAGE_SIZE;
  394. sb->s_blocksize_bits = PAGE_SHIFT;
  395. sb->s_maxbytes = MAX_LFS_FILESIZE;
  396. sb->s_magic = AFS_FS_MAGIC;
  397. sb->s_op = &afs_super_ops;
  398. if (!as->dyn_root)
  399. sb->s_xattr = afs_xattr_handlers;
  400. ret = super_setup_bdi(sb);
  401. if (ret)
  402. return ret;
  403. /* allocate the root inode and dentry */
  404. if (as->dyn_root) {
  405. inode = afs_iget_pseudo_dir(sb, true);
  406. } else {
  407. sprintf(sb->s_id, "%llu", as->volume->vid);
  408. afs_activate_volume(as->volume);
  409. inode = afs_root_iget(sb, ctx->key);
  410. }
  411. if (IS_ERR(inode))
  412. return PTR_ERR(inode);
  413. if (ctx->autocell || as->dyn_root)
  414. set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
  415. ret = -ENOMEM;
  416. sb->s_root = d_make_root(inode);
  417. if (!sb->s_root)
  418. goto error;
  419. if (as->dyn_root) {
  420. sb->s_d_op = &afs_dynroot_dentry_operations;
  421. ret = afs_dynroot_populate(sb);
  422. if (ret < 0)
  423. goto error;
  424. } else {
  425. sb->s_d_op = &afs_fs_dentry_operations;
  426. rcu_assign_pointer(as->volume->sb, sb);
  427. }
  428. _leave(" = 0");
  429. return 0;
  430. error:
  431. _leave(" = %d", ret);
  432. return ret;
  433. }
  434. static struct afs_super_info *afs_alloc_sbi(struct fs_context *fc)
  435. {
  436. struct afs_fs_context *ctx = fc->fs_private;
  437. struct afs_super_info *as;
  438. as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
  439. if (as) {
  440. as->net_ns = get_net(fc->net_ns);
  441. as->flock_mode = ctx->flock_mode;
  442. if (ctx->dyn_root) {
  443. as->dyn_root = true;
  444. } else {
  445. as->cell = afs_use_cell(ctx->cell, afs_cell_trace_use_sbi);
  446. as->volume = afs_get_volume(ctx->volume,
  447. afs_volume_trace_get_alloc_sbi);
  448. }
  449. }
  450. return as;
  451. }
  452. static void afs_destroy_sbi(struct afs_super_info *as)
  453. {
  454. if (as) {
  455. struct afs_net *net = afs_net(as->net_ns);
  456. afs_put_volume(as->volume, afs_volume_trace_put_destroy_sbi);
  457. afs_unuse_cell(net, as->cell, afs_cell_trace_unuse_sbi);
  458. put_net(as->net_ns);
  459. kfree(as);
  460. }
  461. }
  462. static void afs_kill_super(struct super_block *sb)
  463. {
  464. struct afs_super_info *as = AFS_FS_S(sb);
  465. if (as->dyn_root)
  466. afs_dynroot_depopulate(sb);
  467. /* Clear the callback interests (which will do ilookup5) before
  468. * deactivating the superblock.
  469. */
  470. if (as->volume)
  471. rcu_assign_pointer(as->volume->sb, NULL);
  472. kill_anon_super(sb);
  473. if (as->volume)
  474. afs_deactivate_volume(as->volume);
  475. afs_destroy_sbi(as);
  476. }
  477. /*
  478. * Get an AFS superblock and root directory.
  479. */
  480. static int afs_get_tree(struct fs_context *fc)
  481. {
  482. struct afs_fs_context *ctx = fc->fs_private;
  483. struct super_block *sb;
  484. struct afs_super_info *as;
  485. int ret;
  486. ret = afs_validate_fc(fc);
  487. if (ret)
  488. goto error;
  489. _enter("");
  490. /* allocate a superblock info record */
  491. ret = -ENOMEM;
  492. as = afs_alloc_sbi(fc);
  493. if (!as)
  494. goto error;
  495. fc->s_fs_info = as;
  496. /* allocate a deviceless superblock */
  497. sb = sget_fc(fc,
  498. as->dyn_root ? afs_dynroot_test_super : afs_test_super,
  499. afs_set_super);
  500. if (IS_ERR(sb)) {
  501. ret = PTR_ERR(sb);
  502. goto error;
  503. }
  504. if (!sb->s_root) {
  505. /* initial superblock/root creation */
  506. _debug("create");
  507. ret = afs_fill_super(sb, ctx);
  508. if (ret < 0)
  509. goto error_sb;
  510. sb->s_flags |= SB_ACTIVE;
  511. } else {
  512. _debug("reuse");
  513. ASSERTCMP(sb->s_flags, &, SB_ACTIVE);
  514. }
  515. fc->root = dget(sb->s_root);
  516. trace_afs_get_tree(as->cell, as->volume);
  517. _leave(" = 0 [%p]", sb);
  518. return 0;
  519. error_sb:
  520. deactivate_locked_super(sb);
  521. error:
  522. _leave(" = %d", ret);
  523. return ret;
  524. }
  525. static void afs_free_fc(struct fs_context *fc)
  526. {
  527. struct afs_fs_context *ctx = fc->fs_private;
  528. afs_destroy_sbi(fc->s_fs_info);
  529. afs_put_volume(ctx->volume, afs_volume_trace_put_free_fc);
  530. afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_fc);
  531. key_put(ctx->key);
  532. kfree(ctx);
  533. }
  534. static const struct fs_context_operations afs_context_ops = {
  535. .free = afs_free_fc,
  536. .parse_param = afs_parse_param,
  537. .get_tree = afs_get_tree,
  538. };
  539. /*
  540. * Set up the filesystem mount context.
  541. */
  542. static int afs_init_fs_context(struct fs_context *fc)
  543. {
  544. struct afs_fs_context *ctx;
  545. struct afs_cell *cell;
  546. ctx = kzalloc(sizeof(struct afs_fs_context), GFP_KERNEL);
  547. if (!ctx)
  548. return -ENOMEM;
  549. ctx->type = AFSVL_ROVOL;
  550. ctx->net = afs_net(fc->net_ns);
  551. /* Default to the workstation cell. */
  552. cell = afs_find_cell(ctx->net, NULL, 0, afs_cell_trace_use_fc);
  553. if (IS_ERR(cell))
  554. cell = NULL;
  555. ctx->cell = cell;
  556. fc->fs_private = ctx;
  557. fc->ops = &afs_context_ops;
  558. return 0;
  559. }
  560. /*
  561. * Initialise an inode cache slab element prior to any use. Note that
  562. * afs_alloc_inode() *must* reset anything that could incorrectly leak from one
  563. * inode to another.
  564. */
  565. static void afs_i_init_once(void *_vnode)
  566. {
  567. struct afs_vnode *vnode = _vnode;
  568. memset(vnode, 0, sizeof(*vnode));
  569. inode_init_once(&vnode->netfs.inode);
  570. mutex_init(&vnode->io_lock);
  571. init_rwsem(&vnode->validate_lock);
  572. spin_lock_init(&vnode->wb_lock);
  573. spin_lock_init(&vnode->lock);
  574. INIT_LIST_HEAD(&vnode->wb_keys);
  575. INIT_LIST_HEAD(&vnode->pending_locks);
  576. INIT_LIST_HEAD(&vnode->granted_locks);
  577. INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
  578. INIT_LIST_HEAD(&vnode->cb_mmap_link);
  579. seqlock_init(&vnode->cb_lock);
  580. }
  581. /*
  582. * allocate an AFS inode struct from our slab cache
  583. */
  584. static struct inode *afs_alloc_inode(struct super_block *sb)
  585. {
  586. struct afs_vnode *vnode;
  587. vnode = alloc_inode_sb(sb, afs_inode_cachep, GFP_KERNEL);
  588. if (!vnode)
  589. return NULL;
  590. atomic_inc(&afs_count_active_inodes);
  591. /* Reset anything that shouldn't leak from one inode to the next. */
  592. memset(&vnode->fid, 0, sizeof(vnode->fid));
  593. memset(&vnode->status, 0, sizeof(vnode->status));
  594. afs_vnode_set_cache(vnode, NULL);
  595. vnode->volume = NULL;
  596. vnode->lock_key = NULL;
  597. vnode->permit_cache = NULL;
  598. vnode->flags = 1 << AFS_VNODE_UNSET;
  599. vnode->lock_state = AFS_VNODE_LOCK_NONE;
  600. init_rwsem(&vnode->rmdir_lock);
  601. INIT_WORK(&vnode->cb_work, afs_invalidate_mmap_work);
  602. _leave(" = %p", &vnode->netfs.inode);
  603. return &vnode->netfs.inode;
  604. }
  605. static void afs_free_inode(struct inode *inode)
  606. {
  607. kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
  608. }
  609. /*
  610. * destroy an AFS inode struct
  611. */
  612. static void afs_destroy_inode(struct inode *inode)
  613. {
  614. struct afs_vnode *vnode = AFS_FS_I(inode);
  615. _enter("%p{%llx:%llu}", inode, vnode->fid.vid, vnode->fid.vnode);
  616. _debug("DESTROY INODE %p", inode);
  617. atomic_dec(&afs_count_active_inodes);
  618. }
  619. static void afs_get_volume_status_success(struct afs_operation *op)
  620. {
  621. struct afs_volume_status *vs = &op->volstatus.vs;
  622. struct kstatfs *buf = op->volstatus.buf;
  623. if (vs->max_quota == 0)
  624. buf->f_blocks = vs->part_max_blocks;
  625. else
  626. buf->f_blocks = vs->max_quota;
  627. if (buf->f_blocks > vs->blocks_in_use)
  628. buf->f_bavail = buf->f_bfree =
  629. buf->f_blocks - vs->blocks_in_use;
  630. }
  631. static const struct afs_operation_ops afs_get_volume_status_operation = {
  632. .issue_afs_rpc = afs_fs_get_volume_status,
  633. .issue_yfs_rpc = yfs_fs_get_volume_status,
  634. .success = afs_get_volume_status_success,
  635. };
  636. /*
  637. * return information about an AFS volume
  638. */
  639. static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
  640. {
  641. struct afs_super_info *as = AFS_FS_S(dentry->d_sb);
  642. struct afs_operation *op;
  643. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  644. buf->f_type = dentry->d_sb->s_magic;
  645. buf->f_bsize = AFS_BLOCK_SIZE;
  646. buf->f_namelen = AFSNAMEMAX - 1;
  647. if (as->dyn_root) {
  648. buf->f_blocks = 1;
  649. buf->f_bavail = 0;
  650. buf->f_bfree = 0;
  651. return 0;
  652. }
  653. op = afs_alloc_operation(NULL, as->volume);
  654. if (IS_ERR(op))
  655. return PTR_ERR(op);
  656. afs_op_set_vnode(op, 0, vnode);
  657. op->nr_files = 1;
  658. op->volstatus.buf = buf;
  659. op->ops = &afs_get_volume_status_operation;
  660. return afs_do_sync_operation(op);
  661. }