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