]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_cache.h
Update bcachefs sources to 2e70771b8d
[bcachefs-tools-debian] / libbcachefs / btree_cache.h
1 #ifndef _BCACHE_BTREE_CACHE_H
2 #define _BCACHE_BTREE_CACHE_H
3
4 #include "bcachefs.h"
5 #include "btree_types.h"
6
7 struct btree_iter;
8
9 extern const char * const bch2_btree_ids[];
10
11 void bch2_recalc_btree_reserve(struct bch_fs *);
12
13 void bch2_btree_node_hash_remove(struct bch_fs *, struct btree *);
14 int bch2_btree_node_hash_insert(struct bch_fs *, struct btree *,
15                                 unsigned, enum btree_id);
16
17 void bch2_btree_node_cannibalize_unlock(struct bch_fs *);
18 int bch2_btree_node_cannibalize_lock(struct bch_fs *, struct closure *);
19
20 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *);
21
22 struct btree *bch2_btree_node_get(struct btree_iter *, const struct bkey_i *,
23                                   unsigned, enum six_lock_type);
24
25 void bch2_btree_node_prefetch(struct btree_iter *, const struct bkey_i *,
26                               unsigned);
27
28 void bch2_fs_btree_exit(struct bch_fs *);
29 int bch2_fs_btree_init(struct bch_fs *);
30
31 #define for_each_cached_btree(_b, _c, _tbl, _iter, _pos)                \
32         for ((_tbl) = rht_dereference_rcu((_c)->btree_cache_table.tbl,  \
33                                           &(_c)->btree_cache_table),    \
34              _iter = 0; _iter < (_tbl)->size; _iter++)                  \
35                 rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash)
36
37 static inline size_t btree_bytes(struct bch_fs *c)
38 {
39         return c->sb.btree_node_size << 9;
40 }
41
42 static inline size_t btree_max_u64s(struct bch_fs *c)
43 {
44         return (btree_bytes(c) - sizeof(struct btree_node)) / sizeof(u64);
45 }
46
47 static inline size_t btree_pages(struct bch_fs *c)
48 {
49         return c->sb.btree_node_size >> (PAGE_SHIFT - 9);
50 }
51
52 static inline size_t btree_page_order(struct bch_fs *c)
53 {
54         return ilog2(btree_pages(c));
55 }
56
57 static inline unsigned btree_blocks(struct bch_fs *c)
58 {
59         return c->sb.btree_node_size >> c->block_bits;
60 }
61
62 #define BTREE_SPLIT_THRESHOLD(c)                (btree_blocks(c) * 3 / 4)
63
64 #define BTREE_FOREGROUND_MERGE_THRESHOLD(c)     (btree_max_u64s(c) * 1 / 3)
65 #define BTREE_FOREGROUND_MERGE_HYSTERESIS(c)                    \
66         (BTREE_FOREGROUND_MERGE_THRESHOLD(c) +                  \
67          (BTREE_FOREGROUND_MERGE_THRESHOLD(c) << 2))
68
69 #define btree_node_root(_c, _b) ((_c)->btree_roots[(_b)->btree_id].b)
70
71 int bch2_print_btree_node(struct bch_fs *, struct btree *,
72                          char *, size_t);
73
74 #endif /* _BCACHE_BTREE_CACHE_H */