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