#include "debugfs.h"
#include "blkid/blkid.h"
#include "jfs_user.h"
+#if __GNUC_PREREQ (4, 6)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+#endif
#include "ext2fs/fast_commit.h"
+#if __GNUC_PREREQ (4, 6)
+#pragma GCC diagnostic pop
+#endif
#include <uuid/uuid.h>
enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
#define ANY_BLOCK ((blk64_t) -1)
static int dump_all, dump_super, dump_old, dump_contents, dump_descriptors;
+static int64_t dump_counts;
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;
static void dump_descriptor_block(FILE *, struct journal_source *,
char *, journal_superblock_t *,
- unsigned int *, int, __u32, tid_t);
+ unsigned int *, unsigned int, __u32, tid_t);
static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
- unsigned int, int, tid_t);
+ unsigned int, unsigned int, tid_t);
static void dump_metadata_block(FILE *, struct journal_source *,
journal_superblock_t*,
unsigned int, unsigned int, unsigned int,
- int, tid_t);
+ unsigned int, tid_t);
static void dump_fc_block(FILE *out_file, char *buf, int blocksize,
- int transaction, int *fc_done, int dump_old);
+ tid_t transaction, int *fc_done);
static void do_hexdump (FILE *, char *, int);
char *inode_spec = NULL;
char *journal_fn = NULL;
- int journal_fd = 0;
+ int journal_fd = -1;
int use_sb = 0;
ext2_ino_t journal_inum;
struct ext2_inode journal_inode;
- ext2_file_t journal_file;
+ ext2_file_t journal_file = NULL;
char *tmp;
struct journal_source journal_source;
struct ext2_super_block *es = NULL;
bitmap_to_dump = -1;
inode_block_to_dump = ANY_BLOCK;
inode_to_dump = -1;
+ dump_counts = -1;
reset_getopt();
- while ((c = getopt (argc, argv, "ab:ci:f:OsS")) != EOF) {
+ while ((c = getopt (argc, argv, "ab:ci:f:OsSn:")) != EOF) {
switch (c) {
case 'a':
dump_all++;
case 'S':
dump_super++;
break;
+ case 'n':
+ dump_counts = strtol(optarg, &tmp, 10);
+ if (*tmp) {
+ com_err(argv[0], 0,
+ "Bad log counts number - %s", optarg);
+ return;
+ }
+ break;
default:
goto print_usage;
}
goto print_usage;
}
- if (current_fs)
- es = current_fs->super;
-
if (inode_spec) {
int inode_group, group_offset, inodes_per_block;
if (!inode_to_dump)
return;
+ es = current_fs->super;
inode_group = ((inode_to_dump - 1)
/ es->s_inodes_per_group);
group_offset = ((inode_to_dump - 1)
if (!out_file) {
com_err(argv[0], errno, "while opening %s for logdump",
out_fn);
- goto errout;
+ goto cleanup;
}
}
- if (block_to_dump != ANY_BLOCK && current_fs != NULL) {
+ if (block_to_dump != ANY_BLOCK) {
+ if (check_fs_open(argv[0]))
+ goto cleanup;
+ es = current_fs->super;
group_to_dump = ((block_to_dump -
es->s_first_data_block)
/ es->s_blocks_per_group);
bitmap_to_dump = ext2fs_block_bitmap_loc(current_fs, group_to_dump);
}
- if (!journal_fn && check_fs_open(argv[0]))
- goto errout;
-
if (journal_fn) {
/* Set up to read journal from a regular file somewhere */
journal_fd = open(journal_fn, O_RDONLY, 0);
if (journal_fd < 0) {
com_err(argv[0], errno, "while opening %s for logdump",
journal_fn);
- goto errout;
+ goto cleanup;
}
-
journal_source.where = JOURNAL_IS_EXTERNAL;
journal_source.fd = journal_fd;
- } else if ((journal_inum = es->s_journal_inum)) {
+ dump_journal(argv[0], out_file, &journal_source);
+ goto cleanup;
+
+ }
+ if (check_fs_open(argv[0]))
+ goto cleanup;
+ es = current_fs->super;
+
+ if ((journal_inum = es->s_journal_inum)) {
if (use_sb) {
if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
com_err(argv[0], 0,
"no journal backup in super block\n");
- goto errout;
+ goto cleanup;
}
memset(&journal_inode, 0, sizeof(struct ext2_inode));
memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
} else {
if (debugfs_read_inode(journal_inum, &journal_inode,
argv[0]))
- goto errout;
+ goto cleanup;
}
retval = ext2fs_file_open2(current_fs, journal_inum,
&journal_inode, 0, &journal_file);
if (retval) {
com_err(argv[0], retval, "while opening ext2 file");
- goto errout;
+ goto cleanup;
}
journal_source.where = JOURNAL_IS_INTERNAL;
journal_source.file = journal_file;
journal_fn = blkid_devno_to_devname(es->s_journal_dev);
if (!journal_fn) {
com_err(argv[0], 0, "filesystem has no journal");
- goto errout;
+ goto cleanup;
}
journal_fd = open(journal_fn, O_RDONLY, 0);
if (journal_fd < 0) {
com_err(argv[0], errno, "while opening %s for logdump",
journal_fn);
free(journal_fn);
- goto errout;
+ goto cleanup;
}
fprintf(out_file, "Using external journal found at %s\n",
journal_fn);
journal_source.where = JOURNAL_IS_EXTERNAL;
journal_source.fd = journal_fd;
}
-
dump_journal(argv[0], out_file, &journal_source);
-
- if (journal_source.where == JOURNAL_IS_INTERNAL)
- ext2fs_file_close(journal_file);
- else
+cleanup:
+ if (journal_fd >= 0)
close(journal_fd);
-
-errout:
+ if (journal_file)
+ ext2fs_file_close(journal_file);
if (out_file && (out_file != stdout))
fclose(out_file);
return;
print_usage:
- fprintf(stderr, "%s: Usage: logdump [-acsOS] [-b<block>] [-i<filespec>]\n\t"
+ fprintf(stderr, "%s: Usage: logdump [-acsOS] [-n<num_trans>] [-b<block>] [-i<filespec>]\n\t"
"[-f<journal_file>] [output_file]\n", argv[0]);
}
journal_header_t *header;
tid_t transaction;
unsigned int blocknr = 0;
+ unsigned int first_transaction_blocknr;
int fc_done;
__u64 total_len;
__u32 maxlen;
+ int64_t cur_counts = 0;
+ bool exist_no_magic = false;
/* First, check to see if there's an ext2 superblock header */
retval = read_journal_block(cmdname, source, 0, buf, 2048);
blocknr = 1;
}
+ first_transaction_blocknr = blocknr;
+
while (1) {
+ if (dump_old && (dump_counts != -1) && (cur_counts >= dump_counts))
+ break;
+
+ if ((blocknr == first_transaction_blocknr) &&
+ (cur_counts != 0) && dump_old && (dump_counts != -1)) {
+ fprintf(out_file, "Dump all %lld journal records.\n",
+ (long long) cur_counts);
+ break;
+ }
+
retval = read_journal_block(cmdname, source,
((ext2_loff_t) blocknr) * blocksize,
buf, blocksize);
blocktype = be32_to_cpu(header->h_blocktype);
if (magic != JBD2_MAGIC_NUMBER) {
- fprintf (out_file, "No magic number at block %u: "
- "end of journal.\n", blocknr);
+ if (exist_no_magic == false) {
+ exist_no_magic = true;
+ fprintf(out_file, "No magic number at block %u: "
+ "end of journal.\n", blocknr);
+ }
+ if (dump_old && (dump_counts != -1)) {
+ blocknr++;
+ WRAP(jsb, blocknr, maxlen);
+ continue;
+ }
break;
}
continue;
case JBD2_COMMIT_BLOCK:
+ cur_counts++;
transaction++;
blocknr++;
WRAP(jsb, blocknr, maxlen);
if (retval)
return;
- dump_fc_block(out_file, buf, blocksize, transaction, &fc_done,
- dump_old);
+ dump_fc_block(out_file, buf, blocksize, transaction, &fc_done);
if (!dump_old && fc_done)
break;
blocknr++;
}
static void dump_fc_block(FILE *out_file, char *buf, int blocksize,
- int transaction, int *fc_done, int dump_old)
+ tid_t transaction, int *fc_done)
{
- struct ext4_fc_tl *tl;
+ 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;
+ __u8 *cur, *val;
*fc_done = 0;
- fc_for_each_tl(buf, buf + blocksize, tl) {
- switch (le16_to_cpu(tl->fc_tag)) {
+ for (cur = (__u8 *)buf; cur < (__u8 *)buf + blocksize;
+ cur = cur + sizeof(tl) + le16_to_cpu(tl.fc_len)) {
+ memcpy(&tl, cur, sizeof(tl));
+ val = cur + sizeof(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);
+ add_range = (struct ext4_fc_add_range *)val;
ex = (struct ext3_extent *)add_range->fc_ex;
fprintf(out_file,
"tag %s, inode %d, lblk %u, pblk %llu, len %lu\n",
- tag2str(tl->fc_tag),
+ tag2str(tl.fc_tag),
le32_to_cpu(add_range->fc_ino),
le32_to_cpu(ex->ee_block),
le32_to_cpu(ex->ee_start) +
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);
+ del_range = (struct ext4_fc_del_range *)val;
fprintf(out_file, "tag %s, inode %d, lblk %d, len %d\n",
- tag2str(tl->fc_tag),
+ 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));
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);
+ dentry_info = (struct ext4_fc_dentry_info *)val;
fprintf(out_file,
"tag %s, parent %d, ino %d, name \"%s\"\n",
- tag2str(tl->fc_tag),
+ 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));
+ tag2str(tl.fc_tag),
+ le32_to_cpu(((struct ext4_fc_inode *)val)->fc_ino));
break;
case EXT4_FC_TAG_PAD:
- fprintf(out_file, "tag %s\n", tag2str(tl->fc_tag));
+ 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);
+ tail = (struct ext4_fc_tail *)val;
fprintf(out_file, "tag %s, tid %d\n",
- tag2str(tl->fc_tag),
+ tag2str(tl.fc_tag),
le32_to_cpu(tail->fc_tid));
if (!dump_old &&
le32_to_cpu(tail->fc_tid) < transaction) {
break;
case EXT4_FC_TAG_HEAD:
fprintf(out_file, "\n*** Fast Commit Area ***\n");
- head = (struct ext4_fc_head *)ext4_fc_tag_val(tl);
+ head = (struct ext4_fc_head *)val;
fprintf(out_file, "tag %s, features 0x%x, tid %d\n",
- tag2str(tl->fc_tag),
+ tag2str(tl.fc_tag),
le32_to_cpu(head->fc_features),
le32_to_cpu(head->fc_tid));
if (!dump_old &&
struct journal_source *source,
char *buf,
journal_superblock_t *jsb,
- unsigned int *blockp, int blocksize,
+ unsigned int *blockp, unsigned blocksize,
__u32 maxlen,
tid_t transaction)
{
- int offset, tag_size, csum_size = 0;
+ unsigned offset, tag_size, csum_size = 0;
char *tagp;
journal_block_tag_t *tag;
unsigned int blocknr;
static void dump_revoke_block(FILE *out_file, char *buf,
journal_superblock_t *jsb EXT2FS_ATTR((unused)),
unsigned int blocknr,
- int blocksize EXT2FS_ATTR((unused)),
+ unsigned int blocksize,
tid_t transaction)
{
- int offset, max;
+ unsigned int offset, max;
jbd2_journal_revoke_header_t *header;
unsigned long long rblock;
int tag_size = sizeof(__u32);
header = (jbd2_journal_revoke_header_t *) buf;
offset = sizeof(jbd2_journal_revoke_header_t);
max = be32_to_cpu(header->r_count);
- if (max > jsb->s_blocksize) {
+ if (max > blocksize) {
fprintf(out_file, "Revoke block's r_count invalid: %u\b",
max);
- max = jsb->s_blocksize;
+ max = blocksize;
}
while (offset < max) {
unsigned int log_blocknr,
unsigned int fs_blocknr,
unsigned int log_tag_flags,
- int blocksize,
+ unsigned int blocksize,
tid_t transaction)
{
int retval;
- char buf[8192];
+ char buf[EXT2_MAX_BLOCK_SIZE];
if (!(dump_all
|| (fs_blocknr == block_to_dump)