1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_CACHE_H
3 #define _BCACHEFS_BTREE_CACHE_H
6 #include "btree_types.h"
10 extern const char * const bch2_btree_ids[];
12 void bch2_recalc_btree_reserve(struct bch_fs *);
14 void bch2_btree_node_hash_remove(struct btree_cache *, struct btree *);
15 int __bch2_btree_node_hash_insert(struct btree_cache *, struct btree *);
16 int bch2_btree_node_hash_insert(struct btree_cache *, struct btree *,
17 unsigned, enum btree_id);
19 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *);
20 int bch2_btree_cache_cannibalize_lock(struct bch_fs *, struct closure *);
22 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *);
24 struct btree *bch2_btree_node_get(struct bch_fs *, struct btree_iter *,
25 const struct bkey_i *, unsigned,
28 struct btree *bch2_btree_node_get_sibling(struct bch_fs *, struct btree_iter *,
29 struct btree *, enum btree_node_sibling);
31 void bch2_btree_node_prefetch(struct bch_fs *, struct btree_iter *,
32 const struct bkey_i *, unsigned);
34 void bch2_fs_btree_cache_exit(struct bch_fs *);
35 int bch2_fs_btree_cache_init(struct bch_fs *);
36 void bch2_fs_btree_cache_init_early(struct btree_cache *);
38 static inline u64 btree_ptr_hash_val(const struct bkey_i *k)
41 case KEY_TYPE_btree_ptr:
42 return *((u64 *) bkey_i_to_btree_ptr_c(k)->v.start);
43 case KEY_TYPE_btree_ptr_v2:
44 return bkey_i_to_btree_ptr_v2_c(k)->v.seq;
50 /* is btree node in hash table? */
51 static inline bool btree_node_hashed(struct btree *b)
53 return b->hash_val != 0;
56 #define for_each_cached_btree(_b, _c, _tbl, _iter, _pos) \
57 for ((_tbl) = rht_dereference_rcu((_c)->btree_cache.table.tbl, \
58 &(_c)->btree_cache.table), \
59 _iter = 0; _iter < (_tbl)->size; _iter++) \
60 rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash)
62 static inline size_t btree_bytes(struct bch_fs *c)
64 return c->opts.btree_node_size << 9;
67 static inline size_t btree_max_u64s(struct bch_fs *c)
69 return (btree_bytes(c) - sizeof(struct btree_node)) / sizeof(u64);
72 static inline size_t btree_page_order(struct bch_fs *c)
74 return get_order(btree_bytes(c));
77 static inline size_t btree_pages(struct bch_fs *c)
79 return 1 << btree_page_order(c);
82 static inline unsigned btree_blocks(struct bch_fs *c)
84 return c->opts.btree_node_size >> c->block_bits;
87 #define BTREE_SPLIT_THRESHOLD(c) (btree_max_u64s(c) * 3 / 4)
89 #define BTREE_FOREGROUND_MERGE_THRESHOLD(c) (btree_max_u64s(c) * 1 / 3)
90 #define BTREE_FOREGROUND_MERGE_HYSTERESIS(c) \
91 (BTREE_FOREGROUND_MERGE_THRESHOLD(c) + \
92 (BTREE_FOREGROUND_MERGE_THRESHOLD(c) << 2))
94 #define btree_node_root(_c, _b) ((_c)->btree_roots[(_b)->btree_id].b)
96 void bch2_btree_node_to_text(struct printbuf *, struct bch_fs *,
99 #endif /* _BCACHEFS_BTREE_CACHE_H */