xref: /linux/fs/bcachefs/btree_write_buffer.h (revision 249ebf3f65f8530beb2cbfb91bff1d83ba88d23c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_WRITE_BUFFER_H
3 #define _BCACHEFS_BTREE_WRITE_BUFFER_H
4 
5 #include "bkey.h"
6 
7 static inline bool bch2_btree_write_buffer_should_flush(struct bch_fs *c)
8 {
9 	struct btree_write_buffer *wb = &c->btree_write_buffer;
10 
11 	return wb->inc.keys.nr + wb->flushing.keys.nr > wb->inc.keys.size / 4;
12 }
13 
14 static inline bool bch2_btree_write_buffer_must_wait(struct bch_fs *c)
15 {
16 	struct btree_write_buffer *wb = &c->btree_write_buffer;
17 
18 	return wb->inc.keys.nr > wb->inc.keys.size * 3 / 4;
19 }
20 
21 struct btree_trans;
22 int bch2_btree_write_buffer_flush_sync(struct btree_trans *);
23 int bch2_btree_write_buffer_flush_nocheck_rw(struct btree_trans *);
24 int bch2_btree_write_buffer_tryflush(struct btree_trans *);
25 
26 struct journal_keys_to_wb {
27 	struct btree_write_buffer_keys	*wb;
28 	size_t				room;
29 	u64				seq;
30 };
31 
32 int bch2_journal_key_to_wb_slowpath(struct bch_fs *,
33 			     struct journal_keys_to_wb *,
34 			     enum btree_id, struct bkey_i *);
35 
36 static inline int bch2_journal_key_to_wb(struct bch_fs *c,
37 			     struct journal_keys_to_wb *dst,
38 			     enum btree_id btree, struct bkey_i *k)
39 {
40 	EBUG_ON(!dst->seq);
41 
42 	if (unlikely(!dst->room))
43 		return bch2_journal_key_to_wb_slowpath(c, dst, btree, k);
44 
45 	struct btree_write_buffered_key *wb_k = &darray_top(dst->wb->keys);
46 	wb_k->journal_seq	= dst->seq;
47 	wb_k->btree		= btree;
48 	bkey_copy(&wb_k->k, k);
49 	dst->wb->keys.nr++;
50 	dst->room--;
51 	return 0;
52 }
53 
54 void bch2_journal_keys_to_write_buffer_start(struct bch_fs *, struct journal_keys_to_wb *, u64);
55 void bch2_journal_keys_to_write_buffer_end(struct bch_fs *, struct journal_keys_to_wb *);
56 
57 int bch2_btree_write_buffer_resize(struct bch_fs *, size_t);
58 void bch2_fs_btree_write_buffer_exit(struct bch_fs *);
59 int bch2_fs_btree_write_buffer_init(struct bch_fs *);
60 
61 #endif /* _BCACHEFS_BTREE_WRITE_BUFFER_H */
62