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