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