1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_IO_H
3 #define _BCACHEFS_BTREE_IO_H
6 #include "btree_locking.h"
15 struct btree_read_bio {
18 unsigned have_ioref:1;
19 struct extent_ptr_decoded pick;
20 struct work_struct work;
24 struct btree_write_bio {
26 struct work_struct work;
27 struct bch_write_bio wbio;
30 static inline void btree_node_io_unlock(struct btree *b)
32 EBUG_ON(!btree_node_write_in_flight(b));
33 clear_btree_node_write_in_flight(b);
34 wake_up_bit(&b->flags, BTREE_NODE_write_in_flight);
37 static inline void btree_node_io_lock(struct btree *b)
39 wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
40 TASK_UNINTERRUPTIBLE);
43 static inline void btree_node_wait_on_io(struct btree *b)
45 wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
46 TASK_UNINTERRUPTIBLE);
49 static inline bool btree_node_may_write(struct btree *b)
51 return list_empty_careful(&b->write_blocked) &&
52 (!b->written || !b->will_make_reachable);
60 bool bch2_compact_whiteouts(struct bch_fs *, struct btree *,
63 static inline bool should_compact_bset_lazy(struct btree *b,
66 unsigned total_u64s = bset_u64s(t);
67 unsigned dead_u64s = bset_dead_u64s(b, t);
69 return dead_u64s > 64 && dead_u64s * 3 > total_u64s;
72 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
77 if (should_compact_bset_lazy(b, t))
78 return bch2_compact_whiteouts(c, b, COMPACT_LAZY);
83 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
85 void bch2_btree_build_aux_trees(struct btree *);
86 void bch2_btree_init_next(struct bch_fs *, struct btree *,
89 int bch2_btree_node_read_done(struct bch_fs *, struct btree *, bool);
90 void bch2_btree_node_read(struct bch_fs *, struct btree *, bool);
91 int bch2_btree_root_read(struct bch_fs *, enum btree_id,
92 const struct bkey_i *, unsigned);
94 void bch2_btree_complete_write(struct bch_fs *, struct btree *,
95 struct btree_write *);
96 void bch2_btree_write_error_work(struct work_struct *);
98 void __bch2_btree_node_write(struct bch_fs *, struct btree *,
100 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
102 void bch2_btree_node_write(struct bch_fs *, struct btree *,
105 static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b)
108 btree_node_need_write(b) &&
109 btree_node_may_write(b)) {
110 if (!btree_node_write_in_flight(b)) {
111 bch2_btree_node_write(c, b, SIX_LOCK_read);
115 six_unlock_read(&b->lock);
116 btree_node_wait_on_io(b);
117 btree_node_lock_type(c, b, SIX_LOCK_read);
121 #define bch2_btree_node_write_cond(_c, _b, cond) \
123 unsigned long old, new, v = READ_ONCE((_b)->flags); \
128 if (!(old & (1 << BTREE_NODE_dirty)) || !(cond)) \
131 new |= (1 << BTREE_NODE_need_write); \
132 } while ((v = cmpxchg(&(_b)->flags, old, new)) != old); \
134 btree_node_write_if_need(_c, _b); \
137 void bch2_btree_flush_all_reads(struct bch_fs *);
138 void bch2_btree_flush_all_writes(struct bch_fs *);
139 void bch2_btree_verify_flushed(struct bch_fs *);
140 ssize_t bch2_dirty_btree_nodes_print(struct bch_fs *, char *);
142 #endif /* _BCACHEFS_BTREE_IO_H */