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"
14 #include "journal_io.h"
15 #include "journal_reclaim.h"
16 #include "journal_seq_blacklist.h"
19 #include <trace/events/bcachefs.h>
21 static bool __journal_entry_is_open(union journal_res_state state)
23 return state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL;
26 static bool journal_entry_is_open(struct journal *j)
28 return __journal_entry_is_open(j->reservations);
31 static void journal_pin_new_entry(struct journal *j, int count)
33 struct journal_entry_pin_list *p;
36 * The fifo_push() needs to happen at the same time as j->seq is
37 * incremented for journal_last_seq() to be calculated correctly
39 atomic64_inc(&j->seq);
40 p = fifo_push_ref(&j->pin);
42 INIT_LIST_HEAD(&p->list);
43 INIT_LIST_HEAD(&p->flushed);
44 atomic_set(&p->count, count);
48 static void bch2_journal_buf_init(struct journal *j)
50 struct journal_buf *buf = journal_cur_buf(j);
52 memset(buf->has_inode, 0, sizeof(buf->has_inode));
54 memset(buf->data, 0, sizeof(*buf->data));
55 buf->data->seq = cpu_to_le64(journal_cur_seq(j));
59 void bch2_journal_halt(struct journal *j)
61 union journal_res_state old, new;
62 u64 v = atomic64_read(&j->reservations.counter);
66 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL)
69 new.cur_entry_offset = JOURNAL_ENTRY_ERROR_VAL;
70 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
71 old.v, new.v)) != old.v);
74 closure_wake_up(&journal_cur_buf(j)->wait);
77 /* journal entry close/open: */
79 void __bch2_journal_buf_put(struct journal *j, bool need_write_just_set)
81 if (!need_write_just_set &&
82 test_bit(JOURNAL_NEED_WRITE, &j->flags))
83 bch2_time_stats_update(j->delay_time,
86 clear_bit(JOURNAL_NEED_WRITE, &j->flags);
88 closure_call(&j->io, bch2_journal_write, system_highpri_wq, NULL);
92 * Returns true if journal entry is now closed:
94 static bool __journal_entry_close(struct journal *j)
96 struct bch_fs *c = container_of(j, struct bch_fs, journal);
97 struct journal_buf *buf = journal_cur_buf(j);
98 union journal_res_state old, new;
99 u64 v = atomic64_read(&j->reservations.counter);
100 bool set_need_write = false;
103 lockdep_assert_held(&j->lock);
107 if (old.cur_entry_offset == JOURNAL_ENTRY_CLOSED_VAL)
110 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL) {
111 /* this entry will never be written: */
112 closure_wake_up(&buf->wait);
116 if (!test_bit(JOURNAL_NEED_WRITE, &j->flags)) {
117 set_bit(JOURNAL_NEED_WRITE, &j->flags);
118 j->need_write_time = local_clock();
119 set_need_write = true;
122 if (new.prev_buf_unwritten)
125 new.cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL;
127 new.prev_buf_unwritten = 1;
129 BUG_ON(journal_state_count(new, new.idx));
130 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
131 old.v, new.v)) != old.v);
133 buf->data->u64s = cpu_to_le32(old.cur_entry_offset);
135 sectors = vstruct_blocks_plus(buf->data, c->block_bits,
136 buf->u64s_reserved) << c->block_bits;
137 BUG_ON(sectors > buf->sectors);
138 buf->sectors = sectors;
140 bkey_extent_init(&buf->key);
143 * We have to set last_seq here, _before_ opening a new journal entry:
145 * A threads may replace an old pin with a new pin on their current
146 * journal reservation - the expectation being that the journal will
147 * contain either what the old pin protected or what the new pin
150 * After the old pin is dropped journal_last_seq() won't include the old
151 * pin, so we can only write the updated last_seq on the entry that
152 * contains whatever the new pin protects.
154 * Restated, we can _not_ update last_seq for a given entry if there
155 * could be a newer entry open with reservations/pins that have been
158 * Hence, we want update/set last_seq on the current journal entry right
159 * before we open a new one:
161 buf->data->last_seq = cpu_to_le64(journal_last_seq(j));
163 if (journal_entry_empty(buf->data))
164 clear_bit(JOURNAL_NOT_EMPTY, &j->flags);
166 set_bit(JOURNAL_NOT_EMPTY, &j->flags);
168 journal_pin_new_entry(j, 1);
170 bch2_journal_buf_init(j);
172 cancel_delayed_work(&j->write_work);
174 bch2_journal_space_available(j);
176 bch2_journal_buf_put(j, old.idx, set_need_write);
180 static bool journal_entry_close(struct journal *j)
185 ret = __journal_entry_close(j);
186 spin_unlock(&j->lock);
192 * should _only_ called from journal_res_get() - when we actually want a
193 * journal reservation - journal entry is open means journal is dirty:
197 * -ENOSPC: journal currently full, must invoke reclaim
198 * -EAGAIN: journal blocked, must wait
199 * -EROFS: insufficient rw devices or journal error
201 static int journal_entry_open(struct journal *j)
203 struct journal_buf *buf = journal_cur_buf(j);
204 union journal_res_state old, new;
208 lockdep_assert_held(&j->lock);
209 BUG_ON(journal_entry_is_open(j));
214 if (j->cur_entry_error)
215 return j->cur_entry_error;
217 BUG_ON(!j->cur_entry_sectors);
219 buf->u64s_reserved = j->entry_u64s_reserved;
220 buf->disk_sectors = j->cur_entry_sectors;
221 buf->sectors = min(buf->disk_sectors, buf->buf_size >> 9);
223 u64s = (int) (buf->sectors << 9) / sizeof(u64) -
224 journal_entry_overhead(j);
225 u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
227 if (u64s <= le32_to_cpu(buf->data->u64s))
231 * Must be set before marking the journal entry as open:
233 j->cur_entry_u64s = u64s;
235 v = atomic64_read(&j->reservations.counter);
239 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL)
242 /* Handle any already added entries */
243 new.cur_entry_offset = le32_to_cpu(buf->data->u64s);
245 EBUG_ON(journal_state_count(new, new.idx));
246 journal_state_inc(&new);
247 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
248 old.v, new.v)) != old.v);
250 if (j->res_get_blocked_start)
251 bch2_time_stats_update(j->blocked_time,
252 j->res_get_blocked_start);
253 j->res_get_blocked_start = 0;
255 mod_delayed_work(system_freezable_wq,
257 msecs_to_jiffies(j->write_delay_ms));
262 static bool journal_quiesced(struct journal *j)
264 union journal_res_state state = READ_ONCE(j->reservations);
265 bool ret = !state.prev_buf_unwritten && !__journal_entry_is_open(state);
268 journal_entry_close(j);
272 static void journal_quiesce(struct journal *j)
274 wait_event(j->wait, journal_quiesced(j));
277 static void journal_write_work(struct work_struct *work)
279 struct journal *j = container_of(work, struct journal, write_work.work);
281 journal_entry_close(j);
285 * Given an inode number, if that inode number has data in the journal that
286 * hasn't yet been flushed, return the journal sequence number that needs to be
289 u64 bch2_inode_journal_seq(struct journal *j, u64 inode)
291 size_t h = hash_64(inode, ilog2(sizeof(j->buf[0].has_inode) * 8));
294 if (!test_bit(h, j->buf[0].has_inode) &&
295 !test_bit(h, j->buf[1].has_inode))
299 if (test_bit(h, journal_cur_buf(j)->has_inode))
300 seq = journal_cur_seq(j);
301 else if (test_bit(h, journal_prev_buf(j)->has_inode))
302 seq = journal_cur_seq(j) - 1;
303 spin_unlock(&j->lock);
308 static int __journal_res_get(struct journal *j, struct journal_res *res,
311 struct bch_fs *c = container_of(j, struct bch_fs, journal);
312 struct journal_buf *buf;
316 if (journal_res_get_fast(j, res, flags))
319 if (bch2_journal_error(j))
325 * Recheck after taking the lock, so we don't race with another thread
326 * that just did journal_entry_open() and call journal_entry_close()
329 if (journal_res_get_fast(j, res, flags)) {
330 spin_unlock(&j->lock);
334 if (!(flags & JOURNAL_RES_GET_RESERVED) &&
335 !test_bit(JOURNAL_MAY_GET_UNRESERVED, &j->flags)) {
337 * Don't want to close current journal entry, just need to
345 * If we couldn't get a reservation because the current buf filled up,
346 * and we had room for a bigger entry on disk, signal that we want to
347 * realloc the journal bufs:
349 buf = journal_cur_buf(j);
350 if (journal_entry_is_open(j) &&
351 buf->buf_size >> 9 < buf->disk_sectors &&
352 buf->buf_size < JOURNAL_ENTRY_SIZE_MAX)
353 j->buf_size_want = max(j->buf_size_want, buf->buf_size << 1);
355 if (journal_entry_is_open(j) &&
356 !__journal_entry_close(j)) {
358 * We failed to get a reservation on the current open journal
359 * entry because it's full, and we can't close it because
360 * there's still a previous one in flight:
362 trace_journal_entry_full(c);
365 ret = journal_entry_open(j);
368 if ((ret == -EAGAIN || ret == -ENOSPC) &&
369 !j->res_get_blocked_start)
370 j->res_get_blocked_start = local_clock() ?: 1;
372 can_discard = j->can_discard;
373 spin_unlock(&j->lock);
378 if (ret == -ENOSPC) {
379 WARN_ONCE(!can_discard && (flags & JOURNAL_RES_GET_RESERVED),
380 "JOURNAL_RES_GET_RESERVED set but journal full");
383 * Journal is full - can't rely on reclaim from work item due to
386 trace_journal_full(c);
388 if (!(flags & JOURNAL_RES_GET_NONBLOCK)) {
390 bch2_journal_do_discards(j);
394 if (mutex_trylock(&j->reclaim_lock)) {
395 bch2_journal_reclaim(j);
396 mutex_unlock(&j->reclaim_lock);
407 * Essentially the entry function to the journaling code. When bcachefs is doing
408 * a btree insert, it calls this function to get the current journal write.
409 * Journal write is the structure used set up journal writes. The calling
410 * function will then add its keys to the structure, queuing them for the next
413 * To ensure forward progress, the current task must not be holding any
414 * btree node write locks.
416 int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
421 closure_wait_event(&j->async_wait,
422 (ret = __journal_res_get(j, res, flags)) != -EAGAIN ||
423 (flags & JOURNAL_RES_GET_NONBLOCK));
427 /* journal_preres: */
429 static bool journal_preres_available(struct journal *j,
430 struct journal_preres *res,
433 bool ret = bch2_journal_preres_get_fast(j, res, new_u64s);
436 bch2_journal_reclaim_work(&j->reclaim_work.work);
441 int __bch2_journal_preres_get(struct journal *j,
442 struct journal_preres *res,
447 closure_wait_event(&j->preres_wait,
448 (ret = bch2_journal_error(j)) ||
449 journal_preres_available(j, res, new_u64s));
453 /* journal_entry_res: */
455 void bch2_journal_entry_res_resize(struct journal *j,
456 struct journal_entry_res *res,
459 union journal_res_state state;
460 int d = new_u64s - res->u64s;
464 j->entry_u64s_reserved += d;
468 j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
470 state = READ_ONCE(j->reservations);
472 if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
473 state.cur_entry_offset > j->cur_entry_u64s) {
474 j->cur_entry_u64s += d;
476 * Not enough room in current journal entry, have to flush it:
478 __journal_entry_close(j);
480 journal_cur_buf(j)->u64s_reserved += d;
483 spin_unlock(&j->lock);
487 /* journal flushing: */
489 u64 bch2_journal_last_unwritten_seq(struct journal *j)
494 seq = journal_cur_seq(j);
495 if (j->reservations.prev_buf_unwritten)
497 spin_unlock(&j->lock);
503 * bch2_journal_open_seq_async - try to open a new journal entry if @seq isn't
504 * open yet, or wait if we cannot
506 * used by the btree interior update machinery, when it needs to write a new
507 * btree root - every journal entry contains the roots of all the btrees, so it
508 * doesn't need to bother with getting a journal reservation
510 int bch2_journal_open_seq_async(struct journal *j, u64 seq, struct closure *cl)
512 struct bch_fs *c = container_of(j, struct bch_fs, journal);
518 * Can't try to open more than one sequence number ahead:
520 BUG_ON(journal_cur_seq(j) < seq && !journal_entry_is_open(j));
522 if (journal_cur_seq(j) > seq ||
523 journal_entry_is_open(j)) {
524 spin_unlock(&j->lock);
528 if (journal_cur_seq(j) < seq &&
529 !__journal_entry_close(j)) {
530 /* haven't finished writing out the previous one: */
531 trace_journal_entry_full(c);
534 BUG_ON(journal_cur_seq(j) != seq);
536 ret = journal_entry_open(j);
539 if ((ret == -EAGAIN || ret == -ENOSPC) &&
540 !j->res_get_blocked_start)
541 j->res_get_blocked_start = local_clock() ?: 1;
543 if (ret == -EAGAIN || ret == -ENOSPC)
544 closure_wait(&j->async_wait, cl);
546 spin_unlock(&j->lock);
548 if (ret == -ENOSPC) {
549 trace_journal_full(c);
550 bch2_journal_reclaim_work(&j->reclaim_work.work);
557 static int journal_seq_error(struct journal *j, u64 seq)
559 union journal_res_state state = READ_ONCE(j->reservations);
561 if (seq == journal_cur_seq(j))
562 return bch2_journal_error(j);
564 if (seq + 1 == journal_cur_seq(j) &&
565 !state.prev_buf_unwritten &&
572 static inline struct journal_buf *
573 journal_seq_to_buf(struct journal *j, u64 seq)
575 /* seq should be for a journal entry that has been opened: */
576 BUG_ON(seq > journal_cur_seq(j));
577 BUG_ON(seq == journal_cur_seq(j) &&
578 j->reservations.cur_entry_offset == JOURNAL_ENTRY_CLOSED_VAL);
580 if (seq == journal_cur_seq(j))
581 return journal_cur_buf(j);
582 if (seq + 1 == journal_cur_seq(j) &&
583 j->reservations.prev_buf_unwritten)
584 return journal_prev_buf(j);
589 * bch2_journal_wait_on_seq - wait for a journal entry to be written
591 * does _not_ cause @seq to be written immediately - if there is no other
592 * activity to cause the relevant journal entry to be filled up or flushed it
593 * can wait for an arbitrary amount of time (up to @j->write_delay_ms, which is
596 void bch2_journal_wait_on_seq(struct journal *j, u64 seq,
597 struct closure *parent)
599 struct journal_buf *buf;
603 if ((buf = journal_seq_to_buf(j, seq))) {
604 if (!closure_wait(&buf->wait, parent))
607 if (seq == journal_cur_seq(j)) {
609 if (bch2_journal_error(j))
610 closure_wake_up(&buf->wait);
614 spin_unlock(&j->lock);
618 * bch2_journal_flush_seq_async - wait for a journal entry to be written
620 * like bch2_journal_wait_on_seq, except that it triggers a write immediately if
623 void bch2_journal_flush_seq_async(struct journal *j, u64 seq,
624 struct closure *parent)
626 struct journal_buf *buf;
631 (buf = journal_seq_to_buf(j, seq)))
632 if (!closure_wait(&buf->wait, parent))
635 if (seq == journal_cur_seq(j))
636 __journal_entry_close(j);
637 spin_unlock(&j->lock);
640 static int journal_seq_flushed(struct journal *j, u64 seq)
645 ret = seq <= j->seq_ondisk ? 1 : journal_seq_error(j, seq);
647 if (seq == journal_cur_seq(j))
648 __journal_entry_close(j);
649 spin_unlock(&j->lock);
654 int bch2_journal_flush_seq(struct journal *j, u64 seq)
656 u64 start_time = local_clock();
659 ret = wait_event_killable(j->wait, (ret2 = journal_seq_flushed(j, seq)));
661 bch2_time_stats_update(j->flush_seq_time, start_time);
663 return ret ?: ret2 < 0 ? ret2 : 0;
667 * bch2_journal_meta_async - force a journal entry to be written
669 void bch2_journal_meta_async(struct journal *j, struct closure *parent)
671 struct journal_res res;
673 memset(&res, 0, sizeof(res));
675 bch2_journal_res_get(j, &res, jset_u64s(0), 0);
676 bch2_journal_res_put(j, &res);
678 bch2_journal_flush_seq_async(j, res.seq, parent);
681 int bch2_journal_meta(struct journal *j)
683 struct journal_res res;
686 memset(&res, 0, sizeof(res));
688 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
692 bch2_journal_res_put(j, &res);
694 return bch2_journal_flush_seq(j, res.seq);
698 * bch2_journal_flush_async - if there is an open journal entry, or a journal
699 * still being written, write it and wait for the write to complete
701 void bch2_journal_flush_async(struct journal *j, struct closure *parent)
703 u64 seq, journal_seq;
706 journal_seq = journal_cur_seq(j);
708 if (journal_entry_is_open(j)) {
710 } else if (journal_seq) {
711 seq = journal_seq - 1;
713 spin_unlock(&j->lock);
716 spin_unlock(&j->lock);
718 bch2_journal_flush_seq_async(j, seq, parent);
721 int bch2_journal_flush(struct journal *j)
723 u64 seq, journal_seq;
726 journal_seq = journal_cur_seq(j);
728 if (journal_entry_is_open(j)) {
730 } else if (journal_seq) {
731 seq = journal_seq - 1;
733 spin_unlock(&j->lock);
736 spin_unlock(&j->lock);
738 return bch2_journal_flush_seq(j, seq);
741 /* block/unlock the journal: */
743 void bch2_journal_unblock(struct journal *j)
747 spin_unlock(&j->lock);
752 void bch2_journal_block(struct journal *j)
756 spin_unlock(&j->lock);
761 /* allocate journal on a device: */
763 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
764 bool new_fs, struct closure *cl)
766 struct bch_fs *c = ca->fs;
767 struct journal_device *ja = &ca->journal;
768 struct bch_sb_field_journal *journal_buckets;
769 u64 *new_bucket_seq = NULL, *new_buckets = NULL;
772 /* don't handle reducing nr of buckets yet: */
777 new_buckets = kzalloc(nr * sizeof(u64), GFP_KERNEL);
778 new_bucket_seq = kzalloc(nr * sizeof(u64), GFP_KERNEL);
779 if (!new_buckets || !new_bucket_seq)
782 journal_buckets = bch2_sb_resize_journal(&ca->disk_sb,
783 nr + sizeof(*journal_buckets) / sizeof(u64));
784 if (!journal_buckets)
788 * We may be called from the device add path, before the new device has
789 * actually been added to the running filesystem:
792 spin_lock(&c->journal.lock);
794 memcpy(new_buckets, ja->buckets, ja->nr * sizeof(u64));
795 memcpy(new_bucket_seq, ja->bucket_seq, ja->nr * sizeof(u64));
796 swap(new_buckets, ja->buckets);
797 swap(new_bucket_seq, ja->bucket_seq);
800 spin_unlock(&c->journal.lock);
802 while (ja->nr < nr) {
803 struct open_bucket *ob = NULL;
808 bucket = bch2_bucket_alloc_new_fs(ca);
814 ob = bch2_bucket_alloc(c, ca, RESERVE_ALLOC,
817 ret = cl ? -EAGAIN : -ENOSPC;
821 bucket = sector_to_bucket(ca, ob->ptr.offset);
825 percpu_down_read(&c->mark_lock);
826 spin_lock(&c->journal.lock);
829 pos = ja->nr ? (ja->cur_idx + 1) % ja->nr : 0;
830 __array_insert_item(ja->buckets, ja->nr, pos);
831 __array_insert_item(ja->bucket_seq, ja->nr, pos);
832 __array_insert_item(journal_buckets->buckets, ja->nr, pos);
835 ja->buckets[pos] = bucket;
836 ja->bucket_seq[pos] = 0;
837 journal_buckets->buckets[pos] = cpu_to_le64(bucket);
839 if (pos <= ja->discard_idx)
840 ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
841 if (pos <= ja->dirty_idx_ondisk)
842 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
843 if (pos <= ja->dirty_idx)
844 ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
845 if (pos <= ja->cur_idx)
846 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
848 bch2_mark_metadata_bucket(c, ca, bucket, BCH_DATA_JOURNAL,
850 gc_phase(GC_PHASE_SB),
854 spin_unlock(&c->journal.lock);
855 percpu_up_read(&c->mark_lock);
859 bch2_open_bucket_put(c, ob);
864 kfree(new_bucket_seq);
871 * Allocate more journal space at runtime - not currently making use if it, but
874 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
877 struct journal_device *ja = &ca->journal;
882 closure_init_stack(&cl);
885 struct disk_reservation disk_res = { 0, 0 };
889 mutex_lock(&c->sb_lock);
893 * note: journal buckets aren't really counted as _sectors_ used yet, so
894 * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
895 * when space used goes up without a reservation - but we do need the
896 * reservation to ensure we'll actually be able to allocate:
899 if (bch2_disk_reservation_get(c, &disk_res,
900 bucket_to_sector(ca, nr - ja->nr), 1, 0)) {
901 mutex_unlock(&c->sb_lock);
905 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
907 bch2_disk_reservation_put(c, &disk_res);
909 if (ja->nr != current_nr)
911 mutex_unlock(&c->sb_lock);
912 } while (ret == -EAGAIN);
917 int bch2_dev_journal_alloc(struct bch_dev *ca)
921 if (dynamic_fault("bcachefs:add:journal_alloc"))
925 * clamp journal size to 1024 buckets or 512MB (in sectors), whichever
928 nr = clamp_t(unsigned, ca->mi.nbuckets >> 8,
929 BCH_JOURNAL_BUCKETS_MIN,
931 (1 << 20) / ca->mi.bucket_size));
933 return __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
936 /* startup/shutdown: */
938 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
940 union journal_res_state state;
941 struct journal_buf *w;
945 state = READ_ONCE(j->reservations);
946 w = j->buf + !state.idx;
948 ret = state.prev_buf_unwritten &&
949 bch2_bkey_has_device(bkey_i_to_s_c(&w->key), dev_idx);
950 spin_unlock(&j->lock);
955 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
957 wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
960 void bch2_fs_journal_stop(struct journal *j)
962 bch2_journal_flush_all_pins(j);
964 wait_event(j->wait, journal_entry_close(j));
966 /* do we need to write another journal entry? */
967 if (test_bit(JOURNAL_NOT_EMPTY, &j->flags))
968 bch2_journal_meta(j);
972 BUG_ON(!bch2_journal_error(j) &&
973 test_bit(JOURNAL_NOT_EMPTY, &j->flags));
975 cancel_delayed_work_sync(&j->write_work);
976 cancel_delayed_work_sync(&j->reclaim_work);
979 int bch2_fs_journal_start(struct journal *j, u64 cur_seq,
980 struct list_head *journal_entries)
982 struct bch_fs *c = container_of(j, struct bch_fs, journal);
983 struct journal_entry_pin_list *p;
984 struct journal_replay *i;
985 u64 last_seq = cur_seq, nr, seq;
987 if (!list_empty(journal_entries))
988 last_seq = le64_to_cpu(list_first_entry(journal_entries,
989 struct journal_replay,
992 nr = cur_seq - last_seq;
994 if (nr + 1 > j->pin.size) {
996 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
998 bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
1003 j->replay_journal_seq = last_seq;
1004 j->replay_journal_seq_end = cur_seq;
1005 j->last_seq_ondisk = last_seq;
1006 j->pin.front = last_seq;
1007 j->pin.back = cur_seq;
1008 atomic64_set(&j->seq, cur_seq - 1);
1010 fifo_for_each_entry_ptr(p, &j->pin, seq) {
1011 INIT_LIST_HEAD(&p->list);
1012 INIT_LIST_HEAD(&p->flushed);
1013 atomic_set(&p->count, 1);
1017 list_for_each_entry(i, journal_entries, list) {
1018 seq = le64_to_cpu(i->j.seq);
1020 BUG_ON(seq < last_seq || seq >= cur_seq);
1022 journal_seq_pin(j, seq)->devs = i->devs;
1025 spin_lock(&j->lock);
1027 set_bit(JOURNAL_STARTED, &j->flags);
1029 journal_pin_new_entry(j, 1);
1030 bch2_journal_buf_init(j);
1032 c->last_bucket_seq_cleanup = journal_cur_seq(j);
1034 bch2_journal_space_available(j);
1035 spin_unlock(&j->lock);
1042 void bch2_dev_journal_exit(struct bch_dev *ca)
1044 kfree(ca->journal.bio);
1045 kfree(ca->journal.buckets);
1046 kfree(ca->journal.bucket_seq);
1048 ca->journal.bio = NULL;
1049 ca->journal.buckets = NULL;
1050 ca->journal.bucket_seq = NULL;
1053 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1055 struct journal_device *ja = &ca->journal;
1056 struct bch_sb_field_journal *journal_buckets =
1057 bch2_sb_get_journal(sb);
1060 ja->nr = bch2_nr_journal_buckets(journal_buckets);
1062 ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1063 if (!ja->bucket_seq)
1066 ca->journal.bio = bio_kmalloc(GFP_KERNEL,
1067 DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE));
1068 if (!ca->journal.bio)
1071 ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1075 for (i = 0; i < ja->nr; i++)
1076 ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1081 void bch2_fs_journal_exit(struct journal *j)
1083 kvpfree(j->buf[1].data, j->buf[1].buf_size);
1084 kvpfree(j->buf[0].data, j->buf[0].buf_size);
1088 int bch2_fs_journal_init(struct journal *j)
1090 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1091 static struct lock_class_key res_key;
1094 pr_verbose_init(c->opts, "");
1096 spin_lock_init(&j->lock);
1097 spin_lock_init(&j->err_lock);
1098 init_waitqueue_head(&j->wait);
1099 INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1100 INIT_DELAYED_WORK(&j->reclaim_work, bch2_journal_reclaim_work);
1101 init_waitqueue_head(&j->pin_flush_wait);
1102 mutex_init(&j->reclaim_lock);
1103 mutex_init(&j->discard_lock);
1105 lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1107 j->buf[0].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1108 j->buf[1].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1109 j->write_delay_ms = 1000;
1110 j->reclaim_delay_ms = 100;
1113 j->entry_u64s_reserved +=
1114 BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_EXTENT_U64s_MAX);
1116 atomic64_set(&j->reservations.counter,
1117 ((union journal_res_state)
1118 { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1120 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL)) ||
1121 !(j->buf[0].data = kvpmalloc(j->buf[0].buf_size, GFP_KERNEL)) ||
1122 !(j->buf[1].data = kvpmalloc(j->buf[1].buf_size, GFP_KERNEL))) {
1127 j->pin.front = j->pin.back = 1;
1129 pr_verbose_init(c->opts, "ret %i", ret);
1135 ssize_t bch2_journal_print_debug(struct journal *j, char *buf)
1137 struct printbuf out = _PBUF(buf, PAGE_SIZE);
1138 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1139 union journal_res_state s;
1144 spin_lock(&j->lock);
1145 s = READ_ONCE(j->reservations);
1148 "active journal entries:\t%llu\n"
1150 "last_seq:\t\t%llu\n"
1151 "last_seq_ondisk:\t%llu\n"
1152 "prereserved:\t\t%u/%u\n"
1153 "current entry sectors:\t%u\n"
1154 "current entry:\t\t",
1157 journal_last_seq(j),
1159 j->prereserved.reserved,
1160 j->prereserved.remaining,
1161 j->cur_entry_sectors);
1163 switch (s.cur_entry_offset) {
1164 case JOURNAL_ENTRY_ERROR_VAL:
1165 pr_buf(&out, "error\n");
1167 case JOURNAL_ENTRY_CLOSED_VAL:
1168 pr_buf(&out, "closed\n");
1171 pr_buf(&out, "%u/%u\n",
1178 "current entry refs:\t%u\n"
1179 "prev entry unwritten:\t",
1180 journal_state_count(s, s.idx));
1182 if (s.prev_buf_unwritten)
1183 pr_buf(&out, "yes, ref %u sectors %u\n",
1184 journal_state_count(s, !s.idx),
1185 journal_prev_buf(j)->sectors);
1187 pr_buf(&out, "no\n");
1190 "need write:\t\t%i\n"
1191 "replay done:\t\t%i\n",
1192 test_bit(JOURNAL_NEED_WRITE, &j->flags),
1193 test_bit(JOURNAL_REPLAY_DONE, &j->flags));
1195 for_each_member_device_rcu(ca, c, iter,
1196 &c->rw_devs[BCH_DATA_JOURNAL]) {
1197 struct journal_device *ja = &ca->journal;
1205 "\tavailable\t%u:%u\n"
1206 "\tdiscard_idx\t\t%u\n"
1207 "\tdirty_idx_ondisk\t%u (seq %llu)\n"
1208 "\tdirty_idx\t\t%u (seq %llu)\n"
1209 "\tcur_idx\t\t%u (seq %llu)\n",
1211 bch2_journal_dev_buckets_available(j, ja, journal_space_discarded),
1214 ja->dirty_idx_ondisk, ja->bucket_seq[ja->dirty_idx_ondisk],
1215 ja->dirty_idx, ja->bucket_seq[ja->dirty_idx],
1216 ja->cur_idx, ja->bucket_seq[ja->cur_idx]);
1219 spin_unlock(&j->lock);
1222 return out.pos - buf;
1225 ssize_t bch2_journal_print_pins(struct journal *j, char *buf)
1227 struct printbuf out = _PBUF(buf, PAGE_SIZE);
1228 struct journal_entry_pin_list *pin_list;
1229 struct journal_entry_pin *pin;
1232 spin_lock(&j->lock);
1233 fifo_for_each_entry_ptr(pin_list, &j->pin, i) {
1234 pr_buf(&out, "%llu: count %u\n",
1235 i, atomic_read(&pin_list->count));
1237 list_for_each_entry(pin, &pin_list->list, list)
1238 pr_buf(&out, "\t%px %ps\n",
1241 if (!list_empty(&pin_list->flushed))
1242 pr_buf(&out, "flushed:\n");
1244 list_for_each_entry(pin, &pin_list->flushed, list)
1245 pr_buf(&out, "\t%px %ps\n",
1248 spin_unlock(&j->lock);
1250 return out.pos - buf;