1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_key_cache.h"
5 #include "btree_update.h"
6 #include "btree_write_buffer.h"
11 #include "journal_io.h"
12 #include "journal_reclaim.h"
14 #include "sb-members.h"
17 #include <linux/kthread.h>
18 #include <linux/sched/mm.h>
20 /* Free space calculations: */
22 static unsigned journal_space_from(struct journal_device *ja,
23 enum journal_space_from from)
26 case journal_space_discarded:
27 return ja->discard_idx;
28 case journal_space_clean_ondisk:
29 return ja->dirty_idx_ondisk;
30 case journal_space_clean:
37 unsigned bch2_journal_dev_buckets_available(struct journal *j,
38 struct journal_device *ja,
39 enum journal_space_from from)
41 unsigned available = (journal_space_from(ja, from) -
42 ja->cur_idx - 1 + ja->nr) % ja->nr;
45 * Don't use the last bucket unless writing the new last_seq
46 * will make another bucket available:
48 if (available && ja->dirty_idx_ondisk == ja->dirty_idx)
54 void bch2_journal_set_watermark(struct journal *j)
56 struct bch_fs *c = container_of(j, struct bch_fs, journal);
57 bool low_on_space = j->space[journal_space_clean].total * 4 <=
58 j->space[journal_space_total].total;
59 bool low_on_pin = fifo_free(&j->pin) < j->pin.size / 4;
60 bool low_on_wb = bch2_btree_write_buffer_must_wait(c);
61 unsigned watermark = low_on_space || low_on_pin || low_on_wb
62 ? BCH_WATERMARK_reclaim
63 : BCH_WATERMARK_stripe;
65 if (track_event_change(&c->times[BCH_TIME_blocked_journal_low_on_space], low_on_space) ||
66 track_event_change(&c->times[BCH_TIME_blocked_journal_low_on_pin], low_on_pin) ||
67 track_event_change(&c->times[BCH_TIME_blocked_write_buffer_full], low_on_wb))
68 trace_and_count(c, journal_full, c);
70 mod_bit(JOURNAL_SPACE_LOW, &j->flags, low_on_space || low_on_pin);
72 swap(watermark, j->watermark);
73 if (watermark > j->watermark)
77 static struct journal_space
78 journal_dev_space_available(struct journal *j, struct bch_dev *ca,
79 enum journal_space_from from)
81 struct journal_device *ja = &ca->journal;
82 unsigned sectors, buckets, unwritten;
85 if (from == journal_space_total)
86 return (struct journal_space) {
87 .next_entry = ca->mi.bucket_size,
88 .total = ca->mi.bucket_size * ja->nr,
91 buckets = bch2_journal_dev_buckets_available(j, ja, from);
92 sectors = ja->sectors_free;
95 * We that we don't allocate the space for a journal entry
96 * until we write it out - thus, account for it here:
98 for (seq = journal_last_unwritten_seq(j);
99 seq <= journal_cur_seq(j);
101 unwritten = j->buf[seq & JOURNAL_BUF_MASK].sectors;
106 /* entry won't fit on this device, skip: */
107 if (unwritten > ca->mi.bucket_size)
110 if (unwritten >= sectors) {
117 sectors = ca->mi.bucket_size;
120 sectors -= unwritten;
123 if (sectors < ca->mi.bucket_size && buckets) {
125 sectors = ca->mi.bucket_size;
128 return (struct journal_space) {
129 .next_entry = sectors,
130 .total = sectors + buckets * ca->mi.bucket_size,
134 static struct journal_space __journal_space_available(struct journal *j, unsigned nr_devs_want,
135 enum journal_space_from from)
137 struct bch_fs *c = container_of(j, struct bch_fs, journal);
138 unsigned pos, nr_devs = 0;
139 struct journal_space space, dev_space[BCH_SB_MEMBERS_MAX];
141 BUG_ON(nr_devs_want > ARRAY_SIZE(dev_space));
144 for_each_member_device_rcu(c, ca, &c->rw_devs[BCH_DATA_journal]) {
148 space = journal_dev_space_available(j, ca, from);
149 if (!space.next_entry)
152 for (pos = 0; pos < nr_devs; pos++)
153 if (space.total > dev_space[pos].total)
156 array_insert_item(dev_space, nr_devs, pos, space);
160 if (nr_devs < nr_devs_want)
161 return (struct journal_space) { 0, 0 };
164 * We sorted largest to smallest, and we want the smallest out of the
165 * @nr_devs_want largest devices:
167 return dev_space[nr_devs_want - 1];
170 void bch2_journal_space_available(struct journal *j)
172 struct bch_fs *c = container_of(j, struct bch_fs, journal);
173 unsigned clean, clean_ondisk, total;
174 unsigned max_entry_size = min(j->buf[0].buf_size >> 9,
175 j->buf[1].buf_size >> 9);
176 unsigned nr_online = 0, nr_devs_want;
177 bool can_discard = false;
180 lockdep_assert_held(&j->lock);
183 for_each_member_device_rcu(c, ca, &c->rw_devs[BCH_DATA_journal]) {
184 struct journal_device *ja = &ca->journal;
189 while (ja->dirty_idx != ja->cur_idx &&
190 ja->bucket_seq[ja->dirty_idx] < journal_last_seq(j))
191 ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
193 while (ja->dirty_idx_ondisk != ja->dirty_idx &&
194 ja->bucket_seq[ja->dirty_idx_ondisk] < j->last_seq_ondisk)
195 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
197 if (ja->discard_idx != ja->dirty_idx_ondisk)
200 max_entry_size = min_t(unsigned, max_entry_size, ca->mi.bucket_size);
205 j->can_discard = can_discard;
207 if (nr_online < metadata_replicas_required(c)) {
208 ret = JOURNAL_ERR_insufficient_devices;
212 nr_devs_want = min_t(unsigned, nr_online, c->opts.metadata_replicas);
214 for (unsigned i = 0; i < journal_space_nr; i++)
215 j->space[i] = __journal_space_available(j, nr_devs_want, i);
217 clean_ondisk = j->space[journal_space_clean_ondisk].total;
218 clean = j->space[journal_space_clean].total;
219 total = j->space[journal_space_total].total;
221 if (!j->space[journal_space_discarded].next_entry)
222 ret = JOURNAL_ERR_journal_full;
224 if ((j->space[journal_space_clean_ondisk].next_entry <
225 j->space[journal_space_clean_ondisk].total) &&
226 (clean - clean_ondisk <= total / 8) &&
227 (clean_ondisk * 2 > clean))
228 set_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags);
230 clear_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags);
232 bch2_journal_set_watermark(j);
234 j->cur_entry_sectors = !ret ? j->space[journal_space_discarded].next_entry : 0;
235 j->cur_entry_error = ret;
241 /* Discards - last part of journal reclaim: */
243 static bool should_discard_bucket(struct journal *j, struct journal_device *ja)
248 ret = ja->discard_idx != ja->dirty_idx_ondisk;
249 spin_unlock(&j->lock);
255 * Advance ja->discard_idx as long as it points to buckets that are no longer
256 * dirty, issuing discards if necessary:
258 void bch2_journal_do_discards(struct journal *j)
260 struct bch_fs *c = container_of(j, struct bch_fs, journal);
262 mutex_lock(&j->discard_lock);
264 for_each_rw_member(c, ca) {
265 struct journal_device *ja = &ca->journal;
267 while (should_discard_bucket(j, ja)) {
268 if (!c->opts.nochanges &&
270 bdev_max_discard_sectors(ca->disk_sb.bdev))
271 blkdev_issue_discard(ca->disk_sb.bdev,
273 ja->buckets[ja->discard_idx]),
274 ca->mi.bucket_size, GFP_NOFS);
277 ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
279 bch2_journal_space_available(j);
280 spin_unlock(&j->lock);
284 mutex_unlock(&j->discard_lock);
288 * Journal entry pinning - machinery for holding a reference on a given journal
289 * entry, holding it open to ensure it gets replayed during recovery:
292 void bch2_journal_reclaim_fast(struct journal *j)
296 lockdep_assert_held(&j->lock);
299 * Unpin journal entries whose reference counts reached zero, meaning
300 * all btree nodes got written out
302 while (!fifo_empty(&j->pin) &&
303 j->pin.front <= j->seq_ondisk &&
304 !atomic_read(&fifo_peek_front(&j->pin).count)) {
310 bch2_journal_space_available(j);
313 bool __bch2_journal_pin_put(struct journal *j, u64 seq)
315 struct journal_entry_pin_list *pin_list = journal_seq_pin(j, seq);
317 return atomic_dec_and_test(&pin_list->count);
320 void bch2_journal_pin_put(struct journal *j, u64 seq)
322 if (__bch2_journal_pin_put(j, seq)) {
324 bch2_journal_reclaim_fast(j);
325 spin_unlock(&j->lock);
329 static inline bool __journal_pin_drop(struct journal *j,
330 struct journal_entry_pin *pin)
332 struct journal_entry_pin_list *pin_list;
334 if (!journal_pin_active(pin))
337 if (j->flush_in_progress == pin)
338 j->flush_in_progress_dropped = true;
340 pin_list = journal_seq_pin(j, pin->seq);
342 list_del_init(&pin->list);
345 * Unpinning a journal entry may make journal_next_bucket() succeed, if
346 * writing a new last_seq will now make another bucket available:
348 return atomic_dec_and_test(&pin_list->count) &&
349 pin_list == &fifo_peek_front(&j->pin);
352 void bch2_journal_pin_drop(struct journal *j,
353 struct journal_entry_pin *pin)
356 if (__journal_pin_drop(j, pin))
357 bch2_journal_reclaim_fast(j);
358 spin_unlock(&j->lock);
361 static enum journal_pin_type journal_pin_type(journal_pin_flush_fn fn)
363 if (fn == bch2_btree_node_flush0 ||
364 fn == bch2_btree_node_flush1)
365 return JOURNAL_PIN_btree;
366 else if (fn == bch2_btree_key_cache_journal_flush)
367 return JOURNAL_PIN_key_cache;
369 return JOURNAL_PIN_other;
372 static inline void bch2_journal_pin_set_locked(struct journal *j, u64 seq,
373 struct journal_entry_pin *pin,
374 journal_pin_flush_fn flush_fn,
375 enum journal_pin_type type)
377 struct journal_entry_pin_list *pin_list = journal_seq_pin(j, seq);
380 * flush_fn is how we identify journal pins in debugfs, so must always
381 * exist, even if it doesn't do anything:
385 atomic_inc(&pin_list->count);
387 pin->flush = flush_fn;
388 list_add(&pin->list, &pin_list->list[type]);
391 void bch2_journal_pin_copy(struct journal *j,
392 struct journal_entry_pin *dst,
393 struct journal_entry_pin *src,
394 journal_pin_flush_fn flush_fn)
398 u64 seq = READ_ONCE(src->seq);
400 if (seq < journal_last_seq(j)) {
402 * bch2_journal_pin_copy() raced with bch2_journal_pin_drop() on
403 * the src pin - with the pin dropped, the entry to pin might no
404 * longer to exist, but that means there's no longer anything to
405 * copy and we can bail out here:
407 spin_unlock(&j->lock);
411 bool reclaim = __journal_pin_drop(j, dst);
413 bch2_journal_pin_set_locked(j, seq, dst, flush_fn, journal_pin_type(flush_fn));
416 bch2_journal_reclaim_fast(j);
419 * If the journal is currently full, we might want to call flush_fn
422 if (seq == journal_last_seq(j))
424 spin_unlock(&j->lock);
427 void bch2_journal_pin_set(struct journal *j, u64 seq,
428 struct journal_entry_pin *pin,
429 journal_pin_flush_fn flush_fn)
433 BUG_ON(seq < journal_last_seq(j));
435 bool reclaim = __journal_pin_drop(j, pin);
437 bch2_journal_pin_set_locked(j, seq, pin, flush_fn, journal_pin_type(flush_fn));
440 bch2_journal_reclaim_fast(j);
442 * If the journal is currently full, we might want to call flush_fn
445 if (seq == journal_last_seq(j))
448 spin_unlock(&j->lock);
452 * bch2_journal_pin_flush: ensure journal pin callback is no longer running
456 void bch2_journal_pin_flush(struct journal *j, struct journal_entry_pin *pin)
458 BUG_ON(journal_pin_active(pin));
460 wait_event(j->pin_flush_wait, j->flush_in_progress != pin);
464 * Journal reclaim: flush references to open journal entries to reclaim space in
467 * May be done by the journal code in the background as needed to free up space
468 * for more journal entries, or as part of doing a clean shutdown, or to migrate
469 * data off of a specific device:
472 static struct journal_entry_pin *
473 journal_get_next_pin(struct journal *j,
475 unsigned allowed_below_seq,
476 unsigned allowed_above_seq,
479 struct journal_entry_pin_list *pin_list;
480 struct journal_entry_pin *ret = NULL;
483 fifo_for_each_entry_ptr(pin_list, &j->pin, *seq) {
484 if (*seq > seq_to_flush && !allowed_above_seq)
487 for (i = 0; i < JOURNAL_PIN_NR; i++)
488 if ((((1U << i) & allowed_below_seq) && *seq <= seq_to_flush) ||
489 ((1U << i) & allowed_above_seq)) {
490 ret = list_first_entry_or_null(&pin_list->list[i],
491 struct journal_entry_pin, list);
500 /* returns true if we did work */
501 static size_t journal_flush_pins(struct journal *j,
503 unsigned allowed_below_seq,
504 unsigned allowed_above_seq,
506 unsigned min_key_cache)
508 struct journal_entry_pin *pin;
509 size_t nr_flushed = 0;
510 journal_pin_flush_fn flush_fn;
514 lockdep_assert_held(&j->reclaim_lock);
517 unsigned allowed_above = allowed_above_seq;
518 unsigned allowed_below = allowed_below_seq;
526 allowed_above |= 1U << JOURNAL_PIN_key_cache;
527 allowed_below |= 1U << JOURNAL_PIN_key_cache;
532 j->last_flushed = jiffies;
535 pin = journal_get_next_pin(j, seq_to_flush, allowed_below, allowed_above, &seq);
537 BUG_ON(j->flush_in_progress);
538 j->flush_in_progress = pin;
539 j->flush_in_progress_dropped = false;
540 flush_fn = pin->flush;
542 spin_unlock(&j->lock);
547 if (min_key_cache && pin->flush == bch2_btree_key_cache_journal_flush)
553 err = flush_fn(j, pin, seq);
556 /* Pin might have been dropped or rearmed: */
557 if (likely(!err && !j->flush_in_progress_dropped))
558 list_move(&pin->list, &journal_seq_pin(j, seq)->flushed);
559 j->flush_in_progress = NULL;
560 j->flush_in_progress_dropped = false;
561 spin_unlock(&j->lock);
563 wake_up(&j->pin_flush_wait);
574 static u64 journal_seq_to_flush(struct journal *j)
576 struct bch_fs *c = container_of(j, struct bch_fs, journal);
577 u64 seq_to_flush = 0;
581 for_each_rw_member(c, ca) {
582 struct journal_device *ja = &ca->journal;
583 unsigned nr_buckets, bucket_to_flush;
588 /* Try to keep the journal at most half full: */
589 nr_buckets = ja->nr / 2;
591 nr_buckets = min(nr_buckets, ja->nr);
593 bucket_to_flush = (ja->cur_idx + nr_buckets) % ja->nr;
594 seq_to_flush = max(seq_to_flush,
595 ja->bucket_seq[bucket_to_flush]);
598 /* Also flush if the pin fifo is more than half full */
599 seq_to_flush = max_t(s64, seq_to_flush,
600 (s64) journal_cur_seq(j) -
602 spin_unlock(&j->lock);
608 * __bch2_journal_reclaim - free up journal buckets
610 * @direct: direct or background reclaim?
611 * @kicked: requested to run since we last ran?
612 * Returns: 0 on success, or -EIO if the journal has been shutdown
614 * Background journal reclaim writes out btree nodes. It should be run
615 * early enough so that we never completely run out of journal buckets.
617 * High watermarks for triggering background reclaim:
618 * - FIFO has fewer than 512 entries left
619 * - fewer than 25% journal buckets free
621 * Background reclaim runs until low watermarks are reached:
622 * - FIFO has more than 1024 entries left
623 * - more than 50% journal buckets free
625 * As long as a reclaim can complete in the time it takes to fill up
626 * 512 journal entries or 25% of all journal buckets, then
627 * journal_next_bucket() should not stall.
629 static int __bch2_journal_reclaim(struct journal *j, bool direct, bool kicked)
631 struct bch_fs *c = container_of(j, struct bch_fs, journal);
632 bool kthread = (current->flags & PF_KTHREAD) != 0;
634 size_t min_nr, min_key_cache, nr_flushed;
639 * We can't invoke memory reclaim while holding the reclaim_lock -
640 * journal reclaim is required to make progress for memory reclaim
641 * (cleaning the caches), so we can't get stuck in memory reclaim while
642 * we're holding the reclaim lock:
644 lockdep_assert_held(&j->reclaim_lock);
645 flags = memalloc_noreclaim_save();
648 if (kthread && kthread_should_stop())
651 if (bch2_journal_error(j)) {
656 bch2_journal_do_discards(j);
658 seq_to_flush = journal_seq_to_flush(j);
662 * If it's been longer than j->reclaim_delay_ms since we last flushed,
663 * make sure to flush at least one journal pin:
665 if (time_after(jiffies, j->last_flushed +
666 msecs_to_jiffies(c->opts.journal_reclaim_delay)))
669 if (j->watermark != BCH_WATERMARK_stripe)
672 if (atomic_read(&c->btree_cache.dirty) * 2 > c->btree_cache.used)
675 min_key_cache = min(bch2_nr_btree_keys_need_flush(c), (size_t) 128);
677 trace_and_count(c, journal_reclaim_start, c,
679 min_nr, min_key_cache,
680 atomic_read(&c->btree_cache.dirty),
682 atomic_long_read(&c->btree_key_cache.nr_dirty),
683 atomic_long_read(&c->btree_key_cache.nr_keys));
685 nr_flushed = journal_flush_pins(j, seq_to_flush,
687 min_nr, min_key_cache);
690 j->nr_direct_reclaim += nr_flushed;
692 j->nr_background_reclaim += nr_flushed;
693 trace_and_count(c, journal_reclaim_finish, c, nr_flushed);
696 wake_up(&j->reclaim_wait);
697 } while ((min_nr || min_key_cache) && nr_flushed && !direct);
699 memalloc_noreclaim_restore(flags);
704 int bch2_journal_reclaim(struct journal *j)
706 return __bch2_journal_reclaim(j, true, true);
709 static int bch2_journal_reclaim_thread(void *arg)
711 struct journal *j = arg;
712 struct bch_fs *c = container_of(j, struct bch_fs, journal);
713 unsigned long delay, now;
719 j->last_flushed = jiffies;
721 while (!ret && !kthread_should_stop()) {
722 bool kicked = j->reclaim_kicked;
724 j->reclaim_kicked = false;
726 mutex_lock(&j->reclaim_lock);
727 ret = __bch2_journal_reclaim(j, false, kicked);
728 mutex_unlock(&j->reclaim_lock);
731 delay = msecs_to_jiffies(c->opts.journal_reclaim_delay);
732 j->next_reclaim = j->last_flushed + delay;
734 if (!time_in_range(j->next_reclaim, now, now + delay))
735 j->next_reclaim = now + delay;
738 set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
739 if (kthread_should_stop())
741 if (j->reclaim_kicked)
745 journal_empty = fifo_empty(&j->pin);
746 spin_unlock(&j->lock);
750 else if (time_after(j->next_reclaim, jiffies))
751 schedule_timeout(j->next_reclaim - jiffies);
755 __set_current_state(TASK_RUNNING);
761 void bch2_journal_reclaim_stop(struct journal *j)
763 struct task_struct *p = j->reclaim_thread;
765 j->reclaim_thread = NULL;
773 int bch2_journal_reclaim_start(struct journal *j)
775 struct bch_fs *c = container_of(j, struct bch_fs, journal);
776 struct task_struct *p;
779 if (j->reclaim_thread)
782 p = kthread_create(bch2_journal_reclaim_thread, j,
783 "bch-reclaim/%s", c->name);
784 ret = PTR_ERR_OR_ZERO(p);
785 bch_err_msg(c, ret, "creating journal reclaim thread");
790 j->reclaim_thread = p;
795 static int journal_flush_done(struct journal *j, u64 seq_to_flush,
800 ret = bch2_journal_error(j);
804 mutex_lock(&j->reclaim_lock);
806 if (journal_flush_pins(j, seq_to_flush,
807 (1U << JOURNAL_PIN_key_cache)|
808 (1U << JOURNAL_PIN_other), 0, 0, 0) ||
809 journal_flush_pins(j, seq_to_flush,
810 (1U << JOURNAL_PIN_btree), 0, 0, 0))
813 if (seq_to_flush > journal_cur_seq(j))
814 bch2_journal_entry_close(j);
818 * If journal replay hasn't completed, the unreplayed journal entries
819 * hold refs on their corresponding sequence numbers
821 ret = !test_bit(JOURNAL_REPLAY_DONE, &j->flags) ||
822 journal_last_seq(j) > seq_to_flush ||
825 spin_unlock(&j->lock);
826 mutex_unlock(&j->reclaim_lock);
831 bool bch2_journal_flush_pins(struct journal *j, u64 seq_to_flush)
833 /* time_stats this */
834 bool did_work = false;
836 if (!test_bit(JOURNAL_STARTED, &j->flags))
839 closure_wait_event(&j->async_wait,
840 journal_flush_done(j, seq_to_flush, &did_work));
845 int bch2_journal_flush_device_pins(struct journal *j, int dev_idx)
847 struct bch_fs *c = container_of(j, struct bch_fs, journal);
848 struct journal_entry_pin_list *p;
853 fifo_for_each_entry_ptr(p, &j->pin, iter)
855 ? bch2_dev_list_has_dev(p->devs, dev_idx)
856 : p->devs.nr < c->opts.metadata_replicas)
858 spin_unlock(&j->lock);
860 bch2_journal_flush_pins(j, seq);
862 ret = bch2_journal_error(j);
866 mutex_lock(&c->replicas_gc_lock);
867 bch2_replicas_gc_start(c, 1 << BCH_DATA_journal);
870 * Now that we've populated replicas_gc, write to the journal to mark
871 * active journal devices. This handles the case where the journal might
872 * be empty. Otherwise we could clear all journal replicas and
873 * temporarily put the fs into an unrecoverable state. Journal recovery
874 * expects to find devices marked for journal data on unclean mount.
876 ret = bch2_journal_meta(&c->journal);
883 struct bch_replicas_padded replicas;
885 seq = max(seq, journal_last_seq(j));
886 if (seq >= j->pin.back)
888 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
889 journal_seq_pin(j, seq)->devs);
892 if (replicas.e.nr_devs) {
893 spin_unlock(&j->lock);
894 ret = bch2_mark_replicas(c, &replicas.e);
898 spin_unlock(&j->lock);
900 ret = bch2_replicas_gc_end(c, ret);
901 mutex_unlock(&c->replicas_gc_lock);