]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_cache.h
Update bcachefs sources to f38382c574 bcachefs: Improve key marking interface
[bcachefs-tools-debian] / libbcachefs / btree_cache.h
1 #ifndef _BCACHEFS_BTREE_CACHE_H
2 #define _BCACHEFS_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 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);
17
18 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *);
19 int bch2_btree_cache_cannibalize_lock(struct bch_fs *, struct closure *);
20
21 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *);
22
23 struct btree *bch2_btree_node_get(struct bch_fs *, struct btree_iter *,
24                                   const struct bkey_i *, unsigned,
25                                   enum six_lock_type);
26
27 struct btree *bch2_btree_node_get_sibling(struct bch_fs *, struct btree_iter *,
28                                 struct btree *, enum btree_node_sibling);
29
30 void bch2_btree_node_prefetch(struct bch_fs *, struct btree_iter *,
31                               const struct bkey_i *, unsigned);
32
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 *);
36
37 #define PTR_HASH(_k)    *((u64 *) &bkey_i_to_btree_ptr_c(_k)->v)
38
39 /* is btree node in hash table? */
40 static inline bool btree_node_hashed(struct btree *b)
41 {
42         return b->key.k.type == KEY_TYPE_btree_ptr &&
43                 PTR_HASH(&b->key);
44 }
45
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)
51
52 static inline size_t btree_bytes(struct bch_fs *c)
53 {
54         return c->opts.btree_node_size << 9;
55 }
56
57 static inline size_t btree_max_u64s(struct bch_fs *c)
58 {
59         return (btree_bytes(c) - sizeof(struct btree_node)) / sizeof(u64);
60 }
61
62 static inline size_t btree_page_order(struct bch_fs *c)
63 {
64         return get_order(btree_bytes(c));
65 }
66
67 static inline size_t btree_pages(struct bch_fs *c)
68 {
69         return 1 << btree_page_order(c);
70 }
71
72 static inline unsigned btree_blocks(struct bch_fs *c)
73 {
74         return c->opts.btree_node_size >> c->block_bits;
75 }
76
77 #define BTREE_SPLIT_THRESHOLD(c)                (btree_blocks(c) * 3 / 4)
78
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))
83
84 #define btree_node_root(_c, _b) ((_c)->btree_roots[(_b)->btree_id].b)
85
86 void bch2_btree_node_to_text(struct printbuf *, struct bch_fs *,
87                              struct btree *);
88
89 #endif /* _BCACHEFS_BTREE_CACHE_H */