1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_TYPES_H
3 #define _BCACHEFS_BTREE_TYPES_H
5 #include <linux/list.h>
6 #include <linux/rhashtable.h>
8 #include "bkey_methods.h"
9 #include "journal_types.h"
17 struct btree_nr_keys {
20 * Amount of live metadata (i.e. size of node after a compaction) in
24 u16 bset_u64s[MAX_BSETS];
33 * We construct a binary tree in an array as if the array
34 * started at 1, so that things line up on the same cachelines
35 * better: see comments in bset.c at cacheline_to_bkey() for
39 /* size of the binary tree and prev array */
42 /* function of size - precalculated for to_inorder() */
53 struct journal_entry_pin journal;
54 struct closure_waitlist wait;
58 struct open_buckets ob;
63 /* Hottest entries first */
64 struct rhash_head hash;
66 /* Key/pointer for this btree node */
67 __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
78 struct bkey_format format;
80 struct btree_node *data;
84 * Sets of sorted keys - the real btree node - plus a binary search tree
86 * set[0] is special; set[0]->tree, set[0]->prev and set[0]->data point
87 * to the memory we have allocated for this btree node. Additionally,
88 * set[0]->data points to the entire btree node as it exists on disk.
90 struct bset_tree set[MAX_BSETS];
92 struct btree_nr_keys nr;
95 u16 uncompacted_whiteout_u64s;
100 * XXX: add a delete sequence number, so when bch2_btree_node_relock()
101 * fails because the lock sequence number has changed - i.e. the
102 * contents were modified - we can still relock the node if it's still
103 * the one we want, without redoing the traversal
107 * For asynchronous splits/interior node updates:
108 * When we do a split, we allocate new child nodes and update the parent
109 * node to point to them: we update the parent in memory immediately,
110 * but then we must wait until the children have been written out before
111 * the update to the parent can be written - this is a list of the
112 * btree_updates that are blocking this node from being
115 struct list_head write_blocked;
118 * Also for asynchronous splits/interior node updates:
119 * If a btree node isn't reachable yet, we don't want to kick off
120 * another write - because that write also won't yet be reachable and
121 * marking it as completed before it's reachable would be incorrect:
123 unsigned long will_make_reachable;
125 struct open_buckets ob;
128 struct list_head list;
130 struct btree_write writes[2];
132 #ifdef CONFIG_BCACHEFS_DEBUG
133 bool *expensive_debug_checks;
138 struct rhashtable table;
139 bool table_init_done;
141 * We never free a struct btree, except on shutdown - we just put it on
142 * the btree_cache_freed list and reuse it later. This simplifies the
143 * code, and it doesn't cost us much memory as the memory usage is
144 * dominated by buffers that hold the actual btree node data and those
145 * can be freed - and the number of struct btrees allocated is
146 * effectively bounded.
148 * btree_cache_freeable effectively is a small cache - we use it because
149 * high order page allocations can be rather expensive, and it's quite
150 * common to delete and allocate btree nodes in quick succession. It
151 * should never grow past ~2-3 nodes in practice.
154 struct list_head live;
155 struct list_head freeable;
156 struct list_head freed;
158 /* Number of elements in live + freeable lists */
161 struct shrinker shrink;
164 * If we need to allocate memory for a new btree node and that
165 * allocation fails, we can cannibalize another node in the btree cache
166 * to satisfy the allocation - lock to guarantee only one thread does
169 struct task_struct *alloc_lock;
170 struct closure_waitlist alloc_wait;
173 struct btree_node_iter {
174 struct btree_node_iter_set {
179 enum btree_iter_type {
185 #define BTREE_ITER_TYPE ((1 << 2) - 1)
187 #define BTREE_ITER_INTENT (1 << 2)
188 #define BTREE_ITER_PREFETCH (1 << 3)
190 * Used in bch2_btree_iter_traverse(), to indicate whether we're searching for
191 * @pos or the first key strictly greater than @pos
193 #define BTREE_ITER_IS_EXTENTS (1 << 4)
194 #define BTREE_ITER_ERROR (1 << 5)
196 enum btree_iter_uptodate {
197 BTREE_ITER_UPTODATE = 0,
198 BTREE_ITER_NEED_PEEK = 1,
199 BTREE_ITER_NEED_RELOCK = 2,
200 BTREE_ITER_NEED_TRAVERSE = 3,
204 * @pos - iterator's current position
205 * @level - current btree depth
206 * @locks_want - btree level below which we start taking intent locks
207 * @nodes_locked - bitmask indicating which nodes in @nodes are locked
208 * @nodes_intent_locked - bitmask indicating which locks are intent locks
215 enum btree_iter_uptodate uptodate:4;
216 enum btree_id btree_id:4;
220 nodes_intent_locked:4;
222 struct btree_iter_level {
224 struct btree_node_iter iter;
226 } l[BTREE_MAX_DEPTH];
229 * Current unpacked key - so that bch2_btree_iter_next()/
230 * bch2_btree_iter_next_slot() can correctly advance pos.
235 * Circular linked list of linked iterators: linked iterators share
236 * locks (e.g. two linked iterators may have the same node intent
237 * locked, or read and write locked, at the same time), and insertions
238 * through one iterator won't invalidate the other linked iterators.
241 /* Must come last: */
242 struct btree_iter *next;
245 #define BTREE_ITER_MAX 8
247 struct btree_insert_entry {
248 struct btree_iter *iter;
265 struct btree_iter *iters;
266 u64 iter_ids[BTREE_ITER_MAX];
268 struct btree_insert_entry updates[BTREE_ITER_MAX];
270 struct btree_iter iters_onstack[2];
273 #define BTREE_FLAG(flag) \
274 static inline bool btree_node_ ## flag(struct btree *b) \
275 { return test_bit(BTREE_NODE_ ## flag, &b->flags); } \
277 static inline void set_btree_node_ ## flag(struct btree *b) \
278 { set_bit(BTREE_NODE_ ## flag, &b->flags); } \
280 static inline void clear_btree_node_ ## flag(struct btree *b) \
281 { clear_bit(BTREE_NODE_ ## flag, &b->flags); }
284 BTREE_NODE_read_in_flight,
285 BTREE_NODE_read_error,
287 BTREE_NODE_need_write,
289 BTREE_NODE_write_idx,
291 BTREE_NODE_write_in_flight,
292 BTREE_NODE_just_written,
297 BTREE_FLAG(read_in_flight);
298 BTREE_FLAG(read_error);
300 BTREE_FLAG(need_write);
302 BTREE_FLAG(write_idx);
303 BTREE_FLAG(accessed);
304 BTREE_FLAG(write_in_flight);
305 BTREE_FLAG(just_written);
309 static inline struct btree_write *btree_current_write(struct btree *b)
311 return b->writes + btree_node_write_idx(b);
314 static inline struct btree_write *btree_prev_write(struct btree *b)
316 return b->writes + (btree_node_write_idx(b) ^ 1);
319 static inline struct bset_tree *bset_tree_last(struct btree *b)
322 return b->set + b->nsets - 1;
326 __btree_node_offset_to_ptr(const struct btree *b, u16 offset)
328 return (void *) ((u64 *) b->data + 1 + offset);
332 __btree_node_ptr_to_offset(const struct btree *b, const void *p)
334 u16 ret = (u64 *) p - 1 - (u64 *) b->data;
336 EBUG_ON(__btree_node_offset_to_ptr(b, ret) != p);
340 static inline struct bset *bset(const struct btree *b,
341 const struct bset_tree *t)
343 return __btree_node_offset_to_ptr(b, t->data_offset);
346 static inline void set_btree_bset_end(struct btree *b, struct bset_tree *t)
349 __btree_node_ptr_to_offset(b, vstruct_last(bset(b, t)));
352 static inline void set_btree_bset(struct btree *b, struct bset_tree *t,
353 const struct bset *i)
355 t->data_offset = __btree_node_ptr_to_offset(b, i);
356 set_btree_bset_end(b, t);
359 static inline struct bset *btree_bset_first(struct btree *b)
361 return bset(b, b->set);
364 static inline struct bset *btree_bset_last(struct btree *b)
366 return bset(b, bset_tree_last(b));
370 __btree_node_key_to_offset(const struct btree *b, const struct bkey_packed *k)
372 return __btree_node_ptr_to_offset(b, k);
375 static inline struct bkey_packed *
376 __btree_node_offset_to_key(const struct btree *b, u16 k)
378 return __btree_node_offset_to_ptr(b, k);
381 static inline unsigned btree_bkey_first_offset(const struct bset_tree *t)
383 return t->data_offset + offsetof(struct bset, _data) / sizeof(u64);
386 #define btree_bkey_first(_b, _t) \
388 EBUG_ON(bset(_b, _t)->start != \
389 __btree_node_offset_to_key(_b, btree_bkey_first_offset(_t)));\
391 bset(_b, _t)->start; \
394 #define btree_bkey_last(_b, _t) \
396 EBUG_ON(__btree_node_offset_to_key(_b, (_t)->end_offset) != \
397 vstruct_last(bset(_b, _t))); \
399 __btree_node_offset_to_key(_b, (_t)->end_offset); \
402 static inline unsigned bset_byte_offset(struct btree *b, void *i)
404 return i - (void *) b->data;
407 /* Type of keys @b contains: */
408 static inline enum bkey_type btree_node_type(struct btree *b)
410 return b->level ? BKEY_TYPE_BTREE : b->btree_id;
413 static inline const struct bkey_ops *btree_node_ops(struct btree *b)
415 return &bch2_bkey_ops[btree_node_type(b)];
418 static inline bool btree_node_has_ptrs(struct btree *b)
420 return btree_type_has_ptrs(btree_node_type(b));
423 static inline bool btree_node_is_extents(struct btree *b)
425 return btree_node_type(b) == BKEY_TYPE_EXTENTS;
431 struct btree_update *as;
433 /* On disk root - see async splits: */
434 __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
440 * Optional hook that will be called just prior to a btree node update, when
441 * we're holding the write lock and we know what key is about to be overwritten:
444 enum btree_insert_ret {
446 /* extent spanned multiple leaf nodes: have to traverse to next node: */
447 BTREE_INSERT_NEED_TRAVERSE,
448 /* write lock held for too long */
449 /* leaf node needs to be split */
450 BTREE_INSERT_BTREE_NODE_FULL,
452 BTREE_INSERT_NEED_GC_LOCK,
455 enum btree_gc_coalesce_fail_reason {
456 BTREE_GC_COALESCE_FAIL_RESERVE_GET,
457 BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC,
458 BTREE_GC_COALESCE_FAIL_FORMAT_FITS,
461 enum btree_node_sibling {
466 typedef struct btree_nr_keys (*sort_fix_overlapping_fn)(struct bset *,
468 struct btree_node_iter *);
470 #endif /* _BCACHEFS_BTREE_TYPES_H */