1 // SPDX-License-Identifier: GPL-2.0
3 * bcachefs journalling code, for btree insertions
5 * Copyright 2012 Google, Inc.
9 #include "alloc_foreground.h"
10 #include "bkey_methods.h"
12 #include "btree_update.h"
16 #include "journal_io.h"
17 #include "journal_reclaim.h"
18 #include "journal_seq_blacklist.h"
21 #include <trace/events/bcachefs.h>
23 static inline bool journal_seq_unwritten(struct journal *j, u64 seq)
25 return seq > j->seq_ondisk;
28 static bool __journal_entry_is_open(union journal_res_state state)
30 return state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL;
33 static inline unsigned nr_unwritten_journal_entries(struct journal *j)
35 return atomic64_read(&j->seq) - j->seq_ondisk;
38 static bool journal_entry_is_open(struct journal *j)
40 return __journal_entry_is_open(j->reservations);
43 static inline struct journal_buf *
44 journal_seq_to_buf(struct journal *j, u64 seq)
46 struct journal_buf *buf = NULL;
48 EBUG_ON(seq > journal_cur_seq(j));
50 if (journal_seq_unwritten(j, seq)) {
51 buf = j->buf + (seq & JOURNAL_BUF_MASK);
52 EBUG_ON(le64_to_cpu(buf->data->seq) != seq);
57 static void journal_pin_list_init(struct journal_entry_pin_list *p, int count)
59 INIT_LIST_HEAD(&p->list);
60 INIT_LIST_HEAD(&p->key_cache_list);
61 INIT_LIST_HEAD(&p->flushed);
62 atomic_set(&p->count, count);
66 /* journal entry close/open: */
68 void __bch2_journal_buf_put(struct journal *j)
70 struct bch_fs *c = container_of(j, struct bch_fs, journal);
72 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
76 * Returns true if journal entry is now closed:
78 * We don't close a journal_buf until the next journal_buf is finished writing,
79 * and can be opened again - this also initializes the next journal_buf:
81 static void __journal_entry_close(struct journal *j, unsigned closed_val)
83 struct bch_fs *c = container_of(j, struct bch_fs, journal);
84 struct journal_buf *buf = journal_cur_buf(j);
85 union journal_res_state old, new;
86 u64 v = atomic64_read(&j->reservations.counter);
89 BUG_ON(closed_val != JOURNAL_ENTRY_CLOSED_VAL &&
90 closed_val != JOURNAL_ENTRY_ERROR_VAL);
92 lockdep_assert_held(&j->lock);
96 new.cur_entry_offset = closed_val;
98 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL ||
99 old.cur_entry_offset == new.cur_entry_offset)
101 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
102 old.v, new.v)) != old.v);
104 if (!__journal_entry_is_open(old))
107 /* Close out old buffer: */
108 buf->data->u64s = cpu_to_le32(old.cur_entry_offset);
110 sectors = vstruct_blocks_plus(buf->data, c->block_bits,
111 buf->u64s_reserved) << c->block_bits;
112 BUG_ON(sectors > buf->sectors);
113 buf->sectors = sectors;
116 * We have to set last_seq here, _before_ opening a new journal entry:
118 * A threads may replace an old pin with a new pin on their current
119 * journal reservation - the expectation being that the journal will
120 * contain either what the old pin protected or what the new pin
123 * After the old pin is dropped journal_last_seq() won't include the old
124 * pin, so we can only write the updated last_seq on the entry that
125 * contains whatever the new pin protects.
127 * Restated, we can _not_ update last_seq for a given entry if there
128 * could be a newer entry open with reservations/pins that have been
131 * Hence, we want update/set last_seq on the current journal entry right
132 * before we open a new one:
134 buf->last_seq = journal_last_seq(j);
135 buf->data->last_seq = cpu_to_le64(buf->last_seq);
136 BUG_ON(buf->last_seq > le64_to_cpu(buf->data->seq));
138 __bch2_journal_pin_put(j, le64_to_cpu(buf->data->seq));
140 cancel_delayed_work(&j->write_work);
142 bch2_journal_space_available(j);
144 bch2_journal_buf_put(j, old.idx);
147 void bch2_journal_halt(struct journal *j)
150 __journal_entry_close(j, JOURNAL_ENTRY_ERROR_VAL);
152 j->err_seq = journal_cur_seq(j);
153 spin_unlock(&j->lock);
156 static bool journal_entry_want_write(struct journal *j)
158 bool ret = !journal_entry_is_open(j) ||
159 journal_cur_seq(j) == journal_last_unwritten_seq(j);
161 /* Don't close it yet if we already have a write in flight: */
163 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
164 else if (nr_unwritten_journal_entries(j)) {
165 struct journal_buf *buf = journal_cur_buf(j);
167 if (!buf->flush_time) {
168 buf->flush_time = local_clock() ?: 1;
169 buf->expires = jiffies;
176 static bool journal_entry_close(struct journal *j)
181 ret = journal_entry_want_write(j);
182 spin_unlock(&j->lock);
188 * should _only_ called from journal_res_get() - when we actually want a
189 * journal reservation - journal entry is open means journal is dirty:
193 * -ENOSPC: journal currently full, must invoke reclaim
194 * -EAGAIN: journal blocked, must wait
195 * -EROFS: insufficient rw devices or journal error
197 static int journal_entry_open(struct journal *j)
199 struct bch_fs *c = container_of(j, struct bch_fs, journal);
200 struct journal_buf *buf = j->buf +
201 ((journal_cur_seq(j) + 1) & JOURNAL_BUF_MASK);
202 union journal_res_state old, new;
206 lockdep_assert_held(&j->lock);
207 BUG_ON(journal_entry_is_open(j));
208 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
211 return cur_entry_blocked;
213 if (j->cur_entry_error)
214 return j->cur_entry_error;
216 if (bch2_journal_error(j))
217 return cur_entry_insufficient_devices; /* -EROFS */
219 if (!fifo_free(&j->pin))
220 return cur_entry_journal_pin_full;
222 if (nr_unwritten_journal_entries(j) == ARRAY_SIZE(j->buf) - 1)
223 return cur_entry_max_in_flight;
225 BUG_ON(!j->cur_entry_sectors);
228 (journal_cur_seq(j) == j->flushed_seq_ondisk
230 : j->last_flush_write) +
231 msecs_to_jiffies(c->opts.journal_flush_delay);
233 buf->u64s_reserved = j->entry_u64s_reserved;
234 buf->disk_sectors = j->cur_entry_sectors;
235 buf->sectors = min(buf->disk_sectors, buf->buf_size >> 9);
237 u64s = (int) (buf->sectors << 9) / sizeof(u64) -
238 journal_entry_overhead(j);
239 u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
242 return cur_entry_journal_full;
245 * The fifo_push() needs to happen at the same time as j->seq is
246 * incremented for journal_last_seq() to be calculated correctly
248 atomic64_inc(&j->seq);
249 journal_pin_list_init(fifo_push_ref(&j->pin), 1);
251 BUG_ON(j->buf + (journal_cur_seq(j) & JOURNAL_BUF_MASK) != buf);
253 bkey_extent_init(&buf->key);
254 buf->noflush = false;
255 buf->must_flush = false;
256 buf->separate_flush = false;
259 memset(buf->data, 0, sizeof(*buf->data));
260 buf->data->seq = cpu_to_le64(journal_cur_seq(j));
264 * Must be set before marking the journal entry as open:
266 j->cur_entry_u64s = u64s;
268 v = atomic64_read(&j->reservations.counter);
272 BUG_ON(old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL);
275 BUG_ON(journal_state_count(new, new.idx));
276 BUG_ON(new.idx != (journal_cur_seq(j) & JOURNAL_BUF_MASK));
278 journal_state_inc(&new);
279 new.cur_entry_offset = 0;
280 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
281 old.v, new.v)) != old.v);
283 if (j->res_get_blocked_start)
284 bch2_time_stats_update(j->blocked_time,
285 j->res_get_blocked_start);
286 j->res_get_blocked_start = 0;
288 mod_delayed_work(c->io_complete_wq,
290 msecs_to_jiffies(c->opts.journal_flush_delay));
295 static bool journal_quiesced(struct journal *j)
297 bool ret = atomic64_read(&j->seq) == j->seq_ondisk;
300 journal_entry_close(j);
304 static void journal_quiesce(struct journal *j)
306 wait_event(j->wait, journal_quiesced(j));
309 static void journal_write_work(struct work_struct *work)
311 struct journal *j = container_of(work, struct journal, write_work.work);
312 struct bch_fs *c = container_of(j, struct bch_fs, journal);
316 if (!__journal_entry_is_open(j->reservations))
319 delta = journal_cur_buf(j)->expires - jiffies;
322 mod_delayed_work(c->io_complete_wq, &j->write_work, delta);
324 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
326 spin_unlock(&j->lock);
329 static int __journal_res_get(struct journal *j, struct journal_res *res,
332 struct bch_fs *c = container_of(j, struct bch_fs, journal);
333 struct journal_buf *buf;
337 if (journal_res_get_fast(j, res, flags))
340 if (bch2_journal_error(j))
346 * Recheck after taking the lock, so we don't race with another thread
347 * that just did journal_entry_open() and call journal_entry_close()
350 if (journal_res_get_fast(j, res, flags)) {
351 spin_unlock(&j->lock);
355 if (!(flags & JOURNAL_RES_GET_RESERVED) &&
356 !test_bit(JOURNAL_MAY_GET_UNRESERVED, &j->flags)) {
358 * Don't want to close current journal entry, just need to
361 ret = cur_entry_journal_full;
366 * If we couldn't get a reservation because the current buf filled up,
367 * and we had room for a bigger entry on disk, signal that we want to
368 * realloc the journal bufs:
370 buf = journal_cur_buf(j);
371 if (journal_entry_is_open(j) &&
372 buf->buf_size >> 9 < buf->disk_sectors &&
373 buf->buf_size < JOURNAL_ENTRY_SIZE_MAX)
374 j->buf_size_want = max(j->buf_size_want, buf->buf_size << 1);
376 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
377 ret = journal_entry_open(j);
379 if (ret == cur_entry_max_in_flight)
380 trace_journal_entry_full(c);
382 if ((ret && ret != cur_entry_insufficient_devices) &&
383 !j->res_get_blocked_start) {
384 j->res_get_blocked_start = local_clock() ?: 1;
385 trace_journal_full(c);
388 can_discard = j->can_discard;
389 spin_unlock(&j->lock);
394 if ((ret == cur_entry_journal_full ||
395 ret == cur_entry_journal_pin_full) &&
397 !nr_unwritten_journal_entries(j) &&
398 (flags & JOURNAL_RES_GET_RESERVED)) {
399 struct printbuf buf = PRINTBUF;
401 bch_err(c, "Journal stuck! Hava a pre-reservation but journal full");
403 bch2_journal_debug_to_text(&buf, j);
404 bch_err(c, "%s", buf.buf);
406 printbuf_reset(&buf);
407 bch2_journal_pins_to_text(&buf, j);
408 bch_err(c, "Journal pins:\n%s", buf.buf);
416 * Journal is full - can't rely on reclaim from work item due to
419 if ((ret == cur_entry_journal_full ||
420 ret == cur_entry_journal_pin_full) &&
421 !(flags & JOURNAL_RES_GET_NONBLOCK)) {
423 bch2_journal_do_discards(j);
427 if (mutex_trylock(&j->reclaim_lock)) {
428 bch2_journal_reclaim(j);
429 mutex_unlock(&j->reclaim_lock);
433 return ret == cur_entry_insufficient_devices ? -EROFS : -EAGAIN;
437 * Essentially the entry function to the journaling code. When bcachefs is doing
438 * a btree insert, it calls this function to get the current journal write.
439 * Journal write is the structure used set up journal writes. The calling
440 * function will then add its keys to the structure, queuing them for the next
443 * To ensure forward progress, the current task must not be holding any
444 * btree node write locks.
446 int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
451 closure_wait_event(&j->async_wait,
452 (ret = __journal_res_get(j, res, flags)) != -EAGAIN ||
453 (flags & JOURNAL_RES_GET_NONBLOCK));
457 /* journal_preres: */
459 static bool journal_preres_available(struct journal *j,
460 struct journal_preres *res,
464 bool ret = bch2_journal_preres_get_fast(j, res, new_u64s, flags, true);
466 if (!ret && mutex_trylock(&j->reclaim_lock)) {
467 bch2_journal_reclaim(j);
468 mutex_unlock(&j->reclaim_lock);
474 int __bch2_journal_preres_get(struct journal *j,
475 struct journal_preres *res,
481 closure_wait_event(&j->preres_wait,
482 (ret = bch2_journal_error(j)) ||
483 journal_preres_available(j, res, new_u64s, flags));
487 /* journal_entry_res: */
489 void bch2_journal_entry_res_resize(struct journal *j,
490 struct journal_entry_res *res,
493 union journal_res_state state;
494 int d = new_u64s - res->u64s;
498 j->entry_u64s_reserved += d;
502 j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
504 state = READ_ONCE(j->reservations);
506 if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
507 state.cur_entry_offset > j->cur_entry_u64s) {
508 j->cur_entry_u64s += d;
510 * Not enough room in current journal entry, have to flush it:
512 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
514 journal_cur_buf(j)->u64s_reserved += d;
517 spin_unlock(&j->lock);
521 /* journal flushing: */
524 * bch2_journal_flush_seq_async - wait for a journal entry to be written
526 * like bch2_journal_wait_on_seq, except that it triggers a write immediately if
529 int bch2_journal_flush_seq_async(struct journal *j, u64 seq,
530 struct closure *parent)
532 struct journal_buf *buf;
535 if (seq <= j->flushed_seq_ondisk)
540 if (WARN_ONCE(seq > journal_cur_seq(j),
541 "requested to flush journal seq %llu, but currently at %llu",
542 seq, journal_cur_seq(j)))
545 /* Recheck under lock: */
546 if (j->err_seq && seq >= j->err_seq) {
551 if (seq <= j->flushed_seq_ondisk) {
556 /* if seq was written, but not flushed - flush a newer one instead */
557 seq = max(seq, journal_last_unwritten_seq(j));
560 if (seq > journal_cur_seq(j)) {
561 struct journal_res res = { 0 };
563 if (journal_entry_is_open(j))
564 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
566 spin_unlock(&j->lock);
568 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
573 buf = j->buf + (seq & JOURNAL_BUF_MASK);
574 buf->must_flush = true;
576 if (!buf->flush_time) {
577 buf->flush_time = local_clock() ?: 1;
578 buf->expires = jiffies;
581 if (parent && !closure_wait(&buf->wait, parent))
584 bch2_journal_res_put(j, &res);
591 * if write was kicked off without a flush, flush the next sequence
594 buf = journal_seq_to_buf(j, seq);
597 goto recheck_need_open;
600 buf->must_flush = true;
602 if (parent && !closure_wait(&buf->wait, parent))
605 if (seq == journal_cur_seq(j))
606 journal_entry_want_write(j);
608 spin_unlock(&j->lock);
612 int bch2_journal_flush_seq(struct journal *j, u64 seq)
614 u64 start_time = local_clock();
618 * Don't update time_stats when @seq is already flushed:
620 if (seq <= j->flushed_seq_ondisk)
623 ret = wait_event_interruptible(j->wait, (ret2 = bch2_journal_flush_seq_async(j, seq, NULL)));
626 bch2_time_stats_update(j->flush_seq_time, start_time);
628 return ret ?: ret2 < 0 ? ret2 : 0;
631 int bch2_journal_meta(struct journal *j)
633 struct journal_buf *buf;
634 struct journal_res res;
637 memset(&res, 0, sizeof(res));
639 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
643 buf = j->buf + (res.seq & JOURNAL_BUF_MASK);
644 buf->must_flush = true;
646 if (!buf->flush_time) {
647 buf->flush_time = local_clock() ?: 1;
648 buf->expires = jiffies;
651 bch2_journal_res_put(j, &res);
653 return bch2_journal_flush_seq(j, res.seq);
657 * bch2_journal_flush_async - if there is an open journal entry, or a journal
658 * still being written, write it and wait for the write to complete
660 void bch2_journal_flush_async(struct journal *j, struct closure *parent)
662 bch2_journal_flush_seq_async(j, atomic64_read(&j->seq), parent);
665 int bch2_journal_flush(struct journal *j)
667 return bch2_journal_flush_seq(j, atomic64_read(&j->seq));
671 * bch2_journal_noflush_seq - tell the journal not to issue any flushes before
674 bool bch2_journal_noflush_seq(struct journal *j, u64 seq)
676 struct bch_fs *c = container_of(j, struct bch_fs, journal);
680 if (!(c->sb.features & (1ULL << BCH_FEATURE_journal_no_flush)))
683 if (seq <= c->journal.flushed_seq_ondisk)
687 if (seq <= c->journal.flushed_seq_ondisk)
690 for (unwritten_seq = journal_last_unwritten_seq(j);
693 struct journal_buf *buf = journal_seq_to_buf(j, unwritten_seq);
695 /* journal write is already in flight, and was a flush write: */
696 if (unwritten_seq == journal_last_unwritten_seq(j) && !buf->noflush)
704 spin_unlock(&j->lock);
708 /* block/unlock the journal: */
710 void bch2_journal_unblock(struct journal *j)
714 spin_unlock(&j->lock);
719 void bch2_journal_block(struct journal *j)
723 spin_unlock(&j->lock);
728 /* allocate journal on a device: */
730 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
731 bool new_fs, struct closure *cl)
733 struct bch_fs *c = ca->fs;
734 struct journal_device *ja = &ca->journal;
735 struct bch_sb_field_journal *journal_buckets;
736 u64 *new_bucket_seq = NULL, *new_buckets = NULL;
739 /* don't handle reducing nr of buckets yet: */
743 new_buckets = kzalloc(nr * sizeof(u64), GFP_KERNEL);
744 new_bucket_seq = kzalloc(nr * sizeof(u64), GFP_KERNEL);
745 if (!new_buckets || !new_bucket_seq) {
750 journal_buckets = bch2_sb_resize_journal(&ca->disk_sb,
751 nr + sizeof(*journal_buckets) / sizeof(u64));
752 if (!journal_buckets) {
758 * We may be called from the device add path, before the new device has
759 * actually been added to the running filesystem:
762 spin_lock(&c->journal.lock);
764 memcpy(new_buckets, ja->buckets, ja->nr * sizeof(u64));
765 memcpy(new_bucket_seq, ja->bucket_seq, ja->nr * sizeof(u64));
766 swap(new_buckets, ja->buckets);
767 swap(new_bucket_seq, ja->bucket_seq);
770 spin_unlock(&c->journal.lock);
772 while (ja->nr < nr) {
773 struct open_bucket *ob = NULL;
778 b = bch2_bucket_alloc_new_fs(ca);
785 ob = bch2_bucket_alloc(c, ca, RESERVE_NONE,
789 ret = cl ? -EAGAIN : -ENOSPC;
797 spin_lock(&c->journal.lock);
801 * For resize at runtime, we should be writing the new
802 * superblock before inserting into the journal array
805 pos = ja->nr ? (ja->cur_idx + 1) % ja->nr : 0;
806 __array_insert_item(ja->buckets, ja->nr, pos);
807 __array_insert_item(ja->bucket_seq, ja->nr, pos);
808 __array_insert_item(journal_buckets->buckets, ja->nr, pos);
811 ja->buckets[pos] = b;
812 ja->bucket_seq[pos] = 0;
813 journal_buckets->buckets[pos] = cpu_to_le64(b);
815 if (pos <= ja->discard_idx)
816 ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
817 if (pos <= ja->dirty_idx_ondisk)
818 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
819 if (pos <= ja->dirty_idx)
820 ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
821 if (pos <= ja->cur_idx)
822 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
825 spin_unlock(&c->journal.lock);
828 ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
829 bch2_trans_mark_metadata_bucket(&trans, ca,
831 ca->mi.bucket_size));
833 bch2_open_bucket_put(c, ob);
840 bch2_sb_resize_journal(&ca->disk_sb,
841 ja->nr + sizeof(*journal_buckets) / sizeof(u64));
842 kfree(new_bucket_seq);
849 * Allocate more journal space at runtime - not currently making use if it, but
852 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
855 struct journal_device *ja = &ca->journal;
860 closure_init_stack(&cl);
863 struct disk_reservation disk_res = { 0, 0 };
867 mutex_lock(&c->sb_lock);
871 * note: journal buckets aren't really counted as _sectors_ used yet, so
872 * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
873 * when space used goes up without a reservation - but we do need the
874 * reservation to ensure we'll actually be able to allocate:
877 if (bch2_disk_reservation_get(c, &disk_res,
878 bucket_to_sector(ca, nr - ja->nr), 1, 0)) {
879 mutex_unlock(&c->sb_lock);
883 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
885 bch2_disk_reservation_put(c, &disk_res);
887 if (ja->nr != current_nr)
889 mutex_unlock(&c->sb_lock);
890 } while (ret == -EAGAIN);
895 int bch2_dev_journal_alloc(struct bch_dev *ca)
899 if (dynamic_fault("bcachefs:add:journal_alloc"))
902 /* 1/128th of the device by default: */
903 nr = ca->mi.nbuckets >> 7;
906 * clamp journal size to 8192 buckets or 8GB (in sectors), whichever
909 nr = clamp_t(unsigned, nr,
910 BCH_JOURNAL_BUCKETS_MIN,
912 (1 << 24) / ca->mi.bucket_size));
914 return __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
917 /* startup/shutdown: */
919 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
925 for (seq = journal_last_unwritten_seq(j);
926 seq <= journal_cur_seq(j) && !ret;
928 struct journal_buf *buf = journal_seq_to_buf(j, seq);
930 if (bch2_bkey_has_device(bkey_i_to_s_c(&buf->key), dev_idx))
933 spin_unlock(&j->lock);
938 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
940 wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
943 void bch2_fs_journal_stop(struct journal *j)
945 bch2_journal_reclaim_stop(j);
946 bch2_journal_flush_all_pins(j);
948 wait_event(j->wait, journal_entry_close(j));
951 * Always write a new journal entry, to make sure the clock hands are up
952 * to date (and match the superblock)
954 bch2_journal_meta(j);
958 BUG_ON(!bch2_journal_error(j) &&
959 test_bit(JOURNAL_REPLAY_DONE, &j->flags) &&
960 j->last_empty_seq != journal_cur_seq(j));
962 cancel_delayed_work_sync(&j->write_work);
965 int bch2_fs_journal_start(struct journal *j, u64 cur_seq,
966 struct list_head *journal_entries)
968 struct bch_fs *c = container_of(j, struct bch_fs, journal);
969 struct journal_entry_pin_list *p;
970 struct journal_replay *i;
971 u64 last_seq = cur_seq, nr, seq;
973 if (!list_empty(journal_entries))
974 last_seq = le64_to_cpu(list_last_entry(journal_entries,
975 struct journal_replay, list)->j.last_seq);
977 nr = cur_seq - last_seq;
979 if (nr + 1 > j->pin.size) {
981 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
983 bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
988 j->replay_journal_seq = last_seq;
989 j->replay_journal_seq_end = cur_seq;
990 j->last_seq_ondisk = last_seq;
991 j->flushed_seq_ondisk = cur_seq - 1;
992 j->seq_ondisk = cur_seq - 1;
993 j->pin.front = last_seq;
994 j->pin.back = cur_seq;
995 atomic64_set(&j->seq, cur_seq - 1);
997 if (list_empty(journal_entries))
998 j->last_empty_seq = cur_seq - 1;
1000 fifo_for_each_entry_ptr(p, &j->pin, seq)
1001 journal_pin_list_init(p, 1);
1003 list_for_each_entry(i, journal_entries, list) {
1006 seq = le64_to_cpu(i->j.seq);
1007 BUG_ON(seq >= cur_seq);
1012 if (journal_entry_empty(&i->j))
1013 j->last_empty_seq = le64_to_cpu(i->j.seq);
1015 p = journal_seq_pin(j, seq);
1018 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1019 bch2_dev_list_add_dev(&p->devs, i->ptrs[ptr].dev);
1022 if (list_empty(journal_entries))
1023 j->last_empty_seq = cur_seq;
1025 spin_lock(&j->lock);
1027 set_bit(JOURNAL_STARTED, &j->flags);
1028 j->last_flush_write = jiffies;
1030 j->reservations.idx = j->reservations.unwritten_idx = journal_cur_seq(j);
1031 j->reservations.unwritten_idx++;
1033 c->last_bucket_seq_cleanup = journal_cur_seq(j);
1035 bch2_journal_space_available(j);
1036 spin_unlock(&j->lock);
1038 return bch2_journal_reclaim_start(j);
1043 void bch2_dev_journal_exit(struct bch_dev *ca)
1045 kfree(ca->journal.bio);
1046 kfree(ca->journal.buckets);
1047 kfree(ca->journal.bucket_seq);
1049 ca->journal.bio = NULL;
1050 ca->journal.buckets = NULL;
1051 ca->journal.bucket_seq = NULL;
1054 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1056 struct journal_device *ja = &ca->journal;
1057 struct bch_sb_field_journal *journal_buckets =
1058 bch2_sb_get_journal(sb);
1061 ja->nr = bch2_nr_journal_buckets(journal_buckets);
1063 ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1064 if (!ja->bucket_seq)
1067 ca->journal.bio = bio_kmalloc(GFP_KERNEL,
1068 DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE));
1069 if (!ca->journal.bio)
1072 ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1076 for (i = 0; i < ja->nr; i++)
1077 ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1082 void bch2_fs_journal_exit(struct journal *j)
1086 for (i = 0; i < ARRAY_SIZE(j->buf); i++)
1087 kvpfree(j->buf[i].data, j->buf[i].buf_size);
1091 int bch2_fs_journal_init(struct journal *j)
1093 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1094 static struct lock_class_key res_key;
1098 pr_verbose_init(c->opts, "");
1100 spin_lock_init(&j->lock);
1101 spin_lock_init(&j->err_lock);
1102 init_waitqueue_head(&j->wait);
1103 INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1104 init_waitqueue_head(&j->reclaim_wait);
1105 init_waitqueue_head(&j->pin_flush_wait);
1106 mutex_init(&j->reclaim_lock);
1107 mutex_init(&j->discard_lock);
1109 lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1111 atomic64_set(&j->reservations.counter,
1112 ((union journal_res_state)
1113 { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1115 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL))) {
1120 for (i = 0; i < ARRAY_SIZE(j->buf); i++) {
1121 j->buf[i].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1122 j->buf[i].data = kvpmalloc(j->buf[i].buf_size, GFP_KERNEL);
1123 if (!j->buf[i].data) {
1129 j->pin.front = j->pin.back = 1;
1131 pr_verbose_init(c->opts, "ret %i", ret);
1137 void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1139 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1140 union journal_res_state s;
1142 unsigned long now = jiffies;
1147 out->tabstops[0] = 24;
1150 s = READ_ONCE(j->reservations);
1152 pr_buf(out, "dirty journal entries:\t%llu\n", fifo_used(&j->pin));
1153 pr_buf(out, "seq:\t\t\t%llu\n", journal_cur_seq(j));
1154 pr_buf(out, "seq_ondisk:\t\t%llu\n", j->seq_ondisk);
1155 pr_buf(out, "last_seq:\t\t%llu\n", journal_last_seq(j));
1156 pr_buf(out, "last_seq_ondisk:\t%llu\n", j->last_seq_ondisk);
1157 pr_buf(out, "flushed_seq_ondisk:\t%llu\n", j->flushed_seq_ondisk);
1158 pr_buf(out, "prereserved:\t\t%u/%u\n", j->prereserved.reserved, j->prereserved.remaining);
1159 pr_buf(out, "each entry reserved:\t%u\n", j->entry_u64s_reserved);
1160 pr_buf(out, "nr flush writes:\t%llu\n", j->nr_flush_writes);
1161 pr_buf(out, "nr noflush writes:\t%llu\n", j->nr_noflush_writes);
1162 pr_buf(out, "nr direct reclaim:\t%llu\n", j->nr_direct_reclaim);
1163 pr_buf(out, "nr background reclaim:\t%llu\n", j->nr_background_reclaim);
1164 pr_buf(out, "reclaim kicked:\t\t%u\n", j->reclaim_kicked);
1165 pr_buf(out, "reclaim runs in:\t%u ms\n", time_after(j->next_reclaim, now)
1166 ? jiffies_to_msecs(j->next_reclaim - jiffies) : 0);
1167 pr_buf(out, "current entry sectors:\t%u\n", j->cur_entry_sectors);
1168 pr_buf(out, "current entry error:\t%u\n", j->cur_entry_error);
1169 pr_buf(out, "current entry:\t\t");
1171 switch (s.cur_entry_offset) {
1172 case JOURNAL_ENTRY_ERROR_VAL:
1173 pr_buf(out, "error");
1175 case JOURNAL_ENTRY_CLOSED_VAL:
1176 pr_buf(out, "closed");
1179 pr_buf(out, "%u/%u", s.cur_entry_offset, j->cur_entry_u64s);
1185 for (seq = journal_cur_seq(j);
1186 seq >= journal_last_unwritten_seq(j);
1188 i = seq & JOURNAL_BUF_MASK;
1190 pr_buf(out, "unwritten entry:");
1192 pr_buf(out, "%llu", seq);
1194 pr_indent_push(out, 2);
1196 pr_buf(out, "refcount:");
1198 pr_buf(out, "%u", journal_state_count(s, i));
1201 pr_buf(out, "sectors:");
1203 pr_buf(out, "%u", j->buf[i].sectors);
1206 pr_buf(out, "expires");
1208 pr_buf(out, "%li jiffies", j->buf[i].expires - jiffies);
1211 pr_indent_pop(out, 2);
1215 "replay done:\t\t%i\n",
1216 test_bit(JOURNAL_REPLAY_DONE, &j->flags));
1218 pr_buf(out, "space:\n");
1219 pr_buf(out, "\tdiscarded\t%u:%u\n",
1220 j->space[journal_space_discarded].next_entry,
1221 j->space[journal_space_discarded].total);
1222 pr_buf(out, "\tclean ondisk\t%u:%u\n",
1223 j->space[journal_space_clean_ondisk].next_entry,
1224 j->space[journal_space_clean_ondisk].total);
1225 pr_buf(out, "\tclean\t\t%u:%u\n",
1226 j->space[journal_space_clean].next_entry,
1227 j->space[journal_space_clean].total);
1228 pr_buf(out, "\ttotal\t\t%u:%u\n",
1229 j->space[journal_space_total].next_entry,
1230 j->space[journal_space_total].total);
1232 for_each_member_device_rcu(ca, c, i,
1233 &c->rw_devs[BCH_DATA_journal]) {
1234 struct journal_device *ja = &ca->journal;
1236 if (!test_bit(ca->dev_idx, c->rw_devs[BCH_DATA_journal].d))
1242 pr_buf(out, "dev %u:\n", i);
1243 pr_buf(out, "\tnr\t\t%u\n", ja->nr);
1244 pr_buf(out, "\tbucket size\t%u\n", ca->mi.bucket_size);
1245 pr_buf(out, "\tavailable\t%u:%u\n", bch2_journal_dev_buckets_available(j, ja, journal_space_discarded), ja->sectors_free);
1246 pr_buf(out, "\tdiscard_idx\t%u\n", ja->discard_idx);
1247 pr_buf(out, "\tdirty_ondisk\t%u (seq %llu)\n", ja->dirty_idx_ondisk, ja->bucket_seq[ja->dirty_idx_ondisk]);
1248 pr_buf(out, "\tdirty_idx\t%u (seq %llu)\n", ja->dirty_idx, ja->bucket_seq[ja->dirty_idx]);
1249 pr_buf(out, "\tcur_idx\t\t%u (seq %llu)\n", ja->cur_idx, ja->bucket_seq[ja->cur_idx]);
1257 void bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1259 spin_lock(&j->lock);
1260 __bch2_journal_debug_to_text(out, j);
1261 spin_unlock(&j->lock);
1264 bool bch2_journal_seq_pins_to_text(struct printbuf *out, struct journal *j, u64 *seq)
1266 struct journal_entry_pin_list *pin_list;
1267 struct journal_entry_pin *pin;
1269 spin_lock(&j->lock);
1270 *seq = max(*seq, j->pin.front);
1272 if (*seq >= j->pin.back) {
1273 spin_unlock(&j->lock);
1279 pin_list = journal_seq_pin(j, *seq);
1281 pr_buf(out, "%llu: count %u", *seq, atomic_read(&pin_list->count));
1283 pr_indent_push(out, 2);
1285 list_for_each_entry(pin, &pin_list->list, list) {
1286 pr_buf(out, "\t%px %ps", pin, pin->flush);
1290 list_for_each_entry(pin, &pin_list->key_cache_list, list) {
1291 pr_buf(out, "\t%px %ps", pin, pin->flush);
1295 if (!list_empty(&pin_list->flushed)) {
1296 pr_buf(out, "flushed:");
1300 list_for_each_entry(pin, &pin_list->flushed, list) {
1301 pr_buf(out, "\t%px %ps", pin, pin->flush);
1305 pr_indent_pop(out, 2);
1308 spin_unlock(&j->lock);
1313 void bch2_journal_pins_to_text(struct printbuf *out, struct journal *j)
1317 while (!bch2_journal_seq_pins_to_text(out, j, &seq))