1 #ifndef _BCACHEFS_BTREE_IO_H
2 #define _BCACHEFS_BTREE_IO_H
13 struct btree_read_bio {
16 unsigned have_ioref:1;
17 struct extent_ptr_decoded pick;
18 struct work_struct work;
22 struct btree_write_bio {
24 struct work_struct work;
25 struct bch_write_bio wbio;
28 static inline void btree_node_io_unlock(struct btree *b)
30 EBUG_ON(!btree_node_write_in_flight(b));
31 clear_btree_node_write_in_flight(b);
32 wake_up_bit(&b->flags, BTREE_NODE_write_in_flight);
35 static inline void btree_node_io_lock(struct btree *b)
37 wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
38 TASK_UNINTERRUPTIBLE);
41 static inline void btree_node_wait_on_io(struct btree *b)
43 wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
44 TASK_UNINTERRUPTIBLE);
47 static inline bool btree_node_may_write(struct btree *b)
49 return list_empty_careful(&b->write_blocked) &&
50 !b->will_make_reachable;
56 COMPACT_WRITTEN_NO_WRITE_LOCK,
59 bool __bch2_compact_whiteouts(struct bch_fs *, struct btree *, enum compact_mode);
61 static inline unsigned should_compact_bset_lazy(struct btree *b, struct bset_tree *t)
63 unsigned bset_u64s = le16_to_cpu(bset(b, t)->u64s);
64 unsigned dead_u64s = bset_u64s - b->nr.bset_u64s[t - b->set];
66 return dead_u64s > 128 && dead_u64s * 3 > bset_u64s;
69 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
74 if (should_compact_bset_lazy(b, t))
75 return __bch2_compact_whiteouts(c, b, COMPACT_LAZY);
80 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
82 void bch2_btree_build_aux_trees(struct btree *);
83 void bch2_btree_init_next(struct bch_fs *, struct btree *,
86 int bch2_btree_node_read_done(struct bch_fs *, struct btree *, bool);
87 void bch2_btree_node_read(struct bch_fs *, struct btree *, bool);
88 int bch2_btree_root_read(struct bch_fs *, enum btree_id,
89 const struct bkey_i *, unsigned);
91 void bch2_btree_complete_write(struct bch_fs *, struct btree *,
92 struct btree_write *);
93 void bch2_btree_write_error_work(struct work_struct *);
95 void __bch2_btree_node_write(struct bch_fs *, struct btree *,
97 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
99 void bch2_btree_node_write(struct bch_fs *, struct btree *,
103 * btree_node_dirty() can be cleared with only a read lock,
104 * and for bch2_btree_node_write_cond() we want to set need_write iff it's
107 static inline void set_btree_node_need_write_if_dirty(struct btree *b)
109 unsigned long old, new, v = READ_ONCE(b->flags);
114 if (!(old & (1 << BTREE_NODE_dirty)))
117 new |= (1 << BTREE_NODE_need_write);
118 } while ((v = cmpxchg(&b->flags, old, new)) != old);
121 #define bch2_btree_node_write_cond(_c, _b, cond) \
123 while ((_b)->written && btree_node_dirty(_b) && (cond)) { \
124 if (!btree_node_may_write(_b)) { \
125 set_btree_node_need_write_if_dirty(_b); \
129 if (!btree_node_write_in_flight(_b)) { \
130 bch2_btree_node_write(_c, _b, SIX_LOCK_read); \
134 six_unlock_read(&(_b)->lock); \
135 btree_node_wait_on_io(_b); \
136 btree_node_lock_type(c, b, SIX_LOCK_read); \
140 void bch2_btree_flush_all_reads(struct bch_fs *);
141 void bch2_btree_flush_all_writes(struct bch_fs *);
142 void bch2_btree_verify_flushed(struct bch_fs *);
143 ssize_t bch2_dirty_btree_nodes_print(struct bch_fs *, char *);
145 #endif /* _BCACHEFS_BTREE_IO_H */