]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_types.h
Update bcachefs sources to cfb41d25c7 bcachefs: Add an assertion to track down a...
[bcachefs-tools-debian] / libbcachefs / btree_types.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_TYPES_H
3 #define _BCACHEFS_BTREE_TYPES_H
4
5 #include <linux/list.h>
6 #include <linux/rhashtable.h>
7 #include <linux/six.h>
8
9 #include "bkey_methods.h"
10 #include "buckets_types.h"
11 #include "journal_types.h"
12
13 struct open_bucket;
14 struct btree_update;
15 struct btree_trans;
16
17 #define MAX_BSETS               3U
18
19 struct btree_nr_keys {
20
21         /*
22          * Amount of live metadata (i.e. size of node after a compaction) in
23          * units of u64s
24          */
25         u16                     live_u64s;
26         u16                     bset_u64s[MAX_BSETS];
27
28         /* live keys only: */
29         u16                     packed_keys;
30         u16                     unpacked_keys;
31 };
32
33 struct bset_tree {
34         /*
35          * We construct a binary tree in an array as if the array
36          * started at 1, so that things line up on the same cachelines
37          * better: see comments in bset.c at cacheline_to_bkey() for
38          * details
39          */
40
41         /* size of the binary tree and prev array */
42         u16                     size;
43
44         /* function of size - precalculated for to_inorder() */
45         u16                     extra;
46
47         u16                     data_offset;
48         u16                     aux_data_offset;
49         u16                     end_offset;
50
51         struct bpos             max_key;
52 };
53
54 struct btree_write {
55         struct journal_entry_pin        journal;
56         struct closure_waitlist         wait;
57 };
58
59 struct btree_alloc {
60         struct open_buckets     ob;
61         BKEY_PADDED(k);
62 };
63
64 struct btree {
65         /* Hottest entries first */
66         struct rhash_head       hash;
67
68         /* Key/pointer for this btree node */
69         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
70
71         struct six_lock         lock;
72
73         unsigned long           flags;
74         u16                     written;
75         u8                      level;
76         u8                      btree_id;
77         u8                      nsets;
78         u8                      nr_key_bits;
79
80         struct bkey_format      format;
81
82         struct btree_node       *data;
83         void                    *aux_data;
84
85         /*
86          * Sets of sorted keys - the real btree node - plus a binary search tree
87          *
88          * set[0] is special; set[0]->tree, set[0]->prev and set[0]->data point
89          * to the memory we have allocated for this btree node. Additionally,
90          * set[0]->data points to the entire btree node as it exists on disk.
91          */
92         struct bset_tree        set[MAX_BSETS];
93
94         struct btree_nr_keys    nr;
95         u16                     sib_u64s[2];
96         u16                     whiteout_u64s;
97         u8                      page_order;
98         u8                      unpack_fn_len;
99
100         /*
101          * XXX: add a delete sequence number, so when bch2_btree_node_relock()
102          * fails because the lock sequence number has changed - i.e. the
103          * contents were modified - we can still relock the node if it's still
104          * the one we want, without redoing the traversal
105          */
106
107         /*
108          * For asynchronous splits/interior node updates:
109          * When we do a split, we allocate new child nodes and update the parent
110          * node to point to them: we update the parent in memory immediately,
111          * but then we must wait until the children have been written out before
112          * the update to the parent can be written - this is a list of the
113          * btree_updates that are blocking this node from being
114          * written:
115          */
116         struct list_head        write_blocked;
117
118         /*
119          * Also for asynchronous splits/interior node updates:
120          * If a btree node isn't reachable yet, we don't want to kick off
121          * another write - because that write also won't yet be reachable and
122          * marking it as completed before it's reachable would be incorrect:
123          */
124         unsigned long           will_make_reachable;
125
126         struct open_buckets     ob;
127
128         /* lru list */
129         struct list_head        list;
130
131         struct btree_write      writes[2];
132
133 #ifdef CONFIG_BCACHEFS_DEBUG
134         bool                    *expensive_debug_checks;
135 #endif
136 };
137
138 struct btree_cache {
139         struct rhashtable       table;
140         bool                    table_init_done;
141         /*
142          * We never free a struct btree, except on shutdown - we just put it on
143          * the btree_cache_freed list and reuse it later. This simplifies the
144          * code, and it doesn't cost us much memory as the memory usage is
145          * dominated by buffers that hold the actual btree node data and those
146          * can be freed - and the number of struct btrees allocated is
147          * effectively bounded.
148          *
149          * btree_cache_freeable effectively is a small cache - we use it because
150          * high order page allocations can be rather expensive, and it's quite
151          * common to delete and allocate btree nodes in quick succession. It
152          * should never grow past ~2-3 nodes in practice.
153          */
154         struct mutex            lock;
155         struct list_head        live;
156         struct list_head        freeable;
157         struct list_head        freed;
158
159         /* Number of elements in live + freeable lists */
160         unsigned                used;
161         unsigned                reserve;
162         struct shrinker         shrink;
163
164         /*
165          * If we need to allocate memory for a new btree node and that
166          * allocation fails, we can cannibalize another node in the btree cache
167          * to satisfy the allocation - lock to guarantee only one thread does
168          * this at a time:
169          */
170         struct task_struct      *alloc_lock;
171         struct closure_waitlist alloc_wait;
172 };
173
174 struct btree_node_iter {
175         struct btree_node_iter_set {
176                 u16     k, end;
177         } data[MAX_BSETS];
178 };
179
180 enum btree_iter_type {
181         BTREE_ITER_KEYS,
182         BTREE_ITER_NODES,
183 };
184
185 #define BTREE_ITER_TYPE                 ((1 << 2) - 1)
186
187 #define BTREE_ITER_SLOTS                (1 << 2)
188 #define BTREE_ITER_INTENT               (1 << 3)
189 #define BTREE_ITER_PREFETCH             (1 << 4)
190 #define BTREE_ITER_KEEP_UNTIL_COMMIT    (1 << 5)
191 /*
192  * Used in bch2_btree_iter_traverse(), to indicate whether we're searching for
193  * @pos or the first key strictly greater than @pos
194  */
195 #define BTREE_ITER_IS_EXTENTS           (1 << 6)
196 #define BTREE_ITER_ERROR                (1 << 7)
197
198 enum btree_iter_uptodate {
199         BTREE_ITER_UPTODATE             = 0,
200         BTREE_ITER_NEED_PEEK            = 1,
201         BTREE_ITER_NEED_RELOCK          = 2,
202         BTREE_ITER_NEED_TRAVERSE        = 3,
203 };
204
205 /*
206  * @pos                 - iterator's current position
207  * @level               - current btree depth
208  * @locks_want          - btree level below which we start taking intent locks
209  * @nodes_locked        - bitmask indicating which nodes in @nodes are locked
210  * @nodes_intent_locked - bitmask indicating which locks are intent locks
211  */
212 struct btree_iter {
213         u8                      idx;
214
215         struct btree_trans      *trans;
216         struct bpos             pos;
217
218         u8                      flags;
219         enum btree_iter_uptodate uptodate:4;
220         enum btree_id           btree_id:4;
221         unsigned                level:4,
222                                 locks_want:4,
223                                 nodes_locked:4,
224                                 nodes_intent_locked:4;
225
226         struct btree_iter_level {
227                 struct btree    *b;
228                 struct btree_node_iter iter;
229                 u32             lock_seq;
230         }                       l[BTREE_MAX_DEPTH];
231
232         /*
233          * Current unpacked key - so that bch2_btree_iter_next()/
234          * bch2_btree_iter_next_slot() can correctly advance pos.
235          */
236         struct bkey             k;
237 };
238
239 static inline enum btree_iter_type btree_iter_type(struct btree_iter *iter)
240 {
241         return iter->flags & BTREE_ITER_TYPE;
242 }
243
244 struct btree_insert_entry {
245         struct bkey_i           *k;
246         struct btree_iter       *iter;
247 };
248
249 #define BTREE_ITER_MAX          64
250
251 struct btree_trans {
252         struct bch_fs           *c;
253         unsigned long           ip;
254
255         u64                     iters_linked;
256         u64                     iters_live;
257         u64                     iters_touched;
258
259         u8                      nr_iters;
260         u8                      nr_updates;
261         u8                      size;
262         unsigned                used_mempool:1;
263         unsigned                error:1;
264         unsigned                nounlock:1;
265
266         unsigned                mem_top;
267         unsigned                mem_bytes;
268         void                    *mem;
269
270         struct btree_iter       *iters;
271         struct btree_insert_entry *updates;
272         u8                      *updates_sorted;
273
274         /* update path: */
275         struct journal_res      journal_res;
276         struct journal_preres   journal_preres;
277         u64                     *journal_seq;
278         struct disk_reservation *disk_res;
279         unsigned                flags;
280         unsigned                journal_u64s;
281         unsigned                journal_preres_u64s;
282         struct replicas_delta_list *fs_usage_deltas;
283
284         struct btree_iter       iters_onstack[2];
285         struct btree_insert_entry updates_onstack[6];
286         u8                      updates_sorted_onstack[6];
287 };
288
289 #define BTREE_FLAG(flag)                                                \
290 static inline bool btree_node_ ## flag(struct btree *b)                 \
291 {       return test_bit(BTREE_NODE_ ## flag, &b->flags); }              \
292                                                                         \
293 static inline void set_btree_node_ ## flag(struct btree *b)             \
294 {       set_bit(BTREE_NODE_ ## flag, &b->flags); }                      \
295                                                                         \
296 static inline void clear_btree_node_ ## flag(struct btree *b)           \
297 {       clear_bit(BTREE_NODE_ ## flag, &b->flags); }
298
299 enum btree_flags {
300         BTREE_NODE_read_in_flight,
301         BTREE_NODE_read_error,
302         BTREE_NODE_dirty,
303         BTREE_NODE_need_write,
304         BTREE_NODE_noevict,
305         BTREE_NODE_write_idx,
306         BTREE_NODE_accessed,
307         BTREE_NODE_write_in_flight,
308         BTREE_NODE_just_written,
309         BTREE_NODE_dying,
310         BTREE_NODE_fake,
311 };
312
313 BTREE_FLAG(read_in_flight);
314 BTREE_FLAG(read_error);
315 BTREE_FLAG(dirty);
316 BTREE_FLAG(need_write);
317 BTREE_FLAG(noevict);
318 BTREE_FLAG(write_idx);
319 BTREE_FLAG(accessed);
320 BTREE_FLAG(write_in_flight);
321 BTREE_FLAG(just_written);
322 BTREE_FLAG(dying);
323 BTREE_FLAG(fake);
324
325 static inline struct btree_write *btree_current_write(struct btree *b)
326 {
327         return b->writes + btree_node_write_idx(b);
328 }
329
330 static inline struct btree_write *btree_prev_write(struct btree *b)
331 {
332         return b->writes + (btree_node_write_idx(b) ^ 1);
333 }
334
335 static inline struct bset_tree *bset_tree_last(struct btree *b)
336 {
337         EBUG_ON(!b->nsets);
338         return b->set + b->nsets - 1;
339 }
340
341 static inline void *
342 __btree_node_offset_to_ptr(const struct btree *b, u16 offset)
343 {
344         return (void *) ((u64 *) b->data + 1 + offset);
345 }
346
347 static inline u16
348 __btree_node_ptr_to_offset(const struct btree *b, const void *p)
349 {
350         u16 ret = (u64 *) p - 1 - (u64 *) b->data;
351
352         EBUG_ON(__btree_node_offset_to_ptr(b, ret) != p);
353         return ret;
354 }
355
356 static inline struct bset *bset(const struct btree *b,
357                                 const struct bset_tree *t)
358 {
359         return __btree_node_offset_to_ptr(b, t->data_offset);
360 }
361
362 static inline void set_btree_bset_end(struct btree *b, struct bset_tree *t)
363 {
364         t->end_offset =
365                 __btree_node_ptr_to_offset(b, vstruct_last(bset(b, t)));
366 }
367
368 static inline void set_btree_bset(struct btree *b, struct bset_tree *t,
369                                   const struct bset *i)
370 {
371         t->data_offset = __btree_node_ptr_to_offset(b, i);
372         set_btree_bset_end(b, t);
373 }
374
375 static inline struct bset *btree_bset_first(struct btree *b)
376 {
377         return bset(b, b->set);
378 }
379
380 static inline struct bset *btree_bset_last(struct btree *b)
381 {
382         return bset(b, bset_tree_last(b));
383 }
384
385 static inline u16
386 __btree_node_key_to_offset(const struct btree *b, const struct bkey_packed *k)
387 {
388         return __btree_node_ptr_to_offset(b, k);
389 }
390
391 static inline struct bkey_packed *
392 __btree_node_offset_to_key(const struct btree *b, u16 k)
393 {
394         return __btree_node_offset_to_ptr(b, k);
395 }
396
397 static inline unsigned btree_bkey_first_offset(const struct bset_tree *t)
398 {
399         return t->data_offset + offsetof(struct bset, _data) / sizeof(u64);
400 }
401
402 #define btree_bkey_first(_b, _t)                                        \
403 ({                                                                      \
404         EBUG_ON(bset(_b, _t)->start !=                                  \
405                 __btree_node_offset_to_key(_b, btree_bkey_first_offset(_t)));\
406                                                                         \
407         bset(_b, _t)->start;                                            \
408 })
409
410 #define btree_bkey_last(_b, _t)                                         \
411 ({                                                                      \
412         EBUG_ON(__btree_node_offset_to_key(_b, (_t)->end_offset) !=     \
413                 vstruct_last(bset(_b, _t)));                            \
414                                                                         \
415         __btree_node_offset_to_key(_b, (_t)->end_offset);               \
416 })
417
418 static inline unsigned bset_u64s(struct bset_tree *t)
419 {
420         return t->end_offset - t->data_offset -
421                 sizeof(struct bset) / sizeof(u64);
422 }
423
424 static inline unsigned bset_dead_u64s(struct btree *b, struct bset_tree *t)
425 {
426         return bset_u64s(t) - b->nr.bset_u64s[t - b->set];
427 }
428
429 static inline unsigned bset_byte_offset(struct btree *b, void *i)
430 {
431         return i - (void *) b->data;
432 }
433
434 enum btree_node_type {
435 #define x(kwd, val, name) BKEY_TYPE_##kwd = val,
436         BCH_BTREE_IDS()
437 #undef x
438         BKEY_TYPE_BTREE,
439 };
440
441 /* Type of a key in btree @id at level @level: */
442 static inline enum btree_node_type __btree_node_type(unsigned level, enum btree_id id)
443 {
444         return level ? BKEY_TYPE_BTREE : (enum btree_node_type) id;
445 }
446
447 /* Type of keys @b contains: */
448 static inline enum btree_node_type btree_node_type(struct btree *b)
449 {
450         return __btree_node_type(b->level, b->btree_id);
451 }
452
453 static inline bool btree_node_type_is_extents(enum btree_node_type type)
454 {
455         switch (type) {
456         case BKEY_TYPE_EXTENTS:
457         case BKEY_TYPE_REFLINK:
458                 return true;
459         default:
460                 return false;
461         }
462 }
463
464 static inline bool btree_node_is_extents(struct btree *b)
465 {
466         return btree_node_type_is_extents(btree_node_type(b));
467 }
468
469 #define BTREE_NODE_TYPE_HAS_TRIGGERS                    \
470         ((1U << BKEY_TYPE_EXTENTS)|                     \
471          (1U << BKEY_TYPE_ALLOC)|                       \
472          (1U << BKEY_TYPE_INODES)|                      \
473          (1U << BKEY_TYPE_REFLINK)|                     \
474          (1U << BKEY_TYPE_EC)|                          \
475          (1U << BKEY_TYPE_BTREE))
476
477 #define BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS              \
478         ((1U << BKEY_TYPE_EXTENTS)|                     \
479          (1U << BKEY_TYPE_INODES)|                      \
480          (1U << BKEY_TYPE_REFLINK))
481
482 static inline bool btree_node_type_needs_gc(enum btree_node_type type)
483 {
484         return BTREE_NODE_TYPE_HAS_TRIGGERS & (1U << type);
485 }
486
487 struct btree_root {
488         struct btree            *b;
489
490         struct btree_update     *as;
491
492         /* On disk root - see async splits: */
493         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
494         u8                      level;
495         u8                      alive;
496         s8                      error;
497 };
498
499 /*
500  * Optional hook that will be called just prior to a btree node update, when
501  * we're holding the write lock and we know what key is about to be overwritten:
502  */
503
504 enum btree_insert_ret {
505         BTREE_INSERT_OK,
506         /* leaf node needs to be split */
507         BTREE_INSERT_BTREE_NODE_FULL,
508         BTREE_INSERT_ENOSPC,
509         BTREE_INSERT_NEED_MARK_REPLICAS,
510         BTREE_INSERT_NEED_JOURNAL_RES,
511 };
512
513 enum btree_gc_coalesce_fail_reason {
514         BTREE_GC_COALESCE_FAIL_RESERVE_GET,
515         BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC,
516         BTREE_GC_COALESCE_FAIL_FORMAT_FITS,
517 };
518
519 enum btree_node_sibling {
520         btree_prev_sib,
521         btree_next_sib,
522 };
523
524 typedef struct btree_nr_keys (*sort_fix_overlapping_fn)(struct bset *,
525                                                         struct btree *,
526                                                         struct btree_node_iter *);
527
528 #endif /* _BCACHEFS_BTREE_TYPES_H */