cleancache.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_CLEANCACHE_H
  3. #define _LINUX_CLEANCACHE_H
  4. #include <linux/fs.h>
  5. #include <linux/exportfs.h>
  6. #include <linux/mm.h>
  7. #define CLEANCACHE_NO_POOL -1
  8. #define CLEANCACHE_NO_BACKEND -2
  9. #define CLEANCACHE_NO_BACKEND_SHARED -3
  10. #define CLEANCACHE_KEY_MAX 6
  11. /*
  12. * cleancache requires every file with a page in cleancache to have a
  13. * unique key unless/until the file is removed/truncated. For some
  14. * filesystems, the inode number is unique, but for "modern" filesystems
  15. * an exportable filehandle is required (see exportfs.h)
  16. */
  17. struct cleancache_filekey {
  18. union {
  19. ino_t ino;
  20. __u32 fh[CLEANCACHE_KEY_MAX];
  21. u32 key[CLEANCACHE_KEY_MAX];
  22. } u;
  23. };
  24. struct cleancache_ops {
  25. int (*init_fs)(size_t);
  26. int (*init_shared_fs)(uuid_t *uuid, size_t);
  27. int (*get_page)(int, struct cleancache_filekey,
  28. pgoff_t, struct page *);
  29. void (*put_page)(int, struct cleancache_filekey,
  30. pgoff_t, struct page *);
  31. void (*invalidate_page)(int, struct cleancache_filekey, pgoff_t);
  32. void (*invalidate_inode)(int, struct cleancache_filekey);
  33. void (*invalidate_fs)(int);
  34. };
  35. extern int cleancache_register_ops(const struct cleancache_ops *ops);
  36. extern void __cleancache_init_fs(struct super_block *);
  37. extern void __cleancache_init_shared_fs(struct super_block *);
  38. extern int __cleancache_get_page(struct page *);
  39. extern void __cleancache_put_page(struct page *);
  40. extern void __cleancache_invalidate_page(struct address_space *, struct page *);
  41. extern void __cleancache_invalidate_inode(struct address_space *);
  42. extern void __cleancache_invalidate_fs(struct super_block *);
  43. #ifdef CONFIG_CLEANCACHE
  44. #define cleancache_enabled (1)
  45. static inline bool cleancache_fs_enabled_mapping(struct address_space *mapping)
  46. {
  47. return mapping->host->i_sb->cleancache_poolid >= 0;
  48. }
  49. static inline bool cleancache_fs_enabled(struct page *page)
  50. {
  51. return cleancache_fs_enabled_mapping(page->mapping);
  52. }
  53. #else
  54. #define cleancache_enabled (0)
  55. #define cleancache_fs_enabled(_page) (0)
  56. #define cleancache_fs_enabled_mapping(_page) (0)
  57. #endif
  58. /*
  59. * The shim layer provided by these inline functions allows the compiler
  60. * to reduce all cleancache hooks to nothingness if CONFIG_CLEANCACHE
  61. * is disabled, to a single global variable check if CONFIG_CLEANCACHE
  62. * is enabled but no cleancache "backend" has dynamically enabled it,
  63. * and, for the most frequent cleancache ops, to a single global variable
  64. * check plus a superblock element comparison if CONFIG_CLEANCACHE is enabled
  65. * and a cleancache backend has dynamically enabled cleancache, but the
  66. * filesystem referenced by that cleancache op has not enabled cleancache.
  67. * As a result, CONFIG_CLEANCACHE can be enabled by default with essentially
  68. * no measurable performance impact.
  69. */
  70. static inline void cleancache_init_fs(struct super_block *sb)
  71. {
  72. if (cleancache_enabled)
  73. __cleancache_init_fs(sb);
  74. }
  75. static inline void cleancache_init_shared_fs(struct super_block *sb)
  76. {
  77. if (cleancache_enabled)
  78. __cleancache_init_shared_fs(sb);
  79. }
  80. static inline int cleancache_get_page(struct page *page)
  81. {
  82. if (cleancache_enabled && cleancache_fs_enabled(page))
  83. return __cleancache_get_page(page);
  84. return -1;
  85. }
  86. static inline void cleancache_put_page(struct page *page)
  87. {
  88. if (cleancache_enabled && cleancache_fs_enabled(page))
  89. __cleancache_put_page(page);
  90. }
  91. static inline void cleancache_invalidate_page(struct address_space *mapping,
  92. struct page *page)
  93. {
  94. /* careful... page->mapping is NULL sometimes when this is called */
  95. if (cleancache_enabled && cleancache_fs_enabled_mapping(mapping))
  96. __cleancache_invalidate_page(mapping, page);
  97. }
  98. static inline void cleancache_invalidate_inode(struct address_space *mapping)
  99. {
  100. if (cleancache_enabled && cleancache_fs_enabled_mapping(mapping))
  101. __cleancache_invalidate_inode(mapping);
  102. }
  103. static inline void cleancache_invalidate_fs(struct super_block *sb)
  104. {
  105. if (cleancache_enabled)
  106. __cleancache_invalidate_fs(sb);
  107. }
  108. #endif /* _LINUX_CLEANCACHE_H */