]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_types.h
be12c9ff7ea3cf7d823d8a6dff0ce162892c6d23
[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 "darray.h"
12 #include "journal_types.h"
13
14 struct open_bucket;
15 struct btree_update;
16 struct btree_trans;
17
18 #define MAX_BSETS               3U
19
20 struct btree_nr_keys {
21
22         /*
23          * Amount of live metadata (i.e. size of node after a compaction) in
24          * units of u64s
25          */
26         u16                     live_u64s;
27         u16                     bset_u64s[MAX_BSETS];
28
29         /* live keys only: */
30         u16                     packed_keys;
31         u16                     unpacked_keys;
32 };
33
34 struct bset_tree {
35         /*
36          * We construct a binary tree in an array as if the array
37          * started at 1, so that things line up on the same cachelines
38          * better: see comments in bset.c at cacheline_to_bkey() for
39          * details
40          */
41
42         /* size of the binary tree and prev array */
43         u16                     size;
44
45         /* function of size - precalculated for to_inorder() */
46         u16                     extra;
47
48         u16                     data_offset;
49         u16                     aux_data_offset;
50         u16                     end_offset;
51 };
52
53 struct btree_write {
54         struct journal_entry_pin        journal;
55 };
56
57 struct btree_alloc {
58         struct open_buckets     ob;
59         __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX);
60 };
61
62 struct btree_bkey_cached_common {
63         struct six_lock         lock;
64         u8                      level;
65         u8                      btree_id;
66 };
67
68 struct btree {
69         struct btree_bkey_cached_common c;
70
71         struct rhash_head       hash;
72         u64                     hash_val;
73
74         unsigned long           flags;
75         u16                     written;
76         u8                      nsets;
77         u8                      nr_key_bits;
78         u16                     version_ondisk;
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                      byte_order;
98         u8                      unpack_fn_len;
99
100         struct btree_write      writes[2];
101
102         /* Key/pointer for this btree node */
103         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
104
105         /*
106          * XXX: add a delete sequence number, so when bch2_btree_node_relock()
107          * fails because the lock sequence number has changed - i.e. the
108          * contents were modified - we can still relock the node if it's still
109          * the one we want, without redoing the traversal
110          */
111
112         /*
113          * For asynchronous splits/interior node updates:
114          * When we do a split, we allocate new child nodes and update the parent
115          * node to point to them: we update the parent in memory immediately,
116          * but then we must wait until the children have been written out before
117          * the update to the parent can be written - this is a list of the
118          * btree_updates that are blocking this node from being
119          * written:
120          */
121         struct list_head        write_blocked;
122
123         /*
124          * Also for asynchronous splits/interior node updates:
125          * If a btree node isn't reachable yet, we don't want to kick off
126          * another write - because that write also won't yet be reachable and
127          * marking it as completed before it's reachable would be incorrect:
128          */
129         unsigned long           will_make_reachable;
130
131         struct open_buckets     ob;
132
133         /* lru list */
134         struct list_head        list;
135 };
136
137 struct btree_cache {
138         struct rhashtable       table;
139         bool                    table_init_done;
140         /*
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.
147          *
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.
152          */
153         struct mutex            lock;
154         struct list_head        live;
155         struct list_head        freeable;
156         struct list_head        freed_pcpu;
157         struct list_head        freed_nonpcpu;
158
159         /* Number of elements in live + freeable lists */
160         unsigned                used;
161         unsigned                reserve;
162         atomic_t                dirty;
163         struct shrinker         shrink;
164
165         /*
166          * If we need to allocate memory for a new btree node and that
167          * allocation fails, we can cannibalize another node in the btree cache
168          * to satisfy the allocation - lock to guarantee only one thread does
169          * this at a time:
170          */
171         struct task_struct      *alloc_lock;
172         struct closure_waitlist alloc_wait;
173 };
174
175 struct btree_node_iter {
176         struct btree_node_iter_set {
177                 u16     k, end;
178         } data[MAX_BSETS];
179 };
180
181 /*
182  * Iterate over all possible positions, synthesizing deleted keys for holes:
183  */
184 #define BTREE_ITER_SLOTS                (1 << 0)
185 #define BTREE_ITER_ALL_LEVELS           (1 << 1)
186 /*
187  * Indicates that intent locks should be taken on leaf nodes, because we expect
188  * to be doing updates:
189  */
190 #define BTREE_ITER_INTENT               (1 << 2)
191 /*
192  * Causes the btree iterator code to prefetch additional btree nodes from disk:
193  */
194 #define BTREE_ITER_PREFETCH             (1 << 3)
195 /*
196  * Used in bch2_btree_iter_traverse(), to indicate whether we're searching for
197  * @pos or the first key strictly greater than @pos
198  */
199 #define BTREE_ITER_IS_EXTENTS           (1 << 4)
200 #define BTREE_ITER_NOT_EXTENTS          (1 << 5)
201 #define BTREE_ITER_CACHED               (1 << 6)
202 #define BTREE_ITER_CACHED_NOFILL        (1 << 7)
203 #define BTREE_ITER_CACHED_NOCREATE      (1 << 8)
204 #define BTREE_ITER_WITH_KEY_CACHE       (1 << 9)
205 #define BTREE_ITER_WITH_UPDATES         (1 << 10)
206 #define BTREE_ITER_WITH_JOURNAL         (1 << 11)
207 #define __BTREE_ITER_ALL_SNAPSHOTS      (1 << 12)
208 #define BTREE_ITER_ALL_SNAPSHOTS        (1 << 13)
209 #define BTREE_ITER_FILTER_SNAPSHOTS     (1 << 14)
210 #define BTREE_ITER_NOPRESERVE           (1 << 15)
211
212 enum btree_path_uptodate {
213         BTREE_ITER_UPTODATE             = 0,
214         BTREE_ITER_NEED_RELOCK          = 1,
215         BTREE_ITER_NEED_TRAVERSE        = 2,
216 };
217
218 #define BTREE_ITER_NO_NODE_GET_LOCKS    ((struct btree *) 1)
219 #define BTREE_ITER_NO_NODE_DROP         ((struct btree *) 2)
220 #define BTREE_ITER_NO_NODE_LOCK_ROOT    ((struct btree *) 3)
221 #define BTREE_ITER_NO_NODE_UP           ((struct btree *) 4)
222 #define BTREE_ITER_NO_NODE_DOWN         ((struct btree *) 5)
223 #define BTREE_ITER_NO_NODE_INIT         ((struct btree *) 6)
224 #define BTREE_ITER_NO_NODE_ERROR        ((struct btree *) 7)
225 #define BTREE_ITER_NO_NODE_CACHED       ((struct btree *) 8)
226
227 struct btree_path {
228         u8                      idx;
229         u8                      sorted_idx;
230         u8                      ref;
231         u8                      intent_ref;
232
233         /* btree_iter_copy starts here: */
234         struct bpos             pos;
235
236         enum btree_id           btree_id:4;
237         bool                    cached:1;
238         bool                    preserve:1;
239         enum btree_path_uptodate uptodate:2;
240         /*
241          * When true, failing to relock this path will cause the transaction to
242          * restart:
243          */
244         bool                    should_be_locked:1;
245         unsigned                level:3,
246                                 locks_want:4,
247                                 nodes_locked:4,
248                                 nodes_intent_locked:4;
249
250         struct btree_path_level {
251                 struct btree    *b;
252                 struct btree_node_iter iter;
253                 u32             lock_seq;
254         }                       l[BTREE_MAX_DEPTH];
255 #ifdef CONFIG_BCACHEFS_DEBUG
256         unsigned long           ip_allocated;
257 #endif
258 };
259
260 static inline struct btree_path_level *path_l(struct btree_path *path)
261 {
262         return path->l + path->level;
263 }
264
265 /*
266  * @pos                 - iterator's current position
267  * @level               - current btree depth
268  * @locks_want          - btree level below which we start taking intent locks
269  * @nodes_locked        - bitmask indicating which nodes in @nodes are locked
270  * @nodes_intent_locked - bitmask indicating which locks are intent locks
271  */
272 struct btree_iter {
273         struct btree_trans      *trans;
274         struct btree_path       *path;
275         struct btree_path       *update_path;
276         struct btree_path       *key_cache_path;
277
278         enum btree_id           btree_id:4;
279         unsigned                min_depth:3;
280         unsigned                advanced:1;
281
282         /* btree_iter_copy starts here: */
283         u16                     flags;
284
285         /* When we're filtering by snapshot, the snapshot ID we're looking for: */
286         unsigned                snapshot;
287
288         struct bpos             pos;
289         struct bpos             pos_after_commit;
290         /*
291          * Current unpacked key - so that bch2_btree_iter_next()/
292          * bch2_btree_iter_next_slot() can correctly advance pos.
293          */
294         struct bkey             k;
295
296         /* BTREE_ITER_WITH_JOURNAL: */
297         size_t                  journal_idx;
298         struct bpos             journal_pos;
299 #ifdef CONFIG_BCACHEFS_DEBUG
300         unsigned long           ip_allocated;
301 #endif
302 };
303
304 struct btree_key_cache_freelist {
305         struct bkey_cached      *objs[16];
306         unsigned                nr;
307 };
308
309 struct btree_key_cache {
310         struct mutex            lock;
311         struct rhashtable       table;
312         bool                    table_init_done;
313         struct list_head        freed;
314         struct shrinker         shrink;
315         unsigned                shrink_iter;
316         struct btree_key_cache_freelist __percpu *pcpu_freed;
317
318         atomic_long_t           nr_freed;
319         atomic_long_t           nr_keys;
320         atomic_long_t           nr_dirty;
321 };
322
323 struct bkey_cached_key {
324         u32                     btree_id;
325         struct bpos             pos;
326 } __attribute__((packed, aligned(4)));
327
328 #define BKEY_CACHED_ACCESSED            0
329 #define BKEY_CACHED_DIRTY               1
330
331 struct bkey_cached {
332         struct btree_bkey_cached_common c;
333
334         unsigned long           flags;
335         u16                     u64s;
336         bool                    valid;
337         u32                     btree_trans_barrier_seq;
338         struct bkey_cached_key  key;
339
340         struct rhash_head       hash;
341         struct list_head        list;
342
343         struct journal_preres   res;
344         struct journal_entry_pin journal;
345
346         struct bkey_i           *k;
347 };
348
349 struct btree_insert_entry {
350         unsigned                flags;
351         u8                      bkey_type;
352         enum btree_id           btree_id:8;
353         u8                      level:4;
354         bool                    cached:1;
355         bool                    insert_trigger_run:1;
356         bool                    overwrite_trigger_run:1;
357         bool                    key_cache_already_flushed:1;
358         /*
359          * @old_k may be a key from the journal; @old_btree_u64s always refers
360          * to the size of the key being overwritten in the btree:
361          */
362         u8                      old_btree_u64s;
363         struct bkey_i           *k;
364         struct btree_path       *path;
365         /* key being overwritten: */
366         struct bkey             old_k;
367         const struct bch_val    *old_v;
368         unsigned long           ip_allocated;
369 };
370
371 #ifndef CONFIG_LOCKDEP
372 #define BTREE_ITER_MAX          64
373 #else
374 #define BTREE_ITER_MAX          32
375 #endif
376
377 struct btree_trans_commit_hook;
378 typedef int (btree_trans_commit_hook_fn)(struct btree_trans *, struct btree_trans_commit_hook *);
379
380 struct btree_trans_commit_hook {
381         btree_trans_commit_hook_fn      *fn;
382         struct btree_trans_commit_hook  *next;
383 };
384
385 #define BTREE_TRANS_MEM_MAX     (1U << 16)
386
387 #define BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS       10000
388
389 struct btree_trans {
390         struct bch_fs           *c;
391         const char              *fn;
392         struct list_head        list;
393         u64                     last_begin_time;
394         struct btree            *locking;
395         unsigned                locking_path_idx;
396         struct bpos             locking_pos;
397         u8                      locking_btree_id;
398         u8                      locking_level;
399         u8                      locking_lock_type;
400         struct task_struct      *task;
401         int                     srcu_idx;
402
403         u8                      nr_sorted;
404         u8                      nr_updates;
405         u8                      traverse_all_idx;
406         bool                    used_mempool:1;
407         bool                    in_traverse_all:1;
408         bool                    restarted:1;
409         bool                    memory_allocation_failure:1;
410         bool                    is_initial_gc:1;
411         /*
412          * For when bch2_trans_update notices we'll be splitting a compressed
413          * extent:
414          */
415         unsigned                extra_journal_res;
416
417         u64                     paths_allocated;
418
419         unsigned                mem_top;
420         unsigned                mem_bytes;
421         void                    *mem;
422
423         u8                      sorted[BTREE_ITER_MAX];
424         struct btree_path       *paths;
425         struct btree_insert_entry *updates;
426
427         /* update path: */
428         struct btree_trans_commit_hook *hooks;
429         DARRAY(u64)             extra_journal_entries;
430         struct journal_entry_pin *journal_pin;
431
432         struct journal_res      journal_res;
433         struct journal_preres   journal_preres;
434         u64                     *journal_seq;
435         struct disk_reservation *disk_res;
436         unsigned                flags;
437         unsigned                journal_u64s;
438         unsigned                journal_preres_u64s;
439         struct replicas_delta_list *fs_usage_deltas;
440 };
441
442 #define BTREE_FLAGS()                                                   \
443         x(read_in_flight)                                               \
444         x(read_error)                                                   \
445         x(dirty)                                                        \
446         x(need_write)                                                   \
447         x(write_blocked)                                                \
448         x(will_make_reachable)                                          \
449         x(noevict)                                                      \
450         x(write_idx)                                                    \
451         x(accessed)                                                     \
452         x(write_in_flight)                                              \
453         x(write_in_flight_inner)                                        \
454         x(just_written)                                                 \
455         x(dying)                                                        \
456         x(fake)                                                         \
457         x(need_rewrite)                                                 \
458         x(never_write)
459
460 enum btree_flags {
461 #define x(flag) BTREE_NODE_##flag,
462         BTREE_FLAGS()
463 #undef x
464 };
465
466 #define x(flag)                                                         \
467 static inline bool btree_node_ ## flag(struct btree *b)                 \
468 {       return test_bit(BTREE_NODE_ ## flag, &b->flags); }              \
469                                                                         \
470 static inline void set_btree_node_ ## flag(struct btree *b)             \
471 {       set_bit(BTREE_NODE_ ## flag, &b->flags); }                      \
472                                                                         \
473 static inline void clear_btree_node_ ## flag(struct btree *b)           \
474 {       clear_bit(BTREE_NODE_ ## flag, &b->flags); }
475
476 BTREE_FLAGS()
477 #undef x
478
479 static inline struct btree_write *btree_current_write(struct btree *b)
480 {
481         return b->writes + btree_node_write_idx(b);
482 }
483
484 static inline struct btree_write *btree_prev_write(struct btree *b)
485 {
486         return b->writes + (btree_node_write_idx(b) ^ 1);
487 }
488
489 static inline struct bset_tree *bset_tree_last(struct btree *b)
490 {
491         EBUG_ON(!b->nsets);
492         return b->set + b->nsets - 1;
493 }
494
495 static inline void *
496 __btree_node_offset_to_ptr(const struct btree *b, u16 offset)
497 {
498         return (void *) ((u64 *) b->data + 1 + offset);
499 }
500
501 static inline u16
502 __btree_node_ptr_to_offset(const struct btree *b, const void *p)
503 {
504         u16 ret = (u64 *) p - 1 - (u64 *) b->data;
505
506         EBUG_ON(__btree_node_offset_to_ptr(b, ret) != p);
507         return ret;
508 }
509
510 static inline struct bset *bset(const struct btree *b,
511                                 const struct bset_tree *t)
512 {
513         return __btree_node_offset_to_ptr(b, t->data_offset);
514 }
515
516 static inline void set_btree_bset_end(struct btree *b, struct bset_tree *t)
517 {
518         t->end_offset =
519                 __btree_node_ptr_to_offset(b, vstruct_last(bset(b, t)));
520 }
521
522 static inline void set_btree_bset(struct btree *b, struct bset_tree *t,
523                                   const struct bset *i)
524 {
525         t->data_offset = __btree_node_ptr_to_offset(b, i);
526         set_btree_bset_end(b, t);
527 }
528
529 static inline struct bset *btree_bset_first(struct btree *b)
530 {
531         return bset(b, b->set);
532 }
533
534 static inline struct bset *btree_bset_last(struct btree *b)
535 {
536         return bset(b, bset_tree_last(b));
537 }
538
539 static inline u16
540 __btree_node_key_to_offset(const struct btree *b, const struct bkey_packed *k)
541 {
542         return __btree_node_ptr_to_offset(b, k);
543 }
544
545 static inline struct bkey_packed *
546 __btree_node_offset_to_key(const struct btree *b, u16 k)
547 {
548         return __btree_node_offset_to_ptr(b, k);
549 }
550
551 static inline unsigned btree_bkey_first_offset(const struct bset_tree *t)
552 {
553         return t->data_offset + offsetof(struct bset, _data) / sizeof(u64);
554 }
555
556 #define btree_bkey_first(_b, _t)                                        \
557 ({                                                                      \
558         EBUG_ON(bset(_b, _t)->start !=                                  \
559                 __btree_node_offset_to_key(_b, btree_bkey_first_offset(_t)));\
560                                                                         \
561         bset(_b, _t)->start;                                            \
562 })
563
564 #define btree_bkey_last(_b, _t)                                         \
565 ({                                                                      \
566         EBUG_ON(__btree_node_offset_to_key(_b, (_t)->end_offset) !=     \
567                 vstruct_last(bset(_b, _t)));                            \
568                                                                         \
569         __btree_node_offset_to_key(_b, (_t)->end_offset);               \
570 })
571
572 static inline unsigned bset_u64s(struct bset_tree *t)
573 {
574         return t->end_offset - t->data_offset -
575                 sizeof(struct bset) / sizeof(u64);
576 }
577
578 static inline unsigned bset_dead_u64s(struct btree *b, struct bset_tree *t)
579 {
580         return bset_u64s(t) - b->nr.bset_u64s[t - b->set];
581 }
582
583 static inline unsigned bset_byte_offset(struct btree *b, void *i)
584 {
585         return i - (void *) b->data;
586 }
587
588 enum btree_node_type {
589 #define x(kwd, val) BKEY_TYPE_##kwd = val,
590         BCH_BTREE_IDS()
591 #undef x
592         BKEY_TYPE_btree,
593 };
594
595 /* Type of a key in btree @id at level @level: */
596 static inline enum btree_node_type __btree_node_type(unsigned level, enum btree_id id)
597 {
598         return level ? BKEY_TYPE_btree : (enum btree_node_type) id;
599 }
600
601 /* Type of keys @b contains: */
602 static inline enum btree_node_type btree_node_type(struct btree *b)
603 {
604         return __btree_node_type(b->c.level, b->c.btree_id);
605 }
606
607 #define BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS              \
608         ((1U << BKEY_TYPE_extents)|                     \
609          (1U << BKEY_TYPE_alloc)|                       \
610          (1U << BKEY_TYPE_inodes)|                      \
611          (1U << BKEY_TYPE_stripes)|                     \
612          (1U << BKEY_TYPE_reflink)|                     \
613          (1U << BKEY_TYPE_btree))
614
615 #define BTREE_NODE_TYPE_HAS_MEM_TRIGGERS                \
616         ((1U << BKEY_TYPE_alloc)|                       \
617          (1U << BKEY_TYPE_inodes)|                      \
618          (1U << BKEY_TYPE_stripes)|                     \
619          (1U << BKEY_TYPE_snapshots))
620
621 #define BTREE_NODE_TYPE_HAS_TRIGGERS                    \
622         (BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS|            \
623          BTREE_NODE_TYPE_HAS_MEM_TRIGGERS)
624
625 #define BTREE_ID_IS_EXTENTS                             \
626         ((1U << BTREE_ID_extents)|                      \
627          (1U << BTREE_ID_reflink)|                      \
628          (1U << BTREE_ID_freespace))
629
630 static inline bool btree_node_type_is_extents(enum btree_node_type type)
631 {
632         return (1U << type) & BTREE_ID_IS_EXTENTS;
633 }
634
635 #define BTREE_ID_HAS_SNAPSHOTS                          \
636         ((1U << BTREE_ID_extents)|                      \
637          (1U << BTREE_ID_inodes)|                       \
638          (1U << BTREE_ID_dirents)|                      \
639          (1U << BTREE_ID_xattrs))
640
641 #define BTREE_ID_HAS_PTRS                               \
642         ((1U << BTREE_ID_extents)|                      \
643          (1U << BTREE_ID_reflink))
644
645 static inline bool btree_type_has_snapshots(enum btree_id id)
646 {
647         return (1 << id) & BTREE_ID_HAS_SNAPSHOTS;
648 }
649
650 static inline bool btree_type_has_ptrs(enum btree_id id)
651 {
652         return (1 << id) & BTREE_ID_HAS_PTRS;
653 }
654
655 static inline bool btree_node_type_needs_gc(enum btree_node_type type)
656 {
657         return BTREE_NODE_TYPE_HAS_TRIGGERS & (1U << type);
658 }
659
660 struct btree_root {
661         struct btree            *b;
662
663         /* On disk root - see async splits: */
664         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
665         u8                      level;
666         u8                      alive;
667         s8                      error;
668 };
669
670 enum btree_insert_ret {
671         BTREE_INSERT_OK,
672         /* leaf node needs to be split */
673         BTREE_INSERT_BTREE_NODE_FULL,
674         BTREE_INSERT_NEED_MARK_REPLICAS,
675         BTREE_INSERT_NEED_JOURNAL_RES,
676         BTREE_INSERT_NEED_JOURNAL_RECLAIM,
677 };
678
679 enum btree_gc_coalesce_fail_reason {
680         BTREE_GC_COALESCE_FAIL_RESERVE_GET,
681         BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC,
682         BTREE_GC_COALESCE_FAIL_FORMAT_FITS,
683 };
684
685 enum btree_node_sibling {
686         btree_prev_sib,
687         btree_next_sib,
688 };
689
690 #endif /* _BCACHEFS_BTREE_TYPES_H */