lru_cache.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_LRU_CACHE_H
  3. #define BTRFS_LRU_CACHE_H
  4. #include <linux/types.h>
  5. #include <linux/maple_tree.h>
  6. #include <linux/list.h>
  7. /*
  8. * A cache entry. This is meant to be embedded in a structure of a user of
  9. * this module. Similar to how struct list_head and struct rb_node are used.
  10. *
  11. * Note: it should be embedded as the first element in a struct (offset 0), and
  12. * this module assumes it was allocated with kmalloc(), so it calls kfree() when
  13. * it needs to free an entry.
  14. */
  15. struct btrfs_lru_cache_entry {
  16. struct list_head lru_list;
  17. u64 key;
  18. /*
  19. * Optional generation associated to a key. Use 0 if not needed/used.
  20. * Entries with the same key and different generations are stored in a
  21. * linked list, so use this only for cases where there's a small number
  22. * of different generations.
  23. */
  24. u64 gen;
  25. /*
  26. * The maple tree uses unsigned long type for the keys, which is 32 bits
  27. * on 32 bits systems, and 64 bits on 64 bits systems. So if we want to
  28. * use something like inode numbers as keys, which are always a u64, we
  29. * have to deal with this in a special way - we store the key in the
  30. * entry itself, as a u64, and the values inserted into the maple tree
  31. * are linked lists of entries - so in case we are on a 64 bits system,
  32. * that list always has a single entry, while on 32 bits systems it
  33. * may have more than one, with each entry having the same value for
  34. * their lower 32 bits of the u64 key.
  35. */
  36. struct list_head list;
  37. };
  38. struct btrfs_lru_cache {
  39. struct list_head lru_list;
  40. struct maple_tree entries;
  41. /* Number of entries stored in the cache. */
  42. unsigned int size;
  43. /* Maximum number of entries the cache can have. */
  44. unsigned int max_size;
  45. };
  46. #define btrfs_lru_cache_for_each_entry_safe(cache, entry, tmp) \
  47. list_for_each_entry_safe_reverse((entry), (tmp), &(cache)->lru_list, lru_list)
  48. static inline struct btrfs_lru_cache_entry *btrfs_lru_cache_lru_entry(
  49. struct btrfs_lru_cache *cache)
  50. {
  51. return list_first_entry_or_null(&cache->lru_list,
  52. struct btrfs_lru_cache_entry, lru_list);
  53. }
  54. void btrfs_lru_cache_init(struct btrfs_lru_cache *cache, unsigned int max_size);
  55. struct btrfs_lru_cache_entry *btrfs_lru_cache_lookup(struct btrfs_lru_cache *cache,
  56. u64 key, u64 gen);
  57. int btrfs_lru_cache_store(struct btrfs_lru_cache *cache,
  58. struct btrfs_lru_cache_entry *new_entry,
  59. gfp_t gfp);
  60. void btrfs_lru_cache_remove(struct btrfs_lru_cache *cache,
  61. struct btrfs_lru_cache_entry *entry);
  62. void btrfs_lru_cache_clear(struct btrfs_lru_cache *cache);
  63. #endif