xref: /openbmc/linux/fs/btrfs/lru_cache.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
190b90d4aSFilipe Manana /* SPDX-License-Identifier: GPL-2.0 */
290b90d4aSFilipe Manana 
390b90d4aSFilipe Manana #ifndef BTRFS_LRU_CACHE_H
490b90d4aSFilipe Manana #define BTRFS_LRU_CACHE_H
590b90d4aSFilipe Manana 
690b90d4aSFilipe Manana #include <linux/maple_tree.h>
790b90d4aSFilipe Manana #include <linux/list.h>
890b90d4aSFilipe Manana 
990b90d4aSFilipe Manana /*
1090b90d4aSFilipe Manana  * A cache entry. This is meant to be embedded in a structure of a user of
1190b90d4aSFilipe Manana  * this module. Similar to how struct list_head and struct rb_node are used.
1290b90d4aSFilipe Manana  *
1390b90d4aSFilipe Manana  * Note: it should be embedded as the first element in a struct (offset 0), and
1490b90d4aSFilipe Manana  * this module assumes it was allocated with kmalloc(), so it calls kfree() when
1590b90d4aSFilipe Manana  * it needs to free an entry.
1690b90d4aSFilipe Manana  */
1790b90d4aSFilipe Manana struct btrfs_lru_cache_entry {
1890b90d4aSFilipe Manana 	struct list_head lru_list;
1990b90d4aSFilipe Manana 	u64 key;
206273ee62SFilipe Manana 	/*
210da0c560SFilipe Manana 	 * Optional generation associated to a key. Use 0 if not needed/used.
220da0c560SFilipe Manana 	 * Entries with the same key and different generations are stored in a
230da0c560SFilipe Manana 	 * linked list, so use this only for cases where there's a small number
240da0c560SFilipe Manana 	 * of different generations.
250da0c560SFilipe Manana 	 */
260da0c560SFilipe Manana 	u64 gen;
270da0c560SFilipe Manana 	/*
286273ee62SFilipe Manana 	 * The maple tree uses unsigned long type for the keys, which is 32 bits
296273ee62SFilipe Manana 	 * on 32 bits systems, and 64 bits on 64 bits systems. So if we want to
306273ee62SFilipe Manana 	 * use something like inode numbers as keys, which are always a u64, we
316273ee62SFilipe Manana 	 * have to deal with this in a special way - we store the key in the
326273ee62SFilipe Manana 	 * entry itself, as a u64, and the values inserted into the maple tree
336273ee62SFilipe Manana 	 * are linked lists of entries - so in case we are on a 64 bits system,
346273ee62SFilipe Manana 	 * that list always has a single entry, while on 32 bits systems it
356273ee62SFilipe Manana 	 * may have more than one, with each entry having the same value for
366273ee62SFilipe Manana 	 * their lower 32 bits of the u64 key.
376273ee62SFilipe Manana 	 */
386273ee62SFilipe Manana 	struct list_head list;
3990b90d4aSFilipe Manana };
4090b90d4aSFilipe Manana 
4190b90d4aSFilipe Manana struct btrfs_lru_cache {
4290b90d4aSFilipe Manana 	struct list_head lru_list;
4390b90d4aSFilipe Manana 	struct maple_tree entries;
4490b90d4aSFilipe Manana 	/* Number of entries stored in the cache. */
4590b90d4aSFilipe Manana 	unsigned int size;
4690b90d4aSFilipe Manana 	/* Maximum number of entries the cache can have. */
4790b90d4aSFilipe Manana 	unsigned int max_size;
4890b90d4aSFilipe Manana };
4990b90d4aSFilipe Manana 
50*3e49363bSFilipe Manana #define btrfs_lru_cache_for_each_entry_safe(cache, entry, tmp)		\
51*3e49363bSFilipe Manana 	list_for_each_entry_safe_reverse((entry), (tmp), &(cache)->lru_list, lru_list)
52*3e49363bSFilipe Manana 
btrfs_lru_cache_size(const struct btrfs_lru_cache * cache)5390b90d4aSFilipe Manana static inline unsigned int btrfs_lru_cache_size(const struct btrfs_lru_cache *cache)
5490b90d4aSFilipe Manana {
5590b90d4aSFilipe Manana 	return cache->size;
5690b90d4aSFilipe Manana }
5790b90d4aSFilipe Manana 
btrfs_lru_cache_lru_entry(struct btrfs_lru_cache * cache)58*3e49363bSFilipe Manana static inline struct btrfs_lru_cache_entry *btrfs_lru_cache_lru_entry(
59*3e49363bSFilipe Manana 					      struct btrfs_lru_cache *cache)
60*3e49363bSFilipe Manana {
61*3e49363bSFilipe Manana 	return list_first_entry_or_null(&cache->lru_list,
62*3e49363bSFilipe Manana 					struct btrfs_lru_cache_entry, lru_list);
63*3e49363bSFilipe Manana }
64*3e49363bSFilipe Manana 
6590b90d4aSFilipe Manana void btrfs_lru_cache_init(struct btrfs_lru_cache *cache, unsigned int max_size);
6690b90d4aSFilipe Manana struct btrfs_lru_cache_entry *btrfs_lru_cache_lookup(struct btrfs_lru_cache *cache,
670da0c560SFilipe Manana 						     u64 key, u64 gen);
6890b90d4aSFilipe Manana int btrfs_lru_cache_store(struct btrfs_lru_cache *cache,
6990b90d4aSFilipe Manana 			  struct btrfs_lru_cache_entry *new_entry,
7090b90d4aSFilipe Manana 			  gfp_t gfp);
71d588adaeSFilipe Manana void btrfs_lru_cache_remove(struct btrfs_lru_cache *cache,
72d588adaeSFilipe Manana 			    struct btrfs_lru_cache_entry *entry);
7390b90d4aSFilipe Manana void btrfs_lru_cache_clear(struct btrfs_lru_cache *cache);
7490b90d4aSFilipe Manana 
7590b90d4aSFilipe Manana #endif
76