]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal.c
5699a9d8d9f957aa747765fc38e28f6dc37a0352
[bcachefs-tools-debian] / libbcachefs / journal.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bcachefs journalling code, for btree insertions
4  *
5  * Copyright 2012 Google, Inc.
6  */
7
8 #include "bcachefs.h"
9 #include "alloc_foreground.h"
10 #include "bkey_methods.h"
11 #include "btree_gc.h"
12 #include "btree_update.h"
13 #include "buckets.h"
14 #include "error.h"
15 #include "journal.h"
16 #include "journal_io.h"
17 #include "journal_reclaim.h"
18 #include "journal_sb.h"
19 #include "journal_seq_blacklist.h"
20
21 #include <trace/events/bcachefs.h>
22
23 #define x(n)    #n,
24 static const char * const bch2_journal_watermarks[] = {
25         JOURNAL_WATERMARKS()
26         NULL
27 };
28
29 static const char * const bch2_journal_errors[] = {
30         JOURNAL_ERRORS()
31         NULL
32 };
33 #undef x
34
35 static inline bool journal_seq_unwritten(struct journal *j, u64 seq)
36 {
37         return seq > j->seq_ondisk;
38 }
39
40 static bool __journal_entry_is_open(union journal_res_state state)
41 {
42         return state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL;
43 }
44
45 static inline unsigned nr_unwritten_journal_entries(struct journal *j)
46 {
47         return atomic64_read(&j->seq) - j->seq_ondisk;
48 }
49
50 static bool journal_entry_is_open(struct journal *j)
51 {
52         return __journal_entry_is_open(j->reservations);
53 }
54
55 static inline struct journal_buf *
56 journal_seq_to_buf(struct journal *j, u64 seq)
57 {
58         struct journal_buf *buf = NULL;
59
60         EBUG_ON(seq > journal_cur_seq(j));
61
62         if (journal_seq_unwritten(j, seq)) {
63                 buf = j->buf + (seq & JOURNAL_BUF_MASK);
64                 EBUG_ON(le64_to_cpu(buf->data->seq) != seq);
65         }
66         return buf;
67 }
68
69 static void journal_pin_list_init(struct journal_entry_pin_list *p, int count)
70 {
71         unsigned i;
72         for (i = 0; i < ARRAY_SIZE(p->list); i++)
73                 INIT_LIST_HEAD(&p->list[i]);
74         INIT_LIST_HEAD(&p->flushed);
75         atomic_set(&p->count, count);
76         p->devs.nr = 0;
77 }
78
79 /* journal entry close/open: */
80
81 void __bch2_journal_buf_put(struct journal *j)
82 {
83         struct bch_fs *c = container_of(j, struct bch_fs, journal);
84
85         closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
86 }
87
88 /*
89  * Returns true if journal entry is now closed:
90  *
91  * We don't close a journal_buf until the next journal_buf is finished writing,
92  * and can be opened again - this also initializes the next journal_buf:
93  */
94 static void __journal_entry_close(struct journal *j, unsigned closed_val)
95 {
96         struct bch_fs *c = container_of(j, struct bch_fs, journal);
97         struct journal_buf *buf = journal_cur_buf(j);
98         union journal_res_state old, new;
99         u64 v = atomic64_read(&j->reservations.counter);
100         unsigned sectors;
101
102         BUG_ON(closed_val != JOURNAL_ENTRY_CLOSED_VAL &&
103                closed_val != JOURNAL_ENTRY_ERROR_VAL);
104
105         lockdep_assert_held(&j->lock);
106
107         do {
108                 old.v = new.v = v;
109                 new.cur_entry_offset = closed_val;
110
111                 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL ||
112                     old.cur_entry_offset == new.cur_entry_offset)
113                         return;
114         } while ((v = atomic64_cmpxchg(&j->reservations.counter,
115                                        old.v, new.v)) != old.v);
116
117         if (!__journal_entry_is_open(old))
118                 return;
119
120         /* Close out old buffer: */
121         buf->data->u64s         = cpu_to_le32(old.cur_entry_offset);
122
123         sectors = vstruct_blocks_plus(buf->data, c->block_bits,
124                                       buf->u64s_reserved) << c->block_bits;
125         BUG_ON(sectors > buf->sectors);
126         buf->sectors = sectors;
127
128         /*
129          * We have to set last_seq here, _before_ opening a new journal entry:
130          *
131          * A threads may replace an old pin with a new pin on their current
132          * journal reservation - the expectation being that the journal will
133          * contain either what the old pin protected or what the new pin
134          * protects.
135          *
136          * After the old pin is dropped journal_last_seq() won't include the old
137          * pin, so we can only write the updated last_seq on the entry that
138          * contains whatever the new pin protects.
139          *
140          * Restated, we can _not_ update last_seq for a given entry if there
141          * could be a newer entry open with reservations/pins that have been
142          * taken against it.
143          *
144          * Hence, we want update/set last_seq on the current journal entry right
145          * before we open a new one:
146          */
147         buf->last_seq           = journal_last_seq(j);
148         buf->data->last_seq     = cpu_to_le64(buf->last_seq);
149         BUG_ON(buf->last_seq > le64_to_cpu(buf->data->seq));
150
151         __bch2_journal_pin_put(j, le64_to_cpu(buf->data->seq));
152
153         cancel_delayed_work(&j->write_work);
154
155         bch2_journal_space_available(j);
156
157         bch2_journal_buf_put(j, old.idx);
158 }
159
160 void bch2_journal_halt(struct journal *j)
161 {
162         spin_lock(&j->lock);
163         __journal_entry_close(j, JOURNAL_ENTRY_ERROR_VAL);
164         if (!j->err_seq)
165                 j->err_seq = journal_cur_seq(j);
166         spin_unlock(&j->lock);
167 }
168
169 static bool journal_entry_want_write(struct journal *j)
170 {
171         bool ret = !journal_entry_is_open(j) ||
172                 journal_cur_seq(j) == journal_last_unwritten_seq(j);
173
174         /* Don't close it yet if we already have a write in flight: */
175         if (ret)
176                 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
177         else if (nr_unwritten_journal_entries(j)) {
178                 struct journal_buf *buf = journal_cur_buf(j);
179
180                 if (!buf->flush_time) {
181                         buf->flush_time = local_clock() ?: 1;
182                         buf->expires = jiffies;
183                 }
184         }
185
186         return ret;
187 }
188
189 static bool journal_entry_close(struct journal *j)
190 {
191         bool ret;
192
193         spin_lock(&j->lock);
194         ret = journal_entry_want_write(j);
195         spin_unlock(&j->lock);
196
197         return ret;
198 }
199
200 /*
201  * should _only_ called from journal_res_get() - when we actually want a
202  * journal reservation - journal entry is open means journal is dirty:
203  */
204 static int journal_entry_open(struct journal *j)
205 {
206         struct bch_fs *c = container_of(j, struct bch_fs, journal);
207         struct journal_buf *buf = j->buf +
208                 ((journal_cur_seq(j) + 1) & JOURNAL_BUF_MASK);
209         union journal_res_state old, new;
210         int u64s;
211         u64 v;
212
213         lockdep_assert_held(&j->lock);
214         BUG_ON(journal_entry_is_open(j));
215         BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
216
217         if (j->blocked)
218                 return JOURNAL_ERR_blocked;
219
220         if (j->cur_entry_error)
221                 return j->cur_entry_error;
222
223         if (bch2_journal_error(j))
224                 return JOURNAL_ERR_insufficient_devices; /* -EROFS */
225
226         if (!fifo_free(&j->pin))
227                 return JOURNAL_ERR_journal_pin_full;
228
229         if (nr_unwritten_journal_entries(j) == ARRAY_SIZE(j->buf))
230                 return JOURNAL_ERR_max_in_flight;
231
232         BUG_ON(!j->cur_entry_sectors);
233
234         buf->expires            =
235                 (journal_cur_seq(j) == j->flushed_seq_ondisk
236                  ? jiffies
237                  : j->last_flush_write) +
238                 msecs_to_jiffies(c->opts.journal_flush_delay);
239
240         buf->u64s_reserved      = j->entry_u64s_reserved;
241         buf->disk_sectors       = j->cur_entry_sectors;
242         buf->sectors            = min(buf->disk_sectors, buf->buf_size >> 9);
243
244         u64s = (int) (buf->sectors << 9) / sizeof(u64) -
245                 journal_entry_overhead(j);
246         u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
247
248         if (u64s <= (ssize_t) j->early_journal_entries.nr)
249                 return JOURNAL_ERR_journal_full;
250
251         if (fifo_empty(&j->pin) && j->reclaim_thread)
252                 wake_up_process(j->reclaim_thread);
253
254         /*
255          * The fifo_push() needs to happen at the same time as j->seq is
256          * incremented for journal_last_seq() to be calculated correctly
257          */
258         atomic64_inc(&j->seq);
259         journal_pin_list_init(fifo_push_ref(&j->pin), 1);
260
261         BUG_ON(j->buf + (journal_cur_seq(j) & JOURNAL_BUF_MASK) != buf);
262
263         bkey_extent_init(&buf->key);
264         buf->noflush    = false;
265         buf->must_flush = false;
266         buf->separate_flush = false;
267         buf->flush_time = 0;
268
269         memset(buf->data, 0, sizeof(*buf->data));
270         buf->data->seq  = cpu_to_le64(journal_cur_seq(j));
271         buf->data->u64s = 0;
272
273         if (j->early_journal_entries.nr) {
274                 memcpy(buf->data->_data, j->early_journal_entries.data,
275                        j->early_journal_entries.nr * sizeof(u64));
276                 le32_add_cpu(&buf->data->u64s, j->early_journal_entries.nr);
277         }
278
279         /*
280          * Must be set before marking the journal entry as open:
281          */
282         j->cur_entry_u64s = u64s;
283
284         v = atomic64_read(&j->reservations.counter);
285         do {
286                 old.v = new.v = v;
287
288                 BUG_ON(old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL);
289
290                 new.idx++;
291                 BUG_ON(journal_state_count(new, new.idx));
292                 BUG_ON(new.idx != (journal_cur_seq(j) & JOURNAL_BUF_MASK));
293
294                 journal_state_inc(&new);
295
296                 /* Handle any already added entries */
297                 new.cur_entry_offset = le32_to_cpu(buf->data->u64s);
298         } while ((v = atomic64_cmpxchg(&j->reservations.counter,
299                                        old.v, new.v)) != old.v);
300
301         if (j->res_get_blocked_start)
302                 bch2_time_stats_update(j->blocked_time,
303                                        j->res_get_blocked_start);
304         j->res_get_blocked_start = 0;
305
306         mod_delayed_work(c->io_complete_wq,
307                          &j->write_work,
308                          msecs_to_jiffies(c->opts.journal_flush_delay));
309         journal_wake(j);
310
311         if (j->early_journal_entries.nr)
312                 darray_exit(&j->early_journal_entries);
313         return 0;
314 }
315
316 static bool journal_quiesced(struct journal *j)
317 {
318         bool ret = atomic64_read(&j->seq) == j->seq_ondisk;
319
320         if (!ret)
321                 journal_entry_close(j);
322         return ret;
323 }
324
325 static void journal_quiesce(struct journal *j)
326 {
327         wait_event(j->wait, journal_quiesced(j));
328 }
329
330 static void journal_write_work(struct work_struct *work)
331 {
332         struct journal *j = container_of(work, struct journal, write_work.work);
333         struct bch_fs *c = container_of(j, struct bch_fs, journal);
334         long delta;
335
336         spin_lock(&j->lock);
337         if (!__journal_entry_is_open(j->reservations))
338                 goto unlock;
339
340         delta = journal_cur_buf(j)->expires - jiffies;
341
342         if (delta > 0)
343                 mod_delayed_work(c->io_complete_wq, &j->write_work, delta);
344         else
345                 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
346 unlock:
347         spin_unlock(&j->lock);
348 }
349
350 static int __journal_res_get(struct journal *j, struct journal_res *res,
351                              unsigned flags)
352 {
353         struct bch_fs *c = container_of(j, struct bch_fs, journal);
354         struct journal_buf *buf;
355         bool can_discard;
356         int ret;
357 retry:
358         if (journal_res_get_fast(j, res, flags))
359                 return 0;
360
361         if (bch2_journal_error(j))
362                 return -BCH_ERR_erofs_journal_err;
363
364         spin_lock(&j->lock);
365
366         /*
367          * Recheck after taking the lock, so we don't race with another thread
368          * that just did journal_entry_open() and call journal_entry_close()
369          * unnecessarily
370          */
371         if (journal_res_get_fast(j, res, flags)) {
372                 spin_unlock(&j->lock);
373                 return 0;
374         }
375
376         if ((flags & JOURNAL_WATERMARK_MASK) < j->watermark) {
377                 /*
378                  * Don't want to close current journal entry, just need to
379                  * invoke reclaim:
380                  */
381                 ret = JOURNAL_ERR_journal_full;
382                 goto unlock;
383         }
384
385         /*
386          * If we couldn't get a reservation because the current buf filled up,
387          * and we had room for a bigger entry on disk, signal that we want to
388          * realloc the journal bufs:
389          */
390         buf = journal_cur_buf(j);
391         if (journal_entry_is_open(j) &&
392             buf->buf_size >> 9 < buf->disk_sectors &&
393             buf->buf_size < JOURNAL_ENTRY_SIZE_MAX)
394                 j->buf_size_want = max(j->buf_size_want, buf->buf_size << 1);
395
396         __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
397         ret = journal_entry_open(j);
398
399         if (ret == JOURNAL_ERR_max_in_flight)
400                 trace_and_count(c, journal_entry_full, c);
401 unlock:
402         if ((ret && ret != JOURNAL_ERR_insufficient_devices) &&
403             !j->res_get_blocked_start) {
404                 j->res_get_blocked_start = local_clock() ?: 1;
405                 trace_and_count(c, journal_full, c);
406         }
407
408         can_discard = j->can_discard;
409         spin_unlock(&j->lock);
410
411         if (!ret)
412                 goto retry;
413
414         if ((ret == JOURNAL_ERR_journal_full ||
415              ret == JOURNAL_ERR_journal_pin_full) &&
416             !can_discard &&
417             !nr_unwritten_journal_entries(j) &&
418             (flags & JOURNAL_WATERMARK_MASK) == JOURNAL_WATERMARK_reserved) {
419                 struct printbuf buf = PRINTBUF;
420
421                 bch_err(c, "Journal stuck! Hava a pre-reservation but journal full (ret %s)",
422                         bch2_journal_errors[ret]);
423
424                 bch2_journal_debug_to_text(&buf, j);
425                 bch_err(c, "%s", buf.buf);
426
427                 printbuf_reset(&buf);
428                 bch2_journal_pins_to_text(&buf, j);
429                 bch_err(c, "Journal pins:\n%s", buf.buf);
430
431                 printbuf_exit(&buf);
432                 bch2_fatal_error(c);
433                 dump_stack();
434         }
435
436         /*
437          * Journal is full - can't rely on reclaim from work item due to
438          * freezing:
439          */
440         if ((ret == JOURNAL_ERR_journal_full ||
441              ret == JOURNAL_ERR_journal_pin_full) &&
442             !(flags & JOURNAL_RES_GET_NONBLOCK)) {
443                 if (can_discard) {
444                         bch2_journal_do_discards(j);
445                         goto retry;
446                 }
447
448                 if (mutex_trylock(&j->reclaim_lock)) {
449                         bch2_journal_reclaim(j);
450                         mutex_unlock(&j->reclaim_lock);
451                 }
452         }
453
454         return ret == JOURNAL_ERR_insufficient_devices
455                 ? -EROFS
456                 : -BCH_ERR_journal_res_get_blocked;
457 }
458
459 /*
460  * Essentially the entry function to the journaling code. When bcachefs is doing
461  * a btree insert, it calls this function to get the current journal write.
462  * Journal write is the structure used set up journal writes. The calling
463  * function will then add its keys to the structure, queuing them for the next
464  * write.
465  *
466  * To ensure forward progress, the current task must not be holding any
467  * btree node write locks.
468  */
469 int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
470                                   unsigned flags)
471 {
472         int ret;
473
474         closure_wait_event(&j->async_wait,
475                    (ret = __journal_res_get(j, res, flags)) !=
476                    -BCH_ERR_journal_res_get_blocked||
477                    (flags & JOURNAL_RES_GET_NONBLOCK));
478         return ret;
479 }
480
481 /* journal_preres: */
482
483 static bool journal_preres_available(struct journal *j,
484                                      struct journal_preres *res,
485                                      unsigned new_u64s,
486                                      unsigned flags)
487 {
488         bool ret = bch2_journal_preres_get_fast(j, res, new_u64s, flags, true);
489
490         if (!ret && mutex_trylock(&j->reclaim_lock)) {
491                 bch2_journal_reclaim(j);
492                 mutex_unlock(&j->reclaim_lock);
493         }
494
495         return ret;
496 }
497
498 int __bch2_journal_preres_get(struct journal *j,
499                               struct journal_preres *res,
500                               unsigned new_u64s,
501                               unsigned flags)
502 {
503         int ret;
504
505         closure_wait_event(&j->preres_wait,
506                    (ret = bch2_journal_error(j)) ||
507                    journal_preres_available(j, res, new_u64s, flags));
508         return ret;
509 }
510
511 /* journal_entry_res: */
512
513 void bch2_journal_entry_res_resize(struct journal *j,
514                                    struct journal_entry_res *res,
515                                    unsigned new_u64s)
516 {
517         union journal_res_state state;
518         int d = new_u64s - res->u64s;
519
520         spin_lock(&j->lock);
521
522         j->entry_u64s_reserved += d;
523         if (d <= 0)
524                 goto out;
525
526         j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
527         smp_mb();
528         state = READ_ONCE(j->reservations);
529
530         if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
531             state.cur_entry_offset > j->cur_entry_u64s) {
532                 j->cur_entry_u64s += d;
533                 /*
534                  * Not enough room in current journal entry, have to flush it:
535                  */
536                 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
537         } else {
538                 journal_cur_buf(j)->u64s_reserved += d;
539         }
540 out:
541         spin_unlock(&j->lock);
542         res->u64s += d;
543 }
544
545 /* journal flushing: */
546
547 /**
548  * bch2_journal_flush_seq_async - wait for a journal entry to be written
549  *
550  * like bch2_journal_wait_on_seq, except that it triggers a write immediately if
551  * necessary
552  */
553 int bch2_journal_flush_seq_async(struct journal *j, u64 seq,
554                                  struct closure *parent)
555 {
556         struct journal_buf *buf;
557         int ret = 0;
558
559         if (seq <= j->flushed_seq_ondisk)
560                 return 1;
561
562         spin_lock(&j->lock);
563
564         if (WARN_ONCE(seq > journal_cur_seq(j),
565                       "requested to flush journal seq %llu, but currently at %llu",
566                       seq, journal_cur_seq(j)))
567                 goto out;
568
569         /* Recheck under lock: */
570         if (j->err_seq && seq >= j->err_seq) {
571                 ret = -EIO;
572                 goto out;
573         }
574
575         if (seq <= j->flushed_seq_ondisk) {
576                 ret = 1;
577                 goto out;
578         }
579
580         /* if seq was written, but not flushed - flush a newer one instead */
581         seq = max(seq, journal_last_unwritten_seq(j));
582
583 recheck_need_open:
584         if (seq > journal_cur_seq(j)) {
585                 struct journal_res res = { 0 };
586
587                 if (journal_entry_is_open(j))
588                         __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
589
590                 spin_unlock(&j->lock);
591
592                 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
593                 if (ret)
594                         return ret;
595
596                 seq = res.seq;
597                 buf = j->buf + (seq & JOURNAL_BUF_MASK);
598                 buf->must_flush = true;
599
600                 if (!buf->flush_time) {
601                         buf->flush_time = local_clock() ?: 1;
602                         buf->expires = jiffies;
603                 }
604
605                 if (parent && !closure_wait(&buf->wait, parent))
606                         BUG();
607
608                 bch2_journal_res_put(j, &res);
609
610                 spin_lock(&j->lock);
611                 goto want_write;
612         }
613
614         /*
615          * if write was kicked off without a flush, flush the next sequence
616          * number instead
617          */
618         buf = journal_seq_to_buf(j, seq);
619         if (buf->noflush) {
620                 seq++;
621                 goto recheck_need_open;
622         }
623
624         buf->must_flush = true;
625
626         if (parent && !closure_wait(&buf->wait, parent))
627                 BUG();
628 want_write:
629         if (seq == journal_cur_seq(j))
630                 journal_entry_want_write(j);
631 out:
632         spin_unlock(&j->lock);
633         return ret;
634 }
635
636 int bch2_journal_flush_seq(struct journal *j, u64 seq)
637 {
638         u64 start_time = local_clock();
639         int ret, ret2;
640
641         /*
642          * Don't update time_stats when @seq is already flushed:
643          */
644         if (seq <= j->flushed_seq_ondisk)
645                 return 0;
646
647         ret = wait_event_interruptible(j->wait, (ret2 = bch2_journal_flush_seq_async(j, seq, NULL)));
648
649         if (!ret)
650                 bch2_time_stats_update(j->flush_seq_time, start_time);
651
652         return ret ?: ret2 < 0 ? ret2 : 0;
653 }
654
655 /*
656  * bch2_journal_flush_async - if there is an open journal entry, or a journal
657  * still being written, write it and wait for the write to complete
658  */
659 void bch2_journal_flush_async(struct journal *j, struct closure *parent)
660 {
661         bch2_journal_flush_seq_async(j, atomic64_read(&j->seq), parent);
662 }
663
664 int bch2_journal_flush(struct journal *j)
665 {
666         return bch2_journal_flush_seq(j, atomic64_read(&j->seq));
667 }
668
669 /*
670  * bch2_journal_noflush_seq - tell the journal not to issue any flushes before
671  * @seq
672  */
673 bool bch2_journal_noflush_seq(struct journal *j, u64 seq)
674 {
675         struct bch_fs *c = container_of(j, struct bch_fs, journal);
676         u64 unwritten_seq;
677         bool ret = false;
678
679         if (!(c->sb.features & (1ULL << BCH_FEATURE_journal_no_flush)))
680                 return false;
681
682         if (seq <= c->journal.flushed_seq_ondisk)
683                 return false;
684
685         spin_lock(&j->lock);
686         if (seq <= c->journal.flushed_seq_ondisk)
687                 goto out;
688
689         for (unwritten_seq = journal_last_unwritten_seq(j);
690              unwritten_seq < seq;
691              unwritten_seq++) {
692                 struct journal_buf *buf = journal_seq_to_buf(j, unwritten_seq);
693
694                 /* journal write is already in flight, and was a flush write: */
695                 if (unwritten_seq == journal_last_unwritten_seq(j) && !buf->noflush)
696                         goto out;
697
698                 buf->noflush = true;
699         }
700
701         ret = true;
702 out:
703         spin_unlock(&j->lock);
704         return ret;
705 }
706
707 int bch2_journal_meta(struct journal *j)
708 {
709         struct journal_buf *buf;
710         struct journal_res res;
711         int ret;
712
713         memset(&res, 0, sizeof(res));
714
715         ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
716         if (ret)
717                 return ret;
718
719         buf = j->buf + (res.seq & JOURNAL_BUF_MASK);
720         buf->must_flush = true;
721
722         if (!buf->flush_time) {
723                 buf->flush_time = local_clock() ?: 1;
724                 buf->expires = jiffies;
725         }
726
727         bch2_journal_res_put(j, &res);
728
729         return bch2_journal_flush_seq(j, res.seq);
730 }
731
732 /* block/unlock the journal: */
733
734 void bch2_journal_unblock(struct journal *j)
735 {
736         spin_lock(&j->lock);
737         j->blocked--;
738         spin_unlock(&j->lock);
739
740         journal_wake(j);
741 }
742
743 void bch2_journal_block(struct journal *j)
744 {
745         spin_lock(&j->lock);
746         j->blocked++;
747         spin_unlock(&j->lock);
748
749         journal_quiesce(j);
750 }
751
752 /* allocate journal on a device: */
753
754 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
755                                          bool new_fs, struct closure *cl)
756 {
757         struct bch_fs *c = ca->fs;
758         struct journal_device *ja = &ca->journal;
759         u64 *new_bucket_seq = NULL, *new_buckets = NULL;
760         struct open_bucket **ob = NULL;
761         long *bu = NULL;
762         unsigned i, pos, nr_got = 0, nr_want = nr - ja->nr;
763         int ret = 0;
764
765         BUG_ON(nr <= ja->nr);
766
767         bu              = kcalloc(nr_want, sizeof(*bu), GFP_KERNEL);
768         ob              = kcalloc(nr_want, sizeof(*ob), GFP_KERNEL);
769         new_buckets     = kcalloc(nr, sizeof(u64), GFP_KERNEL);
770         new_bucket_seq  = kcalloc(nr, sizeof(u64), GFP_KERNEL);
771         if (!bu || !ob || !new_buckets || !new_bucket_seq) {
772                 ret = -ENOMEM;
773                 goto err_free;
774         }
775
776         for (nr_got = 0; nr_got < nr_want; nr_got++) {
777                 if (new_fs) {
778                         bu[nr_got] = bch2_bucket_alloc_new_fs(ca);
779                         if (bu[nr_got] < 0) {
780                                 ret = -BCH_ERR_ENOSPC_bucket_alloc;
781                                 break;
782                         }
783                 } else {
784                         ob[nr_got] = bch2_bucket_alloc(c, ca, RESERVE_none, cl);
785                         ret = PTR_ERR_OR_ZERO(ob[nr_got]);
786                         if (ret)
787                                 break;
788
789                         ret = bch2_trans_run(c,
790                                 bch2_trans_mark_metadata_bucket(&trans, ca,
791                                                 ob[nr_got]->bucket, BCH_DATA_journal,
792                                                 ca->mi.bucket_size));
793                         if (ret) {
794                                 bch2_open_bucket_put(c, ob[nr_got]);
795                                 bch_err(c, "error marking new journal buckets: %s", bch2_err_str(ret));
796                                 break;
797                         }
798
799                         bu[nr_got] = ob[nr_got]->bucket;
800                 }
801         }
802
803         if (!nr_got)
804                 goto err_free;
805
806         /* Don't return an error if we successfully allocated some buckets: */
807         ret = 0;
808
809         if (c) {
810                 bch2_journal_flush_all_pins(&c->journal);
811                 bch2_journal_block(&c->journal);
812                 mutex_lock(&c->sb_lock);
813         }
814
815         memcpy(new_buckets,     ja->buckets,    ja->nr * sizeof(u64));
816         memcpy(new_bucket_seq,  ja->bucket_seq, ja->nr * sizeof(u64));
817
818         BUG_ON(ja->discard_idx > ja->nr);
819
820         pos = ja->discard_idx ?: ja->nr;
821
822         memmove(new_buckets + pos + nr_got,
823                 new_buckets + pos,
824                 sizeof(new_buckets[0]) * (ja->nr - pos));
825         memmove(new_bucket_seq + pos + nr_got,
826                 new_bucket_seq + pos,
827                 sizeof(new_bucket_seq[0]) * (ja->nr - pos));
828
829         for (i = 0; i < nr_got; i++) {
830                 new_buckets[pos + i] = bu[i];
831                 new_bucket_seq[pos + i] = 0;
832         }
833
834         nr = ja->nr + nr_got;
835
836         ret = bch2_journal_buckets_to_sb(c, ca, new_buckets, nr);
837         if (ret)
838                 goto err_unblock;
839
840         if (!new_fs)
841                 bch2_write_super(c);
842
843         /* Commit: */
844         if (c)
845                 spin_lock(&c->journal.lock);
846
847         swap(new_buckets,       ja->buckets);
848         swap(new_bucket_seq,    ja->bucket_seq);
849         ja->nr = nr;
850
851         if (pos <= ja->discard_idx)
852                 ja->discard_idx = (ja->discard_idx + nr_got) % ja->nr;
853         if (pos <= ja->dirty_idx_ondisk)
854                 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + nr_got) % ja->nr;
855         if (pos <= ja->dirty_idx)
856                 ja->dirty_idx = (ja->dirty_idx + nr_got) % ja->nr;
857         if (pos <= ja->cur_idx)
858                 ja->cur_idx = (ja->cur_idx + nr_got) % ja->nr;
859
860         if (c)
861                 spin_unlock(&c->journal.lock);
862 err_unblock:
863         if (c) {
864                 bch2_journal_unblock(&c->journal);
865                 mutex_unlock(&c->sb_lock);
866         }
867
868         if (ret && !new_fs)
869                 for (i = 0; i < nr_got; i++)
870                         bch2_trans_run(c,
871                                 bch2_trans_mark_metadata_bucket(&trans, ca,
872                                                 bu[i], BCH_DATA_free, 0));
873 err_free:
874         if (!new_fs)
875                 for (i = 0; i < nr_got; i++)
876                         bch2_open_bucket_put(c, ob[i]);
877
878         kfree(new_bucket_seq);
879         kfree(new_buckets);
880         kfree(ob);
881         kfree(bu);
882         return ret;
883 }
884
885 /*
886  * Allocate more journal space at runtime - not currently making use if it, but
887  * the code works:
888  */
889 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
890                                 unsigned nr)
891 {
892         struct journal_device *ja = &ca->journal;
893         struct closure cl;
894         int ret = 0;
895
896         closure_init_stack(&cl);
897
898         down_write(&c->state_lock);
899
900         /* don't handle reducing nr of buckets yet: */
901         if (nr < ja->nr)
902                 goto unlock;
903
904         while (ja->nr < nr) {
905                 struct disk_reservation disk_res = { 0, 0 };
906
907                 /*
908                  * note: journal buckets aren't really counted as _sectors_ used yet, so
909                  * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
910                  * when space used goes up without a reservation - but we do need the
911                  * reservation to ensure we'll actually be able to allocate:
912                  *
913                  * XXX: that's not right, disk reservations only ensure a
914                  * filesystem-wide allocation will succeed, this is a device
915                  * specific allocation - we can hang here:
916                  */
917
918                 ret = bch2_disk_reservation_get(c, &disk_res,
919                                                 bucket_to_sector(ca, nr - ja->nr), 1, 0);
920                 if (ret)
921                         break;
922
923                 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
924
925                 bch2_disk_reservation_put(c, &disk_res);
926
927                 closure_sync(&cl);
928
929                 if (ret && ret != -BCH_ERR_bucket_alloc_blocked)
930                         break;
931         }
932
933         if (ret)
934                 bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
935 unlock:
936         up_write(&c->state_lock);
937         return ret;
938 }
939
940 int bch2_dev_journal_alloc(struct bch_dev *ca)
941 {
942         unsigned nr;
943
944         if (dynamic_fault("bcachefs:add:journal_alloc"))
945                 return -ENOMEM;
946
947         /* 1/128th of the device by default: */
948         nr = ca->mi.nbuckets >> 7;
949
950         /*
951          * clamp journal size to 8192 buckets or 8GB (in sectors), whichever
952          * is smaller:
953          */
954         nr = clamp_t(unsigned, nr,
955                      BCH_JOURNAL_BUCKETS_MIN,
956                      min(1 << 13,
957                          (1 << 24) / ca->mi.bucket_size));
958
959         return __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
960 }
961
962 /* startup/shutdown: */
963
964 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
965 {
966         bool ret = false;
967         u64 seq;
968
969         spin_lock(&j->lock);
970         for (seq = journal_last_unwritten_seq(j);
971              seq <= journal_cur_seq(j) && !ret;
972              seq++) {
973                 struct journal_buf *buf = journal_seq_to_buf(j, seq);
974
975                 if (bch2_bkey_has_device_c(bkey_i_to_s_c(&buf->key), dev_idx))
976                         ret = true;
977         }
978         spin_unlock(&j->lock);
979
980         return ret;
981 }
982
983 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
984 {
985         wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
986 }
987
988 void bch2_fs_journal_stop(struct journal *j)
989 {
990         bch2_journal_reclaim_stop(j);
991         bch2_journal_flush_all_pins(j);
992
993         wait_event(j->wait, journal_entry_close(j));
994
995         /*
996          * Always write a new journal entry, to make sure the clock hands are up
997          * to date (and match the superblock)
998          */
999         bch2_journal_meta(j);
1000
1001         journal_quiesce(j);
1002
1003         BUG_ON(!bch2_journal_error(j) &&
1004                test_bit(JOURNAL_REPLAY_DONE, &j->flags) &&
1005                j->last_empty_seq != journal_cur_seq(j));
1006
1007         cancel_delayed_work_sync(&j->write_work);
1008 }
1009
1010 int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
1011 {
1012         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1013         struct journal_entry_pin_list *p;
1014         struct journal_replay *i, **_i;
1015         struct genradix_iter iter;
1016         bool had_entries = false;
1017         unsigned ptr;
1018         u64 last_seq = cur_seq, nr, seq;
1019
1020         genradix_for_each_reverse(&c->journal_entries, iter, _i) {
1021                 i = *_i;
1022
1023                 if (!i || i->ignore)
1024                         continue;
1025
1026                 last_seq = le64_to_cpu(i->j.last_seq);
1027                 break;
1028         }
1029
1030         nr = cur_seq - last_seq;
1031
1032         if (nr + 1 > j->pin.size) {
1033                 free_fifo(&j->pin);
1034                 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
1035                 if (!j->pin.data) {
1036                         bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
1037                         return -ENOMEM;
1038                 }
1039         }
1040
1041         j->replay_journal_seq   = last_seq;
1042         j->replay_journal_seq_end = cur_seq;
1043         j->last_seq_ondisk      = last_seq;
1044         j->flushed_seq_ondisk   = cur_seq - 1;
1045         j->seq_ondisk           = cur_seq - 1;
1046         j->pin.front            = last_seq;
1047         j->pin.back             = cur_seq;
1048         atomic64_set(&j->seq, cur_seq - 1);
1049
1050         fifo_for_each_entry_ptr(p, &j->pin, seq)
1051                 journal_pin_list_init(p, 1);
1052
1053         genradix_for_each(&c->journal_entries, iter, _i) {
1054                 i = *_i;
1055
1056                 if (!i || i->ignore)
1057                         continue;
1058
1059                 seq = le64_to_cpu(i->j.seq);
1060                 BUG_ON(seq >= cur_seq);
1061
1062                 if (seq < last_seq)
1063                         continue;
1064
1065                 if (journal_entry_empty(&i->j))
1066                         j->last_empty_seq = le64_to_cpu(i->j.seq);
1067
1068                 p = journal_seq_pin(j, seq);
1069
1070                 p->devs.nr = 0;
1071                 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1072                         bch2_dev_list_add_dev(&p->devs, i->ptrs[ptr].dev);
1073
1074                 had_entries = true;
1075         }
1076
1077         if (!had_entries)
1078                 j->last_empty_seq = cur_seq;
1079
1080         spin_lock(&j->lock);
1081
1082         set_bit(JOURNAL_STARTED, &j->flags);
1083         j->last_flush_write = jiffies;
1084
1085         j->reservations.idx = j->reservations.unwritten_idx = journal_cur_seq(j);
1086         j->reservations.unwritten_idx++;
1087
1088         c->last_bucket_seq_cleanup = journal_cur_seq(j);
1089
1090         bch2_journal_space_available(j);
1091         spin_unlock(&j->lock);
1092
1093         return bch2_journal_reclaim_start(j);
1094 }
1095
1096 /* init/exit: */
1097
1098 void bch2_dev_journal_exit(struct bch_dev *ca)
1099 {
1100         kfree(ca->journal.bio);
1101         kfree(ca->journal.buckets);
1102         kfree(ca->journal.bucket_seq);
1103
1104         ca->journal.bio         = NULL;
1105         ca->journal.buckets     = NULL;
1106         ca->journal.bucket_seq  = NULL;
1107 }
1108
1109 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1110 {
1111         struct journal_device *ja = &ca->journal;
1112         struct bch_sb_field_journal *journal_buckets =
1113                 bch2_sb_get_journal(sb);
1114         struct bch_sb_field_journal_v2 *journal_buckets_v2 =
1115                 bch2_sb_get_journal_v2(sb);
1116         unsigned i, nr_bvecs;
1117
1118         ja->nr = 0;
1119
1120         if (journal_buckets_v2) {
1121                 unsigned nr = bch2_sb_field_journal_v2_nr_entries(journal_buckets_v2);
1122
1123                 for (i = 0; i < nr; i++)
1124                         ja->nr += le64_to_cpu(journal_buckets_v2->d[i].nr);
1125         } else if (journal_buckets) {
1126                 ja->nr = bch2_nr_journal_buckets(journal_buckets);
1127         }
1128
1129         ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1130         if (!ja->bucket_seq)
1131                 return -ENOMEM;
1132
1133         nr_bvecs = DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE);
1134
1135         ca->journal.bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
1136         if (!ca->journal.bio)
1137                 return -ENOMEM;
1138
1139         bio_init(ca->journal.bio, NULL, ca->journal.bio->bi_inline_vecs, nr_bvecs, 0);
1140
1141         ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1142         if (!ja->buckets)
1143                 return -ENOMEM;
1144
1145         if (journal_buckets_v2) {
1146                 unsigned nr = bch2_sb_field_journal_v2_nr_entries(journal_buckets_v2);
1147                 unsigned j, dst = 0;
1148
1149                 for (i = 0; i < nr; i++)
1150                         for (j = 0; j < le64_to_cpu(journal_buckets_v2->d[i].nr); j++)
1151                                 ja->buckets[dst++] =
1152                                         le64_to_cpu(journal_buckets_v2->d[i].start) + j;
1153         } else if (journal_buckets) {
1154                 for (i = 0; i < ja->nr; i++)
1155                         ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1156         }
1157
1158         return 0;
1159 }
1160
1161 void bch2_fs_journal_exit(struct journal *j)
1162 {
1163         unsigned i;
1164
1165         darray_exit(&j->early_journal_entries);
1166
1167         for (i = 0; i < ARRAY_SIZE(j->buf); i++)
1168                 kvpfree(j->buf[i].data, j->buf[i].buf_size);
1169         free_fifo(&j->pin);
1170 }
1171
1172 int bch2_fs_journal_init(struct journal *j)
1173 {
1174         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1175         static struct lock_class_key res_key;
1176         unsigned i;
1177         int ret = 0;
1178
1179         pr_verbose_init(c->opts, "");
1180
1181         spin_lock_init(&j->lock);
1182         spin_lock_init(&j->err_lock);
1183         init_waitqueue_head(&j->wait);
1184         INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1185         init_waitqueue_head(&j->reclaim_wait);
1186         init_waitqueue_head(&j->pin_flush_wait);
1187         mutex_init(&j->reclaim_lock);
1188         mutex_init(&j->discard_lock);
1189
1190         lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1191
1192         atomic64_set(&j->reservations.counter,
1193                 ((union journal_res_state)
1194                  { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1195
1196         if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL))) {
1197                 ret = -ENOMEM;
1198                 goto out;
1199         }
1200
1201         for (i = 0; i < ARRAY_SIZE(j->buf); i++) {
1202                 j->buf[i].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1203                 j->buf[i].data = kvpmalloc(j->buf[i].buf_size, GFP_KERNEL);
1204                 if (!j->buf[i].data) {
1205                         ret = -ENOMEM;
1206                         goto out;
1207                 }
1208         }
1209
1210         j->pin.front = j->pin.back = 1;
1211 out:
1212         pr_verbose_init(c->opts, "ret %i", ret);
1213         return ret;
1214 }
1215
1216 /* debug: */
1217
1218 void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1219 {
1220         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1221         union journal_res_state s;
1222         struct bch_dev *ca;
1223         unsigned long now = jiffies;
1224         u64 seq;
1225         unsigned i;
1226
1227         if (!out->nr_tabstops)
1228                 printbuf_tabstop_push(out, 24);
1229         out->atomic++;
1230
1231         rcu_read_lock();
1232         s = READ_ONCE(j->reservations);
1233
1234         prt_printf(out, "dirty journal entries:\t%llu/%llu\n",  fifo_used(&j->pin), j->pin.size);
1235         prt_printf(out, "seq:\t\t\t%llu\n",                     journal_cur_seq(j));
1236         prt_printf(out, "seq_ondisk:\t\t%llu\n",                j->seq_ondisk);
1237         prt_printf(out, "last_seq:\t\t%llu\n",          journal_last_seq(j));
1238         prt_printf(out, "last_seq_ondisk:\t%llu\n",             j->last_seq_ondisk);
1239         prt_printf(out, "flushed_seq_ondisk:\t%llu\n",  j->flushed_seq_ondisk);
1240         prt_printf(out, "prereserved:\t\t%u/%u\n",              j->prereserved.reserved, j->prereserved.remaining);
1241         prt_printf(out, "watermark:\t\t%s\n",           bch2_journal_watermarks[j->watermark]);
1242         prt_printf(out, "each entry reserved:\t%u\n",   j->entry_u64s_reserved);
1243         prt_printf(out, "nr flush writes:\t%llu\n",             j->nr_flush_writes);
1244         prt_printf(out, "nr noflush writes:\t%llu\n",   j->nr_noflush_writes);
1245         prt_printf(out, "nr direct reclaim:\t%llu\n",   j->nr_direct_reclaim);
1246         prt_printf(out, "nr background reclaim:\t%llu\n",       j->nr_background_reclaim);
1247         prt_printf(out, "reclaim kicked:\t\t%u\n",              j->reclaim_kicked);
1248         prt_printf(out, "reclaim runs in:\t%u ms\n",    time_after(j->next_reclaim, now)
1249                ? jiffies_to_msecs(j->next_reclaim - jiffies) : 0);
1250         prt_printf(out, "current entry sectors:\t%u\n", j->cur_entry_sectors);
1251         prt_printf(out, "current entry error:\t%s\n",   bch2_journal_errors[j->cur_entry_error]);
1252         prt_printf(out, "current entry:\t\t");
1253
1254         switch (s.cur_entry_offset) {
1255         case JOURNAL_ENTRY_ERROR_VAL:
1256                 prt_printf(out, "error");
1257                 break;
1258         case JOURNAL_ENTRY_CLOSED_VAL:
1259                 prt_printf(out, "closed");
1260                 break;
1261         default:
1262                 prt_printf(out, "%u/%u", s.cur_entry_offset, j->cur_entry_u64s);
1263                 break;
1264         }
1265
1266         prt_newline(out);
1267
1268         for (seq = journal_cur_seq(j);
1269              seq >= journal_last_unwritten_seq(j);
1270              --seq) {
1271                 i = seq & JOURNAL_BUF_MASK;
1272
1273                 prt_printf(out, "unwritten entry:");
1274                 prt_tab(out);
1275                 prt_printf(out, "%llu", seq);
1276                 prt_newline(out);
1277                 printbuf_indent_add(out, 2);
1278
1279                 prt_printf(out, "refcount:");
1280                 prt_tab(out);
1281                 prt_printf(out, "%u", journal_state_count(s, i));
1282                 prt_newline(out);
1283
1284                 prt_printf(out, "sectors:");
1285                 prt_tab(out);
1286                 prt_printf(out, "%u", j->buf[i].sectors);
1287                 prt_newline(out);
1288
1289                 prt_printf(out, "expires");
1290                 prt_tab(out);
1291                 prt_printf(out, "%li jiffies", j->buf[i].expires - jiffies);
1292                 prt_newline(out);
1293
1294                 printbuf_indent_sub(out, 2);
1295         }
1296
1297         prt_printf(out,
1298                "replay done:\t\t%i\n",
1299                test_bit(JOURNAL_REPLAY_DONE,    &j->flags));
1300
1301         prt_printf(out, "space:\n");
1302         prt_printf(out, "\tdiscarded\t%u:%u\n",
1303                j->space[journal_space_discarded].next_entry,
1304                j->space[journal_space_discarded].total);
1305         prt_printf(out, "\tclean ondisk\t%u:%u\n",
1306                j->space[journal_space_clean_ondisk].next_entry,
1307                j->space[journal_space_clean_ondisk].total);
1308         prt_printf(out, "\tclean\t\t%u:%u\n",
1309                j->space[journal_space_clean].next_entry,
1310                j->space[journal_space_clean].total);
1311         prt_printf(out, "\ttotal\t\t%u:%u\n",
1312                j->space[journal_space_total].next_entry,
1313                j->space[journal_space_total].total);
1314
1315         for_each_member_device_rcu(ca, c, i,
1316                                    &c->rw_devs[BCH_DATA_journal]) {
1317                 struct journal_device *ja = &ca->journal;
1318
1319                 if (!test_bit(ca->dev_idx, c->rw_devs[BCH_DATA_journal].d))
1320                         continue;
1321
1322                 if (!ja->nr)
1323                         continue;
1324
1325                 prt_printf(out, "dev %u:\n",            i);
1326                 prt_printf(out, "\tnr\t\t%u\n",         ja->nr);
1327                 prt_printf(out, "\tbucket size\t%u\n",  ca->mi.bucket_size);
1328                 prt_printf(out, "\tavailable\t%u:%u\n", bch2_journal_dev_buckets_available(j, ja, journal_space_discarded), ja->sectors_free);
1329                 prt_printf(out, "\tdiscard_idx\t%u\n",  ja->discard_idx);
1330                 prt_printf(out, "\tdirty_ondisk\t%u (seq %llu)\n", ja->dirty_idx_ondisk,        ja->bucket_seq[ja->dirty_idx_ondisk]);
1331                 prt_printf(out, "\tdirty_idx\t%u (seq %llu)\n", ja->dirty_idx,          ja->bucket_seq[ja->dirty_idx]);
1332                 prt_printf(out, "\tcur_idx\t\t%u (seq %llu)\n", ja->cur_idx,            ja->bucket_seq[ja->cur_idx]);
1333         }
1334
1335         rcu_read_unlock();
1336
1337         --out->atomic;
1338 }
1339
1340 void bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1341 {
1342         spin_lock(&j->lock);
1343         __bch2_journal_debug_to_text(out, j);
1344         spin_unlock(&j->lock);
1345 }
1346
1347 bool bch2_journal_seq_pins_to_text(struct printbuf *out, struct journal *j, u64 *seq)
1348 {
1349         struct journal_entry_pin_list *pin_list;
1350         struct journal_entry_pin *pin;
1351         unsigned i;
1352
1353         spin_lock(&j->lock);
1354         *seq = max(*seq, j->pin.front);
1355
1356         if (*seq >= j->pin.back) {
1357                 spin_unlock(&j->lock);
1358                 return true;
1359         }
1360
1361         out->atomic++;
1362
1363         pin_list = journal_seq_pin(j, *seq);
1364
1365         prt_printf(out, "%llu: count %u", *seq, atomic_read(&pin_list->count));
1366         prt_newline(out);
1367         printbuf_indent_add(out, 2);
1368
1369         for (i = 0; i < ARRAY_SIZE(pin_list->list); i++)
1370                 list_for_each_entry(pin, &pin_list->list[i], list) {
1371                         prt_printf(out, "\t%px %ps", pin, pin->flush);
1372                         prt_newline(out);
1373                 }
1374
1375         if (!list_empty(&pin_list->flushed)) {
1376                 prt_printf(out, "flushed:");
1377                 prt_newline(out);
1378         }
1379
1380         list_for_each_entry(pin, &pin_list->flushed, list) {
1381                 prt_printf(out, "\t%px %ps", pin, pin->flush);
1382                 prt_newline(out);
1383         }
1384
1385         printbuf_indent_sub(out, 2);
1386
1387         --out->atomic;
1388         spin_unlock(&j->lock);
1389
1390         return false;
1391 }
1392
1393 void bch2_journal_pins_to_text(struct printbuf *out, struct journal *j)
1394 {
1395         u64 seq = 0;
1396
1397         while (!bch2_journal_seq_pins_to_text(out, j, &seq))
1398                 seq++;
1399 }