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;
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;
205 /* Set up to read journal from the open filesystem */
206 if (check_fs_open(argv[0]))
208 journal_inum = current_fs->super->s_journal_inum;
210 com_err(argv[0], 0, "filesystem has no journal");
214 retval = ext2fs_read_inode(current_fs, journal_inum,
217 com_err(argv[0], retval,
218 "while reading inode %u", journal_inum);
222 retval = ext2fs_file_open(current_fs, journal_inum,
225 com_err(argv[0], retval, "while opening ext2 file");
229 journal_source.where = JOURNAL_IS_INTERNAL;
230 journal_source.file = journal_file;
233 dump_journal(argv[0], out_file, &journal_source);
235 if (journal_source.where == JOURNAL_IS_INTERNAL)
236 ext2fs_file_close(journal_file);
240 if (out_file != stdout)
247 static int read_journal_block(const char *cmd, struct journal_source *source,
248 off_t offset, char *buf, int size,
253 if (source->where == JOURNAL_IS_EXTERNAL) {
254 if (lseek(source->fd, offset, SEEK_SET) < 0) {
256 com_err(cmd, retval, "while seeking in reading journal");
259 retval = read(source->fd, buf, size);
266 retval = ext2fs_file_lseek(source->file, offset,
267 EXT2_SEEK_SET, NULL);
269 com_err(cmd, retval, "while seeking in reading journal");
273 retval = ext2fs_file_read(source->file, buf, size, got);
277 com_err(cmd, retval, "while while reading journal");
278 else if (*got != size) {
279 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
286 static const char *type_to_name(int btype)
289 case JFS_DESCRIPTOR_BLOCK:
290 return "descriptor block";
291 case JFS_COMMIT_BLOCK:
292 return "commit block";
293 case JFS_SUPERBLOCK_V1:
294 return "V1 superblock";
295 case JFS_SUPERBLOCK_V2:
296 return "V2 superblock";
297 case JFS_REVOKE_BLOCK:
298 return "revoke table";
300 return "unrecognised type";
304 static void dump_journal(char *cmdname, FILE *out_file,
305 struct journal_source *source)
307 char jsb_buffer[1024];
309 journal_superblock_t *jsb;
313 __u32 magic, sequence, blocktype;
314 journal_header_t *header;
317 unsigned int blocknr;
319 /* First: locate the journal superblock */
321 retval = read_journal_block(cmdname, source, 0,
322 jsb_buffer, 1024, &got);
326 jsb = (journal_superblock_t *) jsb_buffer;
327 blocksize = be32_to_cpu(jsb->s_blocksize);
328 transaction = be32_to_cpu(jsb->s_sequence);
329 blocknr = be32_to_cpu(jsb->s_start);
331 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
332 blocknr, transaction);
335 /* Empty journal, nothing to do. */
339 retval = read_journal_block(cmdname, source,
340 blocknr*blocksize, buf,
342 if (retval || got != blocksize)
345 header = (journal_header_t *) buf;
347 magic = be32_to_cpu(header->h_magic);
348 sequence = be32_to_cpu(header->h_sequence);
349 blocktype = be32_to_cpu(header->h_blocktype);
351 if (magic != JFS_MAGIC_NUMBER) {
352 fprintf (out_file, "No magic number at block %u: "
353 "end of journal.\n", blocknr);
357 if (sequence != transaction) {
358 fprintf (out_file, "Found sequence %u (not %u) at "
359 "block %u: end of journal.\n",
360 sequence, transaction, blocknr);
364 if (dump_descriptors) {
365 fprintf (out_file, "Found expected sequence %u, "
366 "type %u (%s) at block %u\n",
368 type_to_name(blocktype), blocknr);
372 case JFS_DESCRIPTOR_BLOCK:
373 dump_descriptor_block(out_file, source, buf, jsb,
378 case JFS_COMMIT_BLOCK:
384 case JFS_REVOKE_BLOCK:
385 dump_revoke_block(out_file, buf, jsb,
393 fprintf (out_file, "Unexpected block type %u at "
394 "block %u.\n", blocktype, blocknr);
401 static void dump_descriptor_block(FILE *out_file,
402 struct journal_source *source,
404 journal_superblock_t *jsb,
405 unsigned int *blockp, int blocksize,
410 journal_block_tag_t *tag;
411 unsigned int blocknr;
416 offset = sizeof(journal_header_t);
420 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
421 "block %u:\n", transaction, blocknr);
427 /* Work out the location of the current tag, and skip to
430 tag = (journal_block_tag_t *) tagp;
431 offset += sizeof(journal_block_tag_t);
433 /* ... and if we have gone too far, then we've reached the
434 end of this block. */
435 if (offset > blocksize)
438 tag_block = be32_to_cpu(tag->t_blocknr);
439 tag_flags = be32_to_cpu(tag->t_flags);
441 if (!(tag_flags & JFS_FLAG_SAME_UUID))
444 dump_metadata_block(out_file, source, jsb,
445 blocknr, tag_block, blocksize,
451 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
457 static void dump_revoke_block(FILE *out_file, char *buf,
458 journal_superblock_t *jsb,
459 unsigned int blocknr, int blocksize,
463 journal_revoke_header_t *header;
464 unsigned int *entry, rblock;
467 fprintf(out_file, "Dumping revoke block, sequence %u, at "
468 "block %u:\n", transaction, blocknr);
470 header = (journal_revoke_header_t *) buf;
471 offset = sizeof(journal_revoke_header_t);
472 max = be32_to_cpu(header->r_count);
474 while (offset < max) {
475 entry = (unsigned int *) (buf + offset);
476 rblock = be32_to_cpu(*entry);
477 if (dump_all || rblock == block_to_dump) {
478 fprintf(out_file, " Revoke FS block %u", rblock);
480 fprintf(out_file, "\n");
482 fprintf(out_file," at block %u, sequence %u\n",
483 blocknr, transaction);
490 static void show_extent(FILE *out_file, int start_extent, int end_extent,
493 if (start_extent >= 0 && first_block != 0)
494 fprintf(out_file, "(%d+%u): %u ",
495 start_extent, end_extent-start_extent, first_block);
498 static void show_indirect(FILE *out_file, const char *name, __u32 where)
501 fprintf(out_file, "(%s): %u ", name, where);
505 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
506 journal_superblock_t *jsb,
507 unsigned int log_blocknr,
508 unsigned int fs_blocknr,
517 || (fs_blocknr == block_to_dump)
518 || (fs_blocknr == inode_block_to_dump)
519 || (fs_blocknr == bitmap_to_dump)))
522 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
524 fprintf(out_file, "sequence %u, ", transaction);
525 fprintf(out_file, "journal block %u\n", log_blocknr);
527 /* There are two major special cases to parse:
529 * If this block is a block
530 * bitmap block, we need to give it special treatment so that we
531 * can log any allocates and deallocates which affect the
532 * block_to_dump query block.
534 * If the block is an inode block for the inode being searched
535 * for, then we need to dump the contents of that inode
536 * structure symbolically.
539 if (!(dump_contents && dump_all)
540 && fs_blocknr != block_to_dump
541 && fs_blocknr != bitmap_to_dump
542 && fs_blocknr != inode_block_to_dump)
545 retval = read_journal_block("logdump", source,
546 blocksize * log_blocknr,
547 buf, blocksize, &got);
551 if (fs_blocknr == bitmap_to_dump) {
552 struct ext2_super_block *super;
555 super = current_fs->super;
556 offset = ((fs_blocknr - super->s_first_data_block) %
557 super->s_blocks_per_group);
559 fprintf(out_file, " (block bitmap for block %u: "
562 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
565 if (fs_blocknr == inode_block_to_dump) {
566 struct ext2_inode *inode;
567 int first, prev, this, start_extent, i;
569 fprintf(out_file, " (inode block for inode %u):\n",
572 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
573 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
575 /* Dump out the direct/indirect blocks here:
576 * internal_dump_inode can only dump them from the main
577 * on-disk inode, not from the journaled copy of the
580 fprintf (out_file, " Blocks: ");
581 first = prev = start_extent = -1;
583 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
584 this = inode->i_block[i];
585 if (start_extent >= 0 && this == prev+1) {
589 show_extent(out_file, start_extent, i, first);
594 show_extent(out_file, start_extent, i, first);
595 show_indirect(out_file, "IND", inode->i_block[i++]);
596 show_indirect(out_file, "DIND", inode->i_block[i++]);
597 show_indirect(out_file, "TIND", inode->i_block[i++]);
599 fprintf(out_file, "\n");
603 do_hexdump(out_file, buf, blocksize);
607 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
615 charp = (char *) buf;
617 for (i=0; i<blocksize; i+=16) {
618 fprintf(out_file, " %04x: ", i);
619 for (j=0; j<16; j+=4)
620 fprintf(out_file, "%08x ", *intp++);
621 for (j=0; j<16; j++) {
623 if (c < ' ' || c >= 127)
625 fprintf(out_file, "%c", c);
627 fprintf(out_file, "\n");