xref: /linux/fs/bcachefs/btree_cache.h (revision 7a51608d)
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"
7 #include "bkey_methods.h"
8 
9 extern const char * const bch2_btree_node_flags[];
10 
11 struct btree_iter;
12 
13 void bch2_recalc_btree_reserve(struct bch_fs *);
14 
15 void bch2_btree_node_to_freelist(struct bch_fs *, struct btree *);
16 
17 void bch2_btree_node_hash_remove(struct btree_cache *, struct btree *);
18 int __bch2_btree_node_hash_insert(struct btree_cache *, struct btree *);
19 int bch2_btree_node_hash_insert(struct btree_cache *, struct btree *,
20 				unsigned, enum btree_id);
21 
22 void bch2_node_pin(struct bch_fs *, struct btree *);
23 void bch2_btree_cache_unpin(struct bch_fs *);
24 
25 void bch2_btree_node_update_key_early(struct btree_trans *, enum btree_id, unsigned,
26 				      struct bkey_s_c, struct bkey_i *);
27 
28 void bch2_btree_cache_cannibalize_unlock(struct btree_trans *);
29 int bch2_btree_cache_cannibalize_lock(struct btree_trans *, struct closure *);
30 
31 struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *);
32 struct btree *bch2_btree_node_mem_alloc(struct btree_trans *, bool);
33 
34 struct btree *bch2_btree_node_get(struct btree_trans *, struct btree_path *,
35 				  const struct bkey_i *, unsigned,
36 				  enum six_lock_type, unsigned long);
37 
38 struct btree *bch2_btree_node_get_noiter(struct btree_trans *, const struct bkey_i *,
39 					 enum btree_id, unsigned, bool);
40 
41 int bch2_btree_node_prefetch(struct btree_trans *, struct btree_path *,
42 			     const struct bkey_i *, enum btree_id, unsigned);
43 
44 void bch2_btree_node_evict(struct btree_trans *, const struct bkey_i *);
45 
46 void bch2_fs_btree_cache_exit(struct bch_fs *);
47 int bch2_fs_btree_cache_init(struct bch_fs *);
48 void bch2_fs_btree_cache_init_early(struct btree_cache *);
49 
btree_ptr_hash_val(const struct bkey_i * k)50 static inline u64 btree_ptr_hash_val(const struct bkey_i *k)
51 {
52 	switch (k->k.type) {
53 	case KEY_TYPE_btree_ptr:
54 		return *((u64 *) bkey_i_to_btree_ptr_c(k)->v.start);
55 	case KEY_TYPE_btree_ptr_v2:
56 		/*
57 		 * The cast/deref is only necessary to avoid sparse endianness
58 		 * warnings:
59 		 */
60 		return *((u64 *) &bkey_i_to_btree_ptr_v2_c(k)->v.seq);
61 	default:
62 		return 0;
63 	}
64 }
65 
btree_node_mem_ptr(const struct bkey_i * k)66 static inline struct btree *btree_node_mem_ptr(const struct bkey_i *k)
67 {
68 	return k->k.type == KEY_TYPE_btree_ptr_v2
69 		? (void *)(unsigned long)bkey_i_to_btree_ptr_v2_c(k)->v.mem_ptr
70 		: NULL;
71 }
72 
73 /* is btree node in hash table? */
btree_node_hashed(struct btree * b)74 static inline bool btree_node_hashed(struct btree *b)
75 {
76 	return b->hash_val != 0;
77 }
78 
79 #define for_each_cached_btree(_b, _c, _tbl, _iter, _pos)		\
80 	for ((_tbl) = rht_dereference_rcu((_c)->btree_cache.table.tbl,	\
81 					  &(_c)->btree_cache.table),	\
82 	     _iter = 0;	_iter < (_tbl)->size; _iter++)			\
83 		rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash)
84 
btree_buf_bytes(const struct btree * b)85 static inline size_t btree_buf_bytes(const struct btree *b)
86 {
87 	return 1UL << b->byte_order;
88 }
89 
btree_buf_max_u64s(const struct btree * b)90 static inline size_t btree_buf_max_u64s(const struct btree *b)
91 {
92 	return (btree_buf_bytes(b) - sizeof(struct btree_node)) / sizeof(u64);
93 }
94 
btree_max_u64s(const struct bch_fs * c)95 static inline size_t btree_max_u64s(const struct bch_fs *c)
96 {
97 	return (c->opts.btree_node_size - sizeof(struct btree_node)) / sizeof(u64);
98 }
99 
btree_sectors(const struct bch_fs * c)100 static inline size_t btree_sectors(const struct bch_fs *c)
101 {
102 	return c->opts.btree_node_size >> SECTOR_SHIFT;
103 }
104 
btree_blocks(const struct bch_fs * c)105 static inline unsigned btree_blocks(const struct bch_fs *c)
106 {
107 	return btree_sectors(c) >> c->block_bits;
108 }
109 
110 #define BTREE_SPLIT_THRESHOLD(c)		(btree_max_u64s(c) * 2 / 3)
111 
112 #define BTREE_FOREGROUND_MERGE_THRESHOLD(c)	(btree_max_u64s(c) * 1 / 3)
113 #define BTREE_FOREGROUND_MERGE_HYSTERESIS(c)			\
114 	(BTREE_FOREGROUND_MERGE_THRESHOLD(c) +			\
115 	 (BTREE_FOREGROUND_MERGE_THRESHOLD(c) >> 2))
116 
btree_id_nr_alive(struct bch_fs * c)117 static inline unsigned btree_id_nr_alive(struct bch_fs *c)
118 {
119 	return BTREE_ID_NR + c->btree_roots_extra.nr;
120 }
121 
bch2_btree_id_root(struct bch_fs * c,unsigned id)122 static inline struct btree_root *bch2_btree_id_root(struct bch_fs *c, unsigned id)
123 {
124 	if (likely(id < BTREE_ID_NR)) {
125 		return &c->btree_roots_known[id];
126 	} else {
127 		unsigned idx = id - BTREE_ID_NR;
128 
129 		EBUG_ON(idx >= c->btree_roots_extra.nr);
130 		return &c->btree_roots_extra.data[idx];
131 	}
132 }
133 
btree_node_root(struct bch_fs * c,struct btree * b)134 static inline struct btree *btree_node_root(struct bch_fs *c, struct btree *b)
135 {
136 	return bch2_btree_id_root(c, b->c.btree_id)->b;
137 }
138 
139 const char *bch2_btree_id_str(enum btree_id);
140 void bch2_btree_id_to_text(struct printbuf *, enum btree_id);
141 
142 void bch2_btree_pos_to_text(struct printbuf *, struct bch_fs *, const struct btree *);
143 void bch2_btree_node_to_text(struct printbuf *, struct bch_fs *, const struct btree *);
144 void bch2_btree_cache_to_text(struct printbuf *, const struct btree_cache *);
145 
146 #endif /* _BCACHEFS_BTREE_CACHE_H */
147