Commit | Line | Data |
---|---|---|
1c6fdbd8 KO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #include "bcachefs.h" | |
7b3f84ea | 3 | #include "alloc_foreground.h" |
39fb2983 | 4 | #include "btree_io.h" |
00b8ccf7 | 5 | #include "btree_update_interior.h" |
1c6fdbd8 KO |
6 | #include "buckets.h" |
7 | #include "checksum.h" | |
d042b040 | 8 | #include "disk_groups.h" |
1c6fdbd8 | 9 | #include "error.h" |
63b214e7 | 10 | #include "io.h" |
1c6fdbd8 KO |
11 | #include "journal.h" |
12 | #include "journal_io.h" | |
13 | #include "journal_reclaim.h" | |
adbcada4 | 14 | #include "journal_seq_blacklist.h" |
1c6fdbd8 KO |
15 | #include "replicas.h" |
16 | #include "trace.h" | |
17 | ||
adbcada4 KO |
18 | static void __journal_replay_free(struct journal_replay *i) |
19 | { | |
20 | list_del(&i->list); | |
21 | kvpfree(i, offsetof(struct journal_replay, j) + | |
22 | vstruct_bytes(&i->j)); | |
23 | ||
24 | } | |
25 | ||
26 | static void journal_replay_free(struct bch_fs *c, struct journal_replay *i) | |
27 | { | |
28 | i->ignore = true; | |
29 | ||
30 | if (!c->opts.read_entire_journal) | |
31 | __journal_replay_free(i); | |
32 | } | |
33 | ||
1c6fdbd8 KO |
34 | struct journal_list { |
35 | struct closure cl; | |
36 | struct mutex lock; | |
37 | struct list_head *head; | |
38 | int ret; | |
39 | }; | |
40 | ||
41 | #define JOURNAL_ENTRY_ADD_OK 0 | |
42 | #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE 5 | |
43 | ||
44 | /* | |
45 | * Given a journal entry we just read, add it to the list of journal entries to | |
46 | * be replayed: | |
47 | */ | |
48 | static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca, | |
e4c3f386 | 49 | struct bch_extent_ptr entry_ptr, |
ca73852a KO |
50 | struct journal_list *jlist, struct jset *j, |
51 | bool bad) | |
1c6fdbd8 | 52 | { |
e4c3f386 KO |
53 | struct journal_replay *i, *pos, *dup = NULL; |
54 | struct bch_extent_ptr *ptr; | |
1c6fdbd8 KO |
55 | struct list_head *where; |
56 | size_t bytes = vstruct_bytes(j); | |
adbcada4 | 57 | u64 last_seq = 0; |
e4c3f386 | 58 | int ret = JOURNAL_ENTRY_ADD_OK; |
1c6fdbd8 | 59 | |
adbcada4 KO |
60 | list_for_each_entry_reverse(i, jlist->head, list) { |
61 | if (!JSET_NO_FLUSH(&i->j)) { | |
62 | last_seq = le64_to_cpu(i->j.last_seq); | |
63 | break; | |
7fffc85b | 64 | } |
adbcada4 | 65 | } |
1c6fdbd8 | 66 | |
adbcada4 KO |
67 | /* Is this entry older than the range we need? */ |
68 | if (!c->opts.read_entire_journal && | |
69 | le64_to_cpu(j->seq) < last_seq) { | |
70 | ret = JOURNAL_ENTRY_ADD_OUT_OF_RANGE; | |
71 | goto out; | |
72 | } | |
73 | ||
74 | /* Drop entries we don't need anymore */ | |
75 | if (!JSET_NO_FLUSH(j)) { | |
7fffc85b KO |
76 | list_for_each_entry_safe(i, pos, jlist->head, list) { |
77 | if (le64_to_cpu(i->j.seq) >= le64_to_cpu(j->last_seq)) | |
78 | break; | |
adbcada4 | 79 | journal_replay_free(c, i); |
7fffc85b | 80 | } |
1c6fdbd8 KO |
81 | } |
82 | ||
83 | list_for_each_entry_reverse(i, jlist->head, list) { | |
ca73852a KO |
84 | if (le64_to_cpu(j->seq) > le64_to_cpu(i->j.seq)) { |
85 | where = &i->list; | |
86 | goto add; | |
87 | } | |
88 | } | |
89 | ||
90 | where = jlist->head; | |
91 | add: | |
e4c3f386 | 92 | dup = where->next != jlist->head |
ca73852a KO |
93 | ? container_of(where->next, struct journal_replay, list) |
94 | : NULL; | |
95 | ||
e4c3f386 KO |
96 | if (dup && le64_to_cpu(j->seq) != le64_to_cpu(dup->j.seq)) |
97 | dup = NULL; | |
98 | ||
ca73852a KO |
99 | /* |
100 | * Duplicate journal entries? If so we want the one that didn't have a | |
101 | * checksum error: | |
102 | */ | |
e4c3f386 KO |
103 | if (dup) { |
104 | if (dup->bad) { | |
105 | /* we'll replace @dup: */ | |
ca73852a | 106 | } else if (bad) { |
e4c3f386 | 107 | i = dup; |
ca73852a KO |
108 | goto found; |
109 | } else { | |
e4c3f386 KO |
110 | fsck_err_on(bytes != vstruct_bytes(&dup->j) || |
111 | memcmp(j, &dup->j, bytes), c, | |
1c6fdbd8 KO |
112 | "found duplicate but non identical journal entries (seq %llu)", |
113 | le64_to_cpu(j->seq)); | |
e4c3f386 | 114 | i = dup; |
1c6fdbd8 KO |
115 | goto found; |
116 | } | |
1c6fdbd8 KO |
117 | } |
118 | ||
1c6fdbd8 KO |
119 | i = kvpmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL); |
120 | if (!i) { | |
121 | ret = -ENOMEM; | |
122 | goto out; | |
123 | } | |
124 | ||
e4c3f386 KO |
125 | i->nr_ptrs = 0; |
126 | i->bad = bad; | |
127 | i->ignore = false; | |
1c6fdbd8 | 128 | unsafe_memcpy(&i->j, j, bytes, "embedded variable length struct"); |
e4c3f386 KO |
129 | |
130 | if (dup) { | |
131 | i->nr_ptrs = dup->nr_ptrs; | |
132 | memcpy(i->ptrs, dup->ptrs, sizeof(dup->ptrs)); | |
133 | __journal_replay_free(dup); | |
134 | } | |
135 | ||
136 | list_add(&i->list, where); | |
1c6fdbd8 | 137 | found: |
e4c3f386 KO |
138 | for (ptr = i->ptrs; ptr < i->ptrs + i->nr_ptrs; ptr++) { |
139 | if (ptr->dev == ca->dev_idx) { | |
140 | bch_err(c, "duplicate journal entry %llu on same device", | |
141 | le64_to_cpu(i->j.seq)); | |
142 | goto out; | |
143 | } | |
144 | } | |
145 | ||
146 | if (i->nr_ptrs >= ARRAY_SIZE(i->ptrs)) { | |
147 | bch_err(c, "found too many copies of journal entry %llu", | |
148 | le64_to_cpu(i->j.seq)); | |
149 | goto out; | |
150 | } | |
151 | ||
152 | i->ptrs[i->nr_ptrs++] = entry_ptr; | |
1c6fdbd8 KO |
153 | out: |
154 | fsck_err: | |
155 | return ret; | |
156 | } | |
157 | ||
158 | static struct nonce journal_nonce(const struct jset *jset) | |
159 | { | |
160 | return (struct nonce) {{ | |
161 | [0] = 0, | |
162 | [1] = ((__le32 *) &jset->seq)[0], | |
163 | [2] = ((__le32 *) &jset->seq)[1], | |
164 | [3] = BCH_NONCE_JOURNAL, | |
165 | }}; | |
166 | } | |
167 | ||
168 | /* this fills in a range with empty jset_entries: */ | |
169 | static void journal_entry_null_range(void *start, void *end) | |
170 | { | |
171 | struct jset_entry *entry; | |
172 | ||
173 | for (entry = start; entry != end; entry = vstruct_next(entry)) | |
174 | memset(entry, 0, sizeof(*entry)); | |
175 | } | |
176 | ||
177 | #define JOURNAL_ENTRY_REREAD 5 | |
178 | #define JOURNAL_ENTRY_NONE 6 | |
179 | #define JOURNAL_ENTRY_BAD 7 | |
180 | ||
181 | #define journal_entry_err(c, msg, ...) \ | |
182 | ({ \ | |
183 | switch (write) { \ | |
184 | case READ: \ | |
185 | mustfix_fsck_err(c, msg, ##__VA_ARGS__); \ | |
186 | break; \ | |
187 | case WRITE: \ | |
188 | bch_err(c, "corrupt metadata before write:\n" \ | |
189 | msg, ##__VA_ARGS__); \ | |
190 | if (bch2_fs_inconsistent(c)) { \ | |
191 | ret = BCH_FSCK_ERRORS_NOT_FIXED; \ | |
192 | goto fsck_err; \ | |
193 | } \ | |
194 | break; \ | |
195 | } \ | |
196 | true; \ | |
197 | }) | |
198 | ||
199 | #define journal_entry_err_on(cond, c, msg, ...) \ | |
200 | ((cond) ? journal_entry_err(c, msg, ##__VA_ARGS__) : false) | |
201 | ||
4d54337c KO |
202 | #define FSCK_DELETED_KEY 5 |
203 | ||
1c6fdbd8 KO |
204 | static int journal_validate_key(struct bch_fs *c, struct jset *jset, |
205 | struct jset_entry *entry, | |
39fb2983 KO |
206 | unsigned level, enum btree_id btree_id, |
207 | struct bkey_i *k, | |
1c6fdbd8 KO |
208 | const char *type, int write) |
209 | { | |
210 | void *next = vstruct_next(entry); | |
211 | const char *invalid; | |
26609b61 | 212 | unsigned version = le32_to_cpu(jset->version); |
1c6fdbd8 KO |
213 | int ret = 0; |
214 | ||
215 | if (journal_entry_err_on(!k->k.u64s, c, | |
4d54337c | 216 | "invalid %s in jset %llu offset %zi/%u entry offset %zi/%u: k->u64s 0", |
ed0d631f | 217 | type, le64_to_cpu(jset->seq), |
4d54337c KO |
218 | (u64 *) entry - jset->_data, |
219 | le32_to_cpu(jset->u64s), | |
220 | (u64 *) k - entry->_data, | |
221 | le16_to_cpu(entry->u64s))) { | |
1c6fdbd8 KO |
222 | entry->u64s = cpu_to_le16((u64 *) k - entry->_data); |
223 | journal_entry_null_range(vstruct_next(entry), next); | |
4d54337c | 224 | return FSCK_DELETED_KEY; |
1c6fdbd8 KO |
225 | } |
226 | ||
227 | if (journal_entry_err_on((void *) bkey_next(k) > | |
228 | (void *) vstruct_next(entry), c, | |
4d54337c | 229 | "invalid %s in jset %llu offset %zi/%u entry offset %zi/%u: extends past end of journal entry", |
ed0d631f | 230 | type, le64_to_cpu(jset->seq), |
4d54337c KO |
231 | (u64 *) entry - jset->_data, |
232 | le32_to_cpu(jset->u64s), | |
233 | (u64 *) k - entry->_data, | |
234 | le16_to_cpu(entry->u64s))) { | |
1c6fdbd8 KO |
235 | entry->u64s = cpu_to_le16((u64 *) k - entry->_data); |
236 | journal_entry_null_range(vstruct_next(entry), next); | |
4d54337c | 237 | return FSCK_DELETED_KEY; |
1c6fdbd8 KO |
238 | } |
239 | ||
240 | if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT, c, | |
4d54337c | 241 | "invalid %s in jset %llu offset %zi/%u entry offset %zi/%u: bad format %u", |
ed0d631f KO |
242 | type, le64_to_cpu(jset->seq), |
243 | (u64 *) entry - jset->_data, | |
4d54337c KO |
244 | le32_to_cpu(jset->u64s), |
245 | (u64 *) k - entry->_data, | |
246 | le16_to_cpu(entry->u64s), | |
ed0d631f | 247 | k->k.format)) { |
4d54337c | 248 | le16_add_cpu(&entry->u64s, -((u16) k->k.u64s)); |
1c6fdbd8 KO |
249 | memmove(k, bkey_next(k), next - (void *) bkey_next(k)); |
250 | journal_entry_null_range(vstruct_next(entry), next); | |
4d54337c | 251 | return FSCK_DELETED_KEY; |
1c6fdbd8 KO |
252 | } |
253 | ||
39fb2983 KO |
254 | if (!write) |
255 | bch2_bkey_compat(level, btree_id, version, | |
256 | JSET_BIG_ENDIAN(jset), write, | |
257 | NULL, bkey_to_packed(k)); | |
26609b61 | 258 | |
39fb2983 KO |
259 | invalid = bch2_bkey_invalid(c, bkey_i_to_s_c(k), |
260 | __btree_node_type(level, btree_id)); | |
1c6fdbd8 | 261 | if (invalid) { |
319f9ac3 KO |
262 | char buf[160]; |
263 | ||
26609b61 | 264 | bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(k)); |
4d54337c | 265 | mustfix_fsck_err(c, "invalid %s in jset %llu offset %zi/%u entry offset %zi/%u: %s\n%s", |
ed0d631f KO |
266 | type, le64_to_cpu(jset->seq), |
267 | (u64 *) entry - jset->_data, | |
4d54337c KO |
268 | le32_to_cpu(jset->u64s), |
269 | (u64 *) k - entry->_data, | |
270 | le16_to_cpu(entry->u64s), | |
ed0d631f | 271 | invalid, buf); |
1c6fdbd8 | 272 | |
4d54337c | 273 | le16_add_cpu(&entry->u64s, -((u16) k->k.u64s)); |
1c6fdbd8 KO |
274 | memmove(k, bkey_next(k), next - (void *) bkey_next(k)); |
275 | journal_entry_null_range(vstruct_next(entry), next); | |
4d54337c | 276 | return FSCK_DELETED_KEY; |
1c6fdbd8 | 277 | } |
26609b61 | 278 | |
39fb2983 KO |
279 | if (write) |
280 | bch2_bkey_compat(level, btree_id, version, | |
281 | JSET_BIG_ENDIAN(jset), write, | |
282 | NULL, bkey_to_packed(k)); | |
1c6fdbd8 KO |
283 | fsck_err: |
284 | return ret; | |
285 | } | |
286 | ||
287 | static int journal_entry_validate_btree_keys(struct bch_fs *c, | |
288 | struct jset *jset, | |
289 | struct jset_entry *entry, | |
290 | int write) | |
291 | { | |
4d54337c | 292 | struct bkey_i *k = entry->start; |
1c6fdbd8 | 293 | |
4d54337c | 294 | while (k != vstruct_last(entry)) { |
39fb2983 KO |
295 | int ret = journal_validate_key(c, jset, entry, |
296 | entry->level, | |
297 | entry->btree_id, | |
298 | k, "key", write); | |
4d54337c KO |
299 | if (ret == FSCK_DELETED_KEY) |
300 | continue; | |
301 | ||
302 | k = bkey_next(k); | |
1c6fdbd8 KO |
303 | } |
304 | ||
305 | return 0; | |
306 | } | |
307 | ||
308 | static int journal_entry_validate_btree_root(struct bch_fs *c, | |
309 | struct jset *jset, | |
310 | struct jset_entry *entry, | |
311 | int write) | |
312 | { | |
313 | struct bkey_i *k = entry->start; | |
314 | int ret = 0; | |
315 | ||
316 | if (journal_entry_err_on(!entry->u64s || | |
317 | le16_to_cpu(entry->u64s) != k->k.u64s, c, | |
318 | "invalid btree root journal entry: wrong number of keys")) { | |
319 | void *next = vstruct_next(entry); | |
320 | /* | |
321 | * we don't want to null out this jset_entry, | |
322 | * just the contents, so that later we can tell | |
323 | * we were _supposed_ to have a btree root | |
324 | */ | |
325 | entry->u64s = 0; | |
326 | journal_entry_null_range(vstruct_next(entry), next); | |
327 | return 0; | |
328 | } | |
329 | ||
39fb2983 | 330 | return journal_validate_key(c, jset, entry, 1, entry->btree_id, k, |
1c6fdbd8 KO |
331 | "btree root", write); |
332 | fsck_err: | |
333 | return ret; | |
334 | } | |
335 | ||
336 | static int journal_entry_validate_prio_ptrs(struct bch_fs *c, | |
337 | struct jset *jset, | |
338 | struct jset_entry *entry, | |
339 | int write) | |
340 | { | |
341 | /* obsolete, don't care: */ | |
342 | return 0; | |
343 | } | |
344 | ||
345 | static int journal_entry_validate_blacklist(struct bch_fs *c, | |
346 | struct jset *jset, | |
347 | struct jset_entry *entry, | |
348 | int write) | |
349 | { | |
350 | int ret = 0; | |
351 | ||
352 | if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1, c, | |
353 | "invalid journal seq blacklist entry: bad size")) { | |
354 | journal_entry_null_range(entry, vstruct_next(entry)); | |
355 | } | |
356 | fsck_err: | |
357 | return ret; | |
358 | } | |
359 | ||
360 | static int journal_entry_validate_blacklist_v2(struct bch_fs *c, | |
361 | struct jset *jset, | |
362 | struct jset_entry *entry, | |
363 | int write) | |
364 | { | |
365 | struct jset_entry_blacklist_v2 *bl_entry; | |
366 | int ret = 0; | |
367 | ||
368 | if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2, c, | |
369 | "invalid journal seq blacklist entry: bad size")) { | |
370 | journal_entry_null_range(entry, vstruct_next(entry)); | |
2c5af169 | 371 | goto out; |
1c6fdbd8 KO |
372 | } |
373 | ||
374 | bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry); | |
375 | ||
376 | if (journal_entry_err_on(le64_to_cpu(bl_entry->start) > | |
377 | le64_to_cpu(bl_entry->end), c, | |
378 | "invalid journal seq blacklist entry: start > end")) { | |
379 | journal_entry_null_range(entry, vstruct_next(entry)); | |
380 | } | |
2c5af169 KO |
381 | out: |
382 | fsck_err: | |
383 | return ret; | |
384 | } | |
385 | ||
386 | static int journal_entry_validate_usage(struct bch_fs *c, | |
387 | struct jset *jset, | |
388 | struct jset_entry *entry, | |
389 | int write) | |
390 | { | |
391 | struct jset_entry_usage *u = | |
392 | container_of(entry, struct jset_entry_usage, entry); | |
393 | unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64); | |
394 | int ret = 0; | |
395 | ||
3577df5f KO |
396 | if (journal_entry_err_on(bytes < sizeof(*u), |
397 | c, | |
398 | "invalid journal entry usage: bad size")) { | |
399 | journal_entry_null_range(entry, vstruct_next(entry)); | |
400 | return ret; | |
401 | } | |
402 | ||
403 | fsck_err: | |
404 | return ret; | |
405 | } | |
406 | ||
407 | static int journal_entry_validate_data_usage(struct bch_fs *c, | |
408 | struct jset *jset, | |
409 | struct jset_entry *entry, | |
410 | int write) | |
411 | { | |
412 | struct jset_entry_data_usage *u = | |
413 | container_of(entry, struct jset_entry_data_usage, entry); | |
414 | unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64); | |
415 | int ret = 0; | |
416 | ||
2c5af169 KO |
417 | if (journal_entry_err_on(bytes < sizeof(*u) || |
418 | bytes < sizeof(*u) + u->r.nr_devs, | |
419 | c, | |
420 | "invalid journal entry usage: bad size")) { | |
421 | journal_entry_null_range(entry, vstruct_next(entry)); | |
422 | return ret; | |
423 | } | |
1c6fdbd8 KO |
424 | |
425 | fsck_err: | |
426 | return ret; | |
427 | } | |
428 | ||
429 | struct jset_entry_ops { | |
430 | int (*validate)(struct bch_fs *, struct jset *, | |
431 | struct jset_entry *, int); | |
432 | }; | |
433 | ||
434 | static const struct jset_entry_ops bch2_jset_entry_ops[] = { | |
435 | #define x(f, nr) \ | |
436 | [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \ | |
437 | .validate = journal_entry_validate_##f, \ | |
438 | }, | |
439 | BCH_JSET_ENTRY_TYPES() | |
440 | #undef x | |
441 | }; | |
442 | ||
443 | static int journal_entry_validate(struct bch_fs *c, struct jset *jset, | |
444 | struct jset_entry *entry, int write) | |
445 | { | |
2c5af169 KO |
446 | return entry->type < BCH_JSET_ENTRY_NR |
447 | ? bch2_jset_entry_ops[entry->type].validate(c, jset, | |
448 | entry, write) | |
449 | : 0; | |
1c6fdbd8 KO |
450 | } |
451 | ||
452 | static int jset_validate_entries(struct bch_fs *c, struct jset *jset, | |
453 | int write) | |
454 | { | |
455 | struct jset_entry *entry; | |
456 | int ret = 0; | |
457 | ||
458 | vstruct_for_each(jset, entry) { | |
459 | if (journal_entry_err_on(vstruct_next(entry) > | |
460 | vstruct_last(jset), c, | |
461 | "journal entry extends past end of jset")) { | |
462 | jset->u64s = cpu_to_le32((u64 *) entry - jset->_data); | |
463 | break; | |
464 | } | |
465 | ||
466 | ret = journal_entry_validate(c, jset, entry, write); | |
467 | if (ret) | |
468 | break; | |
469 | } | |
470 | fsck_err: | |
471 | return ret; | |
472 | } | |
473 | ||
474 | static int jset_validate(struct bch_fs *c, | |
ca73852a | 475 | struct bch_dev *ca, |
1c6fdbd8 KO |
476 | struct jset *jset, u64 sector, |
477 | unsigned bucket_sectors_left, | |
478 | unsigned sectors_read, | |
479 | int write) | |
480 | { | |
481 | size_t bytes = vstruct_bytes(jset); | |
482 | struct bch_csum csum; | |
26609b61 | 483 | unsigned version; |
1c6fdbd8 KO |
484 | int ret = 0; |
485 | ||
486 | if (le64_to_cpu(jset->magic) != jset_magic(c)) | |
487 | return JOURNAL_ENTRY_NONE; | |
488 | ||
26609b61 | 489 | version = le32_to_cpu(jset->version); |
ca73852a KO |
490 | if (journal_entry_err_on((version != BCH_JSET_VERSION_OLD && |
491 | version < bcachefs_metadata_version_min) || | |
492 | version >= bcachefs_metadata_version_max, c, | |
493 | "%s sector %llu seq %llu: unknown journal entry version %u", | |
ed9d58a2 KO |
494 | ca ? ca->name : c->name, |
495 | sector, le64_to_cpu(jset->seq), | |
ca73852a | 496 | version)) { |
35ef6df5 KO |
497 | /* don't try to continue: */ |
498 | return EINVAL; | |
1c6fdbd8 KO |
499 | } |
500 | ||
35ef6df5 KO |
501 | if (bytes > (sectors_read << 9) && |
502 | sectors_read < bucket_sectors_left) | |
503 | return JOURNAL_ENTRY_REREAD; | |
504 | ||
1c6fdbd8 | 505 | if (journal_entry_err_on(bytes > bucket_sectors_left << 9, c, |
ca73852a | 506 | "%s sector %llu seq %llu: journal entry too big (%zu bytes)", |
ed9d58a2 KO |
507 | ca ? ca->name : c->name, |
508 | sector, le64_to_cpu(jset->seq), bytes)) { | |
35ef6df5 KO |
509 | ret = JOURNAL_ENTRY_BAD; |
510 | le32_add_cpu(&jset->u64s, | |
511 | -((bytes - (bucket_sectors_left << 9)) / 8)); | |
1c6fdbd8 KO |
512 | } |
513 | ||
ed9d58a2 | 514 | if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)), c, |
ca73852a | 515 | "%s sector %llu seq %llu: journal entry with unknown csum type %llu", |
ed9d58a2 KO |
516 | ca ? ca->name : c->name, |
517 | sector, le64_to_cpu(jset->seq), | |
35ef6df5 KO |
518 | JSET_CSUM_TYPE(jset))) { |
519 | ret = JOURNAL_ENTRY_BAD; | |
ed9d58a2 | 520 | goto csum_done; |
35ef6df5 | 521 | } |
1c6fdbd8 | 522 | |
ed9d58a2 KO |
523 | if (write) |
524 | goto csum_done; | |
525 | ||
1c6fdbd8 KO |
526 | csum = csum_vstruct(c, JSET_CSUM_TYPE(jset), journal_nonce(jset), jset); |
527 | if (journal_entry_err_on(bch2_crc_cmp(csum, jset->csum), c, | |
ca73852a | 528 | "%s sector %llu seq %llu: journal checksum bad", |
ed9d58a2 KO |
529 | ca ? ca->name : c->name, |
530 | sector, le64_to_cpu(jset->seq))) | |
35ef6df5 | 531 | ret = JOURNAL_ENTRY_BAD; |
1c6fdbd8 KO |
532 | |
533 | bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset), | |
534 | jset->encrypted_start, | |
535 | vstruct_end(jset) - (void *) jset->encrypted_start); | |
ed9d58a2 KO |
536 | csum_done: |
537 | /* last_seq is ignored when JSET_NO_FLUSH is true */ | |
538 | if (journal_entry_err_on(!JSET_NO_FLUSH(jset) && | |
539 | le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq), c, | |
540 | "invalid journal entry: last_seq > seq (%llu > %llu)", | |
541 | le64_to_cpu(jset->last_seq), | |
542 | le64_to_cpu(jset->seq))) { | |
1c6fdbd8 | 543 | jset->last_seq = jset->seq; |
ca73852a KO |
544 | return JOURNAL_ENTRY_BAD; |
545 | } | |
1c6fdbd8 KO |
546 | fsck_err: |
547 | return ret; | |
548 | } | |
549 | ||
ed9d58a2 KO |
550 | static int jset_validate_for_write(struct bch_fs *c, struct jset *jset) |
551 | { | |
552 | unsigned sectors = vstruct_sectors(jset, c->block_bits); | |
553 | ||
554 | return jset_validate(c, NULL, jset, 0, sectors, sectors, WRITE) ?: | |
555 | jset_validate_entries(c, jset, WRITE); | |
556 | } | |
557 | ||
1c6fdbd8 KO |
558 | struct journal_read_buf { |
559 | void *data; | |
560 | size_t size; | |
561 | }; | |
562 | ||
563 | static int journal_read_buf_realloc(struct journal_read_buf *b, | |
564 | size_t new_size) | |
565 | { | |
566 | void *n; | |
567 | ||
568 | /* the bios are sized for this many pages, max: */ | |
569 | if (new_size > JOURNAL_ENTRY_SIZE_MAX) | |
570 | return -ENOMEM; | |
571 | ||
572 | new_size = roundup_pow_of_two(new_size); | |
573 | n = kvpmalloc(new_size, GFP_KERNEL); | |
574 | if (!n) | |
575 | return -ENOMEM; | |
576 | ||
577 | kvpfree(b->data, b->size); | |
578 | b->data = n; | |
579 | b->size = new_size; | |
580 | return 0; | |
581 | } | |
582 | ||
583 | static int journal_read_bucket(struct bch_dev *ca, | |
584 | struct journal_read_buf *buf, | |
585 | struct journal_list *jlist, | |
a9ec3454 | 586 | unsigned bucket) |
1c6fdbd8 KO |
587 | { |
588 | struct bch_fs *c = ca->fs; | |
589 | struct journal_device *ja = &ca->journal; | |
1c6fdbd8 KO |
590 | struct jset *j = NULL; |
591 | unsigned sectors, sectors_read = 0; | |
592 | u64 offset = bucket_to_sector(ca, ja->buckets[bucket]), | |
593 | end = offset + ca->mi.bucket_size; | |
594 | bool saw_bad = false; | |
595 | int ret = 0; | |
596 | ||
597 | pr_debug("reading %u", bucket); | |
598 | ||
599 | while (offset < end) { | |
600 | if (!sectors_read) { | |
ac10a961 KO |
601 | struct bio *bio; |
602 | unsigned nr_bvecs; | |
603 | reread: | |
604 | sectors_read = min_t(unsigned, | |
1c6fdbd8 | 605 | end - offset, buf->size >> 9); |
ac10a961 KO |
606 | nr_bvecs = buf_pages(buf->data, sectors_read << 9); |
607 | ||
608 | bio = bio_kmalloc(nr_bvecs, GFP_KERNEL); | |
609 | bio_init(bio, ca->disk_sb.bdev, bio->bi_inline_vecs, nr_bvecs, REQ_OP_READ); | |
1c6fdbd8 | 610 | |
885678f6 KO |
611 | bio->bi_iter.bi_sector = offset; |
612 | bch2_bio_map(bio, buf->data, sectors_read << 9); | |
1c6fdbd8 KO |
613 | |
614 | ret = submit_bio_wait(bio); | |
ac10a961 | 615 | kfree(bio); |
1c6fdbd8 KO |
616 | |
617 | if (bch2_dev_io_err_on(ret, ca, | |
0fefe8d8 | 618 | "journal read error: sector %llu", |
1c6fdbd8 | 619 | offset) || |
29d90f61 KO |
620 | bch2_meta_read_fault("journal")) { |
621 | /* | |
622 | * We don't error out of the recovery process | |
623 | * here, since the relevant journal entry may be | |
624 | * found on a different device, and missing or | |
625 | * no journal entries will be handled later | |
626 | */ | |
627 | return 0; | |
628 | } | |
1c6fdbd8 KO |
629 | |
630 | j = buf->data; | |
631 | } | |
632 | ||
ca73852a | 633 | ret = jset_validate(c, ca, j, offset, |
1c6fdbd8 KO |
634 | end - offset, sectors_read, |
635 | READ); | |
636 | switch (ret) { | |
637 | case BCH_FSCK_OK: | |
ca73852a | 638 | sectors = vstruct_sectors(j, c->block_bits); |
1c6fdbd8 KO |
639 | break; |
640 | case JOURNAL_ENTRY_REREAD: | |
641 | if (vstruct_bytes(j) > buf->size) { | |
642 | ret = journal_read_buf_realloc(buf, | |
643 | vstruct_bytes(j)); | |
644 | if (ret) | |
645 | return ret; | |
646 | } | |
647 | goto reread; | |
648 | case JOURNAL_ENTRY_NONE: | |
649 | if (!saw_bad) | |
650 | return 0; | |
651 | sectors = c->opts.block_size; | |
652 | goto next_block; | |
653 | case JOURNAL_ENTRY_BAD: | |
654 | saw_bad = true; | |
ca73852a KO |
655 | /* |
656 | * On checksum error we don't really trust the size | |
657 | * field of the journal entry we read, so try reading | |
658 | * again at next block boundary: | |
659 | */ | |
1c6fdbd8 | 660 | sectors = c->opts.block_size; |
ca73852a | 661 | break; |
1c6fdbd8 KO |
662 | default: |
663 | return ret; | |
664 | } | |
665 | ||
666 | /* | |
667 | * This happens sometimes if we don't have discards on - | |
668 | * when we've partially overwritten a bucket with new | |
669 | * journal entries. We don't need the rest of the | |
670 | * bucket: | |
671 | */ | |
672 | if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket]) | |
673 | return 0; | |
674 | ||
675 | ja->bucket_seq[bucket] = le64_to_cpu(j->seq); | |
676 | ||
677 | mutex_lock(&jlist->lock); | |
e4c3f386 KO |
678 | ret = journal_entry_add(c, ca, (struct bch_extent_ptr) { |
679 | .dev = ca->dev_idx, | |
680 | .offset = offset, | |
681 | }, jlist, j, ret != 0); | |
1c6fdbd8 KO |
682 | mutex_unlock(&jlist->lock); |
683 | ||
684 | switch (ret) { | |
685 | case JOURNAL_ENTRY_ADD_OK: | |
1c6fdbd8 KO |
686 | break; |
687 | case JOURNAL_ENTRY_ADD_OUT_OF_RANGE: | |
688 | break; | |
689 | default: | |
690 | return ret; | |
691 | } | |
1c6fdbd8 KO |
692 | next_block: |
693 | pr_debug("next"); | |
694 | offset += sectors; | |
695 | sectors_read -= sectors; | |
696 | j = ((void *) j) + (sectors << 9); | |
697 | } | |
698 | ||
699 | return 0; | |
700 | } | |
701 | ||
702 | static void bch2_journal_read_device(struct closure *cl) | |
703 | { | |
1c6fdbd8 KO |
704 | struct journal_device *ja = |
705 | container_of(cl, struct journal_device, read); | |
706 | struct bch_dev *ca = container_of(ja, struct bch_dev, journal); | |
707 | struct journal_list *jlist = | |
708 | container_of(cl->parent, struct journal_list, cl); | |
1c6fdbd8 | 709 | struct journal_read_buf buf = { NULL, 0 }; |
a9ec3454 KO |
710 | u64 min_seq = U64_MAX; |
711 | unsigned i; | |
1c6fdbd8 KO |
712 | int ret; |
713 | ||
714 | if (!ja->nr) | |
715 | goto out; | |
716 | ||
1c6fdbd8 KO |
717 | ret = journal_read_buf_realloc(&buf, PAGE_SIZE); |
718 | if (ret) | |
719 | goto err; | |
720 | ||
721 | pr_debug("%u journal buckets", ja->nr); | |
722 | ||
1c6fdbd8 | 723 | for (i = 0; i < ja->nr; i++) { |
a9ec3454 KO |
724 | ret = journal_read_bucket(ca, &buf, jlist, i); |
725 | if (ret) | |
726 | goto err; | |
1c6fdbd8 KO |
727 | } |
728 | ||
a9ec3454 KO |
729 | /* Find the journal bucket with the highest sequence number: */ |
730 | for (i = 0; i < ja->nr; i++) { | |
731 | if (ja->bucket_seq[i] > ja->bucket_seq[ja->cur_idx]) | |
732 | ja->cur_idx = i; | |
1c6fdbd8 | 733 | |
a9ec3454 | 734 | min_seq = min(ja->bucket_seq[i], min_seq); |
1c6fdbd8 KO |
735 | } |
736 | ||
1c6fdbd8 | 737 | /* |
1c6fdbd8 KO |
738 | * If there's duplicate journal entries in multiple buckets (which |
739 | * definitely isn't supposed to happen, but...) - make sure to start | |
740 | * cur_idx at the last of those buckets, so we don't deadlock trying to | |
741 | * allocate | |
742 | */ | |
a9ec3454 KO |
743 | while (ja->bucket_seq[ja->cur_idx] > min_seq && |
744 | ja->bucket_seq[ja->cur_idx] > | |
745 | ja->bucket_seq[(ja->cur_idx + 1) % ja->nr]) | |
a36d3685 | 746 | ja->cur_idx = (ja->cur_idx + 1) % ja->nr; |
a9ec3454 KO |
747 | |
748 | ja->sectors_free = 0; | |
1c6fdbd8 KO |
749 | |
750 | /* | |
0ce2dbbe | 751 | * Set dirty_idx to indicate the entire journal is full and needs to be |
1c6fdbd8 KO |
752 | * reclaimed - journal reclaim will immediately reclaim whatever isn't |
753 | * pinned when it first runs: | |
754 | */ | |
0ce2dbbe KO |
755 | ja->discard_idx = ja->dirty_idx_ondisk = |
756 | ja->dirty_idx = (ja->cur_idx + 1) % ja->nr; | |
1c6fdbd8 KO |
757 | out: |
758 | kvpfree(buf.data, buf.size); | |
1c6fdbd8 KO |
759 | percpu_ref_put(&ca->io_ref); |
760 | closure_return(cl); | |
761 | return; | |
762 | err: | |
763 | mutex_lock(&jlist->lock); | |
764 | jlist->ret = ret; | |
765 | mutex_unlock(&jlist->lock); | |
766 | goto out; | |
1c6fdbd8 KO |
767 | } |
768 | ||
e4c3f386 KO |
769 | static void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c, |
770 | struct journal_replay *j) | |
771 | { | |
772 | unsigned i; | |
773 | ||
774 | for (i = 0; i < j->nr_ptrs; i++) { | |
775 | struct bch_dev *ca = c->devs[j->ptrs[i].dev]; | |
776 | ||
777 | if (i) | |
778 | pr_buf(out, " "); | |
779 | pr_buf(out, "%u:%llu (offset %llu)", | |
780 | j->ptrs[i].dev, | |
781 | (u64) j->ptrs[i].offset, | |
782 | (u64) j->ptrs[i].offset % ca->mi.bucket_size); | |
783 | } | |
784 | } | |
785 | ||
adbcada4 KO |
786 | int bch2_journal_read(struct bch_fs *c, struct list_head *list, |
787 | u64 *blacklist_seq, u64 *start_seq) | |
1c6fdbd8 | 788 | { |
1c6fdbd8 | 789 | struct journal_list jlist; |
adbcada4 | 790 | struct journal_replay *i, *t; |
1c6fdbd8 | 791 | struct bch_dev *ca; |
1c6fdbd8 KO |
792 | unsigned iter; |
793 | size_t keys = 0, entries = 0; | |
794 | bool degraded = false; | |
adbcada4 | 795 | u64 seq, last_seq = 0; |
1c6fdbd8 KO |
796 | int ret = 0; |
797 | ||
798 | closure_init_stack(&jlist.cl); | |
799 | mutex_init(&jlist.lock); | |
800 | jlist.head = list; | |
801 | jlist.ret = 0; | |
802 | ||
803 | for_each_member_device(ca, c, iter) { | |
6bdbfa87 | 804 | if (!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) && |
89fd25be | 805 | !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal))) |
1c6fdbd8 KO |
806 | continue; |
807 | ||
808 | if ((ca->mi.state == BCH_MEMBER_STATE_RW || | |
809 | ca->mi.state == BCH_MEMBER_STATE_RO) && | |
810 | percpu_ref_tryget(&ca->io_ref)) | |
811 | closure_call(&ca->journal.read, | |
812 | bch2_journal_read_device, | |
813 | system_unbound_wq, | |
814 | &jlist.cl); | |
815 | else | |
816 | degraded = true; | |
817 | } | |
818 | ||
819 | closure_sync(&jlist.cl); | |
820 | ||
821 | if (jlist.ret) | |
822 | return jlist.ret; | |
823 | ||
adbcada4 KO |
824 | if (list_empty(list)) { |
825 | bch_info(c, "journal read done, but no entries found"); | |
826 | return 0; | |
827 | } | |
828 | ||
829 | i = list_last_entry(list, struct journal_replay, list); | |
830 | *start_seq = le64_to_cpu(i->j.seq) + 1; | |
831 | ||
832 | /* | |
833 | * Find most recent flush entry, and ignore newer non flush entries - | |
834 | * those entries will be blacklisted: | |
835 | */ | |
836 | list_for_each_entry_safe_reverse(i, t, list, list) { | |
837 | if (i->ignore) | |
838 | continue; | |
839 | ||
840 | if (!JSET_NO_FLUSH(&i->j)) { | |
841 | last_seq = le64_to_cpu(i->j.last_seq); | |
842 | *blacklist_seq = le64_to_cpu(i->j.seq) + 1; | |
843 | break; | |
844 | } | |
845 | ||
846 | journal_replay_free(c, i); | |
847 | } | |
848 | ||
849 | if (!last_seq) { | |
850 | fsck_err(c, "journal read done, but no entries found after dropping non-flushes"); | |
851 | return -1; | |
852 | } | |
853 | ||
854 | /* Drop blacklisted entries and entries older than last_seq: */ | |
855 | list_for_each_entry_safe(i, t, list, list) { | |
856 | if (i->ignore) | |
857 | continue; | |
858 | ||
859 | seq = le64_to_cpu(i->j.seq); | |
860 | if (seq < last_seq) { | |
861 | journal_replay_free(c, i); | |
862 | continue; | |
863 | } | |
864 | ||
865 | if (bch2_journal_seq_is_blacklisted(c, seq, true)) { | |
866 | fsck_err_on(!JSET_NO_FLUSH(&i->j), c, | |
867 | "found blacklisted journal entry %llu", seq); | |
868 | ||
869 | journal_replay_free(c, i); | |
870 | } | |
871 | } | |
872 | ||
873 | /* Check for missing entries: */ | |
874 | seq = last_seq; | |
875 | list_for_each_entry(i, list, list) { | |
876 | if (i->ignore) | |
877 | continue; | |
878 | ||
879 | BUG_ON(seq > le64_to_cpu(i->j.seq)); | |
880 | ||
881 | while (seq < le64_to_cpu(i->j.seq)) { | |
882 | u64 missing_start, missing_end; | |
e4c3f386 | 883 | char buf1[200], buf2[200]; |
adbcada4 KO |
884 | |
885 | while (seq < le64_to_cpu(i->j.seq) && | |
886 | bch2_journal_seq_is_blacklisted(c, seq, false)) | |
887 | seq++; | |
888 | ||
889 | if (seq == le64_to_cpu(i->j.seq)) | |
890 | break; | |
891 | ||
892 | missing_start = seq; | |
893 | ||
894 | while (seq < le64_to_cpu(i->j.seq) && | |
895 | !bch2_journal_seq_is_blacklisted(c, seq, false)) | |
896 | seq++; | |
897 | ||
e4c3f386 KO |
898 | if (i->list.prev != list) { |
899 | struct printbuf out = PBUF(buf1); | |
900 | struct journal_replay *p = list_prev_entry(i, list); | |
901 | ||
902 | bch2_journal_ptrs_to_text(&out, c, p); | |
903 | pr_buf(&out, " size %llu", vstruct_sectors(&p->j, c->block_bits)); | |
904 | } else | |
905 | sprintf(buf1, "(none)"); | |
906 | bch2_journal_ptrs_to_text(&PBUF(buf2), c, i); | |
907 | ||
adbcada4 | 908 | missing_end = seq - 1; |
e4c3f386 KO |
909 | fsck_err(c, "journal entries %llu-%llu missing! (replaying %llu-%llu)\n" |
910 | " prev at %s\n" | |
911 | " next at %s", | |
adbcada4 | 912 | missing_start, missing_end, |
e4c3f386 KO |
913 | last_seq, *blacklist_seq - 1, |
914 | buf1, buf2); | |
adbcada4 KO |
915 | } |
916 | ||
917 | seq++; | |
918 | } | |
919 | ||
1c6fdbd8 | 920 | list_for_each_entry(i, list, list) { |
1dd7f9d9 KO |
921 | struct jset_entry *entry; |
922 | struct bkey_i *k, *_n; | |
e4c3f386 KO |
923 | struct bch_replicas_padded replicas = { |
924 | .e.data_type = BCH_DATA_journal, | |
925 | .e.nr_required = 1, | |
926 | }; | |
927 | unsigned ptr; | |
7ef2a73a KO |
928 | char buf[80]; |
929 | ||
adbcada4 KO |
930 | if (i->ignore) |
931 | continue; | |
932 | ||
1c6fdbd8 KO |
933 | ret = jset_validate_entries(c, &i->j, READ); |
934 | if (ret) | |
935 | goto fsck_err; | |
936 | ||
e4c3f386 KO |
937 | for (ptr = 0; ptr < i->nr_ptrs; ptr++) |
938 | replicas.e.devs[replicas.e.nr_devs++] = i->ptrs[ptr].dev; | |
939 | ||
26452d1d KO |
940 | bch2_replicas_entry_sort(&replicas.e); |
941 | ||
1c6fdbd8 KO |
942 | /* |
943 | * If we're mounting in degraded mode - if we didn't read all | |
944 | * the devices - this is wrong: | |
945 | */ | |
946 | ||
947 | if (!degraded && | |
948 | (test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) || | |
988e98cf | 949 | fsck_err_on(!bch2_replicas_marked(c, &replicas.e), c, |
7ef2a73a KO |
950 | "superblock not marked as containing replicas %s", |
951 | (bch2_replicas_entry_to_text(&PBUF(buf), | |
952 | &replicas.e), buf)))) { | |
953 | ret = bch2_mark_replicas(c, &replicas.e); | |
1c6fdbd8 KO |
954 | if (ret) |
955 | return ret; | |
956 | } | |
1c6fdbd8 KO |
957 | |
958 | for_each_jset_key(k, _n, entry, &i->j) | |
959 | keys++; | |
960 | entries++; | |
961 | } | |
962 | ||
adbcada4 KO |
963 | bch_info(c, "journal read done, %zu keys in %zu entries, seq %llu", |
964 | keys, entries, *start_seq); | |
1dd7f9d9 | 965 | |
adbcada4 KO |
966 | if (*start_seq != *blacklist_seq) |
967 | bch_info(c, "dropped unflushed entries %llu-%llu", | |
968 | *blacklist_seq, *start_seq - 1); | |
1c6fdbd8 KO |
969 | fsck_err: |
970 | return ret; | |
971 | } | |
972 | ||
1c6fdbd8 KO |
973 | /* journal write: */ |
974 | ||
a9ec3454 KO |
975 | static void __journal_write_alloc(struct journal *j, |
976 | struct journal_buf *w, | |
977 | struct dev_alloc_list *devs_sorted, | |
978 | unsigned sectors, | |
979 | unsigned *replicas, | |
980 | unsigned replicas_want) | |
1c6fdbd8 KO |
981 | { |
982 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
1c6fdbd8 KO |
983 | struct journal_device *ja; |
984 | struct bch_dev *ca; | |
a9ec3454 | 985 | unsigned i; |
a2753581 | 986 | |
a9ec3454 KO |
987 | if (*replicas >= replicas_want) |
988 | return; | |
1c6fdbd8 | 989 | |
a9ec3454 KO |
990 | for (i = 0; i < devs_sorted->nr; i++) { |
991 | ca = rcu_dereference(c->devs[devs_sorted->devs[i]]); | |
1c6fdbd8 KO |
992 | if (!ca) |
993 | continue; | |
994 | ||
1c6fdbd8 | 995 | ja = &ca->journal; |
1c6fdbd8 KO |
996 | |
997 | /* | |
998 | * Check that we can use this device, and aren't already using | |
999 | * it: | |
1000 | */ | |
a9ec3454 KO |
1001 | if (!ca->mi.durability || |
1002 | ca->mi.state != BCH_MEMBER_STATE_RW || | |
1003 | !ja->nr || | |
26609b61 KO |
1004 | bch2_bkey_has_device(bkey_i_to_s_c(&w->key), |
1005 | ca->dev_idx) || | |
a9ec3454 | 1006 | sectors > ja->sectors_free) |
1c6fdbd8 KO |
1007 | continue; |
1008 | ||
3d080aa5 | 1009 | bch2_dev_stripe_increment(ca, &j->wp.stripe); |
1c6fdbd8 | 1010 | |
26609b61 | 1011 | bch2_bkey_append_ptr(&w->key, |
1c6fdbd8 KO |
1012 | (struct bch_extent_ptr) { |
1013 | .offset = bucket_to_sector(ca, | |
a9ec3454 KO |
1014 | ja->buckets[ja->cur_idx]) + |
1015 | ca->mi.bucket_size - | |
1016 | ja->sectors_free, | |
1c6fdbd8 KO |
1017 | .dev = ca->dev_idx, |
1018 | }); | |
1019 | ||
a9ec3454 KO |
1020 | ja->sectors_free -= sectors; |
1021 | ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq); | |
1022 | ||
1023 | *replicas += ca->mi.durability; | |
1024 | ||
1025 | if (*replicas >= replicas_want) | |
1026 | break; | |
1c6fdbd8 | 1027 | } |
a9ec3454 | 1028 | } |
1c6fdbd8 | 1029 | |
a9ec3454 KO |
1030 | /** |
1031 | * journal_next_bucket - move on to the next journal bucket if possible | |
1032 | */ | |
1033 | static int journal_write_alloc(struct journal *j, struct journal_buf *w, | |
1034 | unsigned sectors) | |
1035 | { | |
1036 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
d042b040 | 1037 | struct bch_devs_mask devs; |
a9ec3454 KO |
1038 | struct journal_device *ja; |
1039 | struct bch_dev *ca; | |
1040 | struct dev_alloc_list devs_sorted; | |
d042b040 KO |
1041 | unsigned target = c->opts.metadata_target ?: |
1042 | c->opts.foreground_target; | |
a9ec3454 KO |
1043 | unsigned i, replicas = 0, replicas_want = |
1044 | READ_ONCE(c->opts.metadata_replicas); | |
1c6fdbd8 | 1045 | |
a9ec3454 | 1046 | rcu_read_lock(); |
d042b040 KO |
1047 | retry: |
1048 | devs = target_rw_devs(c, BCH_DATA_journal, target); | |
1c6fdbd8 | 1049 | |
d042b040 | 1050 | devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs); |
1c6fdbd8 | 1051 | |
a9ec3454 KO |
1052 | __journal_write_alloc(j, w, &devs_sorted, |
1053 | sectors, &replicas, replicas_want); | |
1c6fdbd8 | 1054 | |
a9ec3454 KO |
1055 | if (replicas >= replicas_want) |
1056 | goto done; | |
1057 | ||
1058 | for (i = 0; i < devs_sorted.nr; i++) { | |
1059 | ca = rcu_dereference(c->devs[devs_sorted.devs[i]]); | |
1060 | if (!ca) | |
1061 | continue; | |
1062 | ||
1063 | ja = &ca->journal; | |
1064 | ||
1065 | if (sectors > ja->sectors_free && | |
1066 | sectors <= ca->mi.bucket_size && | |
03d5eaed KO |
1067 | bch2_journal_dev_buckets_available(j, ja, |
1068 | journal_space_discarded)) { | |
a9ec3454 KO |
1069 | ja->cur_idx = (ja->cur_idx + 1) % ja->nr; |
1070 | ja->sectors_free = ca->mi.bucket_size; | |
68ef94a6 KO |
1071 | |
1072 | /* | |
1073 | * ja->bucket_seq[ja->cur_idx] must always have | |
1074 | * something sensible: | |
1075 | */ | |
1076 | ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq); | |
a9ec3454 KO |
1077 | } |
1078 | } | |
1079 | ||
1080 | __journal_write_alloc(j, w, &devs_sorted, | |
1081 | sectors, &replicas, replicas_want); | |
d042b040 KO |
1082 | |
1083 | if (replicas < replicas_want && target) { | |
1084 | /* Retry from all devices: */ | |
1085 | target = 0; | |
1086 | goto retry; | |
1087 | } | |
a9ec3454 | 1088 | done: |
a9ec3454 KO |
1089 | rcu_read_unlock(); |
1090 | ||
07a1006a KO |
1091 | BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX); |
1092 | ||
57cb2142 | 1093 | return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS; |
1c6fdbd8 KO |
1094 | } |
1095 | ||
1096 | static void journal_write_compact(struct jset *jset) | |
1097 | { | |
1098 | struct jset_entry *i, *next, *prev = NULL; | |
1099 | ||
1100 | /* | |
1101 | * Simple compaction, dropping empty jset_entries (from journal | |
1102 | * reservations that weren't fully used) and merging jset_entries that | |
1103 | * can be. | |
1104 | * | |
1105 | * If we wanted to be really fancy here, we could sort all the keys in | |
1106 | * the jset and drop keys that were overwritten - probably not worth it: | |
1107 | */ | |
1108 | vstruct_for_each_safe(jset, i, next) { | |
1109 | unsigned u64s = le16_to_cpu(i->u64s); | |
1110 | ||
1111 | /* Empty entry: */ | |
1112 | if (!u64s) | |
1113 | continue; | |
1114 | ||
1115 | /* Can we merge with previous entry? */ | |
1116 | if (prev && | |
1117 | i->btree_id == prev->btree_id && | |
1118 | i->level == prev->level && | |
1119 | i->type == prev->type && | |
1120 | i->type == BCH_JSET_ENTRY_btree_keys && | |
1121 | le16_to_cpu(prev->u64s) + u64s <= U16_MAX) { | |
1122 | memmove_u64s_down(vstruct_next(prev), | |
1123 | i->_data, | |
1124 | u64s); | |
1125 | le16_add_cpu(&prev->u64s, u64s); | |
1126 | continue; | |
1127 | } | |
1128 | ||
1129 | /* Couldn't merge, move i into new position (after prev): */ | |
1130 | prev = prev ? vstruct_next(prev) : jset->start; | |
1131 | if (i != prev) | |
1132 | memmove_u64s_down(prev, i, jset_u64s(u64s)); | |
1133 | } | |
1134 | ||
1135 | prev = prev ? vstruct_next(prev) : jset->start; | |
1136 | jset->u64s = cpu_to_le32((u64 *) prev - jset->_data); | |
1137 | } | |
1138 | ||
1139 | static void journal_buf_realloc(struct journal *j, struct journal_buf *buf) | |
1140 | { | |
1141 | /* we aren't holding j->lock: */ | |
1142 | unsigned new_size = READ_ONCE(j->buf_size_want); | |
1143 | void *new_buf; | |
1144 | ||
d16b4a77 | 1145 | if (buf->buf_size >= new_size) |
1c6fdbd8 KO |
1146 | return; |
1147 | ||
1148 | new_buf = kvpmalloc(new_size, GFP_NOIO|__GFP_NOWARN); | |
1149 | if (!new_buf) | |
1150 | return; | |
1151 | ||
d16b4a77 | 1152 | memcpy(new_buf, buf->data, buf->buf_size); |
c859430b KO |
1153 | |
1154 | spin_lock(&j->lock); | |
1155 | swap(buf->data, new_buf); | |
1156 | swap(buf->buf_size, new_size); | |
1157 | spin_unlock(&j->lock); | |
1158 | ||
1159 | kvpfree(new_buf, new_size); | |
1c6fdbd8 KO |
1160 | } |
1161 | ||
ebb84d09 KO |
1162 | static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j) |
1163 | { | |
1164 | return j->buf + j->reservations.unwritten_idx; | |
1165 | } | |
1166 | ||
1c6fdbd8 KO |
1167 | static void journal_write_done(struct closure *cl) |
1168 | { | |
1169 | struct journal *j = container_of(cl, struct journal, io); | |
1170 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
ebb84d09 | 1171 | struct journal_buf *w = journal_last_unwritten_buf(j); |
1c6fdbd8 | 1172 | struct bch_devs_list devs = |
26609b61 | 1173 | bch2_bkey_devs(bkey_i_to_s_c(&w->key)); |
7ef2a73a | 1174 | struct bch_replicas_padded replicas; |
ebb84d09 | 1175 | union journal_res_state old, new; |
ed9d58a2 | 1176 | u64 v, seq, last_seq; |
158eecb8 | 1177 | int err = 0; |
1c6fdbd8 | 1178 | |
9c859dc9 KO |
1179 | bch2_time_stats_update(j->write_time, j->write_start_time); |
1180 | ||
1c6fdbd8 KO |
1181 | if (!devs.nr) { |
1182 | bch_err(c, "unable to write journal to sufficient devices"); | |
158eecb8 KO |
1183 | err = -EIO; |
1184 | } else { | |
1185 | bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal, devs); | |
1186 | if (bch2_mark_replicas(c, &replicas.e)) | |
1187 | err = -EIO; | |
1c6fdbd8 KO |
1188 | } |
1189 | ||
158eecb8 KO |
1190 | if (err) |
1191 | bch2_fatal_error(c); | |
1c6fdbd8 KO |
1192 | |
1193 | spin_lock(&j->lock); | |
ed9d58a2 KO |
1194 | seq = le64_to_cpu(w->data->seq); |
1195 | last_seq = le64_to_cpu(w->data->last_seq); | |
1196 | ||
1c6fdbd8 KO |
1197 | if (seq >= j->pin.front) |
1198 | journal_seq_pin(j, seq)->devs = devs; | |
1199 | ||
0ce2dbbe | 1200 | j->seq_ondisk = seq; |
158eecb8 KO |
1201 | if (err && (!j->err_seq || seq < j->err_seq)) |
1202 | j->err_seq = seq; | |
adbcada4 | 1203 | |
ed9d58a2 | 1204 | if (!JSET_NO_FLUSH(w->data)) { |
adbcada4 KO |
1205 | j->flushed_seq_ondisk = seq; |
1206 | j->last_seq_ondisk = last_seq; | |
adbcada4 | 1207 | } |
0ce2dbbe | 1208 | |
1c6fdbd8 KO |
1209 | /* |
1210 | * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard | |
1211 | * more buckets: | |
1212 | * | |
1213 | * Must come before signaling write completion, for | |
1214 | * bch2_fs_journal_stop(): | |
1215 | */ | |
b7a9bbfc | 1216 | journal_reclaim_kick(&c->journal); |
158eecb8 | 1217 | |
1c6fdbd8 KO |
1218 | /* also must come before signalling write completion: */ |
1219 | closure_debug_destroy(cl); | |
1220 | ||
ebb84d09 KO |
1221 | v = atomic64_read(&j->reservations.counter); |
1222 | do { | |
1223 | old.v = new.v = v; | |
1224 | BUG_ON(new.idx == new.unwritten_idx); | |
1225 | ||
1226 | new.unwritten_idx++; | |
1227 | } while ((v = atomic64_cmpxchg(&j->reservations.counter, | |
1228 | old.v, new.v)) != old.v); | |
1c6fdbd8 | 1229 | |
5d32c5bb KO |
1230 | bch2_journal_space_available(j); |
1231 | ||
1c6fdbd8 KO |
1232 | closure_wake_up(&w->wait); |
1233 | journal_wake(j); | |
1234 | ||
1235 | if (test_bit(JOURNAL_NEED_WRITE, &j->flags)) | |
1236 | mod_delayed_work(system_freezable_wq, &j->write_work, 0); | |
1237 | spin_unlock(&j->lock); | |
ebb84d09 KO |
1238 | |
1239 | if (new.unwritten_idx != new.idx && | |
1240 | !journal_state_count(new, new.unwritten_idx)) | |
1241 | closure_call(&j->io, bch2_journal_write, system_highpri_wq, NULL); | |
1c6fdbd8 KO |
1242 | } |
1243 | ||
1244 | static void journal_write_endio(struct bio *bio) | |
1245 | { | |
1246 | struct bch_dev *ca = bio->bi_private; | |
1247 | struct journal *j = &ca->fs->journal; | |
1248 | ||
0fefe8d8 | 1249 | if (bch2_dev_io_err_on(bio->bi_status, ca, "journal write error: %s", |
63b214e7 | 1250 | bch2_blk_status_to_str(bio->bi_status)) || |
1c6fdbd8 | 1251 | bch2_meta_write_fault("journal")) { |
ebb84d09 | 1252 | struct journal_buf *w = journal_last_unwritten_buf(j); |
1c6fdbd8 KO |
1253 | unsigned long flags; |
1254 | ||
1255 | spin_lock_irqsave(&j->err_lock, flags); | |
26609b61 | 1256 | bch2_bkey_drop_device(bkey_i_to_s(&w->key), ca->dev_idx); |
1c6fdbd8 KO |
1257 | spin_unlock_irqrestore(&j->err_lock, flags); |
1258 | } | |
1259 | ||
1260 | closure_put(&j->io); | |
1261 | percpu_ref_put(&ca->io_ref); | |
1262 | } | |
1263 | ||
280249b9 KO |
1264 | static void do_journal_write(struct closure *cl) |
1265 | { | |
1266 | struct journal *j = container_of(cl, struct journal, io); | |
1267 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
1268 | struct bch_dev *ca; | |
1269 | struct journal_buf *w = journal_last_unwritten_buf(j); | |
1270 | struct bch_extent_ptr *ptr; | |
1271 | struct bio *bio; | |
1272 | unsigned sectors = vstruct_sectors(w->data, c->block_bits); | |
1273 | ||
1274 | extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) { | |
1275 | ca = bch_dev_bkey_exists(c, ptr->dev); | |
1276 | if (!percpu_ref_tryget(&ca->io_ref)) { | |
1277 | /* XXX: fix this */ | |
1278 | bch_err(c, "missing device for journal write\n"); | |
1279 | continue; | |
1280 | } | |
1281 | ||
1282 | this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal], | |
1283 | sectors); | |
1284 | ||
1285 | bio = ca->journal.bio; | |
1286 | bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META); | |
1287 | bio->bi_iter.bi_sector = ptr->offset; | |
1288 | bio->bi_end_io = journal_write_endio; | |
1289 | bio->bi_private = ca; | |
1290 | ||
a28bd48a KO |
1291 | BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector); |
1292 | ca->prev_journal_sector = bio->bi_iter.bi_sector; | |
1293 | ||
280249b9 KO |
1294 | if (!JSET_NO_FLUSH(w->data)) |
1295 | bio->bi_opf |= REQ_FUA; | |
1296 | if (!JSET_NO_FLUSH(w->data) && !w->separate_flush) | |
1297 | bio->bi_opf |= REQ_PREFLUSH; | |
1298 | ||
1299 | bch2_bio_map(bio, w->data, sectors << 9); | |
1300 | ||
1301 | trace_journal_write(bio); | |
1302 | closure_bio_submit(bio, cl); | |
1303 | ||
1304 | ca->journal.bucket_seq[ca->journal.cur_idx] = | |
1305 | le64_to_cpu(w->data->seq); | |
1306 | } | |
1307 | ||
1308 | continue_at(cl, journal_write_done, system_highpri_wq); | |
1309 | return; | |
1310 | } | |
1311 | ||
1c6fdbd8 KO |
1312 | void bch2_journal_write(struct closure *cl) |
1313 | { | |
1314 | struct journal *j = container_of(cl, struct journal, io); | |
1315 | struct bch_fs *c = container_of(j, struct bch_fs, journal); | |
1316 | struct bch_dev *ca; | |
ebb84d09 | 1317 | struct journal_buf *w = journal_last_unwritten_buf(j); |
3ccc5c50 | 1318 | struct jset_entry *start, *end; |
1c6fdbd8 KO |
1319 | struct jset *jset; |
1320 | struct bio *bio; | |
26609b61 | 1321 | bool validate_before_checksum = false; |
280249b9 | 1322 | unsigned i, sectors, bytes, u64s, nr_rw_members = 0; |
e5a66496 KO |
1323 | int ret; |
1324 | ||
b7a9bbfc KO |
1325 | BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb)); |
1326 | ||
1c6fdbd8 KO |
1327 | journal_buf_realloc(j, w); |
1328 | jset = w->data; | |
1329 | ||
1330 | j->write_start_time = local_clock(); | |
1c6fdbd8 | 1331 | |
adbcada4 KO |
1332 | spin_lock(&j->lock); |
1333 | if (c->sb.features & (1ULL << BCH_FEATURE_journal_no_flush) && | |
1334 | !w->must_flush && | |
1335 | (jiffies - j->last_flush_write) < msecs_to_jiffies(j->write_delay_ms) && | |
1336 | test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags)) { | |
1337 | w->noflush = true; | |
1338 | SET_JSET_NO_FLUSH(jset, true); | |
ed9d58a2 | 1339 | jset->last_seq = 0; |
adbcada4 KO |
1340 | |
1341 | j->nr_noflush_writes++; | |
1342 | } else { | |
1343 | j->last_flush_write = jiffies; | |
1344 | j->nr_flush_writes++; | |
1345 | } | |
1346 | spin_unlock(&j->lock); | |
1347 | ||
00b8ccf7 KO |
1348 | /* |
1349 | * New btree roots are set by journalling them; when the journal entry | |
1350 | * gets written we have to propagate them to c->btree_roots | |
1351 | * | |
1352 | * But, every journal entry we write has to contain all the btree roots | |
1353 | * (at least for now); so after we copy btree roots to c->btree_roots we | |
1354 | * have to get any missing btree roots and add them to this journal | |
1355 | * entry: | |
1356 | */ | |
1357 | ||
1358 | bch2_journal_entries_to_btree_roots(c, jset); | |
1359 | ||
1360 | start = end = vstruct_last(jset); | |
1361 | ||
1362 | end = bch2_btree_roots_to_journal_entries(c, jset->start, end); | |
1363 | ||
1364 | end = bch2_journal_super_entries_add_common(c, end, | |
3ccc5c50 KO |
1365 | le64_to_cpu(jset->seq)); |
1366 | u64s = (u64 *) end - (u64 *) start; | |
1367 | BUG_ON(u64s > j->entry_u64s_reserved); | |
1368 | ||
d16b4a77 KO |
1369 | le32_add_cpu(&jset->u64s, u64s); |
1370 | BUG_ON(vstruct_sectors(jset, c->block_bits) > w->sectors); | |
1c6fdbd8 KO |
1371 | |
1372 | journal_write_compact(jset); | |
1373 | ||
1374 | jset->read_clock = cpu_to_le16(c->bucket_clock[READ].hand); | |
1375 | jset->write_clock = cpu_to_le16(c->bucket_clock[WRITE].hand); | |
1376 | jset->magic = cpu_to_le64(jset_magic(c)); | |
26609b61 KO |
1377 | |
1378 | jset->version = c->sb.version < bcachefs_metadata_version_new_versioning | |
1379 | ? cpu_to_le32(BCH_JSET_VERSION_OLD) | |
1380 | : cpu_to_le32(c->sb.version); | |
1c6fdbd8 KO |
1381 | |
1382 | SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN); | |
1383 | SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c)); | |
1384 | ||
158eecb8 KO |
1385 | if (journal_entry_empty(jset)) |
1386 | j->last_empty_seq = le64_to_cpu(jset->seq); | |
1387 | ||
26609b61 KO |
1388 | if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset))) |
1389 | validate_before_checksum = true; | |
1390 | ||
ed9d58a2 | 1391 | if (le32_to_cpu(jset->version) <= bcachefs_metadata_version_inode_btree_change) |
26609b61 KO |
1392 | validate_before_checksum = true; |
1393 | ||
1394 | if (validate_before_checksum && | |
ed9d58a2 | 1395 | jset_validate_for_write(c, jset)) |
1c6fdbd8 KO |
1396 | goto err; |
1397 | ||
1398 | bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset), | |
1399 | jset->encrypted_start, | |
1400 | vstruct_end(jset) - (void *) jset->encrypted_start); | |
1401 | ||
1402 | jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset), | |
1403 | journal_nonce(jset), jset); | |
1404 | ||
26609b61 | 1405 | if (!validate_before_checksum && |
ed9d58a2 | 1406 | jset_validate_for_write(c, jset)) |
1c6fdbd8 KO |
1407 | goto err; |
1408 | ||
1409 | sectors = vstruct_sectors(jset, c->block_bits); | |
d16b4a77 | 1410 | BUG_ON(sectors > w->sectors); |
1c6fdbd8 | 1411 | |
d16b4a77 KO |
1412 | bytes = vstruct_bytes(jset); |
1413 | memset((void *) jset + bytes, 0, (sectors << 9) - bytes); | |
1c6fdbd8 | 1414 | |
c18dade6 | 1415 | retry_alloc: |
e5a66496 KO |
1416 | spin_lock(&j->lock); |
1417 | ret = journal_write_alloc(j, w, sectors); | |
1418 | ||
c18dade6 KO |
1419 | if (ret && j->can_discard) { |
1420 | spin_unlock(&j->lock); | |
1421 | bch2_journal_do_discards(j); | |
1422 | goto retry_alloc; | |
1423 | } | |
1424 | ||
e5a66496 KO |
1425 | /* |
1426 | * write is allocated, no longer need to account for it in | |
1427 | * bch2_journal_space_available(): | |
1428 | */ | |
1429 | w->sectors = 0; | |
1430 | ||
1431 | /* | |
1432 | * journal entry has been compacted and allocated, recalculate space | |
1433 | * available: | |
1434 | */ | |
1435 | bch2_journal_space_available(j); | |
1436 | spin_unlock(&j->lock); | |
1437 | ||
1438 | if (ret) { | |
1c6fdbd8 KO |
1439 | bch_err(c, "Unable to allocate journal write"); |
1440 | bch2_fatal_error(c); | |
1441 | continue_at(cl, journal_write_done, system_highpri_wq); | |
1442 | return; | |
1443 | } | |
1444 | ||
1445 | /* | |
1446 | * XXX: we really should just disable the entire journal in nochanges | |
1447 | * mode | |
1448 | */ | |
1449 | if (c->opts.nochanges) | |
1450 | goto no_io; | |
1451 | ||
280249b9 KO |
1452 | for_each_rw_member(ca, c, i) |
1453 | nr_rw_members++; | |
1c6fdbd8 | 1454 | |
280249b9 KO |
1455 | if (nr_rw_members > 1) |
1456 | w->separate_flush = true; | |
1c6fdbd8 | 1457 | |
280249b9 KO |
1458 | if (!JSET_NO_FLUSH(jset) && w->separate_flush) { |
1459 | for_each_rw_member(ca, c, i) { | |
1460 | percpu_ref_get(&ca->io_ref); | |
1c6fdbd8 | 1461 | |
280249b9 KO |
1462 | bio = ca->journal.bio; |
1463 | bio_reset(bio, ca->disk_sb.bdev, REQ_OP_FLUSH); | |
1464 | bio->bi_end_io = journal_write_endio; | |
1465 | bio->bi_private = ca; | |
1466 | closure_bio_submit(bio, cl); | |
1467 | } | |
1c6fdbd8 KO |
1468 | } |
1469 | ||
280249b9 KO |
1470 | bch2_bucket_seq_cleanup(c); |
1471 | ||
1472 | continue_at(cl, do_journal_write, system_highpri_wq); | |
1473 | return; | |
1c6fdbd8 | 1474 | no_io: |
c6923995 KO |
1475 | bch2_bucket_seq_cleanup(c); |
1476 | ||
1c6fdbd8 KO |
1477 | continue_at(cl, journal_write_done, system_highpri_wq); |
1478 | return; | |
1479 | err: | |
1480 | bch2_inconsistent_error(c); | |
1481 | continue_at(cl, journal_write_done, system_highpri_wq); | |
1482 | } |