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