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