This adds a new watermark for the journal reclaim when flushing btree
key cache entries - it should try and stay ahead of where foreground
threads doing transaction commits will enter direct journal reclaim.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
 #ifndef _BCACHEFS_BTREE_KEY_CACHE_H
 #define _BCACHEFS_BTREE_KEY_CACHE_H
 
+static inline size_t bch2_nr_btree_keys_want_flush(struct bch_fs *c)
+{
+       size_t nr_dirty = atomic_long_read(&c->btree_key_cache.nr_dirty);
+       size_t nr_keys = atomic_long_read(&c->btree_key_cache.nr_keys);
+       size_t max_dirty = nr_keys / 4;
+
+       return max_t(ssize_t, 0, nr_dirty - max_dirty);
+}
+
 static inline size_t bch2_nr_btree_keys_need_flush(struct bch_fs *c)
 {
        size_t nr_dirty = atomic_long_read(&c->btree_key_cache.nr_dirty);
 
                if (fifo_free(&j->pin) <= 32)
                        min_nr = 1;
 
-               min_nr = max(min_nr, bch2_nr_btree_keys_need_flush(c));
+               min_nr = max(min_nr, bch2_nr_btree_keys_want_flush(c));
 
                trace_journal_reclaim_start(c,
                                min_nr,