1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_WRITE_BUFFER_H
3 #define _BCACHEFS_BTREE_WRITE_BUFFER_H
6 #include "disk_accounting.h"
8 static inline bool bch2_btree_write_buffer_should_flush(struct bch_fs
*c
)
10 struct btree_write_buffer
*wb
= &c
->btree_write_buffer
;
12 return wb
->inc
.keys
.nr
+ wb
->flushing
.keys
.nr
> wb
->inc
.keys
.size
/ 4;
15 static inline bool bch2_btree_write_buffer_must_wait(struct bch_fs
*c
)
17 struct btree_write_buffer
*wb
= &c
->btree_write_buffer
;
19 return wb
->inc
.keys
.nr
> wb
->inc
.keys
.size
* 3 / 4;
23 int bch2_btree_write_buffer_flush_sync(struct btree_trans
*);
24 bool bch2_btree_write_buffer_flush_going_ro(struct bch_fs
*);
25 int bch2_btree_write_buffer_flush_nocheck_rw(struct btree_trans
*);
26 int bch2_btree_write_buffer_tryflush(struct btree_trans
*);
29 int bch2_btree_write_buffer_maybe_flush(struct btree_trans
*, struct bkey_s_c
, struct bkey_buf
*);
31 struct journal_keys_to_wb
{
32 struct btree_write_buffer_keys
*wb
;
37 static inline int wb_key_cmp(const void *_l
, const void *_r
)
39 const struct btree_write_buffered_key
*l
= _l
;
40 const struct btree_write_buffered_key
*r
= _r
;
42 return cmp_int(l
->btree
, r
->btree
) ?: bpos_cmp(l
->k
.k
.p
, r
->k
.k
.p
);
45 int bch2_accounting_key_to_wb_slowpath(struct bch_fs
*,
46 enum btree_id
, struct bkey_i_accounting
*);
48 static inline int bch2_accounting_key_to_wb(struct bch_fs
*c
,
49 enum btree_id btree
, struct bkey_i_accounting
*k
)
51 struct btree_write_buffer
*wb
= &c
->btree_write_buffer
;
52 struct btree_write_buffered_key search
;
54 search
.k
.k
.p
= k
->k
.p
;
56 unsigned idx
= eytzinger0_find(wb
->accounting
.data
, wb
->accounting
.nr
,
57 sizeof(wb
->accounting
.data
[0]),
60 if (idx
>= wb
->accounting
.nr
)
61 return bch2_accounting_key_to_wb_slowpath(c
, btree
, k
);
63 struct bkey_i_accounting
*dst
= bkey_i_to_accounting(&wb
->accounting
.data
[idx
].k
);
64 bch2_accounting_accumulate(dst
, accounting_i_to_s_c(k
));
68 int bch2_journal_key_to_wb_slowpath(struct bch_fs
*,
69 struct journal_keys_to_wb
*,
70 enum btree_id
, struct bkey_i
*);
72 static inline int __bch2_journal_key_to_wb(struct bch_fs
*c
,
73 struct journal_keys_to_wb
*dst
,
74 enum btree_id btree
, struct bkey_i
*k
)
76 if (unlikely(!dst
->room
))
77 return bch2_journal_key_to_wb_slowpath(c
, dst
, btree
, k
);
79 struct btree_write_buffered_key
*wb_k
= &darray_top(dst
->wb
->keys
);
80 wb_k
->journal_seq
= dst
->seq
;
82 bkey_copy(&wb_k
->k
, k
);
88 static inline int bch2_journal_key_to_wb(struct bch_fs
*c
,
89 struct journal_keys_to_wb
*dst
,
90 enum btree_id btree
, struct bkey_i
*k
)
94 return k
->k
.type
== KEY_TYPE_accounting
95 ? bch2_accounting_key_to_wb(c
, btree
, bkey_i_to_accounting(k
))
96 : __bch2_journal_key_to_wb(c
, dst
, btree
, k
);
99 void bch2_journal_keys_to_write_buffer_start(struct bch_fs
*, struct journal_keys_to_wb
*, u64
);
100 int bch2_journal_keys_to_write_buffer_end(struct bch_fs
*, struct journal_keys_to_wb
*);
102 int bch2_btree_write_buffer_resize(struct bch_fs
*, size_t);
103 void bch2_fs_btree_write_buffer_exit(struct bch_fs
*);
104 int bch2_fs_btree_write_buffer_init(struct bch_fs
*);
106 #endif /* _BCACHEFS_BTREE_WRITE_BUFFER_H */