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