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