]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_write_buffer.c
Update bcachefs sources to 8c94740b1bf8 bcachefs: Add missing vaidation for jset_entr...
[bcachefs-tools-debian] / libbcachefs / btree_write_buffer.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_locking.h"
5 #include "btree_update.h"
6 #include "btree_update_interior.h"
7 #include "btree_write_buffer.h"
8 #include "error.h"
9 #include "journal.h"
10 #include "journal_io.h"
11 #include "journal_reclaim.h"
12
13 #include <linux/prefetch.h>
14
15 static int bch2_btree_write_buffer_journal_flush(struct journal *,
16                                 struct journal_entry_pin *, u64);
17
18 static int bch2_journal_keys_to_write_buffer(struct bch_fs *, struct journal_buf *);
19
20 static inline bool __wb_key_cmp(const struct wb_key_ref *l, const struct wb_key_ref *r)
21 {
22         return (cmp_int(l->hi, r->hi) ?:
23                 cmp_int(l->mi, r->mi) ?:
24                 cmp_int(l->lo, r->lo)) >= 0;
25 }
26
27 static inline bool wb_key_cmp(const struct wb_key_ref *l, const struct wb_key_ref *r)
28 {
29 #ifdef CONFIG_X86_64
30         int cmp;
31
32         asm("mov   (%[l]), %%rax;"
33             "sub   (%[r]), %%rax;"
34             "mov  8(%[l]), %%rax;"
35             "sbb  8(%[r]), %%rax;"
36             "mov 16(%[l]), %%rax;"
37             "sbb 16(%[r]), %%rax;"
38             : "=@ccae" (cmp)
39             : [l] "r" (l), [r] "r" (r)
40             : "rax", "cc");
41
42         EBUG_ON(cmp != __wb_key_cmp(l, r));
43         return cmp;
44 #else
45         return __wb_key_cmp(l, r);
46 #endif
47 }
48
49 /* Compare excluding idx, the low 24 bits: */
50 static inline bool wb_key_eq(const void *_l, const void *_r)
51 {
52         const struct wb_key_ref *l = _l;
53         const struct wb_key_ref *r = _r;
54
55         return !((l->hi ^ r->hi)|
56                  (l->mi ^ r->mi)|
57                  ((l->lo >> 24) ^ (r->lo >> 24)));
58 }
59
60 static noinline void wb_sort(struct wb_key_ref *base, size_t num)
61 {
62         size_t n = num, a = num / 2;
63
64         if (!a)         /* num < 2 || size == 0 */
65                 return;
66
67         for (;;) {
68                 size_t b, c, d;
69
70                 if (a)                  /* Building heap: sift down --a */
71                         --a;
72                 else if (--n)           /* Sorting: Extract root to --n */
73                         swap(base[0], base[n]);
74                 else                    /* Sort complete */
75                         break;
76
77                 /*
78                  * Sift element at "a" down into heap.  This is the
79                  * "bottom-up" variant, which significantly reduces
80                  * calls to cmp_func(): we find the sift-down path all
81                  * the way to the leaves (one compare per level), then
82                  * backtrack to find where to insert the target element.
83                  *
84                  * Because elements tend to sift down close to the leaves,
85                  * this uses fewer compares than doing two per level
86                  * on the way down.  (A bit more than half as many on
87                  * average, 3/4 worst-case.)
88                  */
89                 for (b = a; c = 2*b + 1, (d = c + 1) < n;)
90                         b = wb_key_cmp(base + c, base + d) ? c : d;
91                 if (d == n)             /* Special case last leaf with no sibling */
92                         b = c;
93
94                 /* Now backtrack from "b" to the correct location for "a" */
95                 while (b != a && wb_key_cmp(base + a, base + b))
96                         b = (b - 1) / 2;
97                 c = b;                  /* Where "a" belongs */
98                 while (b != a) {        /* Shift it into place */
99                         b = (b - 1) / 2;
100                         swap(base[b], base[c]);
101                 }
102         }
103 }
104
105 static noinline int wb_flush_one_slowpath(struct btree_trans *trans,
106                                           struct btree_iter *iter,
107                                           struct btree_write_buffered_key *wb)
108 {
109         bch2_btree_node_unlock_write(trans, iter->path, iter->path->l[0].b);
110
111         trans->journal_res.seq = wb->journal_seq;
112
113         return bch2_trans_update(trans, iter, &wb->k,
114                                  BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
115                 bch2_trans_commit(trans, NULL, NULL,
116                                   BCH_TRANS_COMMIT_no_enospc|
117                                   BCH_TRANS_COMMIT_no_check_rw|
118                                   BCH_TRANS_COMMIT_no_journal_res|
119                                   BCH_TRANS_COMMIT_journal_reclaim);
120 }
121
122 static inline int wb_flush_one(struct btree_trans *trans, struct btree_iter *iter,
123                                struct btree_write_buffered_key *wb,
124                                bool *write_locked, size_t *fast)
125 {
126         struct bch_fs *c = trans->c;
127         struct btree_path *path;
128         int ret;
129
130         EBUG_ON(!wb->journal_seq);
131         EBUG_ON(!c->btree_write_buffer.flushing.pin.seq);
132         EBUG_ON(c->btree_write_buffer.flushing.pin.seq > wb->journal_seq);
133
134         ret = bch2_btree_iter_traverse(iter);
135         if (ret)
136                 return ret;
137
138         /*
139          * We can't clone a path that has write locks: unshare it now, before
140          * set_pos and traverse():
141          */
142         if (iter->path->ref > 1)
143                 iter->path = __bch2_btree_path_make_mut(trans, iter->path, true, _THIS_IP_);
144
145         path = iter->path;
146
147         if (!*write_locked) {
148                 ret = bch2_btree_node_lock_write(trans, path, &path->l[0].b->c);
149                 if (ret)
150                         return ret;
151
152                 bch2_btree_node_prep_for_write(trans, path, path->l[0].b);
153                 *write_locked = true;
154         }
155
156         if (unlikely(!bch2_btree_node_insert_fits(c, path->l[0].b, wb->k.k.u64s))) {
157                 *write_locked = false;
158                 return wb_flush_one_slowpath(trans, iter, wb);
159         }
160
161         bch2_btree_insert_key_leaf(trans, path, &wb->k, wb->journal_seq);
162         (*fast)++;
163         return 0;
164 }
165
166 /*
167  * Update a btree with a write buffered key using the journal seq of the
168  * original write buffer insert.
169  *
170  * It is not safe to rejournal the key once it has been inserted into the write
171  * buffer because that may break recovery ordering. For example, the key may
172  * have already been modified in the active write buffer in a seq that comes
173  * before the current transaction. If we were to journal this key again and
174  * crash, recovery would process updates in the wrong order.
175  */
176 static int
177 btree_write_buffered_insert(struct btree_trans *trans,
178                           struct btree_write_buffered_key *wb)
179 {
180         struct btree_iter iter;
181         int ret;
182
183         bch2_trans_iter_init(trans, &iter, wb->btree, bkey_start_pos(&wb->k.k),
184                              BTREE_ITER_CACHED|BTREE_ITER_INTENT);
185
186         trans->journal_res.seq = wb->journal_seq;
187
188         ret   = bch2_btree_iter_traverse(&iter) ?:
189                 bch2_trans_update(trans, &iter, &wb->k,
190                                   BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
191         bch2_trans_iter_exit(trans, &iter);
192         return ret;
193 }
194
195 static void move_keys_from_inc_to_flushing(struct btree_write_buffer *wb)
196 {
197         struct bch_fs *c = container_of(wb, struct bch_fs, btree_write_buffer);
198         struct journal *j = &c->journal;
199
200         if (!wb->inc.keys.nr)
201                 return;
202
203         bch2_journal_pin_add(j, wb->inc.keys.data[0].journal_seq, &wb->flushing.pin,
204                              bch2_btree_write_buffer_journal_flush);
205
206         darray_resize(&wb->flushing.keys, min_t(size_t, 1U << 20, wb->flushing.keys.nr + wb->inc.keys.nr));
207         darray_resize(&wb->sorted, wb->flushing.keys.size);
208
209         if (!wb->flushing.keys.nr && wb->sorted.size >= wb->inc.keys.nr) {
210                 swap(wb->flushing.keys, wb->inc.keys);
211                 goto out;
212         }
213
214         size_t nr = min(darray_room(wb->flushing.keys),
215                         wb->sorted.size - wb->flushing.keys.nr);
216         nr = min(nr, wb->inc.keys.nr);
217
218         memcpy(&darray_top(wb->flushing.keys),
219                wb->inc.keys.data,
220                sizeof(wb->inc.keys.data[0]) * nr);
221
222         memmove(wb->inc.keys.data,
223                 wb->inc.keys.data + nr,
224                sizeof(wb->inc.keys.data[0]) * (wb->inc.keys.nr - nr));
225
226         wb->flushing.keys.nr    += nr;
227         wb->inc.keys.nr         -= nr;
228 out:
229         if (!wb->inc.keys.nr)
230                 bch2_journal_pin_drop(j, &wb->inc.pin);
231         else
232                 bch2_journal_pin_update(j, wb->inc.keys.data[0].journal_seq, &wb->inc.pin,
233                                         bch2_btree_write_buffer_journal_flush);
234
235         if (j->watermark) {
236                 spin_lock(&j->lock);
237                 bch2_journal_set_watermark(j);
238                 spin_unlock(&j->lock);
239         }
240
241         BUG_ON(wb->sorted.size < wb->flushing.keys.nr);
242 }
243
244 static int bch2_btree_write_buffer_flush_locked(struct btree_trans *trans)
245 {
246         struct bch_fs *c = trans->c;
247         struct journal *j = &c->journal;
248         struct btree_write_buffer *wb = &c->btree_write_buffer;
249         struct wb_key_ref *i;
250         struct btree_iter iter = { NULL };
251         size_t skipped = 0, fast = 0, slowpath = 0;
252         bool write_locked = false;
253         int ret = 0;
254
255         bch2_trans_unlock(trans);
256         bch2_trans_begin(trans);
257
258         mutex_lock(&wb->inc.lock);
259         move_keys_from_inc_to_flushing(wb);
260         mutex_unlock(&wb->inc.lock);
261
262         for (size_t i = 0; i < wb->flushing.keys.nr; i++) {
263                 wb->sorted.data[i].idx = i;
264                 wb->sorted.data[i].btree = wb->flushing.keys.data[i].btree;
265                 memcpy(&wb->sorted.data[i].pos, &wb->flushing.keys.data[i].k.k.p, sizeof(struct bpos));
266         }
267         wb->sorted.nr = wb->flushing.keys.nr;
268
269         /*
270          * We first sort so that we can detect and skip redundant updates, and
271          * then we attempt to flush in sorted btree order, as this is most
272          * efficient.
273          *
274          * However, since we're not flushing in the order they appear in the
275          * journal we won't be able to drop our journal pin until everything is
276          * flushed - which means this could deadlock the journal if we weren't
277          * passing BCH_TRANS_COMMIT_journal_reclaim. This causes the update to fail
278          * if it would block taking a journal reservation.
279          *
280          * If that happens, simply skip the key so we can optimistically insert
281          * as many keys as possible in the fast path.
282          */
283         wb_sort(wb->sorted.data, wb->sorted.nr);
284
285         darray_for_each(wb->sorted, i) {
286                 struct btree_write_buffered_key *k = &wb->flushing.keys.data[i->idx];
287
288                 for (struct wb_key_ref *n = i + 1; n < min(i + 4, &darray_top(wb->sorted)); n++)
289                         prefetch(&wb->flushing.keys.data[n->idx]);
290
291                 BUG_ON(!k->journal_seq);
292
293                 if (i + 1 < &darray_top(wb->sorted) &&
294                     wb_key_eq(i, i + 1)) {
295                         struct btree_write_buffered_key *n = &wb->flushing.keys.data[i[1].idx];
296
297                         skipped++;
298                         n->journal_seq = min_t(u64, n->journal_seq, k->journal_seq);
299                         k->journal_seq = 0;
300                         continue;
301                 }
302
303                 if (write_locked &&
304                     (iter.path->btree_id != k->btree ||
305                      bpos_gt(k->k.k.p, iter.path->l[0].b->key.k.p))) {
306                         bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
307                         write_locked = false;
308                 }
309
310                 if (!iter.path || iter.path->btree_id != k->btree) {
311                         bch2_trans_iter_exit(trans, &iter);
312                         bch2_trans_iter_init(trans, &iter, k->btree, k->k.k.p,
313                                              BTREE_ITER_INTENT|BTREE_ITER_ALL_SNAPSHOTS);
314                 }
315
316                 bch2_btree_iter_set_pos(&iter, k->k.k.p);
317                 iter.path->preserve = false;
318
319                 do {
320                         if (race_fault()) {
321                                 ret = -BCH_ERR_journal_reclaim_would_deadlock;
322                                 break;
323                         }
324
325                         ret = wb_flush_one(trans, &iter, k, &write_locked, &fast);
326                         if (!write_locked)
327                                 bch2_trans_begin(trans);
328                 } while (bch2_err_matches(ret, BCH_ERR_transaction_restart));
329
330                 if (!ret) {
331                         k->journal_seq = 0;
332                 } else if (ret == -BCH_ERR_journal_reclaim_would_deadlock) {
333                         slowpath++;
334                         ret = 0;
335                 } else
336                         break;
337         }
338
339         if (write_locked)
340                 bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
341         bch2_trans_iter_exit(trans, &iter);
342
343         if (ret)
344                 goto err;
345
346         if (slowpath) {
347                 /*
348                  * Flush in the order they were present in the journal, so that
349                  * we can release journal pins:
350                  * The fastpath zapped the seq of keys that were successfully flushed so
351                  * we can skip those here.
352                  */
353                 trace_write_buffer_flush_slowpath(trans, slowpath, wb->flushing.keys.nr);
354
355                 struct btree_write_buffered_key *i;
356                 darray_for_each(wb->flushing.keys, i) {
357                         if (!i->journal_seq)
358                                 continue;
359
360                         bch2_journal_pin_update(j, i->journal_seq, &wb->flushing.pin,
361                                                 bch2_btree_write_buffer_journal_flush);
362
363                         bch2_trans_begin(trans);
364
365                         ret = commit_do(trans, NULL, NULL,
366                                         BCH_WATERMARK_reclaim|
367                                         BCH_TRANS_COMMIT_no_check_rw|
368                                         BCH_TRANS_COMMIT_no_enospc|
369                                         BCH_TRANS_COMMIT_no_journal_res|
370                                         BCH_TRANS_COMMIT_journal_reclaim,
371                                         btree_write_buffered_insert(trans, i));
372                         if (ret)
373                                 goto err;
374                 }
375         }
376 err:
377         bch2_fs_fatal_err_on(ret, c, "%s: insert error %s", __func__, bch2_err_str(ret));
378         trace_write_buffer_flush(trans, wb->flushing.keys.nr, skipped, fast, 0);
379         bch2_journal_pin_drop(j, &wb->flushing.pin);
380         wb->flushing.keys.nr = 0;
381         return ret;
382 }
383
384 static int fetch_wb_keys_from_journal(struct bch_fs *c, u64 seq)
385 {
386         struct journal *j = &c->journal;
387         struct journal_buf *buf;
388         int ret = 0;
389
390         mutex_lock(&j->buf_lock);
391         while ((buf = bch2_next_write_buffer_flush_journal_buf(j, seq)))
392                 if (bch2_journal_keys_to_write_buffer(c, buf)) {
393                         ret = -ENOMEM;
394                         break;
395                 }
396         mutex_unlock(&j->buf_lock);
397
398         return ret;
399 }
400
401 int bch2_btree_write_buffer_flush_sync(struct btree_trans *trans)
402 {
403         struct bch_fs *c = trans->c;
404         struct btree_write_buffer *wb = &c->btree_write_buffer;
405         int ret = 0, fetch_from_journal_err;
406
407         trace_write_buffer_flush_sync(trans, _RET_IP_);
408 retry:
409         bch2_trans_unlock(trans);
410
411         bch2_journal_block_reservations(&c->journal);
412         fetch_from_journal_err = fetch_wb_keys_from_journal(c, U64_MAX);
413         bch2_journal_unblock(&c->journal);
414
415         /*
416          * On memory allocation failure, bch2_btree_write_buffer_flush_locked()
417          * is not guaranteed to empty wb->inc:
418          */
419         mutex_lock(&wb->flushing.lock);
420         while (!ret &&
421                (wb->flushing.keys.nr || wb->inc.keys.nr))
422                 ret = bch2_btree_write_buffer_flush_locked(trans);
423         mutex_unlock(&wb->flushing.lock);
424
425         if (!ret && fetch_from_journal_err)
426                 goto retry;
427
428         return ret;
429 }
430
431 int bch2_btree_write_buffer_flush_nocheck_rw(struct btree_trans *trans)
432 {
433         struct bch_fs *c = trans->c;
434         struct btree_write_buffer *wb = &c->btree_write_buffer;
435         int ret = 0;
436
437         if (mutex_trylock(&wb->flushing.lock)) {
438                 ret = bch2_btree_write_buffer_flush_locked(trans);
439                 mutex_unlock(&wb->flushing.lock);
440         }
441
442         return ret;
443 }
444
445 int bch2_btree_write_buffer_tryflush(struct btree_trans *trans)
446 {
447         struct bch_fs *c = trans->c;
448
449         if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_btree_write_buffer))
450                 return -BCH_ERR_erofs_no_writes;
451
452         int ret = bch2_btree_write_buffer_flush_nocheck_rw(trans);
453         bch2_write_ref_put(c, BCH_WRITE_REF_btree_write_buffer);
454         return ret;
455 }
456
457 static int bch2_btree_write_buffer_journal_flush(struct journal *j,
458                                 struct journal_entry_pin *_pin, u64 seq)
459 {
460         struct bch_fs *c = container_of(j, struct bch_fs, journal);
461         struct btree_write_buffer *wb = &c->btree_write_buffer;
462         int ret, fetch_from_journal_err;
463
464         do {
465                 fetch_from_journal_err = fetch_wb_keys_from_journal(c, seq);
466
467                 mutex_lock(&wb->flushing.lock);
468                 ret = bch2_trans_run(c, bch2_btree_write_buffer_flush_locked(trans));
469                 mutex_unlock(&wb->flushing.lock);
470         } while (!ret &&
471                  (fetch_from_journal_err ||
472                   (wb->flushing.pin.seq && wb->flushing.pin.seq <= seq) ||
473                   (wb->inc.pin.seq && wb->inc.pin.seq <= seq)));
474
475         return ret;
476 }
477
478 static void bch2_btree_write_buffer_flush_work(struct work_struct *work)
479 {
480         struct bch_fs *c = container_of(work, struct bch_fs, btree_write_buffer.flush_work);
481         struct btree_write_buffer *wb = &c->btree_write_buffer;
482         int ret;
483
484         mutex_lock(&wb->flushing.lock);
485         do {
486                 ret = bch2_trans_run(c, bch2_btree_write_buffer_flush_locked(trans));
487         } while (!ret && bch2_btree_write_buffer_should_flush(c));
488         mutex_unlock(&wb->flushing.lock);
489
490         bch2_write_ref_put(c, BCH_WRITE_REF_btree_write_buffer);
491 }
492
493 int __bch2_journal_key_to_wb(struct bch_fs *c,
494                              struct journal_keys_to_wb *dst,
495                              enum btree_id btree, struct bkey_i *k)
496 {
497         struct btree_write_buffer *wb = &c->btree_write_buffer;
498         int ret;
499 retry:
500         ret = darray_make_room_gfp(&dst->wb->keys, 1, GFP_KERNEL);
501         if (!ret && dst->wb == &wb->flushing)
502                 ret = darray_resize(&wb->sorted, wb->flushing.keys.size);
503
504         if (unlikely(ret)) {
505                 if (dst->wb == &c->btree_write_buffer.flushing) {
506                         mutex_unlock(&dst->wb->lock);
507                         dst->wb = &c->btree_write_buffer.inc;
508                         bch2_journal_pin_add(&c->journal, dst->seq, &dst->wb->pin,
509                                              bch2_btree_write_buffer_journal_flush);
510                         goto retry;
511                 }
512
513                 return ret;
514         }
515
516         dst->room = darray_room(dst->wb->keys);
517         if (dst->wb == &wb->flushing)
518                 dst->room = min(dst->room, wb->sorted.size - wb->flushing.keys.nr);
519         BUG_ON(!dst->room);
520         BUG_ON(!dst->seq);
521
522         struct btree_write_buffered_key *wb_k = &darray_top(dst->wb->keys);
523         wb_k->journal_seq       = dst->seq;
524         wb_k->btree             = btree;
525         bkey_copy(&wb_k->k, k);
526         dst->wb->keys.nr++;
527         dst->room--;
528         return 0;
529 }
530
531 void bch2_journal_keys_to_write_buffer_start(struct bch_fs *c, struct journal_keys_to_wb *dst, u64 seq)
532 {
533         struct btree_write_buffer *wb = &c->btree_write_buffer;
534
535         if (mutex_trylock(&wb->flushing.lock)) {
536                 mutex_lock(&wb->inc.lock);
537                 move_keys_from_inc_to_flushing(wb);
538
539                 /*
540                  * Attempt to skip wb->inc, and add keys directly to
541                  * wb->flushing, saving us a copy later:
542                  */
543
544                 if (!wb->inc.keys.nr) {
545                         dst->wb = &wb->flushing;
546                 } else {
547                         mutex_unlock(&wb->flushing.lock);
548                         dst->wb = &wb->inc;
549                 }
550         } else {
551                 mutex_lock(&wb->inc.lock);
552                 dst->wb = &wb->inc;
553         }
554
555         dst->room = darray_room(dst->wb->keys);
556         if (dst->wb == &wb->flushing)
557                 dst->room = min(dst->room, wb->sorted.size - wb->flushing.keys.nr);
558         dst->seq = seq;
559
560         bch2_journal_pin_add(&c->journal, seq, &dst->wb->pin,
561                              bch2_btree_write_buffer_journal_flush);
562 }
563
564 void bch2_journal_keys_to_write_buffer_end(struct bch_fs *c, struct journal_keys_to_wb *dst)
565 {
566         struct btree_write_buffer *wb = &c->btree_write_buffer;
567
568         if (!dst->wb->keys.nr)
569                 bch2_journal_pin_drop(&c->journal, &dst->wb->pin);
570
571         if (bch2_btree_write_buffer_should_flush(c) &&
572             __bch2_write_ref_tryget(c, BCH_WRITE_REF_btree_write_buffer) &&
573             !queue_work(system_unbound_wq, &c->btree_write_buffer.flush_work))
574                 bch2_write_ref_put(c, BCH_WRITE_REF_btree_write_buffer);
575
576         if (dst->wb == &wb->flushing)
577                 mutex_unlock(&wb->flushing.lock);
578         mutex_unlock(&wb->inc.lock);
579 }
580
581 static int bch2_journal_keys_to_write_buffer(struct bch_fs *c, struct journal_buf *buf)
582 {
583         struct journal_keys_to_wb dst;
584         struct jset_entry *entry;
585         struct bkey_i *k;
586         int ret = 0;
587
588         bch2_journal_keys_to_write_buffer_start(c, &dst, le64_to_cpu(buf->data->seq));
589
590         for_each_jset_entry_type(entry, buf->data, BCH_JSET_ENTRY_write_buffer_keys) {
591                 jset_entry_for_each_key(entry, k) {
592                         ret = bch2_journal_key_to_wb(c, &dst, entry->btree_id, k);
593                         if (ret)
594                                 goto out;
595                 }
596
597                 entry->type = BCH_JSET_ENTRY_btree_keys;
598         }
599
600         buf->need_flush_to_write_buffer = false;
601 out:
602         bch2_journal_keys_to_write_buffer_end(c, &dst);
603         return ret;
604 }
605
606 static int wb_keys_resize(struct btree_write_buffer_keys *wb, size_t new_size)
607 {
608         if (wb->keys.size >= new_size)
609                 return 0;
610
611         if (!mutex_trylock(&wb->lock))
612                 return -EINTR;
613
614         int ret = darray_resize(&wb->keys, new_size);
615         mutex_unlock(&wb->lock);
616         return ret;
617 }
618
619 int bch2_btree_write_buffer_resize(struct bch_fs *c, size_t new_size)
620 {
621         struct btree_write_buffer *wb = &c->btree_write_buffer;
622
623         return wb_keys_resize(&wb->flushing, new_size) ?:
624                 wb_keys_resize(&wb->inc, new_size);
625 }
626
627 void bch2_fs_btree_write_buffer_exit(struct bch_fs *c)
628 {
629         struct btree_write_buffer *wb = &c->btree_write_buffer;
630
631         BUG_ON((wb->inc.keys.nr || wb->flushing.keys.nr) &&
632                !bch2_journal_error(&c->journal));
633
634         darray_exit(&wb->sorted);
635         darray_exit(&wb->flushing.keys);
636         darray_exit(&wb->inc.keys);
637 }
638
639 int bch2_fs_btree_write_buffer_init(struct bch_fs *c)
640 {
641         struct btree_write_buffer *wb = &c->btree_write_buffer;
642
643         mutex_init(&wb->inc.lock);
644         mutex_init(&wb->flushing.lock);
645         INIT_WORK(&wb->flush_work, bch2_btree_write_buffer_flush_work);
646
647         /* Will be resized by journal as needed: */
648         unsigned initial_size = 1 << 16;
649
650         return  darray_make_room(&wb->inc.keys, initial_size) ?:
651                 darray_make_room(&wb->flushing.keys, initial_size) ?:
652                 darray_make_room(&wb->sorted, initial_size);
653 }