]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_types.h
Update bcachefs sources to 7227ff07f14b Merge pull request #10 from modelrockettier...
[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
195 enum btree_iter_uptodate {
196         BTREE_ITER_UPTODATE             = 0,
197         BTREE_ITER_NEED_PEEK            = 1,
198         BTREE_ITER_NEED_RELOCK          = 2,
199         BTREE_ITER_NEED_TRAVERSE        = 3,
200 };
201
202 /*
203  * @pos                 - iterator's current position
204  * @level               - current btree depth
205  * @locks_want          - btree level below which we start taking intent locks
206  * @nodes_locked        - bitmask indicating which nodes in @nodes are locked
207  * @nodes_intent_locked - bitmask indicating which locks are intent locks
208  */
209 struct btree_iter {
210         struct bch_fs           *c;
211         struct bpos             pos;
212
213         u8                      flags;
214         enum btree_iter_uptodate uptodate:4;
215         enum btree_id           btree_id:4;
216         unsigned                level:4,
217                                 locks_want:4,
218                                 nodes_locked:4,
219                                 nodes_intent_locked:4;
220
221         struct btree_iter_level {
222                 struct btree    *b;
223                 struct btree_node_iter iter;
224                 u32             lock_seq;
225         }                       l[BTREE_MAX_DEPTH];
226
227         /*
228          * Current unpacked key - so that bch2_btree_iter_next()/
229          * bch2_btree_iter_next_slot() can correctly advance pos.
230          */
231         struct bkey             k;
232
233         /*
234          * Circular linked list of linked iterators: linked iterators share
235          * locks (e.g. two linked iterators may have the same node intent
236          * locked, or read and write locked, at the same time), and insertions
237          * through one iterator won't invalidate the other linked iterators.
238          */
239
240         /* Must come last: */
241         struct btree_iter       *next;
242 };
243
244 #define BTREE_ITER_MAX          8
245
246 struct btree_insert_entry {
247         struct btree_iter *iter;
248         struct bkey_i   *k;
249 };
250
251 struct btree_trans {
252         struct bch_fs           *c;
253         size_t                  nr_restarts;
254
255         u8                      nr_iters;
256         u8                      iters_live;
257         u8                      iters_linked;
258         u8                      nr_updates;
259
260         unsigned                mem_top;
261         unsigned                mem_bytes;
262         void                    *mem;
263
264         struct btree_iter       *iters;
265         u64                     iter_ids[BTREE_ITER_MAX];
266
267         struct btree_insert_entry updates[BTREE_ITER_MAX];
268
269         struct btree_iter       iters_onstack[2];
270 };
271
272 #define BTREE_FLAG(flag)                                                \
273 static inline bool btree_node_ ## flag(struct btree *b)                 \
274 {       return test_bit(BTREE_NODE_ ## flag, &b->flags); }              \
275                                                                         \
276 static inline void set_btree_node_ ## flag(struct btree *b)             \
277 {       set_bit(BTREE_NODE_ ## flag, &b->flags); }                      \
278                                                                         \
279 static inline void clear_btree_node_ ## flag(struct btree *b)           \
280 {       clear_bit(BTREE_NODE_ ## flag, &b->flags); }
281
282 enum btree_flags {
283         BTREE_NODE_read_in_flight,
284         BTREE_NODE_read_error,
285         BTREE_NODE_dirty,
286         BTREE_NODE_need_write,
287         BTREE_NODE_noevict,
288         BTREE_NODE_write_idx,
289         BTREE_NODE_accessed,
290         BTREE_NODE_write_in_flight,
291         BTREE_NODE_just_written,
292         BTREE_NODE_dying,
293         BTREE_NODE_fake,
294 };
295
296 BTREE_FLAG(read_in_flight);
297 BTREE_FLAG(read_error);
298 BTREE_FLAG(dirty);
299 BTREE_FLAG(need_write);
300 BTREE_FLAG(noevict);
301 BTREE_FLAG(write_idx);
302 BTREE_FLAG(accessed);
303 BTREE_FLAG(write_in_flight);
304 BTREE_FLAG(just_written);
305 BTREE_FLAG(dying);
306 BTREE_FLAG(fake);
307
308 static inline struct btree_write *btree_current_write(struct btree *b)
309 {
310         return b->writes + btree_node_write_idx(b);
311 }
312
313 static inline struct btree_write *btree_prev_write(struct btree *b)
314 {
315         return b->writes + (btree_node_write_idx(b) ^ 1);
316 }
317
318 static inline struct bset_tree *bset_tree_last(struct btree *b)
319 {
320         EBUG_ON(!b->nsets);
321         return b->set + b->nsets - 1;
322 }
323
324 static inline void *
325 __btree_node_offset_to_ptr(const struct btree *b, u16 offset)
326 {
327         return (void *) ((u64 *) b->data + 1 + offset);
328 }
329
330 static inline u16
331 __btree_node_ptr_to_offset(const struct btree *b, const void *p)
332 {
333         u16 ret = (u64 *) p - 1 - (u64 *) b->data;
334
335         EBUG_ON(__btree_node_offset_to_ptr(b, ret) != p);
336         return ret;
337 }
338
339 static inline struct bset *bset(const struct btree *b,
340                                 const struct bset_tree *t)
341 {
342         return __btree_node_offset_to_ptr(b, t->data_offset);
343 }
344
345 static inline void set_btree_bset_end(struct btree *b, struct bset_tree *t)
346 {
347         t->end_offset =
348                 __btree_node_ptr_to_offset(b, vstruct_last(bset(b, t)));
349 }
350
351 static inline void set_btree_bset(struct btree *b, struct bset_tree *t,
352                                   const struct bset *i)
353 {
354         t->data_offset = __btree_node_ptr_to_offset(b, i);
355         set_btree_bset_end(b, t);
356 }
357
358 static inline struct bset *btree_bset_first(struct btree *b)
359 {
360         return bset(b, b->set);
361 }
362
363 static inline struct bset *btree_bset_last(struct btree *b)
364 {
365         return bset(b, bset_tree_last(b));
366 }
367
368 static inline u16
369 __btree_node_key_to_offset(const struct btree *b, const struct bkey_packed *k)
370 {
371         return __btree_node_ptr_to_offset(b, k);
372 }
373
374 static inline struct bkey_packed *
375 __btree_node_offset_to_key(const struct btree *b, u16 k)
376 {
377         return __btree_node_offset_to_ptr(b, k);
378 }
379
380 static inline unsigned btree_bkey_first_offset(const struct bset_tree *t)
381 {
382         return t->data_offset + offsetof(struct bset, _data) / sizeof(u64);
383 }
384
385 #define btree_bkey_first(_b, _t)                                        \
386 ({                                                                      \
387         EBUG_ON(bset(_b, _t)->start !=                                  \
388                 __btree_node_offset_to_key(_b, btree_bkey_first_offset(_t)));\
389                                                                         \
390         bset(_b, _t)->start;                                            \
391 })
392
393 #define btree_bkey_last(_b, _t)                                         \
394 ({                                                                      \
395         EBUG_ON(__btree_node_offset_to_key(_b, (_t)->end_offset) !=     \
396                 vstruct_last(bset(_b, _t)));                            \
397                                                                         \
398         __btree_node_offset_to_key(_b, (_t)->end_offset);               \
399 })
400
401 static inline unsigned bset_byte_offset(struct btree *b, void *i)
402 {
403         return i - (void *) b->data;
404 }
405
406 /* Type of keys @b contains: */
407 static inline enum bkey_type btree_node_type(struct btree *b)
408 {
409         return b->level ? BKEY_TYPE_BTREE : b->btree_id;
410 }
411
412 static inline const struct bkey_ops *btree_node_ops(struct btree *b)
413 {
414         return &bch2_bkey_ops[btree_node_type(b)];
415 }
416
417 static inline bool btree_node_is_extents(struct btree *b)
418 {
419         return btree_node_type(b) == BKEY_TYPE_EXTENTS;
420 }
421
422 struct btree_root {
423         struct btree            *b;
424
425         struct btree_update     *as;
426
427         /* On disk root - see async splits: */
428         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
429         u8                      level;
430         u8                      alive;
431 };
432
433 /*
434  * Optional hook that will be called just prior to a btree node update, when
435  * we're holding the write lock and we know what key is about to be overwritten:
436  */
437
438 enum btree_insert_ret {
439         BTREE_INSERT_OK,
440         /* extent spanned multiple leaf nodes: have to traverse to next node: */
441         BTREE_INSERT_NEED_TRAVERSE,
442         /* write lock held for too long */
443         /* leaf node needs to be split */
444         BTREE_INSERT_BTREE_NODE_FULL,
445         BTREE_INSERT_ENOSPC,
446         BTREE_INSERT_NEED_GC_LOCK,
447 };
448
449 enum btree_gc_coalesce_fail_reason {
450         BTREE_GC_COALESCE_FAIL_RESERVE_GET,
451         BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC,
452         BTREE_GC_COALESCE_FAIL_FORMAT_FITS,
453 };
454
455 enum btree_node_sibling {
456         btree_prev_sib,
457         btree_next_sib,
458 };
459
460 typedef struct btree_nr_keys (*sort_fix_overlapping_fn)(struct bset *,
461                                                         struct btree *,
462                                                         struct btree_node_iter *);
463
464 #endif /* _BCACHEFS_BTREE_TYPES_H */