2 * logdump.c --- dump the contents of the journal out to a file
4 * Authro: Stephen C. Tweedie, 2001 <sct@redhat.com>
5 * Copyright (C) 2001 Red Hat, Inc.
6 * Based on portions Copyright (C) 1994 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
21 #include <sys/types.h>
33 #include "blkid/blkid.h"
35 #include <uuid/uuid.h>
37 enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
39 #define ANY_BLOCK ((blk_t) -1)
41 int dump_all, dump_contents, dump_descriptors;
42 blk_t block_to_dump, bitmap_to_dump, inode_block_to_dump;
43 unsigned int group_to_dump, inode_offset_to_dump;
44 ext2_ino_t inode_to_dump;
48 enum journal_location where;
53 static void dump_journal(char *, FILE *, struct journal_source *);
55 static void dump_descriptor_block(FILE *, struct journal_source *,
56 char *, journal_superblock_t *,
57 unsigned int *, int, tid_t);
59 static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
60 unsigned int, int, tid_t);
62 static void dump_metadata_block(FILE *, struct journal_source *,
63 journal_superblock_t*,
64 unsigned int, unsigned int, unsigned int,
67 static void do_hexdump (FILE *, char *, int);
69 #define WRAP(jsb, blocknr) \
70 if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \
71 blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \
72 be32_to_cpu((jsb)->s_first));
74 void do_logdump(int argc, char **argv)
81 char *inode_spec = NULL;
82 char *journal_fn = NULL;
85 ext2_ino_t journal_inum;
86 struct ext2_inode journal_inode;
87 ext2_file_t journal_file;
89 struct journal_source journal_source;
90 struct ext2_super_block *es = NULL;
92 journal_source.where = JOURNAL_IS_INTERNAL;
93 journal_source.fd = 0;
94 journal_source.file = 0;
98 block_to_dump = ANY_BLOCK;
100 inode_block_to_dump = ANY_BLOCK;
104 while ((c = getopt (argc, argv, "ab:ci:f:s")) != EOF) {
110 block_to_dump = strtoul(optarg, &tmp, 0);
113 "Bad block number - %s", optarg);
116 dump_descriptors = 0;
126 dump_descriptors = 0;
135 if (optind != argc && optind != argc-1) {
140 es = current_fs->super;
143 int inode_group, group_offset, inodes_per_block;
145 if (check_fs_open(argv[0]))
148 inode_to_dump = string_to_inode(inode_spec);
152 inode_group = ((inode_to_dump - 1)
153 / es->s_inodes_per_group);
154 group_offset = ((inode_to_dump - 1)
155 % es->s_inodes_per_group);
156 inodes_per_block = (current_fs->blocksize
157 / sizeof(struct ext2_inode));
159 inode_block_to_dump =
160 current_fs->group_desc[inode_group].bg_inode_table +
161 (group_offset / inodes_per_block);
162 inode_offset_to_dump = ((group_offset % inodes_per_block)
163 * sizeof(struct ext2_inode));
164 printf("Inode %u is at group %u, block %u, offset %u\n",
165 inode_to_dump, inode_group,
166 inode_block_to_dump, inode_offset_to_dump);
169 if (optind == argc) {
172 out_fn = argv[optind];
173 out_file = fopen(out_fn, "w");
175 com_err(argv[0], errno, "while opening %s for logdump",
181 if (block_to_dump != ANY_BLOCK && current_fs != NULL) {
182 group_to_dump = ((block_to_dump -
183 es->s_first_data_block)
184 / es->s_blocks_per_group);
185 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
188 if (!journal_fn && check_fs_open(argv[0]))
192 /* Set up to read journal from a regular file somewhere */
193 journal_fd = open(journal_fn, O_RDONLY, 0);
194 if (journal_fd < 0) {
195 com_err(argv[0], errno, "while opening %s for logdump",
200 journal_source.where = JOURNAL_IS_EXTERNAL;
201 journal_source.fd = journal_fd;
202 } else if ((journal_inum = es->s_journal_inum)) {
204 if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
206 "no journal backup in super block\n");
209 memset(&journal_inode, 0, sizeof(struct ext2_inode));
210 memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
212 journal_inode.i_size = es->s_jnl_blocks[16];
213 journal_inode.i_links_count = 1;
214 journal_inode.i_mode = LINUX_S_IFREG | 0600;
216 if (debugfs_read_inode(journal_inum, &journal_inode,
221 retval = ext2fs_file_open2(current_fs, journal_inum,
222 &journal_inode, 0, &journal_file);
224 com_err(argv[0], retval, "while opening ext2 file");
227 journal_source.where = JOURNAL_IS_INTERNAL;
228 journal_source.file = journal_file;
232 uuid_unparse(es->s_journal_uuid, uuid);
233 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
235 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
237 com_err(argv[0], 0, "filesystem has no journal");
240 journal_fd = open(journal_fn, O_RDONLY, 0);
241 if (journal_fd < 0) {
242 com_err(argv[0], errno, "while opening %s for logdump",
247 fprintf(out_file, "Using external journal found at %s\n",
250 journal_source.where = JOURNAL_IS_EXTERNAL;
251 journal_source.fd = journal_fd;
254 dump_journal(argv[0], out_file, &journal_source);
256 if (journal_source.where == JOURNAL_IS_INTERNAL)
257 ext2fs_file_close(journal_file);
262 if (out_file && (out_file != stdout))
268 fprintf(stderr, "%s: Usage: logdump [-acs] [-b<block>] [-i<filespec>]\n\t"
269 "[-f<journal_file>] [output_file]\n", argv[0]);
273 static int read_journal_block(const char *cmd, struct journal_source *source,
274 off_t offset, char *buf, int size,
279 if (source->where == JOURNAL_IS_EXTERNAL) {
280 if (lseek(source->fd, offset, SEEK_SET) < 0) {
282 com_err(cmd, retval, "while seeking in reading journal");
285 retval = read(source->fd, buf, size);
292 retval = ext2fs_file_lseek(source->file, offset,
293 EXT2_SEEK_SET, NULL);
295 com_err(cmd, retval, "while seeking in reading journal");
299 retval = ext2fs_file_read(source->file, buf, size, got);
303 com_err(cmd, retval, "while reading journal");
304 else if (*got != (unsigned int) size) {
305 com_err(cmd, 0, "short read (read %d, expected %d) "
306 "while reading journal", *got, size);
313 static const char *type_to_name(int btype)
316 case JFS_DESCRIPTOR_BLOCK:
317 return "descriptor block";
318 case JFS_COMMIT_BLOCK:
319 return "commit block";
320 case JFS_SUPERBLOCK_V1:
321 return "V1 superblock";
322 case JFS_SUPERBLOCK_V2:
323 return "V2 superblock";
324 case JFS_REVOKE_BLOCK:
325 return "revoke table";
327 return "unrecognised type";
331 static void dump_journal(char *cmdname, FILE *out_file,
332 struct journal_source *source)
334 struct ext2_super_block *sb;
335 char jsb_buffer[1024];
337 journal_superblock_t *jsb;
338 unsigned int blocksize = 1024;
341 __u32 magic, sequence, blocktype;
342 journal_header_t *header;
345 unsigned int blocknr = 0;
347 /* First, check to see if there's an ext2 superblock header */
348 retval = read_journal_block(cmdname, source, 0,
353 jsb = (journal_superblock_t *) buf;
354 sb = (struct ext2_super_block *) (buf+1024);
355 #ifdef WORDS_BIGENDIAN
356 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
357 ext2fs_swap_super(sb);
360 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
361 (sb->s_magic == EXT2_SUPER_MAGIC) &&
362 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
363 blocksize = EXT2_BLOCK_SIZE(sb);
364 blocknr = (blocksize == 1024) ? 2 : 1;
365 uuid_unparse(sb->s_uuid, jsb_buffer);
366 fprintf(out_file, "Ext2 superblock header found.\n");
368 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
369 fprintf(out_file, "\tblocksize=%d\n", blocksize);
370 fprintf(out_file, "\tjournal data size %lu\n",
371 (unsigned long) sb->s_blocks_count);
375 /* Next, read the journal superblock */
377 retval = read_journal_block(cmdname, source, blocknr*blocksize,
378 jsb_buffer, 1024, &got);
382 jsb = (journal_superblock_t *) jsb_buffer;
383 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
385 "Journal superblock magic number invalid!\n");
388 blocksize = be32_to_cpu(jsb->s_blocksize);
389 transaction = be32_to_cpu(jsb->s_sequence);
390 blocknr = be32_to_cpu(jsb->s_start);
392 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
393 blocknr, transaction);
396 /* Empty journal, nothing to do. */
400 retval = read_journal_block(cmdname, source,
401 blocknr*blocksize, buf,
403 if (retval || got != blocksize)
406 header = (journal_header_t *) buf;
408 magic = be32_to_cpu(header->h_magic);
409 sequence = be32_to_cpu(header->h_sequence);
410 blocktype = be32_to_cpu(header->h_blocktype);
412 if (magic != JFS_MAGIC_NUMBER) {
413 fprintf (out_file, "No magic number at block %u: "
414 "end of journal.\n", blocknr);
418 if (sequence != transaction) {
419 fprintf (out_file, "Found sequence %u (not %u) at "
420 "block %u: end of journal.\n",
421 sequence, transaction, blocknr);
425 if (dump_descriptors) {
426 fprintf (out_file, "Found expected sequence %u, "
427 "type %u (%s) at block %u\n",
429 type_to_name(blocktype), blocknr);
433 case JFS_DESCRIPTOR_BLOCK:
434 dump_descriptor_block(out_file, source, buf, jsb,
439 case JFS_COMMIT_BLOCK:
445 case JFS_REVOKE_BLOCK:
446 dump_revoke_block(out_file, buf, jsb,
454 fprintf (out_file, "Unexpected block type %u at "
455 "block %u.\n", blocktype, blocknr);
462 static void dump_descriptor_block(FILE *out_file,
463 struct journal_source *source,
465 journal_superblock_t *jsb,
466 unsigned int *blockp, int blocksize,
469 int offset, tag_size = JBD_TAG_SIZE32;
471 journal_block_tag_t *tag;
472 unsigned int blocknr;
476 if (be32_to_cpu(jsb->s_feature_incompat) & JFS_FEATURE_INCOMPAT_64BIT)
477 tag_size = JBD_TAG_SIZE64;
479 offset = sizeof(journal_header_t);
483 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
484 "block %u:\n", transaction, blocknr);
490 /* Work out the location of the current tag, and skip to
493 tag = (journal_block_tag_t *) tagp;
496 /* ... and if we have gone too far, then we've reached the
497 end of this block. */
498 if (offset > blocksize)
501 tag_block = be32_to_cpu(tag->t_blocknr);
502 tag_flags = be32_to_cpu(tag->t_flags);
504 if (!(tag_flags & JFS_FLAG_SAME_UUID))
507 dump_metadata_block(out_file, source, jsb,
508 blocknr, tag_block, tag_flags, blocksize,
514 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
520 static void dump_revoke_block(FILE *out_file, char *buf,
521 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
522 unsigned int blocknr,
523 int blocksize EXT2FS_ATTR((unused)),
527 journal_revoke_header_t *header;
528 unsigned int *entry, rblock;
531 fprintf(out_file, "Dumping revoke block, sequence %u, at "
532 "block %u:\n", transaction, blocknr);
534 header = (journal_revoke_header_t *) buf;
535 offset = sizeof(journal_revoke_header_t);
536 max = be32_to_cpu(header->r_count);
538 while (offset < max) {
539 entry = (unsigned int *) (buf + offset);
540 rblock = be32_to_cpu(*entry);
541 if (dump_all || rblock == block_to_dump) {
542 fprintf(out_file, " Revoke FS block %u", rblock);
544 fprintf(out_file, "\n");
546 fprintf(out_file," at block %u, sequence %u\n",
547 blocknr, transaction);
554 static void show_extent(FILE *out_file, int start_extent, int end_extent,
557 if (start_extent >= 0 && first_block != 0)
558 fprintf(out_file, "(%d+%u): %u ",
559 start_extent, end_extent-start_extent, first_block);
562 static void show_indirect(FILE *out_file, const char *name, __u32 where)
565 fprintf(out_file, "(%s): %u ", name, where);
569 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
570 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
571 unsigned int log_blocknr,
572 unsigned int fs_blocknr,
573 unsigned int log_tag_flags,
582 || (fs_blocknr == block_to_dump)
583 || (fs_blocknr == inode_block_to_dump)
584 || (fs_blocknr == bitmap_to_dump)))
587 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
589 fprintf(out_file, "sequence %u, ", transaction);
590 fprintf(out_file, "journal block %u (flags 0x%x)\n", log_blocknr,
593 /* There are two major special cases to parse:
595 * If this block is a block
596 * bitmap block, we need to give it special treatment so that we
597 * can log any allocates and deallocates which affect the
598 * block_to_dump query block.
600 * If the block is an inode block for the inode being searched
601 * for, then we need to dump the contents of that inode
602 * structure symbolically.
605 if (!(dump_contents && dump_all)
606 && fs_blocknr != block_to_dump
607 && fs_blocknr != bitmap_to_dump
608 && fs_blocknr != inode_block_to_dump)
611 retval = read_journal_block("logdump", source,
612 blocksize * log_blocknr,
613 buf, blocksize, &got);
617 if (fs_blocknr == bitmap_to_dump) {
618 struct ext2_super_block *super;
621 super = current_fs->super;
622 offset = ((fs_blocknr - super->s_first_data_block) %
623 super->s_blocks_per_group);
625 fprintf(out_file, " (block bitmap for block %u: "
628 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
631 if (fs_blocknr == inode_block_to_dump) {
632 struct ext2_inode *inode;
633 int first, prev, this, start_extent, i;
635 fprintf(out_file, " (inode block for inode %u):\n",
638 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
639 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
641 /* Dump out the direct/indirect blocks here:
642 * internal_dump_inode can only dump them from the main
643 * on-disk inode, not from the journaled copy of the
646 fprintf (out_file, " Blocks: ");
647 first = prev = start_extent = -1;
649 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
650 this = inode->i_block[i];
651 if (start_extent >= 0 && this == prev+1) {
655 show_extent(out_file, start_extent, i, first);
660 show_extent(out_file, start_extent, i, first);
661 show_indirect(out_file, "IND", inode->i_block[i++]);
662 show_indirect(out_file, "DIND", inode->i_block[i++]);
663 show_indirect(out_file, "TIND", inode->i_block[i++]);
665 fprintf(out_file, "\n");
669 do_hexdump(out_file, buf, blocksize);
673 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
681 charp = (char *) buf;
683 for (i=0; i<blocksize; i+=16) {
684 fprintf(out_file, " %04x: ", i);
685 for (j=0; j<16; j+=4)
686 fprintf(out_file, "%08x ", *intp++);
687 for (j=0; j<16; j++) {
689 if (c < ' ' || c >= 127)
691 fprintf(out_file, "%c", c);
693 fprintf(out_file, "\n");