]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_cache.h
Update bcachefs sources to 62de7539dc bcachefs: Make bkey types globally unique
[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, bool);
26
27 struct btree *bch2_btree_node_get_sibling(struct bch_fs *, struct btree_iter *,
28                                           struct btree *, bool,
29                                           enum btree_node_sibling);
30
31 void bch2_btree_node_prefetch(struct bch_fs *, struct btree_iter *,
32                               const struct bkey_i *, unsigned);
33
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 *);
37
38 #define PTR_HASH(_k)    *((u64 *) &bkey_i_to_btree_ptr_c(_k)->v)
39
40 /* is btree node in hash table? */
41 static inline bool btree_node_hashed(struct btree *b)
42 {
43         return b->key.k.type == KEY_TYPE_btree_ptr &&
44                 PTR_HASH(&b->key);
45 }
46
47 #define for_each_cached_btree(_b, _c, _tbl, _iter, _pos)                \
48         for ((_tbl) = rht_dereference_rcu((_c)->btree_cache.table.tbl,  \
49                                           &(_c)->btree_cache.table),    \
50              _iter = 0; _iter < (_tbl)->size; _iter++)                  \
51                 rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash)
52
53 static inline size_t btree_bytes(struct bch_fs *c)
54 {
55         return c->opts.btree_node_size << 9;
56 }
57
58 static inline size_t btree_max_u64s(struct bch_fs *c)
59 {
60         return (btree_bytes(c) - sizeof(struct btree_node)) / sizeof(u64);
61 }
62
63 static inline size_t btree_page_order(struct bch_fs *c)
64 {
65         return get_order(btree_bytes(c));
66 }
67
68 static inline size_t btree_pages(struct bch_fs *c)
69 {
70         return 1 << btree_page_order(c);
71 }
72
73 static inline unsigned btree_blocks(struct bch_fs *c)
74 {
75         return c->opts.btree_node_size >> c->block_bits;
76 }
77
78 #define BTREE_SPLIT_THRESHOLD(c)                (btree_blocks(c) * 3 / 4)
79
80 #define BTREE_FOREGROUND_MERGE_THRESHOLD(c)     (btree_max_u64s(c) * 1 / 3)
81 #define BTREE_FOREGROUND_MERGE_HYSTERESIS(c)                    \
82         (BTREE_FOREGROUND_MERGE_THRESHOLD(c) +                  \
83          (BTREE_FOREGROUND_MERGE_THRESHOLD(c) << 2))
84
85 #define btree_node_root(_c, _b) ((_c)->btree_roots[(_b)->btree_id].b)
86
87 void bch2_btree_node_to_text(struct printbuf *, struct bch_fs *,
88                              struct btree *);
89
90 #endif /* _BCACHEFS_BTREE_CACHE_H */