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);
58 COMPACT_WRITTEN_NO_WRITE_LOCK,
61 bool __bch2_compact_whiteouts(struct bch_fs *, struct btree *, enum compact_mode);
63 static inline unsigned should_compact_bset_lazy(struct btree *b, struct bset_tree *t)
65 unsigned total_u64s = bset_u64s(t);
66 unsigned dead_u64s = total_u64s - b->nr.bset_u64s[t - b->set];
68 return dead_u64s > 64 && dead_u64s * 3 > total_u64s;
71 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
76 if (should_compact_bset_lazy(b, t))
77 return __bch2_compact_whiteouts(c, b, COMPACT_LAZY);
82 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
84 void bch2_btree_build_aux_trees(struct btree *);
85 void bch2_btree_init_next(struct bch_fs *, struct btree *,
88 int bch2_btree_node_read_done(struct bch_fs *, struct btree *, bool);
89 void bch2_btree_node_read(struct bch_fs *, struct btree *, bool);
90 int bch2_btree_root_read(struct bch_fs *, enum btree_id,
91 const struct bkey_i *, unsigned);
93 void bch2_btree_complete_write(struct bch_fs *, struct btree *,
94 struct btree_write *);
95 void bch2_btree_write_error_work(struct work_struct *);
97 void __bch2_btree_node_write(struct bch_fs *, struct btree *,
99 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
101 void bch2_btree_node_write(struct bch_fs *, struct btree *,
104 static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b)
107 btree_node_need_write(b) &&
108 btree_node_may_write(b)) {
109 if (!btree_node_write_in_flight(b)) {
110 bch2_btree_node_write(c, b, SIX_LOCK_read);
114 six_unlock_read(&b->lock);
115 btree_node_wait_on_io(b);
116 btree_node_lock_type(c, b, SIX_LOCK_read);
120 #define bch2_btree_node_write_cond(_c, _b, cond) \
122 unsigned long old, new, v = READ_ONCE((_b)->flags); \
127 if (!(old & (1 << BTREE_NODE_dirty)) || !(cond)) \
130 new |= (1 << BTREE_NODE_need_write); \
131 } while ((v = cmpxchg(&(_b)->flags, old, new)) != old); \
133 btree_node_write_if_need(_c, _b); \
136 void bch2_btree_flush_all_reads(struct bch_fs *);
137 void bch2_btree_flush_all_writes(struct bch_fs *);
138 void bch2_btree_verify_flushed(struct bch_fs *);
139 ssize_t bch2_dirty_btree_nodes_print(struct bch_fs *, char *);
141 #endif /* _BCACHEFS_BTREE_IO_H */