]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_types.h
e97248ca3aa27b11651292d50083a42c3c5862e5
[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 };
57
58 struct btree_alloc {
59         struct open_buckets     ob;
60         BKEY_PADDED(k);
61 };
62
63 struct btree {
64         /* Hottest entries first */
65         struct rhash_head       hash;
66         u64                     hash_val;
67
68         struct six_lock         lock;
69
70         unsigned long           flags;
71         u16                     written;
72         u8                      level;
73         u8                      btree_id;
74         u8                      nsets;
75         u8                      nr_key_bits;
76
77         struct bkey_format      format;
78
79         struct btree_node       *data;
80         void                    *aux_data;
81
82         /*
83          * Sets of sorted keys - the real btree node - plus a binary search tree
84          *
85          * set[0] is special; set[0]->tree, set[0]->prev and set[0]->data point
86          * to the memory we have allocated for this btree node. Additionally,
87          * set[0]->data points to the entire btree node as it exists on disk.
88          */
89         struct bset_tree        set[MAX_BSETS];
90
91         struct btree_nr_keys    nr;
92         u16                     sib_u64s[2];
93         u16                     whiteout_u64s;
94         u8                      page_order;
95         u8                      unpack_fn_len;
96
97         /*
98          * XXX: add a delete sequence number, so when bch2_btree_node_relock()
99          * fails because the lock sequence number has changed - i.e. the
100          * contents were modified - we can still relock the node if it's still
101          * the one we want, without redoing the traversal
102          */
103
104         /*
105          * For asynchronous splits/interior node updates:
106          * When we do a split, we allocate new child nodes and update the parent
107          * node to point to them: we update the parent in memory immediately,
108          * but then we must wait until the children have been written out before
109          * the update to the parent can be written - this is a list of the
110          * btree_updates that are blocking this node from being
111          * written:
112          */
113         struct list_head        write_blocked;
114
115         /*
116          * Also for asynchronous splits/interior node updates:
117          * If a btree node isn't reachable yet, we don't want to kick off
118          * another write - because that write also won't yet be reachable and
119          * marking it as completed before it's reachable would be incorrect:
120          */
121         unsigned long           will_make_reachable;
122
123         struct open_buckets     ob;
124
125         /* lru list */
126         struct list_head        list;
127
128         struct btree_write      writes[2];
129
130 #ifdef CONFIG_BCACHEFS_DEBUG
131         bool                    *expensive_debug_checks;
132 #endif
133
134         /* Key/pointer for this btree node */
135         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
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 /*
188  * Iterate over all possible positions, synthesizing deleted keys for holes:
189  */
190 #define BTREE_ITER_SLOTS                (1 << 2)
191 /*
192  * Indicates that intent locks should be taken on leaf nodes, because we expect
193  * to be doing updates:
194  */
195 #define BTREE_ITER_INTENT               (1 << 3)
196 /*
197  * Causes the btree iterator code to prefetch additional btree nodes from disk:
198  */
199 #define BTREE_ITER_PREFETCH             (1 << 4)
200 /*
201  * Indicates that this iterator should not be reused until transaction commit,
202  * either because a pending update references it or because the update depends
203  * on that particular key being locked (e.g. by the str_hash code, for hash
204  * table consistency)
205  */
206 #define BTREE_ITER_KEEP_UNTIL_COMMIT    (1 << 5)
207 /*
208  * Used in bch2_btree_iter_traverse(), to indicate whether we're searching for
209  * @pos or the first key strictly greater than @pos
210  */
211 #define BTREE_ITER_IS_EXTENTS           (1 << 6)
212 #define BTREE_ITER_ERROR                (1 << 7)
213 #define BTREE_ITER_SET_POS_AFTER_COMMIT (1 << 8)
214
215 enum btree_iter_uptodate {
216         BTREE_ITER_UPTODATE             = 0,
217         BTREE_ITER_NEED_PEEK            = 1,
218         BTREE_ITER_NEED_RELOCK          = 2,
219         BTREE_ITER_NEED_TRAVERSE        = 3,
220 };
221
222 /*
223  * @pos                 - iterator's current position
224  * @level               - current btree depth
225  * @locks_want          - btree level below which we start taking intent locks
226  * @nodes_locked        - bitmask indicating which nodes in @nodes are locked
227  * @nodes_intent_locked - bitmask indicating which locks are intent locks
228  */
229 struct btree_iter {
230         struct btree_trans      *trans;
231         struct bpos             pos;
232         struct bpos             pos_after_commit;
233
234         u16                     flags;
235         u8                      idx;
236
237         enum btree_id           btree_id:4;
238         enum btree_iter_uptodate uptodate:4;
239         unsigned                level:4,
240                                 min_depth:4,
241                                 locks_want:4,
242                                 nodes_locked:4,
243                                 nodes_intent_locked:4;
244
245         struct btree_iter_level {
246                 struct btree    *b;
247                 struct btree_node_iter iter;
248                 u32             lock_seq;
249         }                       l[BTREE_MAX_DEPTH];
250
251         /*
252          * Current unpacked key - so that bch2_btree_iter_next()/
253          * bch2_btree_iter_next_slot() can correctly advance pos.
254          */
255         struct bkey             k;
256         unsigned long           ip_allocated;
257 };
258
259 static inline enum btree_iter_type btree_iter_type(struct btree_iter *iter)
260 {
261         return iter->flags & BTREE_ITER_TYPE;
262 }
263
264 static inline struct btree_iter_level *iter_l(struct btree_iter *iter)
265 {
266         return iter->l + iter->level;
267 }
268
269 struct btree_insert_entry {
270         unsigned                trigger_flags;
271         unsigned                trans_triggers_run:1;
272         struct bkey_i           *k;
273         struct btree_iter       *iter;
274 };
275
276 #ifndef CONFIG_LOCKDEP
277 #define BTREE_ITER_MAX          64
278 #else
279 #define BTREE_ITER_MAX          32
280 #endif
281
282 struct btree_trans {
283         struct bch_fs           *c;
284 #ifdef CONFIG_BCACHEFS_DEBUG
285         struct list_head        list;
286         struct btree            *locking;
287         pid_t                   pid;
288 #endif
289         unsigned long           ip;
290
291         u64                     iters_linked;
292         u64                     iters_live;
293         u64                     iters_touched;
294
295         u8                      nr_iters;
296         u8                      nr_updates;
297         u8                      nr_updates2;
298         u8                      size;
299         unsigned                used_mempool:1;
300         unsigned                error:1;
301         unsigned                nounlock:1;
302         unsigned                need_reset:1;
303
304         unsigned                mem_top;
305         unsigned                mem_bytes;
306         void                    *mem;
307
308         struct btree_iter       *iters;
309         struct btree_insert_entry *updates;
310         struct btree_insert_entry *updates2;
311
312         /* update path: */
313         struct jset_entry       *extra_journal_entries;
314         unsigned                extra_journal_entry_u64s;
315         struct journal_entry_pin *journal_pin;
316
317         struct journal_res      journal_res;
318         struct journal_preres   journal_preres;
319         u64                     *journal_seq;
320         struct disk_reservation *disk_res;
321         unsigned                flags;
322         unsigned                journal_u64s;
323         unsigned                journal_preres_u64s;
324         struct replicas_delta_list *fs_usage_deltas;
325
326         struct btree_iter       iters_onstack[2];
327         struct btree_insert_entry updates_onstack[2];
328         struct btree_insert_entry updates2_onstack[2];
329 };
330
331 #define BTREE_FLAG(flag)                                                \
332 static inline bool btree_node_ ## flag(struct btree *b)                 \
333 {       return test_bit(BTREE_NODE_ ## flag, &b->flags); }              \
334                                                                         \
335 static inline void set_btree_node_ ## flag(struct btree *b)             \
336 {       set_bit(BTREE_NODE_ ## flag, &b->flags); }                      \
337                                                                         \
338 static inline void clear_btree_node_ ## flag(struct btree *b)           \
339 {       clear_bit(BTREE_NODE_ ## flag, &b->flags); }
340
341 enum btree_flags {
342         BTREE_NODE_read_in_flight,
343         BTREE_NODE_read_error,
344         BTREE_NODE_dirty,
345         BTREE_NODE_need_write,
346         BTREE_NODE_noevict,
347         BTREE_NODE_write_idx,
348         BTREE_NODE_accessed,
349         BTREE_NODE_write_in_flight,
350         BTREE_NODE_just_written,
351         BTREE_NODE_dying,
352         BTREE_NODE_fake,
353         BTREE_NODE_old_extent_overwrite,
354 };
355
356 BTREE_FLAG(read_in_flight);
357 BTREE_FLAG(read_error);
358 BTREE_FLAG(dirty);
359 BTREE_FLAG(need_write);
360 BTREE_FLAG(noevict);
361 BTREE_FLAG(write_idx);
362 BTREE_FLAG(accessed);
363 BTREE_FLAG(write_in_flight);
364 BTREE_FLAG(just_written);
365 BTREE_FLAG(dying);
366 BTREE_FLAG(fake);
367 BTREE_FLAG(old_extent_overwrite);
368
369 static inline struct btree_write *btree_current_write(struct btree *b)
370 {
371         return b->writes + btree_node_write_idx(b);
372 }
373
374 static inline struct btree_write *btree_prev_write(struct btree *b)
375 {
376         return b->writes + (btree_node_write_idx(b) ^ 1);
377 }
378
379 static inline struct bset_tree *bset_tree_last(struct btree *b)
380 {
381         EBUG_ON(!b->nsets);
382         return b->set + b->nsets - 1;
383 }
384
385 static inline void *
386 __btree_node_offset_to_ptr(const struct btree *b, u16 offset)
387 {
388         return (void *) ((u64 *) b->data + 1 + offset);
389 }
390
391 static inline u16
392 __btree_node_ptr_to_offset(const struct btree *b, const void *p)
393 {
394         u16 ret = (u64 *) p - 1 - (u64 *) b->data;
395
396         EBUG_ON(__btree_node_offset_to_ptr(b, ret) != p);
397         return ret;
398 }
399
400 static inline struct bset *bset(const struct btree *b,
401                                 const struct bset_tree *t)
402 {
403         return __btree_node_offset_to_ptr(b, t->data_offset);
404 }
405
406 static inline void set_btree_bset_end(struct btree *b, struct bset_tree *t)
407 {
408         t->end_offset =
409                 __btree_node_ptr_to_offset(b, vstruct_last(bset(b, t)));
410 }
411
412 static inline void set_btree_bset(struct btree *b, struct bset_tree *t,
413                                   const struct bset *i)
414 {
415         t->data_offset = __btree_node_ptr_to_offset(b, i);
416         set_btree_bset_end(b, t);
417 }
418
419 static inline struct bset *btree_bset_first(struct btree *b)
420 {
421         return bset(b, b->set);
422 }
423
424 static inline struct bset *btree_bset_last(struct btree *b)
425 {
426         return bset(b, bset_tree_last(b));
427 }
428
429 static inline u16
430 __btree_node_key_to_offset(const struct btree *b, const struct bkey_packed *k)
431 {
432         return __btree_node_ptr_to_offset(b, k);
433 }
434
435 static inline struct bkey_packed *
436 __btree_node_offset_to_key(const struct btree *b, u16 k)
437 {
438         return __btree_node_offset_to_ptr(b, k);
439 }
440
441 static inline unsigned btree_bkey_first_offset(const struct bset_tree *t)
442 {
443         return t->data_offset + offsetof(struct bset, _data) / sizeof(u64);
444 }
445
446 #define btree_bkey_first(_b, _t)                                        \
447 ({                                                                      \
448         EBUG_ON(bset(_b, _t)->start !=                                  \
449                 __btree_node_offset_to_key(_b, btree_bkey_first_offset(_t)));\
450                                                                         \
451         bset(_b, _t)->start;                                            \
452 })
453
454 #define btree_bkey_last(_b, _t)                                         \
455 ({                                                                      \
456         EBUG_ON(__btree_node_offset_to_key(_b, (_t)->end_offset) !=     \
457                 vstruct_last(bset(_b, _t)));                            \
458                                                                         \
459         __btree_node_offset_to_key(_b, (_t)->end_offset);               \
460 })
461
462 static inline unsigned bset_u64s(struct bset_tree *t)
463 {
464         return t->end_offset - t->data_offset -
465                 sizeof(struct bset) / sizeof(u64);
466 }
467
468 static inline unsigned bset_dead_u64s(struct btree *b, struct bset_tree *t)
469 {
470         return bset_u64s(t) - b->nr.bset_u64s[t - b->set];
471 }
472
473 static inline unsigned bset_byte_offset(struct btree *b, void *i)
474 {
475         return i - (void *) b->data;
476 }
477
478 enum btree_node_type {
479 #define x(kwd, val, name) BKEY_TYPE_##kwd = val,
480         BCH_BTREE_IDS()
481 #undef x
482         BKEY_TYPE_BTREE,
483 };
484
485 /* Type of a key in btree @id at level @level: */
486 static inline enum btree_node_type __btree_node_type(unsigned level, enum btree_id id)
487 {
488         return level ? BKEY_TYPE_BTREE : (enum btree_node_type) id;
489 }
490
491 /* Type of keys @b contains: */
492 static inline enum btree_node_type btree_node_type(struct btree *b)
493 {
494         return __btree_node_type(b->level, b->btree_id);
495 }
496
497 static inline bool btree_node_type_is_extents(enum btree_node_type type)
498 {
499         switch (type) {
500         case BKEY_TYPE_EXTENTS:
501         case BKEY_TYPE_REFLINK:
502                 return true;
503         default:
504                 return false;
505         }
506 }
507
508 static inline bool btree_node_is_extents(struct btree *b)
509 {
510         return btree_node_type_is_extents(btree_node_type(b));
511 }
512
513 #define BTREE_NODE_TYPE_HAS_TRIGGERS                    \
514         ((1U << BKEY_TYPE_EXTENTS)|                     \
515          (1U << BKEY_TYPE_ALLOC)|                       \
516          (1U << BKEY_TYPE_INODES)|                      \
517          (1U << BKEY_TYPE_REFLINK)|                     \
518          (1U << BKEY_TYPE_EC)|                          \
519          (1U << BKEY_TYPE_BTREE))
520
521 #define BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS              \
522         ((1U << BKEY_TYPE_EXTENTS)|                     \
523          (1U << BKEY_TYPE_INODES)|                      \
524          (1U << BKEY_TYPE_REFLINK))
525
526 enum btree_trigger_flags {
527         __BTREE_TRIGGER_NORUN,          /* Don't run triggers at all */
528         __BTREE_TRIGGER_NOOVERWRITES,   /* Don't run triggers on overwrites */
529
530         __BTREE_TRIGGER_INSERT,
531         __BTREE_TRIGGER_OVERWRITE,
532         __BTREE_TRIGGER_OVERWRITE_SPLIT,
533
534         __BTREE_TRIGGER_GC,
535         __BTREE_TRIGGER_BUCKET_INVALIDATE,
536         __BTREE_TRIGGER_ALLOC_READ,
537         __BTREE_TRIGGER_NOATOMIC,
538 };
539
540 #define BTREE_TRIGGER_NORUN             (1U << __BTREE_TRIGGER_NORUN)
541 #define BTREE_TRIGGER_NOOVERWRITES      (1U << __BTREE_TRIGGER_NOOVERWRITES)
542
543 #define BTREE_TRIGGER_INSERT            (1U << __BTREE_TRIGGER_INSERT)
544 #define BTREE_TRIGGER_OVERWRITE         (1U << __BTREE_TRIGGER_OVERWRITE)
545 #define BTREE_TRIGGER_OVERWRITE_SPLIT   (1U << __BTREE_TRIGGER_OVERWRITE_SPLIT)
546
547 #define BTREE_TRIGGER_GC                (1U << __BTREE_TRIGGER_GC)
548 #define BTREE_TRIGGER_BUCKET_INVALIDATE (1U << __BTREE_TRIGGER_BUCKET_INVALIDATE)
549 #define BTREE_TRIGGER_ALLOC_READ        (1U << __BTREE_TRIGGER_ALLOC_READ)
550 #define BTREE_TRIGGER_NOATOMIC          (1U << __BTREE_TRIGGER_NOATOMIC)
551
552 static inline bool btree_node_type_needs_gc(enum btree_node_type type)
553 {
554         return BTREE_NODE_TYPE_HAS_TRIGGERS & (1U << type);
555 }
556
557 struct btree_root {
558         struct btree            *b;
559
560         /* On disk root - see async splits: */
561         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
562         u8                      level;
563         u8                      alive;
564         s8                      error;
565 };
566
567 /*
568  * Optional hook that will be called just prior to a btree node update, when
569  * we're holding the write lock and we know what key is about to be overwritten:
570  */
571
572 enum btree_insert_ret {
573         BTREE_INSERT_OK,
574         /* leaf node needs to be split */
575         BTREE_INSERT_BTREE_NODE_FULL,
576         BTREE_INSERT_ENOSPC,
577         BTREE_INSERT_NEED_MARK_REPLICAS,
578         BTREE_INSERT_NEED_JOURNAL_RES,
579 };
580
581 enum btree_gc_coalesce_fail_reason {
582         BTREE_GC_COALESCE_FAIL_RESERVE_GET,
583         BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC,
584         BTREE_GC_COALESCE_FAIL_FORMAT_FITS,
585 };
586
587 enum btree_node_sibling {
588         btree_prev_sib,
589         btree_next_sib,
590 };
591
592 typedef struct btree_nr_keys (*sort_fix_overlapping_fn)(struct bset *,
593                                                         struct btree *,
594                                                         struct btree_node_iter *);
595
596 #endif /* _BCACHEFS_BTREE_TYPES_H */