]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_update_interior.h
7ed67b47e1b9377bf960c15de653074e3a786c74
[bcachefs-tools-debian] / libbcachefs / btree_update_interior.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_UPDATE_INTERIOR_H
3 #define _BCACHEFS_BTREE_UPDATE_INTERIOR_H
4
5 #include "btree_cache.h"
6 #include "btree_locking.h"
7 #include "btree_update.h"
8
9 void __bch2_btree_calc_format(struct bkey_format_state *, struct btree *);
10 bool bch2_btree_node_format_fits(struct bch_fs *c, struct btree *,
11                                 struct bkey_format *);
12
13 #define BTREE_UPDATE_NODES_MAX          ((BTREE_MAX_DEPTH - 2) * 2 + GC_MERGE_NODES)
14
15 #define BTREE_UPDATE_JOURNAL_RES        (BTREE_UPDATE_NODES_MAX * (BKEY_BTREE_PTR_U64s_MAX + 1))
16
17 /*
18  * Tracks an in progress split/rewrite of a btree node and the update to the
19  * parent node:
20  *
21  * When we split/rewrite a node, we do all the updates in memory without
22  * waiting for any writes to complete - we allocate the new node(s) and update
23  * the parent node, possibly recursively up to the root.
24  *
25  * The end result is that we have one or more new nodes being written -
26  * possibly several, if there were multiple splits - and then a write (updating
27  * an interior node) which will make all these new nodes visible.
28  *
29  * Additionally, as we split/rewrite nodes we free the old nodes - but the old
30  * nodes can't be freed (their space on disk can't be reclaimed) until the
31  * update to the interior node that makes the new node visible completes -
32  * until then, the old nodes are still reachable on disk.
33  *
34  */
35 struct btree_update {
36         struct closure                  cl;
37         struct bch_fs                   *c;
38
39         struct list_head                list;
40         struct list_head                unwritten_list;
41
42         /* What kind of update are we doing? */
43         enum {
44                 BTREE_INTERIOR_NO_UPDATE,
45                 BTREE_INTERIOR_UPDATING_NODE,
46                 BTREE_INTERIOR_UPDATING_ROOT,
47                 BTREE_INTERIOR_UPDATING_AS,
48         } mode;
49
50         unsigned                        nodes_written:1;
51         unsigned                        took_gc_lock:1;
52
53         enum btree_id                   btree_id;
54
55         struct disk_reservation         disk_res;
56         struct journal_preres           journal_preres;
57
58         /*
59          * BTREE_INTERIOR_UPDATING_NODE:
60          * The update that made the new nodes visible was a regular update to an
61          * existing interior node - @b. We can't write out the update to @b
62          * until the new nodes we created are finished writing, so we block @b
63          * from writing by putting this btree_interior update on the
64          * @b->write_blocked list with @write_blocked_list:
65          */
66         struct btree                    *b;
67         struct list_head                write_blocked_list;
68
69         /*
70          * We may be freeing nodes that were dirty, and thus had journal entries
71          * pinned: we need to transfer the oldest of those pins to the
72          * btree_update operation, and release it when the new node(s)
73          * are all persistent and reachable:
74          */
75         struct journal_entry_pin        journal;
76
77         /* Preallocated nodes we reserve when we start the update: */
78         struct btree                    *prealloc_nodes[BTREE_UPDATE_NODES_MAX];
79         unsigned                        nr_prealloc_nodes;
80
81         /* Nodes being freed: */
82         struct keylist                  old_keys;
83         u64                             _old_keys[BTREE_UPDATE_NODES_MAX *
84                                                   BKEY_BTREE_PTR_VAL_U64s_MAX];
85
86         /* Nodes being added: */
87         struct keylist                  new_keys;
88         u64                             _new_keys[BTREE_UPDATE_NODES_MAX *
89                                                   BKEY_BTREE_PTR_VAL_U64s_MAX];
90
91         /* New nodes, that will be made reachable by this update: */
92         struct btree                    *new_nodes[BTREE_UPDATE_NODES_MAX];
93         unsigned                        nr_new_nodes;
94
95         struct btree                    *old_nodes[BTREE_UPDATE_NODES_MAX];
96         __le64                          old_nodes_seq[BTREE_UPDATE_NODES_MAX];
97         unsigned                        nr_old_nodes;
98
99         open_bucket_idx_t               open_buckets[BTREE_UPDATE_NODES_MAX *
100                                                      BCH_REPLICAS_MAX];
101         open_bucket_idx_t               nr_open_buckets;
102
103         unsigned                        journal_u64s;
104         u64                             journal_entries[BTREE_UPDATE_JOURNAL_RES];
105
106         /* Only here to reduce stack usage on recursive splits: */
107         struct keylist                  parent_keys;
108         /*
109          * Enough room for btree_split's keys without realloc - btree node
110          * pointers never have crc/compression info, so we only need to acount
111          * for the pointers for three keys
112          */
113         u64                             inline_keys[BKEY_BTREE_PTR_U64s_MAX * 3];
114 };
115
116 void bch2_btree_node_free_inmem(struct bch_fs *, struct btree *,
117                                 struct btree_iter *);
118 void bch2_btree_node_free_never_inserted(struct bch_fs *, struct btree *);
119
120 void bch2_btree_update_get_open_buckets(struct btree_update *, struct btree *);
121
122 struct btree *__bch2_btree_node_alloc_replacement(struct btree_update *,
123                                                   struct btree *,
124                                                   struct bkey_format);
125
126 void bch2_btree_update_done(struct btree_update *);
127 struct btree_update *
128 bch2_btree_update_start(struct btree_iter *, unsigned, unsigned, unsigned);
129
130 void bch2_btree_interior_update_will_free_node(struct btree_update *,
131                                                struct btree *);
132 void bch2_btree_update_add_new_node(struct btree_update *, struct btree *);
133
134 void bch2_btree_insert_node(struct btree_update *, struct btree *,
135                             struct btree_iter *, struct keylist *,
136                             unsigned);
137 int bch2_btree_split_leaf(struct bch_fs *, struct btree_iter *, unsigned);
138
139 int __bch2_foreground_maybe_merge(struct bch_fs *, struct btree_iter *,
140                                   unsigned, unsigned, enum btree_node_sibling);
141
142 static inline int bch2_foreground_maybe_merge_sibling(struct bch_fs *c,
143                                         struct btree_iter *iter,
144                                         unsigned level, unsigned flags,
145                                         enum btree_node_sibling sib)
146 {
147         struct btree *b;
148
149         if (iter->uptodate >= BTREE_ITER_NEED_TRAVERSE)
150                 return 0;
151
152         if (!bch2_btree_node_relock(iter, level))
153                 return 0;
154
155         b = iter->l[level].b;
156         if (b->sib_u64s[sib] > c->btree_foreground_merge_threshold)
157                 return 0;
158
159         return __bch2_foreground_maybe_merge(c, iter, level, flags, sib);
160 }
161
162 static inline int bch2_foreground_maybe_merge(struct bch_fs *c,
163                                                struct btree_iter *iter,
164                                                unsigned level,
165                                                unsigned flags)
166 {
167         return  bch2_foreground_maybe_merge_sibling(c, iter, level, flags,
168                                                     btree_prev_sib) ?:
169                 bch2_foreground_maybe_merge_sibling(c, iter, level, flags,
170                                                     btree_next_sib);
171 }
172
173 void bch2_btree_set_root_for_read(struct bch_fs *, struct btree *);
174 void bch2_btree_root_alloc(struct bch_fs *, enum btree_id);
175
176 static inline unsigned btree_update_reserve_required(struct bch_fs *c,
177                                                      struct btree *b)
178 {
179         unsigned depth = btree_node_root(c, b)->c.level + 1;
180
181         /*
182          * Number of nodes we might have to allocate in a worst case btree
183          * split operation - we split all the way up to the root, then allocate
184          * a new root, unless we're already at max depth:
185          */
186         if (depth < BTREE_MAX_DEPTH)
187                 return (depth - b->c.level) * 2 + 1;
188         else
189                 return (depth - b->c.level) * 2 - 1;
190 }
191
192 static inline void btree_node_reset_sib_u64s(struct btree *b)
193 {
194         b->sib_u64s[0] = b->nr.live_u64s;
195         b->sib_u64s[1] = b->nr.live_u64s;
196 }
197
198 static inline void *btree_data_end(struct bch_fs *c, struct btree *b)
199 {
200         return (void *) b->data + btree_bytes(c);
201 }
202
203 static inline struct bkey_packed *unwritten_whiteouts_start(struct bch_fs *c,
204                                                             struct btree *b)
205 {
206         return (void *) ((u64 *) btree_data_end(c, b) - b->whiteout_u64s);
207 }
208
209 static inline struct bkey_packed *unwritten_whiteouts_end(struct bch_fs *c,
210                                                           struct btree *b)
211 {
212         return btree_data_end(c, b);
213 }
214
215 static inline void *write_block(struct btree *b)
216 {
217         return (void *) b->data + (b->written << 9);
218 }
219
220 static inline bool __btree_addr_written(struct btree *b, void *p)
221 {
222         return p < write_block(b);
223 }
224
225 static inline bool bset_written(struct btree *b, struct bset *i)
226 {
227         return __btree_addr_written(b, i);
228 }
229
230 static inline bool bkey_written(struct btree *b, struct bkey_packed *k)
231 {
232         return __btree_addr_written(b, k);
233 }
234
235 static inline ssize_t __bch_btree_u64s_remaining(struct bch_fs *c,
236                                                  struct btree *b,
237                                                  void *end)
238 {
239         ssize_t used = bset_byte_offset(b, end) / sizeof(u64) +
240                 b->whiteout_u64s;
241         ssize_t total = c->opts.btree_node_size << 6;
242
243         /* Always leave one extra u64 for bch2_varint_decode: */
244         used++;
245
246         return total - used;
247 }
248
249 static inline size_t bch_btree_keys_u64s_remaining(struct bch_fs *c,
250                                                    struct btree *b)
251 {
252         ssize_t remaining = __bch_btree_u64s_remaining(c, b,
253                                 btree_bkey_last(b, bset_tree_last(b)));
254
255         BUG_ON(remaining < 0);
256
257         if (bset_written(b, btree_bset_last(b)))
258                 return 0;
259
260         return remaining;
261 }
262
263 #define BTREE_WRITE_SET_U64s_BITS       9
264
265 static inline unsigned btree_write_set_buffer(struct btree *b)
266 {
267         /*
268          * Could buffer up larger amounts of keys for btrees with larger keys,
269          * pending benchmarking:
270          */
271         return 8 << BTREE_WRITE_SET_U64s_BITS;
272 }
273
274 static inline struct btree_node_entry *want_new_bset(struct bch_fs *c,
275                                                      struct btree *b)
276 {
277         struct bset_tree *t = bset_tree_last(b);
278         struct btree_node_entry *bne = max(write_block(b),
279                         (void *) btree_bkey_last(b, bset_tree_last(b)));
280         ssize_t remaining_space =
281                 __bch_btree_u64s_remaining(c, b, &bne->keys.start[0]);
282
283         if (unlikely(bset_written(b, bset(b, t)))) {
284                 if (remaining_space > (ssize_t) (block_bytes(c) >> 3))
285                         return bne;
286         } else {
287                 if (unlikely(bset_u64s(t) * sizeof(u64) > btree_write_set_buffer(b)) &&
288                     remaining_space > (ssize_t) (btree_write_set_buffer(b) >> 3))
289                         return bne;
290         }
291
292         return NULL;
293 }
294
295 static inline void push_whiteout(struct bch_fs *c, struct btree *b,
296                                  struct bpos pos)
297 {
298         struct bkey_packed k;
299
300         BUG_ON(bch_btree_keys_u64s_remaining(c, b) < BKEY_U64s);
301
302         if (!bkey_pack_pos(&k, pos, b)) {
303                 struct bkey *u = (void *) &k;
304
305                 bkey_init(u);
306                 u->p = pos;
307         }
308
309         k.needs_whiteout = true;
310
311         b->whiteout_u64s += k.u64s;
312         bkey_copy(unwritten_whiteouts_start(c, b), &k);
313 }
314
315 /*
316  * write lock must be held on @b (else the dirty bset that we were going to
317  * insert into could be written out from under us)
318  */
319 static inline bool bch2_btree_node_insert_fits(struct bch_fs *c,
320                                                struct btree *b, unsigned u64s)
321 {
322         if (unlikely(btree_node_need_rewrite(b)))
323                 return false;
324
325         return u64s <= bch_btree_keys_u64s_remaining(c, b);
326 }
327
328 void bch2_btree_updates_to_text(struct printbuf *, struct bch_fs *);
329
330 size_t bch2_btree_interior_updates_nr_pending(struct bch_fs *);
331
332 void bch2_journal_entries_to_btree_roots(struct bch_fs *, struct jset *);
333 struct jset_entry *bch2_btree_roots_to_journal_entries(struct bch_fs *,
334                                         struct jset_entry *, struct jset_entry *);
335
336 void bch2_fs_btree_interior_update_exit(struct bch_fs *);
337 int bch2_fs_btree_interior_update_init(struct bch_fs *);
338
339 #endif /* _BCACHEFS_BTREE_UPDATE_INTERIOR_H */