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