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