xref: /linux/fs/bcachefs/btree_write_buffer.h (revision 3e7819886281e077e82006fe4804b0d6b0f5643b)
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 bkey_buf;
27 int bch2_btree_write_buffer_maybe_flush(struct btree_trans *, struct bkey_s_c, struct bkey_buf *);
28 
29 struct journal_keys_to_wb {
30 	struct btree_write_buffer_keys	*wb;
31 	size_t				room;
32 	u64				seq;
33 };
34 
35 int bch2_journal_key_to_wb_slowpath(struct bch_fs *,
36 			     struct journal_keys_to_wb *,
37 			     enum btree_id, struct bkey_i *);
38 
39 static inline int bch2_journal_key_to_wb(struct bch_fs *c,
40 			     struct journal_keys_to_wb *dst,
41 			     enum btree_id btree, struct bkey_i *k)
42 {
43 	EBUG_ON(!dst->seq);
44 
45 	if (unlikely(!dst->room))
46 		return bch2_journal_key_to_wb_slowpath(c, dst, btree, k);
47 
48 	struct btree_write_buffered_key *wb_k = &darray_top(dst->wb->keys);
49 	wb_k->journal_seq	= dst->seq;
50 	wb_k->btree		= btree;
51 	bkey_copy(&wb_k->k, k);
52 	dst->wb->keys.nr++;
53 	dst->room--;
54 	return 0;
55 }
56 
57 void bch2_journal_keys_to_write_buffer_start(struct bch_fs *, struct journal_keys_to_wb *, u64);
58 void bch2_journal_keys_to_write_buffer_end(struct bch_fs *, struct journal_keys_to_wb *);
59 
60 int bch2_btree_write_buffer_resize(struct bch_fs *, size_t);
61 void bch2_fs_btree_write_buffer_exit(struct bch_fs *);
62 int bch2_fs_btree_write_buffer_init(struct bch_fs *);
63 
64 #endif /* _BCACHEFS_BTREE_WRITE_BUFFER_H */
65