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 u64 last_unwritten_seq(struct journal *j)
25 union journal_res_state s = READ_ONCE(j->reservations);
27 lockdep_assert_held(&j->lock);
29 return journal_cur_seq(j) - ((s.idx - s.unwritten_idx) & JOURNAL_BUF_MASK);
32 static inline bool journal_seq_unwritten(struct journal *j, u64 seq)
34 return seq >= last_unwritten_seq(j);
37 static bool __journal_entry_is_open(union journal_res_state state)
39 return state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL;
42 static bool journal_entry_is_open(struct journal *j)
44 return __journal_entry_is_open(j->reservations);
47 static inline struct journal_buf *
48 journal_seq_to_buf(struct journal *j, u64 seq)
50 struct journal_buf *buf = NULL;
52 EBUG_ON(seq > journal_cur_seq(j));
53 EBUG_ON(seq == journal_cur_seq(j) &&
54 j->reservations.cur_entry_offset == JOURNAL_ENTRY_CLOSED_VAL);
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);
63 static void journal_pin_list_init(struct journal_entry_pin_list *p, int count)
65 INIT_LIST_HEAD(&p->list);
66 INIT_LIST_HEAD(&p->key_cache_list);
67 INIT_LIST_HEAD(&p->flushed);
68 atomic_set(&p->count, count);
72 static void journal_pin_new_entry(struct journal *j)
75 * The fifo_push() needs to happen at the same time as j->seq is
76 * incremented for journal_last_seq() to be calculated correctly
78 atomic64_inc(&j->seq);
79 journal_pin_list_init(fifo_push_ref(&j->pin), 1);
82 static void bch2_journal_buf_init(struct journal *j)
84 struct journal_buf *buf = journal_cur_buf(j);
86 bkey_extent_init(&buf->key);
88 buf->must_flush = false;
89 buf->separate_flush = false;
91 memset(buf->has_inode, 0, sizeof(buf->has_inode));
93 memset(buf->data, 0, sizeof(*buf->data));
94 buf->data->seq = cpu_to_le64(journal_cur_seq(j));
98 void bch2_journal_halt(struct journal *j)
100 union journal_res_state old, new;
101 u64 v = atomic64_read(&j->reservations.counter);
105 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL)
108 new.cur_entry_offset = JOURNAL_ENTRY_ERROR_VAL;
109 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
110 old.v, new.v)) != old.v);
112 j->err_seq = journal_cur_seq(j);
114 closure_wake_up(&journal_cur_buf(j)->wait);
117 /* journal entry close/open: */
119 void __bch2_journal_buf_put(struct journal *j)
121 struct bch_fs *c = container_of(j, struct bch_fs, journal);
123 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
127 * Returns true if journal entry is now closed:
129 * We don't close a journal_buf until the next journal_buf is finished writing,
130 * and can be opened again - this also initializes the next journal_buf:
132 static bool __journal_entry_close(struct journal *j)
134 struct bch_fs *c = container_of(j, struct bch_fs, journal);
135 struct journal_buf *buf = journal_cur_buf(j);
136 union journal_res_state old, new;
137 u64 v = atomic64_read(&j->reservations.counter);
140 lockdep_assert_held(&j->lock);
144 if (old.cur_entry_offset == JOURNAL_ENTRY_CLOSED_VAL)
147 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL) {
148 /* this entry will never be written: */
149 closure_wake_up(&buf->wait);
153 if (!test_bit(JOURNAL_NEED_WRITE, &j->flags)) {
154 set_bit(JOURNAL_NEED_WRITE, &j->flags);
155 j->need_write_time = local_clock();
158 new.cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL;
161 if (new.idx == new.unwritten_idx)
164 BUG_ON(journal_state_count(new, new.idx));
165 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
166 old.v, new.v)) != old.v);
168 /* Close out old buffer: */
169 buf->data->u64s = cpu_to_le32(old.cur_entry_offset);
171 sectors = vstruct_blocks_plus(buf->data, c->block_bits,
172 buf->u64s_reserved) << c->block_bits;
173 BUG_ON(sectors > buf->sectors);
174 buf->sectors = sectors;
177 * We have to set last_seq here, _before_ opening a new journal entry:
179 * A threads may replace an old pin with a new pin on their current
180 * journal reservation - the expectation being that the journal will
181 * contain either what the old pin protected or what the new pin
184 * After the old pin is dropped journal_last_seq() won't include the old
185 * pin, so we can only write the updated last_seq on the entry that
186 * contains whatever the new pin protects.
188 * Restated, we can _not_ update last_seq for a given entry if there
189 * could be a newer entry open with reservations/pins that have been
192 * Hence, we want update/set last_seq on the current journal entry right
193 * before we open a new one:
195 buf->last_seq = journal_last_seq(j);
196 buf->data->last_seq = cpu_to_le64(buf->last_seq);
198 __bch2_journal_pin_put(j, le64_to_cpu(buf->data->seq));
200 /* Initialize new buffer: */
201 journal_pin_new_entry(j);
203 bch2_journal_buf_init(j);
205 cancel_delayed_work(&j->write_work);
206 clear_bit(JOURNAL_NEED_WRITE, &j->flags);
208 bch2_journal_space_available(j);
210 bch2_journal_buf_put(j, old.idx);
214 static bool journal_entry_want_write(struct journal *j)
216 union journal_res_state s = READ_ONCE(j->reservations);
220 * Don't close it yet if we already have a write in flight, but do set
223 if (s.idx != s.unwritten_idx)
224 set_bit(JOURNAL_NEED_WRITE, &j->flags);
226 ret = __journal_entry_close(j);
231 static bool journal_entry_close(struct journal *j)
236 ret = journal_entry_want_write(j);
237 spin_unlock(&j->lock);
243 * should _only_ called from journal_res_get() - when we actually want a
244 * journal reservation - journal entry is open means journal is dirty:
248 * -ENOSPC: journal currently full, must invoke reclaim
249 * -EAGAIN: journal blocked, must wait
250 * -EROFS: insufficient rw devices or journal error
252 static int journal_entry_open(struct journal *j)
254 struct bch_fs *c = container_of(j, struct bch_fs, journal);
255 struct journal_buf *buf = journal_cur_buf(j);
256 union journal_res_state old, new;
260 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
262 lockdep_assert_held(&j->lock);
263 BUG_ON(journal_entry_is_open(j));
266 return cur_entry_blocked;
268 if (j->cur_entry_error)
269 return j->cur_entry_error;
271 BUG_ON(!j->cur_entry_sectors);
273 buf->u64s_reserved = j->entry_u64s_reserved;
274 buf->disk_sectors = j->cur_entry_sectors;
275 buf->sectors = min(buf->disk_sectors, buf->buf_size >> 9);
277 u64s = (int) (buf->sectors << 9) / sizeof(u64) -
278 journal_entry_overhead(j);
279 u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
281 if (u64s <= le32_to_cpu(buf->data->u64s))
282 return cur_entry_journal_full;
285 * Must be set before marking the journal entry as open:
287 j->cur_entry_u64s = u64s;
289 v = atomic64_read(&j->reservations.counter);
293 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL)
294 return cur_entry_insufficient_devices;
296 /* Handle any already added entries */
297 new.cur_entry_offset = le32_to_cpu(buf->data->u64s);
299 EBUG_ON(journal_state_count(new, new.idx));
300 journal_state_inc(&new);
301 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
302 old.v, new.v)) != old.v);
304 if (j->res_get_blocked_start)
305 bch2_time_stats_update(j->blocked_time,
306 j->res_get_blocked_start);
307 j->res_get_blocked_start = 0;
309 mod_delayed_work(c->io_complete_wq,
311 msecs_to_jiffies(j->write_delay_ms));
316 static bool journal_quiesced(struct journal *j)
318 union journal_res_state s = READ_ONCE(j->reservations);
319 bool ret = s.idx == s.unwritten_idx && !__journal_entry_is_open(s);
322 journal_entry_close(j);
326 static void journal_quiesce(struct journal *j)
328 wait_event(j->wait, journal_quiesced(j));
331 static void journal_write_work(struct work_struct *work)
333 struct journal *j = container_of(work, struct journal, write_work.work);
335 journal_entry_close(j);
339 * Given an inode number, if that inode number has data in the journal that
340 * hasn't yet been flushed, return the journal sequence number that needs to be
343 u64 bch2_inode_journal_seq(struct journal *j, u64 inode)
345 size_t h = hash_64(inode, ilog2(sizeof(j->buf[0].has_inode) * 8));
346 union journal_res_state s;
352 seq = journal_cur_seq(j);
353 s = READ_ONCE(j->reservations);
357 if (test_bit(h, j->buf[i].has_inode))
360 if (i == s.unwritten_idx)
363 i = (i - 1) & JOURNAL_BUF_MASK;
369 spin_unlock(&j->lock);
374 void bch2_journal_set_has_inum(struct journal *j, u64 inode, u64 seq)
376 size_t h = hash_64(inode, ilog2(sizeof(j->buf[0].has_inode) * 8));
377 struct journal_buf *buf;
381 if ((buf = journal_seq_to_buf(j, seq)))
382 set_bit(h, buf->has_inode);
384 spin_unlock(&j->lock);
387 static int __journal_res_get(struct journal *j, struct journal_res *res,
390 struct bch_fs *c = container_of(j, struct bch_fs, journal);
391 struct journal_buf *buf;
395 if (journal_res_get_fast(j, res, flags))
398 if (bch2_journal_error(j))
404 * Recheck after taking the lock, so we don't race with another thread
405 * that just did journal_entry_open() and call journal_entry_close()
408 if (journal_res_get_fast(j, res, flags)) {
409 spin_unlock(&j->lock);
413 if (!(flags & JOURNAL_RES_GET_RESERVED) &&
414 !test_bit(JOURNAL_MAY_GET_UNRESERVED, &j->flags)) {
416 * Don't want to close current journal entry, just need to
419 ret = cur_entry_journal_full;
424 * If we couldn't get a reservation because the current buf filled up,
425 * and we had room for a bigger entry on disk, signal that we want to
426 * realloc the journal bufs:
428 buf = journal_cur_buf(j);
429 if (journal_entry_is_open(j) &&
430 buf->buf_size >> 9 < buf->disk_sectors &&
431 buf->buf_size < JOURNAL_ENTRY_SIZE_MAX)
432 j->buf_size_want = max(j->buf_size_want, buf->buf_size << 1);
434 if (journal_entry_is_open(j) &&
435 !__journal_entry_close(j)) {
437 * We failed to get a reservation on the current open journal
438 * entry because it's full, and we can't close it because
439 * there's still a previous one in flight:
441 trace_journal_entry_full(c);
442 ret = cur_entry_blocked;
444 ret = journal_entry_open(j);
447 if ((ret && ret != cur_entry_insufficient_devices) &&
448 !j->res_get_blocked_start) {
449 j->res_get_blocked_start = local_clock() ?: 1;
450 trace_journal_full(c);
453 can_discard = j->can_discard;
454 spin_unlock(&j->lock);
459 if ((ret == cur_entry_journal_full ||
460 ret == cur_entry_journal_pin_full) &&
462 j->reservations.idx == j->reservations.unwritten_idx &&
463 (flags & JOURNAL_RES_GET_RESERVED)) {
464 char *journal_debug_buf = kmalloc(4096, GFP_ATOMIC);
466 bch_err(c, "Journal stuck!");
467 if (journal_debug_buf) {
468 bch2_journal_debug_to_text(&_PBUF(journal_debug_buf, 4096), j);
469 bch_err(c, "%s", journal_debug_buf);
471 bch2_journal_pins_to_text(&_PBUF(journal_debug_buf, 4096), j);
472 bch_err(c, "Journal pins:\n%s", journal_debug_buf);
473 kfree(journal_debug_buf);
481 * Journal is full - can't rely on reclaim from work item due to
484 if ((ret == cur_entry_journal_full ||
485 ret == cur_entry_journal_pin_full) &&
486 !(flags & JOURNAL_RES_GET_NONBLOCK)) {
488 bch2_journal_do_discards(j);
492 if (mutex_trylock(&j->reclaim_lock)) {
493 bch2_journal_reclaim(j);
494 mutex_unlock(&j->reclaim_lock);
498 return ret == cur_entry_insufficient_devices ? -EROFS : -EAGAIN;
502 * Essentially the entry function to the journaling code. When bcachefs is doing
503 * a btree insert, it calls this function to get the current journal write.
504 * Journal write is the structure used set up journal writes. The calling
505 * function will then add its keys to the structure, queuing them for the next
508 * To ensure forward progress, the current task must not be holding any
509 * btree node write locks.
511 int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
516 closure_wait_event(&j->async_wait,
517 (ret = __journal_res_get(j, res, flags)) != -EAGAIN ||
518 (flags & JOURNAL_RES_GET_NONBLOCK));
522 /* journal_preres: */
524 static bool journal_preres_available(struct journal *j,
525 struct journal_preres *res,
529 bool ret = bch2_journal_preres_get_fast(j, res, new_u64s, flags, true);
531 if (!ret && mutex_trylock(&j->reclaim_lock)) {
532 bch2_journal_reclaim(j);
533 mutex_unlock(&j->reclaim_lock);
539 int __bch2_journal_preres_get(struct journal *j,
540 struct journal_preres *res,
546 closure_wait_event(&j->preres_wait,
547 (ret = bch2_journal_error(j)) ||
548 journal_preres_available(j, res, new_u64s, flags));
552 /* journal_entry_res: */
554 void bch2_journal_entry_res_resize(struct journal *j,
555 struct journal_entry_res *res,
558 union journal_res_state state;
559 int d = new_u64s - res->u64s;
563 j->entry_u64s_reserved += d;
567 j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
569 state = READ_ONCE(j->reservations);
571 if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
572 state.cur_entry_offset > j->cur_entry_u64s) {
573 j->cur_entry_u64s += d;
575 * Not enough room in current journal entry, have to flush it:
577 __journal_entry_close(j);
579 journal_cur_buf(j)->u64s_reserved += d;
582 spin_unlock(&j->lock);
586 /* journal flushing: */
589 * bch2_journal_flush_seq_async - wait for a journal entry to be written
591 * like bch2_journal_wait_on_seq, except that it triggers a write immediately if
594 int bch2_journal_flush_seq_async(struct journal *j, u64 seq,
595 struct closure *parent)
597 struct journal_buf *buf;
600 if (seq <= j->flushed_seq_ondisk)
605 BUG_ON(seq > journal_cur_seq(j));
607 /* Recheck under lock: */
608 if (j->err_seq && seq >= j->err_seq) {
613 if (seq <= j->flushed_seq_ondisk) {
618 /* if seq was written, but not flushed - flush a newer one instead */
619 seq = max(seq, last_unwritten_seq(j));
622 if (seq == journal_cur_seq(j) && !journal_entry_is_open(j)) {
623 struct journal_res res = { 0 };
625 spin_unlock(&j->lock);
627 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
632 buf = j->buf + (seq & JOURNAL_BUF_MASK);
633 buf->must_flush = true;
634 set_bit(JOURNAL_NEED_WRITE, &j->flags);
636 if (parent && !closure_wait(&buf->wait, parent))
639 bch2_journal_res_put(j, &res);
646 * if write was kicked off without a flush, flush the next sequence
649 buf = journal_seq_to_buf(j, seq);
652 goto recheck_need_open;
655 buf->must_flush = true;
657 if (parent && !closure_wait(&buf->wait, parent))
660 if (seq == journal_cur_seq(j))
661 journal_entry_want_write(j);
663 spin_unlock(&j->lock);
667 int bch2_journal_flush_seq(struct journal *j, u64 seq)
669 u64 start_time = local_clock();
672 ret = wait_event_interruptible(j->wait, (ret2 = bch2_journal_flush_seq_async(j, seq, NULL)));
675 bch2_time_stats_update(j->flush_seq_time, start_time);
677 return ret ?: ret2 < 0 ? ret2 : 0;
680 int bch2_journal_meta(struct journal *j)
682 struct journal_res res;
685 memset(&res, 0, sizeof(res));
687 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
691 bch2_journal_res_put(j, &res);
693 return bch2_journal_flush_seq(j, res.seq);
697 * bch2_journal_flush_async - if there is an open journal entry, or a journal
698 * still being written, write it and wait for the write to complete
700 void bch2_journal_flush_async(struct journal *j, struct closure *parent)
702 u64 seq, journal_seq;
705 journal_seq = journal_cur_seq(j);
707 if (journal_entry_is_open(j)) {
709 } else if (journal_seq) {
710 seq = journal_seq - 1;
712 spin_unlock(&j->lock);
715 spin_unlock(&j->lock);
717 bch2_journal_flush_seq_async(j, seq, parent);
720 int bch2_journal_flush(struct journal *j)
722 u64 seq, journal_seq;
725 journal_seq = journal_cur_seq(j);
727 if (journal_entry_is_open(j)) {
729 } else if (journal_seq) {
730 seq = journal_seq - 1;
732 spin_unlock(&j->lock);
735 spin_unlock(&j->lock);
737 return bch2_journal_flush_seq(j, seq);
740 /* block/unlock the journal: */
742 void bch2_journal_unblock(struct journal *j)
746 spin_unlock(&j->lock);
751 void bch2_journal_block(struct journal *j)
755 spin_unlock(&j->lock);
760 /* allocate journal on a device: */
762 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
763 bool new_fs, struct closure *cl)
765 struct bch_fs *c = ca->fs;
766 struct journal_device *ja = &ca->journal;
767 struct bch_sb_field_journal *journal_buckets;
768 u64 *new_bucket_seq = NULL, *new_buckets = NULL;
771 /* don't handle reducing nr of buckets yet: */
775 new_buckets = kzalloc(nr * sizeof(u64), GFP_KERNEL);
776 new_bucket_seq = kzalloc(nr * sizeof(u64), GFP_KERNEL);
777 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) {
790 * We may be called from the device add path, before the new device has
791 * actually been added to the running filesystem:
794 spin_lock(&c->journal.lock);
796 memcpy(new_buckets, ja->buckets, ja->nr * sizeof(u64));
797 memcpy(new_bucket_seq, ja->bucket_seq, ja->nr * sizeof(u64));
798 swap(new_buckets, ja->buckets);
799 swap(new_bucket_seq, ja->bucket_seq);
802 spin_unlock(&c->journal.lock);
804 while (ja->nr < nr) {
805 struct open_bucket *ob = NULL;
810 b = bch2_bucket_alloc_new_fs(ca);
817 ob = bch2_bucket_alloc(c, ca, RESERVE_NONE,
821 ret = cl ? -EAGAIN : -ENOSPC;
825 b = sector_to_bucket(ca, ob->ptr.offset);
829 spin_lock(&c->journal.lock);
833 * For resize at runtime, we should be writing the new
834 * superblock before inserting into the journal array
837 pos = ja->nr ? (ja->cur_idx + 1) % ja->nr : 0;
838 __array_insert_item(ja->buckets, ja->nr, pos);
839 __array_insert_item(ja->bucket_seq, ja->nr, pos);
840 __array_insert_item(journal_buckets->buckets, ja->nr, pos);
843 ja->buckets[pos] = b;
844 ja->bucket_seq[pos] = 0;
845 journal_buckets->buckets[pos] = cpu_to_le64(b);
847 if (pos <= ja->discard_idx)
848 ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
849 if (pos <= ja->dirty_idx_ondisk)
850 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
851 if (pos <= ja->dirty_idx)
852 ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
853 if (pos <= ja->cur_idx)
854 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
857 spin_unlock(&c->journal.lock);
860 bch2_mark_metadata_bucket(c, ca, b, BCH_DATA_journal,
862 gc_phase(GC_PHASE_SB),
865 ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
866 bch2_trans_mark_metadata_bucket(&trans, ca,
868 ca->mi.bucket_size));
870 bch2_open_bucket_put(c, ob);
877 bch2_sb_resize_journal(&ca->disk_sb,
878 ja->nr + sizeof(*journal_buckets) / sizeof(u64));
879 kfree(new_bucket_seq);
886 * Allocate more journal space at runtime - not currently making use if it, but
889 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
892 struct journal_device *ja = &ca->journal;
897 closure_init_stack(&cl);
900 struct disk_reservation disk_res = { 0, 0 };
904 mutex_lock(&c->sb_lock);
908 * note: journal buckets aren't really counted as _sectors_ used yet, so
909 * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
910 * when space used goes up without a reservation - but we do need the
911 * reservation to ensure we'll actually be able to allocate:
914 if (bch2_disk_reservation_get(c, &disk_res,
915 bucket_to_sector(ca, nr - ja->nr), 1, 0)) {
916 mutex_unlock(&c->sb_lock);
920 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
922 bch2_disk_reservation_put(c, &disk_res);
924 if (ja->nr != current_nr)
926 mutex_unlock(&c->sb_lock);
927 } while (ret == -EAGAIN);
932 int bch2_dev_journal_alloc(struct bch_dev *ca)
936 if (dynamic_fault("bcachefs:add:journal_alloc"))
939 /* 1/128th of the device by default: */
940 nr = ca->mi.nbuckets >> 7;
943 * clamp journal size to 8192 buckets or 8GB (in sectors), whichever
946 nr = clamp_t(unsigned, nr,
947 BCH_JOURNAL_BUCKETS_MIN,
949 (1 << 24) / ca->mi.bucket_size));
951 return __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
954 /* startup/shutdown: */
956 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
958 union journal_res_state state;
963 state = READ_ONCE(j->reservations);
966 while (i != state.unwritten_idx) {
967 i = (i - 1) & JOURNAL_BUF_MASK;
968 if (bch2_bkey_has_device(bkey_i_to_s_c(&j->buf[i].key), dev_idx))
971 spin_unlock(&j->lock);
976 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
978 wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
981 void bch2_fs_journal_stop(struct journal *j)
983 bch2_journal_flush_all_pins(j);
985 wait_event(j->wait, journal_entry_close(j));
988 * Always write a new journal entry, to make sure the clock hands are up
989 * to date (and match the superblock)
991 bch2_journal_meta(j);
995 BUG_ON(!bch2_journal_error(j) &&
996 test_bit(JOURNAL_REPLAY_DONE, &j->flags) &&
997 (journal_entry_is_open(j) ||
998 j->last_empty_seq + 1 != journal_cur_seq(j)));
1000 cancel_delayed_work_sync(&j->write_work);
1001 bch2_journal_reclaim_stop(j);
1004 int bch2_fs_journal_start(struct journal *j, u64 cur_seq,
1005 struct list_head *journal_entries)
1007 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1008 struct journal_entry_pin_list *p;
1009 struct journal_replay *i;
1010 u64 last_seq = cur_seq, nr, seq;
1012 if (!list_empty(journal_entries))
1013 last_seq = le64_to_cpu(list_last_entry(journal_entries,
1014 struct journal_replay, list)->j.last_seq);
1016 nr = cur_seq - last_seq;
1018 if (nr + 1 > j->pin.size) {
1020 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
1022 bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
1027 j->replay_journal_seq = last_seq;
1028 j->replay_journal_seq_end = cur_seq;
1029 j->last_seq_ondisk = last_seq;
1030 j->pin.front = last_seq;
1031 j->pin.back = cur_seq;
1032 atomic64_set(&j->seq, cur_seq - 1);
1034 fifo_for_each_entry_ptr(p, &j->pin, seq)
1035 journal_pin_list_init(p, 1);
1037 list_for_each_entry(i, journal_entries, list) {
1040 seq = le64_to_cpu(i->j.seq);
1041 BUG_ON(seq >= cur_seq);
1046 p = journal_seq_pin(j, seq);
1049 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1050 bch2_dev_list_add_dev(&p->devs, i->ptrs[ptr].dev);
1053 spin_lock(&j->lock);
1055 set_bit(JOURNAL_STARTED, &j->flags);
1056 j->last_flush_write = jiffies;
1058 journal_pin_new_entry(j);
1060 j->reservations.idx = j->reservations.unwritten_idx = journal_cur_seq(j);
1062 bch2_journal_buf_init(j);
1064 c->last_bucket_seq_cleanup = journal_cur_seq(j);
1066 bch2_journal_space_available(j);
1067 spin_unlock(&j->lock);
1074 void bch2_dev_journal_exit(struct bch_dev *ca)
1076 kfree(ca->journal.bio);
1077 kfree(ca->journal.buckets);
1078 kfree(ca->journal.bucket_seq);
1080 ca->journal.bio = NULL;
1081 ca->journal.buckets = NULL;
1082 ca->journal.bucket_seq = NULL;
1085 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1087 struct journal_device *ja = &ca->journal;
1088 struct bch_sb_field_journal *journal_buckets =
1089 bch2_sb_get_journal(sb);
1092 ja->nr = bch2_nr_journal_buckets(journal_buckets);
1094 ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1095 if (!ja->bucket_seq)
1098 ca->journal.bio = bio_kmalloc(GFP_KERNEL,
1099 DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE));
1100 if (!ca->journal.bio)
1103 ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1107 for (i = 0; i < ja->nr; i++)
1108 ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1113 void bch2_fs_journal_exit(struct journal *j)
1117 for (i = 0; i < ARRAY_SIZE(j->buf); i++)
1118 kvpfree(j->buf[i].data, j->buf[i].buf_size);
1122 int bch2_fs_journal_init(struct journal *j)
1124 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1125 static struct lock_class_key res_key;
1129 pr_verbose_init(c->opts, "");
1131 spin_lock_init(&j->lock);
1132 spin_lock_init(&j->err_lock);
1133 init_waitqueue_head(&j->wait);
1134 INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1135 init_waitqueue_head(&j->reclaim_wait);
1136 init_waitqueue_head(&j->pin_flush_wait);
1137 mutex_init(&j->reclaim_lock);
1138 mutex_init(&j->discard_lock);
1140 lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1142 j->write_delay_ms = 1000;
1143 j->reclaim_delay_ms = 100;
1145 atomic64_set(&j->reservations.counter,
1146 ((union journal_res_state)
1147 { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1149 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL))) {
1154 for (i = 0; i < ARRAY_SIZE(j->buf); i++) {
1155 j->buf[i].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1156 j->buf[i].data = kvpmalloc(j->buf[i].buf_size, GFP_KERNEL);
1157 if (!j->buf[i].data) {
1163 j->pin.front = j->pin.back = 1;
1165 pr_verbose_init(c->opts, "ret %i", ret);
1171 void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1173 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1174 union journal_res_state s;
1179 s = READ_ONCE(j->reservations);
1182 "active journal entries:\t%llu\n"
1184 "last_seq:\t\t%llu\n"
1185 "last_seq_ondisk:\t%llu\n"
1186 "flushed_seq_ondisk:\t%llu\n"
1187 "prereserved:\t\t%u/%u\n"
1188 "each entry reserved:\t%u\n"
1189 "nr flush writes:\t%llu\n"
1190 "nr noflush writes:\t%llu\n"
1191 "nr direct reclaim:\t%llu\n"
1192 "nr background reclaim:\t%llu\n"
1193 "reclaim kicked:\t\t%u\n"
1194 "reclaim runs in:\t%u ms\n"
1195 "current entry sectors:\t%u\n"
1196 "current entry error:\t%u\n"
1197 "current entry:\t\t",
1200 journal_last_seq(j),
1202 j->flushed_seq_ondisk,
1203 j->prereserved.reserved,
1204 j->prereserved.remaining,
1205 j->entry_u64s_reserved,
1207 j->nr_noflush_writes,
1208 j->nr_direct_reclaim,
1209 j->nr_background_reclaim,
1211 jiffies_to_msecs(j->next_reclaim - jiffies),
1212 j->cur_entry_sectors,
1213 j->cur_entry_error);
1215 switch (s.cur_entry_offset) {
1216 case JOURNAL_ENTRY_ERROR_VAL:
1217 pr_buf(out, "error\n");
1219 case JOURNAL_ENTRY_CLOSED_VAL:
1220 pr_buf(out, "closed\n");
1223 pr_buf(out, "%u/%u\n",
1230 "current entry:\t\tidx %u refcount %u\n",
1231 s.idx, journal_state_count(s, s.idx));
1234 while (i != s.unwritten_idx) {
1235 i = (i - 1) & JOURNAL_BUF_MASK;
1237 pr_buf(out, "unwritten entry:\tidx %u refcount %u sectors %u\n",
1238 i, journal_state_count(s, i), j->buf[i].sectors);
1242 "need write:\t\t%i\n"
1243 "replay done:\t\t%i\n",
1244 test_bit(JOURNAL_NEED_WRITE, &j->flags),
1245 test_bit(JOURNAL_REPLAY_DONE, &j->flags));
1247 pr_buf(out, "space:\n");
1248 pr_buf(out, "\tdiscarded\t%u:%u\n",
1249 j->space[journal_space_discarded].next_entry,
1250 j->space[journal_space_discarded].total);
1251 pr_buf(out, "\tclean ondisk\t%u:%u\n",
1252 j->space[journal_space_clean_ondisk].next_entry,
1253 j->space[journal_space_clean_ondisk].total);
1254 pr_buf(out, "\tclean\t\t%u:%u\n",
1255 j->space[journal_space_clean].next_entry,
1256 j->space[journal_space_clean].total);
1257 pr_buf(out, "\ttotal\t\t%u:%u\n",
1258 j->space[journal_space_total].next_entry,
1259 j->space[journal_space_total].total);
1261 for_each_member_device_rcu(ca, c, i,
1262 &c->rw_devs[BCH_DATA_journal]) {
1263 struct journal_device *ja = &ca->journal;
1265 if (!test_bit(ca->dev_idx, c->rw_devs[BCH_DATA_journal].d))
1274 "\tbucket size\t%u\n"
1275 "\tavailable\t%u:%u\n"
1276 "\tdiscard_idx\t%u\n"
1277 "\tdirty_ondisk\t%u (seq %llu)\n"
1278 "\tdirty_idx\t%u (seq %llu)\n"
1279 "\tcur_idx\t\t%u (seq %llu)\n",
1280 i, ja->nr, ca->mi.bucket_size,
1281 bch2_journal_dev_buckets_available(j, ja, journal_space_discarded),
1284 ja->dirty_idx_ondisk, ja->bucket_seq[ja->dirty_idx_ondisk],
1285 ja->dirty_idx, ja->bucket_seq[ja->dirty_idx],
1286 ja->cur_idx, ja->bucket_seq[ja->cur_idx]);
1292 void bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1294 spin_lock(&j->lock);
1295 __bch2_journal_debug_to_text(out, j);
1296 spin_unlock(&j->lock);
1299 void bch2_journal_pins_to_text(struct printbuf *out, struct journal *j)
1301 struct journal_entry_pin_list *pin_list;
1302 struct journal_entry_pin *pin;
1305 spin_lock(&j->lock);
1306 fifo_for_each_entry_ptr(pin_list, &j->pin, i) {
1307 pr_buf(out, "%llu: count %u\n",
1308 i, atomic_read(&pin_list->count));
1310 list_for_each_entry(pin, &pin_list->list, list)
1311 pr_buf(out, "\t%px %ps\n",
1314 if (!list_empty(&pin_list->flushed))
1315 pr_buf(out, "flushed:\n");
1317 list_for_each_entry(pin, &pin_list->flushed, list)
1318 pr_buf(out, "\t%px %ps\n",
1321 spin_unlock(&j->lock);