cache.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Ceph cache definitions.
  4. *
  5. * Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
  6. * Written by Milosz Tanski (milosz@adfin.com)
  7. */
  8. #include <linux/ceph/ceph_debug.h>
  9. #include <linux/fs_context.h>
  10. #include "super.h"
  11. #include "cache.h"
  12. void ceph_fscache_register_inode_cookie(struct inode *inode)
  13. {
  14. struct ceph_inode_info *ci = ceph_inode(inode);
  15. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  16. /* No caching for filesystem? */
  17. if (!fsc->fscache)
  18. return;
  19. /* Regular files only */
  20. if (!S_ISREG(inode->i_mode))
  21. return;
  22. /* Only new inodes! */
  23. if (!(inode_state_read_once(inode) & I_NEW))
  24. return;
  25. WARN_ON_ONCE(ci->netfs.cache);
  26. ci->netfs.cache =
  27. fscache_acquire_cookie(fsc->fscache, 0,
  28. &ci->i_vino, sizeof(ci->i_vino),
  29. &ci->i_version, sizeof(ci->i_version),
  30. i_size_read(inode));
  31. if (ci->netfs.cache)
  32. mapping_set_release_always(inode->i_mapping);
  33. }
  34. void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info *ci)
  35. {
  36. fscache_relinquish_cookie(ceph_fscache_cookie(ci), false);
  37. }
  38. void ceph_fscache_use_cookie(struct inode *inode, bool will_modify)
  39. {
  40. struct ceph_inode_info *ci = ceph_inode(inode);
  41. fscache_use_cookie(ceph_fscache_cookie(ci), will_modify);
  42. }
  43. void ceph_fscache_unuse_cookie(struct inode *inode, bool update)
  44. {
  45. struct ceph_inode_info *ci = ceph_inode(inode);
  46. if (update) {
  47. loff_t i_size = i_size_read(inode);
  48. fscache_unuse_cookie(ceph_fscache_cookie(ci),
  49. &ci->i_version, &i_size);
  50. } else {
  51. fscache_unuse_cookie(ceph_fscache_cookie(ci), NULL, NULL);
  52. }
  53. }
  54. void ceph_fscache_update(struct inode *inode)
  55. {
  56. struct ceph_inode_info *ci = ceph_inode(inode);
  57. loff_t i_size = i_size_read(inode);
  58. fscache_update_cookie(ceph_fscache_cookie(ci), &ci->i_version, &i_size);
  59. }
  60. void ceph_fscache_invalidate(struct inode *inode, bool dio_write)
  61. {
  62. struct ceph_inode_info *ci = ceph_inode(inode);
  63. fscache_invalidate(ceph_fscache_cookie(ci),
  64. &ci->i_version, i_size_read(inode),
  65. dio_write ? FSCACHE_INVAL_DIO_WRITE : 0);
  66. }
  67. int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc)
  68. {
  69. const struct ceph_fsid *fsid = &fsc->client->fsid;
  70. const char *fscache_uniq = fsc->mount_options->fscache_uniq;
  71. size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
  72. char *name;
  73. int err = 0;
  74. name = kasprintf(GFP_KERNEL, "ceph,%pU%s%s", fsid, uniq_len ? "," : "",
  75. uniq_len ? fscache_uniq : "");
  76. if (!name)
  77. return -ENOMEM;
  78. fsc->fscache = fscache_acquire_volume(name, NULL, NULL, 0);
  79. if (IS_ERR_OR_NULL(fsc->fscache)) {
  80. errorfc(fc, "Unable to register fscache cookie for %s", name);
  81. err = fsc->fscache ? PTR_ERR(fsc->fscache) : -EOPNOTSUPP;
  82. fsc->fscache = NULL;
  83. }
  84. kfree(name);
  85. return err;
  86. }
  87. void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
  88. {
  89. fscache_relinquish_volume(fsc->fscache, NULL, false);
  90. }