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