Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/bcachefs/btree_io.h
26278 views
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_write_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 void set_btree_node_dirty_acct(struct bch_fs *c, struct btree *b)
19
{
20
if (!test_and_set_bit(BTREE_NODE_dirty, &b->flags))
21
atomic_long_inc(&c->btree_cache.nr_dirty);
22
}
23
24
static inline void clear_btree_node_dirty_acct(struct bch_fs *c, struct btree *b)
25
{
26
if (test_and_clear_bit(BTREE_NODE_dirty, &b->flags))
27
atomic_long_dec(&c->btree_cache.nr_dirty);
28
}
29
30
static inline unsigned btree_ptr_sectors_written(struct bkey_s_c k)
31
{
32
return k.k->type == KEY_TYPE_btree_ptr_v2
33
? le16_to_cpu(bkey_s_c_to_btree_ptr_v2(k).v->sectors_written)
34
: 0;
35
}
36
37
struct btree_read_bio {
38
struct bch_fs *c;
39
struct btree *b;
40
struct btree_node_read_all *ra;
41
u64 start_time;
42
unsigned have_ioref:1;
43
unsigned idx:7;
44
#ifdef CONFIG_BCACHEFS_ASYNC_OBJECT_LISTS
45
unsigned list_idx;
46
#endif
47
struct extent_ptr_decoded pick;
48
struct work_struct work;
49
struct bio bio;
50
};
51
52
struct btree_write_bio {
53
struct work_struct work;
54
__BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
55
void *data;
56
unsigned data_bytes;
57
unsigned sector_offset;
58
u64 start_time;
59
#ifdef CONFIG_BCACHEFS_ASYNC_OBJECT_LISTS
60
unsigned list_idx;
61
#endif
62
struct bch_write_bio wbio;
63
};
64
65
void bch2_btree_node_io_unlock(struct btree *);
66
void bch2_btree_node_io_lock(struct btree *);
67
void __bch2_btree_node_wait_on_read(struct btree *);
68
void __bch2_btree_node_wait_on_write(struct btree *);
69
void bch2_btree_node_wait_on_read(struct btree *);
70
void bch2_btree_node_wait_on_write(struct btree *);
71
72
enum compact_mode {
73
COMPACT_LAZY,
74
COMPACT_ALL,
75
};
76
77
bool bch2_compact_whiteouts(struct bch_fs *, struct btree *,
78
enum compact_mode);
79
80
static inline bool should_compact_bset_lazy(struct btree *b,
81
struct bset_tree *t)
82
{
83
unsigned total_u64s = bset_u64s(t);
84
unsigned dead_u64s = bset_dead_u64s(b, t);
85
86
return dead_u64s > 64 && dead_u64s * 3 > total_u64s;
87
}
88
89
static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
90
{
91
for_each_bset(b, t)
92
if (should_compact_bset_lazy(b, t))
93
return bch2_compact_whiteouts(c, b, COMPACT_LAZY);
94
95
return false;
96
}
97
98
static inline struct nonce btree_nonce(struct bset *i, unsigned offset)
99
{
100
return (struct nonce) {{
101
[0] = cpu_to_le32(offset),
102
[1] = ((__le32 *) &i->seq)[0],
103
[2] = ((__le32 *) &i->seq)[1],
104
[3] = ((__le32 *) &i->journal_seq)[0]^BCH_NONCE_BTREE,
105
}};
106
}
107
108
static inline int bset_encrypt(struct bch_fs *c, struct bset *i, unsigned offset)
109
{
110
struct nonce nonce = btree_nonce(i, offset);
111
int ret;
112
113
if (!offset) {
114
struct btree_node *bn = container_of(i, struct btree_node, keys);
115
unsigned bytes = (void *) &bn->keys - (void *) &bn->flags;
116
117
ret = bch2_encrypt(c, BSET_CSUM_TYPE(i), nonce,
118
&bn->flags, bytes);
119
if (ret)
120
return ret;
121
122
nonce = nonce_add(nonce, round_up(bytes, CHACHA_BLOCK_SIZE));
123
}
124
125
return bch2_encrypt(c, BSET_CSUM_TYPE(i), nonce, i->_data,
126
vstruct_end(i) - (void *) i->_data);
127
}
128
129
void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
130
131
void bch2_btree_node_drop_keys_outside_node(struct btree *);
132
133
void bch2_btree_build_aux_trees(struct btree *);
134
void bch2_btree_init_next(struct btree_trans *, struct btree *);
135
136
int bch2_btree_node_read_done(struct bch_fs *, struct bch_dev *,
137
struct btree *,
138
struct bch_io_failures *,
139
struct printbuf *);
140
void bch2_btree_node_read(struct btree_trans *, struct btree *, bool);
141
int bch2_btree_root_read(struct bch_fs *, enum btree_id,
142
const struct bkey_i *, unsigned);
143
144
void bch2_btree_read_bio_to_text(struct printbuf *, struct btree_read_bio *);
145
146
int bch2_btree_node_scrub(struct btree_trans *, enum btree_id, unsigned,
147
struct bkey_s_c, unsigned);
148
149
bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
150
151
enum btree_write_flags {
152
__BTREE_WRITE_ONLY_IF_NEED = BTREE_WRITE_TYPE_BITS,
153
__BTREE_WRITE_ALREADY_STARTED,
154
};
155
#define BTREE_WRITE_ONLY_IF_NEED BIT(__BTREE_WRITE_ONLY_IF_NEED)
156
#define BTREE_WRITE_ALREADY_STARTED BIT(__BTREE_WRITE_ALREADY_STARTED)
157
158
void __bch2_btree_node_write(struct bch_fs *, struct btree *, unsigned);
159
void bch2_btree_node_write(struct bch_fs *, struct btree *,
160
enum six_lock_type, unsigned);
161
void bch2_btree_node_write_trans(struct btree_trans *, struct btree *,
162
enum six_lock_type, unsigned);
163
164
static inline void btree_node_write_if_need(struct btree_trans *trans, struct btree *b,
165
enum six_lock_type lock_held)
166
{
167
bch2_btree_node_write_trans(trans, b, lock_held, BTREE_WRITE_ONLY_IF_NEED);
168
}
169
170
bool bch2_btree_flush_all_reads(struct bch_fs *);
171
bool bch2_btree_flush_all_writes(struct bch_fs *);
172
173
static inline void compat_bformat(unsigned level, enum btree_id btree_id,
174
unsigned version, unsigned big_endian,
175
int write, struct bkey_format *f)
176
{
177
if (version < bcachefs_metadata_version_inode_btree_change &&
178
btree_id == BTREE_ID_inodes) {
179
swap(f->bits_per_field[BKEY_FIELD_INODE],
180
f->bits_per_field[BKEY_FIELD_OFFSET]);
181
swap(f->field_offset[BKEY_FIELD_INODE],
182
f->field_offset[BKEY_FIELD_OFFSET]);
183
}
184
185
if (version < bcachefs_metadata_version_snapshot &&
186
(level || btree_type_has_snapshots(btree_id))) {
187
u64 max_packed =
188
~(~0ULL << f->bits_per_field[BKEY_FIELD_SNAPSHOT]);
189
190
f->field_offset[BKEY_FIELD_SNAPSHOT] = write
191
? 0
192
: cpu_to_le64(U32_MAX - max_packed);
193
}
194
}
195
196
static inline void compat_bpos(unsigned level, enum btree_id btree_id,
197
unsigned version, unsigned big_endian,
198
int write, struct bpos *p)
199
{
200
if (big_endian != CPU_BIG_ENDIAN)
201
bch2_bpos_swab(p);
202
203
if (version < bcachefs_metadata_version_inode_btree_change &&
204
btree_id == BTREE_ID_inodes)
205
swap(p->inode, p->offset);
206
}
207
208
static inline void compat_btree_node(unsigned level, enum btree_id btree_id,
209
unsigned version, unsigned big_endian,
210
int write,
211
struct btree_node *bn)
212
{
213
if (version < bcachefs_metadata_version_inode_btree_change &&
214
btree_id_is_extents(btree_id) &&
215
!bpos_eq(bn->min_key, POS_MIN) &&
216
write)
217
bn->min_key = bpos_nosnap_predecessor(bn->min_key);
218
219
if (version < bcachefs_metadata_version_snapshot &&
220
write)
221
bn->max_key.snapshot = 0;
222
223
compat_bpos(level, btree_id, version, big_endian, write, &bn->min_key);
224
compat_bpos(level, btree_id, version, big_endian, write, &bn->max_key);
225
226
if (version < bcachefs_metadata_version_snapshot &&
227
!write)
228
bn->max_key.snapshot = U32_MAX;
229
230
if (version < bcachefs_metadata_version_inode_btree_change &&
231
btree_id_is_extents(btree_id) &&
232
!bpos_eq(bn->min_key, POS_MIN) &&
233
!write)
234
bn->min_key = bpos_nosnap_successor(bn->min_key);
235
}
236
237
void bch2_btree_write_stats_to_text(struct printbuf *, struct bch_fs *);
238
239
#endif /* _BCACHEFS_BTREE_IO_H */
240
241