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 */
37 #include <uuid/uuid.h>
39 enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
41 int dump_all, dump_contents, dump_descriptors;
42 unsigned int block_to_dump, group_to_dump, bitmap_to_dump;
43 unsigned int inode_block_to_dump, inode_offset_to_dump, bitmap_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, int, tid_t);
66 static void do_hexdump (FILE *, char *, int);
68 #define WRAP(jsb, blocknr) \
69 if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \
70 blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \
71 be32_to_cpu((jsb)->s_first));
74 void do_logdump(int argc, char **argv)
81 char *inode_spec = NULL;
82 char *journal_fn = NULL;
84 ext2_ino_t journal_inum;
85 struct ext2_inode journal_inode;
86 ext2_file_t journal_file;
90 const char *logdump_usage = ("Usage: logdump "
91 "[-ac] [-b<block>] [-i<inode>] "
92 "[-f<journal_file>] [output_file]");
94 struct journal_source journal_source;
98 optreset = 1; /* Makes BSD getopt happy */
100 journal_source.where = 0;
101 journal_source.fd = 0;
102 journal_source.file = 0;
105 dump_descriptors = 1;
108 inode_block_to_dump = -1;
111 while ((c = getopt (argc, argv, "ab:ci:f:")) != EOF) {
117 block_to_dump = strtoul(optarg, &tmp, 0);
120 "Bad block number - %s", optarg);
123 dump_descriptors = 0;
133 dump_descriptors = 0;
136 com_err(argv[0], 0, logdump_usage);
140 if (optind != argc && optind != argc-1) {
141 com_err(argv[0], 0, logdump_usage);
146 int inode_group, group_offset, inodes_per_block;
148 if (check_fs_open(argv[0]))
151 inode_to_dump = string_to_inode(inode_spec);
155 inode_group = ((inode_to_dump - 1)
156 / current_fs->super->s_inodes_per_group);
157 group_offset = ((inode_to_dump - 1)
158 % current_fs->super->s_inodes_per_group);
159 inodes_per_block = (current_fs->blocksize
160 / sizeof(struct ext2_inode));
162 inode_block_to_dump =
163 current_fs->group_desc[inode_group].bg_inode_table +
164 (group_offset / inodes_per_block);
165 inode_offset_to_dump = ((group_offset % inodes_per_block)
166 * sizeof(struct ext2_inode));
167 printf("Inode %u is at group %u, block %u, offset %u\n",
168 inode_to_dump, inode_group,
169 inode_block_to_dump, inode_offset_to_dump);
172 if (optind == argc) {
175 out_fn = argv[optind];
176 out_file = fopen(out_fn, "w");
178 com_err(argv[0], errno, "while opening %s for logdump",
184 if (block_to_dump != -1 && current_fs != NULL) {
185 group_to_dump = ((block_to_dump -
186 current_fs->super->s_first_data_block)
187 / current_fs->super->s_blocks_per_group);
188 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
191 if (!journal_fn && check_fs_open(argv[0]))
195 /* Set up to read journal from a regular file somewhere */
196 journal_fd = open(journal_fn, O_RDONLY, 0);
197 if (journal_fd < 0) {
198 com_err(argv[0], errno, "while opening %s for logdump",
203 journal_source.where = JOURNAL_IS_EXTERNAL;
204 journal_source.fd = journal_fd;
205 } else if ((journal_inum = current_fs->super->s_journal_inum)) {
206 retval = ext2fs_read_inode(current_fs, journal_inum,
209 com_err(argv[0], retval,
210 "while reading inode %u", journal_inum);
214 retval = ext2fs_file_open(current_fs, journal_inum,
217 com_err(argv[0], retval, "while opening ext2 file");
220 journal_source.where = JOURNAL_IS_INTERNAL;
221 journal_source.file = journal_file;
222 } else if ((journal_fn =
223 ext2fs_find_block_device(current_fs->super->s_journal_dev))) {
224 journal_fd = open(journal_fn, O_RDONLY, 0);
225 if (journal_fd < 0) {
226 com_err(argv[0], errno, "while opening %s for logdump",
231 fprintf(out_file, "Using external journal found at %s\n",
234 journal_source.where = JOURNAL_IS_EXTERNAL;
235 journal_source.fd = journal_fd;
237 com_err(argv[0], 0, "filesystem has no journal");
241 dump_journal(argv[0], out_file, &journal_source);
243 if (journal_source.where == JOURNAL_IS_INTERNAL)
244 ext2fs_file_close(journal_file);
248 if (out_file != stdout)
255 static int read_journal_block(const char *cmd, struct journal_source *source,
256 off_t offset, char *buf, int size,
261 if (source->where == JOURNAL_IS_EXTERNAL) {
262 if (lseek(source->fd, offset, SEEK_SET) < 0) {
264 com_err(cmd, retval, "while seeking in reading journal");
267 retval = read(source->fd, buf, size);
274 retval = ext2fs_file_lseek(source->file, offset,
275 EXT2_SEEK_SET, NULL);
277 com_err(cmd, retval, "while seeking in reading journal");
281 retval = ext2fs_file_read(source->file, buf, size, got);
285 com_err(cmd, retval, "while while reading journal");
286 else if (*got != size) {
287 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
294 static const char *type_to_name(int btype)
297 case JFS_DESCRIPTOR_BLOCK:
298 return "descriptor block";
299 case JFS_COMMIT_BLOCK:
300 return "commit block";
301 case JFS_SUPERBLOCK_V1:
302 return "V1 superblock";
303 case JFS_SUPERBLOCK_V2:
304 return "V2 superblock";
305 case JFS_REVOKE_BLOCK:
306 return "revoke table";
308 return "unrecognised type";
312 static void dump_journal(char *cmdname, FILE *out_file,
313 struct journal_source *source)
315 struct ext2_super_block *sb;
316 char jsb_buffer[1024];
318 journal_superblock_t *jsb;
319 int blocksize = 1024;
322 __u32 magic, sequence, blocktype;
323 journal_header_t *header;
326 unsigned int blocknr = 0;
328 /* First, check to see if there's an ext2 superblock header */
329 retval = read_journal_block(cmdname, source, 0,
334 jsb = (journal_superblock_t *) buf;
335 sb = (struct ext2_super_block *) (buf+1024);
337 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
338 ext2fs_swap_super(sb);
341 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
342 (sb->s_magic == EXT2_SUPER_MAGIC) &&
343 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
344 blocksize = EXT2_BLOCK_SIZE(sb);
345 blocknr = (blocksize == 1024) ? 2 : 1;
346 uuid_unparse(sb->s_uuid, jsb_buffer);
347 fprintf(out_file, "Ext2 superblock header found.\n");
349 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
350 fprintf(out_file, "\tblocksize=%d\n", blocksize);
351 fprintf(out_file, "\tjournal data size %ld\n",
352 (long) sb->s_blocks_count);
356 /* Next, read the journal superblock */
358 retval = read_journal_block(cmdname, source, blocknr*blocksize,
359 jsb_buffer, 1024, &got);
363 jsb = (journal_superblock_t *) jsb_buffer;
364 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
366 "Journal superblock magic number invalid!\n");
369 blocksize = be32_to_cpu(jsb->s_blocksize);
370 transaction = be32_to_cpu(jsb->s_sequence);
371 blocknr = be32_to_cpu(jsb->s_start);
373 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
374 blocknr, transaction);
377 /* Empty journal, nothing to do. */
381 retval = read_journal_block(cmdname, source,
382 blocknr*blocksize, buf,
384 if (retval || got != blocksize)
387 header = (journal_header_t *) buf;
389 magic = be32_to_cpu(header->h_magic);
390 sequence = be32_to_cpu(header->h_sequence);
391 blocktype = be32_to_cpu(header->h_blocktype);
393 if (magic != JFS_MAGIC_NUMBER) {
394 fprintf (out_file, "No magic number at block %u: "
395 "end of journal.\n", blocknr);
399 if (sequence != transaction) {
400 fprintf (out_file, "Found sequence %u (not %u) at "
401 "block %u: end of journal.\n",
402 sequence, transaction, blocknr);
406 if (dump_descriptors) {
407 fprintf (out_file, "Found expected sequence %u, "
408 "type %u (%s) at block %u\n",
410 type_to_name(blocktype), blocknr);
414 case JFS_DESCRIPTOR_BLOCK:
415 dump_descriptor_block(out_file, source, buf, jsb,
420 case JFS_COMMIT_BLOCK:
426 case JFS_REVOKE_BLOCK:
427 dump_revoke_block(out_file, buf, jsb,
435 fprintf (out_file, "Unexpected block type %u at "
436 "block %u.\n", blocktype, blocknr);
443 static void dump_descriptor_block(FILE *out_file,
444 struct journal_source *source,
446 journal_superblock_t *jsb,
447 unsigned int *blockp, int blocksize,
452 journal_block_tag_t *tag;
453 unsigned int blocknr;
458 offset = sizeof(journal_header_t);
462 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
463 "block %u:\n", transaction, blocknr);
469 /* Work out the location of the current tag, and skip to
472 tag = (journal_block_tag_t *) tagp;
473 offset += sizeof(journal_block_tag_t);
475 /* ... and if we have gone too far, then we've reached the
476 end of this block. */
477 if (offset > blocksize)
480 tag_block = be32_to_cpu(tag->t_blocknr);
481 tag_flags = be32_to_cpu(tag->t_flags);
483 if (!(tag_flags & JFS_FLAG_SAME_UUID))
486 dump_metadata_block(out_file, source, jsb,
487 blocknr, tag_block, blocksize,
493 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
499 static void dump_revoke_block(FILE *out_file, char *buf,
500 journal_superblock_t *jsb,
501 unsigned int blocknr, int blocksize,
505 journal_revoke_header_t *header;
506 unsigned int *entry, rblock;
509 fprintf(out_file, "Dumping revoke block, sequence %u, at "
510 "block %u:\n", transaction, blocknr);
512 header = (journal_revoke_header_t *) buf;
513 offset = sizeof(journal_revoke_header_t);
514 max = be32_to_cpu(header->r_count);
516 while (offset < max) {
517 entry = (unsigned int *) (buf + offset);
518 rblock = be32_to_cpu(*entry);
519 if (dump_all || rblock == block_to_dump) {
520 fprintf(out_file, " Revoke FS block %u", rblock);
522 fprintf(out_file, "\n");
524 fprintf(out_file," at block %u, sequence %u\n",
525 blocknr, transaction);
532 static void show_extent(FILE *out_file, int start_extent, int end_extent,
535 if (start_extent >= 0 && first_block != 0)
536 fprintf(out_file, "(%d+%u): %u ",
537 start_extent, end_extent-start_extent, first_block);
540 static void show_indirect(FILE *out_file, const char *name, __u32 where)
543 fprintf(out_file, "(%s): %u ", name, where);
547 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
548 journal_superblock_t *jsb,
549 unsigned int log_blocknr,
550 unsigned int fs_blocknr,
559 || (fs_blocknr == block_to_dump)
560 || (fs_blocknr == inode_block_to_dump)
561 || (fs_blocknr == bitmap_to_dump)))
564 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
566 fprintf(out_file, "sequence %u, ", transaction);
567 fprintf(out_file, "journal block %u\n", log_blocknr);
569 /* There are two major special cases to parse:
571 * If this block is a block
572 * bitmap block, we need to give it special treatment so that we
573 * can log any allocates and deallocates which affect the
574 * block_to_dump query block.
576 * If the block is an inode block for the inode being searched
577 * for, then we need to dump the contents of that inode
578 * structure symbolically.
581 if (!(dump_contents && dump_all)
582 && fs_blocknr != block_to_dump
583 && fs_blocknr != bitmap_to_dump
584 && fs_blocknr != inode_block_to_dump)
587 retval = read_journal_block("logdump", source,
588 blocksize * log_blocknr,
589 buf, blocksize, &got);
593 if (fs_blocknr == bitmap_to_dump) {
594 struct ext2_super_block *super;
597 super = current_fs->super;
598 offset = ((fs_blocknr - super->s_first_data_block) %
599 super->s_blocks_per_group);
601 fprintf(out_file, " (block bitmap for block %u: "
604 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
607 if (fs_blocknr == inode_block_to_dump) {
608 struct ext2_inode *inode;
609 int first, prev, this, start_extent, i;
611 fprintf(out_file, " (inode block for inode %u):\n",
614 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
615 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
617 /* Dump out the direct/indirect blocks here:
618 * internal_dump_inode can only dump them from the main
619 * on-disk inode, not from the journaled copy of the
622 fprintf (out_file, " Blocks: ");
623 first = prev = start_extent = -1;
625 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
626 this = inode->i_block[i];
627 if (start_extent >= 0 && this == prev+1) {
631 show_extent(out_file, start_extent, i, first);
636 show_extent(out_file, start_extent, i, first);
637 show_indirect(out_file, "IND", inode->i_block[i++]);
638 show_indirect(out_file, "DIND", inode->i_block[i++]);
639 show_indirect(out_file, "TIND", inode->i_block[i++]);
641 fprintf(out_file, "\n");
645 do_hexdump(out_file, buf, blocksize);
649 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
657 charp = (char *) buf;
659 for (i=0; i<blocksize; i+=16) {
660 fprintf(out_file, " %04x: ", i);
661 for (j=0; j<16; j+=4)
662 fprintf(out_file, "%08x ", *intp++);
663 for (j=0; j<16; j++) {
665 if (c < ' ' || c >= 127)
667 fprintf(out_file, "%c", c);
669 fprintf(out_file, "\n");