1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | #ifndef _BCACHEFS_BTREE_IO_H |
3 | #define _BCACHEFS_BTREE_IO_H |
4 | |
5 | #include "bkey_methods.h" |
6 | #include "bset.h" |
7 | #include "btree_locking.h" |
8 | #include "checksum.h" |
9 | #include "extents.h" |
10 | #include "io_write_types.h" |
11 | |
12 | struct bch_fs; |
13 | struct btree_write; |
14 | struct btree; |
15 | struct btree_iter; |
16 | struct btree_node_read_all; |
17 | |
18 | static inline void set_btree_node_dirty_acct(struct bch_fs *c, struct btree *b) |
19 | { |
20 | if (!test_and_set_bit(nr: BTREE_NODE_dirty, addr: &b->flags)) |
21 | atomic_inc(v: &c->btree_cache.dirty); |
22 | } |
23 | |
24 | static inline void clear_btree_node_dirty_acct(struct bch_fs *c, struct btree *b) |
25 | { |
26 | if (test_and_clear_bit(nr: BTREE_NODE_dirty, addr: &b->flags)) |
27 | atomic_dec(v: &c->btree_cache.dirty); |
28 | } |
29 | |
30 | static inline unsigned btree_ptr_sectors_written(struct bkey_i *k) |
31 | { |
32 | return k->k.type == KEY_TYPE_btree_ptr_v2 |
33 | ? le16_to_cpu(bkey_i_to_btree_ptr_v2(k)->v.sectors_written) |
34 | : 0; |
35 | } |
36 | |
37 | struct btree_read_bio { |
38 | struct bch_fs *c; |
39 | struct btree *b; |
40 | struct btree_node_read_all *ra; |
41 | u64 start_time; |
42 | unsigned have_ioref:1; |
43 | unsigned idx:7; |
44 | struct extent_ptr_decoded pick; |
45 | struct work_struct work; |
46 | struct bio bio; |
47 | }; |
48 | |
49 | struct btree_write_bio { |
50 | struct work_struct work; |
51 | __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX); |
52 | void *data; |
53 | unsigned data_bytes; |
54 | unsigned sector_offset; |
55 | struct bch_write_bio wbio; |
56 | }; |
57 | |
58 | void bch2_btree_node_io_unlock(struct btree *); |
59 | void bch2_btree_node_io_lock(struct btree *); |
60 | void __bch2_btree_node_wait_on_read(struct btree *); |
61 | void __bch2_btree_node_wait_on_write(struct btree *); |
62 | void bch2_btree_node_wait_on_read(struct btree *); |
63 | void bch2_btree_node_wait_on_write(struct btree *); |
64 | |
65 | enum compact_mode { |
66 | COMPACT_LAZY, |
67 | COMPACT_ALL, |
68 | }; |
69 | |
70 | bool bch2_compact_whiteouts(struct bch_fs *, struct btree *, |
71 | enum compact_mode); |
72 | |
73 | static inline bool should_compact_bset_lazy(struct btree *b, |
74 | struct bset_tree *t) |
75 | { |
76 | unsigned total_u64s = bset_u64s(t); |
77 | unsigned dead_u64s = bset_dead_u64s(b, t); |
78 | |
79 | return dead_u64s > 64 && dead_u64s * 3 > total_u64s; |
80 | } |
81 | |
82 | static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b) |
83 | { |
84 | struct bset_tree *t; |
85 | |
86 | for_each_bset(b, t) |
87 | if (should_compact_bset_lazy(b, t)) |
88 | return bch2_compact_whiteouts(c, b, COMPACT_LAZY); |
89 | |
90 | return false; |
91 | } |
92 | |
93 | static inline struct nonce btree_nonce(struct bset *i, unsigned offset) |
94 | { |
95 | return (struct nonce) {{ |
96 | [0] = cpu_to_le32(offset), |
97 | [1] = ((__le32 *) &i->seq)[0], |
98 | [2] = ((__le32 *) &i->seq)[1], |
99 | [3] = ((__le32 *) &i->journal_seq)[0]^BCH_NONCE_BTREE, |
100 | }}; |
101 | } |
102 | |
103 | static inline int bset_encrypt(struct bch_fs *c, struct bset *i, unsigned offset) |
104 | { |
105 | struct nonce nonce = btree_nonce(i, offset); |
106 | int ret; |
107 | |
108 | if (!offset) { |
109 | struct btree_node *bn = container_of(i, struct btree_node, keys); |
110 | unsigned bytes = (void *) &bn->keys - (void *) &bn->flags; |
111 | |
112 | ret = bch2_encrypt(c, BSET_CSUM_TYPE(k: i), nonce, |
113 | data: &bn->flags, bytes); |
114 | if (ret) |
115 | return ret; |
116 | |
117 | nonce = nonce_add(nonce, round_up(bytes, CHACHA_BLOCK_SIZE)); |
118 | } |
119 | |
120 | return bch2_encrypt(c, BSET_CSUM_TYPE(k: i), nonce, data: i->_data, |
121 | vstruct_end(i) - (void *) i->_data); |
122 | } |
123 | |
124 | void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *); |
125 | |
126 | void bch2_btree_node_drop_keys_outside_node(struct btree *); |
127 | |
128 | void bch2_btree_build_aux_trees(struct btree *); |
129 | void bch2_btree_init_next(struct btree_trans *, struct btree *); |
130 | |
131 | int bch2_btree_node_read_done(struct bch_fs *, struct bch_dev *, |
132 | struct btree *, bool, bool *); |
133 | void bch2_btree_node_read(struct btree_trans *, struct btree *, bool); |
134 | int bch2_btree_root_read(struct bch_fs *, enum btree_id, |
135 | const struct bkey_i *, unsigned); |
136 | |
137 | bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *); |
138 | |
139 | enum btree_write_flags { |
140 | __BTREE_WRITE_ONLY_IF_NEED = BTREE_WRITE_TYPE_BITS, |
141 | __BTREE_WRITE_ALREADY_STARTED, |
142 | }; |
143 | #define BTREE_WRITE_ONLY_IF_NEED BIT(__BTREE_WRITE_ONLY_IF_NEED) |
144 | #define BTREE_WRITE_ALREADY_STARTED BIT(__BTREE_WRITE_ALREADY_STARTED) |
145 | |
146 | void __bch2_btree_node_write(struct bch_fs *, struct btree *, unsigned); |
147 | void bch2_btree_node_write(struct bch_fs *, struct btree *, |
148 | enum six_lock_type, unsigned); |
149 | |
150 | static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b, |
151 | enum six_lock_type lock_held) |
152 | { |
153 | bch2_btree_node_write(c, b, lock_held, BTREE_WRITE_ONLY_IF_NEED); |
154 | } |
155 | |
156 | bool bch2_btree_flush_all_reads(struct bch_fs *); |
157 | bool bch2_btree_flush_all_writes(struct bch_fs *); |
158 | |
159 | static inline void compat_bformat(unsigned level, enum btree_id btree_id, |
160 | unsigned version, unsigned big_endian, |
161 | int write, struct bkey_format *f) |
162 | { |
163 | if (version < bcachefs_metadata_version_inode_btree_change && |
164 | btree_id == BTREE_ID_inodes) { |
165 | swap(f->bits_per_field[BKEY_FIELD_INODE], |
166 | f->bits_per_field[BKEY_FIELD_OFFSET]); |
167 | swap(f->field_offset[BKEY_FIELD_INODE], |
168 | f->field_offset[BKEY_FIELD_OFFSET]); |
169 | } |
170 | |
171 | if (version < bcachefs_metadata_version_snapshot && |
172 | (level || btree_type_has_snapshots(id: btree_id))) { |
173 | u64 max_packed = |
174 | ~(~0ULL << f->bits_per_field[BKEY_FIELD_SNAPSHOT]); |
175 | |
176 | f->field_offset[BKEY_FIELD_SNAPSHOT] = write |
177 | ? 0 |
178 | : cpu_to_le64(U32_MAX - max_packed); |
179 | } |
180 | } |
181 | |
182 | static inline void compat_bpos(unsigned level, enum btree_id btree_id, |
183 | unsigned version, unsigned big_endian, |
184 | int write, struct bpos *p) |
185 | { |
186 | if (big_endian != CPU_BIG_ENDIAN) |
187 | bch2_bpos_swab(p); |
188 | |
189 | if (version < bcachefs_metadata_version_inode_btree_change && |
190 | btree_id == BTREE_ID_inodes) |
191 | swap(p->inode, p->offset); |
192 | } |
193 | |
194 | static inline void compat_btree_node(unsigned level, enum btree_id btree_id, |
195 | unsigned version, unsigned big_endian, |
196 | int write, |
197 | struct btree_node *bn) |
198 | { |
199 | if (version < bcachefs_metadata_version_inode_btree_change && |
200 | btree_id_is_extents(btree: btree_id) && |
201 | !bpos_eq(l: bn->min_key, POS_MIN) && |
202 | write) |
203 | bn->min_key = bpos_nosnap_predecessor(p: bn->min_key); |
204 | |
205 | if (version < bcachefs_metadata_version_snapshot && |
206 | write) |
207 | bn->max_key.snapshot = 0; |
208 | |
209 | compat_bpos(level, btree_id, version, big_endian, write, p: &bn->min_key); |
210 | compat_bpos(level, btree_id, version, big_endian, write, p: &bn->max_key); |
211 | |
212 | if (version < bcachefs_metadata_version_snapshot && |
213 | !write) |
214 | bn->max_key.snapshot = U32_MAX; |
215 | |
216 | if (version < bcachefs_metadata_version_inode_btree_change && |
217 | btree_id_is_extents(btree: btree_id) && |
218 | !bpos_eq(l: bn->min_key, POS_MIN) && |
219 | !write) |
220 | bn->min_key = bpos_nosnap_successor(p: bn->min_key); |
221 | } |
222 | |
223 | void bch2_btree_write_stats_to_text(struct printbuf *, struct bch_fs *); |
224 | |
225 | #endif /* _BCACHEFS_BTREE_IO_H */ |
226 | |