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