Whamcloud - gitweb
debugfs: add fast commit support to logdump
[tools/e2fsprogs.git] / debugfs / logdump.c
index 70a7c36..151be3e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * logdump.c --- dump the contents of the journal out to a file
  *
- * Authro: Stephen C. Tweedie, 2001  <sct@redhat.com>
+ * Author: Stephen C. Tweedie, 2001  <sct@redhat.com>
  * Copyright (C) 2001 Red Hat, Inc.
  * Based on portions  Copyright (C) 1994 Theodore Ts'o.
  *
@@ -33,13 +33,14 @@ extern char *optarg;
 #include "debugfs.h"
 #include "blkid/blkid.h"
 #include "jfs_user.h"
+#include "ext2fs/fast_commit.h"
 #include <uuid/uuid.h>
 
 enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
 
 #define ANY_BLOCK ((blk64_t) -1)
 
-static int             dump_all, dump_old, dump_contents, dump_descriptors;
+static int             dump_all, dump_super, dump_old, dump_contents, dump_descriptors;
 static blk64_t         block_to_dump, bitmap_to_dump, inode_block_to_dump;
 static unsigned int    group_to_dump, inode_offset_to_dump;
 static ext2_ino_t      inode_to_dump;
@@ -65,6 +66,9 @@ static void dump_metadata_block(FILE *, struct journal_source *,
                                unsigned int, unsigned int, unsigned int,
                                int, tid_t);
 
+static void dump_fc_block(FILE *out_file, char *buf, int blocksize,
+                         int transaction, int *fc_done, int dump_old);
+
 static void do_hexdump (FILE *, char *, int);
 
 #define WRAP(jsb, blocknr)                                     \
@@ -72,7 +76,8 @@ static void do_hexdump (FILE *, char *, int);
                blocknr -= (be32_to_cpu((jsb)->s_maxlen) -      \
                            be32_to_cpu((jsb)->s_first));
 
-void do_logdump(int argc, char **argv)
+void do_logdump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
+                   void *infop EXT2FS_ATTR((unused)))
 {
        int             c;
        int             retval;
@@ -96,6 +101,7 @@ void do_logdump(int argc, char **argv)
        dump_all = 0;
        dump_old = 0;
        dump_contents = 0;
+       dump_super = 0;
        dump_descriptors = 1;
        block_to_dump = ANY_BLOCK;
        bitmap_to_dump = -1;
@@ -103,7 +109,7 @@ void do_logdump(int argc, char **argv)
        inode_to_dump = -1;
 
        reset_getopt();
-       while ((c = getopt (argc, argv, "ab:ci:f:Os")) != EOF) {
+       while ((c = getopt (argc, argv, "ab:ci:f:OsS")) != EOF) {
                switch (c) {
                case 'a':
                        dump_all++;
@@ -133,6 +139,9 @@ void do_logdump(int argc, char **argv)
                case 's':
                        use_sb++;
                        break;
+               case 'S':
+                       dump_super++;
+                       break;
                default:
                        goto print_usage;
                }
@@ -271,13 +280,13 @@ errout:
        return;
 
 print_usage:
-       fprintf(stderr, "%s: Usage: logdump [-acsO] [-b<block>] [-i<filespec>]\n\t"
+       fprintf(stderr, "%s: Usage: logdump [-acsOS] [-b<block>] [-i<filespec>]\n\t"
                "[-f<journal_file>] [output_file]\n", argv[0]);
 }
 
 
 static int read_journal_block(const char *cmd, struct journal_source *source,
-                             off_t offset, char *buf, unsigned int size)
+                             ext2_loff_t offset, char *buf, unsigned int size)
 {
        int retval;
        unsigned int got;
@@ -295,8 +304,8 @@ static int read_journal_block(const char *cmd, struct journal_source *source,
                got = retval;
                retval = 0;
        } else {
-               retval = ext2fs_file_lseek(source->file, offset,
-                                          EXT2_SEEK_SET, NULL);
+               retval = ext2fs_file_llseek(source->file, offset,
+                                           EXT2_SEEK_SET, NULL);
                if (retval) {
                seek_err:
                        com_err(cmd, retval, "while seeking in reading journal");
@@ -320,15 +329,15 @@ static int read_journal_block(const char *cmd, struct journal_source *source,
 static const char *type_to_name(int btype)
 {
        switch (btype) {
-       case JFS_DESCRIPTOR_BLOCK:
+       case JBD2_DESCRIPTOR_BLOCK:
                return "descriptor block";
-       case JFS_COMMIT_BLOCK:
+       case JBD2_COMMIT_BLOCK:
                return "commit block";
-       case JFS_SUPERBLOCK_V1:
+       case JBD2_SUPERBLOCK_V1:
                return "V1 superblock";
-       case JFS_SUPERBLOCK_V2:
+       case JBD2_SUPERBLOCK_V2:
                return "V2 superblock";
-       case JFS_REVOKE_BLOCK:
+       case JBD2_REVOKE_BLOCK:
                return "revoke table";
        }
        return "unrecognised type";
@@ -346,9 +355,9 @@ static void dump_journal(char *cmdname, FILE *out_file,
        int                     retval;
        __u32                   magic, sequence, blocktype;
        journal_header_t        *header;
-
        tid_t                   transaction;
        unsigned int            blocknr = 0;
+       int                     fc_done;
 
        /* First, check to see if there's an ext2 superblock header */
        retval = read_journal_block(cmdname, source, 0, buf, 2048);
@@ -362,9 +371,9 @@ static void dump_journal(char *cmdname, FILE *out_file,
                ext2fs_swap_super(sb);
 #endif
 
-       if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
+       if ((be32_to_cpu(jsb->s_header.h_magic) != JBD2_MAGIC_NUMBER) &&
            (sb->s_magic == EXT2_SUPER_MAGIC) &&
-           (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
+           ext2fs_has_feature_journal_dev(sb)) {
                blocksize = EXT2_BLOCK_SIZE(sb);
                blocknr = (blocksize == 1024) ? 2 : 1;
                uuid_unparse(sb->s_uuid, jsb_buffer);
@@ -378,14 +387,20 @@ static void dump_journal(char *cmdname, FILE *out_file,
        }
 
        /* Next, read the journal superblock */
-
-       retval = read_journal_block(cmdname, source, blocknr*blocksize,
+       retval = read_journal_block(cmdname, source,
+                                   ((ext2_loff_t) blocknr) * blocksize,
                                    jsb_buffer, 1024);
        if (retval)
                return;
 
+       if (dump_super) {
+               e2p_list_journal_super(out_file, jsb_buffer,
+                                      current_fs->blocksize, 0);
+               fputc('\n', out_file);
+       }
+
        jsb = (journal_superblock_t *) jsb_buffer;
-       if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
+       if (be32_to_cpu(jsb->s_header.h_magic) != JBD2_MAGIC_NUMBER) {
                fprintf(out_file,
                        "Journal superblock magic number invalid!\n");
                return;
@@ -400,17 +415,17 @@ static void dump_journal(char *cmdname, FILE *out_file,
        if (!blocknr) {
                /* Empty journal, nothing to do. */
                if (!dump_old)
-                       return;
+                       goto fc;
                else
                        blocknr = 1;
        }
 
        while (1) {
                retval = read_journal_block(cmdname, source,
-                                           blocknr*blocksize, buf,
-                                           blocksize);
+                               ((ext2_loff_t) blocknr) * blocksize,
+                               buf, blocksize);
                if (retval)
-                       return;
+                       break;
 
                header = (journal_header_t *) buf;
 
@@ -418,10 +433,10 @@ static void dump_journal(char *cmdname, FILE *out_file,
                sequence = be32_to_cpu(header->h_sequence);
                blocktype = be32_to_cpu(header->h_blocktype);
 
-               if (magic != JFS_MAGIC_NUMBER) {
+               if (magic != JBD2_MAGIC_NUMBER) {
                        fprintf (out_file, "No magic number at block %u: "
                                 "end of journal.\n", blocknr);
-                       return;
+                       break;
                }
 
                if (sequence != transaction) {
@@ -429,7 +444,7 @@ static void dump_journal(char *cmdname, FILE *out_file,
                                 "block %u: end of journal.\n",
                                 sequence, transaction, blocknr);
                        if (!dump_old)
-                               return;
+                               break;
                }
 
                if (dump_descriptors) {
@@ -440,19 +455,19 @@ static void dump_journal(char *cmdname, FILE *out_file,
                }
 
                switch (blocktype) {
-               case JFS_DESCRIPTOR_BLOCK:
+               case JBD2_DESCRIPTOR_BLOCK:
                        dump_descriptor_block(out_file, source, buf, jsb,
                                              &blocknr, blocksize,
                                              transaction);
                        continue;
 
-               case JFS_COMMIT_BLOCK:
+               case JBD2_COMMIT_BLOCK:
                        transaction++;
                        blocknr++;
                        WRAP(jsb, blocknr);
                        continue;
 
-               case JFS_REVOKE_BLOCK:
+               case JBD2_REVOKE_BLOCK:
                        dump_revoke_block(out_file, buf, jsb,
                                          blocknr, blocksize,
                                          transaction);
@@ -463,11 +478,135 @@ static void dump_journal(char *cmdname, FILE *out_file,
                default:
                        fprintf (out_file, "Unexpected block type %u at "
                                 "block %u.\n", blocktype, blocknr);
-                       return;
+                       break;
                }
        }
+
+fc:
+       blocknr = be32_to_cpu(jsb->s_maxlen) - jbd2_journal_get_num_fc_blks(jsb) + 1;
+       while (blocknr <= be32_to_cpu(jsb->s_maxlen)) {
+               retval = read_journal_block(cmdname, source,
+                               ((ext2_loff_t) blocknr) * blocksize,
+                               buf, blocksize);
+               if (retval)
+                       return;
+
+               dump_fc_block(out_file, buf, blocksize, transaction, &fc_done,
+                       dump_old);
+               if (!dump_old && fc_done)
+                       break;
+               blocknr++;
+       }
+}
+
+static inline size_t journal_super_tag_bytes(journal_superblock_t *jsb)
+{
+       size_t sz;
+
+       if (JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_CSUM_V3))
+               return sizeof(journal_block_tag3_t);
+
+       sz = sizeof(journal_block_tag_t);
+
+       if (JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_CSUM_V2))
+               sz += sizeof(__u16);
+
+       if (JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_64BIT))
+               return sz;
+
+       return sz - sizeof(__u32);
 }
 
+static void dump_fc_block(FILE *out_file, char *buf, int blocksize,
+       int transaction, int *fc_done, int dump_old)
+{
+       struct ext4_fc_tl       *tl;
+       struct ext4_fc_head     *head;
+       struct ext4_fc_add_range        *add_range;
+       struct ext4_fc_del_range        *del_range;
+       struct ext4_fc_dentry_info      *dentry_info;
+       struct ext4_fc_tail             *tail;
+       struct ext3_extent      *ex;
+
+       *fc_done = 0;
+       fc_for_each_tl(buf, buf + blocksize, tl) {
+               switch (le16_to_cpu(tl->fc_tag)) {
+               case EXT4_FC_TAG_ADD_RANGE:
+                       add_range =
+                               (struct ext4_fc_add_range *)ext4_fc_tag_val(tl);
+                       ex = (struct ext3_extent *)add_range->fc_ex;
+                       fprintf(out_file,
+                               "tag %s, inode %d, lblk %d, pblk %ld, len %d\n",
+                               tag2str(tl->fc_tag),
+                               le32_to_cpu(add_range->fc_ino),
+                               le32_to_cpu(ex->ee_block),
+                               le32_to_cpu(ex->ee_start) +
+                               (((__u64) le16_to_cpu(ex->ee_start_hi)) << 32),
+                               le16_to_cpu(ex->ee_len) > EXT_INIT_MAX_LEN ?
+                               le16_to_cpu(ex->ee_len) - EXT_INIT_MAX_LEN :
+                               le16_to_cpu(ex->ee_len));
+                       break;
+               case EXT4_FC_TAG_DEL_RANGE:
+                       del_range =
+                               (struct ext4_fc_del_range *)ext4_fc_tag_val(tl);
+                       fprintf(out_file, "tag %s, inode %d, lblk %d, len %d\n",
+                               tag2str(tl->fc_tag),
+                               le32_to_cpu(del_range->fc_ino),
+                               le32_to_cpu(del_range->fc_lblk),
+                               le32_to_cpu(del_range->fc_len));
+                       break;
+               case EXT4_FC_TAG_LINK:
+               case EXT4_FC_TAG_UNLINK:
+               case EXT4_FC_TAG_CREAT:
+                       dentry_info =
+                               (struct ext4_fc_dentry_info *)
+                                       ext4_fc_tag_val(tl);
+                       fprintf(out_file,
+                               "tag %s, parent %d, ino %d, name \"%s\"\n",
+                               tag2str(tl->fc_tag),
+                               le32_to_cpu(dentry_info->fc_parent_ino),
+                               le32_to_cpu(dentry_info->fc_ino),
+                               dentry_info->fc_dname);
+                       break;
+               case EXT4_FC_TAG_INODE:
+                       fprintf(out_file, "tag %s, inode %d\n",
+                               tag2str(tl->fc_tag),
+                               le32_to_cpu(((struct ext4_fc_inode *)
+                                       ext4_fc_tag_val(tl))->fc_ino));
+                       break;
+               case EXT4_FC_TAG_PAD:
+                       fprintf(out_file, "tag %s\n", tag2str(tl->fc_tag));
+                       break;
+               case EXT4_FC_TAG_TAIL:
+                       tail = (struct ext4_fc_tail *)ext4_fc_tag_val(tl);
+                       fprintf(out_file, "tag %s, tid %d\n",
+                               tag2str(tl->fc_tag),
+                               le32_to_cpu(tail->fc_tid));
+                       if (!dump_old &&
+                               le32_to_cpu(tail->fc_tid) < transaction) {
+                               *fc_done = 1;
+                               return;
+                       }
+                       break;
+               case EXT4_FC_TAG_HEAD:
+                       fprintf(out_file, "\n*** Fast Commit Area ***\n");
+                       head = (struct ext4_fc_head *)ext4_fc_tag_val(tl);
+                       fprintf(out_file, "tag %s, features 0x%x, tid %d\n",
+                               tag2str(tl->fc_tag),
+                               le32_to_cpu(head->fc_features),
+                               le32_to_cpu(head->fc_tid));
+                       if (!dump_old &&
+                               le32_to_cpu(head->fc_tid) < transaction) {
+                               *fc_done = 1;
+                               return;
+                       }
+                       break;
+               default:
+                       *fc_done = 1;
+                       break;
+               }
+       }
+}
 
 static void dump_descriptor_block(FILE *out_file,
                                  struct journal_source *source,
@@ -487,9 +626,9 @@ static void dump_descriptor_block(FILE *out_file,
        offset = sizeof(journal_header_t);
        blocknr = *blockp;
 
-       if (JSB_HAS_INCOMPAT_FEATURE(jsb, JFS_FEATURE_INCOMPAT_CSUM_V3) ||
-           JSB_HAS_INCOMPAT_FEATURE(jsb, JFS_FEATURE_INCOMPAT_CSUM_V2))
-               csum_size = sizeof(struct journal_block_tail);
+       if (JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_CSUM_V3) ||
+           JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_CSUM_V2))
+               csum_size = sizeof(struct jbd2_journal_block_tail);
 
        if (dump_all)
                fprintf(out_file, "Dumping descriptor block, sequence %u, at "
@@ -513,7 +652,7 @@ static void dump_descriptor_block(FILE *out_file,
                tag_block = be32_to_cpu(tag->t_blocknr);
                tag_flags = be16_to_cpu(tag->t_flags);
 
-               if (!(tag_flags & JFS_FLAG_SAME_UUID))
+               if (!(tag_flags & JBD2_FLAG_SAME_UUID))
                        offset += 16;
 
                dump_metadata_block(out_file, source, jsb,
@@ -523,7 +662,7 @@ static void dump_descriptor_block(FILE *out_file,
                ++blocknr;
                WRAP(jsb, blocknr);
 
-       } while (!(tag_flags & JFS_FLAG_LAST_TAG));
+       } while (!(tag_flags & JBD2_FLAG_LAST_TAG));
 
        *blockp = blocknr;
 }
@@ -536,7 +675,7 @@ static void dump_revoke_block(FILE *out_file, char *buf,
                              tid_t transaction)
 {
        int                     offset, max;
-       journal_revoke_header_t *header;
+       jbd2_journal_revoke_header_t *header;
        unsigned long long      rblock;
        int                     tag_size = sizeof(__u32);
 
@@ -544,11 +683,11 @@ static void dump_revoke_block(FILE *out_file, char *buf,
                fprintf(out_file, "Dumping revoke block, sequence %u, at "
                        "block %u:\n", transaction, blocknr);
 
-       if (be32_to_cpu(jsb->s_feature_incompat) & JFS_FEATURE_INCOMPAT_64BIT)
+       if (be32_to_cpu(jsb->s_feature_incompat) & JBD2_FEATURE_INCOMPAT_64BIT)
                tag_size = sizeof(__u64);
 
-       header = (journal_revoke_header_t *) buf;
-       offset = sizeof(journal_revoke_header_t);
+       header = (jbd2_journal_revoke_header_t *) buf;
+       offset = sizeof(jbd2_journal_revoke_header_t);
        max = be32_to_cpu(header->r_count);
 
        while (offset < max) {
@@ -629,7 +768,7 @@ static void dump_metadata_block(FILE *out_file, struct journal_source *source,
                return;
 
        retval = read_journal_block("logdump", source,
-                                   blocksize * log_blocknr,
+                                   ((ext2_loff_t) log_blocknr) * blocksize,
                                    buf, blocksize);
        if (retval)
                return;