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>
32 extern int optreset; /* defined by BSD, but not others */
38 enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
40 int dump_all, dump_contents, dump_descriptors;
41 unsigned int block_to_dump, group_to_dump, bitmap_to_dump;
42 unsigned int inode_block_to_dump, inode_offset_to_dump, bitmap_to_dump;
43 ext2_ino_t inode_to_dump;
47 enum journal_location where;
52 static void dump_journal(char *, FILE *, struct journal_source *);
54 static void dump_descriptor_block(FILE *, struct journal_source *,
55 char *, journal_superblock_t *,
56 unsigned int *, int, tid_t);
58 static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
59 unsigned int, int, tid_t);
61 static void dump_metadata_block(FILE *, struct journal_source *,
62 journal_superblock_t*,
63 unsigned int, unsigned int, int, tid_t);
65 static void do_hexdump (FILE *, char *, int);
67 #define WRAP(jsb, blocknr) \
68 if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \
69 blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \
70 be32_to_cpu((jsb)->s_first));
73 void do_logdump(int argc, char **argv)
80 char *inode_spec = NULL;
81 char *journal_fn = NULL;
83 ext2_ino_t journal_inum;
84 struct ext2_inode journal_inode;
85 ext2_file_t journal_file;
89 const char *logdump_usage = ("Usage: logdump "
90 "[-ac] [-b<block>] [-i<inode>] "
91 "[-f<journal_file>] [output_file]");
93 struct journal_source journal_source;
97 optreset = 1; /* Makes BSD getopt happy */
99 journal_source.where = 0;
100 journal_source.fd = 0;
101 journal_source.file = 0;
104 dump_descriptors = 1;
107 inode_block_to_dump = -1;
110 while ((c = getopt (argc, argv, "ab:ci:f:")) != EOF) {
116 block_to_dump = strtoul(optarg, &tmp, 0);
119 "Bad block number - %s", optarg);
122 dump_descriptors = 0;
132 dump_descriptors = 0;
135 com_err(argv[0], 0, logdump_usage);
139 if (optind != argc && optind != argc-1) {
140 com_err(argv[0], 0, logdump_usage);
145 int inode_group, group_offset, inodes_per_block;
147 if (check_fs_open(argv[0]))
150 inode_to_dump = string_to_inode(inode_spec);
154 inode_group = ((inode_to_dump - 1)
155 / current_fs->super->s_inodes_per_group);
156 group_offset = ((inode_to_dump - 1)
157 % current_fs->super->s_inodes_per_group);
158 inodes_per_block = (current_fs->blocksize
159 / sizeof(struct ext2_inode));
161 inode_block_to_dump =
162 current_fs->group_desc[inode_group].bg_inode_table +
163 (group_offset / inodes_per_block);
164 inode_offset_to_dump = ((group_offset % inodes_per_block)
165 * sizeof(struct ext2_inode));
166 printf("Inode %u is at group %u, block %u, offset %u\n",
167 inode_to_dump, inode_group,
168 inode_block_to_dump, inode_offset_to_dump);
171 if (optind == argc) {
174 out_fn = argv[optind];
175 out_file = fopen(out_fn, "w");
177 com_err(argv[0], errno, "while opening %s for logdump",
183 if (block_to_dump != -1 && current_fs != NULL) {
184 group_to_dump = ((block_to_dump -
185 current_fs->super->s_first_data_block)
186 / current_fs->super->s_blocks_per_group);
187 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
190 if (!journal_fn && check_fs_open(argv[0]))
194 /* Set up to read journal from a regular file somewhere */
195 journal_fd = open(journal_fn, O_RDONLY, 0);
196 if (journal_fd < 0) {
197 com_err(argv[0], errno, "while opening %s for logdump",
202 journal_source.where = JOURNAL_IS_EXTERNAL;
203 journal_source.fd = journal_fd;
204 } else if ((journal_inum = current_fs->super->s_journal_inum)) {
205 retval = ext2fs_read_inode(current_fs, journal_inum,
208 com_err(argv[0], retval,
209 "while reading inode %u", journal_inum);
213 retval = ext2fs_file_open(current_fs, journal_inum,
216 com_err(argv[0], retval, "while opening ext2 file");
219 journal_source.where = JOURNAL_IS_INTERNAL;
220 journal_source.file = journal_file;
221 } else if ((journal_fn =
222 ext2fs_find_block_device(current_fs->super->s_journal_dev))) {
223 journal_fd = open(journal_fn, O_RDONLY, 0);
224 if (journal_fd < 0) {
225 com_err(argv[0], errno, "while opening %s for logdump",
230 fprintf(out_file, "Using external journal found at %s\n",
233 journal_source.where = JOURNAL_IS_EXTERNAL;
234 journal_source.fd = journal_fd;
236 com_err(argv[0], 0, "filesystem has no journal");
240 dump_journal(argv[0], out_file, &journal_source);
242 if (journal_source.where == JOURNAL_IS_INTERNAL)
243 ext2fs_file_close(journal_file);
247 if (out_file != stdout)
254 static int read_journal_block(const char *cmd, struct journal_source *source,
255 off_t offset, char *buf, int size,
260 if (source->where == JOURNAL_IS_EXTERNAL) {
261 if (lseek(source->fd, offset, SEEK_SET) < 0) {
263 com_err(cmd, retval, "while seeking in reading journal");
266 retval = read(source->fd, buf, size);
273 retval = ext2fs_file_lseek(source->file, offset,
274 EXT2_SEEK_SET, NULL);
276 com_err(cmd, retval, "while seeking in reading journal");
280 retval = ext2fs_file_read(source->file, buf, size, got);
284 com_err(cmd, retval, "while while reading journal");
285 else if (*got != size) {
286 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
293 static const char *type_to_name(int btype)
296 case JFS_DESCRIPTOR_BLOCK:
297 return "descriptor block";
298 case JFS_COMMIT_BLOCK:
299 return "commit block";
300 case JFS_SUPERBLOCK_V1:
301 return "V1 superblock";
302 case JFS_SUPERBLOCK_V2:
303 return "V2 superblock";
304 case JFS_REVOKE_BLOCK:
305 return "revoke table";
307 return "unrecognised type";
311 static void dump_journal(char *cmdname, FILE *out_file,
312 struct journal_source *source)
314 struct ext2_super_block *sb;
315 char jsb_buffer[1024];
317 journal_superblock_t *jsb;
318 int blocksize = 1024;
321 __u32 magic, sequence, blocktype;
322 journal_header_t *header;
325 unsigned int blocknr = 0;
327 /* First, check to see if there's an ext2 superblock header */
328 retval = read_journal_block(cmdname, source, 0,
333 jsb = (journal_superblock_t *) buf;
334 sb = (struct ext2_super_block *) (buf+1024);
336 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
337 ext2fs_swap_super(sb);
340 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
341 (sb->s_magic == EXT2_SUPER_MAGIC) &&
342 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
343 blocksize = EXT2_BLOCK_SIZE(sb);
344 blocknr = (blocksize == 1024) ? 2 : 1;
345 uuid_unparse(&sb->s_uuid, jsb_buffer);
346 fprintf(out_file, "Ext2 superblock header found.\n");
348 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
349 fprintf(out_file, "\tblocksize=%d\n", blocksize);
350 fprintf(out_file, "\tjournal data size %ld\n",
355 /* Next, read the journal superblock */
357 retval = read_journal_block(cmdname, source, blocknr*blocksize,
358 jsb_buffer, 1024, &got);
362 jsb = (journal_superblock_t *) jsb_buffer;
363 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
365 "Journal superblock magic number invalid!\n");
368 blocksize = be32_to_cpu(jsb->s_blocksize);
369 transaction = be32_to_cpu(jsb->s_sequence);
370 blocknr = be32_to_cpu(jsb->s_start);
372 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
373 blocknr, transaction);
376 /* Empty journal, nothing to do. */
380 retval = read_journal_block(cmdname, source,
381 blocknr*blocksize, buf,
383 if (retval || got != blocksize)
386 header = (journal_header_t *) buf;
388 magic = be32_to_cpu(header->h_magic);
389 sequence = be32_to_cpu(header->h_sequence);
390 blocktype = be32_to_cpu(header->h_blocktype);
392 if (magic != JFS_MAGIC_NUMBER) {
393 fprintf (out_file, "No magic number at block %u: "
394 "end of journal.\n", blocknr);
398 if (sequence != transaction) {
399 fprintf (out_file, "Found sequence %u (not %u) at "
400 "block %u: end of journal.\n",
401 sequence, transaction, blocknr);
405 if (dump_descriptors) {
406 fprintf (out_file, "Found expected sequence %u, "
407 "type %u (%s) at block %u\n",
409 type_to_name(blocktype), blocknr);
413 case JFS_DESCRIPTOR_BLOCK:
414 dump_descriptor_block(out_file, source, buf, jsb,
419 case JFS_COMMIT_BLOCK:
425 case JFS_REVOKE_BLOCK:
426 dump_revoke_block(out_file, buf, jsb,
434 fprintf (out_file, "Unexpected block type %u at "
435 "block %u.\n", blocktype, blocknr);
442 static void dump_descriptor_block(FILE *out_file,
443 struct journal_source *source,
445 journal_superblock_t *jsb,
446 unsigned int *blockp, int blocksize,
451 journal_block_tag_t *tag;
452 unsigned int blocknr;
457 offset = sizeof(journal_header_t);
461 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
462 "block %u:\n", transaction, blocknr);
468 /* Work out the location of the current tag, and skip to
471 tag = (journal_block_tag_t *) tagp;
472 offset += sizeof(journal_block_tag_t);
474 /* ... and if we have gone too far, then we've reached the
475 end of this block. */
476 if (offset > blocksize)
479 tag_block = be32_to_cpu(tag->t_blocknr);
480 tag_flags = be32_to_cpu(tag->t_flags);
482 if (!(tag_flags & JFS_FLAG_SAME_UUID))
485 dump_metadata_block(out_file, source, jsb,
486 blocknr, tag_block, blocksize,
492 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
498 static void dump_revoke_block(FILE *out_file, char *buf,
499 journal_superblock_t *jsb,
500 unsigned int blocknr, int blocksize,
504 journal_revoke_header_t *header;
505 unsigned int *entry, rblock;
508 fprintf(out_file, "Dumping revoke block, sequence %u, at "
509 "block %u:\n", transaction, blocknr);
511 header = (journal_revoke_header_t *) buf;
512 offset = sizeof(journal_revoke_header_t);
513 max = be32_to_cpu(header->r_count);
515 while (offset < max) {
516 entry = (unsigned int *) (buf + offset);
517 rblock = be32_to_cpu(*entry);
518 if (dump_all || rblock == block_to_dump) {
519 fprintf(out_file, " Revoke FS block %u", rblock);
521 fprintf(out_file, "\n");
523 fprintf(out_file," at block %u, sequence %u\n",
524 blocknr, transaction);
531 static void show_extent(FILE *out_file, int start_extent, int end_extent,
534 if (start_extent >= 0 && first_block != 0)
535 fprintf(out_file, "(%d+%u): %u ",
536 start_extent, end_extent-start_extent, first_block);
539 static void show_indirect(FILE *out_file, const char *name, __u32 where)
542 fprintf(out_file, "(%s): %u ", name, where);
546 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
547 journal_superblock_t *jsb,
548 unsigned int log_blocknr,
549 unsigned int fs_blocknr,
558 || (fs_blocknr == block_to_dump)
559 || (fs_blocknr == inode_block_to_dump)
560 || (fs_blocknr == bitmap_to_dump)))
563 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
565 fprintf(out_file, "sequence %u, ", transaction);
566 fprintf(out_file, "journal block %u\n", log_blocknr);
568 /* There are two major special cases to parse:
570 * If this block is a block
571 * bitmap block, we need to give it special treatment so that we
572 * can log any allocates and deallocates which affect the
573 * block_to_dump query block.
575 * If the block is an inode block for the inode being searched
576 * for, then we need to dump the contents of that inode
577 * structure symbolically.
580 if (!(dump_contents && dump_all)
581 && fs_blocknr != block_to_dump
582 && fs_blocknr != bitmap_to_dump
583 && fs_blocknr != inode_block_to_dump)
586 retval = read_journal_block("logdump", source,
587 blocksize * log_blocknr,
588 buf, blocksize, &got);
592 if (fs_blocknr == bitmap_to_dump) {
593 struct ext2_super_block *super;
596 super = current_fs->super;
597 offset = ((fs_blocknr - super->s_first_data_block) %
598 super->s_blocks_per_group);
600 fprintf(out_file, " (block bitmap for block %u: "
603 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
606 if (fs_blocknr == inode_block_to_dump) {
607 struct ext2_inode *inode;
608 int first, prev, this, start_extent, i;
610 fprintf(out_file, " (inode block for inode %u):\n",
613 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
614 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
616 /* Dump out the direct/indirect blocks here:
617 * internal_dump_inode can only dump them from the main
618 * on-disk inode, not from the journaled copy of the
621 fprintf (out_file, " Blocks: ");
622 first = prev = start_extent = -1;
624 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
625 this = inode->i_block[i];
626 if (start_extent >= 0 && this == prev+1) {
630 show_extent(out_file, start_extent, i, first);
635 show_extent(out_file, start_extent, i, first);
636 show_indirect(out_file, "IND", inode->i_block[i++]);
637 show_indirect(out_file, "DIND", inode->i_block[i++]);
638 show_indirect(out_file, "TIND", inode->i_block[i++]);
640 fprintf(out_file, "\n");
644 do_hexdump(out_file, buf, blocksize);
648 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
656 charp = (char *) buf;
658 for (i=0; i<blocksize; i+=16) {
659 fprintf(out_file, " %04x: ", i);
660 for (j=0; j<16; j+=4)
661 fprintf(out_file, "%08x ", *intp++);
662 for (j=0; j<16; j++) {
664 if (c < ' ' || c >= 127)
666 fprintf(out_file, "%c", c);
668 fprintf(out_file, "\n");