Whamcloud - gitweb
blkid: Make regression test tolerate older versions of mkswap
[tools/e2fsprogs.git] / e2fsck / recovery.c
index 22e79a5..8e40575 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * linux/fs/recovery.c
- * 
+ * linux/fs/jbd/recovery.c
+ *
  * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
  *
  * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
@@ -10,7 +10,7 @@
  * option, any later version, incorporated herein by reference.
  *
  * Journal recovery routines for the generic filesystem journaling code;
- * part of the ext2fs journaling system.  
+ * part of the ext2fs journaling system.
  */
 
 #ifndef __KERNEL__
 
 /*
  * Maintain information about the progress of the recovery job, so that
- * the different passes can carry information between them. 
+ * the different passes can carry information between them.
  */
-struct recovery_info 
+struct recovery_info
 {
-       tid_t           start_transaction;      
+       tid_t           start_transaction;
        tid_t           end_transaction;
-       
+
        int             nr_replays;
        int             nr_revokes;
        int             nr_revoke_hits;
@@ -46,7 +46,7 @@ static int scan_revoke_records(journal_t *, struct buffer_head *,
 #ifdef __KERNEL__
 
 /* Release readahead buffers after use */
-void journal_brelse_array(struct buffer_head *b[], int n)
+static void journal_brelse_array(struct buffer_head *b[], int n)
 {
        while (--n >= 0)
                brelse (b[n]);
@@ -72,9 +72,9 @@ static int do_readahead(journal_t *journal, unsigned int start)
        unsigned int max, nbufs, next;
        unsigned long blocknr;
        struct buffer_head *bh;
-       
+
        struct buffer_head * bufs[MAXBUF];
-       
+
        /* Do up to 128K of readahead */
        max = start + (128 * 1024 / journal->j_blocksize);
        if (max > journal->j_maxlen)
@@ -82,9 +82,9 @@ static int do_readahead(journal_t *journal, unsigned int start)
 
        /* Do the readahead itself.  We'll submit MAXBUF buffer_heads at
         * a time to the block device IO layer. */
-       
+
        nbufs = 0;
-       
+
        for (next = start; next < max; next++) {
                err = journal_bmap(journal, next, &blocknr);
 
@@ -115,8 +115,8 @@ static int do_readahead(journal_t *journal, unsigned int start)
                ll_rw_block(READ, nbufs, bufs);
        err = 0;
 
-failed:        
-       if (nbufs) 
+failed:
+       if (nbufs)
                journal_brelse_array(bufs, nbufs);
        return err;
 }
@@ -128,7 +128,7 @@ failed:
  * Read a block from the journal
  */
 
-static int jread(struct buffer_head **bhp, journal_t *journal, 
+static int jread(struct buffer_head **bhp, journal_t *journal,
                 unsigned int offset)
 {
        int err;
@@ -137,8 +137,11 @@ static int jread(struct buffer_head **bhp, journal_t *journal,
 
        *bhp = NULL;
 
-       J_ASSERT (offset < journal->j_maxlen);
-       
+       if (offset >= journal->j_maxlen) {
+               printk(KERN_ERR "JBD: corrupted journal superblock\n");
+               return -EIO;
+       }
+
        err = journal_bmap(journal, offset, &blocknr);
 
        if (err) {
@@ -175,23 +178,24 @@ static int jread(struct buffer_head **bhp, journal_t *journal,
  * Count the number of in-use tags in a journal descriptor block.
  */
 
-static int count_tags(struct buffer_head *bh, int size)
+static int count_tags(journal_t *journal, struct buffer_head *bh)
 {
        char *                  tagp;
        journal_block_tag_t *   tag;
-       int                     nr = 0;
+       int                     nr = 0, size = journal->j_blocksize;
+       int                     tag_bytes = journal_tag_bytes(journal);
 
        tagp = &bh->b_data[sizeof(journal_header_t)];
 
-       while ((tagp - bh->b_data + sizeof(journal_block_tag_t)) <= size) {
+       while ((tagp - bh->b_data + tag_bytes) <= size) {
                tag = (journal_block_tag_t *) tagp;
 
                nr++;
-               tagp += sizeof(journal_block_tag_t);
-               if (!(tag->t_flags & htonl(JFS_FLAG_SAME_UUID)))
+               tagp += tag_bytes;
+               if (!(tag->t_flags & cpu_to_be32(JFS_FLAG_SAME_UUID)))
                        tagp += 16;
 
-               if (tag->t_flags & htonl(JFS_FLAG_LAST_TAG))
+               if (tag->t_flags & cpu_to_be32(JFS_FLAG_LAST_TAG))
                        break;
        }
 
@@ -207,16 +211,16 @@ do {                                                                      \
 } while (0)
 
 /**
- * int journal_recover(journal_t *journal) - recovers a on-disk journal
+ * journal_recover - recovers a on-disk journal
  * @journal: the journal to recover
  *
  * The primary function for recovering the log contents when mounting a
- * journaled device.  
- * 
+ * journaled device.
+ *
  * Recovery is done in three passes.  In the first pass, we look for the
  * end of the log.  In the second, we assemble the list of revoke
  * blocks.  In the third and final pass, we replay any un-revoked blocks
- * in the log.  
+ * in the log.
  */
 int journal_recover(journal_t *journal)
 {
@@ -224,56 +228,56 @@ int journal_recover(journal_t *journal)
        journal_superblock_t *  sb;
 
        struct recovery_info    info;
-       
+
        memset(&info, 0, sizeof(info));
        sb = journal->j_superblock;
-       
-       /* 
+
+       /*
         * The journal superblock's s_start field (the current log head)
         * is always zero if, and only if, the journal was cleanly
-        * unmounted.  
+        * unmounted.
         */
 
        if (!sb->s_start) {
                jbd_debug(1, "No recovery required, last transaction %d\n",
-                         (int)ntohl(sb->s_sequence));
-               journal->j_transaction_sequence = ntohl(sb->s_sequence) + 1;
+                         be32_to_cpu(sb->s_sequence));
+               journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
                return 0;
        }
-       
+
        err = do_one_pass(journal, &info, PASS_SCAN);
        if (!err)
                err = do_one_pass(journal, &info, PASS_REVOKE);
        if (!err)
                err = do_one_pass(journal, &info, PASS_REPLAY);
 
-       jbd_debug(0, "JBD: recovery, exit status %d, "
+       jbd_debug(1, "JBD: recovery, exit status %d, "
                  "recovered transactions %u to %u\n",
                  err, info.start_transaction, info.end_transaction);
-       jbd_debug(0, "JBD: Replayed %d and revoked %d/%d blocks\n", 
+       jbd_debug(1, "JBD: Replayed %d and revoked %d/%d blocks\n",
                  info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
 
        /* Restart the log at the next transaction ID, thus invalidating
         * any existing commit records in the log. */
        journal->j_transaction_sequence = ++info.end_transaction;
-               
+
        journal_clear_revoke(journal);
        sync_blockdev(journal->j_fs_dev);
        return err;
 }
 
 /**
- * int journal_skip_recovery() - Start journal and wipe exiting records
+ * journal_skip_recovery - Start journal and wipe exiting records
  * @journal: journal to startup
  *
  * Locate any valid recovery information from the journal and set up the
  * journal structures in memory to ignore it (presumably because the
- * caller has evidence that it is out of date).  
+ * caller has evidence that it is out of date).
  * This function does'nt appear to be exorted..
  *
  * We perform one pass over the journal to allow us to tell the user how
  * much recovery information is being erased, and to let us initialise
- * the journal transaction sequence numbers to the next unused ID. 
+ * the journal transaction sequence numbers to the next unused ID.
  */
 int journal_skip_recovery(journal_t *journal)
 {
@@ -281,10 +285,10 @@ int journal_skip_recovery(journal_t *journal)
        journal_superblock_t *  sb;
 
        struct recovery_info    info;
-       
+
        memset (&info, 0, sizeof(info));
        sb = journal->j_superblock;
-       
+
        err = do_one_pass(journal, &info, PASS_SCAN);
 
        if (err) {
@@ -292,9 +296,9 @@ int journal_skip_recovery(journal_t *journal)
                ++journal->j_transaction_sequence;
        } else {
 #ifdef CONFIG_JBD_DEBUG
-               int dropped = info.end_transaction - ntohl(sb->s_sequence);
+               int dropped = info.end_transaction - be32_to_cpu(sb->s_sequence);
 #endif
-               jbd_debug(0, 
+               jbd_debug(1,
                          "JBD: ignoring %d transaction%s from the journal.\n",
                          dropped, (dropped == 1) ? "" : "s");
                journal->j_transaction_sequence = ++info.end_transaction;
@@ -304,6 +308,48 @@ int journal_skip_recovery(journal_t *journal)
        return err;
 }
 
+#if 0
+static inline unsigned long long read_tag_block(int tag_bytes, journal_block_tag_t *tag)
+{
+       unsigned long long block = be32_to_cpu(tag->t_blocknr);
+       if (tag_bytes > JBD_TAG_SIZE32)
+               block |= (__u64)be32_to_cpu(tag->t_blocknr_high) << 32;
+       return block;
+}
+#endif
+
+/*
+ * calc_chksums calculates the checksums for the blocks described in the
+ * descriptor block.
+ */
+static int calc_chksums(journal_t *journal, struct buffer_head *bh,
+                       unsigned long *next_log_block, __u32 *crc32_sum)
+{
+       int i, num_blks, err;
+       unsigned long io_block;
+       struct buffer_head *obh;
+
+       num_blks = count_tags(journal, bh);
+       /* Calculate checksum of the descriptor block. */
+       *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
+
+       for (i = 0; i < num_blks; i++) {
+               io_block = (*next_log_block)++;
+               wrap(journal, *next_log_block);
+               err = jread(&obh, journal, io_block);
+               if (err) {
+                       printk(KERN_ERR "JBD: IO error %d recovering block "
+                               "%lu in log\n", err, io_block);
+                       return 1;
+               } else {
+                       *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
+                                    obh->b_size);
+               }
+               brelse(obh);
+       }
+       return 0;
+}
+
 static int do_one_pass(journal_t *journal,
                        struct recovery_info *info, enum passtype pass)
 {
@@ -311,25 +357,27 @@ static int do_one_pass(journal_t *journal,
        unsigned long           next_log_block;
        int                     err, success = 0;
        journal_superblock_t *  sb;
-       journal_header_t *      tmp;
+       journal_header_t *      tmp;
        struct buffer_head *    bh;
        unsigned int            sequence;
        int                     blocktype;
-       
+       int                     tag_bytes = journal_tag_bytes(journal);
+       __u32                   crc32_sum = ~0; /* Transactional Checksums */
+
        /* Precompute the maximum metadata descriptors in a descriptor block */
        int                     MAX_BLOCKS_PER_DESC;
        MAX_BLOCKS_PER_DESC = ((journal->j_blocksize-sizeof(journal_header_t))
-                              / sizeof(journal_block_tag_t));
+                              / tag_bytes);
 
-       /* 
+       /*
         * First thing is to establish what we expect to find in the log
         * (in terms of transaction IDs), and where (in terms of log
-        * block offsets): query the superblock.  
+        * block offsets): query the superblock.
         */
 
        sb = journal->j_superblock;
-       next_commit_ID = ntohl(sb->s_sequence);
-       next_log_block = ntohl(sb->s_start);
+       next_commit_ID = be32_to_cpu(sb->s_sequence);
+       next_log_block = be32_to_cpu(sb->s_start);
 
        first_commit_ID = next_commit_ID;
        if (pass == PASS_SCAN)
@@ -341,7 +389,7 @@ static int do_one_pass(journal_t *journal,
         * Now we walk through the log, transaction by transaction,
         * making sure that each transaction has a commit block in the
         * expected place.  Each complete transaction gets replayed back
-        * into the main filesystem. 
+        * into the main filesystem.
         */
 
        while (1) {
@@ -350,11 +398,13 @@ static int do_one_pass(journal_t *journal,
                journal_block_tag_t *   tag;
                struct buffer_head *    obh;
                struct buffer_head *    nbh;
-               
+
+               cond_resched();
+
                /* If we already know where to stop the log traversal,
                 * check right now that we haven't gone past the end of
                 * the log. */
-               
+
                if (pass != PASS_SCAN)
                        if (tid_geq(next_commit_ID, info->end_transaction))
                                break;
@@ -365,7 +415,7 @@ static int do_one_pass(journal_t *journal,
                /* Skip over each chunk of the transaction looking
                 * either the next descriptor block or the final commit
                 * record. */
-               
+
                jbd_debug(3, "JBD: checking block %ld\n", next_log_block);
                err = jread(&bh, journal, next_log_block);
                if (err)
@@ -373,30 +423,30 @@ static int do_one_pass(journal_t *journal,
 
                next_log_block++;
                wrap(journal, next_log_block);
-               
-               /* What kind of buffer is it? 
-                * 
+
+               /* What kind of buffer is it?
+                *
                 * If it is a descriptor block, check that it has the
                 * expected sequence number.  Otherwise, we're all done
                 * here. */
 
                tmp = (journal_header_t *)bh->b_data;
-               
-               if (tmp->h_magic != htonl(JFS_MAGIC_NUMBER)) {
+
+               if (tmp->h_magic != cpu_to_be32(JFS_MAGIC_NUMBER)) {
                        brelse(bh);
                        break;
                }
 
-               blocktype = ntohl(tmp->h_blocktype);
-               sequence = ntohl(tmp->h_sequence);
-               jbd_debug(3, "Found magic %d, sequence %d\n", 
+               blocktype = be32_to_cpu(tmp->h_blocktype);
+               sequence = be32_to_cpu(tmp->h_sequence);
+               jbd_debug(3, "Found magic %d, sequence %d\n",
                          blocktype, sequence);
-               
+
                if (sequence != next_commit_ID) {
                        brelse(bh);
                        break;
                }
-               
+
                /* OK, we have a valid descriptor block which matches
                 * all of the sequence number checks.  What are we going
                 * to do with it?  That depends on the pass... */
@@ -404,11 +454,24 @@ static int do_one_pass(journal_t *journal,
                switch(blocktype) {
                case JFS_DESCRIPTOR_BLOCK:
                        /* If it is a valid descriptor block, replay it
-                        * in pass REPLAY; otherwise, just skip over the
-                        * blocks it describes. */
+                        * in pass REPLAY; if journal_checksums enabled, then
+                        * calculate checksums in PASS_SCAN, otherwise,
+                        * just skip over the blocks it describes. */
                        if (pass != PASS_REPLAY) {
-                               next_log_block +=
-                                       count_tags(bh, journal->j_blocksize);
+                               if (pass == PASS_SCAN &&
+                                   JFS_HAS_COMPAT_FEATURE(journal,
+                                           JFS_FEATURE_COMPAT_CHECKSUM) &&
+                                   !info->end_transaction) {
+                                       if (calc_chksums(journal, bh,
+                                                       &next_log_block,
+                                                       &crc32_sum)) {
+                                               brelse(bh);
+                                               break;
+                                       }
+                                       brelse(bh);
+                                       continue;
+                               }
+                               next_log_block += count_tags(journal, bh);
                                wrap(journal, next_log_block);
                                brelse(bh);
                                continue;
@@ -419,13 +482,13 @@ static int do_one_pass(journal_t *journal,
                         * getting done here! */
 
                        tagp = &bh->b_data[sizeof(journal_header_t)];
-                       while ((tagp - bh->b_data +sizeof(journal_block_tag_t))
+                       while ((tagp - bh->b_data + tag_bytes)
                               <= journal->j_blocksize) {
                                unsigned long io_block;
 
                                tag = (journal_block_tag_t *) tagp;
-                               flags = ntohl(tag->t_flags);
-                               
+                               flags = be32_to_cpu(tag->t_flags);
+
                                io_block = next_log_block++;
                                wrap(journal, next_log_block);
                                err = jread(&obh, journal, io_block);
@@ -433,34 +496,34 @@ static int do_one_pass(journal_t *journal,
                                        /* Recover what we can, but
                                         * report failure at the end. */
                                        success = err;
-                                       printk (KERN_ERR 
+                                       printk (KERN_ERR
                                                "JBD: IO error %d recovering "
-                                               "block %lu in log\n",
+                                               "block %ld in log\n",
                                                err, io_block);
                                } else {
                                        unsigned long blocknr;
-                                       
+
                                        J_ASSERT(obh != NULL);
-                                       blocknr = ntohl(tag->t_blocknr);
+                                       blocknr = be32_to_cpu(tag->t_blocknr);
 
                                        /* If the block has been
                                         * revoked, then we're all done
                                         * here. */
                                        if (journal_test_revoke
-                                           (journal, blocknr, 
+                                           (journal, blocknr,
                                             next_commit_ID)) {
                                                brelse(obh);
                                                ++info->nr_revoke_hits;
                                                goto skip_write;
                                        }
-                                                               
+
                                        /* Find a buffer for the new
                                         * data being restored */
-                                       nbh = __getblk(journal->j_fs_dev, 
-                                                      blocknr,
-                                                    journal->j_blocksize);
+                                       nbh = __getblk(journal->j_fs_dev,
+                                                       blocknr,
+                                                       journal->j_blocksize);
                                        if (nbh == NULL) {
-                                               printk(KERN_ERR 
+                                               printk(KERN_ERR
                                                       "JBD: Out of memory "
                                                       "during recovery.\n");
                                                err = -ENOMEM;
@@ -473,8 +536,8 @@ static int do_one_pass(journal_t *journal,
                                        memcpy(nbh->b_data, obh->b_data,
                                                        journal->j_blocksize);
                                        if (flags & JFS_FLAG_ESCAPE) {
-                                               *((unsigned int *)bh->b_data) =
-                                                       htonl(JFS_MAGIC_NUMBER);
+                                               *((__be32 *)nbh->b_data) =
+                                               cpu_to_be32(JFS_MAGIC_NUMBER);
                                        }
 
                                        BUFFER_TRACE(nbh, "marking dirty");
@@ -487,23 +550,112 @@ static int do_one_pass(journal_t *journal,
                                        brelse(obh);
                                        brelse(nbh);
                                }
-                               
+
                        skip_write:
-                               tagp += sizeof(journal_block_tag_t);
+                               tagp += tag_bytes;
                                if (!(flags & JFS_FLAG_SAME_UUID))
                                        tagp += 16;
 
                                if (flags & JFS_FLAG_LAST_TAG)
                                        break;
                        }
-                       
+
                        brelse(bh);
                        continue;
 
                case JFS_COMMIT_BLOCK:
-                       /* Found an expected commit block: not much to
-                        * do other than move on to the next sequence
+                       jbd_debug(3, "Commit block for #%u found\n",
+                                 next_commit_ID);
+                       /*     How to differentiate between interrupted commit
+                        *               and journal corruption ?
+                        *
+                        * {nth transaction}
+                        *        Checksum Verification Failed
+                        *                       |
+                        *               ____________________
+                        *              |                    |
+                        *      async_commit             sync_commit
+                        *              |                    |
+                        *              | GO TO NEXT    "Journal Corruption"
+                        *              | TRANSACTION
+                        *              |
+                        * {(n+1)th transanction}
+                        *              |
+                        *       _______|______________
+                        *      |                     |
+                        * Commit block found   Commit block not found
+                        *      |                     |
+                        * "Journal Corruption"       |
+                        *               _____________|_________
+                        *              |                       |
+                        *      nth trans corrupt       OR   nth trans
+                        *      and (n+1)th interrupted     interrupted
+                        *      before commit block
+                        *      could reach the disk.
+                        *      (Cannot find the difference in above
+                        *       mentioned conditions. Hence assume
+                        *       "Interrupted Commit".)
+                        */
+
+                       /* Found an expected commit block: if checksums
+                        * are present verify them in PASS_SCAN; else not
+                        * much to do other than move on to the next sequence
                         * number. */
+                       if (pass == PASS_SCAN &&
+                           JFS_HAS_COMPAT_FEATURE(journal,
+                                   JFS_FEATURE_COMPAT_CHECKSUM)) {
+                               int chksum_err, chksum_seen;
+                               struct commit_header *cbh =
+                                       (struct commit_header *)bh->b_data;
+                               unsigned found_chksum =
+                                       be32_to_cpu(cbh->h_chksum[0]);
+
+                               chksum_err = chksum_seen = 0;
+
+                               jbd_debug(3, "Checksums %x %x\n",
+                                         crc32_sum, found_chksum);
+                               if (info->end_transaction) {
+                                       journal->j_failed_commit =
+                                               info->end_transaction;
+                                       brelse(bh);
+                                       break;
+                               }
+
+                               if (crc32_sum == found_chksum &&
+                                   cbh->h_chksum_type == JBD2_CRC32_CHKSUM &&
+                                   cbh->h_chksum_size ==
+                                               JBD2_CRC32_CHKSUM_SIZE)
+                                      chksum_seen = 1;
+                               else if (!(cbh->h_chksum_type == 0 &&
+                                            cbh->h_chksum_size == 0 &&
+                                            found_chksum == 0 &&
+                                            !chksum_seen))
+                               /*
+                                * If fs is mounted using an old kernel and then
+                                * kernel with journal_chksum is used then we
+                                * get a situation where the journal flag has
+                                * checksum flag set but checksums are not
+                                * present i.e chksum = 0, in the individual
+                                * commit blocks.
+                                * Hence to avoid checksum failures, in this
+                                * situation, this extra check is added.
+                                */
+                                               chksum_err = 1;
+
+                               if (chksum_err) {
+                                       info->end_transaction = next_commit_ID;
+                                       jbd_debug(1, "Checksum_err %x %x\n",
+                                                 crc32_sum, found_chksum);
+                                       if (!JFS_HAS_INCOMPAT_FEATURE(journal,
+                                          JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)){
+                                               journal->j_failed_commit =
+                                                       next_commit_ID;
+                                               brelse(bh);
+                                               break;
+                                       }
+                               }
+                               crc32_sum = ~0;
+                       }
                        brelse(bh);
                        next_commit_ID++;
                        continue;
@@ -532,16 +684,17 @@ static int do_one_pass(journal_t *journal,
        }
 
  done:
-       /* 
+       /*
         * We broke out of the log scan loop: either we came to the
         * known end of the log or we found an unexpected block in the
         * log.  If the latter happened, then we know that the "current"
         * transaction marks the end of the valid log.
         */
-       
-       if (pass == PASS_SCAN)
-               info->end_transaction = next_commit_ID;
-       else {
+
+       if (pass == PASS_SCAN) {
+               if (!info->end_transaction)
+                       info->end_transaction = next_commit_ID;
+       } else {
                /* It's really bad news if different passes end up at
                 * different places (but possible due to IO errors). */
                if (info->end_transaction != next_commit_ID) {
@@ -562,7 +715,7 @@ static int do_one_pass(journal_t *journal,
 
 /* Scan a revoke record, marking all blocks mentioned as revoked. */
 
-static int scan_revoke_records(journal_t *journal, struct buffer_head *bh, 
+static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
                               tid_t sequence, struct recovery_info *info)
 {
        journal_revoke_header_t *header;
@@ -570,13 +723,13 @@ static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
 
        header = (journal_revoke_header_t *) bh->b_data;
        offset = sizeof(journal_revoke_header_t);
-       max = ntohl(header->r_count);
-       
+       max = be32_to_cpu(header->r_count);
+
        while (offset < max) {
                unsigned long blocknr;
                int err;
-               
-               blocknr = ntohl(* ((unsigned int *) (bh->b_data+offset)));
+
+               blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
                offset += 4;
                err = journal_set_revoke(journal, blocknr, sequence);
                if (err)