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