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