1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_JOURNAL_ITER_H
3 #define _BCACHEFS_BTREE_JOURNAL_ITER_H
4
5 #include "bkey.h"
6
7 struct journal_iter {
8 struct list_head list;
9 enum btree_id btree_id;
10 unsigned level;
11 size_t idx;
12 struct journal_keys *keys;
13 };
14
15 /*
16 * Iterate over keys in the btree, with keys from the journal overlaid on top:
17 */
18
19 struct btree_and_journal_iter {
20 struct btree_trans *trans;
21 struct btree *b;
22 struct btree_node_iter node_iter;
23 struct bkey unpacked;
24
25 struct journal_iter journal;
26 struct bpos pos;
27 bool at_end;
28 bool prefetch;
29 };
30
__journal_key_cmp(enum btree_id l_btree_id,unsigned l_level,struct bpos l_pos,const struct journal_key * r)31 static inline int __journal_key_cmp(enum btree_id l_btree_id,
32 unsigned l_level,
33 struct bpos l_pos,
34 const struct journal_key *r)
35 {
36 return (cmp_int(l_btree_id, r->btree_id) ?:
37 cmp_int(l_level, r->level) ?:
38 bpos_cmp(l_pos, r->k->k.p));
39 }
40
journal_key_cmp(const struct journal_key * l,const struct journal_key * r)41 static inline int journal_key_cmp(const struct journal_key *l, const struct journal_key *r)
42 {
43 return __journal_key_cmp(l->btree_id, l->level, l->k->k.p, r);
44 }
45
46 struct bkey_i *bch2_journal_keys_peek_upto(struct bch_fs *, enum btree_id,
47 unsigned, struct bpos, struct bpos, size_t *);
48 struct bkey_i *bch2_journal_keys_peek_slot(struct bch_fs *, enum btree_id,
49 unsigned, struct bpos);
50
51 int bch2_btree_and_journal_iter_prefetch(struct btree_trans *, struct btree_path *,
52 struct btree_and_journal_iter *);
53
54 int bch2_journal_key_insert_take(struct bch_fs *, enum btree_id,
55 unsigned, struct bkey_i *);
56 int bch2_journal_key_insert(struct bch_fs *, enum btree_id,
57 unsigned, struct bkey_i *);
58 int bch2_journal_key_delete(struct bch_fs *, enum btree_id,
59 unsigned, struct bpos);
60 bool bch2_key_deleted_in_journal(struct btree_trans *, enum btree_id, unsigned, struct bpos);
61 void bch2_journal_key_overwritten(struct bch_fs *, enum btree_id, unsigned, struct bpos);
62
63 void bch2_btree_and_journal_iter_advance(struct btree_and_journal_iter *);
64 struct bkey_s_c bch2_btree_and_journal_iter_peek(struct btree_and_journal_iter *);
65
66 void bch2_btree_and_journal_iter_exit(struct btree_and_journal_iter *);
67 void __bch2_btree_and_journal_iter_init_node_iter(struct btree_trans *,
68 struct btree_and_journal_iter *, struct btree *,
69 struct btree_node_iter, struct bpos);
70 void bch2_btree_and_journal_iter_init_node_iter(struct btree_trans *,
71 struct btree_and_journal_iter *, struct btree *);
72
73 void bch2_journal_keys_put(struct bch_fs *);
74
bch2_journal_keys_put_initial(struct bch_fs * c)75 static inline void bch2_journal_keys_put_initial(struct bch_fs *c)
76 {
77 if (c->journal_keys.initial_ref_held)
78 bch2_journal_keys_put(c);
79 c->journal_keys.initial_ref_held = false;
80 }
81
82 void bch2_journal_entries_free(struct bch_fs *);
83
84 int bch2_journal_keys_sort(struct bch_fs *);
85
86 void bch2_shoot_down_journal_keys(struct bch_fs *, enum btree_id,
87 unsigned, unsigned,
88 struct bpos, struct bpos);
89
90 void bch2_journal_keys_dump(struct bch_fs *);
91
92 #endif /* _BCACHEFS_BTREE_JOURNAL_ITER_H */
93