1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_IO_H
3 #define _BCACHEFS_BTREE_IO_H
14 struct btree_read_bio {
17 unsigned have_ioref:1;
18 struct extent_pick_ptr pick;
19 struct work_struct work;
23 struct btree_write_bio {
25 struct work_struct work;
26 struct bch_write_bio wbio;
29 static inline void btree_node_io_unlock(struct btree *b)
31 EBUG_ON(!btree_node_write_in_flight(b));
32 clear_btree_node_write_in_flight(b);
33 wake_up_bit(&b->flags, BTREE_NODE_write_in_flight);
36 static inline void btree_node_io_lock(struct btree *b)
38 wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
39 TASK_UNINTERRUPTIBLE);
42 static inline void btree_node_wait_on_io(struct btree *b)
44 wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
45 TASK_UNINTERRUPTIBLE);
48 static inline bool btree_node_may_write(struct btree *b)
50 return list_empty_careful(&b->write_blocked) &&
51 !b->will_make_reachable;
57 COMPACT_WRITTEN_NO_WRITE_LOCK,
60 bool __bch2_compact_whiteouts(struct bch_fs *, struct btree *, enum compact_mode);
62 static inline unsigned should_compact_bset_lazy(struct btree *b, struct bset_tree *t)
64 unsigned bset_u64s = le16_to_cpu(bset(b, t)->u64s);
65 unsigned dead_u64s = bset_u64s - b->nr.bset_u64s[t - b->set];
67 return dead_u64s > 128 && dead_u64s * 3 > bset_u64s;
70 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
75 if (should_compact_bset_lazy(b, t))
76 return __bch2_compact_whiteouts(c, b, COMPACT_LAZY);
81 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
83 void bch2_btree_build_aux_trees(struct btree *);
84 void bch2_btree_init_next(struct bch_fs *, struct btree *,
87 int bch2_btree_node_read_done(struct bch_fs *, struct btree *, bool);
88 void bch2_btree_node_read(struct bch_fs *, struct btree *, bool);
89 int bch2_btree_root_read(struct bch_fs *, enum btree_id,
90 const struct bkey_i *, unsigned);
92 void bch2_btree_complete_write(struct bch_fs *, struct btree *,
93 struct btree_write *);
94 void bch2_btree_write_error_work(struct work_struct *);
96 void __bch2_btree_node_write(struct bch_fs *, struct btree *,
98 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
100 void bch2_btree_node_write(struct bch_fs *, struct btree *,
104 * btree_node_dirty() can be cleared with only a read lock,
105 * and for bch2_btree_node_write_cond() we want to set need_write iff it's
108 static inline void set_btree_node_need_write_if_dirty(struct btree *b)
110 unsigned long old, new, v = READ_ONCE(b->flags);
115 if (!(old & (1 << BTREE_NODE_dirty)))
118 new |= (1 << BTREE_NODE_need_write);
119 } while ((v = cmpxchg(&b->flags, old, new)) != old);
122 #define bch2_btree_node_write_cond(_c, _b, cond) \
124 while ((_b)->written && btree_node_dirty(_b) && (cond)) { \
125 if (!btree_node_may_write(_b)) { \
126 set_btree_node_need_write_if_dirty(_b); \
130 if (!btree_node_write_in_flight(_b)) { \
131 bch2_btree_node_write(_c, _b, SIX_LOCK_read); \
135 six_unlock_read(&(_b)->lock); \
136 btree_node_wait_on_io(_b); \
137 btree_node_lock_type(c, b, SIX_LOCK_read); \
141 void bch2_btree_flush_all_reads(struct bch_fs *);
142 void bch2_btree_flush_all_writes(struct bch_fs *);
143 void bch2_btree_verify_flushed(struct bch_fs *);
144 ssize_t bch2_dirty_btree_nodes_print(struct bch_fs *, char *);
148 struct btree_node_iter_large {
152 struct btree_node_iter_set data[MAX_BSETS];
156 __bch2_btree_node_iter_large_init(struct btree_node_iter_large *iter,
160 iter->is_extents = is_extents;
163 void bch2_btree_node_iter_large_advance(struct btree_node_iter_large *,
166 void bch2_btree_node_iter_large_push(struct btree_node_iter_large *,
168 const struct bkey_packed *,
169 const struct bkey_packed *);
171 static inline bool bch2_btree_node_iter_large_end(struct btree_node_iter_large *iter)
176 static inline struct bkey_packed *
177 bch2_btree_node_iter_large_peek_all(struct btree_node_iter_large *iter,
180 return bch2_btree_node_iter_large_end(iter)
182 : __btree_node_offset_to_key(b, iter->data->k);
185 static inline struct bkey_packed *
186 bch2_btree_node_iter_large_next_all(struct btree_node_iter_large *iter,
189 struct bkey_packed *ret = bch2_btree_node_iter_large_peek_all(iter, b);
192 bch2_btree_node_iter_large_advance(iter, b);
197 #endif /* _BCACHEFS_BTREE_IO_H */