1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * linux/fs/jbd2/recovery.c
4 *
5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
6 *
7 * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
8 *
9 * Journal recovery routines for the generic filesystem journaling code;
10 * part of the ext2fs journaling system.
11 */
12
13 #ifndef __KERNEL__
14 #include "jfs_user.h"
15 #else
16 #include <linux/time.h>
17 #include <linux/fs.h>
18 #include <linux/jbd2.h>
19 #include <linux/errno.h>
20 #include <linux/crc32.h>
21 #include <linux/blkdev.h>
22 #endif
23
24 /*
25 * Maintain information about the progress of the recovery job, so that
26 * the different passes can carry information between them.
27 */
28 struct recovery_info
29 {
30 tid_t start_transaction;
31 tid_t end_transaction;
32 unsigned long head_block;
33
34 int nr_replays;
35 int nr_revokes;
36 int nr_revoke_hits;
37 };
38
39 static int do_one_pass(journal_t *journal,
40 struct recovery_info *info, enum passtype pass);
41 static int scan_revoke_records(journal_t *, struct buffer_head *,
42 tid_t, struct recovery_info *);
43
44 #ifdef __KERNEL__
45
46 /* Release readahead buffers after use */
journal_brelse_array(struct buffer_head * b[],int n)47 static void journal_brelse_array(struct buffer_head *b[], int n)
48 {
49 while (--n >= 0)
50 brelse (b[n]);
51 }
52
53
54 /*
55 * When reading from the journal, we are going through the block device
56 * layer directly and so there is no readahead being done for us. We
57 * need to implement any readahead ourselves if we want it to happen at
58 * all. Recovery is basically one long sequential read, so make sure we
59 * do the IO in reasonably large chunks.
60 *
61 * This is not so critical that we need to be enormously clever about
62 * the readahead size, though. 128K is a purely arbitrary, good-enough
63 * fixed value.
64 */
65
66 #define MAXBUF 8
do_readahead(journal_t * journal,unsigned int start)67 static int do_readahead(journal_t *journal, unsigned int start)
68 {
69 int err;
70 unsigned int max, nbufs, next;
71 unsigned long long blocknr;
72 struct buffer_head *bh;
73
74 struct buffer_head * bufs[MAXBUF];
75
76 /* Do up to 128K of readahead */
77 max = start + (128 * 1024 / journal->j_blocksize);
78 if (max > journal->j_total_len)
79 max = journal->j_total_len;
80
81 /* Do the readahead itself. We'll submit MAXBUF buffer_heads at
82 * a time to the block device IO layer. */
83
84 nbufs = 0;
85
86 for (next = start; next < max; next++) {
87 err = jbd2_journal_bmap(journal, next, &blocknr);
88
89 if (err) {
90 printk(KERN_ERR "JBD2: bad block at offset %u\n",
91 next);
92 goto failed;
93 }
94
95 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
96 if (!bh) {
97 err = -ENOMEM;
98 goto failed;
99 }
100
101 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
102 bufs[nbufs++] = bh;
103 if (nbufs == MAXBUF) {
104 bh_readahead_batch(nbufs, bufs, 0);
105 journal_brelse_array(bufs, nbufs);
106 nbufs = 0;
107 }
108 } else
109 brelse(bh);
110 }
111
112 if (nbufs)
113 bh_readahead_batch(nbufs, bufs, 0);
114 err = 0;
115
116 failed:
117 if (nbufs)
118 journal_brelse_array(bufs, nbufs);
119 return err;
120 }
121
122 #endif /* __KERNEL__ */
123
124
125 /*
126 * Read a block from the journal
127 */
128
jread(struct buffer_head ** bhp,journal_t * journal,unsigned int offset)129 static int jread(struct buffer_head **bhp, journal_t *journal,
130 unsigned int offset)
131 {
132 int err;
133 unsigned long long blocknr;
134 struct buffer_head *bh;
135
136 *bhp = NULL;
137
138 if (offset >= journal->j_total_len) {
139 printk(KERN_ERR "JBD2: corrupted journal superblock\n");
140 return -EFSCORRUPTED;
141 }
142
143 err = jbd2_journal_bmap(journal, offset, &blocknr);
144
145 if (err) {
146 printk(KERN_ERR "JBD2: bad block at offset %u\n",
147 offset);
148 return err;
149 }
150
151 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
152 if (!bh)
153 return -ENOMEM;
154
155 if (!buffer_uptodate(bh)) {
156 /*
157 * If this is a brand new buffer, start readahead.
158 * Otherwise, we assume we are already reading it.
159 */
160 bool need_readahead = !buffer_req(bh);
161
162 bh_read_nowait(bh, 0);
163 if (need_readahead)
164 do_readahead(journal, offset);
165 wait_on_buffer(bh);
166 }
167
168 if (!buffer_uptodate(bh)) {
169 printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
170 offset);
171 brelse(bh);
172 return -EIO;
173 }
174
175 *bhp = bh;
176 return 0;
177 }
178
jbd2_descriptor_block_csum_verify(journal_t * j,void * buf)179 static int jbd2_descriptor_block_csum_verify(journal_t *j, void *buf)
180 {
181 struct jbd2_journal_block_tail *tail;
182 __be32 provided;
183 __u32 calculated;
184
185 if (!jbd2_journal_has_csum_v2or3(j))
186 return 1;
187
188 tail = (struct jbd2_journal_block_tail *)((char *)buf +
189 j->j_blocksize - sizeof(struct jbd2_journal_block_tail));
190 provided = tail->t_checksum;
191 tail->t_checksum = 0;
192 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
193 tail->t_checksum = provided;
194
195 return provided == cpu_to_be32(calculated);
196 }
197
198 /*
199 * Count the number of in-use tags in a journal descriptor block.
200 */
201
count_tags(journal_t * journal,struct buffer_head * bh)202 static int count_tags(journal_t *journal, struct buffer_head *bh)
203 {
204 char * tagp;
205 journal_block_tag_t tag;
206 int nr = 0, size = journal->j_blocksize;
207 int tag_bytes = journal_tag_bytes(journal);
208
209 if (jbd2_journal_has_csum_v2or3(journal))
210 size -= sizeof(struct jbd2_journal_block_tail);
211
212 tagp = &bh->b_data[sizeof(journal_header_t)];
213
214 while ((tagp - bh->b_data + tag_bytes) <= size) {
215 memcpy(&tag, tagp, sizeof(tag));
216
217 nr++;
218 tagp += tag_bytes;
219 if (!(tag.t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID)))
220 tagp += 16;
221
222 if (tag.t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG))
223 break;
224 }
225
226 return nr;
227 }
228
229
230 /* Make sure we wrap around the log correctly! */
231 #define wrap(journal, var) \
232 do { \
233 if (var >= (journal)->j_last) \
234 var -= ((journal)->j_last - (journal)->j_first); \
235 } while (0)
236
fc_do_one_pass(journal_t * journal,struct recovery_info * info,enum passtype pass)237 static int fc_do_one_pass(journal_t *journal,
238 struct recovery_info *info, enum passtype pass)
239 {
240 unsigned int expected_commit_id = info->end_transaction;
241 unsigned long next_fc_block;
242 struct buffer_head *bh;
243 int err = 0;
244
245 next_fc_block = journal->j_fc_first;
246 if (!journal->j_fc_replay_callback)
247 return 0;
248
249 while (next_fc_block <= journal->j_fc_last) {
250 jbd2_debug(3, "Fast commit replay: next block %ld\n",
251 next_fc_block);
252 err = jread(&bh, journal, next_fc_block);
253 if (err) {
254 jbd2_debug(3, "Fast commit replay: read error\n");
255 break;
256 }
257
258 err = journal->j_fc_replay_callback(journal, bh, pass,
259 next_fc_block - journal->j_fc_first,
260 expected_commit_id);
261 brelse(bh);
262 next_fc_block++;
263 if (err < 0 || err == JBD2_FC_REPLAY_STOP)
264 break;
265 err = 0;
266 }
267
268 if (err)
269 jbd2_debug(3, "Fast commit replay failed, err = %d\n", err);
270
271 return err;
272 }
273
274 /**
275 * jbd2_journal_recover - recovers a on-disk journal
276 * @journal: the journal to recover
277 *
278 * The primary function for recovering the log contents when mounting a
279 * journaled device.
280 *
281 * Recovery is done in three passes. In the first pass, we look for the
282 * end of the log. In the second, we assemble the list of revoke
283 * blocks. In the third and final pass, we replay any un-revoked blocks
284 * in the log.
285 */
jbd2_journal_recover(journal_t * journal)286 int jbd2_journal_recover(journal_t *journal)
287 {
288 int err, err2;
289 struct recovery_info info;
290 errseq_t wb_err;
291 struct address_space *mapping;
292
293 memset(&info, 0, sizeof(info));
294
295 /*
296 * The journal superblock's s_start field (the current log head)
297 * is always zero if, and only if, the journal was cleanly
298 * unmounted. We use its in-memory version j_tail here because
299 * jbd2_journal_wipe() could have updated it without updating journal
300 * superblock.
301 */
302 if (!journal->j_tail) {
303 journal_superblock_t *sb = journal->j_superblock;
304
305 jbd2_debug(1, "No recovery required, last transaction %d, head block %u\n",
306 be32_to_cpu(sb->s_sequence), be32_to_cpu(sb->s_head));
307 journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
308 journal->j_head = be32_to_cpu(sb->s_head);
309 return 0;
310 }
311
312 wb_err = 0;
313 mapping = journal->j_fs_dev->bd_inode->i_mapping;
314 errseq_check_and_advance(&mapping->wb_err, &wb_err);
315 err = do_one_pass(journal, &info, PASS_SCAN);
316 if (!err)
317 err = do_one_pass(journal, &info, PASS_REVOKE);
318 if (!err)
319 err = do_one_pass(journal, &info, PASS_REPLAY);
320
321 jbd2_debug(1, "JBD2: recovery, exit status %d, "
322 "recovered transactions %u to %u\n",
323 err, info.start_transaction, info.end_transaction);
324 jbd2_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
325 info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
326
327 /* Restart the log at the next transaction ID, thus invalidating
328 * any existing commit records in the log. */
329 journal->j_transaction_sequence = ++info.end_transaction;
330 journal->j_head = info.head_block;
331 jbd2_debug(1, "JBD2: last transaction %d, head block %lu\n",
332 journal->j_transaction_sequence, journal->j_head);
333
334 jbd2_journal_clear_revoke(journal);
335 err2 = sync_blockdev(journal->j_fs_dev);
336 if (!err)
337 err = err2;
338 err2 = errseq_check_and_advance(&mapping->wb_err, &wb_err);
339 if (!err)
340 err = err2;
341 /* Make sure all replayed data is on permanent storage */
342 if (journal->j_flags & JBD2_BARRIER) {
343 err2 = blkdev_issue_flush(journal->j_fs_dev);
344 if (!err)
345 err = err2;
346 }
347 return err;
348 }
349
350 /**
351 * jbd2_journal_skip_recovery - Start journal and wipe exiting records
352 * @journal: journal to startup
353 *
354 * Locate any valid recovery information from the journal and set up the
355 * journal structures in memory to ignore it (presumably because the
356 * caller has evidence that it is out of date).
357 * This function doesn't appear to be exported..
358 *
359 * We perform one pass over the journal to allow us to tell the user how
360 * much recovery information is being erased, and to let us initialise
361 * the journal transaction sequence numbers to the next unused ID.
362 */
jbd2_journal_skip_recovery(journal_t * journal)363 int jbd2_journal_skip_recovery(journal_t *journal)
364 {
365 int err;
366
367 struct recovery_info info;
368
369 memset (&info, 0, sizeof(info));
370
371 err = do_one_pass(journal, &info, PASS_SCAN);
372
373 if (err) {
374 printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
375 ++journal->j_transaction_sequence;
376 journal->j_head = journal->j_first;
377 } else {
378 #ifdef CONFIG_JBD2_DEBUG
379 int dropped = info.end_transaction -
380 be32_to_cpu(journal->j_superblock->s_sequence);
381 jbd2_debug(1,
382 "JBD2: ignoring %d transaction%s from the journal.\n",
383 dropped, (dropped == 1) ? "" : "s");
384 #endif
385 journal->j_transaction_sequence = ++info.end_transaction;
386 journal->j_head = info.head_block;
387 }
388
389 journal->j_tail = 0;
390 return err;
391 }
392
read_tag_block(journal_t * journal,journal_block_tag_t * tag)393 static inline unsigned long long read_tag_block(journal_t *journal,
394 journal_block_tag_t *tag)
395 {
396 unsigned long long block = be32_to_cpu(tag->t_blocknr);
397 if (jbd2_has_feature_64bit(journal))
398 block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
399 return block;
400 }
401
402 /*
403 * calc_chksums calculates the checksums for the blocks described in the
404 * descriptor block.
405 */
calc_chksums(journal_t * journal,struct buffer_head * bh,unsigned long * next_log_block,__u32 * crc32_sum)406 static int calc_chksums(journal_t *journal, struct buffer_head *bh,
407 unsigned long *next_log_block, __u32 *crc32_sum)
408 {
409 int i, num_blks, err;
410 unsigned long io_block;
411 struct buffer_head *obh;
412
413 num_blks = count_tags(journal, bh);
414 /* Calculate checksum of the descriptor block. */
415 *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
416
417 for (i = 0; i < num_blks; i++) {
418 io_block = (*next_log_block)++;
419 wrap(journal, *next_log_block);
420 err = jread(&obh, journal, io_block);
421 if (err) {
422 printk(KERN_ERR "JBD2: IO error %d recovering block "
423 "%lu in log\n", err, io_block);
424 return 1;
425 } else {
426 *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
427 obh->b_size);
428 }
429 put_bh(obh);
430 }
431 return 0;
432 }
433
jbd2_commit_block_csum_verify(journal_t * j,void * buf)434 static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
435 {
436 struct commit_header *h;
437 __be32 provided;
438 __u32 calculated;
439
440 if (!jbd2_journal_has_csum_v2or3(j))
441 return 1;
442
443 h = buf;
444 provided = h->h_chksum[0];
445 h->h_chksum[0] = 0;
446 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
447 h->h_chksum[0] = provided;
448
449 return provided == cpu_to_be32(calculated);
450 }
451
jbd2_commit_block_csum_verify_partial(journal_t * j,void * buf)452 static bool jbd2_commit_block_csum_verify_partial(journal_t *j, void *buf)
453 {
454 struct commit_header *h;
455 __be32 provided;
456 __u32 calculated;
457 void *tmpbuf;
458
459 tmpbuf = kzalloc(j->j_blocksize, GFP_KERNEL);
460 if (!tmpbuf)
461 return false;
462
463 memcpy(tmpbuf, buf, sizeof(struct commit_header));
464 h = tmpbuf;
465 provided = h->h_chksum[0];
466 h->h_chksum[0] = 0;
467 calculated = jbd2_chksum(j, j->j_csum_seed, tmpbuf, j->j_blocksize);
468 kfree(tmpbuf);
469
470 return provided == cpu_to_be32(calculated);
471 }
472
jbd2_block_tag_csum_verify(journal_t * j,journal_block_tag_t * tag,journal_block_tag3_t * tag3,void * buf,__u32 sequence)473 static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
474 journal_block_tag3_t *tag3,
475 void *buf, __u32 sequence)
476 {
477 __u32 csum32;
478 __be32 seq;
479
480 if (!jbd2_journal_has_csum_v2or3(j))
481 return 1;
482
483 seq = cpu_to_be32(sequence);
484 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
485 csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
486
487 if (jbd2_has_feature_csum3(j))
488 return tag3->t_checksum == cpu_to_be32(csum32);
489 else
490 return tag->t_checksum == cpu_to_be16(csum32);
491 }
492
do_one_pass(journal_t * journal,struct recovery_info * info,enum passtype pass)493 static int do_one_pass(journal_t *journal,
494 struct recovery_info *info, enum passtype pass)
495 {
496 unsigned int first_commit_ID, next_commit_ID;
497 unsigned long next_log_block, head_block;
498 int err, success = 0;
499 journal_superblock_t * sb;
500 journal_header_t * tmp;
501 struct buffer_head * bh;
502 unsigned int sequence;
503 int blocktype;
504 int tag_bytes = journal_tag_bytes(journal);
505 __u32 crc32_sum = ~0; /* Transactional Checksums */
506 int descr_csum_size = 0;
507 int block_error = 0;
508 bool need_check_commit_time = false;
509 __u64 last_trans_commit_time = 0, commit_time;
510
511 /*
512 * First thing is to establish what we expect to find in the log
513 * (in terms of transaction IDs), and where (in terms of log
514 * block offsets): query the superblock.
515 */
516
517 sb = journal->j_superblock;
518 next_commit_ID = be32_to_cpu(sb->s_sequence);
519 next_log_block = be32_to_cpu(sb->s_start);
520 head_block = next_log_block;
521
522 first_commit_ID = next_commit_ID;
523 if (pass == PASS_SCAN)
524 info->start_transaction = first_commit_ID;
525
526 jbd2_debug(1, "Starting recovery pass %d\n", pass);
527
528 /*
529 * Now we walk through the log, transaction by transaction,
530 * making sure that each transaction has a commit block in the
531 * expected place. Each complete transaction gets replayed back
532 * into the main filesystem.
533 */
534
535 while (1) {
536 int flags;
537 char * tagp;
538 journal_block_tag_t tag;
539 struct buffer_head * obh;
540 struct buffer_head * nbh;
541
542 cond_resched();
543
544 /* If we already know where to stop the log traversal,
545 * check right now that we haven't gone past the end of
546 * the log. */
547
548 if (pass != PASS_SCAN)
549 if (tid_geq(next_commit_ID, info->end_transaction))
550 break;
551
552 jbd2_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
553 next_commit_ID, next_log_block, journal->j_last);
554
555 /* Skip over each chunk of the transaction looking
556 * either the next descriptor block or the final commit
557 * record. */
558
559 jbd2_debug(3, "JBD2: checking block %ld\n", next_log_block);
560 err = jread(&bh, journal, next_log_block);
561 if (err)
562 goto failed;
563
564 next_log_block++;
565 wrap(journal, next_log_block);
566
567 /* What kind of buffer is it?
568 *
569 * If it is a descriptor block, check that it has the
570 * expected sequence number. Otherwise, we're all done
571 * here. */
572
573 tmp = (journal_header_t *)bh->b_data;
574
575 if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER)) {
576 brelse(bh);
577 break;
578 }
579
580 blocktype = be32_to_cpu(tmp->h_blocktype);
581 sequence = be32_to_cpu(tmp->h_sequence);
582 jbd2_debug(3, "Found magic %d, sequence %d\n",
583 blocktype, sequence);
584
585 if (sequence != next_commit_ID) {
586 brelse(bh);
587 break;
588 }
589
590 /* OK, we have a valid descriptor block which matches
591 * all of the sequence number checks. What are we going
592 * to do with it? That depends on the pass... */
593
594 switch(blocktype) {
595 case JBD2_DESCRIPTOR_BLOCK:
596 /* Verify checksum first */
597 if (jbd2_journal_has_csum_v2or3(journal))
598 descr_csum_size =
599 sizeof(struct jbd2_journal_block_tail);
600 if (descr_csum_size > 0 &&
601 !jbd2_descriptor_block_csum_verify(journal,
602 bh->b_data)) {
603 /*
604 * PASS_SCAN can see stale blocks due to lazy
605 * journal init. Don't error out on those yet.
606 */
607 if (pass != PASS_SCAN) {
608 pr_err("JBD2: Invalid checksum recovering block %lu in log\n",
609 next_log_block);
610 err = -EFSBADCRC;
611 brelse(bh);
612 goto failed;
613 }
614 need_check_commit_time = true;
615 jbd2_debug(1,
616 "invalid descriptor block found in %lu\n",
617 next_log_block);
618 }
619
620 /* If it is a valid descriptor block, replay it
621 * in pass REPLAY; if journal_checksums enabled, then
622 * calculate checksums in PASS_SCAN, otherwise,
623 * just skip over the blocks it describes. */
624 if (pass != PASS_REPLAY) {
625 if (pass == PASS_SCAN &&
626 jbd2_has_feature_checksum(journal) &&
627 !need_check_commit_time &&
628 !info->end_transaction) {
629 if (calc_chksums(journal, bh,
630 &next_log_block,
631 &crc32_sum)) {
632 put_bh(bh);
633 break;
634 }
635 put_bh(bh);
636 continue;
637 }
638 next_log_block += count_tags(journal, bh);
639 wrap(journal, next_log_block);
640 put_bh(bh);
641 continue;
642 }
643
644 /* A descriptor block: we can now write all of
645 * the data blocks. Yay, useful work is finally
646 * getting done here! */
647
648 tagp = &bh->b_data[sizeof(journal_header_t)];
649 while ((tagp - bh->b_data + tag_bytes)
650 <= journal->j_blocksize - descr_csum_size) {
651 unsigned long io_block;
652
653 memcpy(&tag, tagp, sizeof(tag));
654 flags = be16_to_cpu(tag.t_flags);
655
656 io_block = next_log_block++;
657 wrap(journal, next_log_block);
658 err = jread(&obh, journal, io_block);
659 if (err) {
660 /* Recover what we can, but
661 * report failure at the end. */
662 success = err;
663 printk(KERN_ERR
664 "JBD2: IO error %d recovering "
665 "block %ld in log\n",
666 err, io_block);
667 } else {
668 unsigned long long blocknr;
669
670 J_ASSERT(obh != NULL);
671 blocknr = read_tag_block(journal,
672 &tag);
673
674 /* If the block has been
675 * revoked, then we're all done
676 * here. */
677 if (jbd2_journal_test_revoke
678 (journal, blocknr,
679 next_commit_ID)) {
680 brelse(obh);
681 ++info->nr_revoke_hits;
682 goto skip_write;
683 }
684
685 /* Look for block corruption */
686 if (!jbd2_block_tag_csum_verify(
687 journal, &tag, (journal_block_tag3_t *)tagp,
688 obh->b_data, be32_to_cpu(tmp->h_sequence))) {
689 brelse(obh);
690 success = -EFSBADCRC;
691 printk(KERN_ERR "JBD2: Invalid "
692 "checksum recovering "
693 "data block %llu in "
694 "log\n", blocknr);
695 block_error = 1;
696 goto skip_write;
697 }
698
699 /* Find a buffer for the new
700 * data being restored */
701 nbh = __getblk(journal->j_fs_dev,
702 blocknr,
703 journal->j_blocksize);
704 if (nbh == NULL) {
705 printk(KERN_ERR
706 "JBD2: Out of memory "
707 "during recovery.\n");
708 err = -ENOMEM;
709 brelse(bh);
710 brelse(obh);
711 goto failed;
712 }
713
714 lock_buffer(nbh);
715 memcpy(nbh->b_data, obh->b_data,
716 journal->j_blocksize);
717 if (flags & JBD2_FLAG_ESCAPE) {
718 *((__be32 *)nbh->b_data) =
719 cpu_to_be32(JBD2_MAGIC_NUMBER);
720 }
721
722 BUFFER_TRACE(nbh, "marking dirty");
723 set_buffer_uptodate(nbh);
724 mark_buffer_dirty(nbh);
725 BUFFER_TRACE(nbh, "marking uptodate");
726 ++info->nr_replays;
727 unlock_buffer(nbh);
728 brelse(obh);
729 brelse(nbh);
730 }
731
732 skip_write:
733 tagp += tag_bytes;
734 if (!(flags & JBD2_FLAG_SAME_UUID))
735 tagp += 16;
736
737 if (flags & JBD2_FLAG_LAST_TAG)
738 break;
739 }
740
741 brelse(bh);
742 continue;
743
744 case JBD2_COMMIT_BLOCK:
745 /* How to differentiate between interrupted commit
746 * and journal corruption ?
747 *
748 * {nth transaction}
749 * Checksum Verification Failed
750 * |
751 * ____________________
752 * | |
753 * async_commit sync_commit
754 * | |
755 * | GO TO NEXT "Journal Corruption"
756 * | TRANSACTION
757 * |
758 * {(n+1)th transanction}
759 * |
760 * _______|______________
761 * | |
762 * Commit block found Commit block not found
763 * | |
764 * "Journal Corruption" |
765 * _____________|_________
766 * | |
767 * nth trans corrupt OR nth trans
768 * and (n+1)th interrupted interrupted
769 * before commit block
770 * could reach the disk.
771 * (Cannot find the difference in above
772 * mentioned conditions. Hence assume
773 * "Interrupted Commit".)
774 */
775 commit_time = be64_to_cpu(
776 ((struct commit_header *)bh->b_data)->h_commit_sec);
777 /*
778 * If need_check_commit_time is set, it means we are in
779 * PASS_SCAN and csum verify failed before. If
780 * commit_time is increasing, it's the same journal,
781 * otherwise it is stale journal block, just end this
782 * recovery.
783 */
784 if (need_check_commit_time) {
785 if (commit_time >= last_trans_commit_time) {
786 pr_err("JBD2: Invalid checksum found in transaction %u\n",
787 next_commit_ID);
788 err = -EFSBADCRC;
789 brelse(bh);
790 goto failed;
791 }
792 ignore_crc_mismatch:
793 /*
794 * It likely does not belong to same journal,
795 * just end this recovery with success.
796 */
797 jbd2_debug(1, "JBD2: Invalid checksum ignored in transaction %u, likely stale data\n",
798 next_commit_ID);
799 brelse(bh);
800 goto done;
801 }
802
803 /*
804 * Found an expected commit block: if checksums
805 * are present, verify them in PASS_SCAN; else not
806 * much to do other than move on to the next sequence
807 * number.
808 */
809 if (pass == PASS_SCAN &&
810 jbd2_has_feature_checksum(journal)) {
811 struct commit_header *cbh =
812 (struct commit_header *)bh->b_data;
813 unsigned found_chksum =
814 be32_to_cpu(cbh->h_chksum[0]);
815
816 if (info->end_transaction) {
817 journal->j_failed_commit =
818 info->end_transaction;
819 brelse(bh);
820 break;
821 }
822
823 /* Neither checksum match nor unused? */
824 if (!((crc32_sum == found_chksum &&
825 cbh->h_chksum_type ==
826 JBD2_CRC32_CHKSUM &&
827 cbh->h_chksum_size ==
828 JBD2_CRC32_CHKSUM_SIZE) ||
829 (cbh->h_chksum_type == 0 &&
830 cbh->h_chksum_size == 0 &&
831 found_chksum == 0)))
832 goto chksum_error;
833
834 crc32_sum = ~0;
835 }
836 if (pass == PASS_SCAN &&
837 !jbd2_commit_block_csum_verify(journal,
838 bh->b_data)) {
839 if (jbd2_commit_block_csum_verify_partial(
840 journal,
841 bh->b_data)) {
842 pr_notice("JBD2: Find incomplete commit block in transaction %u block %lu\n",
843 next_commit_ID, next_log_block);
844 goto chksum_ok;
845 }
846 chksum_error:
847 if (commit_time < last_trans_commit_time)
848 goto ignore_crc_mismatch;
849 info->end_transaction = next_commit_ID;
850 info->head_block = head_block;
851
852 if (!jbd2_has_feature_async_commit(journal)) {
853 journal->j_failed_commit =
854 next_commit_ID;
855 brelse(bh);
856 break;
857 }
858 }
859 if (pass == PASS_SCAN) {
860 chksum_ok:
861 last_trans_commit_time = commit_time;
862 head_block = next_log_block;
863 }
864 brelse(bh);
865 next_commit_ID++;
866 continue;
867
868 case JBD2_REVOKE_BLOCK:
869 /*
870 * Check revoke block crc in pass_scan, if csum verify
871 * failed, check commit block time later.
872 */
873 if (pass == PASS_SCAN &&
874 !jbd2_descriptor_block_csum_verify(journal,
875 bh->b_data)) {
876 jbd2_debug(1, "JBD2: invalid revoke block found in %lu\n",
877 next_log_block);
878 need_check_commit_time = true;
879 }
880
881 /* If we aren't in the REVOKE pass, then we can
882 * just skip over this block. */
883 if (pass != PASS_REVOKE) {
884 brelse(bh);
885 continue;
886 }
887
888 err = scan_revoke_records(journal, bh,
889 next_commit_ID, info);
890 brelse(bh);
891 if (err)
892 goto failed;
893 continue;
894
895 default:
896 jbd2_debug(3, "Unrecognised magic %d, end of scan.\n",
897 blocktype);
898 brelse(bh);
899 goto done;
900 }
901 }
902
903 done:
904 /*
905 * We broke out of the log scan loop: either we came to the
906 * known end of the log or we found an unexpected block in the
907 * log. If the latter happened, then we know that the "current"
908 * transaction marks the end of the valid log.
909 */
910
911 if (pass == PASS_SCAN) {
912 if (!info->end_transaction)
913 info->end_transaction = next_commit_ID;
914 if (!info->head_block)
915 info->head_block = head_block;
916 } else {
917 /* It's really bad news if different passes end up at
918 * different places (but possible due to IO errors). */
919 if (info->end_transaction != next_commit_ID) {
920 printk(KERN_ERR "JBD2: recovery pass %d ended at "
921 "transaction %u, expected %u\n",
922 pass, next_commit_ID, info->end_transaction);
923 if (!success)
924 success = -EIO;
925 }
926 }
927
928 if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE) {
929 err = fc_do_one_pass(journal, info, pass);
930 if (err)
931 success = err;
932 }
933
934 if (block_error && success == 0)
935 success = -EIO;
936 return success;
937
938 failed:
939 return err;
940 }
941
942 /* Scan a revoke record, marking all blocks mentioned as revoked. */
943
scan_revoke_records(journal_t * journal,struct buffer_head * bh,tid_t sequence,struct recovery_info * info)944 static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
945 tid_t sequence, struct recovery_info *info)
946 {
947 jbd2_journal_revoke_header_t *header;
948 int offset, max;
949 unsigned csum_size = 0;
950 __u32 rcount;
951 int record_len = 4;
952
953 header = (jbd2_journal_revoke_header_t *) bh->b_data;
954 offset = sizeof(jbd2_journal_revoke_header_t);
955 rcount = be32_to_cpu(header->r_count);
956
957 if (jbd2_journal_has_csum_v2or3(journal))
958 csum_size = sizeof(struct jbd2_journal_block_tail);
959 if (rcount > journal->j_blocksize - csum_size)
960 return -EINVAL;
961 max = rcount;
962
963 if (jbd2_has_feature_64bit(journal))
964 record_len = 8;
965
966 while (offset + record_len <= max) {
967 unsigned long long blocknr;
968 int err;
969
970 if (record_len == 4)
971 blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
972 else
973 blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
974 offset += record_len;
975 err = jbd2_journal_set_revoke(journal, blocknr, sequence);
976 if (err)
977 return err;
978 ++info->nr_revokes;
979 }
980 return 0;
981 }
982