]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_io.h
Update bcachefs sources to 3913e0cac3 bcachefs: Journal space calculation fix
[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 "bkey_methods.h"
6 #include "bset.h"
7 #include "btree_locking.h"
8 #include "checksum.h"
9 #include "extents.h"
10 #include "io_types.h"
11
12 struct bch_fs;
13 struct btree_write;
14 struct btree;
15 struct btree_iter;
16 struct btree_node_read_all;
17
18 static inline bool btree_node_dirty(struct btree *b)
19 {
20         return test_bit(BTREE_NODE_dirty, &b->flags);
21 }
22
23 static inline void set_btree_node_dirty(struct bch_fs *c, struct btree *b)
24 {
25         if (!test_and_set_bit(BTREE_NODE_dirty, &b->flags))
26                 atomic_inc(&c->btree_cache.dirty);
27 }
28
29 static inline void clear_btree_node_dirty(struct bch_fs *c, struct btree *b)
30 {
31         if (test_and_clear_bit(BTREE_NODE_dirty, &b->flags))
32                 atomic_dec(&c->btree_cache.dirty);
33 }
34
35 struct btree_read_bio {
36         struct bch_fs           *c;
37         struct btree            *b;
38         struct btree_node_read_all *ra;
39         u64                     start_time;
40         unsigned                have_ioref:1;
41         unsigned                idx:7;
42         struct extent_ptr_decoded       pick;
43         struct work_struct      work;
44         struct bio              bio;
45 };
46
47 struct btree_write_bio {
48         struct work_struct      work;
49         __BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
50         void                    *data;
51         unsigned                bytes;
52         struct bch_write_bio    wbio;
53 };
54
55 static inline void btree_node_io_unlock(struct btree *b)
56 {
57         EBUG_ON(!btree_node_write_in_flight(b));
58         clear_btree_node_write_in_flight(b);
59         wake_up_bit(&b->flags, BTREE_NODE_write_in_flight);
60 }
61
62 static inline void btree_node_io_lock(struct btree *b)
63 {
64         wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
65                             TASK_UNINTERRUPTIBLE);
66 }
67
68 static inline void btree_node_wait_on_io(struct btree *b)
69 {
70         wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
71                        TASK_UNINTERRUPTIBLE);
72 }
73
74 static inline bool btree_node_may_write(struct btree *b)
75 {
76         return list_empty_careful(&b->write_blocked) &&
77                 (!b->written || !b->will_make_reachable);
78 }
79
80 enum compact_mode {
81         COMPACT_LAZY,
82         COMPACT_ALL,
83 };
84
85 bool bch2_compact_whiteouts(struct bch_fs *, struct btree *,
86                             enum compact_mode);
87
88 static inline bool should_compact_bset_lazy(struct btree *b,
89                                             struct bset_tree *t)
90 {
91         unsigned total_u64s = bset_u64s(t);
92         unsigned dead_u64s = bset_dead_u64s(b, t);
93
94         return dead_u64s > 64 && dead_u64s * 3 > total_u64s;
95 }
96
97 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
98 {
99         struct bset_tree *t;
100
101         for_each_bset(b, t)
102                 if (should_compact_bset_lazy(b, t))
103                         return bch2_compact_whiteouts(c, b, COMPACT_LAZY);
104
105         return false;
106 }
107
108 static inline struct nonce btree_nonce(struct bset *i, unsigned offset)
109 {
110         return (struct nonce) {{
111                 [0] = cpu_to_le32(offset),
112                 [1] = ((__le32 *) &i->seq)[0],
113                 [2] = ((__le32 *) &i->seq)[1],
114                 [3] = ((__le32 *) &i->journal_seq)[0]^BCH_NONCE_BTREE,
115         }};
116 }
117
118 static inline void bset_encrypt(struct bch_fs *c, struct bset *i, unsigned offset)
119 {
120         struct nonce nonce = btree_nonce(i, offset);
121
122         if (!offset) {
123                 struct btree_node *bn = container_of(i, struct btree_node, keys);
124                 unsigned bytes = (void *) &bn->keys - (void *) &bn->flags;
125
126                 bch2_encrypt(c, BSET_CSUM_TYPE(i), nonce, &bn->flags,
127                              bytes);
128
129                 nonce = nonce_add(nonce, round_up(bytes, CHACHA_BLOCK_SIZE));
130         }
131
132         bch2_encrypt(c, BSET_CSUM_TYPE(i), nonce, i->_data,
133                      vstruct_end(i) - (void *) i->_data);
134 }
135
136 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
137
138 void bch2_btree_node_drop_keys_outside_node(struct btree *);
139
140 void bch2_btree_build_aux_trees(struct btree *);
141 void bch2_btree_init_next(struct bch_fs *, struct btree *,
142                          struct btree_iter *);
143
144 int bch2_btree_node_read_done(struct bch_fs *, struct bch_dev *,
145                               struct btree *, bool);
146 void bch2_btree_node_read(struct bch_fs *, struct btree *, bool);
147 int bch2_btree_root_read(struct bch_fs *, enum btree_id,
148                          const struct bkey_i *, unsigned);
149
150 void bch2_btree_complete_write(struct bch_fs *, struct btree *,
151                               struct btree_write *);
152 void bch2_btree_write_error_work(struct work_struct *);
153
154 void __bch2_btree_node_write(struct bch_fs *, struct btree *);
155 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
156
157 void bch2_btree_node_write(struct bch_fs *, struct btree *,
158                           enum six_lock_type);
159
160 static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b,
161                                             enum six_lock_type lock_held)
162 {
163         while (b->written &&
164                btree_node_need_write(b) &&
165                btree_node_may_write(b)) {
166                 if (!btree_node_write_in_flight(b)) {
167                         bch2_btree_node_write(c, b, lock_held);
168                         break;
169                 }
170
171                 six_unlock_type(&b->c.lock, lock_held);
172                 btree_node_wait_on_io(b);
173                 btree_node_lock_type(c, b, lock_held);
174         }
175 }
176
177 #define bch2_btree_node_write_cond(_c, _b, cond)                        \
178 do {                                                                    \
179         unsigned long old, new, v = READ_ONCE((_b)->flags);             \
180                                                                         \
181         do {                                                            \
182                 old = new = v;                                          \
183                                                                         \
184                 if (!(old & (1 << BTREE_NODE_dirty)) || !(cond))        \
185                         break;                                          \
186                                                                         \
187                 new |= (1 << BTREE_NODE_need_write);                    \
188         } while ((v = cmpxchg(&(_b)->flags, old, new)) != old);         \
189                                                                         \
190         btree_node_write_if_need(_c, _b, SIX_LOCK_read);                \
191 } while (0)
192
193 void bch2_btree_flush_all_reads(struct bch_fs *);
194 void bch2_btree_flush_all_writes(struct bch_fs *);
195 void bch2_dirty_btree_nodes_to_text(struct printbuf *, struct bch_fs *);
196
197 static inline void compat_bformat(unsigned level, enum btree_id btree_id,
198                                   unsigned version, unsigned big_endian,
199                                   int write, struct bkey_format *f)
200 {
201         if (version < bcachefs_metadata_version_inode_btree_change &&
202             btree_id == BTREE_ID_inodes) {
203                 swap(f->bits_per_field[BKEY_FIELD_INODE],
204                      f->bits_per_field[BKEY_FIELD_OFFSET]);
205                 swap(f->field_offset[BKEY_FIELD_INODE],
206                      f->field_offset[BKEY_FIELD_OFFSET]);
207         }
208
209         if (version < bcachefs_metadata_version_snapshot &&
210             (level || btree_type_has_snapshots(btree_id))) {
211                 u64 max_packed =
212                         ~(~0ULL << f->bits_per_field[BKEY_FIELD_SNAPSHOT]);
213
214                 f->field_offset[BKEY_FIELD_SNAPSHOT] = write
215                         ? 0
216                         : U32_MAX - max_packed;
217         }
218 }
219
220 static inline void compat_bpos(unsigned level, enum btree_id btree_id,
221                                unsigned version, unsigned big_endian,
222                                int write, struct bpos *p)
223 {
224         if (big_endian != CPU_BIG_ENDIAN)
225                 bch2_bpos_swab(p);
226
227         if (version < bcachefs_metadata_version_inode_btree_change &&
228             btree_id == BTREE_ID_inodes)
229                 swap(p->inode, p->offset);
230 }
231
232 static inline void compat_btree_node(unsigned level, enum btree_id btree_id,
233                                      unsigned version, unsigned big_endian,
234                                      int write,
235                                      struct btree_node *bn)
236 {
237         if (version < bcachefs_metadata_version_inode_btree_change &&
238             btree_node_type_is_extents(btree_id) &&
239             bpos_cmp(bn->min_key, POS_MIN) &&
240             write)
241                 bn->min_key = bpos_nosnap_predecessor(bn->min_key);
242
243         if (version < bcachefs_metadata_version_snapshot &&
244             write)
245                 bn->max_key.snapshot = 0;
246
247         compat_bpos(level, btree_id, version, big_endian, write, &bn->min_key);
248         compat_bpos(level, btree_id, version, big_endian, write, &bn->max_key);
249
250         if (version < bcachefs_metadata_version_snapshot &&
251             !write)
252                 bn->max_key.snapshot = U32_MAX;
253
254         if (version < bcachefs_metadata_version_inode_btree_change &&
255             btree_node_type_is_extents(btree_id) &&
256             bpos_cmp(bn->min_key, POS_MIN) &&
257             !write)
258                 bn->min_key = bpos_nosnap_successor(bn->min_key);
259 }
260
261 #endif /* _BCACHEFS_BTREE_IO_H */