1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_IO_H
3 #define _BCACHEFS_BTREE_IO_H
5 #include "bkey_methods.h"
7 #include "btree_locking.h"
17 static inline bool btree_node_dirty(struct btree *b)
19 return test_bit(BTREE_NODE_dirty, &b->flags);
22 static inline void set_btree_node_dirty(struct bch_fs *c, struct btree *b)
24 if (!test_and_set_bit(BTREE_NODE_dirty, &b->flags))
25 atomic_inc(&c->btree_cache.dirty);
28 static inline void clear_btree_node_dirty(struct bch_fs *c, struct btree *b)
30 if (test_and_clear_bit(BTREE_NODE_dirty, &b->flags))
31 atomic_dec(&c->btree_cache.dirty);
34 struct btree_read_bio {
37 unsigned have_ioref:1;
38 struct extent_ptr_decoded pick;
39 struct work_struct work;
43 struct btree_write_bio {
44 struct work_struct work;
47 struct bch_write_bio wbio;
50 static inline void btree_node_io_unlock(struct btree *b)
52 EBUG_ON(!btree_node_write_in_flight(b));
53 clear_btree_node_write_in_flight(b);
54 wake_up_bit(&b->flags, BTREE_NODE_write_in_flight);
57 static inline void btree_node_io_lock(struct btree *b)
59 wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
60 TASK_UNINTERRUPTIBLE);
63 static inline void btree_node_wait_on_io(struct btree *b)
65 wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
66 TASK_UNINTERRUPTIBLE);
69 static inline bool btree_node_may_write(struct btree *b)
71 return list_empty_careful(&b->write_blocked) &&
72 (!b->written || !b->will_make_reachable);
80 bool bch2_compact_whiteouts(struct bch_fs *, struct btree *,
83 static inline bool should_compact_bset_lazy(struct btree *b,
86 unsigned total_u64s = bset_u64s(t);
87 unsigned dead_u64s = bset_dead_u64s(b, t);
89 return dead_u64s > 64 && dead_u64s * 3 > total_u64s;
92 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
97 if (should_compact_bset_lazy(b, t))
98 return bch2_compact_whiteouts(c, b, COMPACT_LAZY);
103 static inline struct nonce btree_nonce(struct bset *i, unsigned offset)
105 return (struct nonce) {{
106 [0] = cpu_to_le32(offset),
107 [1] = ((__le32 *) &i->seq)[0],
108 [2] = ((__le32 *) &i->seq)[1],
109 [3] = ((__le32 *) &i->journal_seq)[0]^BCH_NONCE_BTREE,
113 static inline void bset_encrypt(struct bch_fs *c, struct bset *i, unsigned offset)
115 struct nonce nonce = btree_nonce(i, offset);
118 struct btree_node *bn = container_of(i, struct btree_node, keys);
119 unsigned bytes = (void *) &bn->keys - (void *) &bn->flags;
121 bch2_encrypt(c, BSET_CSUM_TYPE(i), nonce, &bn->flags,
124 nonce = nonce_add(nonce, round_up(bytes, CHACHA_BLOCK_SIZE));
127 bch2_encrypt(c, BSET_CSUM_TYPE(i), nonce, i->_data,
128 vstruct_end(i) - (void *) i->_data);
131 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
133 void bch2_btree_build_aux_trees(struct btree *);
134 void bch2_btree_init_next(struct bch_fs *, struct btree *,
135 struct btree_iter *);
137 int bch2_btree_node_read_done(struct bch_fs *, struct bch_dev *,
138 struct btree *, bool);
139 void bch2_btree_node_read(struct bch_fs *, struct btree *, bool);
140 int bch2_btree_root_read(struct bch_fs *, enum btree_id,
141 const struct bkey_i *, unsigned);
143 void bch2_btree_complete_write(struct bch_fs *, struct btree *,
144 struct btree_write *);
145 void bch2_btree_write_error_work(struct work_struct *);
147 void __bch2_btree_node_write(struct bch_fs *, struct btree *,
149 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
151 void bch2_btree_node_write(struct bch_fs *, struct btree *,
154 static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b,
155 enum six_lock_type lock_held)
158 btree_node_need_write(b) &&
159 btree_node_may_write(b)) {
160 if (!btree_node_write_in_flight(b)) {
161 bch2_btree_node_write(c, b, lock_held);
165 six_unlock_type(&b->c.lock, lock_held);
166 btree_node_wait_on_io(b);
167 btree_node_lock_type(c, b, lock_held);
171 #define bch2_btree_node_write_cond(_c, _b, cond) \
173 unsigned long old, new, v = READ_ONCE((_b)->flags); \
178 if (!(old & (1 << BTREE_NODE_dirty)) || !(cond)) \
181 new |= (1 << BTREE_NODE_need_write); \
182 } while ((v = cmpxchg(&(_b)->flags, old, new)) != old); \
184 btree_node_write_if_need(_c, _b, SIX_LOCK_read); \
187 void bch2_btree_flush_all_reads(struct bch_fs *);
188 void bch2_btree_flush_all_writes(struct bch_fs *);
189 void bch2_dirty_btree_nodes_to_text(struct printbuf *, struct bch_fs *);
191 static inline void compat_bformat(unsigned level, enum btree_id btree_id,
192 unsigned version, unsigned big_endian,
193 int write, struct bkey_format *f)
195 if (version < bcachefs_metadata_version_inode_btree_change &&
196 btree_id == BTREE_ID_inodes) {
197 swap(f->bits_per_field[BKEY_FIELD_INODE],
198 f->bits_per_field[BKEY_FIELD_OFFSET]);
199 swap(f->field_offset[BKEY_FIELD_INODE],
200 f->field_offset[BKEY_FIELD_OFFSET]);
203 if (version < bcachefs_metadata_version_snapshot &&
204 (level || btree_type_has_snapshots(btree_id))) {
206 ~(~0ULL << f->bits_per_field[BKEY_FIELD_SNAPSHOT]);
208 f->field_offset[BKEY_FIELD_SNAPSHOT] = write
210 : U32_MAX - max_packed;
214 static inline void compat_bpos(unsigned level, enum btree_id btree_id,
215 unsigned version, unsigned big_endian,
216 int write, struct bpos *p)
218 if (big_endian != CPU_BIG_ENDIAN)
221 if (version < bcachefs_metadata_version_inode_btree_change &&
222 btree_id == BTREE_ID_inodes)
223 swap(p->inode, p->offset);
226 static inline void compat_btree_node(unsigned level, enum btree_id btree_id,
227 unsigned version, unsigned big_endian,
229 struct btree_node *bn)
231 if (version < bcachefs_metadata_version_inode_btree_change &&
232 btree_node_type_is_extents(btree_id) &&
233 bpos_cmp(bn->min_key, POS_MIN) &&
235 bn->min_key = bpos_nosnap_predecessor(bn->min_key);
237 if (version < bcachefs_metadata_version_snapshot &&
239 bn->max_key.snapshot = 0;
241 compat_bpos(level, btree_id, version, big_endian, write, &bn->min_key);
242 compat_bpos(level, btree_id, version, big_endian, write, &bn->max_key);
244 if (version < bcachefs_metadata_version_snapshot &&
246 bn->max_key.snapshot = U32_MAX;
248 if (version < bcachefs_metadata_version_inode_btree_change &&
249 btree_node_type_is_extents(btree_id) &&
250 bpos_cmp(bn->min_key, POS_MIN) &&
252 bn->min_key = bpos_nosnap_successor(bn->min_key);
255 #endif /* _BCACHEFS_BTREE_IO_H */