162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#ifndef BTRFS_LRU_CACHE_H
462306a36Sopenharmony_ci#define BTRFS_LRU_CACHE_H
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/maple_tree.h>
762306a36Sopenharmony_ci#include <linux/list.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/*
1062306a36Sopenharmony_ci * A cache entry. This is meant to be embedded in a structure of a user of
1162306a36Sopenharmony_ci * this module. Similar to how struct list_head and struct rb_node are used.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Note: it should be embedded as the first element in a struct (offset 0), and
1462306a36Sopenharmony_ci * this module assumes it was allocated with kmalloc(), so it calls kfree() when
1562306a36Sopenharmony_ci * it needs to free an entry.
1662306a36Sopenharmony_ci */
1762306a36Sopenharmony_cistruct btrfs_lru_cache_entry {
1862306a36Sopenharmony_ci	struct list_head lru_list;
1962306a36Sopenharmony_ci	u64 key;
2062306a36Sopenharmony_ci	/*
2162306a36Sopenharmony_ci	 * Optional generation associated to a key. Use 0 if not needed/used.
2262306a36Sopenharmony_ci	 * Entries with the same key and different generations are stored in a
2362306a36Sopenharmony_ci	 * linked list, so use this only for cases where there's a small number
2462306a36Sopenharmony_ci	 * of different generations.
2562306a36Sopenharmony_ci	 */
2662306a36Sopenharmony_ci	u64 gen;
2762306a36Sopenharmony_ci	/*
2862306a36Sopenharmony_ci	 * The maple tree uses unsigned long type for the keys, which is 32 bits
2962306a36Sopenharmony_ci	 * on 32 bits systems, and 64 bits on 64 bits systems. So if we want to
3062306a36Sopenharmony_ci	 * use something like inode numbers as keys, which are always a u64, we
3162306a36Sopenharmony_ci	 * have to deal with this in a special way - we store the key in the
3262306a36Sopenharmony_ci	 * entry itself, as a u64, and the values inserted into the maple tree
3362306a36Sopenharmony_ci	 * are linked lists of entries - so in case we are on a 64 bits system,
3462306a36Sopenharmony_ci	 * that list always has a single entry, while on 32 bits systems it
3562306a36Sopenharmony_ci	 * may have more than one, with each entry having the same value for
3662306a36Sopenharmony_ci	 * their lower 32 bits of the u64 key.
3762306a36Sopenharmony_ci	 */
3862306a36Sopenharmony_ci	struct list_head list;
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistruct btrfs_lru_cache {
4262306a36Sopenharmony_ci	struct list_head lru_list;
4362306a36Sopenharmony_ci	struct maple_tree entries;
4462306a36Sopenharmony_ci	/* Number of entries stored in the cache. */
4562306a36Sopenharmony_ci	unsigned int size;
4662306a36Sopenharmony_ci	/* Maximum number of entries the cache can have. */
4762306a36Sopenharmony_ci	unsigned int max_size;
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#define btrfs_lru_cache_for_each_entry_safe(cache, entry, tmp)		\
5162306a36Sopenharmony_ci	list_for_each_entry_safe_reverse((entry), (tmp), &(cache)->lru_list, lru_list)
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic inline unsigned int btrfs_lru_cache_size(const struct btrfs_lru_cache *cache)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	return cache->size;
5662306a36Sopenharmony_ci}
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistatic inline struct btrfs_lru_cache_entry *btrfs_lru_cache_lru_entry(
5962306a36Sopenharmony_ci					      struct btrfs_lru_cache *cache)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	return list_first_entry_or_null(&cache->lru_list,
6262306a36Sopenharmony_ci					struct btrfs_lru_cache_entry, lru_list);
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_civoid btrfs_lru_cache_init(struct btrfs_lru_cache *cache, unsigned int max_size);
6662306a36Sopenharmony_cistruct btrfs_lru_cache_entry *btrfs_lru_cache_lookup(struct btrfs_lru_cache *cache,
6762306a36Sopenharmony_ci						     u64 key, u64 gen);
6862306a36Sopenharmony_ciint btrfs_lru_cache_store(struct btrfs_lru_cache *cache,
6962306a36Sopenharmony_ci			  struct btrfs_lru_cache_entry *new_entry,
7062306a36Sopenharmony_ci			  gfp_t gfp);
7162306a36Sopenharmony_civoid btrfs_lru_cache_remove(struct btrfs_lru_cache *cache,
7262306a36Sopenharmony_ci			    struct btrfs_lru_cache_entry *entry);
7362306a36Sopenharmony_civoid btrfs_lru_cache_clear(struct btrfs_lru_cache *cache);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#endif
76