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