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 int dump_all, dump_contents, dump_descriptors;
40 unsigned int block_to_dump, group_to_dump, bitmap_to_dump;
41 unsigned int inode_block_to_dump, inode_offset_to_dump, bitmap_to_dump;
42 ext2_ino_t inode_to_dump;
46 enum journal_location where;
51 static void dump_journal(char *, FILE *, struct journal_source *);
53 static void dump_descriptor_block(FILE *, struct journal_source *,
54 char *, journal_superblock_t *,
55 unsigned int *, int, tid_t);
57 static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
58 unsigned int, int, tid_t);
60 static void dump_metadata_block(FILE *, struct journal_source *,
61 journal_superblock_t*,
62 unsigned int, unsigned int, int, tid_t);
64 static void do_hexdump (FILE *, char *, int);
66 #define WRAP(jsb, blocknr) \
67 if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \
68 blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \
69 be32_to_cpu((jsb)->s_first));
72 void do_logdump(int argc, char **argv)
79 char *inode_spec = NULL;
80 char *journal_fn = NULL;
83 ext2_ino_t journal_inum;
84 struct ext2_inode journal_inode;
85 ext2_file_t journal_file;
87 const char *logdump_usage = ("Usage: logdump "
88 "[-ac] [-b<block>] [-i<inode>] "
89 "[-f<journal_file>] [output_file]");
90 struct journal_source journal_source;
91 struct ext2_super_block *es = NULL;
93 journal_source.where = 0;
94 journal_source.fd = 0;
95 journal_source.file = 0;
101 inode_block_to_dump = -1;
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;
133 com_err(argv[0], 0, logdump_usage);
137 if (optind != argc && optind != argc-1) {
138 com_err(argv[0], 0, logdump_usage);
143 es = current_fs->super;
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 / es->s_inodes_per_group);
157 group_offset = ((inode_to_dump - 1)
158 % es->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 es->s_first_data_block)
187 / es->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 = es->s_journal_inum)) {
207 if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
209 "no journal backup in super block\n");
212 memset(&journal_inode, 0, sizeof(struct ext2_inode));
213 memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
215 journal_inode.i_size = es->s_jnl_blocks[16];
216 journal_inode.i_links_count = 1;
217 journal_inode.i_mode = LINUX_S_IFREG | 0600;
219 if (debugfs_read_inode(journal_inum, &journal_inode,
224 retval = ext2fs_file_open2(current_fs, journal_inum,
225 &journal_inode, 0, &journal_file);
227 com_err(argv[0], retval, "while opening ext2 file");
230 journal_source.where = JOURNAL_IS_INTERNAL;
231 journal_source.file = journal_file;
235 uuid_unparse(es->s_journal_uuid, uuid);
236 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
238 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
240 com_err(argv[0], 0, "filesystem has no journal");
243 journal_fd = open(journal_fn, O_RDONLY, 0);
244 if (journal_fd < 0) {
245 com_err(argv[0], errno, "while opening %s for logdump",
250 fprintf(out_file, "Using external journal found at %s\n",
253 journal_source.where = JOURNAL_IS_EXTERNAL;
254 journal_source.fd = journal_fd;
257 dump_journal(argv[0], out_file, &journal_source);
259 if (journal_source.where == JOURNAL_IS_INTERNAL)
260 ext2fs_file_close(journal_file);
264 if (out_file != stdout)
271 static int read_journal_block(const char *cmd, struct journal_source *source,
272 off_t offset, char *buf, int size,
277 if (source->where == JOURNAL_IS_EXTERNAL) {
278 if (lseek(source->fd, offset, SEEK_SET) < 0) {
280 com_err(cmd, retval, "while seeking in reading journal");
283 retval = read(source->fd, buf, size);
290 retval = ext2fs_file_lseek(source->file, offset,
291 EXT2_SEEK_SET, NULL);
293 com_err(cmd, retval, "while seeking in reading journal");
297 retval = ext2fs_file_read(source->file, buf, size, got);
301 com_err(cmd, retval, "while while reading journal");
302 else if (*got != size) {
303 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
310 static const char *type_to_name(int btype)
313 case JFS_DESCRIPTOR_BLOCK:
314 return "descriptor block";
315 case JFS_COMMIT_BLOCK:
316 return "commit block";
317 case JFS_SUPERBLOCK_V1:
318 return "V1 superblock";
319 case JFS_SUPERBLOCK_V2:
320 return "V2 superblock";
321 case JFS_REVOKE_BLOCK:
322 return "revoke table";
324 return "unrecognised type";
328 static void dump_journal(char *cmdname, FILE *out_file,
329 struct journal_source *source)
331 struct ext2_super_block *sb;
332 char jsb_buffer[1024];
334 journal_superblock_t *jsb;
335 int blocksize = 1024;
338 __u32 magic, sequence, blocktype;
339 journal_header_t *header;
342 unsigned int blocknr = 0;
344 /* First, check to see if there's an ext2 superblock header */
345 retval = read_journal_block(cmdname, source, 0,
350 jsb = (journal_superblock_t *) buf;
351 sb = (struct ext2_super_block *) (buf+1024);
353 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
354 ext2fs_swap_super(sb);
357 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
358 (sb->s_magic == EXT2_SUPER_MAGIC) &&
359 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
360 blocksize = EXT2_BLOCK_SIZE(sb);
361 blocknr = (blocksize == 1024) ? 2 : 1;
362 uuid_unparse(sb->s_uuid, jsb_buffer);
363 fprintf(out_file, "Ext2 superblock header found.\n");
365 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
366 fprintf(out_file, "\tblocksize=%d\n", blocksize);
367 fprintf(out_file, "\tjournal data size %ld\n",
368 (long) sb->s_blocks_count);
372 /* Next, read the journal superblock */
374 retval = read_journal_block(cmdname, source, blocknr*blocksize,
375 jsb_buffer, 1024, &got);
379 jsb = (journal_superblock_t *) jsb_buffer;
380 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
382 "Journal superblock magic number invalid!\n");
385 blocksize = be32_to_cpu(jsb->s_blocksize);
386 transaction = be32_to_cpu(jsb->s_sequence);
387 blocknr = be32_to_cpu(jsb->s_start);
389 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
390 blocknr, transaction);
393 /* Empty journal, nothing to do. */
397 retval = read_journal_block(cmdname, source,
398 blocknr*blocksize, buf,
400 if (retval || got != blocksize)
403 header = (journal_header_t *) buf;
405 magic = be32_to_cpu(header->h_magic);
406 sequence = be32_to_cpu(header->h_sequence);
407 blocktype = be32_to_cpu(header->h_blocktype);
409 if (magic != JFS_MAGIC_NUMBER) {
410 fprintf (out_file, "No magic number at block %u: "
411 "end of journal.\n", blocknr);
415 if (sequence != transaction) {
416 fprintf (out_file, "Found sequence %u (not %u) at "
417 "block %u: end of journal.\n",
418 sequence, transaction, blocknr);
422 if (dump_descriptors) {
423 fprintf (out_file, "Found expected sequence %u, "
424 "type %u (%s) at block %u\n",
426 type_to_name(blocktype), blocknr);
430 case JFS_DESCRIPTOR_BLOCK:
431 dump_descriptor_block(out_file, source, buf, jsb,
436 case JFS_COMMIT_BLOCK:
442 case JFS_REVOKE_BLOCK:
443 dump_revoke_block(out_file, buf, jsb,
451 fprintf (out_file, "Unexpected block type %u at "
452 "block %u.\n", blocktype, blocknr);
459 static void dump_descriptor_block(FILE *out_file,
460 struct journal_source *source,
462 journal_superblock_t *jsb,
463 unsigned int *blockp, int blocksize,
468 journal_block_tag_t *tag;
469 unsigned int blocknr;
474 offset = sizeof(journal_header_t);
478 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
479 "block %u:\n", transaction, blocknr);
485 /* Work out the location of the current tag, and skip to
488 tag = (journal_block_tag_t *) tagp;
489 offset += sizeof(journal_block_tag_t);
491 /* ... and if we have gone too far, then we've reached the
492 end of this block. */
493 if (offset > blocksize)
496 tag_block = be32_to_cpu(tag->t_blocknr);
497 tag_flags = be32_to_cpu(tag->t_flags);
499 if (!(tag_flags & JFS_FLAG_SAME_UUID))
502 dump_metadata_block(out_file, source, jsb,
503 blocknr, tag_block, blocksize,
509 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
515 static void dump_revoke_block(FILE *out_file, char *buf,
516 journal_superblock_t *jsb,
517 unsigned int blocknr, int blocksize,
521 journal_revoke_header_t *header;
522 unsigned int *entry, rblock;
525 fprintf(out_file, "Dumping revoke block, sequence %u, at "
526 "block %u:\n", transaction, blocknr);
528 header = (journal_revoke_header_t *) buf;
529 offset = sizeof(journal_revoke_header_t);
530 max = be32_to_cpu(header->r_count);
532 while (offset < max) {
533 entry = (unsigned int *) (buf + offset);
534 rblock = be32_to_cpu(*entry);
535 if (dump_all || rblock == block_to_dump) {
536 fprintf(out_file, " Revoke FS block %u", rblock);
538 fprintf(out_file, "\n");
540 fprintf(out_file," at block %u, sequence %u\n",
541 blocknr, transaction);
548 static void show_extent(FILE *out_file, int start_extent, int end_extent,
551 if (start_extent >= 0 && first_block != 0)
552 fprintf(out_file, "(%d+%u): %u ",
553 start_extent, end_extent-start_extent, first_block);
556 static void show_indirect(FILE *out_file, const char *name, __u32 where)
559 fprintf(out_file, "(%s): %u ", name, where);
563 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
564 journal_superblock_t *jsb,
565 unsigned int log_blocknr,
566 unsigned int fs_blocknr,
575 || (fs_blocknr == block_to_dump)
576 || (fs_blocknr == inode_block_to_dump)
577 || (fs_blocknr == bitmap_to_dump)))
580 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
582 fprintf(out_file, "sequence %u, ", transaction);
583 fprintf(out_file, "journal block %u\n", log_blocknr);
585 /* There are two major special cases to parse:
587 * If this block is a block
588 * bitmap block, we need to give it special treatment so that we
589 * can log any allocates and deallocates which affect the
590 * block_to_dump query block.
592 * If the block is an inode block for the inode being searched
593 * for, then we need to dump the contents of that inode
594 * structure symbolically.
597 if (!(dump_contents && dump_all)
598 && fs_blocknr != block_to_dump
599 && fs_blocknr != bitmap_to_dump
600 && fs_blocknr != inode_block_to_dump)
603 retval = read_journal_block("logdump", source,
604 blocksize * log_blocknr,
605 buf, blocksize, &got);
609 if (fs_blocknr == bitmap_to_dump) {
610 struct ext2_super_block *super;
613 super = current_fs->super;
614 offset = ((fs_blocknr - super->s_first_data_block) %
615 super->s_blocks_per_group);
617 fprintf(out_file, " (block bitmap for block %u: "
620 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
623 if (fs_blocknr == inode_block_to_dump) {
624 struct ext2_inode *inode;
625 int first, prev, this, start_extent, i;
627 fprintf(out_file, " (inode block for inode %u):\n",
630 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
631 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
633 /* Dump out the direct/indirect blocks here:
634 * internal_dump_inode can only dump them from the main
635 * on-disk inode, not from the journaled copy of the
638 fprintf (out_file, " Blocks: ");
639 first = prev = start_extent = -1;
641 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
642 this = inode->i_block[i];
643 if (start_extent >= 0 && this == prev+1) {
647 show_extent(out_file, start_extent, i, first);
652 show_extent(out_file, start_extent, i, first);
653 show_indirect(out_file, "IND", inode->i_block[i++]);
654 show_indirect(out_file, "DIND", inode->i_block[i++]);
655 show_indirect(out_file, "TIND", inode->i_block[i++]);
657 fprintf(out_file, "\n");
661 do_hexdump(out_file, buf, blocksize);
665 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
673 charp = (char *) buf;
675 for (i=0; i<blocksize; i+=16) {
676 fprintf(out_file, " %04x: ", i);
677 for (j=0; j<16; j+=4)
678 fprintf(out_file, "%08x ", *intp++);
679 for (j=0; j<16; j++) {
681 if (c < ' ' || c >= 127)
683 fprintf(out_file, "%c", c);
685 fprintf(out_file, "\n");