]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_types.h
Update bcachefs sources to d83b992f65 bcachefs: Rewrite journal_seq_blacklist machinery
[bcachefs-tools-debian] / libbcachefs / btree_types.h
1 #ifndef _BCACHEFS_BTREE_TYPES_H
2 #define _BCACHEFS_BTREE_TYPES_H
3
4 #include <linux/list.h>
5 #include <linux/rhashtable.h>
6 #include <linux/six.h>
7
8 #include "bkey_methods.h"
9 #include "journal_types.h"
10
11 struct open_bucket;
12 struct btree_update;
13 struct btree_trans;
14
15 #define MAX_BSETS               3U
16
17 struct btree_nr_keys {
18
19         /*
20          * Amount of live metadata (i.e. size of node after a compaction) in
21          * units of u64s
22          */
23         u16                     live_u64s;
24         u16                     bset_u64s[MAX_BSETS];
25
26         /* live keys only: */
27         u16                     packed_keys;
28         u16                     unpacked_keys;
29 };
30
31 struct bset_tree {
32         /*
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
36          * details
37          */
38
39         /* size of the binary tree and prev array */
40         u16                     size;
41
42         /* function of size - precalculated for to_inorder() */
43         u16                     extra;
44
45         u16                     data_offset;
46         u16                     aux_data_offset;
47         u16                     end_offset;
48
49         struct bpos             max_key;
50 };
51
52 struct btree_write {
53         struct journal_entry_pin        journal;
54         struct closure_waitlist         wait;
55 };
56
57 struct btree_alloc {
58         struct open_buckets     ob;
59         BKEY_PADDED(k);
60 };
61
62 struct btree {
63         /* Hottest entries first */
64         struct rhash_head       hash;
65
66         /* Key/pointer for this btree node */
67         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
68
69         struct six_lock         lock;
70
71         unsigned long           flags;
72         u16                     written;
73         u8                      level;
74         u8                      btree_id;
75         u8                      nsets;
76         u8                      nr_key_bits;
77
78         struct bkey_format      format;
79
80         struct btree_node       *data;
81         void                    *aux_data;
82
83         /*
84          * Sets of sorted keys - the real btree node - plus a binary search tree
85          *
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.
89          */
90         struct bset_tree        set[MAX_BSETS];
91
92         struct btree_nr_keys    nr;
93         u16                     sib_u64s[2];
94         u16                     whiteout_u64s;
95         u16                     uncompacted_whiteout_u64s;
96         u8                      page_order;
97         u8                      unpack_fn_len;
98
99         /*
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
104          */
105
106         /*
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
113          * written:
114          */
115         struct list_head        write_blocked;
116
117         /*
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:
122          */
123         unsigned long           will_make_reachable;
124
125         struct open_buckets     ob;
126
127         /* lru list */
128         struct list_head        list;
129
130         struct btree_write      writes[2];
131
132 #ifdef CONFIG_BCACHEFS_DEBUG
133         bool                    *expensive_debug_checks;
134 #endif
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;
157
158         /* Number of elements in live + freeable lists */
159         unsigned                used;
160         unsigned                reserve;
161         struct shrinker         shrink;
162
163         /*
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
167          * this at a time:
168          */
169         struct task_struct      *alloc_lock;
170         struct closure_waitlist alloc_wait;
171 };
172
173 struct btree_node_iter {
174         struct btree_node_iter_set {
175                 u16     k, end;
176         } data[MAX_BSETS];
177 };
178
179 enum btree_iter_type {
180         BTREE_ITER_KEYS,
181         BTREE_ITER_SLOTS,
182         BTREE_ITER_NODES,
183 };
184
185 #define BTREE_ITER_TYPE                 ((1 << 2) - 1)
186
187 #define BTREE_ITER_INTENT               (1 << 2)
188 #define BTREE_ITER_PREFETCH             (1 << 3)
189 /*
190  * Used in bch2_btree_iter_traverse(), to indicate whether we're searching for
191  * @pos or the first key strictly greater than @pos
192  */
193 #define BTREE_ITER_IS_EXTENTS           (1 << 4)
194 #define BTREE_ITER_ERROR                (1 << 5)
195 #define BTREE_ITER_NOUNLOCK             (1 << 6)
196
197 enum btree_iter_uptodate {
198         BTREE_ITER_UPTODATE             = 0,
199         BTREE_ITER_NEED_PEEK            = 1,
200         BTREE_ITER_NEED_RELOCK          = 2,
201         BTREE_ITER_NEED_TRAVERSE        = 3,
202 };
203
204 /*
205  * @pos                 - iterator's current position
206  * @level               - current btree depth
207  * @locks_want          - btree level below which we start taking intent locks
208  * @nodes_locked        - bitmask indicating which nodes in @nodes are locked
209  * @nodes_intent_locked - bitmask indicating which locks are intent locks
210  */
211 struct btree_iter {
212         u8                      idx;
213
214         struct btree_trans      *trans;
215         struct bpos             pos;
216
217         u8                      flags;
218         enum btree_iter_uptodate uptodate:4;
219         enum btree_id           btree_id:4;
220         unsigned                level:4,
221                                 locks_want:4,
222                                 nodes_locked:4,
223                                 nodes_intent_locked:4;
224
225         struct btree_iter_level {
226                 struct btree    *b;
227                 struct btree_node_iter iter;
228                 u32             lock_seq;
229         }                       l[BTREE_MAX_DEPTH];
230
231         /*
232          * Current unpacked key - so that bch2_btree_iter_next()/
233          * bch2_btree_iter_next_slot() can correctly advance pos.
234          */
235         struct bkey             k;
236
237         u64                     id;
238 };
239
240 struct deferred_update {
241         struct journal_preres   res;
242         struct journal_entry_pin journal;
243
244         spinlock_t              lock;
245         unsigned                dirty:1;
246
247         u8                      allocated_u64s;
248         enum btree_id           btree_id;
249
250         /* must be last: */
251         struct bkey_i           k;
252 };
253
254 struct btree_insert_entry {
255         struct bkey_i           *k;
256
257         union {
258         struct btree_iter       *iter;
259         struct deferred_update  *d;
260         };
261
262         bool                    deferred;
263 };
264
265 #define BTREE_ITER_MAX          64
266
267 struct btree_trans {
268         struct bch_fs           *c;
269         size_t                  nr_restarts;
270         u64                     commit_start;
271
272         u64                     iters_linked;
273         u64                     iters_live;
274         u64                     iters_touched;
275         u64                     iters_unlink_on_restart;
276         u64                     iters_unlink_on_commit;
277
278         u8                      nr_iters;
279         u8                      nr_updates;
280         u8                      size;
281         unsigned                used_mempool:1;
282         unsigned                error:1;
283
284         unsigned                mem_top;
285         unsigned                mem_bytes;
286         void                    *mem;
287
288         struct btree_iter       *iters;
289         struct btree_insert_entry *updates;
290
291         /* update path: */
292         struct journal_res      journal_res;
293         struct journal_preres   journal_preres;
294         u64                     *journal_seq;
295         struct disk_reservation *disk_res;
296         unsigned                flags;
297
298         struct btree_iter       iters_onstack[2];
299         struct btree_insert_entry updates_onstack[6];
300 };
301
302 #define BTREE_FLAG(flag)                                                \
303 static inline bool btree_node_ ## flag(struct btree *b)                 \
304 {       return test_bit(BTREE_NODE_ ## flag, &b->flags); }              \
305                                                                         \
306 static inline void set_btree_node_ ## flag(struct btree *b)             \
307 {       set_bit(BTREE_NODE_ ## flag, &b->flags); }                      \
308                                                                         \
309 static inline void clear_btree_node_ ## flag(struct btree *b)           \
310 {       clear_bit(BTREE_NODE_ ## flag, &b->flags); }
311
312 enum btree_flags {
313         BTREE_NODE_read_in_flight,
314         BTREE_NODE_read_error,
315         BTREE_NODE_dirty,
316         BTREE_NODE_need_write,
317         BTREE_NODE_noevict,
318         BTREE_NODE_write_idx,
319         BTREE_NODE_accessed,
320         BTREE_NODE_write_in_flight,
321         BTREE_NODE_just_written,
322         BTREE_NODE_dying,
323         BTREE_NODE_fake,
324 };
325
326 BTREE_FLAG(read_in_flight);
327 BTREE_FLAG(read_error);
328 BTREE_FLAG(dirty);
329 BTREE_FLAG(need_write);
330 BTREE_FLAG(noevict);
331 BTREE_FLAG(write_idx);
332 BTREE_FLAG(accessed);
333 BTREE_FLAG(write_in_flight);
334 BTREE_FLAG(just_written);
335 BTREE_FLAG(dying);
336 BTREE_FLAG(fake);
337
338 static inline struct btree_write *btree_current_write(struct btree *b)
339 {
340         return b->writes + btree_node_write_idx(b);
341 }
342
343 static inline struct btree_write *btree_prev_write(struct btree *b)
344 {
345         return b->writes + (btree_node_write_idx(b) ^ 1);
346 }
347
348 static inline struct bset_tree *bset_tree_last(struct btree *b)
349 {
350         EBUG_ON(!b->nsets);
351         return b->set + b->nsets - 1;
352 }
353
354 static inline void *
355 __btree_node_offset_to_ptr(const struct btree *b, u16 offset)
356 {
357         return (void *) ((u64 *) b->data + 1 + offset);
358 }
359
360 static inline u16
361 __btree_node_ptr_to_offset(const struct btree *b, const void *p)
362 {
363         u16 ret = (u64 *) p - 1 - (u64 *) b->data;
364
365         EBUG_ON(__btree_node_offset_to_ptr(b, ret) != p);
366         return ret;
367 }
368
369 static inline struct bset *bset(const struct btree *b,
370                                 const struct bset_tree *t)
371 {
372         return __btree_node_offset_to_ptr(b, t->data_offset);
373 }
374
375 static inline void set_btree_bset_end(struct btree *b, struct bset_tree *t)
376 {
377         t->end_offset =
378                 __btree_node_ptr_to_offset(b, vstruct_last(bset(b, t)));
379 }
380
381 static inline void set_btree_bset(struct btree *b, struct bset_tree *t,
382                                   const struct bset *i)
383 {
384         t->data_offset = __btree_node_ptr_to_offset(b, i);
385         set_btree_bset_end(b, t);
386 }
387
388 static inline struct bset *btree_bset_first(struct btree *b)
389 {
390         return bset(b, b->set);
391 }
392
393 static inline struct bset *btree_bset_last(struct btree *b)
394 {
395         return bset(b, bset_tree_last(b));
396 }
397
398 static inline u16
399 __btree_node_key_to_offset(const struct btree *b, const struct bkey_packed *k)
400 {
401         return __btree_node_ptr_to_offset(b, k);
402 }
403
404 static inline struct bkey_packed *
405 __btree_node_offset_to_key(const struct btree *b, u16 k)
406 {
407         return __btree_node_offset_to_ptr(b, k);
408 }
409
410 static inline unsigned btree_bkey_first_offset(const struct bset_tree *t)
411 {
412         return t->data_offset + offsetof(struct bset, _data) / sizeof(u64);
413 }
414
415 #define btree_bkey_first(_b, _t)                                        \
416 ({                                                                      \
417         EBUG_ON(bset(_b, _t)->start !=                                  \
418                 __btree_node_offset_to_key(_b, btree_bkey_first_offset(_t)));\
419                                                                         \
420         bset(_b, _t)->start;                                            \
421 })
422
423 #define btree_bkey_last(_b, _t)                                         \
424 ({                                                                      \
425         EBUG_ON(__btree_node_offset_to_key(_b, (_t)->end_offset) !=     \
426                 vstruct_last(bset(_b, _t)));                            \
427                                                                         \
428         __btree_node_offset_to_key(_b, (_t)->end_offset);               \
429 })
430
431 static inline unsigned bset_byte_offset(struct btree *b, void *i)
432 {
433         return i - (void *) b->data;
434 }
435
436 enum btree_node_type {
437 #define x(kwd, val, name) BKEY_TYPE_##kwd = val,
438         BCH_BTREE_IDS()
439 #undef x
440         BKEY_TYPE_BTREE,
441 };
442
443 /* Type of a key in btree @id at level @level: */
444 static inline enum btree_node_type __btree_node_type(unsigned level, enum btree_id id)
445 {
446         return level ? BKEY_TYPE_BTREE : (enum btree_node_type) id;
447 }
448
449 /* Type of keys @b contains: */
450 static inline enum btree_node_type btree_node_type(struct btree *b)
451 {
452         return __btree_node_type(b->level, b->btree_id);
453 }
454
455 static inline bool btree_node_type_is_extents(enum btree_node_type type)
456 {
457         return type == BKEY_TYPE_EXTENTS;
458 }
459
460 static inline bool btree_node_is_extents(struct btree *b)
461 {
462         return btree_node_type_is_extents(btree_node_type(b));
463 }
464
465 static inline bool btree_node_type_needs_gc(enum btree_node_type type)
466 {
467         switch (type) {
468         case BKEY_TYPE_ALLOC:
469         case BKEY_TYPE_BTREE:
470         case BKEY_TYPE_EXTENTS:
471         case BKEY_TYPE_INODES:
472         case BKEY_TYPE_EC:
473                 return true;
474         default:
475                 return false;
476         }
477 }
478
479 struct btree_root {
480         struct btree            *b;
481
482         struct btree_update     *as;
483
484         /* On disk root - see async splits: */
485         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
486         u8                      level;
487         u8                      alive;
488         s8                      error;
489 };
490
491 /*
492  * Optional hook that will be called just prior to a btree node update, when
493  * we're holding the write lock and we know what key is about to be overwritten:
494  */
495
496 enum btree_insert_ret {
497         BTREE_INSERT_OK,
498         /* leaf node needs to be split */
499         BTREE_INSERT_BTREE_NODE_FULL,
500         BTREE_INSERT_ENOSPC,
501         BTREE_INSERT_NEED_MARK_REPLICAS,
502         BTREE_INSERT_NEED_JOURNAL_RES,
503 };
504
505 enum btree_gc_coalesce_fail_reason {
506         BTREE_GC_COALESCE_FAIL_RESERVE_GET,
507         BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC,
508         BTREE_GC_COALESCE_FAIL_FORMAT_FITS,
509 };
510
511 enum btree_node_sibling {
512         btree_prev_sib,
513         btree_next_sib,
514 };
515
516 typedef struct btree_nr_keys (*sort_fix_overlapping_fn)(struct bset *,
517                                                         struct btree *,
518                                                         struct btree_node_iter *);
519
520 #endif /* _BCACHEFS_BTREE_TYPES_H */