Commit | Line | Data |
---|---|---|
1c6fdbd8 KO |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | #ifndef _BCACHEFS_BTREE_CACHE_H | |
3 | #define _BCACHEFS_BTREE_CACHE_H | |
4 | ||
5 | #include "bcachefs.h" | |
6 | #include "btree_types.h" | |
1c6fdbd8 KO |
7 | |
8 | struct btree_iter; | |
9 | ||
1c6fdbd8 KO |
10 | void bch2_recalc_btree_reserve(struct bch_fs *); |
11 | ||
12 | void bch2_btree_node_hash_remove(struct btree_cache *, struct btree *); | |
13 | int __bch2_btree_node_hash_insert(struct btree_cache *, struct btree *); | |
14 | int bch2_btree_node_hash_insert(struct btree_cache *, struct btree *, | |
15 | unsigned, enum btree_id); | |
16 | ||
17 | void bch2_btree_cache_cannibalize_unlock(struct bch_fs *); | |
18 | int bch2_btree_cache_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 bch_fs *, struct btree_iter *, | |
23 | const struct bkey_i *, unsigned, | |
a301dc38 | 24 | enum six_lock_type, unsigned long); |
1c6fdbd8 | 25 | |
e62d65f2 | 26 | struct btree *bch2_btree_node_get_noiter(struct bch_fs *, const struct bkey_i *, |
a0b73c1c | 27 | enum btree_id, unsigned, bool); |
e62d65f2 | 28 | |
1c6fdbd8 | 29 | void bch2_btree_node_prefetch(struct bch_fs *, struct btree_iter *, |
edfbba58 | 30 | const struct bkey_i *, enum btree_id, unsigned); |
1c6fdbd8 KO |
31 | |
32 | void bch2_fs_btree_cache_exit(struct bch_fs *); | |
33 | int bch2_fs_btree_cache_init(struct bch_fs *); | |
34 | void bch2_fs_btree_cache_init_early(struct btree_cache *); | |
35 | ||
237e8048 KO |
36 | static inline u64 btree_ptr_hash_val(const struct bkey_i *k) |
37 | { | |
38 | switch (k->k.type) { | |
39 | case KEY_TYPE_btree_ptr: | |
40 | return *((u64 *) bkey_i_to_btree_ptr_c(k)->v.start); | |
548b3d20 KO |
41 | case KEY_TYPE_btree_ptr_v2: |
42 | return bkey_i_to_btree_ptr_v2_c(k)->v.seq; | |
237e8048 KO |
43 | default: |
44 | return 0; | |
45 | } | |
46 | } | |
1c6fdbd8 | 47 | |
72141e1f KO |
48 | static inline struct btree *btree_node_mem_ptr(const struct bkey_i *k) |
49 | { | |
50 | return k->k.type == KEY_TYPE_btree_ptr_v2 | |
51 | ? (void *)(unsigned long)bkey_i_to_btree_ptr_v2_c(k)->v.mem_ptr | |
52 | : NULL; | |
53 | } | |
54 | ||
1c6fdbd8 KO |
55 | /* is btree node in hash table? */ |
56 | static inline bool btree_node_hashed(struct btree *b) | |
57 | { | |
237e8048 | 58 | return b->hash_val != 0; |
1c6fdbd8 KO |
59 | } |
60 | ||
61 | #define for_each_cached_btree(_b, _c, _tbl, _iter, _pos) \ | |
62 | for ((_tbl) = rht_dereference_rcu((_c)->btree_cache.table.tbl, \ | |
63 | &(_c)->btree_cache.table), \ | |
64 | _iter = 0; _iter < (_tbl)->size; _iter++) \ | |
65 | rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash) | |
66 | ||
67 | static inline size_t btree_bytes(struct bch_fs *c) | |
68 | { | |
69 | return c->opts.btree_node_size << 9; | |
70 | } | |
71 | ||
72 | static inline size_t btree_max_u64s(struct bch_fs *c) | |
73 | { | |
74 | return (btree_bytes(c) - sizeof(struct btree_node)) / sizeof(u64); | |
75 | } | |
76 | ||
1c6fdbd8 KO |
77 | static inline size_t btree_pages(struct bch_fs *c) |
78 | { | |
4580baec | 79 | return btree_bytes(c) / PAGE_SIZE; |
1c6fdbd8 KO |
80 | } |
81 | ||
82 | static inline unsigned btree_blocks(struct bch_fs *c) | |
83 | { | |
84 | return c->opts.btree_node_size >> c->block_bits; | |
85 | } | |
86 | ||
f270667a | 87 | #define BTREE_SPLIT_THRESHOLD(c) (btree_max_u64s(c) * 2 / 3) |
1c6fdbd8 KO |
88 | |
89 | #define BTREE_FOREGROUND_MERGE_THRESHOLD(c) (btree_max_u64s(c) * 1 / 3) | |
90 | #define BTREE_FOREGROUND_MERGE_HYSTERESIS(c) \ | |
91 | (BTREE_FOREGROUND_MERGE_THRESHOLD(c) + \ | |
2fa81d0b | 92 | (BTREE_FOREGROUND_MERGE_THRESHOLD(c) >> 2)) |
1c6fdbd8 | 93 | |
c43a6ef9 | 94 | #define btree_node_root(_c, _b) ((_c)->btree_roots[(_b)->c.btree_id].b) |
1c6fdbd8 | 95 | |
319f9ac3 KO |
96 | void bch2_btree_node_to_text(struct printbuf *, struct bch_fs *, |
97 | struct btree *); | |
d8ebed7d | 98 | void bch2_btree_cache_to_text(struct printbuf *, struct bch_fs *); |
1c6fdbd8 KO |
99 | |
100 | #endif /* _BCACHEFS_BTREE_CACHE_H */ |