cache.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 ([email protected])
  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_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->i_state & 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. }
  32. void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info *ci)
  33. {
  34. fscache_relinquish_cookie(ceph_fscache_cookie(ci), false);
  35. }
  36. void ceph_fscache_use_cookie(struct inode *inode, bool will_modify)
  37. {
  38. struct ceph_inode_info *ci = ceph_inode(inode);
  39. fscache_use_cookie(ceph_fscache_cookie(ci), will_modify);
  40. }
  41. void ceph_fscache_unuse_cookie(struct inode *inode, bool update)
  42. {
  43. struct ceph_inode_info *ci = ceph_inode(inode);
  44. if (update) {
  45. loff_t i_size = i_size_read(inode);
  46. fscache_unuse_cookie(ceph_fscache_cookie(ci),
  47. &ci->i_version, &i_size);
  48. } else {
  49. fscache_unuse_cookie(ceph_fscache_cookie(ci), NULL, NULL);
  50. }
  51. }
  52. void ceph_fscache_update(struct inode *inode)
  53. {
  54. struct ceph_inode_info *ci = ceph_inode(inode);
  55. loff_t i_size = i_size_read(inode);
  56. fscache_update_cookie(ceph_fscache_cookie(ci), &ci->i_version, &i_size);
  57. }
  58. void ceph_fscache_invalidate(struct inode *inode, bool dio_write)
  59. {
  60. struct ceph_inode_info *ci = ceph_inode(inode);
  61. fscache_invalidate(ceph_fscache_cookie(ci),
  62. &ci->i_version, i_size_read(inode),
  63. dio_write ? FSCACHE_INVAL_DIO_WRITE : 0);
  64. }
  65. int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc)
  66. {
  67. const struct ceph_fsid *fsid = &fsc->client->fsid;
  68. const char *fscache_uniq = fsc->mount_options->fscache_uniq;
  69. size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
  70. char *name;
  71. int err = 0;
  72. name = kasprintf(GFP_KERNEL, "ceph,%pU%s%s", fsid, uniq_len ? "," : "",
  73. uniq_len ? fscache_uniq : "");
  74. if (!name)
  75. return -ENOMEM;
  76. fsc->fscache = fscache_acquire_volume(name, NULL, NULL, 0);
  77. if (IS_ERR_OR_NULL(fsc->fscache)) {
  78. errorfc(fc, "Unable to register fscache cookie for %s", name);
  79. err = fsc->fscache ? PTR_ERR(fsc->fscache) : -EOPNOTSUPP;
  80. fsc->fscache = NULL;
  81. }
  82. kfree(name);
  83. return err;
  84. }
  85. void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
  86. {
  87. fscache_relinquish_volume(fsc->fscache, NULL, false);
  88. }