1 #ifndef _BCACHEFS_BTREE_IO_H
2 #define _BCACHEFS_BTREE_IO_H
5 #include "btree_locking.h"
14 struct btree_read_bio {
17 unsigned have_ioref:1;
18 struct extent_ptr_decoded 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->written || !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 *,
103 static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b)
106 btree_node_need_write(b) &&
107 btree_node_may_write(b)) {
108 if (!btree_node_write_in_flight(b)) {
109 bch2_btree_node_write(c, b, SIX_LOCK_read);
113 six_unlock_read(&b->lock);
114 btree_node_wait_on_io(b);
115 btree_node_lock_type(c, b, SIX_LOCK_read);
119 #define bch2_btree_node_write_cond(_c, _b, cond) \
121 unsigned long old, new, v = READ_ONCE((_b)->flags); \
126 if (!(old & (1 << BTREE_NODE_dirty)) || !(cond)) \
129 new |= (1 << BTREE_NODE_need_write); \
130 } while ((v = cmpxchg(&(_b)->flags, old, new)) != old); \
132 btree_node_write_if_need(_c, _b); \
135 void bch2_btree_flush_all_reads(struct bch_fs *);
136 void bch2_btree_flush_all_writes(struct bch_fs *);
137 void bch2_btree_verify_flushed(struct bch_fs *);
138 ssize_t bch2_dirty_btree_nodes_print(struct bch_fs *, char *);
140 #endif /* _BCACHEFS_BTREE_IO_H */