1 // SPDX-License-Identifier: GPL-2.0
8 * Initialize a cache object.
11 * @max_size: Maximum size (number of entries) for the cache.
12 * Use 0 for unlimited size, it's the user's responsability to
13 * trim the cache in that case.
15 void btrfs_lru_cache_init(struct btrfs_lru_cache *cache, unsigned int max_size)
17 INIT_LIST_HEAD(&cache->lru_list);
18 mt_init(&cache->entries);
20 cache->max_size = max_size;
23 static struct btrfs_lru_cache_entry *match_entry(struct list_head *head, u64 key,
26 struct btrfs_lru_cache_entry *entry;
28 list_for_each_entry(entry, head, list) {
29 if (entry->key == key && entry->gen == gen)
37 * Lookup for an entry in the cache.
40 * @key: The key of the entry we are looking for.
41 * @gen: Generation associated to the key.
43 * Returns the entry associated with the key or NULL if none found.
45 struct btrfs_lru_cache_entry *btrfs_lru_cache_lookup(struct btrfs_lru_cache *cache,
48 struct list_head *head;
49 struct btrfs_lru_cache_entry *entry;
51 head = mtree_load(&cache->entries, key);
55 entry = match_entry(head, key, gen);
57 list_move_tail(&entry->lru_list, &cache->lru_list);
63 * Remove an entry from the cache.
65 * @cache: The cache to remove from.
66 * @entry: The entry to remove from the cache.
68 * Note: this also frees the memory used by the entry.
70 void btrfs_lru_cache_remove(struct btrfs_lru_cache *cache,
71 struct btrfs_lru_cache_entry *entry)
73 struct list_head *prev = entry->list.prev;
75 ASSERT(cache->size > 0);
76 ASSERT(!mtree_empty(&cache->entries));
78 list_del(&entry->list);
79 list_del(&entry->lru_list);
81 if (list_empty(prev)) {
82 struct list_head *head;
85 * If previous element in the list entry->list is now empty, it
86 * means it's a head entry not pointing to any cached entries,
87 * so remove it from the maple tree and free it.
89 head = mtree_erase(&cache->entries, entry->key);
99 * Store an entry in the cache.
102 * @entry: The entry to store.
104 * Returns 0 on success and < 0 on error.
106 int btrfs_lru_cache_store(struct btrfs_lru_cache *cache,
107 struct btrfs_lru_cache_entry *new_entry,
110 const u64 key = new_entry->key;
111 struct list_head *head;
114 head = kmalloc(sizeof(*head), gfp);
118 ret = mtree_insert(&cache->entries, key, head, gfp);
120 INIT_LIST_HEAD(head);
121 list_add_tail(&new_entry->list, head);
122 } else if (ret == -EEXIST) {
124 head = mtree_load(&cache->entries, key);
125 ASSERT(head != NULL);
126 if (match_entry(head, key, new_entry->gen) != NULL)
128 list_add_tail(&new_entry->list, head);
129 } else if (ret < 0) {
134 if (cache->max_size > 0 && cache->size == cache->max_size) {
135 struct btrfs_lru_cache_entry *lru_entry;
137 lru_entry = list_first_entry(&cache->lru_list,
138 struct btrfs_lru_cache_entry,
140 btrfs_lru_cache_remove(cache, lru_entry);
143 list_add_tail(&new_entry->lru_list, &cache->lru_list);
152 * @cache: The cache to empty.
154 * Removes all entries from the cache.
156 void btrfs_lru_cache_clear(struct btrfs_lru_cache *cache)
158 struct btrfs_lru_cache_entry *entry;
159 struct btrfs_lru_cache_entry *tmp;
161 list_for_each_entry_safe(entry, tmp, &cache->lru_list, lru_list)
162 btrfs_lru_cache_remove(cache, entry);
164 ASSERT(cache->size == 0);
165 ASSERT(mtree_empty(&cache->entries));