]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_cache.h
Update bcachefs sources to d4b7ef921a bcachefs: Refactoring
[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 #include "extents.h"
7
8 struct btree_iter;
9
10 extern const char * const bch2_btree_ids[];
11
12 void bch2_recalc_btree_reserve(struct bch_fs *);
13
14 void bch2_btree_node_hash_remove(struct bch_fs *, struct btree *);
15 int __bch2_btree_node_hash_insert(struct bch_fs *, struct btree *);
16 int bch2_btree_node_hash_insert(struct bch_fs *, struct btree *,
17                                 unsigned, enum btree_id);
18
19 void bch2_btree_node_cannibalize_unlock(struct bch_fs *);
20 int bch2_btree_node_cannibalize_lock(struct bch_fs *, struct closure *);
21
22 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *);
23
24 struct btree *bch2_btree_node_get(struct bch_fs *, struct btree_iter *,
25                                   const struct bkey_i *, unsigned,
26                                   enum six_lock_type);
27
28 struct btree *bch2_btree_node_get_sibling(struct bch_fs *, struct btree_iter *,
29                                           struct btree *,
30                                           enum btree_node_sibling);
31
32 void bch2_btree_node_prefetch(struct bch_fs *, const struct bkey_i *,
33                               unsigned, enum btree_id);
34
35 void bch2_fs_btree_exit(struct bch_fs *);
36 int bch2_fs_btree_init(struct bch_fs *);
37
38 #define PTR_HASH(_k)    (bkey_i_to_extent_c(_k)->v._data[0])
39
40 /* is btree node in hash table? */
41 static inline bool btree_node_hashed(struct btree *b)
42 {
43         return bkey_extent_is_data(&b->key.k) && 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 int bch2_print_btree_node(struct bch_fs *, struct btree *,
87                          char *, size_t);
88
89 #endif /* _BCACHEFS_BTREE_CACHE_H */