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"
13 #include "btree_write_buffer.h"
17 #include "journal_io.h"
18 #include "journal_reclaim.h"
19 #include "journal_sb.h"
20 #include "journal_seq_blacklist.h"
23 static const char * const bch2_journal_errors[] = {
30 static inline bool journal_seq_unwritten(struct journal *j, u64 seq)
32 return seq > j->seq_ondisk;
35 static bool __journal_entry_is_open(union journal_res_state state)
37 return state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL;
40 static inline unsigned nr_unwritten_journal_entries(struct journal *j)
42 return atomic64_read(&j->seq) - j->seq_ondisk;
45 static bool journal_entry_is_open(struct journal *j)
47 return __journal_entry_is_open(j->reservations);
50 static inline struct journal_buf *
51 journal_seq_to_buf(struct journal *j, u64 seq)
53 struct journal_buf *buf = NULL;
55 EBUG_ON(seq > journal_cur_seq(j));
57 if (journal_seq_unwritten(j, seq)) {
58 buf = j->buf + (seq & JOURNAL_BUF_MASK);
59 EBUG_ON(le64_to_cpu(buf->data->seq) != seq);
64 static void journal_pin_list_init(struct journal_entry_pin_list *p, int count)
68 for (i = 0; i < ARRAY_SIZE(p->list); i++)
69 INIT_LIST_HEAD(&p->list[i]);
70 INIT_LIST_HEAD(&p->flushed);
71 atomic_set(&p->count, count);
76 * Detect stuck journal conditions and trigger shutdown. Technically the journal
77 * can end up stuck for a variety of reasons, such as a blocked I/O, journal
78 * reservation lockup, etc. Since this is a fatal error with potentially
79 * unpredictable characteristics, we want to be fairly conservative before we
80 * decide to shut things down.
82 * Consider the journal stuck when it appears full with no ability to commit
83 * btree transactions, to discard journal buckets, nor acquire priority
84 * (reserved watermark) reservation.
87 journal_error_check_stuck(struct journal *j, int error, unsigned flags)
89 struct bch_fs *c = container_of(j, struct bch_fs, journal);
91 struct printbuf buf = PRINTBUF;
93 if (!(error == JOURNAL_ERR_journal_full ||
94 error == JOURNAL_ERR_journal_pin_full) ||
95 nr_unwritten_journal_entries(j) ||
96 (flags & BCH_WATERMARK_MASK) != BCH_WATERMARK_reclaim)
101 if (j->can_discard) {
102 spin_unlock(&j->lock);
109 * The journal shutdown path will set ->err_seq, but do it here first to
110 * serialize against concurrent failures and avoid duplicate error
114 spin_unlock(&j->lock);
117 j->err_seq = journal_cur_seq(j);
118 spin_unlock(&j->lock);
120 bch_err(c, "Journal stuck! Hava a pre-reservation but journal full (error %s)",
121 bch2_journal_errors[error]);
122 bch2_journal_debug_to_text(&buf, j);
123 bch_err(c, "%s", buf.buf);
125 printbuf_reset(&buf);
126 bch2_journal_pins_to_text(&buf, j);
127 bch_err(c, "Journal pins:\n%s", buf.buf);
137 * Final processing when the last reference of a journal buffer has been
138 * dropped. Drop the pin list reference acquired at journal entry open and write
139 * the buffer, if requested.
141 void bch2_journal_buf_put_final(struct journal *j, u64 seq, bool write)
143 struct bch_fs *c = container_of(j, struct bch_fs, journal);
145 lockdep_assert_held(&j->lock);
147 if (__bch2_journal_pin_put(j, seq))
148 bch2_journal_reclaim_fast(j);
150 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
154 * Returns true if journal entry is now closed:
156 * We don't close a journal_buf until the next journal_buf is finished writing,
157 * and can be opened again - this also initializes the next journal_buf:
159 static void __journal_entry_close(struct journal *j, unsigned closed_val)
161 struct bch_fs *c = container_of(j, struct bch_fs, journal);
162 struct journal_buf *buf = journal_cur_buf(j);
163 union journal_res_state old, new;
164 u64 v = atomic64_read(&j->reservations.counter);
167 BUG_ON(closed_val != JOURNAL_ENTRY_CLOSED_VAL &&
168 closed_val != JOURNAL_ENTRY_ERROR_VAL);
170 lockdep_assert_held(&j->lock);
174 new.cur_entry_offset = closed_val;
176 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL ||
177 old.cur_entry_offset == new.cur_entry_offset)
179 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
180 old.v, new.v)) != old.v);
182 if (!__journal_entry_is_open(old))
185 /* Close out old buffer: */
186 buf->data->u64s = cpu_to_le32(old.cur_entry_offset);
188 trace_journal_entry_close(c, vstruct_bytes(buf->data));
190 sectors = vstruct_blocks_plus(buf->data, c->block_bits,
191 buf->u64s_reserved) << c->block_bits;
192 BUG_ON(sectors > buf->sectors);
193 buf->sectors = sectors;
196 * We have to set last_seq here, _before_ opening a new journal entry:
198 * A threads may replace an old pin with a new pin on their current
199 * journal reservation - the expectation being that the journal will
200 * contain either what the old pin protected or what the new pin
203 * After the old pin is dropped journal_last_seq() won't include the old
204 * pin, so we can only write the updated last_seq on the entry that
205 * contains whatever the new pin protects.
207 * Restated, we can _not_ update last_seq for a given entry if there
208 * could be a newer entry open with reservations/pins that have been
211 * Hence, we want update/set last_seq on the current journal entry right
212 * before we open a new one:
214 buf->last_seq = journal_last_seq(j);
215 buf->data->last_seq = cpu_to_le64(buf->last_seq);
216 BUG_ON(buf->last_seq > le64_to_cpu(buf->data->seq));
218 cancel_delayed_work(&j->write_work);
220 bch2_journal_space_available(j);
222 __bch2_journal_buf_put(j, old.idx, le64_to_cpu(buf->data->seq));
225 void bch2_journal_halt(struct journal *j)
228 __journal_entry_close(j, JOURNAL_ENTRY_ERROR_VAL);
230 j->err_seq = journal_cur_seq(j);
232 spin_unlock(&j->lock);
235 static bool journal_entry_want_write(struct journal *j)
237 bool ret = !journal_entry_is_open(j) ||
238 journal_cur_seq(j) == journal_last_unwritten_seq(j);
240 /* Don't close it yet if we already have a write in flight: */
242 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
243 else if (nr_unwritten_journal_entries(j)) {
244 struct journal_buf *buf = journal_cur_buf(j);
246 if (!buf->flush_time) {
247 buf->flush_time = local_clock() ?: 1;
248 buf->expires = jiffies;
255 bool bch2_journal_entry_close(struct journal *j)
260 ret = journal_entry_want_write(j);
261 spin_unlock(&j->lock);
267 * should _only_ called from journal_res_get() - when we actually want a
268 * journal reservation - journal entry is open means journal is dirty:
270 static int journal_entry_open(struct journal *j)
272 struct bch_fs *c = container_of(j, struct bch_fs, journal);
273 struct journal_buf *buf = j->buf +
274 ((journal_cur_seq(j) + 1) & JOURNAL_BUF_MASK);
275 union journal_res_state old, new;
279 lockdep_assert_held(&j->lock);
280 BUG_ON(journal_entry_is_open(j));
281 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
284 return JOURNAL_ERR_blocked;
286 if (j->cur_entry_error)
287 return j->cur_entry_error;
289 if (bch2_journal_error(j))
290 return JOURNAL_ERR_insufficient_devices; /* -EROFS */
292 if (!fifo_free(&j->pin))
293 return JOURNAL_ERR_journal_pin_full;
295 if (nr_unwritten_journal_entries(j) == ARRAY_SIZE(j->buf))
296 return JOURNAL_ERR_max_in_flight;
298 BUG_ON(!j->cur_entry_sectors);
301 (journal_cur_seq(j) == j->flushed_seq_ondisk
303 : j->last_flush_write) +
304 msecs_to_jiffies(c->opts.journal_flush_delay);
306 buf->u64s_reserved = j->entry_u64s_reserved;
307 buf->disk_sectors = j->cur_entry_sectors;
308 buf->sectors = min(buf->disk_sectors, buf->buf_size >> 9);
310 u64s = (int) (buf->sectors << 9) / sizeof(u64) -
311 journal_entry_overhead(j);
312 u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
314 if (u64s <= (ssize_t) j->early_journal_entries.nr)
315 return JOURNAL_ERR_journal_full;
317 if (fifo_empty(&j->pin) && j->reclaim_thread)
318 wake_up_process(j->reclaim_thread);
321 * The fifo_push() needs to happen at the same time as j->seq is
322 * incremented for journal_last_seq() to be calculated correctly
324 atomic64_inc(&j->seq);
325 journal_pin_list_init(fifo_push_ref(&j->pin), 1);
327 BUG_ON(j->pin.back - 1 != atomic64_read(&j->seq));
329 BUG_ON(j->buf + (journal_cur_seq(j) & JOURNAL_BUF_MASK) != buf);
331 bkey_extent_init(&buf->key);
332 buf->noflush = false;
333 buf->must_flush = false;
334 buf->separate_flush = false;
336 buf->need_flush_to_write_buffer = true;
338 memset(buf->data, 0, sizeof(*buf->data));
339 buf->data->seq = cpu_to_le64(journal_cur_seq(j));
342 if (j->early_journal_entries.nr) {
343 memcpy(buf->data->_data, j->early_journal_entries.data,
344 j->early_journal_entries.nr * sizeof(u64));
345 le32_add_cpu(&buf->data->u64s, j->early_journal_entries.nr);
349 * Must be set before marking the journal entry as open:
351 j->cur_entry_u64s = u64s;
353 v = atomic64_read(&j->reservations.counter);
357 BUG_ON(old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL);
360 BUG_ON(journal_state_count(new, new.idx));
361 BUG_ON(new.idx != (journal_cur_seq(j) & JOURNAL_BUF_MASK));
363 journal_state_inc(&new);
365 /* Handle any already added entries */
366 new.cur_entry_offset = le32_to_cpu(buf->data->u64s);
367 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
368 old.v, new.v)) != old.v);
370 mod_delayed_work(c->io_complete_wq,
372 msecs_to_jiffies(c->opts.journal_flush_delay));
375 if (j->early_journal_entries.nr)
376 darray_exit(&j->early_journal_entries);
380 static bool journal_quiesced(struct journal *j)
382 bool ret = atomic64_read(&j->seq) == j->seq_ondisk;
385 bch2_journal_entry_close(j);
389 static void journal_quiesce(struct journal *j)
391 wait_event(j->wait, journal_quiesced(j));
394 static void journal_write_work(struct work_struct *work)
396 struct journal *j = container_of(work, struct journal, write_work.work);
397 struct bch_fs *c = container_of(j, struct bch_fs, journal);
401 if (!__journal_entry_is_open(j->reservations))
404 delta = journal_cur_buf(j)->expires - jiffies;
407 mod_delayed_work(c->io_complete_wq, &j->write_work, delta);
409 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
411 spin_unlock(&j->lock);
414 static int __journal_res_get(struct journal *j, struct journal_res *res,
417 struct bch_fs *c = container_of(j, struct bch_fs, journal);
418 struct journal_buf *buf;
422 if (journal_res_get_fast(j, res, flags))
425 if (bch2_journal_error(j))
426 return -BCH_ERR_erofs_journal_err;
430 /* check once more in case somebody else shut things down... */
431 if (bch2_journal_error(j)) {
432 spin_unlock(&j->lock);
433 return -BCH_ERR_erofs_journal_err;
437 * Recheck after taking the lock, so we don't race with another thread
438 * that just did journal_entry_open() and call bch2_journal_entry_close()
441 if (journal_res_get_fast(j, res, flags)) {
442 spin_unlock(&j->lock);
446 if ((flags & BCH_WATERMARK_MASK) < j->watermark) {
448 * Don't want to close current journal entry, just need to
451 ret = JOURNAL_ERR_journal_full;
456 * If we couldn't get a reservation because the current buf filled up,
457 * and we had room for a bigger entry on disk, signal that we want to
458 * realloc the journal bufs:
460 buf = journal_cur_buf(j);
461 if (journal_entry_is_open(j) &&
462 buf->buf_size >> 9 < buf->disk_sectors &&
463 buf->buf_size < JOURNAL_ENTRY_SIZE_MAX)
464 j->buf_size_want = max(j->buf_size_want, buf->buf_size << 1);
466 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
467 ret = journal_entry_open(j);
469 if (ret == JOURNAL_ERR_max_in_flight) {
470 track_event_change(&c->times[BCH_TIME_blocked_journal_max_in_flight],
471 &j->max_in_flight_start, true);
472 trace_and_count(c, journal_entry_full, c);
475 can_discard = j->can_discard;
476 spin_unlock(&j->lock);
480 if (journal_error_check_stuck(j, ret, flags))
481 ret = -BCH_ERR_journal_res_get_blocked;
484 * Journal is full - can't rely on reclaim from work item due to
487 if ((ret == JOURNAL_ERR_journal_full ||
488 ret == JOURNAL_ERR_journal_pin_full) &&
489 !(flags & JOURNAL_RES_GET_NONBLOCK)) {
491 bch2_journal_do_discards(j);
495 if (mutex_trylock(&j->reclaim_lock)) {
496 bch2_journal_reclaim(j);
497 mutex_unlock(&j->reclaim_lock);
501 return ret == JOURNAL_ERR_insufficient_devices
502 ? -BCH_ERR_erofs_journal_err
503 : -BCH_ERR_journal_res_get_blocked;
507 * Essentially the entry function to the journaling code. When bcachefs is doing
508 * a btree insert, it calls this function to get the current journal write.
509 * Journal write is the structure used set up journal writes. The calling
510 * function will then add its keys to the structure, queuing them for the next
513 * To ensure forward progress, the current task must not be holding any
514 * btree node write locks.
516 int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
521 closure_wait_event(&j->async_wait,
522 (ret = __journal_res_get(j, res, flags)) != -BCH_ERR_journal_res_get_blocked ||
523 (flags & JOURNAL_RES_GET_NONBLOCK));
527 /* journal_entry_res: */
529 void bch2_journal_entry_res_resize(struct journal *j,
530 struct journal_entry_res *res,
533 union journal_res_state state;
534 int d = new_u64s - res->u64s;
538 j->entry_u64s_reserved += d;
542 j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
544 state = READ_ONCE(j->reservations);
546 if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
547 state.cur_entry_offset > j->cur_entry_u64s) {
548 j->cur_entry_u64s += d;
550 * Not enough room in current journal entry, have to flush it:
552 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
554 journal_cur_buf(j)->u64s_reserved += d;
557 spin_unlock(&j->lock);
561 /* journal flushing: */
564 * bch2_journal_flush_seq_async - wait for a journal entry to be written
567 * @parent: closure object to wait with
568 * Returns: 1 if @seq has already been flushed, 0 if @seq is being flushed,
569 * -EIO if @seq will never be flushed
571 * Like bch2_journal_wait_on_seq, except that it triggers a write immediately if
574 int bch2_journal_flush_seq_async(struct journal *j, u64 seq,
575 struct closure *parent)
577 struct journal_buf *buf;
580 if (seq <= j->flushed_seq_ondisk)
585 if (WARN_ONCE(seq > journal_cur_seq(j),
586 "requested to flush journal seq %llu, but currently at %llu",
587 seq, journal_cur_seq(j)))
590 /* Recheck under lock: */
591 if (j->err_seq && seq >= j->err_seq) {
596 if (seq <= j->flushed_seq_ondisk) {
601 /* if seq was written, but not flushed - flush a newer one instead */
602 seq = max(seq, journal_last_unwritten_seq(j));
605 if (seq > journal_cur_seq(j)) {
606 struct journal_res res = { 0 };
608 if (journal_entry_is_open(j))
609 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
611 spin_unlock(&j->lock);
613 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
618 buf = j->buf + (seq & JOURNAL_BUF_MASK);
619 buf->must_flush = true;
621 if (!buf->flush_time) {
622 buf->flush_time = local_clock() ?: 1;
623 buf->expires = jiffies;
626 if (parent && !closure_wait(&buf->wait, parent))
629 bch2_journal_res_put(j, &res);
636 * if write was kicked off without a flush, flush the next sequence
639 buf = journal_seq_to_buf(j, seq);
642 goto recheck_need_open;
645 buf->must_flush = true;
647 if (parent && !closure_wait(&buf->wait, parent))
650 if (seq == journal_cur_seq(j))
651 journal_entry_want_write(j);
653 spin_unlock(&j->lock);
657 int bch2_journal_flush_seq(struct journal *j, u64 seq)
659 u64 start_time = local_clock();
663 * Don't update time_stats when @seq is already flushed:
665 if (seq <= j->flushed_seq_ondisk)
668 ret = wait_event_interruptible(j->wait, (ret2 = bch2_journal_flush_seq_async(j, seq, NULL)));
671 bch2_time_stats_update(j->flush_seq_time, start_time);
673 return ret ?: ret2 < 0 ? ret2 : 0;
677 * bch2_journal_flush_async - if there is an open journal entry, or a journal
678 * still being written, write it and wait for the write to complete
680 void bch2_journal_flush_async(struct journal *j, struct closure *parent)
682 bch2_journal_flush_seq_async(j, atomic64_read(&j->seq), parent);
685 int bch2_journal_flush(struct journal *j)
687 return bch2_journal_flush_seq(j, atomic64_read(&j->seq));
691 * bch2_journal_noflush_seq - tell the journal not to issue any flushes before
694 bool bch2_journal_noflush_seq(struct journal *j, u64 seq)
696 struct bch_fs *c = container_of(j, struct bch_fs, journal);
700 if (!(c->sb.features & (1ULL << BCH_FEATURE_journal_no_flush)))
703 if (seq <= c->journal.flushed_seq_ondisk)
707 if (seq <= c->journal.flushed_seq_ondisk)
710 for (unwritten_seq = journal_last_unwritten_seq(j);
713 struct journal_buf *buf = journal_seq_to_buf(j, unwritten_seq);
715 /* journal write is already in flight, and was a flush write: */
716 if (unwritten_seq == journal_last_unwritten_seq(j) && !buf->noflush)
724 spin_unlock(&j->lock);
728 int bch2_journal_meta(struct journal *j)
730 struct journal_buf *buf;
731 struct journal_res res;
734 memset(&res, 0, sizeof(res));
736 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
740 buf = j->buf + (res.seq & JOURNAL_BUF_MASK);
741 buf->must_flush = true;
743 if (!buf->flush_time) {
744 buf->flush_time = local_clock() ?: 1;
745 buf->expires = jiffies;
748 bch2_journal_res_put(j, &res);
750 return bch2_journal_flush_seq(j, res.seq);
753 /* block/unlock the journal: */
755 void bch2_journal_unblock(struct journal *j)
759 spin_unlock(&j->lock);
764 void bch2_journal_block(struct journal *j)
768 spin_unlock(&j->lock);
773 static struct journal_buf *__bch2_next_write_buffer_flush_journal_buf(struct journal *j, u64 max_seq)
775 struct journal_buf *ret = NULL;
777 mutex_lock(&j->buf_lock);
779 max_seq = min(max_seq, journal_cur_seq(j));
781 for (u64 seq = journal_last_unwritten_seq(j);
784 unsigned idx = seq & JOURNAL_BUF_MASK;
785 struct journal_buf *buf = j->buf + idx;
787 if (buf->need_flush_to_write_buffer) {
788 if (seq == journal_cur_seq(j))
789 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
791 union journal_res_state s;
792 s.v = atomic64_read_acquire(&j->reservations.counter);
794 ret = journal_state_count(s, idx)
801 spin_unlock(&j->lock);
802 if (IS_ERR_OR_NULL(ret))
803 mutex_unlock(&j->buf_lock);
807 struct journal_buf *bch2_next_write_buffer_flush_journal_buf(struct journal *j, u64 max_seq)
809 struct journal_buf *ret;
811 wait_event(j->wait, (ret = __bch2_next_write_buffer_flush_journal_buf(j, max_seq)) != ERR_PTR(-EAGAIN));
815 /* allocate journal on a device: */
817 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
818 bool new_fs, struct closure *cl)
820 struct bch_fs *c = ca->fs;
821 struct journal_device *ja = &ca->journal;
822 u64 *new_bucket_seq = NULL, *new_buckets = NULL;
823 struct open_bucket **ob = NULL;
825 unsigned i, pos, nr_got = 0, nr_want = nr - ja->nr;
828 BUG_ON(nr <= ja->nr);
830 bu = kcalloc(nr_want, sizeof(*bu), GFP_KERNEL);
831 ob = kcalloc(nr_want, sizeof(*ob), GFP_KERNEL);
832 new_buckets = kcalloc(nr, sizeof(u64), GFP_KERNEL);
833 new_bucket_seq = kcalloc(nr, sizeof(u64), GFP_KERNEL);
834 if (!bu || !ob || !new_buckets || !new_bucket_seq) {
835 ret = -BCH_ERR_ENOMEM_set_nr_journal_buckets;
839 for (nr_got = 0; nr_got < nr_want; nr_got++) {
841 bu[nr_got] = bch2_bucket_alloc_new_fs(ca);
842 if (bu[nr_got] < 0) {
843 ret = -BCH_ERR_ENOSPC_bucket_alloc;
847 ob[nr_got] = bch2_bucket_alloc(c, ca, BCH_WATERMARK_normal, cl);
848 ret = PTR_ERR_OR_ZERO(ob[nr_got]);
852 ret = bch2_trans_run(c,
853 bch2_trans_mark_metadata_bucket(trans, ca,
854 ob[nr_got]->bucket, BCH_DATA_journal,
855 ca->mi.bucket_size));
857 bch2_open_bucket_put(c, ob[nr_got]);
858 bch_err_msg(c, ret, "marking new journal buckets");
862 bu[nr_got] = ob[nr_got]->bucket;
869 /* Don't return an error if we successfully allocated some buckets: */
873 bch2_journal_flush_all_pins(&c->journal);
874 bch2_journal_block(&c->journal);
875 mutex_lock(&c->sb_lock);
878 memcpy(new_buckets, ja->buckets, ja->nr * sizeof(u64));
879 memcpy(new_bucket_seq, ja->bucket_seq, ja->nr * sizeof(u64));
881 BUG_ON(ja->discard_idx > ja->nr);
883 pos = ja->discard_idx ?: ja->nr;
885 memmove(new_buckets + pos + nr_got,
887 sizeof(new_buckets[0]) * (ja->nr - pos));
888 memmove(new_bucket_seq + pos + nr_got,
889 new_bucket_seq + pos,
890 sizeof(new_bucket_seq[0]) * (ja->nr - pos));
892 for (i = 0; i < nr_got; i++) {
893 new_buckets[pos + i] = bu[i];
894 new_bucket_seq[pos + i] = 0;
897 nr = ja->nr + nr_got;
899 ret = bch2_journal_buckets_to_sb(c, ca, new_buckets, nr);
908 spin_lock(&c->journal.lock);
910 swap(new_buckets, ja->buckets);
911 swap(new_bucket_seq, ja->bucket_seq);
914 if (pos <= ja->discard_idx)
915 ja->discard_idx = (ja->discard_idx + nr_got) % ja->nr;
916 if (pos <= ja->dirty_idx_ondisk)
917 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + nr_got) % ja->nr;
918 if (pos <= ja->dirty_idx)
919 ja->dirty_idx = (ja->dirty_idx + nr_got) % ja->nr;
920 if (pos <= ja->cur_idx)
921 ja->cur_idx = (ja->cur_idx + nr_got) % ja->nr;
924 spin_unlock(&c->journal.lock);
927 bch2_journal_unblock(&c->journal);
928 mutex_unlock(&c->sb_lock);
932 for (i = 0; i < nr_got; i++)
934 bch2_trans_mark_metadata_bucket(trans, ca,
935 bu[i], BCH_DATA_free, 0));
938 for (i = 0; i < nr_got; i++)
939 bch2_open_bucket_put(c, ob[i]);
941 kfree(new_bucket_seq);
949 * Allocate more journal space at runtime - not currently making use if it, but
952 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
955 struct journal_device *ja = &ca->journal;
959 closure_init_stack(&cl);
961 down_write(&c->state_lock);
963 /* don't handle reducing nr of buckets yet: */
967 while (ja->nr < nr) {
968 struct disk_reservation disk_res = { 0, 0, 0 };
971 * note: journal buckets aren't really counted as _sectors_ used yet, so
972 * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
973 * when space used goes up without a reservation - but we do need the
974 * reservation to ensure we'll actually be able to allocate:
976 * XXX: that's not right, disk reservations only ensure a
977 * filesystem-wide allocation will succeed, this is a device
978 * specific allocation - we can hang here:
981 ret = bch2_disk_reservation_get(c, &disk_res,
982 bucket_to_sector(ca, nr - ja->nr), 1, 0);
986 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
988 bch2_disk_reservation_put(c, &disk_res);
992 if (ret && ret != -BCH_ERR_bucket_alloc_blocked)
998 up_write(&c->state_lock);
1002 int bch2_dev_journal_alloc(struct bch_dev *ca)
1007 if (dynamic_fault("bcachefs:add:journal_alloc")) {
1008 ret = -BCH_ERR_ENOMEM_set_nr_journal_buckets;
1012 /* 1/128th of the device by default: */
1013 nr = ca->mi.nbuckets >> 7;
1016 * clamp journal size to 8192 buckets or 8GB (in sectors), whichever
1019 nr = clamp_t(unsigned, nr,
1020 BCH_JOURNAL_BUCKETS_MIN,
1022 (1 << 24) / ca->mi.bucket_size));
1024 ret = __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
1026 bch_err_fn(ca, ret);
1030 int bch2_fs_journal_alloc(struct bch_fs *c)
1032 for_each_online_member(c, ca) {
1036 int ret = bch2_dev_journal_alloc(ca);
1038 percpu_ref_put(&ca->io_ref);
1046 /* startup/shutdown: */
1048 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
1053 spin_lock(&j->lock);
1054 for (seq = journal_last_unwritten_seq(j);
1055 seq <= journal_cur_seq(j) && !ret;
1057 struct journal_buf *buf = journal_seq_to_buf(j, seq);
1059 if (bch2_bkey_has_device_c(bkey_i_to_s_c(&buf->key), dev_idx))
1062 spin_unlock(&j->lock);
1067 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
1069 wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
1072 void bch2_fs_journal_stop(struct journal *j)
1074 bch2_journal_reclaim_stop(j);
1075 bch2_journal_flush_all_pins(j);
1077 wait_event(j->wait, bch2_journal_entry_close(j));
1080 * Always write a new journal entry, to make sure the clock hands are up
1081 * to date (and match the superblock)
1083 bch2_journal_meta(j);
1087 BUG_ON(!bch2_journal_error(j) &&
1088 test_bit(JOURNAL_REPLAY_DONE, &j->flags) &&
1089 j->last_empty_seq != journal_cur_seq(j));
1091 cancel_delayed_work_sync(&j->write_work);
1094 int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
1096 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1097 struct journal_entry_pin_list *p;
1098 struct journal_replay *i, **_i;
1099 struct genradix_iter iter;
1100 bool had_entries = false;
1102 u64 last_seq = cur_seq, nr, seq;
1104 genradix_for_each_reverse(&c->journal_entries, iter, _i) {
1107 if (!i || i->ignore)
1110 last_seq = le64_to_cpu(i->j.last_seq);
1114 nr = cur_seq - last_seq;
1116 if (nr + 1 > j->pin.size) {
1118 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
1120 bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
1121 return -BCH_ERR_ENOMEM_journal_pin_fifo;
1125 j->replay_journal_seq = last_seq;
1126 j->replay_journal_seq_end = cur_seq;
1127 j->last_seq_ondisk = last_seq;
1128 j->flushed_seq_ondisk = cur_seq - 1;
1129 j->seq_ondisk = cur_seq - 1;
1130 j->pin.front = last_seq;
1131 j->pin.back = cur_seq;
1132 atomic64_set(&j->seq, cur_seq - 1);
1134 fifo_for_each_entry_ptr(p, &j->pin, seq)
1135 journal_pin_list_init(p, 1);
1137 genradix_for_each(&c->journal_entries, iter, _i) {
1140 if (!i || i->ignore)
1143 seq = le64_to_cpu(i->j.seq);
1144 BUG_ON(seq >= cur_seq);
1149 if (journal_entry_empty(&i->j))
1150 j->last_empty_seq = le64_to_cpu(i->j.seq);
1152 p = journal_seq_pin(j, seq);
1155 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1156 bch2_dev_list_add_dev(&p->devs, i->ptrs[ptr].dev);
1162 j->last_empty_seq = cur_seq;
1164 spin_lock(&j->lock);
1166 set_bit(JOURNAL_STARTED, &j->flags);
1167 j->last_flush_write = jiffies;
1169 j->reservations.idx = j->reservations.unwritten_idx = journal_cur_seq(j);
1170 j->reservations.unwritten_idx++;
1172 c->last_bucket_seq_cleanup = journal_cur_seq(j);
1174 bch2_journal_space_available(j);
1175 spin_unlock(&j->lock);
1177 return bch2_journal_reclaim_start(j);
1182 void bch2_dev_journal_exit(struct bch_dev *ca)
1184 kfree(ca->journal.bio);
1185 kfree(ca->journal.buckets);
1186 kfree(ca->journal.bucket_seq);
1188 ca->journal.bio = NULL;
1189 ca->journal.buckets = NULL;
1190 ca->journal.bucket_seq = NULL;
1193 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1195 struct journal_device *ja = &ca->journal;
1196 struct bch_sb_field_journal *journal_buckets =
1197 bch2_sb_field_get(sb, journal);
1198 struct bch_sb_field_journal_v2 *journal_buckets_v2 =
1199 bch2_sb_field_get(sb, journal_v2);
1200 unsigned i, nr_bvecs;
1204 if (journal_buckets_v2) {
1205 unsigned nr = bch2_sb_field_journal_v2_nr_entries(journal_buckets_v2);
1207 for (i = 0; i < nr; i++)
1208 ja->nr += le64_to_cpu(journal_buckets_v2->d[i].nr);
1209 } else if (journal_buckets) {
1210 ja->nr = bch2_nr_journal_buckets(journal_buckets);
1213 ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1214 if (!ja->bucket_seq)
1215 return -BCH_ERR_ENOMEM_dev_journal_init;
1217 nr_bvecs = DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE);
1219 ca->journal.bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
1220 if (!ca->journal.bio)
1221 return -BCH_ERR_ENOMEM_dev_journal_init;
1223 bio_init(ca->journal.bio, NULL, ca->journal.bio->bi_inline_vecs, nr_bvecs, 0);
1225 ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1227 return -BCH_ERR_ENOMEM_dev_journal_init;
1229 if (journal_buckets_v2) {
1230 unsigned nr = bch2_sb_field_journal_v2_nr_entries(journal_buckets_v2);
1231 unsigned j, dst = 0;
1233 for (i = 0; i < nr; i++)
1234 for (j = 0; j < le64_to_cpu(journal_buckets_v2->d[i].nr); j++)
1235 ja->buckets[dst++] =
1236 le64_to_cpu(journal_buckets_v2->d[i].start) + j;
1237 } else if (journal_buckets) {
1238 for (i = 0; i < ja->nr; i++)
1239 ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1245 void bch2_fs_journal_exit(struct journal *j)
1249 darray_exit(&j->early_journal_entries);
1251 for (i = 0; i < ARRAY_SIZE(j->buf); i++)
1252 kvpfree(j->buf[i].data, j->buf[i].buf_size);
1256 int bch2_fs_journal_init(struct journal *j)
1258 static struct lock_class_key res_key;
1261 mutex_init(&j->buf_lock);
1262 spin_lock_init(&j->lock);
1263 spin_lock_init(&j->err_lock);
1264 init_waitqueue_head(&j->wait);
1265 INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1266 init_waitqueue_head(&j->reclaim_wait);
1267 init_waitqueue_head(&j->pin_flush_wait);
1268 mutex_init(&j->reclaim_lock);
1269 mutex_init(&j->discard_lock);
1271 lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1273 atomic64_set(&j->reservations.counter,
1274 ((union journal_res_state)
1275 { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1277 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL)))
1278 return -BCH_ERR_ENOMEM_journal_pin_fifo;
1280 for (i = 0; i < ARRAY_SIZE(j->buf); i++) {
1281 j->buf[i].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1282 j->buf[i].data = kvpmalloc(j->buf[i].buf_size, GFP_KERNEL);
1283 if (!j->buf[i].data)
1284 return -BCH_ERR_ENOMEM_journal_buf;
1287 j->pin.front = j->pin.back = 1;
1293 void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1295 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1296 union journal_res_state s;
1297 unsigned long now = jiffies;
1298 u64 nr_writes = j->nr_flush_writes + j->nr_noflush_writes;
1300 if (!out->nr_tabstops)
1301 printbuf_tabstop_push(out, 24);
1305 s = READ_ONCE(j->reservations);
1307 prt_printf(out, "dirty journal entries:\t%llu/%llu\n", fifo_used(&j->pin), j->pin.size);
1308 prt_printf(out, "seq:\t\t\t%llu\n", journal_cur_seq(j));
1309 prt_printf(out, "seq_ondisk:\t\t%llu\n", j->seq_ondisk);
1310 prt_printf(out, "last_seq:\t\t%llu\n", journal_last_seq(j));
1311 prt_printf(out, "last_seq_ondisk:\t%llu\n", j->last_seq_ondisk);
1312 prt_printf(out, "flushed_seq_ondisk:\t%llu\n", j->flushed_seq_ondisk);
1313 prt_printf(out, "watermark:\t\t%s\n", bch2_watermarks[j->watermark]);
1314 prt_printf(out, "each entry reserved:\t%u\n", j->entry_u64s_reserved);
1315 prt_printf(out, "nr flush writes:\t%llu\n", j->nr_flush_writes);
1316 prt_printf(out, "nr noflush writes:\t%llu\n", j->nr_noflush_writes);
1317 prt_printf(out, "average write size:\t");
1318 prt_human_readable_u64(out, nr_writes ? div64_u64(j->entry_bytes_written, nr_writes) : 0);
1320 prt_printf(out, "nr direct reclaim:\t%llu\n", j->nr_direct_reclaim);
1321 prt_printf(out, "nr background reclaim:\t%llu\n", j->nr_background_reclaim);
1322 prt_printf(out, "reclaim kicked:\t\t%u\n", j->reclaim_kicked);
1323 prt_printf(out, "reclaim runs in:\t%u ms\n", time_after(j->next_reclaim, now)
1324 ? jiffies_to_msecs(j->next_reclaim - jiffies) : 0);
1325 prt_printf(out, "current entry sectors:\t%u\n", j->cur_entry_sectors);
1326 prt_printf(out, "current entry error:\t%s\n", bch2_journal_errors[j->cur_entry_error]);
1327 prt_printf(out, "current entry:\t\t");
1329 switch (s.cur_entry_offset) {
1330 case JOURNAL_ENTRY_ERROR_VAL:
1331 prt_printf(out, "error");
1333 case JOURNAL_ENTRY_CLOSED_VAL:
1334 prt_printf(out, "closed");
1337 prt_printf(out, "%u/%u", s.cur_entry_offset, j->cur_entry_u64s);
1343 for (u64 seq = journal_cur_seq(j);
1344 seq >= journal_last_unwritten_seq(j);
1346 unsigned i = seq & JOURNAL_BUF_MASK;
1348 prt_printf(out, "unwritten entry:");
1350 prt_printf(out, "%llu", seq);
1352 printbuf_indent_add(out, 2);
1354 prt_printf(out, "refcount:");
1356 prt_printf(out, "%u", journal_state_count(s, i));
1359 prt_printf(out, "sectors:");
1361 prt_printf(out, "%u", j->buf[i].sectors);
1364 prt_printf(out, "expires");
1366 prt_printf(out, "%li jiffies", j->buf[i].expires - jiffies);
1369 printbuf_indent_sub(out, 2);
1373 "replay done:\t\t%i\n",
1374 test_bit(JOURNAL_REPLAY_DONE, &j->flags));
1376 prt_printf(out, "space:\n");
1377 prt_printf(out, "\tdiscarded\t%u:%u\n",
1378 j->space[journal_space_discarded].next_entry,
1379 j->space[journal_space_discarded].total);
1380 prt_printf(out, "\tclean ondisk\t%u:%u\n",
1381 j->space[journal_space_clean_ondisk].next_entry,
1382 j->space[journal_space_clean_ondisk].total);
1383 prt_printf(out, "\tclean\t\t%u:%u\n",
1384 j->space[journal_space_clean].next_entry,
1385 j->space[journal_space_clean].total);
1386 prt_printf(out, "\ttotal\t\t%u:%u\n",
1387 j->space[journal_space_total].next_entry,
1388 j->space[journal_space_total].total);
1390 for_each_member_device_rcu(c, ca, &c->rw_devs[BCH_DATA_journal]) {
1391 struct journal_device *ja = &ca->journal;
1393 if (!test_bit(ca->dev_idx, c->rw_devs[BCH_DATA_journal].d))
1399 prt_printf(out, "dev %u:\n", ca->dev_idx);
1400 prt_printf(out, "\tnr\t\t%u\n", ja->nr);
1401 prt_printf(out, "\tbucket size\t%u\n", ca->mi.bucket_size);
1402 prt_printf(out, "\tavailable\t%u:%u\n", bch2_journal_dev_buckets_available(j, ja, journal_space_discarded), ja->sectors_free);
1403 prt_printf(out, "\tdiscard_idx\t%u\n", ja->discard_idx);
1404 prt_printf(out, "\tdirty_ondisk\t%u (seq %llu)\n", ja->dirty_idx_ondisk, ja->bucket_seq[ja->dirty_idx_ondisk]);
1405 prt_printf(out, "\tdirty_idx\t%u (seq %llu)\n", ja->dirty_idx, ja->bucket_seq[ja->dirty_idx]);
1406 prt_printf(out, "\tcur_idx\t\t%u (seq %llu)\n", ja->cur_idx, ja->bucket_seq[ja->cur_idx]);
1414 void bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1416 spin_lock(&j->lock);
1417 __bch2_journal_debug_to_text(out, j);
1418 spin_unlock(&j->lock);
1421 bool bch2_journal_seq_pins_to_text(struct printbuf *out, struct journal *j, u64 *seq)
1423 struct journal_entry_pin_list *pin_list;
1424 struct journal_entry_pin *pin;
1427 spin_lock(&j->lock);
1428 *seq = max(*seq, j->pin.front);
1430 if (*seq >= j->pin.back) {
1431 spin_unlock(&j->lock);
1437 pin_list = journal_seq_pin(j, *seq);
1439 prt_printf(out, "%llu: count %u", *seq, atomic_read(&pin_list->count));
1441 printbuf_indent_add(out, 2);
1443 for (i = 0; i < ARRAY_SIZE(pin_list->list); i++)
1444 list_for_each_entry(pin, &pin_list->list[i], list) {
1445 prt_printf(out, "\t%px %ps", pin, pin->flush);
1449 if (!list_empty(&pin_list->flushed)) {
1450 prt_printf(out, "flushed:");
1454 list_for_each_entry(pin, &pin_list->flushed, list) {
1455 prt_printf(out, "\t%px %ps", pin, pin->flush);
1459 printbuf_indent_sub(out, 2);
1462 spin_unlock(&j->lock);
1467 void bch2_journal_pins_to_text(struct printbuf *out, struct journal *j)
1471 while (!bch2_journal_seq_pins_to_text(out, j, &seq))