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