Btrfs: remove btrfs_init_path
[safe/jmp/linux-2.6] / fs / jbd2 / recovery.c
index b2012d1..7306328 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * linux/fs/recovery.c
+ * linux/fs/jbd2/recovery.c
  *
  * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
  *
@@ -21,6 +21,7 @@
 #include <linux/jbd2.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
+#include <linux/crc32.h>
 #endif
 
 /*
@@ -70,7 +71,7 @@ static int do_readahead(journal_t *journal, unsigned int start)
 {
        int err;
        unsigned int max, nbufs, next;
-       unsigned long blocknr;
+       unsigned long long blocknr;
        struct buffer_head *bh;
 
        struct buffer_head * bufs[MAXBUF];
@@ -132,7 +133,7 @@ static int jread(struct buffer_head **bhp, journal_t *journal,
                 unsigned int offset)
 {
        int err;
-       unsigned long blocknr;
+       unsigned long long blocknr;
        struct buffer_head *bh;
 
        *bhp = NULL;
@@ -178,19 +179,20 @@ 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);
+               tagp += tag_bytes;
                if (!(tag->t_flags & cpu_to_be32(JBD2_FLAG_SAME_UUID)))
                        tagp += 16;
 
@@ -223,7 +225,7 @@ do {                                                                        \
  */
 int jbd2_journal_recover(journal_t *journal)
 {
-       int                     err;
+       int                     err, err2;
        journal_superblock_t *  sb;
 
        struct recovery_info    info;
@@ -250,10 +252,10 @@ int jbd2_journal_recover(journal_t *journal)
        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
@@ -261,7 +263,10 @@ int jbd2_journal_recover(journal_t *journal)
        journal->j_transaction_sequence = ++info.end_transaction;
 
        jbd2_journal_clear_revoke(journal);
-       sync_blockdev(journal->j_fs_dev);
+       err2 = sync_blockdev(journal->j_fs_dev);
+       if (!err)
+               err = err2;
+
        return err;
 }
 
@@ -294,10 +299,10 @@ int jbd2_journal_skip_recovery(journal_t *journal)
                printk(KERN_ERR "JBD: error %d scanning journal\n", err);
                ++journal->j_transaction_sequence;
        } else {
-#ifdef CONFIG_JBD_DEBUG
+#ifdef CONFIG_JBD2_DEBUG
                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;
@@ -307,6 +312,46 @@ int jbd2_journal_skip_recovery(journal_t *journal)
        return err;
 }
 
+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 > JBD2_TAG_SIZE32)
+               block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
+       return block;
+}
+
+/*
+ * 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);
+               }
+               put_bh(obh);
+       }
+       return 0;
+}
+
 static int do_one_pass(journal_t *journal,
                        struct recovery_info *info, enum passtype pass)
 {
@@ -318,11 +363,13 @@ static int do_one_pass(journal_t *journal,
        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
@@ -354,7 +401,7 @@ static int do_one_pass(journal_t *journal,
                struct buffer_head *    obh;
                struct buffer_head *    nbh;
 
-               cond_resched();         /* We're under lock_kernel() */
+               cond_resched();
 
                /* If we already know where to stop the log traversal,
                 * check right now that we haven't gone past the end of
@@ -409,13 +456,26 @@ static int do_one_pass(journal_t *journal,
                switch(blocktype) {
                case JBD2_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 &&
+                                   JBD2_HAS_COMPAT_FEATURE(journal,
+                                           JBD2_FEATURE_COMPAT_CHECKSUM) &&
+                                   !info->end_transaction) {
+                                       if (calc_chksums(journal, bh,
+                                                       &next_log_block,
+                                                       &crc32_sum)) {
+                                               put_bh(bh);
+                                               break;
+                                       }
+                                       put_bh(bh);
+                                       continue;
+                               }
+                               next_log_block += count_tags(journal, bh);
                                wrap(journal, next_log_block);
-                               brelse(bh);
+                               put_bh(bh);
                                continue;
                        }
 
@@ -424,7 +484,7 @@ 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;
 
@@ -443,10 +503,11 @@ static int do_one_pass(journal_t *journal,
                                                "block %ld in log\n",
                                                err, io_block);
                                } else {
-                                       unsigned long blocknr;
+                                       unsigned long long blocknr;
 
                                        J_ASSERT(obh != NULL);
-                                       blocknr = be32_to_cpu(tag->t_blocknr);
+                                       blocknr = read_tag_block(tag_bytes,
+                                                                tag);
 
                                        /* If the block has been
                                         * revoked, then we're all done
@@ -478,7 +539,7 @@ static int do_one_pass(journal_t *journal,
                                        memcpy(nbh->b_data, obh->b_data,
                                                        journal->j_blocksize);
                                        if (flags & JBD2_FLAG_ESCAPE) {
-                                               *((__be32 *)bh->b_data) =
+                                               *((__be32 *)nbh->b_data) =
                                                cpu_to_be32(JBD2_MAGIC_NUMBER);
                                        }
 
@@ -494,7 +555,7 @@ static int do_one_pass(journal_t *journal,
                                }
 
                        skip_write:
-                               tagp += sizeof(journal_block_tag_t);
+                               tagp += tag_bytes;
                                if (!(flags & JBD2_FLAG_SAME_UUID))
                                        tagp += 16;
 
@@ -506,9 +567,93 @@ static int do_one_pass(journal_t *journal,
                        continue;
 
                case JBD2_COMMIT_BLOCK:
-                       /* Found an expected commit block: not much to
-                        * do other than move on to the next sequence
+                       /*     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 &&
+                           JBD2_HAS_COMPAT_FEATURE(journal,
+                                   JBD2_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;
+
+                               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;
+
+                                       if (!JBD2_HAS_INCOMPAT_FEATURE(journal,
+                                          JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)){
+                                               journal->j_failed_commit =
+                                                       next_commit_ID;
+                                               brelse(bh);
+                                               break;
+                                       }
+                               }
+                               crc32_sum = ~0;
+                       }
                        brelse(bh);
                        next_commit_ID++;
                        continue;
@@ -544,9 +689,10 @@ static int do_one_pass(journal_t *journal,
         * 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) {
@@ -572,17 +718,24 @@ static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
 {
        jbd2_journal_revoke_header_t *header;
        int offset, max;
+       int record_len = 4;
 
        header = (jbd2_journal_revoke_header_t *) bh->b_data;
        offset = sizeof(jbd2_journal_revoke_header_t);
        max = be32_to_cpu(header->r_count);
 
-       while (offset < max) {
-               unsigned long blocknr;
+       if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
+               record_len = 8;
+
+       while (offset + record_len <= max) {
+               unsigned long long blocknr;
                int err;
 
-               blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
-               offset += 4;
+               if (record_len == 4)
+                       blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
+               else
+                       blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
+               offset += record_len;
                err = jbd2_journal_set_revoke(journal, blocknr, sequence);
                if (err)
                        return err;