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