1 #ifndef _BCACHEFS_BTREE_CACHE_H
2 #define _BCACHEFS_BTREE_CACHE_H
5 #include "btree_types.h"
9 extern const char * const bch2_btree_ids[];
11 void bch2_recalc_btree_reserve(struct bch_fs *);
13 void bch2_btree_node_hash_remove(struct btree_cache *, struct btree *);
14 int __bch2_btree_node_hash_insert(struct btree_cache *, struct btree *);
15 int bch2_btree_node_hash_insert(struct btree_cache *, struct btree *,
16 unsigned, enum btree_id);
18 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *);
19 int bch2_btree_cache_cannibalize_lock(struct bch_fs *, struct closure *);
21 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *);
23 struct btree *bch2_btree_node_get(struct bch_fs *, struct btree_iter *,
24 const struct bkey_i *, unsigned,
27 struct btree *bch2_btree_node_get_sibling(struct bch_fs *, struct btree_iter *,
28 struct btree *, enum btree_node_sibling);
30 void bch2_btree_node_prefetch(struct bch_fs *, struct btree_iter *,
31 const struct bkey_i *, unsigned);
33 void bch2_fs_btree_cache_exit(struct bch_fs *);
34 int bch2_fs_btree_cache_init(struct bch_fs *);
35 void bch2_fs_btree_cache_init_early(struct btree_cache *);
37 #define PTR_HASH(_k) *((u64 *) &bkey_i_to_btree_ptr_c(_k)->v)
39 /* is btree node in hash table? */
40 static inline bool btree_node_hashed(struct btree *b)
42 return b->key.k.type == KEY_TYPE_btree_ptr &&
46 #define for_each_cached_btree(_b, _c, _tbl, _iter, _pos) \
47 for ((_tbl) = rht_dereference_rcu((_c)->btree_cache.table.tbl, \
48 &(_c)->btree_cache.table), \
49 _iter = 0; _iter < (_tbl)->size; _iter++) \
50 rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash)
52 static inline size_t btree_bytes(struct bch_fs *c)
54 return c->opts.btree_node_size << 9;
57 static inline size_t btree_max_u64s(struct bch_fs *c)
59 return (btree_bytes(c) - sizeof(struct btree_node)) / sizeof(u64);
62 static inline size_t btree_page_order(struct bch_fs *c)
64 return get_order(btree_bytes(c));
67 static inline size_t btree_pages(struct bch_fs *c)
69 return 1 << btree_page_order(c);
72 static inline unsigned btree_blocks(struct bch_fs *c)
74 return c->opts.btree_node_size >> c->block_bits;
77 #define BTREE_SPLIT_THRESHOLD(c) (btree_blocks(c) * 3 / 4)
79 #define BTREE_FOREGROUND_MERGE_THRESHOLD(c) (btree_max_u64s(c) * 1 / 3)
80 #define BTREE_FOREGROUND_MERGE_HYSTERESIS(c) \
81 (BTREE_FOREGROUND_MERGE_THRESHOLD(c) + \
82 (BTREE_FOREGROUND_MERGE_THRESHOLD(c) << 2))
84 #define btree_node_root(_c, _b) ((_c)->btree_roots[(_b)->btree_id].b)
86 void bch2_btree_node_to_text(struct printbuf *, struct bch_fs *,
89 #endif /* _BCACHEFS_BTREE_CACHE_H */