xref: /linux/fs/bcachefs/btree_key_cache.h (revision 170aafe35cb98e0f3fbacb446ea86389fbce22ea)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_KEY_CACHE_H
3 #define _BCACHEFS_BTREE_KEY_CACHE_H
4 
5 static inline size_t bch2_nr_btree_keys_need_flush(struct bch_fs *c)
6 {
7 	size_t nr_dirty = atomic_long_read(&c->btree_key_cache.nr_dirty);
8 	size_t nr_keys = atomic_long_read(&c->btree_key_cache.nr_keys);
9 	size_t max_dirty = 1024 + nr_keys  / 2;
10 
11 	return max_t(ssize_t, 0, nr_dirty - max_dirty);
12 }
13 
14 static inline ssize_t __bch2_btree_key_cache_must_wait(struct bch_fs *c)
15 {
16 	size_t nr_dirty = atomic_long_read(&c->btree_key_cache.nr_dirty);
17 	size_t nr_keys = atomic_long_read(&c->btree_key_cache.nr_keys);
18 	size_t max_dirty = 4096 + (nr_keys * 3) / 4;
19 
20 	return nr_dirty - max_dirty;
21 }
22 
23 static inline bool bch2_btree_key_cache_must_wait(struct bch_fs *c)
24 {
25 	return __bch2_btree_key_cache_must_wait(c) > 0;
26 }
27 
28 static inline bool bch2_btree_key_cache_wait_done(struct bch_fs *c)
29 {
30 	size_t nr_dirty = atomic_long_read(&c->btree_key_cache.nr_dirty);
31 	size_t nr_keys = atomic_long_read(&c->btree_key_cache.nr_keys);
32 	size_t max_dirty = 2048 + (nr_keys * 5) / 8;
33 
34 	return nr_dirty <= max_dirty;
35 }
36 
37 int bch2_btree_key_cache_journal_flush(struct journal *,
38 				struct journal_entry_pin *, u64);
39 
40 struct bkey_cached *
41 bch2_btree_key_cache_find(struct bch_fs *, enum btree_id, struct bpos);
42 
43 int bch2_btree_path_traverse_cached(struct btree_trans *, struct btree_path *,
44 				    unsigned);
45 
46 bool bch2_btree_insert_key_cached(struct btree_trans *, unsigned,
47 			struct btree_insert_entry *);
48 void bch2_btree_key_cache_drop(struct btree_trans *,
49 			       struct btree_path *);
50 
51 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *);
52 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *);
53 int bch2_fs_btree_key_cache_init(struct btree_key_cache *);
54 
55 void bch2_btree_key_cache_to_text(struct printbuf *, struct btree_key_cache *);
56 
57 void bch2_btree_key_cache_exit(void);
58 int __init bch2_btree_key_cache_init(void);
59 
60 #endif /* _BCACHEFS_BTREE_KEY_CACHE_H */
61