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