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