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"
15 #include "journal_io.h"
16 #include "journal_reclaim.h"
17 #include "journal_seq_blacklist.h"
20 #include <trace/events/bcachefs.h>
22 static u64 last_unwritten_seq(struct journal *j)
24 union journal_res_state s = READ_ONCE(j->reservations);
26 lockdep_assert_held(&j->lock);
28 return journal_cur_seq(j) - ((s.idx - s.unwritten_idx) & JOURNAL_BUF_MASK);
31 static inline bool journal_seq_unwritten(struct journal *j, u64 seq)
33 return seq >= last_unwritten_seq(j);
36 static bool __journal_entry_is_open(union journal_res_state state)
38 return state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL;
41 static bool journal_entry_is_open(struct journal *j)
43 return __journal_entry_is_open(j->reservations);
46 static inline struct journal_buf *
47 journal_seq_to_buf(struct journal *j, u64 seq)
49 struct journal_buf *buf = NULL;
51 EBUG_ON(seq > journal_cur_seq(j));
52 EBUG_ON(seq == journal_cur_seq(j) &&
53 j->reservations.cur_entry_offset == JOURNAL_ENTRY_CLOSED_VAL);
55 if (journal_seq_unwritten(j, seq)) {
56 buf = j->buf + (seq & JOURNAL_BUF_MASK);
57 EBUG_ON(le64_to_cpu(buf->data->seq) != seq);
62 static void journal_pin_new_entry(struct journal *j, int count)
64 struct journal_entry_pin_list *p;
67 * The fifo_push() needs to happen at the same time as j->seq is
68 * incremented for journal_last_seq() to be calculated correctly
70 atomic64_inc(&j->seq);
71 p = fifo_push_ref(&j->pin);
73 INIT_LIST_HEAD(&p->list);
74 INIT_LIST_HEAD(&p->flushed);
75 atomic_set(&p->count, count);
79 static void bch2_journal_buf_init(struct journal *j)
81 struct journal_buf *buf = journal_cur_buf(j);
83 bkey_extent_init(&buf->key);
85 buf->must_flush = false;
86 buf->separate_flush = false;
88 memset(buf->has_inode, 0, sizeof(buf->has_inode));
90 memset(buf->data, 0, sizeof(*buf->data));
91 buf->data->seq = cpu_to_le64(journal_cur_seq(j));
95 void bch2_journal_halt(struct journal *j)
97 union journal_res_state old, new;
98 u64 v = atomic64_read(&j->reservations.counter);
102 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL)
105 new.cur_entry_offset = JOURNAL_ENTRY_ERROR_VAL;
106 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
107 old.v, new.v)) != old.v);
109 j->err_seq = journal_cur_seq(j);
111 closure_wake_up(&journal_cur_buf(j)->wait);
114 /* journal entry close/open: */
116 void __bch2_journal_buf_put(struct journal *j)
118 closure_call(&j->io, bch2_journal_write, system_highpri_wq, NULL);
122 * Returns true if journal entry is now closed:
124 * We don't close a journal_buf until the next journal_buf is finished writing,
125 * and can be opened again - this also initializes the next journal_buf:
127 static bool __journal_entry_close(struct journal *j)
129 struct bch_fs *c = container_of(j, struct bch_fs, journal);
130 struct journal_buf *buf = journal_cur_buf(j);
131 union journal_res_state old, new;
132 u64 v = atomic64_read(&j->reservations.counter);
135 lockdep_assert_held(&j->lock);
139 if (old.cur_entry_offset == JOURNAL_ENTRY_CLOSED_VAL)
142 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL) {
143 /* this entry will never be written: */
144 closure_wake_up(&buf->wait);
148 if (!test_bit(JOURNAL_NEED_WRITE, &j->flags)) {
149 set_bit(JOURNAL_NEED_WRITE, &j->flags);
150 j->need_write_time = local_clock();
153 new.cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL;
156 if (new.idx == new.unwritten_idx)
159 BUG_ON(journal_state_count(new, new.idx));
160 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
161 old.v, new.v)) != old.v);
163 /* Close out old buffer: */
164 buf->data->u64s = cpu_to_le32(old.cur_entry_offset);
166 sectors = vstruct_blocks_plus(buf->data, c->block_bits,
167 buf->u64s_reserved) << c->block_bits;
168 BUG_ON(sectors > buf->sectors);
169 buf->sectors = sectors;
172 * We have to set last_seq here, _before_ opening a new journal entry:
174 * A threads may replace an old pin with a new pin on their current
175 * journal reservation - the expectation being that the journal will
176 * contain either what the old pin protected or what the new pin
179 * After the old pin is dropped journal_last_seq() won't include the old
180 * pin, so we can only write the updated last_seq on the entry that
181 * contains whatever the new pin protects.
183 * Restated, we can _not_ update last_seq for a given entry if there
184 * could be a newer entry open with reservations/pins that have been
187 * Hence, we want update/set last_seq on the current journal entry right
188 * before we open a new one:
190 buf->data->last_seq = cpu_to_le64(journal_last_seq(j));
192 __bch2_journal_pin_put(j, le64_to_cpu(buf->data->seq));
194 /* Initialize new buffer: */
195 journal_pin_new_entry(j, 1);
197 bch2_journal_buf_init(j);
199 cancel_delayed_work(&j->write_work);
200 clear_bit(JOURNAL_NEED_WRITE, &j->flags);
202 bch2_journal_space_available(j);
204 bch2_journal_buf_put(j, old.idx);
208 static bool journal_entry_want_write(struct journal *j)
210 union journal_res_state s = READ_ONCE(j->reservations);
214 * Don't close it yet if we already have a write in flight, but do set
217 if (s.idx != s.unwritten_idx)
218 set_bit(JOURNAL_NEED_WRITE, &j->flags);
220 ret = __journal_entry_close(j);
225 static bool journal_entry_close(struct journal *j)
230 ret = journal_entry_want_write(j);
231 spin_unlock(&j->lock);
237 * should _only_ called from journal_res_get() - when we actually want a
238 * journal reservation - journal entry is open means journal is dirty:
242 * -ENOSPC: journal currently full, must invoke reclaim
243 * -EAGAIN: journal blocked, must wait
244 * -EROFS: insufficient rw devices or journal error
246 static int journal_entry_open(struct journal *j)
248 struct bch_fs *c = container_of(j, struct bch_fs, journal);
249 struct journal_buf *buf = journal_cur_buf(j);
250 union journal_res_state old, new;
254 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
256 lockdep_assert_held(&j->lock);
257 BUG_ON(journal_entry_is_open(j));
260 return cur_entry_blocked;
262 if (j->cur_entry_error)
263 return j->cur_entry_error;
265 BUG_ON(!j->cur_entry_sectors);
267 buf->u64s_reserved = j->entry_u64s_reserved;
268 buf->disk_sectors = j->cur_entry_sectors;
269 buf->sectors = min(buf->disk_sectors, buf->buf_size >> 9);
271 u64s = (int) (buf->sectors << 9) / sizeof(u64) -
272 journal_entry_overhead(j);
273 u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
275 if (u64s <= le32_to_cpu(buf->data->u64s))
276 return cur_entry_journal_full;
279 * Must be set before marking the journal entry as open:
281 j->cur_entry_u64s = u64s;
283 v = atomic64_read(&j->reservations.counter);
287 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL)
288 return cur_entry_insufficient_devices;
290 /* Handle any already added entries */
291 new.cur_entry_offset = le32_to_cpu(buf->data->u64s);
293 EBUG_ON(journal_state_count(new, new.idx));
294 journal_state_inc(&new);
295 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
296 old.v, new.v)) != old.v);
298 if (j->res_get_blocked_start)
299 bch2_time_stats_update(j->blocked_time,
300 j->res_get_blocked_start);
301 j->res_get_blocked_start = 0;
303 mod_delayed_work(system_freezable_wq,
305 msecs_to_jiffies(j->write_delay_ms));
310 static bool journal_quiesced(struct journal *j)
312 union journal_res_state s = READ_ONCE(j->reservations);
313 bool ret = s.idx == s.unwritten_idx && !__journal_entry_is_open(s);
316 journal_entry_close(j);
320 static void journal_quiesce(struct journal *j)
322 wait_event(j->wait, journal_quiesced(j));
325 static void journal_write_work(struct work_struct *work)
327 struct journal *j = container_of(work, struct journal, write_work.work);
329 journal_entry_close(j);
333 * Given an inode number, if that inode number has data in the journal that
334 * hasn't yet been flushed, return the journal sequence number that needs to be
337 u64 bch2_inode_journal_seq(struct journal *j, u64 inode)
339 size_t h = hash_64(inode, ilog2(sizeof(j->buf[0].has_inode) * 8));
340 union journal_res_state s;
346 seq = journal_cur_seq(j);
347 s = READ_ONCE(j->reservations);
351 if (test_bit(h, j->buf[i].has_inode))
354 if (i == s.unwritten_idx)
357 i = (i - 1) & JOURNAL_BUF_MASK;
363 spin_unlock(&j->lock);
368 void bch2_journal_set_has_inum(struct journal *j, u64 inode, u64 seq)
370 size_t h = hash_64(inode, ilog2(sizeof(j->buf[0].has_inode) * 8));
371 struct journal_buf *buf;
375 if ((buf = journal_seq_to_buf(j, seq)))
376 set_bit(h, buf->has_inode);
378 spin_unlock(&j->lock);
381 static int __journal_res_get(struct journal *j, struct journal_res *res,
384 struct bch_fs *c = container_of(j, struct bch_fs, journal);
385 struct journal_buf *buf;
389 if (journal_res_get_fast(j, res, flags))
392 if (bch2_journal_error(j))
398 * Recheck after taking the lock, so we don't race with another thread
399 * that just did journal_entry_open() and call journal_entry_close()
402 if (journal_res_get_fast(j, res, flags)) {
403 spin_unlock(&j->lock);
407 if (!(flags & JOURNAL_RES_GET_RESERVED) &&
408 !test_bit(JOURNAL_MAY_GET_UNRESERVED, &j->flags)) {
410 * Don't want to close current journal entry, just need to
413 ret = cur_entry_journal_full;
418 * If we couldn't get a reservation because the current buf filled up,
419 * and we had room for a bigger entry on disk, signal that we want to
420 * realloc the journal bufs:
422 buf = journal_cur_buf(j);
423 if (journal_entry_is_open(j) &&
424 buf->buf_size >> 9 < buf->disk_sectors &&
425 buf->buf_size < JOURNAL_ENTRY_SIZE_MAX)
426 j->buf_size_want = max(j->buf_size_want, buf->buf_size << 1);
428 if (journal_entry_is_open(j) &&
429 !__journal_entry_close(j)) {
431 * We failed to get a reservation on the current open journal
432 * entry because it's full, and we can't close it because
433 * there's still a previous one in flight:
435 trace_journal_entry_full(c);
436 ret = cur_entry_blocked;
438 ret = journal_entry_open(j);
441 if ((ret && ret != cur_entry_insufficient_devices) &&
442 !j->res_get_blocked_start) {
443 j->res_get_blocked_start = local_clock() ?: 1;
444 trace_journal_full(c);
447 can_discard = j->can_discard;
448 spin_unlock(&j->lock);
454 * Journal is full - can't rely on reclaim from work item due to
457 if ((ret == cur_entry_journal_full ||
458 ret == cur_entry_journal_pin_full) &&
459 !(flags & JOURNAL_RES_GET_NONBLOCK)) {
461 bch2_journal_do_discards(j);
465 if (mutex_trylock(&j->reclaim_lock)) {
466 bch2_journal_reclaim(j);
467 mutex_unlock(&j->reclaim_lock);
471 return ret == cur_entry_insufficient_devices ? -EROFS : -EAGAIN;
475 * Essentially the entry function to the journaling code. When bcachefs is doing
476 * a btree insert, it calls this function to get the current journal write.
477 * Journal write is the structure used set up journal writes. The calling
478 * function will then add its keys to the structure, queuing them for the next
481 * To ensure forward progress, the current task must not be holding any
482 * btree node write locks.
484 int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
489 closure_wait_event(&j->async_wait,
490 (ret = __journal_res_get(j, res, flags)) != -EAGAIN ||
491 (flags & JOURNAL_RES_GET_NONBLOCK));
495 /* journal_preres: */
497 static bool journal_preres_available(struct journal *j,
498 struct journal_preres *res,
502 bool ret = bch2_journal_preres_get_fast(j, res, new_u64s, flags);
504 if (!ret && mutex_trylock(&j->reclaim_lock)) {
505 bch2_journal_reclaim(j);
506 mutex_unlock(&j->reclaim_lock);
512 int __bch2_journal_preres_get(struct journal *j,
513 struct journal_preres *res,
519 closure_wait_event(&j->preres_wait,
520 (ret = bch2_journal_error(j)) ||
521 journal_preres_available(j, res, new_u64s, flags));
525 /* journal_entry_res: */
527 void bch2_journal_entry_res_resize(struct journal *j,
528 struct journal_entry_res *res,
531 union journal_res_state state;
532 int d = new_u64s - res->u64s;
536 j->entry_u64s_reserved += d;
540 j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
542 state = READ_ONCE(j->reservations);
544 if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
545 state.cur_entry_offset > j->cur_entry_u64s) {
546 j->cur_entry_u64s += d;
548 * Not enough room in current journal entry, have to flush it:
550 __journal_entry_close(j);
552 journal_cur_buf(j)->u64s_reserved += d;
555 spin_unlock(&j->lock);
559 /* journal flushing: */
562 * bch2_journal_flush_seq_async - wait for a journal entry to be written
564 * like bch2_journal_wait_on_seq, except that it triggers a write immediately if
567 int bch2_journal_flush_seq_async(struct journal *j, u64 seq,
568 struct closure *parent)
570 struct journal_buf *buf;
573 if (seq <= j->flushed_seq_ondisk)
578 BUG_ON(seq > journal_cur_seq(j));
580 /* Recheck under lock: */
581 if (j->err_seq && seq >= j->err_seq) {
586 if (seq <= j->flushed_seq_ondisk) {
591 /* if seq was written, but not flushed - flush a newer one instead */
592 seq = max(seq, last_unwritten_seq(j));
595 if (seq == journal_cur_seq(j) && !journal_entry_is_open(j)) {
596 struct journal_res res = { 0 };
598 spin_unlock(&j->lock);
600 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
605 buf = j->buf + (seq & JOURNAL_BUF_MASK);
606 buf->must_flush = true;
607 set_bit(JOURNAL_NEED_WRITE, &j->flags);
609 if (parent && !closure_wait(&buf->wait, parent))
612 bch2_journal_res_put(j, &res);
619 * if write was kicked off without a flush, flush the next sequence
622 buf = journal_seq_to_buf(j, seq);
625 goto recheck_need_open;
628 buf->must_flush = true;
630 if (parent && !closure_wait(&buf->wait, parent))
633 if (seq == journal_cur_seq(j))
634 journal_entry_want_write(j);
636 spin_unlock(&j->lock);
640 int bch2_journal_flush_seq(struct journal *j, u64 seq)
642 u64 start_time = local_clock();
645 ret = wait_event_interruptible(j->wait, (ret2 = bch2_journal_flush_seq_async(j, seq, NULL)));
648 bch2_time_stats_update(j->flush_seq_time, start_time);
650 return ret ?: ret2 < 0 ? ret2 : 0;
653 int bch2_journal_meta(struct journal *j)
655 struct journal_res res;
658 memset(&res, 0, sizeof(res));
660 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
664 bch2_journal_res_put(j, &res);
666 return bch2_journal_flush_seq(j, res.seq);
670 * bch2_journal_flush_async - if there is an open journal entry, or a journal
671 * still being written, write it and wait for the write to complete
673 void bch2_journal_flush_async(struct journal *j, struct closure *parent)
675 u64 seq, journal_seq;
678 journal_seq = journal_cur_seq(j);
680 if (journal_entry_is_open(j)) {
682 } else if (journal_seq) {
683 seq = journal_seq - 1;
685 spin_unlock(&j->lock);
688 spin_unlock(&j->lock);
690 bch2_journal_flush_seq_async(j, seq, parent);
693 int bch2_journal_flush(struct journal *j)
695 u64 seq, journal_seq;
698 journal_seq = journal_cur_seq(j);
700 if (journal_entry_is_open(j)) {
702 } else if (journal_seq) {
703 seq = journal_seq - 1;
705 spin_unlock(&j->lock);
708 spin_unlock(&j->lock);
710 return bch2_journal_flush_seq(j, seq);
713 /* block/unlock the journal: */
715 void bch2_journal_unblock(struct journal *j)
719 spin_unlock(&j->lock);
724 void bch2_journal_block(struct journal *j)
728 spin_unlock(&j->lock);
733 /* allocate journal on a device: */
735 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
736 bool new_fs, struct closure *cl)
738 struct bch_fs *c = ca->fs;
739 struct journal_device *ja = &ca->journal;
740 struct bch_sb_field_journal *journal_buckets;
741 u64 *new_bucket_seq = NULL, *new_buckets = NULL;
744 /* don't handle reducing nr of buckets yet: */
748 new_buckets = kzalloc(nr * sizeof(u64), GFP_KERNEL);
749 new_bucket_seq = kzalloc(nr * sizeof(u64), GFP_KERNEL);
750 if (!new_buckets || !new_bucket_seq) {
755 journal_buckets = bch2_sb_resize_journal(&ca->disk_sb,
756 nr + sizeof(*journal_buckets) / sizeof(u64));
757 if (!journal_buckets) {
763 * We may be called from the device add path, before the new device has
764 * actually been added to the running filesystem:
767 spin_lock(&c->journal.lock);
769 memcpy(new_buckets, ja->buckets, ja->nr * sizeof(u64));
770 memcpy(new_bucket_seq, ja->bucket_seq, ja->nr * sizeof(u64));
771 swap(new_buckets, ja->buckets);
772 swap(new_bucket_seq, ja->bucket_seq);
775 spin_unlock(&c->journal.lock);
777 while (ja->nr < nr) {
778 struct open_bucket *ob = NULL;
783 bucket = bch2_bucket_alloc_new_fs(ca);
790 ob = bch2_bucket_alloc(c, ca, RESERVE_NONE,
794 ret = cl ? -EAGAIN : -ENOSPC;
798 bucket = sector_to_bucket(ca, ob->ptr.offset);
802 percpu_down_read(&c->mark_lock);
803 spin_lock(&c->journal.lock);
808 * For resize at runtime, we should be writing the new
809 * superblock before inserting into the journal array
812 pos = ja->nr ? (ja->cur_idx + 1) % ja->nr : 0;
813 __array_insert_item(ja->buckets, ja->nr, pos);
814 __array_insert_item(ja->bucket_seq, ja->nr, pos);
815 __array_insert_item(journal_buckets->buckets, ja->nr, pos);
818 ja->buckets[pos] = bucket;
819 ja->bucket_seq[pos] = 0;
820 journal_buckets->buckets[pos] = cpu_to_le64(bucket);
822 if (pos <= ja->discard_idx)
823 ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
824 if (pos <= ja->dirty_idx_ondisk)
825 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
826 if (pos <= ja->dirty_idx)
827 ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
828 if (pos <= ja->cur_idx)
829 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
832 bch2_mark_metadata_bucket(c, ca, bucket, BCH_DATA_journal,
834 gc_phase(GC_PHASE_SB),
838 spin_unlock(&c->journal.lock);
839 percpu_up_read(&c->mark_lock);
843 ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
844 bch2_trans_mark_metadata_bucket(&trans, NULL, ca,
845 bucket, BCH_DATA_journal,
846 ca->mi.bucket_size));
849 bch2_open_bucket_put(c, ob);
855 bch2_sb_resize_journal(&ca->disk_sb,
856 ja->nr + sizeof(*journal_buckets) / sizeof(u64));
857 kfree(new_bucket_seq);
864 * Allocate more journal space at runtime - not currently making use if it, but
867 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
870 struct journal_device *ja = &ca->journal;
875 closure_init_stack(&cl);
878 struct disk_reservation disk_res = { 0, 0 };
882 mutex_lock(&c->sb_lock);
886 * note: journal buckets aren't really counted as _sectors_ used yet, so
887 * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
888 * when space used goes up without a reservation - but we do need the
889 * reservation to ensure we'll actually be able to allocate:
892 if (bch2_disk_reservation_get(c, &disk_res,
893 bucket_to_sector(ca, nr - ja->nr), 1, 0)) {
894 mutex_unlock(&c->sb_lock);
898 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
900 bch2_disk_reservation_put(c, &disk_res);
902 if (ja->nr != current_nr)
904 mutex_unlock(&c->sb_lock);
905 } while (ret == -EAGAIN);
910 int bch2_dev_journal_alloc(struct bch_dev *ca)
914 if (dynamic_fault("bcachefs:add:journal_alloc"))
918 * clamp journal size to 1024 buckets or 512MB (in sectors), whichever
921 nr = clamp_t(unsigned, ca->mi.nbuckets >> 8,
922 BCH_JOURNAL_BUCKETS_MIN,
924 (1 << 20) / ca->mi.bucket_size));
926 return __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
929 /* startup/shutdown: */
931 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
933 union journal_res_state state;
938 state = READ_ONCE(j->reservations);
941 while (i != state.unwritten_idx) {
942 i = (i - 1) & JOURNAL_BUF_MASK;
943 if (bch2_bkey_has_device(bkey_i_to_s_c(&j->buf[i].key), dev_idx))
946 spin_unlock(&j->lock);
951 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
953 wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
956 void bch2_fs_journal_stop(struct journal *j)
958 bch2_journal_flush_all_pins(j);
960 wait_event(j->wait, journal_entry_close(j));
963 * Always write a new journal entry, to make sure the clock hands are up
964 * to date (and match the superblock)
966 bch2_journal_meta(j);
970 BUG_ON(!bch2_journal_error(j) &&
971 test_bit(JOURNAL_REPLAY_DONE, &j->flags) &&
972 (journal_entry_is_open(j) ||
973 j->last_empty_seq + 1 != journal_cur_seq(j)));
975 cancel_delayed_work_sync(&j->write_work);
976 bch2_journal_reclaim_stop(j);
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_last_entry(journal_entries,
989 struct journal_replay, list)->j.last_seq);
991 nr = cur_seq - last_seq;
993 if (nr + 1 > j->pin.size) {
995 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
997 bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
1002 j->replay_journal_seq = last_seq;
1003 j->replay_journal_seq_end = cur_seq;
1004 j->last_seq_ondisk = last_seq;
1005 j->pin.front = last_seq;
1006 j->pin.back = cur_seq;
1007 atomic64_set(&j->seq, cur_seq - 1);
1009 fifo_for_each_entry_ptr(p, &j->pin, seq) {
1010 INIT_LIST_HEAD(&p->list);
1011 INIT_LIST_HEAD(&p->flushed);
1012 atomic_set(&p->count, 1);
1016 list_for_each_entry(i, journal_entries, list) {
1019 seq = le64_to_cpu(i->j.seq);
1020 BUG_ON(seq >= cur_seq);
1025 p = journal_seq_pin(j, seq);
1028 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1029 bch2_dev_list_add_dev(&p->devs, i->ptrs[ptr].dev);
1032 spin_lock(&j->lock);
1034 set_bit(JOURNAL_STARTED, &j->flags);
1035 j->last_flush_write = jiffies;
1037 journal_pin_new_entry(j, 1);
1039 j->reservations.idx = j->reservations.unwritten_idx = journal_cur_seq(j);
1041 bch2_journal_buf_init(j);
1043 c->last_bucket_seq_cleanup = journal_cur_seq(j);
1045 bch2_journal_space_available(j);
1046 spin_unlock(&j->lock);
1053 void bch2_dev_journal_exit(struct bch_dev *ca)
1055 kfree(ca->journal.bio);
1056 kfree(ca->journal.buckets);
1057 kfree(ca->journal.bucket_seq);
1059 ca->journal.bio = NULL;
1060 ca->journal.buckets = NULL;
1061 ca->journal.bucket_seq = NULL;
1064 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1066 struct journal_device *ja = &ca->journal;
1067 struct bch_sb_field_journal *journal_buckets =
1068 bch2_sb_get_journal(sb);
1071 ja->nr = bch2_nr_journal_buckets(journal_buckets);
1073 ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1074 if (!ja->bucket_seq)
1077 ca->journal.bio = bio_kmalloc(GFP_KERNEL,
1078 DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE));
1079 if (!ca->journal.bio)
1082 ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1086 for (i = 0; i < ja->nr; i++)
1087 ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1092 void bch2_fs_journal_exit(struct journal *j)
1096 for (i = 0; i < ARRAY_SIZE(j->buf); i++)
1097 kvpfree(j->buf[i].data, j->buf[i].buf_size);
1101 int bch2_fs_journal_init(struct journal *j)
1103 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1104 static struct lock_class_key res_key;
1108 pr_verbose_init(c->opts, "");
1110 spin_lock_init(&j->lock);
1111 spin_lock_init(&j->err_lock);
1112 init_waitqueue_head(&j->wait);
1113 INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1114 init_waitqueue_head(&j->pin_flush_wait);
1115 mutex_init(&j->reclaim_lock);
1116 mutex_init(&j->discard_lock);
1118 lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1120 j->write_delay_ms = 1000;
1121 j->reclaim_delay_ms = 100;
1123 atomic64_set(&j->reservations.counter,
1124 ((union journal_res_state)
1125 { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1127 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL))) {
1132 for (i = 0; i < ARRAY_SIZE(j->buf); i++) {
1133 j->buf[i].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1134 j->buf[i].data = kvpmalloc(j->buf[i].buf_size, GFP_KERNEL);
1135 if (!j->buf[i].data) {
1141 j->pin.front = j->pin.back = 1;
1143 pr_verbose_init(c->opts, "ret %i", ret);
1149 void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1151 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1152 union journal_res_state s;
1157 s = READ_ONCE(j->reservations);
1160 "active journal entries:\t%llu\n"
1162 "last_seq:\t\t%llu\n"
1163 "last_seq_ondisk:\t%llu\n"
1164 "flushed_seq_ondisk:\t%llu\n"
1165 "prereserved:\t\t%u/%u\n"
1166 "nr flush writes:\t%llu\n"
1167 "nr noflush writes:\t%llu\n"
1168 "nr direct reclaim:\t%llu\n"
1169 "nr background reclaim:\t%llu\n"
1170 "current entry sectors:\t%u\n"
1171 "current entry error:\t%u\n"
1172 "current entry:\t\t",
1175 journal_last_seq(j),
1177 j->flushed_seq_ondisk,
1178 j->prereserved.reserved,
1179 j->prereserved.remaining,
1181 j->nr_noflush_writes,
1182 j->nr_direct_reclaim,
1183 j->nr_background_reclaim,
1184 j->cur_entry_sectors,
1185 j->cur_entry_error);
1187 switch (s.cur_entry_offset) {
1188 case JOURNAL_ENTRY_ERROR_VAL:
1189 pr_buf(out, "error\n");
1191 case JOURNAL_ENTRY_CLOSED_VAL:
1192 pr_buf(out, "closed\n");
1195 pr_buf(out, "%u/%u\n",
1202 "current entry:\t\tidx %u refcount %u\n",
1203 s.idx, journal_state_count(s, s.idx));
1206 while (i != s.unwritten_idx) {
1207 i = (i - 1) & JOURNAL_BUF_MASK;
1209 pr_buf(out, "unwritten entry:\tidx %u refcount %u sectors %u\n",
1210 i, journal_state_count(s, i), j->buf[i].sectors);
1214 "need write:\t\t%i\n"
1215 "replay done:\t\t%i\n",
1216 test_bit(JOURNAL_NEED_WRITE, &j->flags),
1217 test_bit(JOURNAL_REPLAY_DONE, &j->flags));
1219 pr_buf(out, "space:\n");
1220 pr_buf(out, "\tdiscarded\t%u:%u\n",
1221 j->space[journal_space_discarded].next_entry,
1222 j->space[journal_space_discarded].total);
1223 pr_buf(out, "\tclean ondisk\t%u:%u\n",
1224 j->space[journal_space_clean_ondisk].next_entry,
1225 j->space[journal_space_clean_ondisk].total);
1226 pr_buf(out, "\tclean\t\t%u:%u\n",
1227 j->space[journal_space_clean].next_entry,
1228 j->space[journal_space_clean].total);
1229 pr_buf(out, "\ttotal\t\t%u:%u\n",
1230 j->space[journal_space_total].next_entry,
1231 j->space[journal_space_total].total);
1233 for_each_member_device_rcu(ca, c, i,
1234 &c->rw_devs[BCH_DATA_journal]) {
1235 struct journal_device *ja = &ca->journal;
1243 "\tbucket size\t%u\n"
1244 "\tavailable\t%u:%u\n"
1245 "\tdiscard_idx\t%u\n"
1246 "\tdirty_ondisk\t%u (seq %llu)\n"
1247 "\tdirty_idx\t%u (seq %llu)\n"
1248 "\tcur_idx\t\t%u (seq %llu)\n",
1249 i, ja->nr, ca->mi.bucket_size,
1250 bch2_journal_dev_buckets_available(j, ja, journal_space_discarded),
1253 ja->dirty_idx_ondisk, ja->bucket_seq[ja->dirty_idx_ondisk],
1254 ja->dirty_idx, ja->bucket_seq[ja->dirty_idx],
1255 ja->cur_idx, ja->bucket_seq[ja->cur_idx]);
1261 void bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1263 spin_lock(&j->lock);
1264 __bch2_journal_debug_to_text(out, j);
1265 spin_unlock(&j->lock);
1268 void bch2_journal_pins_to_text(struct printbuf *out, struct journal *j)
1270 struct journal_entry_pin_list *pin_list;
1271 struct journal_entry_pin *pin;
1274 spin_lock(&j->lock);
1275 fifo_for_each_entry_ptr(pin_list, &j->pin, i) {
1276 pr_buf(out, "%llu: count %u\n",
1277 i, atomic_read(&pin_list->count));
1279 list_for_each_entry(pin, &pin_list->list, list)
1280 pr_buf(out, "\t%px %ps\n",
1283 if (!list_empty(&pin_list->flushed))
1284 pr_buf(out, "flushed:\n");
1286 list_for_each_entry(pin, &pin_list->flushed, list)
1287 pr_buf(out, "\t%px %ps\n",
1290 spin_unlock(&j->lock);