1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_key_cache.h"
5 #include "btree_update.h"
10 #include "journal_io.h"
11 #include "journal_reclaim.h"
13 #include "sb-members.h"
16 #include <linux/kthread.h>
17 #include <linux/sched/mm.h>
19 /* Free space calculations: */
21 static unsigned journal_space_from(struct journal_device *ja,
22 enum journal_space_from from)
25 case journal_space_discarded:
26 return ja->discard_idx;
27 case journal_space_clean_ondisk:
28 return ja->dirty_idx_ondisk;
29 case journal_space_clean:
36 unsigned bch2_journal_dev_buckets_available(struct journal *j,
37 struct journal_device *ja,
38 enum journal_space_from from)
40 unsigned available = (journal_space_from(ja, from) -
41 ja->cur_idx - 1 + ja->nr) % ja->nr;
44 * Don't use the last bucket unless writing the new last_seq
45 * will make another bucket available:
47 if (available && ja->dirty_idx_ondisk == ja->dirty_idx)
53 static void journal_set_remaining(struct journal *j, unsigned u64s_remaining)
55 union journal_preres_state old, new;
56 u64 v = atomic64_read(&j->prereserved.counter);
60 new.remaining = u64s_remaining;
61 } while ((v = atomic64_cmpxchg(&j->prereserved.counter,
62 old.v, new.v)) != old.v);
65 static struct journal_space
66 journal_dev_space_available(struct journal *j, struct bch_dev *ca,
67 enum journal_space_from from)
69 struct journal_device *ja = &ca->journal;
70 unsigned sectors, buckets, unwritten;
73 if (from == journal_space_total)
74 return (struct journal_space) {
75 .next_entry = ca->mi.bucket_size,
76 .total = ca->mi.bucket_size * ja->nr,
79 buckets = bch2_journal_dev_buckets_available(j, ja, from);
80 sectors = ja->sectors_free;
83 * We that we don't allocate the space for a journal entry
84 * until we write it out - thus, account for it here:
86 for (seq = journal_last_unwritten_seq(j);
87 seq <= journal_cur_seq(j);
89 unwritten = j->buf[seq & JOURNAL_BUF_MASK].sectors;
94 /* entry won't fit on this device, skip: */
95 if (unwritten > ca->mi.bucket_size)
98 if (unwritten >= sectors) {
105 sectors = ca->mi.bucket_size;
108 sectors -= unwritten;
111 if (sectors < ca->mi.bucket_size && buckets) {
113 sectors = ca->mi.bucket_size;
116 return (struct journal_space) {
117 .next_entry = sectors,
118 .total = sectors + buckets * ca->mi.bucket_size,
122 static struct journal_space __journal_space_available(struct journal *j, unsigned nr_devs_want,
123 enum journal_space_from from)
125 struct bch_fs *c = container_of(j, struct bch_fs, journal);
127 unsigned i, pos, nr_devs = 0;
128 struct journal_space space, dev_space[BCH_SB_MEMBERS_MAX];
130 BUG_ON(nr_devs_want > ARRAY_SIZE(dev_space));
133 for_each_member_device_rcu(ca, c, i,
134 &c->rw_devs[BCH_DATA_journal]) {
138 space = journal_dev_space_available(j, ca, from);
139 if (!space.next_entry)
142 for (pos = 0; pos < nr_devs; pos++)
143 if (space.total > dev_space[pos].total)
146 array_insert_item(dev_space, nr_devs, pos, space);
150 if (nr_devs < nr_devs_want)
151 return (struct journal_space) { 0, 0 };
154 * We sorted largest to smallest, and we want the smallest out of the
155 * @nr_devs_want largest devices:
157 return dev_space[nr_devs_want - 1];
160 void bch2_journal_space_available(struct journal *j)
162 struct bch_fs *c = container_of(j, struct bch_fs, journal);
164 unsigned clean, clean_ondisk, total;
165 s64 u64s_remaining = 0;
166 unsigned max_entry_size = min(j->buf[0].buf_size >> 9,
167 j->buf[1].buf_size >> 9);
168 unsigned i, nr_online = 0, nr_devs_want;
169 bool can_discard = false;
172 lockdep_assert_held(&j->lock);
175 for_each_member_device_rcu(ca, c, i,
176 &c->rw_devs[BCH_DATA_journal]) {
177 struct journal_device *ja = &ca->journal;
182 while (ja->dirty_idx != ja->cur_idx &&
183 ja->bucket_seq[ja->dirty_idx] < journal_last_seq(j))
184 ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
186 while (ja->dirty_idx_ondisk != ja->dirty_idx &&
187 ja->bucket_seq[ja->dirty_idx_ondisk] < j->last_seq_ondisk)
188 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
190 if (ja->discard_idx != ja->dirty_idx_ondisk)
193 max_entry_size = min_t(unsigned, max_entry_size, ca->mi.bucket_size);
198 j->can_discard = can_discard;
200 if (nr_online < c->opts.metadata_replicas_required) {
201 ret = JOURNAL_ERR_insufficient_devices;
205 nr_devs_want = min_t(unsigned, nr_online, c->opts.metadata_replicas);
207 for (i = 0; i < journal_space_nr; i++)
208 j->space[i] = __journal_space_available(j, nr_devs_want, i);
210 clean_ondisk = j->space[journal_space_clean_ondisk].total;
211 clean = j->space[journal_space_clean].total;
212 total = j->space[journal_space_total].total;
214 if (!j->space[journal_space_discarded].next_entry)
215 ret = JOURNAL_ERR_journal_full;
217 if ((j->space[journal_space_clean_ondisk].next_entry <
218 j->space[journal_space_clean_ondisk].total) &&
219 (clean - clean_ondisk <= total / 8) &&
220 (clean_ondisk * 2 > clean))
221 set_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags);
223 clear_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags);
225 u64s_remaining = (u64) clean << 6;
226 u64s_remaining -= (u64) total << 3;
227 u64s_remaining = max(0LL, u64s_remaining);
229 u64s_remaining = min_t(u64, u64s_remaining, U32_MAX);
231 j->cur_entry_sectors = !ret ? j->space[journal_space_discarded].next_entry : 0;
232 j->cur_entry_error = ret;
233 journal_set_remaining(j, u64s_remaining);
234 journal_set_watermark(j);
240 /* Discards - last part of journal reclaim: */
242 static bool should_discard_bucket(struct journal *j, struct journal_device *ja)
247 ret = ja->discard_idx != ja->dirty_idx_ondisk;
248 spin_unlock(&j->lock);
254 * Advance ja->discard_idx as long as it points to buckets that are no longer
255 * dirty, issuing discards if necessary:
257 void bch2_journal_do_discards(struct journal *j)
259 struct bch_fs *c = container_of(j, struct bch_fs, journal);
263 mutex_lock(&j->discard_lock);
265 for_each_rw_member(ca, c, iter) {
266 struct journal_device *ja = &ca->journal;
268 while (should_discard_bucket(j, ja)) {
269 if (!c->opts.nochanges &&
271 bdev_max_discard_sectors(ca->disk_sb.bdev))
272 blkdev_issue_discard(ca->disk_sb.bdev,
274 ja->buckets[ja->discard_idx]),
275 ca->mi.bucket_size, GFP_NOFS);
278 ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
280 bch2_journal_space_available(j);
281 spin_unlock(&j->lock);
285 mutex_unlock(&j->discard_lock);
289 * Journal entry pinning - machinery for holding a reference on a given journal
290 * entry, holding it open to ensure it gets replayed during recovery:
293 void bch2_journal_reclaim_fast(struct journal *j)
297 lockdep_assert_held(&j->lock);
300 * Unpin journal entries whose reference counts reached zero, meaning
301 * all btree nodes got written out
303 while (!fifo_empty(&j->pin) &&
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 void bch2_journal_pin_set(struct journal *j, u64 seq,
373 struct journal_entry_pin *pin,
374 journal_pin_flush_fn flush_fn)
376 struct journal_entry_pin_list *pin_list;
381 if (seq < journal_last_seq(j)) {
383 * bch2_journal_pin_copy() raced with bch2_journal_pin_drop() on
384 * the src pin - with the pin dropped, the entry to pin might no
385 * longer to exist, but that means there's no longer anything to
386 * copy and we can bail out here:
388 spin_unlock(&j->lock);
392 pin_list = journal_seq_pin(j, seq);
394 reclaim = __journal_pin_drop(j, pin);
396 atomic_inc(&pin_list->count);
398 pin->flush = flush_fn;
401 list_add(&pin->list, &pin_list->list[journal_pin_type(flush_fn)]);
403 list_add(&pin->list, &pin_list->flushed);
406 bch2_journal_reclaim_fast(j);
407 spin_unlock(&j->lock);
410 * If the journal is currently full, we might want to call flush_fn
417 * bch2_journal_pin_flush: ensure journal pin callback is no longer running
421 void bch2_journal_pin_flush(struct journal *j, struct journal_entry_pin *pin)
423 BUG_ON(journal_pin_active(pin));
425 wait_event(j->pin_flush_wait, j->flush_in_progress != pin);
429 * Journal reclaim: flush references to open journal entries to reclaim space in
432 * May be done by the journal code in the background as needed to free up space
433 * for more journal entries, or as part of doing a clean shutdown, or to migrate
434 * data off of a specific device:
437 static struct journal_entry_pin *
438 journal_get_next_pin(struct journal *j,
440 unsigned allowed_below_seq,
441 unsigned allowed_above_seq,
444 struct journal_entry_pin_list *pin_list;
445 struct journal_entry_pin *ret = NULL;
448 fifo_for_each_entry_ptr(pin_list, &j->pin, *seq) {
449 if (*seq > seq_to_flush && !allowed_above_seq)
452 for (i = 0; i < JOURNAL_PIN_NR; i++)
453 if ((((1U << i) & allowed_below_seq) && *seq <= seq_to_flush) ||
454 ((1U << i) & allowed_above_seq)) {
455 ret = list_first_entry_or_null(&pin_list->list[i],
456 struct journal_entry_pin, list);
465 /* returns true if we did work */
466 static size_t journal_flush_pins(struct journal *j,
468 unsigned allowed_below_seq,
469 unsigned allowed_above_seq,
471 unsigned min_key_cache)
473 struct journal_entry_pin *pin;
474 size_t nr_flushed = 0;
475 journal_pin_flush_fn flush_fn;
479 lockdep_assert_held(&j->reclaim_lock);
482 unsigned allowed_above = allowed_above_seq;
483 unsigned allowed_below = allowed_below_seq;
491 allowed_above |= 1U << JOURNAL_PIN_key_cache;
492 allowed_below |= 1U << JOURNAL_PIN_key_cache;
497 j->last_flushed = jiffies;
500 pin = journal_get_next_pin(j, seq_to_flush, allowed_below, allowed_above, &seq);
502 BUG_ON(j->flush_in_progress);
503 j->flush_in_progress = pin;
504 j->flush_in_progress_dropped = false;
505 flush_fn = pin->flush;
507 spin_unlock(&j->lock);
512 if (min_key_cache && pin->flush == bch2_btree_key_cache_journal_flush)
518 err = flush_fn(j, pin, seq);
521 /* Pin might have been dropped or rearmed: */
522 if (likely(!err && !j->flush_in_progress_dropped))
523 list_move(&pin->list, &journal_seq_pin(j, seq)->flushed);
524 j->flush_in_progress = NULL;
525 j->flush_in_progress_dropped = false;
526 spin_unlock(&j->lock);
528 wake_up(&j->pin_flush_wait);
539 static u64 journal_seq_to_flush(struct journal *j)
541 struct bch_fs *c = container_of(j, struct bch_fs, journal);
543 u64 seq_to_flush = 0;
548 for_each_rw_member(ca, c, iter) {
549 struct journal_device *ja = &ca->journal;
550 unsigned nr_buckets, bucket_to_flush;
555 /* Try to keep the journal at most half full: */
556 nr_buckets = ja->nr / 2;
558 /* And include pre-reservations: */
559 nr_buckets += DIV_ROUND_UP(j->prereserved.reserved,
560 (ca->mi.bucket_size << 6) -
561 journal_entry_overhead(j));
563 nr_buckets = min(nr_buckets, ja->nr);
565 bucket_to_flush = (ja->cur_idx + nr_buckets) % ja->nr;
566 seq_to_flush = max(seq_to_flush,
567 ja->bucket_seq[bucket_to_flush]);
570 /* Also flush if the pin fifo is more than half full */
571 seq_to_flush = max_t(s64, seq_to_flush,
572 (s64) journal_cur_seq(j) -
574 spin_unlock(&j->lock);
580 * __bch2_journal_reclaim - free up journal buckets
582 * @direct: direct or background reclaim?
583 * @kicked: requested to run since we last ran?
584 * Returns: 0 on success, or -EIO if the journal has been shutdown
586 * Background journal reclaim writes out btree nodes. It should be run
587 * early enough so that we never completely run out of journal buckets.
589 * High watermarks for triggering background reclaim:
590 * - FIFO has fewer than 512 entries left
591 * - fewer than 25% journal buckets free
593 * Background reclaim runs until low watermarks are reached:
594 * - FIFO has more than 1024 entries left
595 * - more than 50% journal buckets free
597 * As long as a reclaim can complete in the time it takes to fill up
598 * 512 journal entries or 25% of all journal buckets, then
599 * journal_next_bucket() should not stall.
601 static int __bch2_journal_reclaim(struct journal *j, bool direct, bool kicked)
603 struct bch_fs *c = container_of(j, struct bch_fs, journal);
604 bool kthread = (current->flags & PF_KTHREAD) != 0;
606 size_t min_nr, min_key_cache, nr_flushed;
611 * We can't invoke memory reclaim while holding the reclaim_lock -
612 * journal reclaim is required to make progress for memory reclaim
613 * (cleaning the caches), so we can't get stuck in memory reclaim while
614 * we're holding the reclaim lock:
616 lockdep_assert_held(&j->reclaim_lock);
617 flags = memalloc_noreclaim_save();
620 if (kthread && kthread_should_stop())
623 if (bch2_journal_error(j)) {
628 bch2_journal_do_discards(j);
630 seq_to_flush = journal_seq_to_flush(j);
634 * If it's been longer than j->reclaim_delay_ms since we last flushed,
635 * make sure to flush at least one journal pin:
637 if (time_after(jiffies, j->last_flushed +
638 msecs_to_jiffies(c->opts.journal_reclaim_delay)))
641 if (j->prereserved.reserved * 4 > j->prereserved.remaining)
644 if (fifo_free(&j->pin) <= 32)
647 if (atomic_read(&c->btree_cache.dirty) * 2 > c->btree_cache.used)
650 min_key_cache = min(bch2_nr_btree_keys_need_flush(c), (size_t) 128);
652 trace_and_count(c, journal_reclaim_start, c,
654 min_nr, min_key_cache,
655 j->prereserved.reserved,
656 j->prereserved.remaining,
657 atomic_read(&c->btree_cache.dirty),
659 atomic_long_read(&c->btree_key_cache.nr_dirty),
660 atomic_long_read(&c->btree_key_cache.nr_keys));
662 nr_flushed = journal_flush_pins(j, seq_to_flush,
664 min_nr, min_key_cache);
667 j->nr_direct_reclaim += nr_flushed;
669 j->nr_background_reclaim += nr_flushed;
670 trace_and_count(c, journal_reclaim_finish, c, nr_flushed);
673 wake_up(&j->reclaim_wait);
674 } while ((min_nr || min_key_cache) && nr_flushed && !direct);
676 memalloc_noreclaim_restore(flags);
681 int bch2_journal_reclaim(struct journal *j)
683 return __bch2_journal_reclaim(j, true, true);
686 static int bch2_journal_reclaim_thread(void *arg)
688 struct journal *j = arg;
689 struct bch_fs *c = container_of(j, struct bch_fs, journal);
690 unsigned long delay, now;
696 j->last_flushed = jiffies;
698 while (!ret && !kthread_should_stop()) {
699 bool kicked = j->reclaim_kicked;
701 j->reclaim_kicked = false;
703 mutex_lock(&j->reclaim_lock);
704 ret = __bch2_journal_reclaim(j, false, kicked);
705 mutex_unlock(&j->reclaim_lock);
708 delay = msecs_to_jiffies(c->opts.journal_reclaim_delay);
709 j->next_reclaim = j->last_flushed + delay;
711 if (!time_in_range(j->next_reclaim, now, now + delay))
712 j->next_reclaim = now + delay;
715 set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
716 if (kthread_should_stop())
718 if (j->reclaim_kicked)
722 journal_empty = fifo_empty(&j->pin);
723 spin_unlock(&j->lock);
727 else if (time_after(j->next_reclaim, jiffies))
728 schedule_timeout(j->next_reclaim - jiffies);
732 __set_current_state(TASK_RUNNING);
738 void bch2_journal_reclaim_stop(struct journal *j)
740 struct task_struct *p = j->reclaim_thread;
742 j->reclaim_thread = NULL;
750 int bch2_journal_reclaim_start(struct journal *j)
752 struct bch_fs *c = container_of(j, struct bch_fs, journal);
753 struct task_struct *p;
756 if (j->reclaim_thread)
759 p = kthread_create(bch2_journal_reclaim_thread, j,
760 "bch-reclaim/%s", c->name);
761 ret = PTR_ERR_OR_ZERO(p);
763 bch_err_msg(c, ret, "creating journal reclaim thread");
768 j->reclaim_thread = p;
773 static int journal_flush_done(struct journal *j, u64 seq_to_flush,
778 ret = bch2_journal_error(j);
782 mutex_lock(&j->reclaim_lock);
784 if (journal_flush_pins(j, seq_to_flush,
785 (1U << JOURNAL_PIN_key_cache)|
786 (1U << JOURNAL_PIN_other), 0, 0, 0) ||
787 journal_flush_pins(j, seq_to_flush,
788 (1U << JOURNAL_PIN_btree), 0, 0, 0))
793 * If journal replay hasn't completed, the unreplayed journal entries
794 * hold refs on their corresponding sequence numbers
796 ret = !test_bit(JOURNAL_REPLAY_DONE, &j->flags) ||
797 journal_last_seq(j) > seq_to_flush ||
800 spin_unlock(&j->lock);
801 mutex_unlock(&j->reclaim_lock);
806 bool bch2_journal_flush_pins(struct journal *j, u64 seq_to_flush)
808 bool did_work = false;
810 if (!test_bit(JOURNAL_STARTED, &j->flags))
813 closure_wait_event(&j->async_wait,
814 journal_flush_done(j, seq_to_flush, &did_work));
819 int bch2_journal_flush_device_pins(struct journal *j, int dev_idx)
821 struct bch_fs *c = container_of(j, struct bch_fs, journal);
822 struct journal_entry_pin_list *p;
827 fifo_for_each_entry_ptr(p, &j->pin, iter)
829 ? bch2_dev_list_has_dev(p->devs, dev_idx)
830 : p->devs.nr < c->opts.metadata_replicas)
832 spin_unlock(&j->lock);
834 bch2_journal_flush_pins(j, seq);
836 ret = bch2_journal_error(j);
840 mutex_lock(&c->replicas_gc_lock);
841 bch2_replicas_gc_start(c, 1 << BCH_DATA_journal);
844 * Now that we've populated replicas_gc, write to the journal to mark
845 * active journal devices. This handles the case where the journal might
846 * be empty. Otherwise we could clear all journal replicas and
847 * temporarily put the fs into an unrecoverable state. Journal recovery
848 * expects to find devices marked for journal data on unclean mount.
850 ret = bch2_journal_meta(&c->journal);
857 struct bch_replicas_padded replicas;
859 seq = max(seq, journal_last_seq(j));
860 if (seq >= j->pin.back)
862 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
863 journal_seq_pin(j, seq)->devs);
866 spin_unlock(&j->lock);
867 ret = bch2_mark_replicas(c, &replicas.e);
870 spin_unlock(&j->lock);
872 ret = bch2_replicas_gc_end(c, ret);
873 mutex_unlock(&c->replicas_gc_lock);