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_sb.h"
19 #include "journal_seq_blacklist.h"
21 #include <trace/events/bcachefs.h>
24 static const char * const bch2_journal_watermarks[] = {
29 static const char * const bch2_journal_errors[] = {
35 static inline bool journal_seq_unwritten(struct journal *j, u64 seq)
37 return seq > j->seq_ondisk;
40 static bool __journal_entry_is_open(union journal_res_state state)
42 return state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL;
45 static inline unsigned nr_unwritten_journal_entries(struct journal *j)
47 return atomic64_read(&j->seq) - j->seq_ondisk;
50 static bool journal_entry_is_open(struct journal *j)
52 return __journal_entry_is_open(j->reservations);
55 static inline struct journal_buf *
56 journal_seq_to_buf(struct journal *j, u64 seq)
58 struct journal_buf *buf = NULL;
60 EBUG_ON(seq > journal_cur_seq(j));
62 if (journal_seq_unwritten(j, seq)) {
63 buf = j->buf + (seq & JOURNAL_BUF_MASK);
64 EBUG_ON(le64_to_cpu(buf->data->seq) != seq);
69 static void journal_pin_list_init(struct journal_entry_pin_list *p, int count)
71 INIT_LIST_HEAD(&p->list);
72 INIT_LIST_HEAD(&p->key_cache_list);
73 INIT_LIST_HEAD(&p->flushed);
74 atomic_set(&p->count, count);
78 /* journal entry close/open: */
80 void __bch2_journal_buf_put(struct journal *j)
82 struct bch_fs *c = container_of(j, struct bch_fs, journal);
84 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
88 * Returns true if journal entry is now closed:
90 * We don't close a journal_buf until the next journal_buf is finished writing,
91 * and can be opened again - this also initializes the next journal_buf:
93 static void __journal_entry_close(struct journal *j, unsigned closed_val)
95 struct bch_fs *c = container_of(j, struct bch_fs, journal);
96 struct journal_buf *buf = journal_cur_buf(j);
97 union journal_res_state old, new;
98 u64 v = atomic64_read(&j->reservations.counter);
101 BUG_ON(closed_val != JOURNAL_ENTRY_CLOSED_VAL &&
102 closed_val != JOURNAL_ENTRY_ERROR_VAL);
104 lockdep_assert_held(&j->lock);
108 new.cur_entry_offset = closed_val;
110 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL ||
111 old.cur_entry_offset == new.cur_entry_offset)
113 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
114 old.v, new.v)) != old.v);
116 if (!__journal_entry_is_open(old))
119 /* Close out old buffer: */
120 buf->data->u64s = cpu_to_le32(old.cur_entry_offset);
122 sectors = vstruct_blocks_plus(buf->data, c->block_bits,
123 buf->u64s_reserved) << c->block_bits;
124 BUG_ON(sectors > buf->sectors);
125 buf->sectors = sectors;
128 * We have to set last_seq here, _before_ opening a new journal entry:
130 * A threads may replace an old pin with a new pin on their current
131 * journal reservation - the expectation being that the journal will
132 * contain either what the old pin protected or what the new pin
135 * After the old pin is dropped journal_last_seq() won't include the old
136 * pin, so we can only write the updated last_seq on the entry that
137 * contains whatever the new pin protects.
139 * Restated, we can _not_ update last_seq for a given entry if there
140 * could be a newer entry open with reservations/pins that have been
143 * Hence, we want update/set last_seq on the current journal entry right
144 * before we open a new one:
146 buf->last_seq = journal_last_seq(j);
147 buf->data->last_seq = cpu_to_le64(buf->last_seq);
148 BUG_ON(buf->last_seq > le64_to_cpu(buf->data->seq));
150 __bch2_journal_pin_put(j, le64_to_cpu(buf->data->seq));
152 cancel_delayed_work(&j->write_work);
154 bch2_journal_space_available(j);
156 bch2_journal_buf_put(j, old.idx);
159 void bch2_journal_halt(struct journal *j)
162 __journal_entry_close(j, JOURNAL_ENTRY_ERROR_VAL);
164 j->err_seq = journal_cur_seq(j);
165 spin_unlock(&j->lock);
168 static bool journal_entry_want_write(struct journal *j)
170 bool ret = !journal_entry_is_open(j) ||
171 journal_cur_seq(j) == journal_last_unwritten_seq(j);
173 /* Don't close it yet if we already have a write in flight: */
175 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
176 else if (nr_unwritten_journal_entries(j)) {
177 struct journal_buf *buf = journal_cur_buf(j);
179 if (!buf->flush_time) {
180 buf->flush_time = local_clock() ?: 1;
181 buf->expires = jiffies;
188 static bool journal_entry_close(struct journal *j)
193 ret = journal_entry_want_write(j);
194 spin_unlock(&j->lock);
200 * should _only_ called from journal_res_get() - when we actually want a
201 * journal reservation - journal entry is open means journal is dirty:
205 * -ENOSPC: journal currently full, must invoke reclaim
206 * -EAGAIN: journal blocked, must wait
207 * -EROFS: insufficient rw devices or journal error
209 static int journal_entry_open(struct journal *j)
211 struct bch_fs *c = container_of(j, struct bch_fs, journal);
212 struct journal_buf *buf = j->buf +
213 ((journal_cur_seq(j) + 1) & JOURNAL_BUF_MASK);
214 union journal_res_state old, new;
218 lockdep_assert_held(&j->lock);
219 BUG_ON(journal_entry_is_open(j));
220 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
223 return JOURNAL_ERR_blocked;
225 if (j->cur_entry_error)
226 return j->cur_entry_error;
228 if (bch2_journal_error(j))
229 return JOURNAL_ERR_insufficient_devices; /* -EROFS */
231 if (!fifo_free(&j->pin))
232 return JOURNAL_ERR_journal_pin_full;
234 if (nr_unwritten_journal_entries(j) == ARRAY_SIZE(j->buf) - 1)
235 return JOURNAL_ERR_max_in_flight;
237 BUG_ON(!j->cur_entry_sectors);
240 (journal_cur_seq(j) == j->flushed_seq_ondisk
242 : j->last_flush_write) +
243 msecs_to_jiffies(c->opts.journal_flush_delay);
245 buf->u64s_reserved = j->entry_u64s_reserved;
246 buf->disk_sectors = j->cur_entry_sectors;
247 buf->sectors = min(buf->disk_sectors, buf->buf_size >> 9);
249 u64s = (int) (buf->sectors << 9) / sizeof(u64) -
250 journal_entry_overhead(j);
251 u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
254 return JOURNAL_ERR_journal_full;
256 if (fifo_empty(&j->pin) && j->reclaim_thread)
257 wake_up_process(j->reclaim_thread);
260 * The fifo_push() needs to happen at the same time as j->seq is
261 * incremented for journal_last_seq() to be calculated correctly
263 atomic64_inc(&j->seq);
264 journal_pin_list_init(fifo_push_ref(&j->pin), 1);
266 BUG_ON(j->buf + (journal_cur_seq(j) & JOURNAL_BUF_MASK) != buf);
268 bkey_extent_init(&buf->key);
269 buf->noflush = false;
270 buf->must_flush = false;
271 buf->separate_flush = false;
274 memset(buf->data, 0, sizeof(*buf->data));
275 buf->data->seq = cpu_to_le64(journal_cur_seq(j));
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 BUG_ON(old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL);
290 BUG_ON(journal_state_count(new, new.idx));
291 BUG_ON(new.idx != (journal_cur_seq(j) & JOURNAL_BUF_MASK));
293 journal_state_inc(&new);
294 new.cur_entry_offset = 0;
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(c->io_complete_wq,
305 msecs_to_jiffies(c->opts.journal_flush_delay));
310 static bool journal_quiesced(struct journal *j)
312 bool ret = atomic64_read(&j->seq) == j->seq_ondisk;
315 journal_entry_close(j);
319 static void journal_quiesce(struct journal *j)
321 wait_event(j->wait, journal_quiesced(j));
324 static void journal_write_work(struct work_struct *work)
326 struct journal *j = container_of(work, struct journal, write_work.work);
327 struct bch_fs *c = container_of(j, struct bch_fs, journal);
331 if (!__journal_entry_is_open(j->reservations))
334 delta = journal_cur_buf(j)->expires - jiffies;
337 mod_delayed_work(c->io_complete_wq, &j->write_work, delta);
339 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
341 spin_unlock(&j->lock);
344 static int __journal_res_get(struct journal *j, struct journal_res *res,
347 struct bch_fs *c = container_of(j, struct bch_fs, journal);
348 struct journal_buf *buf;
352 if (journal_res_get_fast(j, res, flags))
355 if (bch2_journal_error(j))
361 * Recheck after taking the lock, so we don't race with another thread
362 * that just did journal_entry_open() and call journal_entry_close()
365 if (journal_res_get_fast(j, res, flags)) {
366 spin_unlock(&j->lock);
370 if ((flags & JOURNAL_WATERMARK_MASK) < j->watermark) {
372 * Don't want to close current journal entry, just need to
375 ret = JOURNAL_ERR_journal_full;
380 * If we couldn't get a reservation because the current buf filled up,
381 * and we had room for a bigger entry on disk, signal that we want to
382 * realloc the journal bufs:
384 buf = journal_cur_buf(j);
385 if (journal_entry_is_open(j) &&
386 buf->buf_size >> 9 < buf->disk_sectors &&
387 buf->buf_size < JOURNAL_ENTRY_SIZE_MAX)
388 j->buf_size_want = max(j->buf_size_want, buf->buf_size << 1);
390 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
391 ret = journal_entry_open(j);
393 if (ret == JOURNAL_ERR_max_in_flight)
394 trace_journal_entry_full(c);
396 if ((ret && ret != JOURNAL_ERR_insufficient_devices) &&
397 !j->res_get_blocked_start) {
398 j->res_get_blocked_start = local_clock() ?: 1;
399 trace_journal_full(c);
402 can_discard = j->can_discard;
403 spin_unlock(&j->lock);
408 if ((ret == JOURNAL_ERR_journal_full ||
409 ret == JOURNAL_ERR_journal_pin_full) &&
411 !nr_unwritten_journal_entries(j) &&
412 (flags & JOURNAL_WATERMARK_MASK) == JOURNAL_WATERMARK_reserved) {
413 struct printbuf buf = PRINTBUF;
415 bch_err(c, "Journal stuck! Hava a pre-reservation but journal full (ret %s)",
416 bch2_journal_errors[ret]);
418 bch2_journal_debug_to_text(&buf, j);
419 bch_err(c, "%s", buf.buf);
421 printbuf_reset(&buf);
422 bch2_journal_pins_to_text(&buf, j);
423 bch_err(c, "Journal pins:\n%s", buf.buf);
431 * Journal is full - can't rely on reclaim from work item due to
434 if ((ret == JOURNAL_ERR_journal_full ||
435 ret == JOURNAL_ERR_journal_pin_full) &&
436 !(flags & JOURNAL_RES_GET_NONBLOCK)) {
438 bch2_journal_do_discards(j);
442 if (mutex_trylock(&j->reclaim_lock)) {
443 bch2_journal_reclaim(j);
444 mutex_unlock(&j->reclaim_lock);
448 return ret == JOURNAL_ERR_insufficient_devices ? -EROFS : -EAGAIN;
452 * Essentially the entry function to the journaling code. When bcachefs is doing
453 * a btree insert, it calls this function to get the current journal write.
454 * Journal write is the structure used set up journal writes. The calling
455 * function will then add its keys to the structure, queuing them for the next
458 * To ensure forward progress, the current task must not be holding any
459 * btree node write locks.
461 int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
466 closure_wait_event(&j->async_wait,
467 (ret = __journal_res_get(j, res, flags)) != -EAGAIN ||
468 (flags & JOURNAL_RES_GET_NONBLOCK));
472 /* journal_preres: */
474 static bool journal_preres_available(struct journal *j,
475 struct journal_preres *res,
479 bool ret = bch2_journal_preres_get_fast(j, res, new_u64s, flags, true);
481 if (!ret && mutex_trylock(&j->reclaim_lock)) {
482 bch2_journal_reclaim(j);
483 mutex_unlock(&j->reclaim_lock);
489 int __bch2_journal_preres_get(struct journal *j,
490 struct journal_preres *res,
496 closure_wait_event(&j->preres_wait,
497 (ret = bch2_journal_error(j)) ||
498 journal_preres_available(j, res, new_u64s, flags));
502 /* journal_entry_res: */
504 void bch2_journal_entry_res_resize(struct journal *j,
505 struct journal_entry_res *res,
508 union journal_res_state state;
509 int d = new_u64s - res->u64s;
513 j->entry_u64s_reserved += d;
517 j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
519 state = READ_ONCE(j->reservations);
521 if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
522 state.cur_entry_offset > j->cur_entry_u64s) {
523 j->cur_entry_u64s += d;
525 * Not enough room in current journal entry, have to flush it:
527 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
529 journal_cur_buf(j)->u64s_reserved += d;
532 spin_unlock(&j->lock);
536 /* journal flushing: */
539 * bch2_journal_flush_seq_async - wait for a journal entry to be written
541 * like bch2_journal_wait_on_seq, except that it triggers a write immediately if
544 int bch2_journal_flush_seq_async(struct journal *j, u64 seq,
545 struct closure *parent)
547 struct journal_buf *buf;
550 if (seq <= j->flushed_seq_ondisk)
555 if (WARN_ONCE(seq > journal_cur_seq(j),
556 "requested to flush journal seq %llu, but currently at %llu",
557 seq, journal_cur_seq(j)))
560 /* Recheck under lock: */
561 if (j->err_seq && seq >= j->err_seq) {
566 if (seq <= j->flushed_seq_ondisk) {
571 /* if seq was written, but not flushed - flush a newer one instead */
572 seq = max(seq, journal_last_unwritten_seq(j));
575 if (seq > journal_cur_seq(j)) {
576 struct journal_res res = { 0 };
578 if (journal_entry_is_open(j))
579 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
581 spin_unlock(&j->lock);
583 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
588 buf = j->buf + (seq & JOURNAL_BUF_MASK);
589 buf->must_flush = true;
591 if (!buf->flush_time) {
592 buf->flush_time = local_clock() ?: 1;
593 buf->expires = jiffies;
596 if (parent && !closure_wait(&buf->wait, parent))
599 bch2_journal_res_put(j, &res);
606 * if write was kicked off without a flush, flush the next sequence
609 buf = journal_seq_to_buf(j, seq);
612 goto recheck_need_open;
615 buf->must_flush = true;
617 if (parent && !closure_wait(&buf->wait, parent))
620 if (seq == journal_cur_seq(j))
621 journal_entry_want_write(j);
623 spin_unlock(&j->lock);
627 int bch2_journal_flush_seq(struct journal *j, u64 seq)
629 u64 start_time = local_clock();
633 * Don't update time_stats when @seq is already flushed:
635 if (seq <= j->flushed_seq_ondisk)
638 ret = wait_event_interruptible(j->wait, (ret2 = bch2_journal_flush_seq_async(j, seq, NULL)));
641 bch2_time_stats_update(j->flush_seq_time, start_time);
643 return ret ?: ret2 < 0 ? ret2 : 0;
647 * bch2_journal_flush_async - if there is an open journal entry, or a journal
648 * still being written, write it and wait for the write to complete
650 void bch2_journal_flush_async(struct journal *j, struct closure *parent)
652 bch2_journal_flush_seq_async(j, atomic64_read(&j->seq), parent);
655 int bch2_journal_flush(struct journal *j)
657 return bch2_journal_flush_seq(j, atomic64_read(&j->seq));
661 * bch2_journal_noflush_seq - tell the journal not to issue any flushes before
664 bool bch2_journal_noflush_seq(struct journal *j, u64 seq)
666 struct bch_fs *c = container_of(j, struct bch_fs, journal);
670 if (!(c->sb.features & (1ULL << BCH_FEATURE_journal_no_flush)))
673 if (seq <= c->journal.flushed_seq_ondisk)
677 if (seq <= c->journal.flushed_seq_ondisk)
680 for (unwritten_seq = journal_last_unwritten_seq(j);
683 struct journal_buf *buf = journal_seq_to_buf(j, unwritten_seq);
685 /* journal write is already in flight, and was a flush write: */
686 if (unwritten_seq == journal_last_unwritten_seq(j) && !buf->noflush)
694 spin_unlock(&j->lock);
698 int bch2_journal_meta(struct journal *j)
700 struct journal_buf *buf;
701 struct journal_res res;
704 memset(&res, 0, sizeof(res));
706 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
710 buf = j->buf + (res.seq & JOURNAL_BUF_MASK);
711 buf->must_flush = true;
713 if (!buf->flush_time) {
714 buf->flush_time = local_clock() ?: 1;
715 buf->expires = jiffies;
718 bch2_journal_res_put(j, &res);
720 return bch2_journal_flush_seq(j, res.seq);
723 int bch2_journal_log_msg(struct journal *j, const char *fmt, ...)
725 struct jset_entry_log *entry;
726 struct journal_res res = { 0 };
727 unsigned msglen, u64s;
732 msglen = vsnprintf(NULL, 0, fmt, args) + 1;
735 u64s = jset_u64s(DIV_ROUND_UP(msglen, sizeof(u64)));
737 ret = bch2_journal_res_get(j, &res, u64s, 0);
741 entry = container_of(journal_res_entry(j, &res),
742 struct jset_entry_log, entry);;
743 memset(entry, 0, u64s * sizeof(u64));
744 entry->entry.type = BCH_JSET_ENTRY_log;
745 entry->entry.u64s = u64s - 1;
748 vsnprintf(entry->d, INT_MAX, fmt, args);
751 bch2_journal_res_put(j, &res);
753 return bch2_journal_flush_seq(j, res.seq);
756 /* block/unlock the journal: */
758 void bch2_journal_unblock(struct journal *j)
762 spin_unlock(&j->lock);
767 void bch2_journal_block(struct journal *j)
771 spin_unlock(&j->lock);
776 /* allocate journal on a device: */
778 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
779 bool new_fs, struct closure *cl)
781 struct bch_fs *c = ca->fs;
782 struct journal_device *ja = &ca->journal;
783 u64 *new_bucket_seq = NULL, *new_buckets = NULL;
784 struct open_bucket **ob = NULL;
786 unsigned i, nr_got = 0, nr_want = nr - ja->nr;
787 unsigned old_nr = ja->nr;
788 unsigned old_discard_idx = ja->discard_idx;
789 unsigned old_dirty_idx_ondisk = ja->dirty_idx_ondisk;
790 unsigned old_dirty_idx = ja->dirty_idx;
791 unsigned old_cur_idx = ja->cur_idx;
795 bch2_journal_flush_all_pins(&c->journal);
796 bch2_journal_block(&c->journal);
799 bu = kzalloc(nr_want * sizeof(*bu), GFP_KERNEL);
800 ob = kzalloc(nr_want * sizeof(*ob), GFP_KERNEL);
801 new_buckets = kzalloc(nr * sizeof(u64), GFP_KERNEL);
802 new_bucket_seq = kzalloc(nr * sizeof(u64), GFP_KERNEL);
803 if (!bu || !ob || !new_buckets || !new_bucket_seq) {
808 for (nr_got = 0; nr_got < nr_want; nr_got++) {
810 bu[nr_got] = bch2_bucket_alloc_new_fs(ca);
811 if (bu[nr_got] < 0) {
816 ob[nr_got] = bch2_bucket_alloc(c, ca, RESERVE_none,
818 if (IS_ERR(ob[nr_got])) {
819 ret = cl ? -EAGAIN : -ENOSPC;
823 bu[nr_got] = ob[nr_got]->bucket;
831 * We may be called from the device add path, before the new device has
832 * actually been added to the running filesystem:
835 spin_lock(&c->journal.lock);
837 memcpy(new_buckets, ja->buckets, ja->nr * sizeof(u64));
838 memcpy(new_bucket_seq, ja->bucket_seq, ja->nr * sizeof(u64));
839 swap(new_buckets, ja->buckets);
840 swap(new_bucket_seq, ja->bucket_seq);
842 for (i = 0; i < nr_got; i++) {
843 unsigned pos = ja->discard_idx ?: ja->nr;
846 __array_insert_item(ja->buckets, ja->nr, pos);
847 __array_insert_item(ja->bucket_seq, ja->nr, pos);
850 ja->buckets[pos] = b;
851 ja->bucket_seq[pos] = 0;
853 if (pos <= ja->discard_idx)
854 ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
855 if (pos <= ja->dirty_idx_ondisk)
856 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
857 if (pos <= ja->dirty_idx)
858 ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
859 if (pos <= ja->cur_idx)
860 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
863 ret = bch2_journal_buckets_to_sb(c, ca);
866 swap(new_buckets, ja->buckets);
867 swap(new_bucket_seq, ja->bucket_seq);
869 ja->discard_idx = old_discard_idx;
870 ja->dirty_idx_ondisk = old_dirty_idx_ondisk;
871 ja->dirty_idx = old_dirty_idx;
872 ja->cur_idx = old_cur_idx;
876 spin_unlock(&c->journal.lock);
879 bch2_journal_unblock(&c->journal);
885 for (i = 0; i < nr_got; i++) {
886 ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
887 bch2_trans_mark_metadata_bucket(&trans, ca,
888 bu[i], BCH_DATA_journal,
889 ca->mi.bucket_size));
891 bch2_fs_inconsistent(c, "error marking new journal buckets: %i", ret);
898 for (i = 0; i < nr_got; i++)
899 bch2_open_bucket_put(c, ob[i]);
901 kfree(new_bucket_seq);
909 bch2_journal_unblock(&c->journal);
914 * Allocate more journal space at runtime - not currently making use if it, but
917 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
920 struct journal_device *ja = &ca->journal;
925 /* don't handle reducing nr of buckets yet: */
929 closure_init_stack(&cl);
931 while (ja->nr != nr && (ret == 0 || ret == -EAGAIN)) {
932 struct disk_reservation disk_res = { 0, 0 };
936 mutex_lock(&c->sb_lock);
940 * note: journal buckets aren't really counted as _sectors_ used yet, so
941 * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
942 * when space used goes up without a reservation - but we do need the
943 * reservation to ensure we'll actually be able to allocate:
946 if (bch2_disk_reservation_get(c, &disk_res,
947 bucket_to_sector(ca, nr - ja->nr), 1, 0)) {
948 mutex_unlock(&c->sb_lock);
952 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
954 bch2_disk_reservation_put(c, &disk_res);
956 if (ja->nr != current_nr)
958 mutex_unlock(&c->sb_lock);
964 int bch2_dev_journal_alloc(struct bch_dev *ca)
969 if (dynamic_fault("bcachefs:add:journal_alloc"))
972 /* 1/128th of the device by default: */
973 nr = ca->mi.nbuckets >> 7;
976 * clamp journal size to 8192 buckets or 8GB (in sectors), whichever
979 nr = clamp_t(unsigned, nr,
980 BCH_JOURNAL_BUCKETS_MIN,
982 (1 << 24) / ca->mi.bucket_size));
985 mutex_lock(&ca->fs->sb_lock);
987 ret = __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
990 mutex_unlock(&ca->fs->sb_lock);
995 /* startup/shutdown: */
997 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
1002 spin_lock(&j->lock);
1003 for (seq = journal_last_unwritten_seq(j);
1004 seq <= journal_cur_seq(j) && !ret;
1006 struct journal_buf *buf = journal_seq_to_buf(j, seq);
1008 if (bch2_bkey_has_device(bkey_i_to_s_c(&buf->key), dev_idx))
1011 spin_unlock(&j->lock);
1016 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
1018 wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
1021 void bch2_fs_journal_stop(struct journal *j)
1023 bch2_journal_reclaim_stop(j);
1024 bch2_journal_flush_all_pins(j);
1026 wait_event(j->wait, journal_entry_close(j));
1029 * Always write a new journal entry, to make sure the clock hands are up
1030 * to date (and match the superblock)
1032 bch2_journal_meta(j);
1036 BUG_ON(!bch2_journal_error(j) &&
1037 test_bit(JOURNAL_REPLAY_DONE, &j->flags) &&
1038 j->last_empty_seq != journal_cur_seq(j));
1040 cancel_delayed_work_sync(&j->write_work);
1043 int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
1045 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1046 struct journal_entry_pin_list *p;
1047 struct journal_replay *i, **_i;
1048 struct genradix_iter iter;
1049 bool had_entries = false;
1051 u64 last_seq = cur_seq, nr, seq;
1053 genradix_for_each_reverse(&c->journal_entries, iter, _i) {
1056 if (!i || i->ignore)
1059 last_seq = le64_to_cpu(i->j.last_seq);
1063 nr = cur_seq - last_seq;
1065 if (nr + 1 > j->pin.size) {
1067 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
1069 bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
1074 j->replay_journal_seq = last_seq;
1075 j->replay_journal_seq_end = cur_seq;
1076 j->last_seq_ondisk = last_seq;
1077 j->flushed_seq_ondisk = cur_seq - 1;
1078 j->seq_ondisk = cur_seq - 1;
1079 j->pin.front = last_seq;
1080 j->pin.back = cur_seq;
1081 atomic64_set(&j->seq, cur_seq - 1);
1083 fifo_for_each_entry_ptr(p, &j->pin, seq)
1084 journal_pin_list_init(p, 1);
1086 genradix_for_each(&c->journal_entries, iter, _i) {
1089 if (!i || i->ignore)
1092 seq = le64_to_cpu(i->j.seq);
1093 BUG_ON(seq >= cur_seq);
1098 if (journal_entry_empty(&i->j))
1099 j->last_empty_seq = le64_to_cpu(i->j.seq);
1101 p = journal_seq_pin(j, seq);
1104 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1105 bch2_dev_list_add_dev(&p->devs, i->ptrs[ptr].dev);
1111 j->last_empty_seq = cur_seq;
1113 spin_lock(&j->lock);
1115 set_bit(JOURNAL_STARTED, &j->flags);
1116 j->last_flush_write = jiffies;
1118 j->reservations.idx = j->reservations.unwritten_idx = journal_cur_seq(j);
1119 j->reservations.unwritten_idx++;
1121 c->last_bucket_seq_cleanup = journal_cur_seq(j);
1123 bch2_journal_space_available(j);
1124 spin_unlock(&j->lock);
1126 return bch2_journal_reclaim_start(j);
1131 void bch2_dev_journal_exit(struct bch_dev *ca)
1133 kfree(ca->journal.bio);
1134 kfree(ca->journal.buckets);
1135 kfree(ca->journal.bucket_seq);
1137 ca->journal.bio = NULL;
1138 ca->journal.buckets = NULL;
1139 ca->journal.bucket_seq = NULL;
1142 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1144 struct journal_device *ja = &ca->journal;
1145 struct bch_sb_field_journal *journal_buckets =
1146 bch2_sb_get_journal(sb);
1147 struct bch_sb_field_journal_v2 *journal_buckets_v2 =
1148 bch2_sb_get_journal_v2(sb);
1153 if (journal_buckets_v2) {
1154 unsigned nr = bch2_sb_field_journal_v2_nr_entries(journal_buckets_v2);
1156 for (i = 0; i < nr; i++)
1157 ja->nr += le64_to_cpu(journal_buckets_v2->d[i].nr);
1158 } else if (journal_buckets) {
1159 ja->nr = bch2_nr_journal_buckets(journal_buckets);
1162 ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1163 if (!ja->bucket_seq)
1166 ca->journal.bio = bio_kmalloc(GFP_KERNEL,
1167 DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE));
1168 if (!ca->journal.bio)
1171 ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1175 if (journal_buckets_v2) {
1176 unsigned nr = bch2_sb_field_journal_v2_nr_entries(journal_buckets_v2);
1177 unsigned j, dst = 0;
1179 for (i = 0; i < nr; i++)
1180 for (j = 0; j < le64_to_cpu(journal_buckets_v2->d[i].nr); j++)
1181 ja->buckets[dst++] =
1182 le64_to_cpu(journal_buckets_v2->d[i].start) + j;
1183 } else if (journal_buckets) {
1184 for (i = 0; i < ja->nr; i++)
1185 ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1191 void bch2_fs_journal_exit(struct journal *j)
1195 for (i = 0; i < ARRAY_SIZE(j->buf); i++)
1196 kvpfree(j->buf[i].data, j->buf[i].buf_size);
1200 int bch2_fs_journal_init(struct journal *j)
1202 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1203 static struct lock_class_key res_key;
1207 pr_verbose_init(c->opts, "");
1209 spin_lock_init(&j->lock);
1210 spin_lock_init(&j->err_lock);
1211 init_waitqueue_head(&j->wait);
1212 INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1213 init_waitqueue_head(&j->reclaim_wait);
1214 init_waitqueue_head(&j->pin_flush_wait);
1215 mutex_init(&j->reclaim_lock);
1216 mutex_init(&j->discard_lock);
1218 lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1220 atomic64_set(&j->reservations.counter,
1221 ((union journal_res_state)
1222 { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1224 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL))) {
1229 for (i = 0; i < ARRAY_SIZE(j->buf); i++) {
1230 j->buf[i].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1231 j->buf[i].data = kvpmalloc(j->buf[i].buf_size, GFP_KERNEL);
1232 if (!j->buf[i].data) {
1238 j->pin.front = j->pin.back = 1;
1240 pr_verbose_init(c->opts, "ret %i", ret);
1246 void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1248 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1249 union journal_res_state s;
1251 unsigned long now = jiffies;
1256 out->tabstops[0] = 24;
1259 s = READ_ONCE(j->reservations);
1261 prt_printf(out, "dirty journal entries:\t%llu/%llu\n",fifo_used(&j->pin), j->pin.size);
1262 prt_printf(out, "seq:\t\t\t%llu\n", journal_cur_seq(j));
1263 prt_printf(out, "seq_ondisk:\t\t%llu\n", j->seq_ondisk);
1264 prt_printf(out, "last_seq:\t\t%llu\n", journal_last_seq(j));
1265 prt_printf(out, "last_seq_ondisk:\t%llu\n", j->last_seq_ondisk);
1266 prt_printf(out, "flushed_seq_ondisk:\t%llu\n", j->flushed_seq_ondisk);
1267 prt_printf(out, "prereserved:\t\t%u/%u\n", j->prereserved.reserved, j->prereserved.remaining);
1268 prt_printf(out, "watermark:\t\t%s\n", bch2_journal_watermarks[j->watermark]);
1269 prt_printf(out, "each entry reserved:\t%u\n", j->entry_u64s_reserved);
1270 prt_printf(out, "nr flush writes:\t%llu\n", j->nr_flush_writes);
1271 prt_printf(out, "nr noflush writes:\t%llu\n", j->nr_noflush_writes);
1272 prt_printf(out, "nr direct reclaim:\t%llu\n", j->nr_direct_reclaim);
1273 prt_printf(out, "nr background reclaim:\t%llu\n", j->nr_background_reclaim);
1274 prt_printf(out, "reclaim kicked:\t\t%u\n", j->reclaim_kicked);
1275 prt_printf(out, "reclaim runs in:\t%u ms\n", time_after(j->next_reclaim, now)
1276 ? jiffies_to_msecs(j->next_reclaim - jiffies) : 0);
1277 prt_printf(out, "current entry sectors:\t%u\n", j->cur_entry_sectors);
1278 prt_printf(out, "current entry error:\t%s\n", bch2_journal_errors[j->cur_entry_error]);
1279 prt_printf(out, "current entry:\t\t");
1281 switch (s.cur_entry_offset) {
1282 case JOURNAL_ENTRY_ERROR_VAL:
1283 prt_printf(out, "error");
1285 case JOURNAL_ENTRY_CLOSED_VAL:
1286 prt_printf(out, "closed");
1289 prt_printf(out, "%u/%u", s.cur_entry_offset, j->cur_entry_u64s);
1295 for (seq = journal_cur_seq(j);
1296 seq >= journal_last_unwritten_seq(j);
1298 i = seq & JOURNAL_BUF_MASK;
1300 prt_printf(out, "unwritten entry:");
1302 prt_printf(out, "%llu", seq);
1304 printbuf_indent_add(out, 2);
1306 prt_printf(out, "refcount:");
1308 prt_printf(out, "%u", journal_state_count(s, i));
1311 prt_printf(out, "sectors:");
1313 prt_printf(out, "%u", j->buf[i].sectors);
1316 prt_printf(out, "expires");
1318 prt_printf(out, "%li jiffies", j->buf[i].expires - jiffies);
1321 printbuf_indent_sub(out, 2);
1325 "replay done:\t\t%i\n",
1326 test_bit(JOURNAL_REPLAY_DONE, &j->flags));
1328 prt_printf(out, "space:\n");
1329 prt_printf(out, "\tdiscarded\t%u:%u\n",
1330 j->space[journal_space_discarded].next_entry,
1331 j->space[journal_space_discarded].total);
1332 prt_printf(out, "\tclean ondisk\t%u:%u\n",
1333 j->space[journal_space_clean_ondisk].next_entry,
1334 j->space[journal_space_clean_ondisk].total);
1335 prt_printf(out, "\tclean\t\t%u:%u\n",
1336 j->space[journal_space_clean].next_entry,
1337 j->space[journal_space_clean].total);
1338 prt_printf(out, "\ttotal\t\t%u:%u\n",
1339 j->space[journal_space_total].next_entry,
1340 j->space[journal_space_total].total);
1342 for_each_member_device_rcu(ca, c, i,
1343 &c->rw_devs[BCH_DATA_journal]) {
1344 struct journal_device *ja = &ca->journal;
1346 if (!test_bit(ca->dev_idx, c->rw_devs[BCH_DATA_journal].d))
1352 prt_printf(out, "dev %u:\n", i);
1353 prt_printf(out, "\tnr\t\t%u\n", ja->nr);
1354 prt_printf(out, "\tbucket size\t%u\n", ca->mi.bucket_size);
1355 prt_printf(out, "\tavailable\t%u:%u\n", bch2_journal_dev_buckets_available(j, ja, journal_space_discarded), ja->sectors_free);
1356 prt_printf(out, "\tdiscard_idx\t%u\n", ja->discard_idx);
1357 prt_printf(out, "\tdirty_ondisk\t%u (seq %llu)\n", ja->dirty_idx_ondisk, ja->bucket_seq[ja->dirty_idx_ondisk]);
1358 prt_printf(out, "\tdirty_idx\t%u (seq %llu)\n", ja->dirty_idx, ja->bucket_seq[ja->dirty_idx]);
1359 prt_printf(out, "\tcur_idx\t\t%u (seq %llu)\n", ja->cur_idx, ja->bucket_seq[ja->cur_idx]);
1367 void bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1369 spin_lock(&j->lock);
1370 __bch2_journal_debug_to_text(out, j);
1371 spin_unlock(&j->lock);
1374 bool bch2_journal_seq_pins_to_text(struct printbuf *out, struct journal *j, u64 *seq)
1376 struct journal_entry_pin_list *pin_list;
1377 struct journal_entry_pin *pin;
1379 spin_lock(&j->lock);
1380 *seq = max(*seq, j->pin.front);
1382 if (*seq >= j->pin.back) {
1383 spin_unlock(&j->lock);
1389 pin_list = journal_seq_pin(j, *seq);
1391 prt_printf(out, "%llu: count %u", *seq, atomic_read(&pin_list->count));
1393 printbuf_indent_add(out, 2);
1395 list_for_each_entry(pin, &pin_list->list, list) {
1396 prt_printf(out, "\t%px %ps", pin, pin->flush);
1400 list_for_each_entry(pin, &pin_list->key_cache_list, list) {
1401 prt_printf(out, "\t%px %ps", pin, pin->flush);
1405 if (!list_empty(&pin_list->flushed)) {
1406 prt_printf(out, "flushed:");
1410 list_for_each_entry(pin, &pin_list->flushed, list) {
1411 prt_printf(out, "\t%px %ps", pin, pin->flush);
1415 printbuf_indent_sub(out, 2);
1418 spin_unlock(&j->lock);
1423 void bch2_journal_pins_to_text(struct printbuf *out, struct journal *j)
1427 while (!bch2_journal_seq_pins_to_text(out, j, &seq))