]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_io.h
Update bcachefs sources to ef60854e99 bcachefs: More allocator startup improvements
[bcachefs-tools-debian] / libbcachefs / btree_io.h
1 #ifndef _BCACHEFS_BTREE_IO_H
2 #define _BCACHEFS_BTREE_IO_H
3
4 #include "bset.h"
5 #include "btree_locking.h"
6 #include "extents.h"
7 #include "io_types.h"
8
9 struct bch_fs;
10 struct btree_write;
11 struct btree;
12 struct btree_iter;
13
14 struct btree_read_bio {
15         struct bch_fs           *c;
16         u64                     start_time;
17         unsigned                have_ioref:1;
18         struct extent_ptr_decoded       pick;
19         struct work_struct      work;
20         struct bio              bio;
21 };
22
23 struct btree_write_bio {
24         void                    *data;
25         struct work_struct      work;
26         struct bch_write_bio    wbio;
27 };
28
29 static inline void btree_node_io_unlock(struct btree *b)
30 {
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);
34 }
35
36 static inline void btree_node_io_lock(struct btree *b)
37 {
38         wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
39                             TASK_UNINTERRUPTIBLE);
40 }
41
42 static inline void btree_node_wait_on_io(struct btree *b)
43 {
44         wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
45                        TASK_UNINTERRUPTIBLE);
46 }
47
48 static inline bool btree_node_may_write(struct btree *b)
49 {
50         return list_empty_careful(&b->write_blocked) &&
51                 (!b->written || !b->will_make_reachable);
52 }
53
54 enum compact_mode {
55         COMPACT_LAZY,
56         COMPACT_WRITTEN,
57         COMPACT_WRITTEN_NO_WRITE_LOCK,
58 };
59
60 bool __bch2_compact_whiteouts(struct bch_fs *, struct btree *, enum compact_mode);
61
62 static inline unsigned should_compact_bset_lazy(struct btree *b, struct bset_tree *t)
63 {
64         unsigned bset_u64s = le16_to_cpu(bset(b, t)->u64s);
65         unsigned dead_u64s = bset_u64s - b->nr.bset_u64s[t - b->set];
66
67         return dead_u64s > 128 && dead_u64s * 3 > bset_u64s;
68 }
69
70 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
71 {
72         struct bset_tree *t;
73
74         for_each_bset(b, t)
75                 if (should_compact_bset_lazy(b, t))
76                         return __bch2_compact_whiteouts(c, b, COMPACT_LAZY);
77
78         return false;
79 }
80
81 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
82
83 void bch2_btree_build_aux_trees(struct btree *);
84 void bch2_btree_init_next(struct bch_fs *, struct btree *,
85                          struct btree_iter *);
86
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);
91
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 *);
95
96 void __bch2_btree_node_write(struct bch_fs *, struct btree *,
97                             enum six_lock_type);
98 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
99
100 void bch2_btree_node_write(struct bch_fs *, struct btree *,
101                           enum six_lock_type);
102
103 static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b)
104 {
105         while (b->written &&
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);
110                         break;
111                 }
112
113                 six_unlock_read(&b->lock);
114                 btree_node_wait_on_io(b);
115                 btree_node_lock_type(c, b, SIX_LOCK_read);
116         }
117 }
118
119 #define bch2_btree_node_write_cond(_c, _b, cond)                        \
120 do {                                                                    \
121         unsigned long old, new, v = READ_ONCE((_b)->flags);             \
122                                                                         \
123         do {                                                            \
124                 old = new = v;                                          \
125                                                                         \
126                 if (!(old & (1 << BTREE_NODE_dirty)) || !(cond))        \
127                         break;                                          \
128                                                                         \
129                 new |= (1 << BTREE_NODE_need_write);                    \
130         } while ((v = cmpxchg(&(_b)->flags, old, new)) != old);         \
131                                                                         \
132         btree_node_write_if_need(_c, _b);                               \
133 } while (0)
134
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 *);
139
140 #endif /* _BCACHEFS_BTREE_IO_H */