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
22 #include <sys/types.h>
34 #include "blkid/blkid.h"
36 #include <uuid/uuid.h>
38 enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
40 #define ANY_BLOCK ((blk64_t) -1)
42 static int dump_all, dump_contents, dump_descriptors;
43 static blk64_t block_to_dump, bitmap_to_dump, inode_block_to_dump;
44 static unsigned int group_to_dump, inode_offset_to_dump;
45 static ext2_ino_t inode_to_dump;
49 enum journal_location where;
54 static void dump_journal(char *, FILE *, struct journal_source *);
56 static void dump_descriptor_block(FILE *, struct journal_source *,
57 char *, journal_superblock_t *,
58 unsigned int *, int, tid_t);
60 static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
61 unsigned int, int, tid_t);
63 static void dump_metadata_block(FILE *, struct journal_source *,
64 journal_superblock_t*,
65 unsigned int, unsigned int, unsigned int,
68 static void do_hexdump (FILE *, char *, int);
70 #define WRAP(jsb, blocknr) \
71 if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \
72 blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \
73 be32_to_cpu((jsb)->s_first));
75 void do_logdump(int argc, char **argv)
82 char *inode_spec = NULL;
83 char *journal_fn = NULL;
86 ext2_ino_t journal_inum;
87 struct ext2_inode journal_inode;
88 ext2_file_t journal_file;
90 struct journal_source journal_source;
91 struct ext2_super_block *es = NULL;
93 journal_source.where = JOURNAL_IS_INTERNAL;
94 journal_source.fd = 0;
95 journal_source.file = 0;
99 block_to_dump = ANY_BLOCK;
101 inode_block_to_dump = ANY_BLOCK;
105 while ((c = getopt (argc, argv, "ab:ci:f:s")) != EOF) {
111 block_to_dump = strtoul(optarg, &tmp, 0);
114 "Bad block number - %s", optarg);
117 dump_descriptors = 0;
127 dump_descriptors = 0;
136 if (optind != argc && optind != argc-1) {
141 es = current_fs->super;
144 int inode_group, group_offset, inodes_per_block;
146 if (check_fs_open(argv[0]))
149 inode_to_dump = string_to_inode(inode_spec);
153 inode_group = ((inode_to_dump - 1)
154 / es->s_inodes_per_group);
155 group_offset = ((inode_to_dump - 1)
156 % es->s_inodes_per_group);
157 inodes_per_block = (current_fs->blocksize
158 / sizeof(struct ext2_inode));
160 inode_block_to_dump =
161 ext2fs_inode_table_loc(current_fs, inode_group) +
162 (group_offset / inodes_per_block);
163 inode_offset_to_dump = ((group_offset % inodes_per_block)
164 * sizeof(struct ext2_inode));
165 printf("Inode %u is at group %u, block %llu, offset %u\n",
166 inode_to_dump, inode_group,
167 inode_block_to_dump, inode_offset_to_dump);
170 if (optind == argc) {
173 out_fn = argv[optind];
174 out_file = fopen(out_fn, "w");
176 com_err(argv[0], errno, "while opening %s for logdump",
182 if (block_to_dump != ANY_BLOCK && current_fs != NULL) {
183 group_to_dump = ((block_to_dump -
184 es->s_first_data_block)
185 / es->s_blocks_per_group);
186 bitmap_to_dump = ext2fs_block_bitmap_loc(current_fs, group_to_dump);
189 if (!journal_fn && check_fs_open(argv[0]))
193 /* Set up to read journal from a regular file somewhere */
194 journal_fd = open(journal_fn, O_RDONLY, 0);
195 if (journal_fd < 0) {
196 com_err(argv[0], errno, "while opening %s for logdump",
201 journal_source.where = JOURNAL_IS_EXTERNAL;
202 journal_source.fd = journal_fd;
203 } else if ((journal_inum = es->s_journal_inum)) {
205 if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
207 "no journal backup in super block\n");
210 memset(&journal_inode, 0, sizeof(struct ext2_inode));
211 memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
213 journal_inode.i_size_high = es->s_jnl_blocks[15];
214 journal_inode.i_size = es->s_jnl_blocks[16];
215 journal_inode.i_links_count = 1;
216 journal_inode.i_mode = LINUX_S_IFREG | 0600;
218 if (debugfs_read_inode(journal_inum, &journal_inode,
223 retval = ext2fs_file_open2(current_fs, journal_inum,
224 &journal_inode, 0, &journal_file);
226 com_err(argv[0], retval, "while opening ext2 file");
229 journal_source.where = JOURNAL_IS_INTERNAL;
230 journal_source.file = journal_file;
234 uuid_unparse(es->s_journal_uuid, uuid);
235 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
237 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
239 com_err(argv[0], 0, "filesystem has no journal");
242 journal_fd = open(journal_fn, O_RDONLY, 0);
243 if (journal_fd < 0) {
244 com_err(argv[0], errno, "while opening %s for logdump",
249 fprintf(out_file, "Using external journal found at %s\n",
252 journal_source.where = JOURNAL_IS_EXTERNAL;
253 journal_source.fd = journal_fd;
256 dump_journal(argv[0], out_file, &journal_source);
258 if (journal_source.where == JOURNAL_IS_INTERNAL)
259 ext2fs_file_close(journal_file);
264 if (out_file && (out_file != stdout))
270 fprintf(stderr, "%s: Usage: logdump [-acs] [-b<block>] [-i<filespec>]\n\t"
271 "[-f<journal_file>] [output_file]\n", argv[0]);
275 static int read_journal_block(const char *cmd, struct journal_source *source,
276 off_t offset, char *buf, unsigned int size)
281 if (source->where == JOURNAL_IS_EXTERNAL) {
282 if (lseek(source->fd, offset, SEEK_SET) < 0) {
286 retval = read(source->fd, buf, size);
294 retval = ext2fs_file_lseek(source->file, offset,
295 EXT2_SEEK_SET, NULL);
298 com_err(cmd, retval, "while seeking in reading journal");
301 retval = ext2fs_file_read(source->file, buf, size, &got);
304 com_err(cmd, retval, "while reading journal");
309 com_err(cmd, 0, "short read (read %u, expected %u) "
310 "while reading journal", got, size);
316 static const char *type_to_name(int btype)
319 case JFS_DESCRIPTOR_BLOCK:
320 return "descriptor block";
321 case JFS_COMMIT_BLOCK:
322 return "commit block";
323 case JFS_SUPERBLOCK_V1:
324 return "V1 superblock";
325 case JFS_SUPERBLOCK_V2:
326 return "V2 superblock";
327 case JFS_REVOKE_BLOCK:
328 return "revoke table";
330 return "unrecognised type";
334 static void dump_journal(char *cmdname, FILE *out_file,
335 struct journal_source *source)
337 struct ext2_super_block *sb;
338 char jsb_buffer[1024];
340 journal_superblock_t *jsb;
341 unsigned int blocksize = 1024;
343 __u32 magic, sequence, blocktype;
344 journal_header_t *header;
347 unsigned int blocknr = 0;
349 /* First, check to see if there's an ext2 superblock header */
350 retval = read_journal_block(cmdname, source, 0, buf, 2048);
354 jsb = (journal_superblock_t *) buf;
355 sb = (struct ext2_super_block *) (buf+1024);
356 #ifdef WORDS_BIGENDIAN
357 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
358 ext2fs_swap_super(sb);
361 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
362 (sb->s_magic == EXT2_SUPER_MAGIC) &&
363 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
364 blocksize = EXT2_BLOCK_SIZE(sb);
365 blocknr = (blocksize == 1024) ? 2 : 1;
366 uuid_unparse(sb->s_uuid, jsb_buffer);
367 fprintf(out_file, "Ext2 superblock header found.\n");
369 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
370 fprintf(out_file, "\tblocksize=%d\n", blocksize);
371 fprintf(out_file, "\tjournal data size %lu\n",
372 (unsigned long) ext2fs_blocks_count(sb));
376 /* Next, read the journal superblock */
378 retval = read_journal_block(cmdname, source, blocknr*blocksize,
383 jsb = (journal_superblock_t *) jsb_buffer;
384 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
386 "Journal superblock magic number invalid!\n");
389 blocksize = be32_to_cpu(jsb->s_blocksize);
390 transaction = be32_to_cpu(jsb->s_sequence);
391 blocknr = be32_to_cpu(jsb->s_start);
393 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
394 blocknr, transaction);
397 /* Empty journal, nothing to do. */
401 retval = read_journal_block(cmdname, source,
402 blocknr*blocksize, buf,
407 header = (journal_header_t *) buf;
409 magic = be32_to_cpu(header->h_magic);
410 sequence = be32_to_cpu(header->h_sequence);
411 blocktype = be32_to_cpu(header->h_blocktype);
413 if (magic != JFS_MAGIC_NUMBER) {
414 fprintf (out_file, "No magic number at block %u: "
415 "end of journal.\n", blocknr);
419 if (sequence != transaction) {
420 fprintf (out_file, "Found sequence %u (not %u) at "
421 "block %u: end of journal.\n",
422 sequence, transaction, blocknr);
426 if (dump_descriptors) {
427 fprintf (out_file, "Found expected sequence %u, "
428 "type %u (%s) at block %u\n",
430 type_to_name(blocktype), blocknr);
434 case JFS_DESCRIPTOR_BLOCK:
435 dump_descriptor_block(out_file, source, buf, jsb,
440 case JFS_COMMIT_BLOCK:
446 case JFS_REVOKE_BLOCK:
447 dump_revoke_block(out_file, buf, jsb,
455 fprintf (out_file, "Unexpected block type %u at "
456 "block %u.\n", blocktype, blocknr);
463 static void dump_descriptor_block(FILE *out_file,
464 struct journal_source *source,
466 journal_superblock_t *jsb,
467 unsigned int *blockp, int blocksize,
470 int offset, tag_size = JBD_TAG_SIZE32;
472 journal_block_tag_t *tag;
473 unsigned int blocknr;
477 if (be32_to_cpu(jsb->s_feature_incompat) & JFS_FEATURE_INCOMPAT_64BIT)
478 tag_size = JBD_TAG_SIZE64;
480 offset = sizeof(journal_header_t);
484 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
485 "block %u:\n", transaction, blocknr);
491 /* Work out the location of the current tag, and skip to
494 tag = (journal_block_tag_t *) tagp;
497 /* ... and if we have gone too far, then we've reached the
498 end of this block. */
499 if (offset > blocksize)
502 tag_block = be32_to_cpu(tag->t_blocknr);
503 tag_flags = be16_to_cpu(tag->t_flags);
505 if (!(tag_flags & JFS_FLAG_SAME_UUID))
508 dump_metadata_block(out_file, source, jsb,
509 blocknr, tag_block, tag_flags, blocksize,
515 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
521 static void dump_revoke_block(FILE *out_file, char *buf,
522 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
523 unsigned int blocknr,
524 int blocksize EXT2FS_ATTR((unused)),
528 journal_revoke_header_t *header;
529 unsigned int *entry, rblock;
532 fprintf(out_file, "Dumping revoke block, sequence %u, at "
533 "block %u:\n", transaction, blocknr);
535 header = (journal_revoke_header_t *) buf;
536 offset = sizeof(journal_revoke_header_t);
537 max = be32_to_cpu(header->r_count);
539 while (offset < max) {
540 entry = (unsigned int *) (buf + offset);
541 rblock = be32_to_cpu(*entry);
542 if (dump_all || rblock == block_to_dump) {
543 fprintf(out_file, " Revoke FS block %u", rblock);
545 fprintf(out_file, "\n");
547 fprintf(out_file," at block %u, sequence %u\n",
548 blocknr, transaction);
555 static void show_extent(FILE *out_file, int start_extent, int end_extent,
558 if (start_extent >= 0 && first_block != 0)
559 fprintf(out_file, "(%d+%u): %u ",
560 start_extent, end_extent-start_extent, first_block);
563 static void show_indirect(FILE *out_file, const char *name, __u32 where)
566 fprintf(out_file, "(%s): %u ", name, where);
570 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
571 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
572 unsigned int log_blocknr,
573 unsigned int fs_blocknr,
574 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,
617 if (fs_blocknr == bitmap_to_dump) {
618 struct ext2_super_block *super;
621 super = current_fs->super;
622 offset = ((block_to_dump - super->s_first_data_block) %
623 super->s_blocks_per_group);
625 fprintf(out_file, " (block bitmap for block %llu: "
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");