Commit | Line | Data |
---|---|---|
1c6fdbd8 KO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #include "bcachefs.h" | |
59cc38b8 | 3 | #include "alloc_background.h" |
7b3f84ea | 4 | #include "alloc_foreground.h" |
39fb2983 | 5 | #include "btree_io.h" |
00b8ccf7 | 6 | #include "btree_update_interior.h" |
09caeabe | 7 | #include "btree_write_buffer.h" |
1c6fdbd8 KO |
8 | #include "buckets.h" |
9 | #include "checksum.h" | |
d042b040 | 10 | #include "disk_groups.h" |
1c6fdbd8 KO |
11 | #include "error.h" |
12 | #include "journal.h" | |
13 | #include "journal_io.h" | |
14 | #include "journal_reclaim.h" | |
adbcada4 | 15 | #include "journal_seq_blacklist.h" |
1c6fdbd8 | 16 | #include "replicas.h" |
a37ad1a3 | 17 | #include "sb-clean.h" |
1c6fdbd8 KO |
18 | #include "trace.h" |
19 | ||
75474a54 | 20 | #include <linux/ioprio.h> |
de902e3b | 21 | #include <linux/string_choices.h> |
aed4ccbf | 22 | #include <linux/sched/sysctl.h> |
de902e3b | 23 | |
45150765 KO |
24 | void bch2_journal_pos_from_member_info_set(struct bch_fs *c) |
25 | { | |
26 | lockdep_assert_held(&c->sb_lock); | |
27 | ||
28 | for_each_member_device(c, ca) { | |
29 | struct bch_member *m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); | |
30 | ||
31 | m->last_journal_bucket = cpu_to_le32(ca->journal.cur_idx); | |
32 | m->last_journal_bucket_offset = cpu_to_le32(ca->mi.bucket_size - ca->journal.sectors_free); | |
33 | } | |
34 | } | |
35 | ||
36 | void bch2_journal_pos_from_member_info_resume(struct bch_fs *c) | |
37 | { | |
38 | mutex_lock(&c->sb_lock); | |
39 | for_each_member_device(c, ca) { | |
40 | struct bch_member m = bch2_sb_member_get(c->disk_sb.sb, ca->dev_idx); | |
41 | ||
42 | unsigned idx = le32_to_cpu(m.last_journal_bucket); | |
43 | if (idx < ca->journal.nr) | |
44 | ca->journal.cur_idx = idx; | |
45 | unsigned offset = le32_to_cpu(m.last_journal_bucket_offset); | |
46 | if (offset <= ca->mi.bucket_size) | |
47 | ca->journal.sectors_free = ca->mi.bucket_size - offset; | |
48 | } | |
49 | mutex_unlock(&c->sb_lock); | |
50 | } | |
51 | ||
cd04497b KO |
52 | static void bch2_journal_ptr_to_text(struct printbuf *out, struct bch_fs *c, struct journal_ptr *p) |
53 | { | |
54 | struct bch_dev *ca = bch2_dev_tryget_noerror(c, p->dev); | |
55 | prt_printf(out, "%s %u:%u:%u (sector %llu)", | |
56 | ca ? ca->name : "(invalid dev)", | |
57 | p->dev, p->bucket, p->bucket_offset, p->sector); | |
58 | bch2_dev_put(ca); | |
59 | } | |
60 | ||
61 | void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c, struct journal_replay *j) | |
3d3d23b3 KO |
62 | { |
63 | darray_for_each(j->ptrs, i) { | |
3d3d23b3 KO |
64 | if (i != j->ptrs.data) |
65 | prt_printf(out, " "); | |
cd04497b | 66 | bch2_journal_ptr_to_text(out, c, i); |
3d3d23b3 KO |
67 | } |
68 | } | |
69 | ||
cd04497b | 70 | static void bch2_journal_datetime_to_text(struct printbuf *out, struct jset *j) |
3d3d23b3 | 71 | { |
cd04497b | 72 | for_each_jset_entry_type(entry, j, BCH_JSET_ENTRY_datetime) { |
52f7d75e KO |
73 | struct jset_entry_datetime *datetime = |
74 | container_of(entry, struct jset_entry_datetime, entry); | |
75 | bch2_prt_datetime(out, le64_to_cpu(datetime->seconds)); | |
76 | break; | |
77 | } | |
3d3d23b3 KO |
78 | } |
79 | ||
cd04497b KO |
80 | static void bch2_journal_replay_to_text(struct printbuf *out, struct bch_fs *c, |
81 | struct journal_replay *j) | |
82 | { | |
83 | prt_printf(out, "seq %llu ", le64_to_cpu(j->j.seq)); | |
84 | bch2_journal_datetime_to_text(out, &j->j); | |
85 | prt_char(out, ' '); | |
86 | bch2_journal_ptrs_to_text(out, c, j); | |
87 | } | |
88 | ||
17fe3b64 KO |
89 | static struct nonce journal_nonce(const struct jset *jset) |
90 | { | |
91 | return (struct nonce) {{ | |
92 | [0] = 0, | |
93 | [1] = ((__le32 *) &jset->seq)[0], | |
94 | [2] = ((__le32 *) &jset->seq)[1], | |
95 | [3] = BCH_NONCE_JOURNAL, | |
96 | }}; | |
97 | } | |
98 | ||
4819b66e | 99 | static bool jset_csum_good(struct bch_fs *c, struct jset *j, struct bch_csum *csum) |
17fe3b64 | 100 | { |
4819b66e KO |
101 | if (!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(j))) { |
102 | *csum = (struct bch_csum) {}; | |
103 | return false; | |
104 | } | |
105 | ||
106 | *csum = csum_vstruct(c, JSET_CSUM_TYPE(j), journal_nonce(j), j); | |
107 | return !bch2_crc_cmp(j->csum, *csum); | |
17fe3b64 KO |
108 | } |
109 | ||
ec7ccbde | 110 | static inline u32 journal_entry_radix_idx(struct bch_fs *c, u64 seq) |
adbcada4 | 111 | { |
ec7ccbde | 112 | return (seq - c->journal_entries_base_seq) & (~0U >> 1); |
ce6201c4 KO |
113 | } |
114 | ||
115 | static void __journal_replay_free(struct bch_fs *c, | |
116 | struct journal_replay *i) | |
117 | { | |
118 | struct journal_replay **p = | |
ec7ccbde KO |
119 | genradix_ptr(&c->journal_entries, |
120 | journal_entry_radix_idx(c, le64_to_cpu(i->j.seq))); | |
ce6201c4 KO |
121 | |
122 | BUG_ON(*p != i); | |
123 | *p = NULL; | |
cb6fc943 | 124 | kvfree(i); |
adbcada4 KO |
125 | } |
126 | ||
2cce3752 | 127 | static void journal_replay_free(struct bch_fs *c, struct journal_replay *i, bool blacklisted) |
adbcada4 | 128 | { |
2cce3752 KO |
129 | if (blacklisted) |
130 | i->ignore_blacklisted = true; | |
131 | else | |
132 | i->ignore_not_dirty = true; | |
adbcada4 KO |
133 | |
134 | if (!c->opts.read_entire_journal) | |
ce6201c4 | 135 | __journal_replay_free(c, i); |
adbcada4 KO |
136 | } |
137 | ||
1c6fdbd8 KO |
138 | struct journal_list { |
139 | struct closure cl; | |
ec7ccbde | 140 | u64 last_seq; |
1c6fdbd8 | 141 | struct mutex lock; |
1c6fdbd8 KO |
142 | int ret; |
143 | }; | |
144 | ||
145 | #define JOURNAL_ENTRY_ADD_OK 0 | |
146 | #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE 5 | |
147 | ||
148 | /* | |
149 | * Given a journal entry we just read, add it to the list of journal entries to | |
150 | * be replayed: | |
151 | */ | |
152 | static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca, | |
72b7d633 | 153 | struct journal_ptr entry_ptr, |
17fe3b64 | 154 | struct journal_list *jlist, struct jset *j) |
1c6fdbd8 | 155 | { |
ce6201c4 KO |
156 | struct genradix_iter iter; |
157 | struct journal_replay **_i, *i, *dup; | |
1c6fdbd8 | 158 | size_t bytes = vstruct_bytes(j); |
ec7ccbde | 159 | u64 last_seq = !JSET_NO_FLUSH(j) ? le64_to_cpu(j->last_seq) : 0; |
3d3d23b3 | 160 | struct printbuf buf = PRINTBUF; |
e4c3f386 | 161 | int ret = JOURNAL_ENTRY_ADD_OK; |
1c6fdbd8 | 162 | |
17c3395e KO |
163 | if (last_seq && c->opts.journal_rewind) |
164 | last_seq = min(last_seq, c->opts.journal_rewind); | |
165 | ||
f0415829 KO |
166 | if (!c->journal.oldest_seq_found_ondisk || |
167 | le64_to_cpu(j->seq) < c->journal.oldest_seq_found_ondisk) | |
168 | c->journal.oldest_seq_found_ondisk = le64_to_cpu(j->seq); | |
169 | ||
ec7ccbde KO |
170 | /* Is this entry older than the range we need? */ |
171 | if (!c->opts.read_entire_journal && | |
172 | le64_to_cpu(j->seq) < jlist->last_seq) | |
173 | return JOURNAL_ENTRY_ADD_OUT_OF_RANGE; | |
174 | ||
ce6201c4 | 175 | /* |
ec7ccbde KO |
176 | * genradixes are indexed by a ulong, not a u64, so we can't index them |
177 | * by sequence number directly: Assume instead that they will all fall | |
178 | * within the range of +-2billion of the filrst one we find. | |
ce6201c4 KO |
179 | */ |
180 | if (!c->journal_entries_base_seq) | |
181 | c->journal_entries_base_seq = max_t(s64, 1, le64_to_cpu(j->seq) - S32_MAX); | |
182 | ||
adbcada4 | 183 | /* Drop entries we don't need anymore */ |
ec7ccbde KO |
184 | if (last_seq > jlist->last_seq && !c->opts.read_entire_journal) { |
185 | genradix_for_each_from(&c->journal_entries, iter, _i, | |
186 | journal_entry_radix_idx(c, jlist->last_seq)) { | |
ce6201c4 KO |
187 | i = *_i; |
188 | ||
2cce3752 | 189 | if (journal_replay_ignore(i)) |
ce6201c4 KO |
190 | continue; |
191 | ||
ec7ccbde | 192 | if (le64_to_cpu(i->j.seq) >= last_seq) |
7fffc85b | 193 | break; |
2cce3752 KO |
194 | |
195 | journal_replay_free(c, i, false); | |
7fffc85b | 196 | } |
1c6fdbd8 KO |
197 | } |
198 | ||
ec7ccbde KO |
199 | jlist->last_seq = max(jlist->last_seq, last_seq); |
200 | ||
201 | _i = genradix_ptr_alloc(&c->journal_entries, | |
202 | journal_entry_radix_idx(c, le64_to_cpu(j->seq)), | |
203 | GFP_KERNEL); | |
204 | if (!_i) | |
09b9c72b | 205 | return bch_err_throw(c, ENOMEM_journal_entry_add); |
e4c3f386 | 206 | |
ca73852a KO |
207 | /* |
208 | * Duplicate journal entries? If so we want the one that didn't have a | |
209 | * checksum error: | |
210 | */ | |
ec7ccbde | 211 | dup = *_i; |
e4c3f386 | 212 | if (dup) { |
3d3d23b3 KO |
213 | bool identical = bytes == vstruct_bytes(&dup->j) && |
214 | !memcmp(j, &dup->j, bytes); | |
215 | bool not_identical = !identical && | |
216 | entry_ptr.csum_good && | |
217 | dup->csum_good; | |
17fe3b64 | 218 | |
3d3d23b3 KO |
219 | bool same_device = false; |
220 | darray_for_each(dup->ptrs, ptr) | |
221 | if (ptr->dev == ca->dev_idx) | |
222 | same_device = true; | |
223 | ||
224 | ret = darray_push(&dup->ptrs, entry_ptr); | |
225 | if (ret) | |
226 | goto out; | |
1c6fdbd8 | 227 | |
3d3d23b3 KO |
228 | bch2_journal_replay_to_text(&buf, c, dup); |
229 | ||
230 | fsck_err_on(same_device, | |
231 | c, journal_entry_dup_same_device, | |
1ece5323 | 232 | "duplicate journal entry on same device\n%s", |
3d3d23b3 KO |
233 | buf.buf); |
234 | ||
235 | fsck_err_on(not_identical, | |
236 | c, journal_entry_replicas_data_mismatch, | |
1ece5323 | 237 | "found duplicate but non identical journal entries\n%s", |
3d3d23b3 KO |
238 | buf.buf); |
239 | ||
240 | if (entry_ptr.csum_good && !identical) | |
17fe3b64 KO |
241 | goto replace; |
242 | ||
3d3d23b3 | 243 | goto out; |
17fe3b64 KO |
244 | } |
245 | replace: | |
cb6fc943 | 246 | i = kvmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL); |
ec7ccbde | 247 | if (!i) |
09b9c72b | 248 | return bch_err_throw(c, ENOMEM_journal_entry_add); |
1c6fdbd8 | 249 | |
a555bcf4 | 250 | darray_init(&i->ptrs); |
2cce3752 KO |
251 | i->csum_good = entry_ptr.csum_good; |
252 | i->ignore_blacklisted = false; | |
253 | i->ignore_not_dirty = false; | |
1c6fdbd8 | 254 | unsafe_memcpy(&i->j, j, bytes, "embedded variable length struct"); |
e4c3f386 KO |
255 | |
256 | if (dup) { | |
17fe3b64 | 257 | /* The first ptr should represent the jset we kept: */ |
a555bcf4 KO |
258 | darray_for_each(dup->ptrs, ptr) |
259 | darray_push(&i->ptrs, *ptr); | |
ce6201c4 | 260 | __journal_replay_free(c, dup); |
3d3d23b3 KO |
261 | } else { |
262 | darray_push(&i->ptrs, entry_ptr); | |
e4c3f386 KO |
263 | } |
264 | ||
ce6201c4 | 265 | *_i = i; |
1c6fdbd8 KO |
266 | out: |
267 | fsck_err: | |
3d3d23b3 | 268 | printbuf_exit(&buf); |
1c6fdbd8 KO |
269 | return ret; |
270 | } | |
271 | ||
1c6fdbd8 KO |
272 | /* this fills in a range with empty jset_entries: */ |
273 | static void journal_entry_null_range(void *start, void *end) | |
274 | { | |
275 | struct jset_entry *entry; | |
276 | ||
277 | for (entry = start; entry != end; entry = vstruct_next(entry)) | |
278 | memset(entry, 0, sizeof(*entry)); | |
279 | } | |
280 | ||
281 | #define JOURNAL_ENTRY_REREAD 5 | |
282 | #define JOURNAL_ENTRY_NONE 6 | |
283 | #define JOURNAL_ENTRY_BAD 7 | |
284 | ||
c23a9e08 | 285 | static void journal_entry_err_msg(struct printbuf *out, |
a8712967 | 286 | u32 version, |
c23a9e08 KO |
287 | struct jset *jset, |
288 | struct jset_entry *entry) | |
289 | { | |
a8712967 KO |
290 | prt_str(out, "invalid journal entry, version="); |
291 | bch2_version_to_text(out, version); | |
292 | ||
293 | if (entry) { | |
294 | prt_str(out, " type="); | |
9abb6dd7 | 295 | bch2_prt_jset_entry_type(out, entry->type); |
a8712967 KO |
296 | } |
297 | ||
298 | if (!jset) { | |
299 | prt_printf(out, " in superblock"); | |
300 | } else { | |
301 | ||
302 | prt_printf(out, " seq=%llu", le64_to_cpu(jset->seq)); | |
303 | ||
304 | if (entry) | |
305 | prt_printf(out, " offset=%zi/%u", | |
306 | (u64 *) entry - jset->_data, | |
307 | le32_to_cpu(jset->u64s)); | |
308 | } | |
309 | ||
c23a9e08 KO |
310 | prt_str(out, ": "); |
311 | } | |
312 | ||
b65db750 | 313 | #define journal_entry_err(c, version, jset, entry, _err, msg, ...) \ |
1c6fdbd8 | 314 | ({ \ |
96dea3d5 | 315 | struct printbuf _buf = PRINTBUF; \ |
c23a9e08 | 316 | \ |
96dea3d5 KO |
317 | journal_entry_err_msg(&_buf, version, jset, entry); \ |
318 | prt_printf(&_buf, msg, ##__VA_ARGS__); \ | |
c23a9e08 | 319 | \ |
60558d55 | 320 | switch (from.flags & BCH_VALIDATE_write) { \ |
1c6fdbd8 | 321 | case READ: \ |
b65db750 | 322 | mustfix_fsck_err(c, _err, "%s", _buf.buf); \ |
1c6fdbd8 KO |
323 | break; \ |
324 | case WRITE: \ | |
b65db750 | 325 | bch2_sb_error_count(c, BCH_FSCK_ERR_##_err); \ |
6b1e0b9e KO |
326 | if (bch2_fs_inconsistent(c, \ |
327 | "corrupt metadata before write: %s\n", _buf.buf)) {\ | |
09b9c72b | 328 | ret = bch_err_throw(c, fsck_errors_not_fixed); \ |
1c6fdbd8 KO |
329 | goto fsck_err; \ |
330 | } \ | |
331 | break; \ | |
332 | } \ | |
c23a9e08 | 333 | \ |
96dea3d5 | 334 | printbuf_exit(&_buf); \ |
1c6fdbd8 KO |
335 | true; \ |
336 | }) | |
337 | ||
b65db750 KO |
338 | #define journal_entry_err_on(cond, ...) \ |
339 | ((cond) ? journal_entry_err(__VA_ARGS__) : false) | |
1c6fdbd8 | 340 | |
4d54337c KO |
341 | #define FSCK_DELETED_KEY 5 |
342 | ||
c23a9e08 KO |
343 | static int journal_validate_key(struct bch_fs *c, |
344 | struct jset *jset, | |
1c6fdbd8 | 345 | struct jset_entry *entry, |
cb685ce7 | 346 | struct bkey_i *k, |
a6f4794f KO |
347 | struct bkey_validate_context from, |
348 | unsigned version, int big_endian) | |
1c6fdbd8 | 349 | { |
a6f4794f | 350 | enum bch_validate_flags flags = from.flags; |
65eaf4e2 | 351 | int write = flags & BCH_VALIDATE_write; |
1c6fdbd8 | 352 | void *next = vstruct_next(entry); |
1c6fdbd8 KO |
353 | int ret = 0; |
354 | ||
b65db750 KO |
355 | if (journal_entry_err_on(!k->k.u64s, |
356 | c, version, jset, entry, | |
357 | journal_entry_bkey_u64s_0, | |
358 | "k->u64s 0")) { | |
1c6fdbd8 KO |
359 | entry->u64s = cpu_to_le16((u64 *) k - entry->_data); |
360 | journal_entry_null_range(vstruct_next(entry), next); | |
4d54337c | 361 | return FSCK_DELETED_KEY; |
1c6fdbd8 KO |
362 | } |
363 | ||
364 | if (journal_entry_err_on((void *) bkey_next(k) > | |
c23a9e08 | 365 | (void *) vstruct_next(entry), |
a8712967 | 366 | c, version, jset, entry, |
b65db750 | 367 | journal_entry_bkey_past_end, |
c23a9e08 | 368 | "extends past end of journal entry")) { |
1c6fdbd8 KO |
369 | entry->u64s = cpu_to_le16((u64 *) k - entry->_data); |
370 | journal_entry_null_range(vstruct_next(entry), next); | |
4d54337c | 371 | return FSCK_DELETED_KEY; |
1c6fdbd8 KO |
372 | } |
373 | ||
c23a9e08 | 374 | if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT, |
a8712967 | 375 | c, version, jset, entry, |
b65db750 | 376 | journal_entry_bkey_bad_format, |
c23a9e08 | 377 | "bad format %u", k->k.format)) { |
4d54337c | 378 | le16_add_cpu(&entry->u64s, -((u16) k->k.u64s)); |
1c6fdbd8 KO |
379 | memmove(k, bkey_next(k), next - (void *) bkey_next(k)); |
380 | journal_entry_null_range(vstruct_next(entry), next); | |
4d54337c | 381 | return FSCK_DELETED_KEY; |
1c6fdbd8 KO |
382 | } |
383 | ||
39fb2983 | 384 | if (!write) |
a6f4794f | 385 | bch2_bkey_compat(from.level, from.btree, version, big_endian, |
7d6f07ed | 386 | write, NULL, bkey_to_packed(k)); |
26609b61 | 387 | |
a6f4794f | 388 | ret = bch2_bkey_validate(c, bkey_i_to_s_c(k), from); |
d97de0d0 | 389 | if (ret == -BCH_ERR_fsck_delete_bkey) { |
4d54337c | 390 | le16_add_cpu(&entry->u64s, -((u16) k->k.u64s)); |
1c6fdbd8 KO |
391 | memmove(k, bkey_next(k), next - (void *) bkey_next(k)); |
392 | journal_entry_null_range(vstruct_next(entry), next); | |
4d54337c | 393 | return FSCK_DELETED_KEY; |
1c6fdbd8 | 394 | } |
d97de0d0 KO |
395 | if (ret) |
396 | goto fsck_err; | |
26609b61 | 397 | |
39fb2983 | 398 | if (write) |
a6f4794f | 399 | bch2_bkey_compat(from.level, from.btree, version, big_endian, |
7d6f07ed | 400 | write, NULL, bkey_to_packed(k)); |
1c6fdbd8 KO |
401 | fsck_err: |
402 | return ret; | |
403 | } | |
404 | ||
528b18e6 | 405 | static int journal_entry_btree_keys_validate(struct bch_fs *c, |
c4e382e2 KO |
406 | struct jset *jset, |
407 | struct jset_entry *entry, | |
408 | unsigned version, int big_endian, | |
60558d55 | 409 | struct bkey_validate_context from) |
1c6fdbd8 | 410 | { |
4d54337c | 411 | struct bkey_i *k = entry->start; |
60558d55 KO |
412 | |
413 | from.level = entry->level; | |
414 | from.btree = entry->btree_id; | |
1c6fdbd8 | 415 | |
4d54337c | 416 | while (k != vstruct_last(entry)) { |
a6f4794f | 417 | int ret = journal_validate_key(c, jset, entry, k, from, version, big_endian); |
4d54337c KO |
418 | if (ret == FSCK_DELETED_KEY) |
419 | continue; | |
0f6f8f76 KO |
420 | else if (ret) |
421 | return ret; | |
4d54337c KO |
422 | |
423 | k = bkey_next(k); | |
1c6fdbd8 KO |
424 | } |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
528b18e6 KO |
429 | static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c, |
430 | struct jset_entry *entry) | |
431 | { | |
e7bc7cdf | 432 | bool first = true; |
528b18e6 | 433 | |
ac2ccddc | 434 | jset_entry_for_each_key(entry, k) { |
d6efd42a KO |
435 | /* We may be called on entries that haven't been validated: */ |
436 | if (!k->k.u64s) | |
437 | break; | |
438 | ||
e7bc7cdf | 439 | if (!first) { |
401ec4db | 440 | prt_newline(out); |
9abb6dd7 KO |
441 | bch2_prt_jset_entry_type(out, entry->type); |
442 | prt_str(out, ": "); | |
e7bc7cdf | 443 | } |
db514cf6 KO |
444 | bch2_btree_id_level_to_text(out, entry->btree_id, entry->level); |
445 | prt_char(out, ' '); | |
528b18e6 | 446 | bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k)); |
e7bc7cdf KO |
447 | first = false; |
448 | } | |
528b18e6 KO |
449 | } |
450 | ||
451 | static int journal_entry_btree_root_validate(struct bch_fs *c, | |
c4e382e2 KO |
452 | struct jset *jset, |
453 | struct jset_entry *entry, | |
454 | unsigned version, int big_endian, | |
60558d55 | 455 | struct bkey_validate_context from) |
1c6fdbd8 KO |
456 | { |
457 | struct bkey_i *k = entry->start; | |
458 | int ret = 0; | |
459 | ||
60558d55 KO |
460 | from.root = true; |
461 | from.level = entry->level + 1; | |
462 | from.btree = entry->btree_id; | |
463 | ||
1c6fdbd8 | 464 | if (journal_entry_err_on(!entry->u64s || |
c23a9e08 | 465 | le16_to_cpu(entry->u64s) != k->k.u64s, |
a8712967 | 466 | c, version, jset, entry, |
b65db750 | 467 | journal_entry_btree_root_bad_size, |
1c6fdbd8 KO |
468 | "invalid btree root journal entry: wrong number of keys")) { |
469 | void *next = vstruct_next(entry); | |
470 | /* | |
471 | * we don't want to null out this jset_entry, | |
472 | * just the contents, so that later we can tell | |
473 | * we were _supposed_ to have a btree root | |
474 | */ | |
475 | entry->u64s = 0; | |
476 | journal_entry_null_range(vstruct_next(entry), next); | |
477 | return 0; | |
478 | } | |
479 | ||
a6f4794f | 480 | ret = journal_validate_key(c, jset, entry, k, from, version, big_endian); |
c8296d73 KO |
481 | if (ret == FSCK_DELETED_KEY) |
482 | ret = 0; | |
1c6fdbd8 KO |
483 | fsck_err: |
484 | return ret; | |
485 | } | |
486 | ||
528b18e6 KO |
487 | static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c, |
488 | struct jset_entry *entry) | |
489 | { | |
490 | journal_entry_btree_keys_to_text(out, c, entry); | |
491 | } | |
492 | ||
493 | static int journal_entry_prio_ptrs_validate(struct bch_fs *c, | |
c4e382e2 KO |
494 | struct jset *jset, |
495 | struct jset_entry *entry, | |
496 | unsigned version, int big_endian, | |
60558d55 | 497 | struct bkey_validate_context from) |
1c6fdbd8 KO |
498 | { |
499 | /* obsolete, don't care: */ | |
500 | return 0; | |
501 | } | |
502 | ||
528b18e6 KO |
503 | static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c, |
504 | struct jset_entry *entry) | |
505 | { | |
506 | } | |
507 | ||
508 | static int journal_entry_blacklist_validate(struct bch_fs *c, | |
c4e382e2 KO |
509 | struct jset *jset, |
510 | struct jset_entry *entry, | |
511 | unsigned version, int big_endian, | |
60558d55 | 512 | struct bkey_validate_context from) |
1c6fdbd8 KO |
513 | { |
514 | int ret = 0; | |
515 | ||
c23a9e08 | 516 | if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1, |
a8712967 | 517 | c, version, jset, entry, |
b65db750 | 518 | journal_entry_blacklist_bad_size, |
1c6fdbd8 KO |
519 | "invalid journal seq blacklist entry: bad size")) { |
520 | journal_entry_null_range(entry, vstruct_next(entry)); | |
521 | } | |
522 | fsck_err: | |
523 | return ret; | |
524 | } | |
525 | ||
528b18e6 KO |
526 | static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c, |
527 | struct jset_entry *entry) | |
528 | { | |
529 | struct jset_entry_blacklist *bl = | |
530 | container_of(entry, struct jset_entry_blacklist, entry); | |
531 | ||
401ec4db | 532 | prt_printf(out, "seq=%llu", le64_to_cpu(bl->seq)); |
528b18e6 KO |
533 | } |
534 | ||
535 | static int journal_entry_blacklist_v2_validate(struct bch_fs *c, | |
c4e382e2 KO |
536 | struct jset *jset, |
537 | struct jset_entry *entry, | |
538 | unsigned version, int big_endian, | |
60558d55 | 539 | struct bkey_validate_context from) |
1c6fdbd8 KO |
540 | { |
541 | struct jset_entry_blacklist_v2 *bl_entry; | |
542 | int ret = 0; | |
543 | ||
c23a9e08 | 544 | if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2, |
a8712967 | 545 | c, version, jset, entry, |
b65db750 | 546 | journal_entry_blacklist_v2_bad_size, |
1c6fdbd8 KO |
547 | "invalid journal seq blacklist entry: bad size")) { |
548 | journal_entry_null_range(entry, vstruct_next(entry)); | |
2c5af169 | 549 | goto out; |
1c6fdbd8 KO |
550 | } |
551 | ||
552 | bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry); | |
553 | ||
554 | if (journal_entry_err_on(le64_to_cpu(bl_entry->start) > | |
c23a9e08 | 555 | le64_to_cpu(bl_entry->end), |
a8712967 | 556 | c, version, jset, entry, |
b65db750 | 557 | journal_entry_blacklist_v2_start_past_end, |
1c6fdbd8 KO |
558 | "invalid journal seq blacklist entry: start > end")) { |
559 | journal_entry_null_range(entry, vstruct_next(entry)); | |
560 | } | |
2c5af169 KO |
561 | out: |
562 | fsck_err: | |
563 | return ret; | |
564 | } | |
565 | ||
528b18e6 KO |
566 | static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c, |
567 | struct jset_entry *entry) | |
568 | { | |
569 | struct jset_entry_blacklist_v2 *bl = | |
570 | container_of(entry, struct jset_entry_blacklist_v2, entry); | |
571 | ||
401ec4db | 572 | prt_printf(out, "start=%llu end=%llu", |
528b18e6 KO |
573 | le64_to_cpu(bl->start), |
574 | le64_to_cpu(bl->end)); | |
575 | } | |
576 | ||
577 | static int journal_entry_usage_validate(struct bch_fs *c, | |
c4e382e2 KO |
578 | struct jset *jset, |
579 | struct jset_entry *entry, | |
580 | unsigned version, int big_endian, | |
60558d55 | 581 | struct bkey_validate_context from) |
2c5af169 KO |
582 | { |
583 | struct jset_entry_usage *u = | |
584 | container_of(entry, struct jset_entry_usage, entry); | |
585 | unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64); | |
586 | int ret = 0; | |
587 | ||
3577df5f | 588 | if (journal_entry_err_on(bytes < sizeof(*u), |
a8712967 | 589 | c, version, jset, entry, |
b65db750 | 590 | journal_entry_usage_bad_size, |
3577df5f KO |
591 | "invalid journal entry usage: bad size")) { |
592 | journal_entry_null_range(entry, vstruct_next(entry)); | |
593 | return ret; | |
594 | } | |
595 | ||
596 | fsck_err: | |
597 | return ret; | |
598 | } | |
599 | ||
528b18e6 KO |
600 | static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c, |
601 | struct jset_entry *entry) | |
602 | { | |
603 | struct jset_entry_usage *u = | |
604 | container_of(entry, struct jset_entry_usage, entry); | |
605 | ||
9abb6dd7 KO |
606 | prt_str(out, "type="); |
607 | bch2_prt_fs_usage_type(out, u->entry.btree_id); | |
608 | prt_printf(out, " v=%llu", le64_to_cpu(u->v)); | |
528b18e6 KO |
609 | } |
610 | ||
611 | static int journal_entry_data_usage_validate(struct bch_fs *c, | |
c4e382e2 KO |
612 | struct jset *jset, |
613 | struct jset_entry *entry, | |
614 | unsigned version, int big_endian, | |
60558d55 | 615 | struct bkey_validate_context from) |
3577df5f KO |
616 | { |
617 | struct jset_entry_data_usage *u = | |
618 | container_of(entry, struct jset_entry_data_usage, entry); | |
619 | unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64); | |
d5bd3787 | 620 | struct printbuf err = PRINTBUF; |
3577df5f KO |
621 | int ret = 0; |
622 | ||
2c5af169 KO |
623 | if (journal_entry_err_on(bytes < sizeof(*u) || |
624 | bytes < sizeof(*u) + u->r.nr_devs, | |
a8712967 | 625 | c, version, jset, entry, |
b65db750 | 626 | journal_entry_data_usage_bad_size, |
2c5af169 KO |
627 | "invalid journal entry usage: bad size")) { |
628 | journal_entry_null_range(entry, vstruct_next(entry)); | |
d5bd3787 | 629 | goto out; |
2c5af169 | 630 | } |
1c6fdbd8 | 631 | |
49fd90b2 | 632 | if (journal_entry_err_on(bch2_replicas_entry_validate(&u->r, c, &err), |
d5bd3787 KO |
633 | c, version, jset, entry, |
634 | journal_entry_data_usage_bad_size, | |
635 | "invalid journal entry usage: %s", err.buf)) { | |
636 | journal_entry_null_range(entry, vstruct_next(entry)); | |
637 | goto out; | |
638 | } | |
639 | out: | |
1c6fdbd8 | 640 | fsck_err: |
d5bd3787 | 641 | printbuf_exit(&err); |
1c6fdbd8 KO |
642 | return ret; |
643 | } | |
644 | ||
528b18e6 KO |
645 | static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c, |
646 | struct jset_entry *entry) | |
647 | { | |
648 | struct jset_entry_data_usage *u = | |
649 | container_of(entry, struct jset_entry_data_usage, entry); | |
650 | ||
651 | bch2_replicas_entry_to_text(out, &u->r); | |
401ec4db | 652 | prt_printf(out, "=%llu", le64_to_cpu(u->v)); |
528b18e6 KO |
653 | } |
654 | ||
655 | static int journal_entry_clock_validate(struct bch_fs *c, | |
c4e382e2 KO |
656 | struct jset *jset, |
657 | struct jset_entry *entry, | |
658 | unsigned version, int big_endian, | |
60558d55 | 659 | struct bkey_validate_context from) |
2abe5420 KO |
660 | { |
661 | struct jset_entry_clock *clock = | |
662 | container_of(entry, struct jset_entry_clock, entry); | |
663 | unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64); | |
664 | int ret = 0; | |
665 | ||
666 | if (journal_entry_err_on(bytes != sizeof(*clock), | |
b65db750 KO |
667 | c, version, jset, entry, |
668 | journal_entry_clock_bad_size, | |
669 | "bad size")) { | |
2abe5420 KO |
670 | journal_entry_null_range(entry, vstruct_next(entry)); |
671 | return ret; | |
672 | } | |
673 | ||
674 | if (journal_entry_err_on(clock->rw > 1, | |
b65db750 KO |
675 | c, version, jset, entry, |
676 | journal_entry_clock_bad_rw, | |
677 | "bad rw")) { | |
2abe5420 KO |
678 | journal_entry_null_range(entry, vstruct_next(entry)); |
679 | return ret; | |
680 | } | |
681 | ||
682 | fsck_err: | |
683 | return ret; | |
684 | } | |
685 | ||
528b18e6 KO |
686 | static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c, |
687 | struct jset_entry *entry) | |
688 | { | |
689 | struct jset_entry_clock *clock = | |
690 | container_of(entry, struct jset_entry_clock, entry); | |
691 | ||
de902e3b | 692 | prt_printf(out, "%s=%llu", str_write_read(clock->rw), le64_to_cpu(clock->time)); |
528b18e6 KO |
693 | } |
694 | ||
695 | static int journal_entry_dev_usage_validate(struct bch_fs *c, | |
c4e382e2 KO |
696 | struct jset *jset, |
697 | struct jset_entry *entry, | |
698 | unsigned version, int big_endian, | |
60558d55 | 699 | struct bkey_validate_context from) |
180fb49d KO |
700 | { |
701 | struct jset_entry_dev_usage *u = | |
702 | container_of(entry, struct jset_entry_dev_usage, entry); | |
703 | unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64); | |
45c2e33f | 704 | unsigned expected = sizeof(*u); |
180fb49d KO |
705 | int ret = 0; |
706 | ||
707 | if (journal_entry_err_on(bytes < expected, | |
b65db750 KO |
708 | c, version, jset, entry, |
709 | journal_entry_dev_usage_bad_size, | |
710 | "bad size (%u < %u)", | |
180fb49d KO |
711 | bytes, expected)) { |
712 | journal_entry_null_range(entry, vstruct_next(entry)); | |
713 | return ret; | |
714 | } | |
715 | ||
180fb49d | 716 | if (journal_entry_err_on(u->pad, |
b65db750 KO |
717 | c, version, jset, entry, |
718 | journal_entry_dev_usage_bad_pad, | |
719 | "bad pad")) { | |
180fb49d KO |
720 | journal_entry_null_range(entry, vstruct_next(entry)); |
721 | return ret; | |
722 | } | |
723 | ||
724 | fsck_err: | |
725 | return ret; | |
726 | } | |
727 | ||
528b18e6 KO |
728 | static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c, |
729 | struct jset_entry *entry) | |
730 | { | |
731 | struct jset_entry_dev_usage *u = | |
732 | container_of(entry, struct jset_entry_dev_usage, entry); | |
733 | unsigned i, nr_types = jset_entry_dev_usage_nr_types(u); | |
734 | ||
657d4282 KO |
735 | if (vstruct_bytes(entry) < sizeof(*u)) |
736 | return; | |
737 | ||
401ec4db | 738 | prt_printf(out, "dev=%u", le32_to_cpu(u->dev)); |
528b18e6 | 739 | |
12e7ff1a | 740 | printbuf_indent_add(out, 2); |
528b18e6 | 741 | for (i = 0; i < nr_types; i++) { |
12e7ff1a | 742 | prt_newline(out); |
e58f963c | 743 | bch2_prt_data_type(out, i); |
401ec4db | 744 | prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu", |
528b18e6 KO |
745 | le64_to_cpu(u->d[i].buckets), |
746 | le64_to_cpu(u->d[i].sectors), | |
747 | le64_to_cpu(u->d[i].fragmented)); | |
748 | } | |
12e7ff1a | 749 | printbuf_indent_sub(out, 2); |
528b18e6 KO |
750 | } |
751 | ||
752 | static int journal_entry_log_validate(struct bch_fs *c, | |
c4e382e2 KO |
753 | struct jset *jset, |
754 | struct jset_entry *entry, | |
755 | unsigned version, int big_endian, | |
60558d55 | 756 | struct bkey_validate_context from) |
fb64f3fd KO |
757 | { |
758 | return 0; | |
759 | } | |
760 | ||
528b18e6 KO |
761 | static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c, |
762 | struct jset_entry *entry) | |
763 | { | |
764 | struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry); | |
528b18e6 | 765 | |
106480e9 | 766 | prt_printf(out, "%.*s", jset_entry_log_msg_bytes(l), l->d); |
528b18e6 KO |
767 | } |
768 | ||
c23a9e08 | 769 | static int journal_entry_overwrite_validate(struct bch_fs *c, |
c4e382e2 KO |
770 | struct jset *jset, |
771 | struct jset_entry *entry, | |
772 | unsigned version, int big_endian, | |
60558d55 | 773 | struct bkey_validate_context from) |
cb685ce7 | 774 | { |
60558d55 | 775 | from.flags = 0; |
dbe17f18 | 776 | return journal_entry_btree_keys_validate(c, jset, entry, |
60558d55 | 777 | version, big_endian, from); |
cb685ce7 KO |
778 | } |
779 | ||
780 | static void journal_entry_overwrite_to_text(struct printbuf *out, struct bch_fs *c, | |
781 | struct jset_entry *entry) | |
782 | { | |
783 | journal_entry_btree_keys_to_text(out, c, entry); | |
784 | } | |
785 | ||
edaed8ee KO |
786 | static int journal_entry_log_bkey_validate(struct bch_fs *c, |
787 | struct jset *jset, | |
788 | struct jset_entry *entry, | |
789 | unsigned version, int big_endian, | |
790 | struct bkey_validate_context from) | |
791 | { | |
792 | from.flags = 0; | |
793 | return journal_entry_btree_keys_validate(c, jset, entry, | |
794 | version, big_endian, from); | |
795 | } | |
796 | ||
797 | static void journal_entry_log_bkey_to_text(struct printbuf *out, struct bch_fs *c, | |
798 | struct jset_entry *entry) | |
799 | { | |
800 | journal_entry_btree_keys_to_text(out, c, entry); | |
801 | } | |
802 | ||
09caeabe KO |
803 | static int journal_entry_write_buffer_keys_validate(struct bch_fs *c, |
804 | struct jset *jset, | |
805 | struct jset_entry *entry, | |
806 | unsigned version, int big_endian, | |
60558d55 | 807 | struct bkey_validate_context from) |
09caeabe KO |
808 | { |
809 | return journal_entry_btree_keys_validate(c, jset, entry, | |
60558d55 | 810 | version, big_endian, from); |
09caeabe KO |
811 | } |
812 | ||
813 | static void journal_entry_write_buffer_keys_to_text(struct printbuf *out, struct bch_fs *c, | |
814 | struct jset_entry *entry) | |
815 | { | |
816 | journal_entry_btree_keys_to_text(out, c, entry); | |
817 | } | |
818 | ||
52f7d75e KO |
819 | static int journal_entry_datetime_validate(struct bch_fs *c, |
820 | struct jset *jset, | |
821 | struct jset_entry *entry, | |
822 | unsigned version, int big_endian, | |
60558d55 | 823 | struct bkey_validate_context from) |
52f7d75e KO |
824 | { |
825 | unsigned bytes = vstruct_bytes(entry); | |
826 | unsigned expected = 16; | |
827 | int ret = 0; | |
828 | ||
829 | if (journal_entry_err_on(vstruct_bytes(entry) < expected, | |
830 | c, version, jset, entry, | |
831 | journal_entry_dev_usage_bad_size, | |
832 | "bad size (%u < %u)", | |
833 | bytes, expected)) { | |
834 | journal_entry_null_range(entry, vstruct_next(entry)); | |
835 | return ret; | |
836 | } | |
837 | fsck_err: | |
838 | return ret; | |
839 | } | |
840 | ||
841 | static void journal_entry_datetime_to_text(struct printbuf *out, struct bch_fs *c, | |
842 | struct jset_entry *entry) | |
843 | { | |
844 | struct jset_entry_datetime *datetime = | |
845 | container_of(entry, struct jset_entry_datetime, entry); | |
846 | ||
847 | bch2_prt_datetime(out, le64_to_cpu(datetime->seconds)); | |
848 | } | |
849 | ||
1c6fdbd8 | 850 | struct jset_entry_ops { |
c23a9e08 | 851 | int (*validate)(struct bch_fs *, struct jset *, |
c4e382e2 | 852 | struct jset_entry *, unsigned, int, |
60558d55 | 853 | struct bkey_validate_context); |
528b18e6 | 854 | void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *); |
1c6fdbd8 KO |
855 | }; |
856 | ||
857 | static const struct jset_entry_ops bch2_jset_entry_ops[] = { | |
858 | #define x(f, nr) \ | |
859 | [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \ | |
528b18e6 KO |
860 | .validate = journal_entry_##f##_validate, \ |
861 | .to_text = journal_entry_##f##_to_text, \ | |
1c6fdbd8 KO |
862 | }, |
863 | BCH_JSET_ENTRY_TYPES() | |
864 | #undef x | |
865 | }; | |
866 | ||
c23a9e08 KO |
867 | int bch2_journal_entry_validate(struct bch_fs *c, |
868 | struct jset *jset, | |
7d6f07ed | 869 | struct jset_entry *entry, |
c4e382e2 | 870 | unsigned version, int big_endian, |
60558d55 | 871 | struct bkey_validate_context from) |
1c6fdbd8 | 872 | { |
2c5af169 | 873 | return entry->type < BCH_JSET_ENTRY_NR |
c23a9e08 | 874 | ? bch2_jset_entry_ops[entry->type].validate(c, jset, entry, |
60558d55 | 875 | version, big_endian, from) |
2c5af169 | 876 | : 0; |
1c6fdbd8 KO |
877 | } |
878 | ||
528b18e6 KO |
879 | void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c, |
880 | struct jset_entry *entry) | |
881 | { | |
9abb6dd7 KO |
882 | bch2_prt_jset_entry_type(out, entry->type); |
883 | ||
528b18e6 | 884 | if (entry->type < BCH_JSET_ENTRY_NR) { |
9abb6dd7 | 885 | prt_str(out, ": "); |
528b18e6 | 886 | bch2_jset_entry_ops[entry->type].to_text(out, c, entry); |
528b18e6 KO |
887 | } |
888 | } | |
889 | ||
1c6fdbd8 | 890 | static int jset_validate_entries(struct bch_fs *c, struct jset *jset, |
65eaf4e2 | 891 | enum bch_validate_flags flags) |
1c6fdbd8 | 892 | { |
60558d55 KO |
893 | struct bkey_validate_context from = { |
894 | .flags = flags, | |
895 | .from = BKEY_VALIDATE_journal, | |
896 | .journal_seq = le64_to_cpu(jset->seq), | |
897 | }; | |
898 | ||
a8712967 | 899 | unsigned version = le32_to_cpu(jset->version); |
1c6fdbd8 KO |
900 | int ret = 0; |
901 | ||
902 | vstruct_for_each(jset, entry) { | |
60558d55 KO |
903 | from.journal_offset = (u64 *) entry - jset->_data; |
904 | ||
a8712967 | 905 | if (journal_entry_err_on(vstruct_next(entry) > vstruct_last(jset), |
b65db750 KO |
906 | c, version, jset, entry, |
907 | journal_entry_past_jset_end, | |
1c6fdbd8 KO |
908 | "journal entry extends past end of jset")) { |
909 | jset->u64s = cpu_to_le32((u64 *) entry - jset->_data); | |
910 | break; | |
911 | } | |
912 | ||
60558d55 KO |
913 | ret = bch2_journal_entry_validate(c, jset, entry, version, |
914 | JSET_BIG_ENDIAN(jset), from); | |
1c6fdbd8 KO |
915 | if (ret) |
916 | break; | |
917 | } | |
918 | fsck_err: | |
919 | return ret; | |
920 | } | |
921 | ||
922 | static int jset_validate(struct bch_fs *c, | |
ca73852a | 923 | struct bch_dev *ca, |
1c6fdbd8 | 924 | struct jset *jset, u64 sector, |
65eaf4e2 | 925 | enum bch_validate_flags flags) |
1c6fdbd8 | 926 | { |
60558d55 KO |
927 | struct bkey_validate_context from = { |
928 | .flags = flags, | |
929 | .from = BKEY_VALIDATE_journal, | |
930 | .journal_seq = le64_to_cpu(jset->seq), | |
931 | }; | |
1c6fdbd8 KO |
932 | int ret = 0; |
933 | ||
934 | if (le64_to_cpu(jset->magic) != jset_magic(c)) | |
935 | return JOURNAL_ENTRY_NONE; | |
936 | ||
60558d55 | 937 | unsigned version = le32_to_cpu(jset->version); |
a8712967 KO |
938 | if (journal_entry_err_on(!bch2_version_compatible(version), |
939 | c, version, jset, NULL, | |
b65db750 | 940 | jset_unsupported_version, |
ba8eeae8 | 941 | "%s sector %llu seq %llu: incompatible journal entry version %u.%u", |
ed9d58a2 | 942 | ca ? ca->name : c->name, |
ba8eeae8 KO |
943 | sector, le64_to_cpu(jset->seq), |
944 | BCH_VERSION_MAJOR(version), | |
945 | BCH_VERSION_MINOR(version))) { | |
35ef6df5 | 946 | /* don't try to continue: */ |
d1b2c864 | 947 | return -EINVAL; |
1c6fdbd8 KO |
948 | } |
949 | ||
c23a9e08 | 950 | if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)), |
b65db750 KO |
951 | c, version, jset, NULL, |
952 | jset_unknown_csum, | |
ca73852a | 953 | "%s sector %llu seq %llu: journal entry with unknown csum type %llu", |
ed9d58a2 KO |
954 | ca ? ca->name : c->name, |
955 | sector, le64_to_cpu(jset->seq), | |
d1b2c864 | 956 | JSET_CSUM_TYPE(jset))) |
35ef6df5 | 957 | ret = JOURNAL_ENTRY_BAD; |
1c6fdbd8 | 958 | |
ed9d58a2 KO |
959 | /* last_seq is ignored when JSET_NO_FLUSH is true */ |
960 | if (journal_entry_err_on(!JSET_NO_FLUSH(jset) && | |
c23a9e08 | 961 | le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq), |
a8712967 | 962 | c, version, jset, NULL, |
b65db750 | 963 | jset_last_seq_newer_than_seq, |
ed9d58a2 KO |
964 | "invalid journal entry: last_seq > seq (%llu > %llu)", |
965 | le64_to_cpu(jset->last_seq), | |
966 | le64_to_cpu(jset->seq))) { | |
1c6fdbd8 | 967 | jset->last_seq = jset->seq; |
ca73852a KO |
968 | return JOURNAL_ENTRY_BAD; |
969 | } | |
d1b2c864 | 970 | |
c4e382e2 | 971 | ret = jset_validate_entries(c, jset, flags); |
1c6fdbd8 KO |
972 | fsck_err: |
973 | return ret; | |
974 | } | |
975 | ||
d1b2c864 KO |
976 | static int jset_validate_early(struct bch_fs *c, |
977 | struct bch_dev *ca, | |
978 | struct jset *jset, u64 sector, | |
979 | unsigned bucket_sectors_left, | |
980 | unsigned sectors_read) | |
ed9d58a2 | 981 | { |
60558d55 KO |
982 | struct bkey_validate_context from = { |
983 | .from = BKEY_VALIDATE_journal, | |
984 | .journal_seq = le64_to_cpu(jset->seq), | |
985 | }; | |
d1b2c864 KO |
986 | int ret = 0; |
987 | ||
988 | if (le64_to_cpu(jset->magic) != jset_magic(c)) | |
989 | return JOURNAL_ENTRY_NONE; | |
990 | ||
60558d55 | 991 | unsigned version = le32_to_cpu(jset->version); |
a8712967 | 992 | if (journal_entry_err_on(!bch2_version_compatible(version), |
b65db750 KO |
993 | c, version, jset, NULL, |
994 | jset_unsupported_version, | |
ba8eeae8 | 995 | "%s sector %llu seq %llu: unknown journal entry version %u.%u", |
d1b2c864 | 996 | ca ? ca->name : c->name, |
ba8eeae8 KO |
997 | sector, le64_to_cpu(jset->seq), |
998 | BCH_VERSION_MAJOR(version), | |
999 | BCH_VERSION_MINOR(version))) { | |
d1b2c864 KO |
1000 | /* don't try to continue: */ |
1001 | return -EINVAL; | |
1002 | } | |
1003 | ||
60558d55 | 1004 | size_t bytes = vstruct_bytes(jset); |
d1b2c864 KO |
1005 | if (bytes > (sectors_read << 9) && |
1006 | sectors_read < bucket_sectors_left) | |
1007 | return JOURNAL_ENTRY_REREAD; | |
ed9d58a2 | 1008 | |
d1b2c864 | 1009 | if (journal_entry_err_on(bytes > bucket_sectors_left << 9, |
b65db750 KO |
1010 | c, version, jset, NULL, |
1011 | jset_past_bucket_end, | |
d1b2c864 KO |
1012 | "%s sector %llu seq %llu: journal entry too big (%zu bytes)", |
1013 | ca ? ca->name : c->name, | |
1014 | sector, le64_to_cpu(jset->seq), bytes)) | |
1015 | le32_add_cpu(&jset->u64s, | |
1016 | -((bytes - (bucket_sectors_left << 9)) / 8)); | |
1017 | fsck_err: | |
1018 | return ret; | |
ed9d58a2 KO |
1019 | } |
1020 | ||
1c6fdbd8 KO |
1021 | struct journal_read_buf { |
1022 | void *data; | |
1023 | size_t size; | |
1024 | }; | |
1025 | ||
09b9c72b | 1026 | static int journal_read_buf_realloc(struct bch_fs *c, struct journal_read_buf *b, |
1c6fdbd8 KO |
1027 | size_t new_size) |
1028 | { | |
1029 | void *n; | |
1030 | ||
1031 | /* the bios are sized for this many pages, max: */ | |
1032 | if (new_size > JOURNAL_ENTRY_SIZE_MAX) | |
09b9c72b | 1033 | return bch_err_throw(c, ENOMEM_journal_read_buf_realloc); |
1c6fdbd8 KO |
1034 | |
1035 | new_size = roundup_pow_of_two(new_size); | |
cb6fc943 | 1036 | n = kvmalloc(new_size, GFP_KERNEL); |
1c6fdbd8 | 1037 | if (!n) |
09b9c72b | 1038 | return bch_err_throw(c, ENOMEM_journal_read_buf_realloc); |
1c6fdbd8 | 1039 | |
cb6fc943 | 1040 | kvfree(b->data); |
1c6fdbd8 KO |
1041 | b->data = n; |
1042 | b->size = new_size; | |
1043 | return 0; | |
1044 | } | |
1045 | ||
1046 | static int journal_read_bucket(struct bch_dev *ca, | |
1047 | struct journal_read_buf *buf, | |
1048 | struct journal_list *jlist, | |
a9ec3454 | 1049 | unsigned bucket) |
1c6fdbd8 KO |
1050 | { |
1051 | struct bch_fs *c = ca->fs; | |
1052 | struct journal_device *ja = &ca->journal; | |
1c6fdbd8 KO |
1053 | struct jset *j = NULL; |
1054 | unsigned sectors, sectors_read = 0; | |
1055 | u64 offset = bucket_to_sector(ca, ja->buckets[bucket]), | |
1056 | end = offset + ca->mi.bucket_size; | |
17fe3b64 | 1057 | bool saw_bad = false, csum_good; |
1c6fdbd8 KO |
1058 | int ret = 0; |
1059 | ||
1060 | pr_debug("reading %u", bucket); | |
1061 | ||
1062 | while (offset < end) { | |
1063 | if (!sectors_read) { | |
ac10a961 KO |
1064 | struct bio *bio; |
1065 | unsigned nr_bvecs; | |
1066 | reread: | |
1067 | sectors_read = min_t(unsigned, | |
1c6fdbd8 | 1068 | end - offset, buf->size >> 9); |
ac10a961 KO |
1069 | nr_bvecs = buf_pages(buf->data, sectors_read << 9); |
1070 | ||
1071 | bio = bio_kmalloc(nr_bvecs, GFP_KERNEL); | |
93d53f1c | 1072 | if (!bio) |
09b9c72b | 1073 | return bch_err_throw(c, ENOMEM_journal_read_bucket); |
ac10a961 | 1074 | bio_init(bio, ca->disk_sb.bdev, bio->bi_inline_vecs, nr_bvecs, REQ_OP_READ); |
1c6fdbd8 | 1075 | |
885678f6 KO |
1076 | bio->bi_iter.bi_sector = offset; |
1077 | bch2_bio_map(bio, buf->data, sectors_read << 9); | |
1c6fdbd8 | 1078 | |
b31c0704 | 1079 | u64 submit_time = local_clock(); |
1c6fdbd8 | 1080 | ret = submit_bio_wait(bio); |
ac10a961 | 1081 | kfree(bio); |
1c6fdbd8 | 1082 | |
b31c0704 | 1083 | if (!ret && bch2_meta_read_fault("journal")) |
09b9c72b | 1084 | ret = bch_err_throw(c, EIO_fault_injected); |
b31c0704 KO |
1085 | |
1086 | bch2_account_io_completion(ca, BCH_MEMBER_ERROR_read, | |
1087 | submit_time, !ret); | |
1088 | ||
1089 | if (ret) { | |
1090 | bch_err_dev_ratelimited(ca, | |
1091 | "journal read error: sector %llu", offset); | |
29d90f61 KO |
1092 | /* |
1093 | * We don't error out of the recovery process | |
1094 | * here, since the relevant journal entry may be | |
1095 | * found on a different device, and missing or | |
1096 | * no journal entries will be handled later | |
1097 | */ | |
cd04497b | 1098 | return 0; |
29d90f61 | 1099 | } |
1c6fdbd8 KO |
1100 | |
1101 | j = buf->data; | |
1102 | } | |
1103 | ||
d1b2c864 KO |
1104 | ret = jset_validate_early(c, ca, j, offset, |
1105 | end - offset, sectors_read); | |
1c6fdbd8 | 1106 | switch (ret) { |
1ed0a5d2 | 1107 | case 0: |
ca73852a | 1108 | sectors = vstruct_sectors(j, c->block_bits); |
1c6fdbd8 KO |
1109 | break; |
1110 | case JOURNAL_ENTRY_REREAD: | |
1111 | if (vstruct_bytes(j) > buf->size) { | |
09b9c72b | 1112 | ret = journal_read_buf_realloc(c, buf, |
1c6fdbd8 KO |
1113 | vstruct_bytes(j)); |
1114 | if (ret) | |
cd04497b | 1115 | return ret; |
1c6fdbd8 KO |
1116 | } |
1117 | goto reread; | |
1118 | case JOURNAL_ENTRY_NONE: | |
1119 | if (!saw_bad) | |
cd04497b | 1120 | return 0; |
ca73852a KO |
1121 | /* |
1122 | * On checksum error we don't really trust the size | |
1123 | * field of the journal entry we read, so try reading | |
1124 | * again at next block boundary: | |
1125 | */ | |
8244f320 | 1126 | sectors = block_sectors(c); |
d1b2c864 | 1127 | goto next_block; |
1c6fdbd8 | 1128 | default: |
cd04497b | 1129 | return ret; |
1c6fdbd8 KO |
1130 | } |
1131 | ||
c8bda9f2 KO |
1132 | if (le64_to_cpu(j->seq) > ja->highest_seq_found) { |
1133 | ja->highest_seq_found = le64_to_cpu(j->seq); | |
1134 | ja->cur_idx = bucket; | |
1135 | ja->sectors_free = ca->mi.bucket_size - | |
1136 | bucket_remainder(ca, offset) - sectors; | |
1137 | } | |
1138 | ||
1c6fdbd8 KO |
1139 | /* |
1140 | * This happens sometimes if we don't have discards on - | |
1141 | * when we've partially overwritten a bucket with new | |
1142 | * journal entries. We don't need the rest of the | |
1143 | * bucket: | |
1144 | */ | |
1145 | if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket]) | |
cd04497b | 1146 | return 0; |
1c6fdbd8 KO |
1147 | |
1148 | ja->bucket_seq[bucket] = le64_to_cpu(j->seq); | |
1149 | ||
4819b66e KO |
1150 | struct bch_csum csum; |
1151 | csum_good = jset_csum_good(c, j, &csum); | |
1152 | ||
b31c0704 KO |
1153 | bch2_account_io_completion(ca, BCH_MEMBER_ERROR_checksum, 0, csum_good); |
1154 | ||
1155 | if (!csum_good) { | |
cd04497b KO |
1156 | /* |
1157 | * Don't print an error here, we'll print the error | |
1158 | * later if we need this journal entry | |
1159 | */ | |
17fe3b64 | 1160 | saw_bad = true; |
b31c0704 | 1161 | } |
17fe3b64 | 1162 | |
d1b2c864 KO |
1163 | ret = bch2_encrypt(c, JSET_CSUM_TYPE(j), journal_nonce(j), |
1164 | j->encrypted_start, | |
1165 | vstruct_end(j) - (void *) j->encrypted_start); | |
3ed94062 | 1166 | bch2_fs_fatal_err_on(ret, c, "decrypting journal entry: %s", bch2_err_str(ret)); |
d1b2c864 | 1167 | |
1c6fdbd8 | 1168 | mutex_lock(&jlist->lock); |
72b7d633 | 1169 | ret = journal_entry_add(c, ca, (struct journal_ptr) { |
17fe3b64 | 1170 | .csum_good = csum_good, |
cd04497b | 1171 | .csum = csum, |
72b7d633 KO |
1172 | .dev = ca->dev_idx, |
1173 | .bucket = bucket, | |
1174 | .bucket_offset = offset - | |
1175 | bucket_to_sector(ca, ja->buckets[bucket]), | |
1176 | .sector = offset, | |
17fe3b64 | 1177 | }, jlist, j); |
1c6fdbd8 KO |
1178 | mutex_unlock(&jlist->lock); |
1179 | ||
1180 | switch (ret) { | |
1181 | case JOURNAL_ENTRY_ADD_OK: | |
1c6fdbd8 KO |
1182 | break; |
1183 | case JOURNAL_ENTRY_ADD_OUT_OF_RANGE: | |
1184 | break; | |
1185 | default: | |
cd04497b | 1186 | return ret; |
1c6fdbd8 | 1187 | } |
1c6fdbd8 KO |
1188 | next_block: |
1189 | pr_debug("next"); | |
1190 | offset += sectors; | |
1191 | sectors_read -= sectors; | |
1192 | j = ((void *) j) + (sectors << 9); | |
1193 | } | |
1194 | ||
cd04497b | 1195 | return 0; |
1c6fdbd8 KO |
1196 | } |
1197 | ||
d4e3b928 | 1198 | static CLOSURE_CALLBACK(bch2_journal_read_device) |
1c6fdbd8 | 1199 | { |
d4e3b928 | 1200 | closure_type(ja, struct journal_device, read); |
1c6fdbd8 | 1201 | struct bch_dev *ca = container_of(ja, struct bch_dev, journal); |
365f64f3 | 1202 | struct bch_fs *c = ca->fs; |
1c6fdbd8 KO |
1203 | struct journal_list *jlist = |
1204 | container_of(cl->parent, struct journal_list, cl); | |
1c6fdbd8 | 1205 | struct journal_read_buf buf = { NULL, 0 }; |
a9ec3454 | 1206 | unsigned i; |
9714baaa | 1207 | int ret = 0; |
1c6fdbd8 KO |
1208 | |
1209 | if (!ja->nr) | |
1210 | goto out; | |
1211 | ||
09b9c72b | 1212 | ret = journal_read_buf_realloc(c, &buf, PAGE_SIZE); |
1c6fdbd8 KO |
1213 | if (ret) |
1214 | goto err; | |
1215 | ||
1216 | pr_debug("%u journal buckets", ja->nr); | |
1217 | ||
1c6fdbd8 | 1218 | for (i = 0; i < ja->nr; i++) { |
a9ec3454 KO |
1219 | ret = journal_read_bucket(ca, &buf, jlist, i); |
1220 | if (ret) | |
1221 | goto err; | |
1c6fdbd8 KO |
1222 | } |
1223 | ||
1c6fdbd8 | 1224 | /* |
0ce2dbbe | 1225 | * Set dirty_idx to indicate the entire journal is full and needs to be |
1c6fdbd8 KO |
1226 | * reclaimed - journal reclaim will immediately reclaim whatever isn't |
1227 | * pinned when it first runs: | |
1228 | */ | |
0ce2dbbe KO |
1229 | ja->discard_idx = ja->dirty_idx_ondisk = |
1230 | ja->dirty_idx = (ja->cur_idx + 1) % ja->nr; | |
1c6fdbd8 | 1231 | out: |
365f64f3 | 1232 | bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret); |
cb6fc943 | 1233 | kvfree(buf.data); |
cca2c0d2 | 1234 | enumerated_ref_put(&ca->io_ref[READ], BCH_DEV_READ_REF_journal_read); |
1c6fdbd8 KO |
1235 | closure_return(cl); |
1236 | return; | |
1237 | err: | |
1238 | mutex_lock(&jlist->lock); | |
1239 | jlist->ret = ret; | |
1240 | mutex_unlock(&jlist->lock); | |
1241 | goto out; | |
1c6fdbd8 KO |
1242 | } |
1243 | ||
cd04497b KO |
1244 | noinline_for_stack |
1245 | static void bch2_journal_print_checksum_error(struct bch_fs *c, struct journal_replay *j) | |
1246 | { | |
1247 | struct printbuf buf = PRINTBUF; | |
1248 | enum bch_csum_type csum_type = JSET_CSUM_TYPE(&j->j); | |
1249 | bool have_good = false; | |
1250 | ||
1251 | prt_printf(&buf, "invalid journal checksum(s) at seq %llu ", le64_to_cpu(j->j.seq)); | |
1252 | bch2_journal_datetime_to_text(&buf, &j->j); | |
1253 | prt_newline(&buf); | |
1254 | ||
1255 | darray_for_each(j->ptrs, ptr) | |
1256 | if (!ptr->csum_good) { | |
1257 | bch2_journal_ptr_to_text(&buf, c, ptr); | |
1258 | prt_char(&buf, ' '); | |
1259 | bch2_csum_to_text(&buf, csum_type, ptr->csum); | |
1260 | prt_newline(&buf); | |
1261 | } else { | |
1262 | have_good = true; | |
1263 | } | |
1264 | ||
1265 | prt_printf(&buf, "should be "); | |
1266 | bch2_csum_to_text(&buf, csum_type, j->j.csum); | |
1267 | ||
1268 | if (have_good) | |
1269 | prt_printf(&buf, "\n(had good copy on another device)"); | |
1270 | ||
1271 | bch2_print_str(c, KERN_ERR, buf.buf); | |
1272 | printbuf_exit(&buf); | |
1273 | } | |
1274 | ||
1275 | noinline_for_stack | |
1276 | static int bch2_journal_check_for_missing(struct bch_fs *c, u64 start_seq, u64 end_seq) | |
1277 | { | |
1278 | struct printbuf buf = PRINTBUF; | |
1279 | int ret = 0; | |
1280 | ||
1281 | struct genradix_iter radix_iter; | |
1282 | struct journal_replay *i, **_i, *prev = NULL; | |
1283 | u64 seq = start_seq; | |
1284 | ||
1285 | genradix_for_each(&c->journal_entries, radix_iter, _i) { | |
1286 | i = *_i; | |
1287 | ||
1288 | if (journal_replay_ignore(i)) | |
1289 | continue; | |
1290 | ||
1291 | BUG_ON(seq > le64_to_cpu(i->j.seq)); | |
1292 | ||
1293 | while (seq < le64_to_cpu(i->j.seq)) { | |
1294 | while (seq < le64_to_cpu(i->j.seq) && | |
1295 | bch2_journal_seq_is_blacklisted(c, seq, false)) | |
1296 | seq++; | |
1297 | ||
1298 | if (seq == le64_to_cpu(i->j.seq)) | |
1299 | break; | |
1300 | ||
1301 | u64 missing_start = seq; | |
1302 | ||
1303 | while (seq < le64_to_cpu(i->j.seq) && | |
1304 | !bch2_journal_seq_is_blacklisted(c, seq, false)) | |
1305 | seq++; | |
1306 | ||
1307 | u64 missing_end = seq - 1; | |
1308 | ||
1309 | printbuf_reset(&buf); | |
1310 | prt_printf(&buf, "journal entries %llu-%llu missing! (replaying %llu-%llu)", | |
1311 | missing_start, missing_end, | |
1312 | start_seq, end_seq); | |
1313 | ||
1314 | prt_printf(&buf, "\nprev at "); | |
1315 | if (prev) { | |
1316 | bch2_journal_ptrs_to_text(&buf, c, prev); | |
1317 | prt_printf(&buf, " size %zu", vstruct_sectors(&prev->j, c->block_bits)); | |
1318 | } else | |
1319 | prt_printf(&buf, "(none)"); | |
1320 | ||
1321 | prt_printf(&buf, "\nnext at "); | |
1322 | bch2_journal_ptrs_to_text(&buf, c, i); | |
1323 | prt_printf(&buf, ", continue?"); | |
1324 | ||
1325 | fsck_err(c, journal_entries_missing, "%s", buf.buf); | |
1326 | } | |
1327 | ||
1328 | prev = i; | |
1329 | seq++; | |
1330 | } | |
1331 | fsck_err: | |
1332 | printbuf_exit(&buf); | |
1333 | return ret; | |
1334 | } | |
1335 | ||
5bbe3f2d KO |
1336 | int bch2_journal_read(struct bch_fs *c, |
1337 | u64 *last_seq, | |
1338 | u64 *blacklist_seq, | |
1339 | u64 *start_seq) | |
1c6fdbd8 | 1340 | { |
1c6fdbd8 | 1341 | struct journal_list jlist; |
cd04497b | 1342 | struct journal_replay *i, **_i; |
ce6201c4 | 1343 | struct genradix_iter radix_iter; |
fa8e94fa | 1344 | struct printbuf buf = PRINTBUF; |
dab1e248 | 1345 | bool degraded = false, last_write_torn = false; |
5bbe3f2d | 1346 | u64 seq; |
1c6fdbd8 KO |
1347 | int ret = 0; |
1348 | ||
1349 | closure_init_stack(&jlist.cl); | |
1350 | mutex_init(&jlist.lock); | |
ec7ccbde | 1351 | jlist.last_seq = 0; |
1c6fdbd8 KO |
1352 | jlist.ret = 0; |
1353 | ||
9fea2274 | 1354 | for_each_member_device(c, ca) { |
75c8d030 | 1355 | if (!c->opts.fsck && |
89fd25be | 1356 | !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal))) |
1c6fdbd8 KO |
1357 | continue; |
1358 | ||
2436cb9f KO |
1359 | if ((ca->mi.state == BCH_MEMBER_STATE_rw || |
1360 | ca->mi.state == BCH_MEMBER_STATE_ro) && | |
cca2c0d2 KO |
1361 | enumerated_ref_tryget(&ca->io_ref[READ], |
1362 | BCH_DEV_READ_REF_journal_read)) | |
1c6fdbd8 KO |
1363 | closure_call(&ca->journal.read, |
1364 | bch2_journal_read_device, | |
1365 | system_unbound_wq, | |
1366 | &jlist.cl); | |
1367 | else | |
1368 | degraded = true; | |
1369 | } | |
1370 | ||
aed4ccbf KO |
1371 | while (closure_sync_timeout(&jlist.cl, sysctl_hung_task_timeout_secs * HZ / 2)) |
1372 | ; | |
1c6fdbd8 KO |
1373 | |
1374 | if (jlist.ret) | |
1375 | return jlist.ret; | |
1376 | ||
5bbe3f2d | 1377 | *last_seq = 0; |
ff56d68c KO |
1378 | *start_seq = 0; |
1379 | *blacklist_seq = 0; | |
adbcada4 KO |
1380 | |
1381 | /* | |
1382 | * Find most recent flush entry, and ignore newer non flush entries - | |
1383 | * those entries will be blacklisted: | |
1384 | */ | |
ce6201c4 KO |
1385 | genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) { |
1386 | i = *_i; | |
1387 | ||
2cce3752 | 1388 | if (journal_replay_ignore(i)) |
adbcada4 KO |
1389 | continue; |
1390 | ||
ce6201c4 | 1391 | if (!*start_seq) |
ff56d68c | 1392 | *blacklist_seq = *start_seq = le64_to_cpu(i->j.seq) + 1; |
ce6201c4 | 1393 | |
dab1e248 | 1394 | if (JSET_NO_FLUSH(&i->j)) { |
2cce3752 | 1395 | i->ignore_blacklisted = true; |
dab1e248 | 1396 | continue; |
adbcada4 KO |
1397 | } |
1398 | ||
dab1e248 KO |
1399 | if (!last_write_torn && !i->csum_good) { |
1400 | last_write_torn = true; | |
2cce3752 | 1401 | i->ignore_blacklisted = true; |
dab1e248 KO |
1402 | continue; |
1403 | } | |
1404 | ||
60558d55 KO |
1405 | struct bkey_validate_context from = { |
1406 | .from = BKEY_VALIDATE_journal, | |
1407 | .journal_seq = le64_to_cpu(i->j.seq), | |
1408 | }; | |
dab1e248 | 1409 | if (journal_entry_err_on(le64_to_cpu(i->j.last_seq) > le64_to_cpu(i->j.seq), |
a8712967 | 1410 | c, le32_to_cpu(i->j.version), &i->j, NULL, |
b65db750 | 1411 | jset_last_seq_newer_than_seq, |
dab1e248 KO |
1412 | "invalid journal entry: last_seq > seq (%llu > %llu)", |
1413 | le64_to_cpu(i->j.last_seq), | |
1414 | le64_to_cpu(i->j.seq))) | |
1415 | i->j.last_seq = i->j.seq; | |
1416 | ||
5bbe3f2d | 1417 | *last_seq = le64_to_cpu(i->j.last_seq); |
dab1e248 KO |
1418 | *blacklist_seq = le64_to_cpu(i->j.seq) + 1; |
1419 | break; | |
adbcada4 KO |
1420 | } |
1421 | ||
ce6201c4 KO |
1422 | if (!*start_seq) { |
1423 | bch_info(c, "journal read done, but no entries found"); | |
1424 | return 0; | |
1425 | } | |
1426 | ||
5bbe3f2d | 1427 | if (!*last_seq) { |
b65db750 KO |
1428 | fsck_err(c, dirty_but_no_journal_entries_post_drop_nonflushes, |
1429 | "journal read done, but no entries found after dropping non-flushes"); | |
e0de429a | 1430 | return 0; |
adbcada4 KO |
1431 | } |
1432 | ||
a4b0f750 KO |
1433 | printbuf_reset(&buf); |
1434 | prt_printf(&buf, "journal read done, replaying entries %llu-%llu", | |
1435 | *last_seq, *blacklist_seq - 1); | |
17c3395e KO |
1436 | |
1437 | /* | |
1438 | * Drop blacklisted entries and entries older than last_seq (or start of | |
1439 | * journal rewind: | |
1440 | */ | |
1441 | u64 drop_before = *last_seq; | |
1442 | if (c->opts.journal_rewind) { | |
1443 | drop_before = min(drop_before, c->opts.journal_rewind); | |
1444 | prt_printf(&buf, " (rewinding from %llu)", c->opts.journal_rewind); | |
1445 | } | |
1446 | ||
1447 | *last_seq = drop_before; | |
ff56d68c | 1448 | if (*start_seq != *blacklist_seq) |
a4b0f750 KO |
1449 | prt_printf(&buf, " (unflushed %llu-%llu)", *blacklist_seq, *start_seq - 1); |
1450 | bch_info(c, "%s", buf.buf); | |
ce6201c4 KO |
1451 | genradix_for_each(&c->journal_entries, radix_iter, _i) { |
1452 | i = *_i; | |
1453 | ||
2cce3752 | 1454 | if (journal_replay_ignore(i)) |
adbcada4 KO |
1455 | continue; |
1456 | ||
1457 | seq = le64_to_cpu(i->j.seq); | |
17c3395e | 1458 | if (seq < drop_before) { |
2cce3752 | 1459 | journal_replay_free(c, i, false); |
adbcada4 KO |
1460 | continue; |
1461 | } | |
1462 | ||
1463 | if (bch2_journal_seq_is_blacklisted(c, seq, true)) { | |
1464 | fsck_err_on(!JSET_NO_FLUSH(&i->j), c, | |
b65db750 | 1465 | jset_seq_blacklisted, |
adbcada4 | 1466 | "found blacklisted journal entry %llu", seq); |
2cce3752 | 1467 | i->ignore_blacklisted = true; |
adbcada4 KO |
1468 | } |
1469 | } | |
1470 | ||
17c3395e | 1471 | ret = bch2_journal_check_for_missing(c, drop_before, *blacklist_seq - 1); |
cd04497b KO |
1472 | if (ret) |
1473 | goto err; | |
adbcada4 | 1474 | |
ce6201c4 | 1475 | genradix_for_each(&c->journal_entries, radix_iter, _i) { |
ae0386e1 | 1476 | union bch_replicas_padded replicas = { |
e4c3f386 | 1477 | .e.data_type = BCH_DATA_journal, |
fa1ab1b4 | 1478 | .e.nr_devs = 0, |
e4c3f386 KO |
1479 | .e.nr_required = 1, |
1480 | }; | |
7ef2a73a | 1481 | |
ce6201c4 | 1482 | i = *_i; |
2cce3752 | 1483 | if (journal_replay_ignore(i)) |
adbcada4 KO |
1484 | continue; |
1485 | ||
cd04497b KO |
1486 | /* |
1487 | * Don't print checksum errors until we know we're going to use | |
1488 | * a given journal entry: | |
1489 | */ | |
1490 | darray_for_each(i->ptrs, ptr) | |
1491 | if (!ptr->csum_good) { | |
1492 | bch2_journal_print_checksum_error(c, i); | |
1493 | break; | |
1494 | } | |
d1b2c864 KO |
1495 | |
1496 | ret = jset_validate(c, | |
d8585a79 | 1497 | bch2_dev_have_ref(c, i->ptrs.data[0].dev), |
d1b2c864 | 1498 | &i->j, |
a555bcf4 | 1499 | i->ptrs.data[0].sector, |
d1b2c864 | 1500 | READ); |
1c6fdbd8 | 1501 | if (ret) |
fa8e94fa | 1502 | goto err; |
1c6fdbd8 | 1503 | |
a555bcf4 | 1504 | darray_for_each(i->ptrs, ptr) |
fa1ab1b4 | 1505 | replicas_entry_add_dev(&replicas.e, ptr->dev); |
e4c3f386 | 1506 | |
26452d1d KO |
1507 | bch2_replicas_entry_sort(&replicas.e); |
1508 | ||
fa8e94fa KO |
1509 | printbuf_reset(&buf); |
1510 | bch2_replicas_entry_to_text(&buf, &replicas.e); | |
1511 | ||
1c6fdbd8 | 1512 | if (!degraded && |
83b3d959 KO |
1513 | !bch2_replicas_marked(c, &replicas.e) && |
1514 | (le64_to_cpu(i->j.seq) == *last_seq || | |
b65db750 | 1515 | fsck_err(c, journal_entry_replicas_not_marked, |
1ece5323 | 1516 | "superblock not marked as containing replicas for journal entry %llu\n%s", |
83b3d959 | 1517 | le64_to_cpu(i->j.seq), buf.buf))) { |
7ef2a73a | 1518 | ret = bch2_mark_replicas(c, &replicas.e); |
1c6fdbd8 | 1519 | if (ret) |
fa8e94fa | 1520 | goto err; |
1c6fdbd8 | 1521 | } |
1c6fdbd8 | 1522 | } |
fa8e94fa | 1523 | err: |
1c6fdbd8 | 1524 | fsck_err: |
fa8e94fa | 1525 | printbuf_exit(&buf); |
1c6fdbd8 KO |
1526 | return ret; |
1527 | } | |
1528 | ||
1c6fdbd8 KO |
1529 | /* journal write: */ |
1530 | ||
ff7e7c53 KO |
1531 | static void journal_advance_devs_to_next_bucket(struct journal *j, |
1532 | struct dev_alloc_list *devs, | |
afc5444e | 1533 | unsigned sectors, __le64 seq) |
ff7e7c53 KO |
1534 | { |
1535 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
1536 | ||
18dad454 | 1537 | guard(rcu)(); |
ff7e7c53 KO |
1538 | darray_for_each(*devs, i) { |
1539 | struct bch_dev *ca = rcu_dereference(c->devs[*i]); | |
1540 | if (!ca) | |
1541 | continue; | |
1542 | ||
1543 | struct journal_device *ja = &ca->journal; | |
1544 | ||
1545 | if (sectors > ja->sectors_free && | |
1546 | sectors <= ca->mi.bucket_size && | |
1547 | bch2_journal_dev_buckets_available(j, ja, | |
1548 | journal_space_discarded)) { | |
1549 | ja->cur_idx = (ja->cur_idx + 1) % ja->nr; | |
1550 | ja->sectors_free = ca->mi.bucket_size; | |
1551 | ||
1552 | /* | |
1553 | * ja->bucket_seq[ja->cur_idx] must always have | |
1554 | * something sensible: | |
1555 | */ | |
1556 | ja->bucket_seq[ja->cur_idx] = le64_to_cpu(seq); | |
1557 | } | |
1558 | } | |
1559 | } | |
1560 | ||
a9ec3454 KO |
1561 | static void __journal_write_alloc(struct journal *j, |
1562 | struct journal_buf *w, | |
47d6ee76 | 1563 | struct dev_alloc_list *devs, |
a9ec3454 KO |
1564 | unsigned sectors, |
1565 | unsigned *replicas, | |
1566 | unsigned replicas_want) | |
1c6fdbd8 KO |
1567 | { |
1568 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
a2753581 | 1569 | |
47d6ee76 | 1570 | darray_for_each(*devs, i) { |
7d4f2687 KO |
1571 | struct bch_dev *ca = bch2_dev_get_ioref(c, *i, WRITE, |
1572 | BCH_DEV_WRITE_REF_journal_write); | |
1c6fdbd8 KO |
1573 | if (!ca) |
1574 | continue; | |
1575 | ||
47d6ee76 | 1576 | struct journal_device *ja = &ca->journal; |
1c6fdbd8 KO |
1577 | |
1578 | /* | |
1579 | * Check that we can use this device, and aren't already using | |
1580 | * it: | |
1581 | */ | |
a9ec3454 | 1582 | if (!ca->mi.durability || |
2436cb9f | 1583 | ca->mi.state != BCH_MEMBER_STATE_rw || |
a9ec3454 | 1584 | !ja->nr || |
702ffea2 | 1585 | bch2_bkey_has_device_c(bkey_i_to_s_c(&w->key), ca->dev_idx) || |
7d4f2687 KO |
1586 | sectors > ja->sectors_free) { |
1587 | enumerated_ref_put(&ca->io_ref[WRITE], BCH_DEV_WRITE_REF_journal_write); | |
1c6fdbd8 | 1588 | continue; |
7d4f2687 | 1589 | } |
1c6fdbd8 | 1590 | |
3d080aa5 | 1591 | bch2_dev_stripe_increment(ca, &j->wp.stripe); |
1c6fdbd8 | 1592 | |
26609b61 | 1593 | bch2_bkey_append_ptr(&w->key, |
1c6fdbd8 KO |
1594 | (struct bch_extent_ptr) { |
1595 | .offset = bucket_to_sector(ca, | |
a9ec3454 KO |
1596 | ja->buckets[ja->cur_idx]) + |
1597 | ca->mi.bucket_size - | |
1598 | ja->sectors_free, | |
1c6fdbd8 KO |
1599 | .dev = ca->dev_idx, |
1600 | }); | |
1601 | ||
a9ec3454 KO |
1602 | ja->sectors_free -= sectors; |
1603 | ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq); | |
1604 | ||
1605 | *replicas += ca->mi.durability; | |
1606 | ||
1607 | if (*replicas >= replicas_want) | |
1608 | break; | |
1c6fdbd8 | 1609 | } |
a9ec3454 | 1610 | } |
1c6fdbd8 | 1611 | |
7d4f2687 KO |
1612 | static int journal_write_alloc(struct journal *j, struct journal_buf *w, |
1613 | unsigned *replicas) | |
a9ec3454 KO |
1614 | { |
1615 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
d042b040 | 1616 | struct bch_devs_mask devs; |
a9ec3454 | 1617 | struct dev_alloc_list devs_sorted; |
96dea3d5 | 1618 | unsigned sectors = vstruct_sectors(w->data, c->block_bits); |
d042b040 KO |
1619 | unsigned target = c->opts.metadata_target ?: |
1620 | c->opts.foreground_target; | |
7d4f2687 | 1621 | unsigned replicas_want = READ_ONCE(c->opts.metadata_replicas); |
4e074475 KO |
1622 | unsigned replicas_need = min_t(unsigned, replicas_want, |
1623 | READ_ONCE(c->opts.metadata_replicas_required)); | |
ff7e7c53 | 1624 | bool advance_done = false; |
1c6fdbd8 | 1625 | |
ff7e7c53 | 1626 | retry_target: |
d042b040 | 1627 | devs = target_rw_devs(c, BCH_DATA_journal, target); |
e87de7d4 | 1628 | bch2_dev_alloc_list(c, &j->wp.stripe, &devs, &devs_sorted); |
ff7e7c53 | 1629 | retry_alloc: |
7d4f2687 | 1630 | __journal_write_alloc(j, w, &devs_sorted, sectors, replicas, replicas_want); |
1c6fdbd8 | 1631 | |
7d4f2687 | 1632 | if (likely(*replicas >= replicas_want)) |
a9ec3454 KO |
1633 | goto done; |
1634 | ||
ff7e7c53 KO |
1635 | if (!advance_done) { |
1636 | journal_advance_devs_to_next_bucket(j, &devs_sorted, sectors, w->data->seq); | |
1637 | advance_done = true; | |
1638 | goto retry_alloc; | |
a9ec3454 KO |
1639 | } |
1640 | ||
7d4f2687 | 1641 | if (*replicas < replicas_want && target) { |
d042b040 KO |
1642 | /* Retry from all devices: */ |
1643 | target = 0; | |
ff7e7c53 KO |
1644 | advance_done = false; |
1645 | goto retry_target; | |
d042b040 | 1646 | } |
a9ec3454 | 1647 | done: |
07a1006a KO |
1648 | BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX); |
1649 | ||
813825d2 KO |
1650 | #if 0 |
1651 | /* | |
1652 | * XXX: we need a way to alert the user when we go degraded for any | |
1653 | * reason | |
1654 | */ | |
1655 | if (*replicas < min(replicas_want, | |
1656 | dev_mask_nr(&c->rw_devs[BCH_DATA_free]))) { | |
1657 | } | |
1658 | #endif | |
1659 | ||
7d4f2687 | 1660 | return *replicas >= replicas_need ? 0 : -BCH_ERR_insufficient_journal_devices; |
1c6fdbd8 KO |
1661 | } |
1662 | ||
1c6fdbd8 KO |
1663 | static void journal_buf_realloc(struct journal *j, struct journal_buf *buf) |
1664 | { | |
09caeabe KO |
1665 | struct bch_fs *c = container_of(j, struct bch_fs, journal); |
1666 | ||
1c6fdbd8 KO |
1667 | /* we aren't holding j->lock: */ |
1668 | unsigned new_size = READ_ONCE(j->buf_size_want); | |
1669 | void *new_buf; | |
1670 | ||
d16b4a77 | 1671 | if (buf->buf_size >= new_size) |
1c6fdbd8 KO |
1672 | return; |
1673 | ||
09caeabe KO |
1674 | size_t btree_write_buffer_size = new_size / 64; |
1675 | ||
1676 | if (bch2_btree_write_buffer_resize(c, btree_write_buffer_size)) | |
1677 | return; | |
1678 | ||
cb6fc943 | 1679 | new_buf = kvmalloc(new_size, GFP_NOFS|__GFP_NOWARN); |
1c6fdbd8 KO |
1680 | if (!new_buf) |
1681 | return; | |
1682 | ||
d16b4a77 | 1683 | memcpy(new_buf, buf->data, buf->buf_size); |
c859430b KO |
1684 | |
1685 | spin_lock(&j->lock); | |
1686 | swap(buf->data, new_buf); | |
1687 | swap(buf->buf_size, new_size); | |
1688 | spin_unlock(&j->lock); | |
1689 | ||
cb6fc943 | 1690 | kvfree(new_buf); |
1c6fdbd8 KO |
1691 | } |
1692 | ||
d4e3b928 | 1693 | static CLOSURE_CALLBACK(journal_write_done) |
1c6fdbd8 | 1694 | { |
38789c25 KO |
1695 | closure_type(w, struct journal_buf, io); |
1696 | struct journal *j = container_of(w, struct journal, buf[w->idx]); | |
1c6fdbd8 | 1697 | struct bch_fs *c = container_of(j, struct bch_fs, journal); |
ae0386e1 | 1698 | union bch_replicas_padded replicas; |
68573b93 | 1699 | u64 seq = le64_to_cpu(w->data->seq); |
158eecb8 | 1700 | int err = 0; |
1c6fdbd8 | 1701 | |
991ba021 KO |
1702 | bch2_time_stats_update(!JSET_NO_FLUSH(w->data) |
1703 | ? j->flush_write_time | |
1704 | : j->noflush_write_time, j->write_start_time); | |
9c859dc9 | 1705 | |
d797ca3d | 1706 | if (!w->devs_written.nr) { |
09b9c72b | 1707 | err = bch_err_throw(c, journal_write_err); |
83b3d959 KO |
1708 | } else { |
1709 | bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal, | |
1710 | w->devs_written); | |
cb87f623 | 1711 | err = bch2_mark_replicas(c, &replicas.e); |
1c6fdbd8 | 1712 | } |
83b3d959 | 1713 | |
b42fac04 KO |
1714 | if (err && !bch2_journal_error(j)) { |
1715 | struct printbuf buf = PRINTBUF; | |
1716 | bch2_log_msg_start(c, &buf); | |
1717 | ||
1718 | if (err == -BCH_ERR_journal_write_err) | |
43463598 | 1719 | prt_printf(&buf, "unable to write journal to sufficient devices\n"); |
b42fac04 | 1720 | else |
43463598 KO |
1721 | prt_printf(&buf, "journal write error marking replicas: %s\n", |
1722 | bch2_err_str(err)); | |
b42fac04 KO |
1723 | |
1724 | bch2_fs_emergency_read_only2(c, &buf); | |
1725 | ||
1726 | bch2_print_str(c, KERN_ERR, buf.buf); | |
1727 | printbuf_exit(&buf); | |
1728 | } | |
1c6fdbd8 | 1729 | |
916abefd | 1730 | closure_debug_destroy(cl); |
ed9d58a2 | 1731 | |
916abefd | 1732 | spin_lock(&j->lock); |
1c6fdbd8 | 1733 | if (seq >= j->pin.front) |
d797ca3d | 1734 | journal_seq_pin(j, seq)->devs = w->devs_written; |
916abefd KO |
1735 | if (err && (!j->err_seq || seq < j->err_seq)) |
1736 | j->err_seq = seq; | |
1737 | w->write_done = true; | |
1738 | ||
35282ce9 KO |
1739 | if (!j->free_buf || j->free_buf_size < w->buf_size) { |
1740 | swap(j->free_buf, w->data); | |
1741 | swap(j->free_buf_size, w->buf_size); | |
1742 | } | |
1743 | ||
1744 | if (w->data) { | |
1745 | void *buf = w->data; | |
1746 | w->data = NULL; | |
1747 | w->buf_size = 0; | |
1748 | ||
1749 | spin_unlock(&j->lock); | |
1750 | kvfree(buf); | |
1751 | spin_lock(&j->lock); | |
1752 | } | |
1753 | ||
916abefd | 1754 | bool completed = false; |
cf164a91 | 1755 | bool do_discards = false; |
916abefd KO |
1756 | |
1757 | for (seq = journal_last_unwritten_seq(j); | |
1758 | seq <= journal_cur_seq(j); | |
1759 | seq++) { | |
1760 | w = j->buf + (seq & JOURNAL_BUF_MASK); | |
1761 | if (!w->write_done) | |
1762 | break; | |
1c6fdbd8 | 1763 | |
35282ce9 | 1764 | if (!j->err_seq && !w->noflush) { |
9be1efe9 KO |
1765 | j->flushed_seq_ondisk = seq; |
1766 | j->last_seq_ondisk = w->last_seq; | |
f25d8215 KO |
1767 | |
1768 | closure_wake_up(&c->freelist_wait); | |
f25d8215 | 1769 | bch2_reset_alloc_cursors(c); |
9be1efe9 | 1770 | } |
0ce2dbbe | 1771 | |
916abefd | 1772 | j->seq_ondisk = seq; |
f0a3a2cc | 1773 | |
916abefd KO |
1774 | /* |
1775 | * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard | |
1776 | * more buckets: | |
1777 | * | |
1778 | * Must come before signaling write completion, for | |
1779 | * bch2_fs_journal_stop(): | |
1780 | */ | |
1781 | if (j->watermark != BCH_WATERMARK_stripe) | |
1782 | journal_reclaim_kick(&c->journal); | |
158eecb8 | 1783 | |
ada02c20 | 1784 | closure_wake_up(&w->wait); |
916abefd KO |
1785 | completed = true; |
1786 | } | |
1c6fdbd8 | 1787 | |
916abefd KO |
1788 | if (completed) { |
1789 | bch2_journal_reclaim_fast(j); | |
1790 | bch2_journal_space_available(j); | |
5d32c5bb | 1791 | |
f1ca1abf | 1792 | track_event_change(&c->times[BCH_TIME_blocked_journal_max_in_flight], false); |
066a2646 | 1793 | |
916abefd KO |
1794 | journal_wake(j); |
1795 | } | |
1c6fdbd8 | 1796 | |
916abefd | 1797 | if (journal_last_unwritten_seq(j) == journal_cur_seq(j) && |
c2be81d4 | 1798 | j->reservations.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) { |
fbec3b88 KO |
1799 | struct journal_buf *buf = journal_cur_buf(j); |
1800 | long delta = buf->expires - jiffies; | |
ebb84d09 | 1801 | |
24a3d53b KO |
1802 | /* |
1803 | * We don't close a journal entry to write it while there's | |
1804 | * previous entries still in flight - the current journal entry | |
1805 | * might want to be written now: | |
1806 | */ | |
656f05d8 | 1807 | mod_delayed_work(j->wq, &j->write_work, max(0L, delta)); |
24a3d53b | 1808 | } |
916abefd | 1809 | |
89d21b69 KO |
1810 | /* |
1811 | * We don't typically trigger journal writes from her - the next journal | |
1812 | * write will be triggered immediately after the previous one is | |
1813 | * allocated, in bch2_journal_write() - but the journal write error path | |
1814 | * is special: | |
1815 | */ | |
1816 | bch2_journal_do_writes(j); | |
916abefd | 1817 | spin_unlock(&j->lock); |
cf164a91 KO |
1818 | |
1819 | if (do_discards) | |
1820 | bch2_do_discards(c); | |
1c6fdbd8 KO |
1821 | } |
1822 | ||
1823 | static void journal_write_endio(struct bio *bio) | |
1824 | { | |
38789c25 KO |
1825 | struct journal_bio *jbio = container_of(bio, struct journal_bio, bio); |
1826 | struct bch_dev *ca = jbio->ca; | |
1c6fdbd8 | 1827 | struct journal *j = &ca->fs->journal; |
38789c25 | 1828 | struct journal_buf *w = j->buf + jbio->buf_idx; |
1c6fdbd8 | 1829 | |
b31c0704 KO |
1830 | bch2_account_io_completion(ca, BCH_MEMBER_ERROR_write, |
1831 | jbio->submit_time, !bio->bi_status); | |
1832 | ||
1833 | if (bio->bi_status) { | |
1834 | bch_err_dev_ratelimited(ca, | |
94119eeb | 1835 | "error writing journal entry %llu: %s", |
d797ca3d | 1836 | le64_to_cpu(w->data->seq), |
b31c0704 | 1837 | bch2_blk_status_to_str(bio->bi_status)); |
38789c25 | 1838 | |
b31c0704 | 1839 | unsigned long flags; |
1c6fdbd8 | 1840 | spin_lock_irqsave(&j->err_lock, flags); |
d797ca3d | 1841 | bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx); |
1c6fdbd8 KO |
1842 | spin_unlock_irqrestore(&j->err_lock, flags); |
1843 | } | |
1844 | ||
38789c25 | 1845 | closure_put(&w->io); |
cca2c0d2 | 1846 | enumerated_ref_put(&ca->io_ref[WRITE], BCH_DEV_WRITE_REF_journal_write); |
1c6fdbd8 KO |
1847 | } |
1848 | ||
85ab365f | 1849 | static CLOSURE_CALLBACK(journal_write_submit) |
280249b9 | 1850 | { |
38789c25 KO |
1851 | closure_type(w, struct journal_buf, io); |
1852 | struct journal *j = container_of(w, struct journal, buf[w->idx]); | |
280249b9 | 1853 | struct bch_fs *c = container_of(j, struct bch_fs, journal); |
280249b9 KO |
1854 | unsigned sectors = vstruct_sectors(w->data, c->block_bits); |
1855 | ||
1856 | extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) { | |
7d4f2687 | 1857 | struct bch_dev *ca = bch2_dev_have_ref(c, ptr->dev); |
280249b9 KO |
1858 | |
1859 | this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal], | |
1860 | sectors); | |
1861 | ||
6212ea24 | 1862 | struct journal_device *ja = &ca->journal; |
b31c0704 KO |
1863 | struct journal_bio *jbio = ja->bio[w->idx]; |
1864 | struct bio *bio = &jbio->bio; | |
1865 | ||
1866 | jbio->submit_time = local_clock(); | |
1867 | ||
280249b9 KO |
1868 | bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META); |
1869 | bio->bi_iter.bi_sector = ptr->offset; | |
1870 | bio->bi_end_io = journal_write_endio; | |
1871 | bio->bi_private = ca; | |
75474a54 | 1872 | bio->bi_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 0); |
280249b9 | 1873 | |
a28bd48a KO |
1874 | BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector); |
1875 | ca->prev_journal_sector = bio->bi_iter.bi_sector; | |
1876 | ||
280249b9 KO |
1877 | if (!JSET_NO_FLUSH(w->data)) |
1878 | bio->bi_opf |= REQ_FUA; | |
1879 | if (!JSET_NO_FLUSH(w->data) && !w->separate_flush) | |
1880 | bio->bi_opf |= REQ_PREFLUSH; | |
1881 | ||
1882 | bch2_bio_map(bio, w->data, sectors << 9); | |
1883 | ||
674cfc26 | 1884 | trace_and_count(c, journal_write, bio); |
280249b9 KO |
1885 | closure_bio_submit(bio, cl); |
1886 | ||
51654002 | 1887 | ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq); |
280249b9 KO |
1888 | } |
1889 | ||
656f05d8 | 1890 | continue_at(cl, journal_write_done, j->wq); |
280249b9 KO |
1891 | } |
1892 | ||
85ab365f KO |
1893 | static CLOSURE_CALLBACK(journal_write_preflush) |
1894 | { | |
1895 | closure_type(w, struct journal_buf, io); | |
1896 | struct journal *j = container_of(w, struct journal, buf[w->idx]); | |
1897 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
1898 | ||
4b0fac4b KO |
1899 | /* |
1900 | * Wait for previous journal writes to comelete; they won't necessarily | |
1901 | * be flushed if they're still in flight | |
1902 | */ | |
85ab365f KO |
1903 | if (j->seq_ondisk + 1 != le64_to_cpu(w->data->seq)) { |
1904 | spin_lock(&j->lock); | |
04357732 KO |
1905 | if (j->seq_ondisk + 1 != le64_to_cpu(w->data->seq)) { |
1906 | closure_wait(&j->async_wait, cl); | |
1907 | spin_unlock(&j->lock); | |
1908 | continue_at(cl, journal_write_preflush, j->wq); | |
1909 | return; | |
1910 | } | |
85ab365f | 1911 | spin_unlock(&j->lock); |
85ab365f KO |
1912 | } |
1913 | ||
1914 | if (w->separate_flush) { | |
cca2c0d2 KO |
1915 | for_each_rw_member(c, ca, BCH_DEV_WRITE_REF_journal_write) { |
1916 | enumerated_ref_get(&ca->io_ref[WRITE], | |
1917 | BCH_DEV_WRITE_REF_journal_write); | |
85ab365f KO |
1918 | |
1919 | struct journal_device *ja = &ca->journal; | |
1920 | struct bio *bio = &ja->bio[w->idx]->bio; | |
1921 | bio_reset(bio, ca->disk_sb.bdev, | |
1922 | REQ_OP_WRITE|REQ_SYNC|REQ_META|REQ_PREFLUSH); | |
1923 | bio->bi_end_io = journal_write_endio; | |
1924 | bio->bi_private = ca; | |
1925 | closure_bio_submit(bio, cl); | |
1926 | } | |
1927 | ||
1928 | continue_at(cl, journal_write_submit, j->wq); | |
1929 | } else { | |
1930 | /* | |
1931 | * no need to punt to another work item if we're not waiting on | |
1932 | * preflushes | |
1933 | */ | |
1934 | journal_write_submit(&cl->work); | |
1935 | } | |
1936 | } | |
1937 | ||
769b3600 | 1938 | static int bch2_journal_write_prep(struct journal *j, struct journal_buf *w) |
9f6db127 | 1939 | { |
769b3600 | 1940 | struct bch_fs *c = container_of(j, struct bch_fs, journal); |
cea07a7b | 1941 | struct jset_entry *start, *end; |
769b3600 | 1942 | struct jset *jset = w->data; |
09caeabe | 1943 | struct journal_keys_to_wb wb = { NULL }; |
2a6c0136 | 1944 | unsigned u64s; |
769b3600 | 1945 | unsigned long btree_roots_have = 0; |
09caeabe | 1946 | u64 seq = le64_to_cpu(jset->seq); |
769b3600 | 1947 | int ret; |
9f6db127 KO |
1948 | |
1949 | /* | |
1950 | * Simple compaction, dropping empty jset_entries (from journal | |
1951 | * reservations that weren't fully used) and merging jset_entries that | |
1952 | * can be. | |
1953 | * | |
1954 | * If we wanted to be really fancy here, we could sort all the keys in | |
1955 | * the jset and drop keys that were overwritten - probably not worth it: | |
1956 | */ | |
73ffa530 | 1957 | vstruct_for_each(jset, i) { |
9f6db127 KO |
1958 | unsigned u64s = le16_to_cpu(i->u64s); |
1959 | ||
1960 | /* Empty entry: */ | |
1961 | if (!u64s) | |
1962 | continue; | |
1963 | ||
769b3600 KO |
1964 | /* |
1965 | * New btree roots are set by journalling them; when the journal | |
1966 | * entry gets written we have to propagate them to | |
1967 | * c->btree_roots | |
1968 | * | |
1969 | * But, every journal entry we write has to contain all the | |
1970 | * btree roots (at least for now); so after we copy btree roots | |
1971 | * to c->btree_roots we have to get any missing btree roots and | |
1972 | * add them to this journal entry: | |
1973 | */ | |
09caeabe KO |
1974 | switch (i->type) { |
1975 | case BCH_JSET_ENTRY_btree_root: | |
9f6db127 | 1976 | bch2_journal_entry_to_btree_root(c, i); |
769b3600 | 1977 | __set_bit(i->btree_id, &btree_roots_have); |
09caeabe KO |
1978 | break; |
1979 | case BCH_JSET_ENTRY_write_buffer_keys: | |
1980 | EBUG_ON(!w->need_flush_to_write_buffer); | |
1981 | ||
1982 | if (!wb.wb) | |
1983 | bch2_journal_keys_to_write_buffer_start(c, &wb, seq); | |
1984 | ||
09caeabe KO |
1985 | jset_entry_for_each_key(i, k) { |
1986 | ret = bch2_journal_key_to_wb(c, &wb, i->btree_id, k); | |
1987 | if (ret) { | |
3ed94062 KO |
1988 | bch2_fs_fatal_error(c, "flushing journal keys to btree write buffer: %s", |
1989 | bch2_err_str(ret)); | |
09caeabe KO |
1990 | bch2_journal_keys_to_write_buffer_end(c, &wb); |
1991 | return ret; | |
1992 | } | |
1993 | } | |
1994 | i->type = BCH_JSET_ENTRY_btree_keys; | |
1995 | break; | |
769b3600 | 1996 | } |
9f6db127 KO |
1997 | } |
1998 | ||
b9efa967 KO |
1999 | if (wb.wb) { |
2000 | ret = bch2_journal_keys_to_write_buffer_end(c, &wb); | |
2001 | if (ret) { | |
2002 | bch2_fs_fatal_error(c, "error flushing journal keys to btree write buffer: %s", | |
2003 | bch2_err_str(ret)); | |
2004 | return ret; | |
2005 | } | |
2006 | } | |
d9290c99 KO |
2007 | |
2008 | spin_lock(&c->journal.lock); | |
09caeabe | 2009 | w->need_flush_to_write_buffer = false; |
d9290c99 | 2010 | spin_unlock(&c->journal.lock); |
09caeabe | 2011 | |
00b8ccf7 KO |
2012 | start = end = vstruct_last(jset); |
2013 | ||
769b3600 | 2014 | end = bch2_btree_roots_to_journal_entries(c, end, btree_roots_have); |
00b8ccf7 | 2015 | |
52f7d75e KO |
2016 | struct jset_entry_datetime *d = |
2017 | container_of(jset_entry_init(&end, sizeof(*d)), struct jset_entry_datetime, entry); | |
2018 | d->entry.type = BCH_JSET_ENTRY_datetime; | |
2019 | d->seconds = cpu_to_le64(ktime_get_real_seconds()); | |
2020 | ||
09caeabe | 2021 | bch2_journal_super_entries_add_common(c, &end, seq); |
3ccc5c50 | 2022 | u64s = (u64 *) end - (u64 *) start; |
62f35024 KO |
2023 | |
2024 | WARN_ON(u64s > j->entry_u64s_reserved); | |
3ccc5c50 | 2025 | |
d16b4a77 | 2026 | le32_add_cpu(&jset->u64s, u64s); |
4a2e5d7b | 2027 | |
2a6c0136 | 2028 | unsigned sectors = vstruct_sectors(jset, c->block_bits); |
4a2e5d7b KO |
2029 | |
2030 | if (sectors > w->sectors) { | |
3ed94062 | 2031 | bch2_fs_fatal_error(c, ": journal write overran available space, %zu > %u (extra %u reserved %u/%u)", |
4a2e5d7b KO |
2032 | vstruct_bytes(jset), w->sectors << 9, |
2033 | u64s, w->u64s_reserved, j->entry_u64s_reserved); | |
80396a47 | 2034 | return -EINVAL; |
4a2e5d7b | 2035 | } |
1c6fdbd8 | 2036 | |
2a6c0136 KO |
2037 | return 0; |
2038 | } | |
2039 | ||
2040 | static int bch2_journal_write_checksum(struct journal *j, struct journal_buf *w) | |
2041 | { | |
2042 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
2043 | struct jset *jset = w->data; | |
2044 | u64 seq = le64_to_cpu(jset->seq); | |
2045 | bool validate_before_checksum = false; | |
2046 | int ret = 0; | |
2047 | ||
1c6fdbd8 | 2048 | jset->magic = cpu_to_le64(jset_magic(c)); |
a02a0121 | 2049 | jset->version = cpu_to_le32(c->sb.version); |
1c6fdbd8 KO |
2050 | |
2051 | SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN); | |
2052 | SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c)); | |
2053 | ||
4141fde0 | 2054 | if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset)) |
09caeabe | 2055 | j->last_empty_seq = seq; |
158eecb8 | 2056 | |
26609b61 KO |
2057 | if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset))) |
2058 | validate_before_checksum = true; | |
2059 | ||
e751c01a | 2060 | if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current) |
26609b61 KO |
2061 | validate_before_checksum = true; |
2062 | ||
2063 | if (validate_before_checksum && | |
80396a47 KO |
2064 | (ret = jset_validate(c, NULL, jset, 0, WRITE))) |
2065 | return ret; | |
1c6fdbd8 | 2066 | |
a9de137b | 2067 | ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset), |
1c6fdbd8 KO |
2068 | jset->encrypted_start, |
2069 | vstruct_end(jset) - (void *) jset->encrypted_start); | |
2a6c0136 | 2070 | if (bch2_fs_fatal_err_on(ret, c, "encrypting journal entry: %s", bch2_err_str(ret))) |
80396a47 | 2071 | return ret; |
1c6fdbd8 KO |
2072 | |
2073 | jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset), | |
2074 | journal_nonce(jset), jset); | |
2075 | ||
26609b61 | 2076 | if (!validate_before_checksum && |
80396a47 KO |
2077 | (ret = jset_validate(c, NULL, jset, 0, WRITE))) |
2078 | return ret; | |
1c6fdbd8 | 2079 | |
2a6c0136 KO |
2080 | unsigned sectors = vstruct_sectors(jset, c->block_bits); |
2081 | unsigned bytes = vstruct_bytes(jset); | |
d16b4a77 | 2082 | memset((void *) jset + bytes, 0, (sectors << 9) - bytes); |
80396a47 KO |
2083 | return 0; |
2084 | } | |
2085 | ||
2086 | static int bch2_journal_write_pick_flush(struct journal *j, struct journal_buf *w) | |
2087 | { | |
2088 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
2089 | int error = bch2_journal_error(j); | |
2090 | ||
2091 | /* | |
2092 | * If the journal is in an error state - we did an emergency shutdown - | |
2093 | * we prefer to continue doing journal writes. We just mark them as | |
2094 | * noflush so they'll never be used, but they'll still be visible by the | |
2095 | * list_journal tool - this helps in debugging. | |
2096 | * | |
2097 | * There's a caveat: the first journal write after marking the | |
2098 | * superblock dirty must always be a flush write, because on startup | |
2099 | * from a clean shutdown we didn't necessarily read the journal and the | |
2100 | * new journal write might overwrite whatever was in the journal | |
2101 | * previously - we can't leave the journal without any flush writes in | |
2102 | * it. | |
2103 | * | |
2104 | * So if we're in an error state, and we're still starting up, we don't | |
2105 | * write anything at all. | |
2106 | */ | |
b895c703 | 2107 | if (error && test_bit(JOURNAL_need_flush_write, &j->flags)) |
cb87f623 | 2108 | return error; |
80396a47 KO |
2109 | |
2110 | if (error || | |
2111 | w->noflush || | |
2112 | (!w->must_flush && | |
848c3ff8 CY |
2113 | time_before(jiffies, j->last_flush_write + |
2114 | msecs_to_jiffies(c->opts.journal_flush_delay)) && | |
b895c703 | 2115 | test_bit(JOURNAL_may_skip_flush, &j->flags))) { |
225879f4 | 2116 | w->noflush = true; |
80396a47 KO |
2117 | SET_JSET_NO_FLUSH(w->data, true); |
2118 | w->data->last_seq = 0; | |
2119 | w->last_seq = 0; | |
2120 | ||
2121 | j->nr_noflush_writes++; | |
2122 | } else { | |
7efa2875 | 2123 | w->must_flush = true; |
80396a47 KO |
2124 | j->last_flush_write = jiffies; |
2125 | j->nr_flush_writes++; | |
b895c703 | 2126 | clear_bit(JOURNAL_need_flush_write, &j->flags); |
80396a47 KO |
2127 | } |
2128 | ||
2129 | return 0; | |
2130 | } | |
2131 | ||
d4e3b928 | 2132 | CLOSURE_CALLBACK(bch2_journal_write) |
80396a47 | 2133 | { |
38789c25 KO |
2134 | closure_type(w, struct journal_buf, io); |
2135 | struct journal *j = container_of(w, struct journal, buf[w->idx]); | |
80396a47 | 2136 | struct bch_fs *c = container_of(j, struct bch_fs, journal); |
ae0386e1 | 2137 | union bch_replicas_padded replicas; |
813825d2 | 2138 | unsigned nr_rw_members = dev_mask_nr(&c->rw_devs[BCH_DATA_free]); |
80396a47 KO |
2139 | int ret; |
2140 | ||
2141 | BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb)); | |
d9290c99 | 2142 | BUG_ON(!w->write_started); |
916abefd | 2143 | BUG_ON(w->write_allocated); |
d9290c99 | 2144 | BUG_ON(w->write_done); |
80396a47 KO |
2145 | |
2146 | j->write_start_time = local_clock(); | |
1c6fdbd8 | 2147 | |
e5a66496 | 2148 | spin_lock(&j->lock); |
d9290c99 KO |
2149 | if (nr_rw_members > 1) |
2150 | w->separate_flush = true; | |
2151 | ||
80396a47 KO |
2152 | ret = bch2_journal_write_pick_flush(j, w); |
2153 | spin_unlock(&j->lock); | |
7d4f2687 KO |
2154 | |
2155 | if (unlikely(ret)) | |
80396a47 KO |
2156 | goto err; |
2157 | ||
b05c0e93 | 2158 | mutex_lock(&j->buf_lock); |
769b3600 KO |
2159 | journal_buf_realloc(j, w); |
2160 | ||
80396a47 | 2161 | ret = bch2_journal_write_prep(j, w); |
b05c0e93 | 2162 | mutex_unlock(&j->buf_lock); |
80396a47 | 2163 | |
7d4f2687 KO |
2164 | if (unlikely(ret)) |
2165 | goto err; | |
fa5df9e7 | 2166 | |
7d4f2687 | 2167 | unsigned replicas_allocated = 0; |
80396a47 | 2168 | while (1) { |
7d4f2687 | 2169 | ret = journal_write_alloc(j, w, &replicas_allocated); |
80396a47 KO |
2170 | if (!ret || !j->can_discard) |
2171 | break; | |
e5a66496 | 2172 | |
c18dade6 | 2173 | bch2_journal_do_discards(j); |
c18dade6 KO |
2174 | } |
2175 | ||
7d4f2687 KO |
2176 | if (unlikely(ret)) |
2177 | goto err_allocate_write; | |
85674154 | 2178 | |
2a6c0136 KO |
2179 | ret = bch2_journal_write_checksum(j, w); |
2180 | if (unlikely(ret)) | |
2181 | goto err; | |
2182 | ||
7d4f2687 | 2183 | spin_lock(&j->lock); |
e5a66496 KO |
2184 | /* |
2185 | * write is allocated, no longer need to account for it in | |
2186 | * bch2_journal_space_available(): | |
2187 | */ | |
2188 | w->sectors = 0; | |
916abefd | 2189 | w->write_allocated = true; |
7d4f2687 | 2190 | j->entry_bytes_written += vstruct_bytes(w->data); |
e5a66496 KO |
2191 | |
2192 | /* | |
2193 | * journal entry has been compacted and allocated, recalculate space | |
2194 | * available: | |
2195 | */ | |
2196 | bch2_journal_space_available(j); | |
916abefd | 2197 | bch2_journal_do_writes(j); |
e5a66496 KO |
2198 | spin_unlock(&j->lock); |
2199 | ||
d797ca3d KO |
2200 | w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key)); |
2201 | ||
a7b29b8d BF |
2202 | /* |
2203 | * Mark journal replicas before we submit the write to guarantee | |
2204 | * recovery will find the journal entries after a crash. | |
2205 | */ | |
2206 | bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal, | |
2207 | w->devs_written); | |
2208 | ret = bch2_mark_replicas(c, &replicas.e); | |
2209 | if (ret) | |
2210 | goto err; | |
2211 | ||
7d4f2687 KO |
2212 | if (c->opts.nochanges) |
2213 | goto no_io; | |
2214 | ||
38789c25 | 2215 | if (!JSET_NO_FLUSH(w->data)) |
85ab365f KO |
2216 | continue_at(cl, journal_write_preflush, j->wq); |
2217 | else | |
2218 | continue_at(cl, journal_write_submit, j->wq); | |
280249b9 | 2219 | return; |
7d4f2687 KO |
2220 | err_allocate_write: |
2221 | if (!bch2_journal_error(j)) { | |
2222 | struct printbuf buf = PRINTBUF; | |
2223 | ||
2224 | bch2_journal_debug_to_text(&buf, j); | |
2225 | prt_printf(&buf, bch2_fmt(c, "Unable to allocate journal write at seq %llu for %zu sectors: %s"), | |
2226 | le64_to_cpu(w->data->seq), | |
2227 | vstruct_sectors(w->data, c->block_bits), | |
2228 | bch2_err_str(ret)); | |
2229 | bch2_print_str(c, KERN_ERR, buf.buf); | |
2230 | printbuf_exit(&buf); | |
2231 | } | |
1c6fdbd8 | 2232 | err: |
b74b147d | 2233 | bch2_fatal_error(c); |
7d4f2687 KO |
2234 | no_io: |
2235 | extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) { | |
2236 | struct bch_dev *ca = bch2_dev_have_ref(c, ptr->dev); | |
2237 | enumerated_ref_put(&ca->io_ref[WRITE], BCH_DEV_WRITE_REF_journal_write); | |
2238 | } | |
2239 | ||
656f05d8 | 2240 | continue_at(cl, journal_write_done, j->wq); |
1c6fdbd8 | 2241 | } |