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 #include "disk_accounting.h"
7
bch2_btree_write_buffer_should_flush(struct bch_fs * c)8 static inline bool bch2_btree_write_buffer_should_flush(struct bch_fs *c)
9 {
10 struct btree_write_buffer *wb = &c->btree_write_buffer;
11
12 return wb->inc.keys.nr + wb->flushing.keys.nr > wb->inc.keys.size / 4;
13 }
14
bch2_btree_write_buffer_must_wait(struct bch_fs * c)15 static inline bool bch2_btree_write_buffer_must_wait(struct bch_fs *c)
16 {
17 struct btree_write_buffer *wb = &c->btree_write_buffer;
18
19 return wb->inc.keys.nr > wb->inc.keys.size * 3 / 4;
20 }
21
22 struct btree_trans;
23 int bch2_btree_write_buffer_flush_sync(struct btree_trans *);
24 int bch2_btree_write_buffer_flush_nocheck_rw(struct btree_trans *);
25 int bch2_btree_write_buffer_tryflush(struct btree_trans *);
26
27 struct bkey_buf;
28 int bch2_btree_write_buffer_maybe_flush(struct btree_trans *, struct bkey_s_c, struct bkey_buf *);
29
30 struct journal_keys_to_wb {
31 struct btree_write_buffer_keys *wb;
32 size_t room;
33 u64 seq;
34 };
35
wb_key_cmp(const void * _l,const void * _r)36 static inline int wb_key_cmp(const void *_l, const void *_r)
37 {
38 const struct btree_write_buffered_key *l = _l;
39 const struct btree_write_buffered_key *r = _r;
40
41 return cmp_int(l->btree, r->btree) ?: bpos_cmp(l->k.k.p, r->k.k.p);
42 }
43
44 int bch2_accounting_key_to_wb_slowpath(struct bch_fs *,
45 enum btree_id, struct bkey_i_accounting *);
46
bch2_accounting_key_to_wb(struct bch_fs * c,enum btree_id btree,struct bkey_i_accounting * k)47 static inline int bch2_accounting_key_to_wb(struct bch_fs *c,
48 enum btree_id btree, struct bkey_i_accounting *k)
49 {
50 struct btree_write_buffer *wb = &c->btree_write_buffer;
51 struct btree_write_buffered_key search;
52 search.btree = btree;
53 search.k.k.p = k->k.p;
54
55 unsigned idx = eytzinger0_find(wb->accounting.data, wb->accounting.nr,
56 sizeof(wb->accounting.data[0]),
57 wb_key_cmp, &search);
58
59 if (idx >= wb->accounting.nr)
60 return bch2_accounting_key_to_wb_slowpath(c, btree, k);
61
62 struct bkey_i_accounting *dst = bkey_i_to_accounting(&wb->accounting.data[idx].k);
63 bch2_accounting_accumulate(dst, accounting_i_to_s_c(k));
64 return 0;
65 }
66
67 int bch2_journal_key_to_wb_slowpath(struct bch_fs *,
68 struct journal_keys_to_wb *,
69 enum btree_id, struct bkey_i *);
70
__bch2_journal_key_to_wb(struct bch_fs * c,struct journal_keys_to_wb * dst,enum btree_id btree,struct bkey_i * k)71 static inline int __bch2_journal_key_to_wb(struct bch_fs *c,
72 struct journal_keys_to_wb *dst,
73 enum btree_id btree, struct bkey_i *k)
74 {
75 if (unlikely(!dst->room))
76 return bch2_journal_key_to_wb_slowpath(c, dst, btree, k);
77
78 struct btree_write_buffered_key *wb_k = &darray_top(dst->wb->keys);
79 wb_k->journal_seq = dst->seq;
80 wb_k->btree = btree;
81 bkey_copy(&wb_k->k, k);
82 dst->wb->keys.nr++;
83 dst->room--;
84 return 0;
85 }
86
bch2_journal_key_to_wb(struct bch_fs * c,struct journal_keys_to_wb * dst,enum btree_id btree,struct bkey_i * k)87 static inline int bch2_journal_key_to_wb(struct bch_fs *c,
88 struct journal_keys_to_wb *dst,
89 enum btree_id btree, struct bkey_i *k)
90 {
91 EBUG_ON(!dst->seq);
92
93 return k->k.type == KEY_TYPE_accounting
94 ? bch2_accounting_key_to_wb(c, btree, bkey_i_to_accounting(k))
95 : __bch2_journal_key_to_wb(c, dst, btree, k);
96 }
97
98 void bch2_journal_keys_to_write_buffer_start(struct bch_fs *, struct journal_keys_to_wb *, u64);
99 int bch2_journal_keys_to_write_buffer_end(struct bch_fs *, struct journal_keys_to_wb *);
100
101 int bch2_btree_write_buffer_resize(struct bch_fs *, size_t);
102 void bch2_fs_btree_write_buffer_exit(struct bch_fs *);
103 int bch2_fs_btree_write_buffer_init(struct bch_fs *);
104
105 #endif /* _BCACHEFS_BTREE_WRITE_BUFFER_H */
106