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