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