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 struct btree_read_bio {
20 unsigned have_ioref:1;
21 struct extent_ptr_decoded pick;
22 struct work_struct work;
26 struct btree_write_bio {
27 struct work_struct work;
30 struct bch_write_bio wbio;
33 static inline void btree_node_io_unlock(struct btree *b)
35 EBUG_ON(!btree_node_write_in_flight(b));
36 clear_btree_node_write_in_flight(b);
37 wake_up_bit(&b->flags, BTREE_NODE_write_in_flight);
40 static inline void btree_node_io_lock(struct btree *b)
42 wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
43 TASK_UNINTERRUPTIBLE);
46 static inline void btree_node_wait_on_io(struct btree *b)
48 wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
49 TASK_UNINTERRUPTIBLE);
52 static inline bool btree_node_may_write(struct btree *b)
54 return list_empty_careful(&b->write_blocked) &&
55 (!b->written || !b->will_make_reachable);
63 bool bch2_compact_whiteouts(struct bch_fs *, struct btree *,
66 static inline bool should_compact_bset_lazy(struct btree *b,
69 unsigned total_u64s = bset_u64s(t);
70 unsigned dead_u64s = bset_dead_u64s(b, t);
72 return dead_u64s > 64 && dead_u64s * 3 > total_u64s;
75 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
80 if (should_compact_bset_lazy(b, t))
81 return bch2_compact_whiteouts(c, b, COMPACT_LAZY);
86 static inline struct nonce btree_nonce(struct bset *i, unsigned offset)
88 return (struct nonce) {{
89 [0] = cpu_to_le32(offset),
90 [1] = ((__le32 *) &i->seq)[0],
91 [2] = ((__le32 *) &i->seq)[1],
92 [3] = ((__le32 *) &i->journal_seq)[0]^BCH_NONCE_BTREE,
96 static inline void bset_encrypt(struct bch_fs *c, struct bset *i, unsigned offset)
98 struct nonce nonce = btree_nonce(i, offset);
101 struct btree_node *bn = container_of(i, struct btree_node, keys);
102 unsigned bytes = (void *) &bn->keys - (void *) &bn->flags;
104 bch2_encrypt(c, BSET_CSUM_TYPE(i), nonce, &bn->flags,
107 nonce = nonce_add(nonce, round_up(bytes, CHACHA_BLOCK_SIZE));
110 bch2_encrypt(c, BSET_CSUM_TYPE(i), nonce, i->_data,
111 vstruct_end(i) - (void *) i->_data);
114 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
116 void bch2_btree_build_aux_trees(struct btree *);
117 void bch2_btree_init_next(struct bch_fs *, struct btree *,
118 struct btree_iter *);
120 int bch2_btree_node_read_done(struct bch_fs *, struct btree *, bool);
121 void bch2_btree_node_read(struct bch_fs *, struct btree *, bool);
122 int bch2_btree_root_read(struct bch_fs *, enum btree_id,
123 const struct bkey_i *, unsigned);
125 void bch2_btree_complete_write(struct bch_fs *, struct btree *,
126 struct btree_write *);
127 void bch2_btree_write_error_work(struct work_struct *);
129 void __bch2_btree_node_write(struct bch_fs *, struct btree *,
131 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
133 void bch2_btree_node_write(struct bch_fs *, struct btree *,
136 static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b,
137 enum six_lock_type lock_held)
140 btree_node_need_write(b) &&
141 btree_node_may_write(b)) {
142 if (!btree_node_write_in_flight(b)) {
143 bch2_btree_node_write(c, b, lock_held);
147 six_unlock_type(&b->c.lock, lock_held);
148 btree_node_wait_on_io(b);
149 btree_node_lock_type(c, b, lock_held);
153 #define bch2_btree_node_write_cond(_c, _b, cond) \
155 unsigned long old, new, v = READ_ONCE((_b)->flags); \
160 if (!(old & (1 << BTREE_NODE_dirty)) || !(cond)) \
163 new |= (1 << BTREE_NODE_need_write); \
164 } while ((v = cmpxchg(&(_b)->flags, old, new)) != old); \
166 btree_node_write_if_need(_c, _b, SIX_LOCK_read); \
169 void bch2_btree_flush_all_reads(struct bch_fs *);
170 void bch2_btree_flush_all_writes(struct bch_fs *);
171 void bch2_btree_verify_flushed(struct bch_fs *);
172 void bch2_dirty_btree_nodes_to_text(struct printbuf *, struct bch_fs *);
174 static inline void compat_bformat(unsigned level, enum btree_id btree_id,
175 unsigned version, unsigned big_endian,
176 int write, struct bkey_format *f)
178 if (version < bcachefs_metadata_version_inode_btree_change &&
179 btree_id == BTREE_ID_INODES) {
180 swap(f->bits_per_field[BKEY_FIELD_INODE],
181 f->bits_per_field[BKEY_FIELD_OFFSET]);
182 swap(f->field_offset[BKEY_FIELD_INODE],
183 f->field_offset[BKEY_FIELD_OFFSET]);
187 static inline void compat_bpos(unsigned level, enum btree_id btree_id,
188 unsigned version, unsigned big_endian,
189 int write, struct bpos *p)
191 if (big_endian != CPU_BIG_ENDIAN)
194 if (version < bcachefs_metadata_version_inode_btree_change &&
195 btree_id == BTREE_ID_INODES)
196 swap(p->inode, p->offset);
199 static inline void compat_btree_node(unsigned level, enum btree_id btree_id,
200 unsigned version, unsigned big_endian,
202 struct btree_node *bn)
204 if (version < bcachefs_metadata_version_inode_btree_change &&
205 btree_node_type_is_extents(btree_id) &&
206 bkey_cmp(bn->min_key, POS_MIN) &&
208 bn->min_key = bkey_predecessor(bn->min_key);
210 compat_bpos(level, btree_id, version, big_endian, write, &bn->min_key);
211 compat_bpos(level, btree_id, version, big_endian, write, &bn->max_key);
213 if (version < bcachefs_metadata_version_inode_btree_change &&
214 btree_node_type_is_extents(btree_id) &&
215 bkey_cmp(bn->min_key, POS_MIN) &&
217 bn->min_key = bkey_successor(bn->min_key);
220 #endif /* _BCACHEFS_BTREE_IO_H */