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_and_count(c, 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_and_count(c, 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) {
812 ret = -BCH_ERR_ENOSPC_bucket_alloc;
816 ob[nr_got] = bch2_bucket_alloc(c, ca, RESERVE_none,
818 if (IS_ERR(ob[nr_got])) {
821 : -BCH_ERR_ENOSPC_bucket_alloc;
825 bu[nr_got] = ob[nr_got]->bucket;
833 * We may be called from the device add path, before the new device has
834 * actually been added to the running filesystem:
837 spin_lock(&c->journal.lock);
839 memcpy(new_buckets, ja->buckets, ja->nr * sizeof(u64));
840 memcpy(new_bucket_seq, ja->bucket_seq, ja->nr * sizeof(u64));
841 swap(new_buckets, ja->buckets);
842 swap(new_bucket_seq, ja->bucket_seq);
844 for (i = 0; i < nr_got; i++) {
845 unsigned pos = ja->discard_idx ?: ja->nr;
848 __array_insert_item(ja->buckets, ja->nr, pos);
849 __array_insert_item(ja->bucket_seq, ja->nr, pos);
852 ja->buckets[pos] = b;
853 ja->bucket_seq[pos] = 0;
855 if (pos <= ja->discard_idx)
856 ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
857 if (pos <= ja->dirty_idx_ondisk)
858 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
859 if (pos <= ja->dirty_idx)
860 ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
861 if (pos <= ja->cur_idx)
862 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
865 ret = bch2_journal_buckets_to_sb(c, ca);
868 swap(new_buckets, ja->buckets);
869 swap(new_bucket_seq, ja->bucket_seq);
871 ja->discard_idx = old_discard_idx;
872 ja->dirty_idx_ondisk = old_dirty_idx_ondisk;
873 ja->dirty_idx = old_dirty_idx;
874 ja->cur_idx = old_cur_idx;
878 spin_unlock(&c->journal.lock);
881 bch2_journal_unblock(&c->journal);
887 for (i = 0; i < nr_got; i++) {
888 ret = bch2_trans_run(c,
889 bch2_trans_mark_metadata_bucket(&trans, ca,
890 bu[i], BCH_DATA_journal,
891 ca->mi.bucket_size));
893 bch2_fs_inconsistent(c, "error marking new journal buckets: %i", ret);
900 for (i = 0; i < nr_got; i++)
901 bch2_open_bucket_put(c, ob[i]);
903 kfree(new_bucket_seq);
911 bch2_journal_unblock(&c->journal);
916 * Allocate more journal space at runtime - not currently making use if it, but
919 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
922 struct journal_device *ja = &ca->journal;
927 /* don't handle reducing nr of buckets yet: */
931 closure_init_stack(&cl);
933 while (ja->nr != nr && (ret == 0 || ret == -EAGAIN)) {
934 struct disk_reservation disk_res = { 0, 0 };
938 mutex_lock(&c->sb_lock);
942 * note: journal buckets aren't really counted as _sectors_ used yet, so
943 * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
944 * when space used goes up without a reservation - but we do need the
945 * reservation to ensure we'll actually be able to allocate:
948 ret = bch2_disk_reservation_get(c, &disk_res,
949 bucket_to_sector(ca, nr - ja->nr), 1, 0);
951 mutex_unlock(&c->sb_lock);
955 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
957 bch2_disk_reservation_put(c, &disk_res);
959 if (ja->nr != current_nr)
961 mutex_unlock(&c->sb_lock);
967 int bch2_dev_journal_alloc(struct bch_dev *ca)
972 if (dynamic_fault("bcachefs:add:journal_alloc"))
975 /* 1/128th of the device by default: */
976 nr = ca->mi.nbuckets >> 7;
979 * clamp journal size to 8192 buckets or 8GB (in sectors), whichever
982 nr = clamp_t(unsigned, nr,
983 BCH_JOURNAL_BUCKETS_MIN,
985 (1 << 24) / ca->mi.bucket_size));
988 mutex_lock(&ca->fs->sb_lock);
990 ret = __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
993 mutex_unlock(&ca->fs->sb_lock);
998 /* startup/shutdown: */
1000 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
1005 spin_lock(&j->lock);
1006 for (seq = journal_last_unwritten_seq(j);
1007 seq <= journal_cur_seq(j) && !ret;
1009 struct journal_buf *buf = journal_seq_to_buf(j, seq);
1011 if (bch2_bkey_has_device(bkey_i_to_s_c(&buf->key), dev_idx))
1014 spin_unlock(&j->lock);
1019 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
1021 wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
1024 void bch2_fs_journal_stop(struct journal *j)
1026 bch2_journal_reclaim_stop(j);
1027 bch2_journal_flush_all_pins(j);
1029 wait_event(j->wait, journal_entry_close(j));
1032 * Always write a new journal entry, to make sure the clock hands are up
1033 * to date (and match the superblock)
1035 bch2_journal_meta(j);
1039 BUG_ON(!bch2_journal_error(j) &&
1040 test_bit(JOURNAL_REPLAY_DONE, &j->flags) &&
1041 j->last_empty_seq != journal_cur_seq(j));
1043 cancel_delayed_work_sync(&j->write_work);
1046 int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
1048 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1049 struct journal_entry_pin_list *p;
1050 struct journal_replay *i, **_i;
1051 struct genradix_iter iter;
1052 bool had_entries = false;
1054 u64 last_seq = cur_seq, nr, seq;
1056 genradix_for_each_reverse(&c->journal_entries, iter, _i) {
1059 if (!i || i->ignore)
1062 last_seq = le64_to_cpu(i->j.last_seq);
1066 nr = cur_seq - last_seq;
1068 if (nr + 1 > j->pin.size) {
1070 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
1072 bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
1077 j->replay_journal_seq = last_seq;
1078 j->replay_journal_seq_end = cur_seq;
1079 j->last_seq_ondisk = last_seq;
1080 j->flushed_seq_ondisk = cur_seq - 1;
1081 j->seq_ondisk = cur_seq - 1;
1082 j->pin.front = last_seq;
1083 j->pin.back = cur_seq;
1084 atomic64_set(&j->seq, cur_seq - 1);
1086 fifo_for_each_entry_ptr(p, &j->pin, seq)
1087 journal_pin_list_init(p, 1);
1089 genradix_for_each(&c->journal_entries, iter, _i) {
1092 if (!i || i->ignore)
1095 seq = le64_to_cpu(i->j.seq);
1096 BUG_ON(seq >= cur_seq);
1101 if (journal_entry_empty(&i->j))
1102 j->last_empty_seq = le64_to_cpu(i->j.seq);
1104 p = journal_seq_pin(j, seq);
1107 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1108 bch2_dev_list_add_dev(&p->devs, i->ptrs[ptr].dev);
1114 j->last_empty_seq = cur_seq;
1116 spin_lock(&j->lock);
1118 set_bit(JOURNAL_STARTED, &j->flags);
1119 j->last_flush_write = jiffies;
1121 j->reservations.idx = j->reservations.unwritten_idx = journal_cur_seq(j);
1122 j->reservations.unwritten_idx++;
1124 c->last_bucket_seq_cleanup = journal_cur_seq(j);
1126 bch2_journal_space_available(j);
1127 spin_unlock(&j->lock);
1129 return bch2_journal_reclaim_start(j);
1134 void bch2_dev_journal_exit(struct bch_dev *ca)
1136 kfree(ca->journal.bio);
1137 kfree(ca->journal.buckets);
1138 kfree(ca->journal.bucket_seq);
1140 ca->journal.bio = NULL;
1141 ca->journal.buckets = NULL;
1142 ca->journal.bucket_seq = NULL;
1145 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1147 struct journal_device *ja = &ca->journal;
1148 struct bch_sb_field_journal *journal_buckets =
1149 bch2_sb_get_journal(sb);
1150 struct bch_sb_field_journal_v2 *journal_buckets_v2 =
1151 bch2_sb_get_journal_v2(sb);
1152 unsigned i, nr_bvecs;
1156 if (journal_buckets_v2) {
1157 unsigned nr = bch2_sb_field_journal_v2_nr_entries(journal_buckets_v2);
1159 for (i = 0; i < nr; i++)
1160 ja->nr += le64_to_cpu(journal_buckets_v2->d[i].nr);
1161 } else if (journal_buckets) {
1162 ja->nr = bch2_nr_journal_buckets(journal_buckets);
1165 ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1166 if (!ja->bucket_seq)
1169 nr_bvecs = DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE);
1171 ca->journal.bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
1172 if (!ca->journal.bio)
1175 bio_init(ca->journal.bio, NULL, ca->journal.bio->bi_inline_vecs, nr_bvecs, 0);
1177 ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1181 if (journal_buckets_v2) {
1182 unsigned nr = bch2_sb_field_journal_v2_nr_entries(journal_buckets_v2);
1183 unsigned j, dst = 0;
1185 for (i = 0; i < nr; i++)
1186 for (j = 0; j < le64_to_cpu(journal_buckets_v2->d[i].nr); j++)
1187 ja->buckets[dst++] =
1188 le64_to_cpu(journal_buckets_v2->d[i].start) + j;
1189 } else if (journal_buckets) {
1190 for (i = 0; i < ja->nr; i++)
1191 ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1197 void bch2_fs_journal_exit(struct journal *j)
1201 for (i = 0; i < ARRAY_SIZE(j->buf); i++)
1202 kvpfree(j->buf[i].data, j->buf[i].buf_size);
1206 int bch2_fs_journal_init(struct journal *j)
1208 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1209 static struct lock_class_key res_key;
1213 pr_verbose_init(c->opts, "");
1215 spin_lock_init(&j->lock);
1216 spin_lock_init(&j->err_lock);
1217 init_waitqueue_head(&j->wait);
1218 INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1219 init_waitqueue_head(&j->reclaim_wait);
1220 init_waitqueue_head(&j->pin_flush_wait);
1221 mutex_init(&j->reclaim_lock);
1222 mutex_init(&j->discard_lock);
1224 lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1226 atomic64_set(&j->reservations.counter,
1227 ((union journal_res_state)
1228 { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1230 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL))) {
1235 for (i = 0; i < ARRAY_SIZE(j->buf); i++) {
1236 j->buf[i].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1237 j->buf[i].data = kvpmalloc(j->buf[i].buf_size, GFP_KERNEL);
1238 if (!j->buf[i].data) {
1244 j->pin.front = j->pin.back = 1;
1246 pr_verbose_init(c->opts, "ret %i", ret);
1252 void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1254 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1255 union journal_res_state s;
1257 unsigned long now = jiffies;
1261 if (!out->nr_tabstops)
1262 printbuf_tabstop_push(out, 24);
1266 s = READ_ONCE(j->reservations);
1268 prt_printf(out, "dirty journal entries:\t%llu/%llu\n",fifo_used(&j->pin), j->pin.size);
1269 prt_printf(out, "seq:\t\t\t%llu\n", journal_cur_seq(j));
1270 prt_printf(out, "seq_ondisk:\t\t%llu\n", j->seq_ondisk);
1271 prt_printf(out, "last_seq:\t\t%llu\n", journal_last_seq(j));
1272 prt_printf(out, "last_seq_ondisk:\t%llu\n", j->last_seq_ondisk);
1273 prt_printf(out, "flushed_seq_ondisk:\t%llu\n", j->flushed_seq_ondisk);
1274 prt_printf(out, "prereserved:\t\t%u/%u\n", j->prereserved.reserved, j->prereserved.remaining);
1275 prt_printf(out, "watermark:\t\t%s\n", bch2_journal_watermarks[j->watermark]);
1276 prt_printf(out, "each entry reserved:\t%u\n", j->entry_u64s_reserved);
1277 prt_printf(out, "nr flush writes:\t%llu\n", j->nr_flush_writes);
1278 prt_printf(out, "nr noflush writes:\t%llu\n", j->nr_noflush_writes);
1279 prt_printf(out, "nr direct reclaim:\t%llu\n", j->nr_direct_reclaim);
1280 prt_printf(out, "nr background reclaim:\t%llu\n", j->nr_background_reclaim);
1281 prt_printf(out, "reclaim kicked:\t\t%u\n", j->reclaim_kicked);
1282 prt_printf(out, "reclaim runs in:\t%u ms\n", time_after(j->next_reclaim, now)
1283 ? jiffies_to_msecs(j->next_reclaim - jiffies) : 0);
1284 prt_printf(out, "current entry sectors:\t%u\n", j->cur_entry_sectors);
1285 prt_printf(out, "current entry error:\t%s\n", bch2_journal_errors[j->cur_entry_error]);
1286 prt_printf(out, "current entry:\t\t");
1288 switch (s.cur_entry_offset) {
1289 case JOURNAL_ENTRY_ERROR_VAL:
1290 prt_printf(out, "error");
1292 case JOURNAL_ENTRY_CLOSED_VAL:
1293 prt_printf(out, "closed");
1296 prt_printf(out, "%u/%u", s.cur_entry_offset, j->cur_entry_u64s);
1302 for (seq = journal_cur_seq(j);
1303 seq >= journal_last_unwritten_seq(j);
1305 i = seq & JOURNAL_BUF_MASK;
1307 prt_printf(out, "unwritten entry:");
1309 prt_printf(out, "%llu", seq);
1311 printbuf_indent_add(out, 2);
1313 prt_printf(out, "refcount:");
1315 prt_printf(out, "%u", journal_state_count(s, i));
1318 prt_printf(out, "sectors:");
1320 prt_printf(out, "%u", j->buf[i].sectors);
1323 prt_printf(out, "expires");
1325 prt_printf(out, "%li jiffies", j->buf[i].expires - jiffies);
1328 printbuf_indent_sub(out, 2);
1332 "replay done:\t\t%i\n",
1333 test_bit(JOURNAL_REPLAY_DONE, &j->flags));
1335 prt_printf(out, "space:\n");
1336 prt_printf(out, "\tdiscarded\t%u:%u\n",
1337 j->space[journal_space_discarded].next_entry,
1338 j->space[journal_space_discarded].total);
1339 prt_printf(out, "\tclean ondisk\t%u:%u\n",
1340 j->space[journal_space_clean_ondisk].next_entry,
1341 j->space[journal_space_clean_ondisk].total);
1342 prt_printf(out, "\tclean\t\t%u:%u\n",
1343 j->space[journal_space_clean].next_entry,
1344 j->space[journal_space_clean].total);
1345 prt_printf(out, "\ttotal\t\t%u:%u\n",
1346 j->space[journal_space_total].next_entry,
1347 j->space[journal_space_total].total);
1349 for_each_member_device_rcu(ca, c, i,
1350 &c->rw_devs[BCH_DATA_journal]) {
1351 struct journal_device *ja = &ca->journal;
1353 if (!test_bit(ca->dev_idx, c->rw_devs[BCH_DATA_journal].d))
1359 prt_printf(out, "dev %u:\n", i);
1360 prt_printf(out, "\tnr\t\t%u\n", ja->nr);
1361 prt_printf(out, "\tbucket size\t%u\n", ca->mi.bucket_size);
1362 prt_printf(out, "\tavailable\t%u:%u\n", bch2_journal_dev_buckets_available(j, ja, journal_space_discarded), ja->sectors_free);
1363 prt_printf(out, "\tdiscard_idx\t%u\n", ja->discard_idx);
1364 prt_printf(out, "\tdirty_ondisk\t%u (seq %llu)\n", ja->dirty_idx_ondisk, ja->bucket_seq[ja->dirty_idx_ondisk]);
1365 prt_printf(out, "\tdirty_idx\t%u (seq %llu)\n", ja->dirty_idx, ja->bucket_seq[ja->dirty_idx]);
1366 prt_printf(out, "\tcur_idx\t\t%u (seq %llu)\n", ja->cur_idx, ja->bucket_seq[ja->cur_idx]);
1374 void bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1376 spin_lock(&j->lock);
1377 __bch2_journal_debug_to_text(out, j);
1378 spin_unlock(&j->lock);
1381 bool bch2_journal_seq_pins_to_text(struct printbuf *out, struct journal *j, u64 *seq)
1383 struct journal_entry_pin_list *pin_list;
1384 struct journal_entry_pin *pin;
1386 spin_lock(&j->lock);
1387 *seq = max(*seq, j->pin.front);
1389 if (*seq >= j->pin.back) {
1390 spin_unlock(&j->lock);
1396 pin_list = journal_seq_pin(j, *seq);
1398 prt_printf(out, "%llu: count %u", *seq, atomic_read(&pin_list->count));
1400 printbuf_indent_add(out, 2);
1402 list_for_each_entry(pin, &pin_list->list, list) {
1403 prt_printf(out, "\t%px %ps", pin, pin->flush);
1407 list_for_each_entry(pin, &pin_list->key_cache_list, list) {
1408 prt_printf(out, "\t%px %ps", pin, pin->flush);
1412 if (!list_empty(&pin_list->flushed)) {
1413 prt_printf(out, "flushed:");
1417 list_for_each_entry(pin, &pin_list->flushed, list) {
1418 prt_printf(out, "\t%px %ps", pin, pin->flush);
1422 printbuf_indent_sub(out, 2);
1425 spin_unlock(&j->lock);
1430 void bch2_journal_pins_to_text(struct printbuf *out, struct journal *j)
1434 while (!bch2_journal_seq_pins_to_text(out, j, &seq))