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;
82 ext2_ino_t journal_inum;
83 struct ext2_inode journal_inode;
84 ext2_file_t journal_file;
86 const char *logdump_usage = ("Usage: logdump "
87 "[-ac] [-b<block>] [-i<inode>] "
88 "[-f<journal_file>] [output_file]");
89 struct journal_source journal_source;
90 struct ext2_super_block *es = NULL;
92 journal_source.where = 0;
93 journal_source.fd = 0;
94 journal_source.file = 0;
100 inode_block_to_dump = -1;
104 while ((c = getopt (argc, argv, "ab:ci:f:")) != EOF) {
110 block_to_dump = strtoul(optarg, &tmp, 0);
113 "Bad block number - %s", optarg);
116 dump_descriptors = 0;
126 dump_descriptors = 0;
129 com_err(argv[0], 0, logdump_usage);
133 if (optind != argc && optind != argc-1) {
134 com_err(argv[0], 0, logdump_usage);
139 es = current_fs->super;
142 int inode_group, group_offset, inodes_per_block;
144 if (check_fs_open(argv[0]))
147 inode_to_dump = string_to_inode(inode_spec);
151 inode_group = ((inode_to_dump - 1)
152 / es->s_inodes_per_group);
153 group_offset = ((inode_to_dump - 1)
154 % es->s_inodes_per_group);
155 inodes_per_block = (current_fs->blocksize
156 / sizeof(struct ext2_inode));
158 inode_block_to_dump =
159 current_fs->group_desc[inode_group].bg_inode_table +
160 (group_offset / inodes_per_block);
161 inode_offset_to_dump = ((group_offset % inodes_per_block)
162 * sizeof(struct ext2_inode));
163 printf("Inode %u is at group %u, block %u, offset %u\n",
164 inode_to_dump, inode_group,
165 inode_block_to_dump, inode_offset_to_dump);
168 if (optind == argc) {
171 out_fn = argv[optind];
172 out_file = fopen(out_fn, "w");
174 com_err(argv[0], errno, "while opening %s for logdump",
180 if (block_to_dump != -1 && current_fs != NULL) {
181 group_to_dump = ((block_to_dump -
182 es->s_first_data_block)
183 / es->s_blocks_per_group);
184 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
187 if (!journal_fn && check_fs_open(argv[0]))
191 /* Set up to read journal from a regular file somewhere */
192 journal_fd = open(journal_fn, O_RDONLY, 0);
193 if (journal_fd < 0) {
194 com_err(argv[0], errno, "while opening %s for logdump",
199 journal_source.where = JOURNAL_IS_EXTERNAL;
200 journal_source.fd = journal_fd;
201 } else if ((journal_inum = es->s_journal_inum)) {
202 if (debugfs_read_inode(journal_inum, &journal_inode, argv[0]))
205 retval = ext2fs_file_open(current_fs, journal_inum,
208 com_err(argv[0], retval, "while opening ext2 file");
211 journal_source.where = JOURNAL_IS_INTERNAL;
212 journal_source.file = journal_file;
216 uuid_unparse(es->s_journal_uuid, uuid);
217 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
219 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
221 com_err(argv[0], 0, "filesystem has no journal");
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;
238 dump_journal(argv[0], out_file, &journal_source);
240 if (journal_source.where == JOURNAL_IS_INTERNAL)
241 ext2fs_file_close(journal_file);
245 if (out_file != stdout)
252 static int read_journal_block(const char *cmd, struct journal_source *source,
253 off_t offset, char *buf, int size,
258 if (source->where == JOURNAL_IS_EXTERNAL) {
259 if (lseek(source->fd, offset, SEEK_SET) < 0) {
261 com_err(cmd, retval, "while seeking in reading journal");
264 retval = read(source->fd, buf, size);
271 retval = ext2fs_file_lseek(source->file, offset,
272 EXT2_SEEK_SET, NULL);
274 com_err(cmd, retval, "while seeking in reading journal");
278 retval = ext2fs_file_read(source->file, buf, size, got);
282 com_err(cmd, retval, "while while reading journal");
283 else if (*got != size) {
284 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
291 static const char *type_to_name(int btype)
294 case JFS_DESCRIPTOR_BLOCK:
295 return "descriptor block";
296 case JFS_COMMIT_BLOCK:
297 return "commit block";
298 case JFS_SUPERBLOCK_V1:
299 return "V1 superblock";
300 case JFS_SUPERBLOCK_V2:
301 return "V2 superblock";
302 case JFS_REVOKE_BLOCK:
303 return "revoke table";
305 return "unrecognised type";
309 static void dump_journal(char *cmdname, FILE *out_file,
310 struct journal_source *source)
312 struct ext2_super_block *sb;
313 char jsb_buffer[1024];
315 journal_superblock_t *jsb;
316 int blocksize = 1024;
319 __u32 magic, sequence, blocktype;
320 journal_header_t *header;
323 unsigned int blocknr = 0;
325 /* First, check to see if there's an ext2 superblock header */
326 retval = read_journal_block(cmdname, source, 0,
331 jsb = (journal_superblock_t *) buf;
332 sb = (struct ext2_super_block *) (buf+1024);
334 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
335 ext2fs_swap_super(sb);
338 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
339 (sb->s_magic == EXT2_SUPER_MAGIC) &&
340 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
341 blocksize = EXT2_BLOCK_SIZE(sb);
342 blocknr = (blocksize == 1024) ? 2 : 1;
343 uuid_unparse(sb->s_uuid, jsb_buffer);
344 fprintf(out_file, "Ext2 superblock header found.\n");
346 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
347 fprintf(out_file, "\tblocksize=%d\n", blocksize);
348 fprintf(out_file, "\tjournal data size %ld\n",
349 (long) sb->s_blocks_count);
353 /* Next, read the journal superblock */
355 retval = read_journal_block(cmdname, source, blocknr*blocksize,
356 jsb_buffer, 1024, &got);
360 jsb = (journal_superblock_t *) jsb_buffer;
361 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
363 "Journal superblock magic number invalid!\n");
366 blocksize = be32_to_cpu(jsb->s_blocksize);
367 transaction = be32_to_cpu(jsb->s_sequence);
368 blocknr = be32_to_cpu(jsb->s_start);
370 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
371 blocknr, transaction);
374 /* Empty journal, nothing to do. */
378 retval = read_journal_block(cmdname, source,
379 blocknr*blocksize, buf,
381 if (retval || got != blocksize)
384 header = (journal_header_t *) buf;
386 magic = be32_to_cpu(header->h_magic);
387 sequence = be32_to_cpu(header->h_sequence);
388 blocktype = be32_to_cpu(header->h_blocktype);
390 if (magic != JFS_MAGIC_NUMBER) {
391 fprintf (out_file, "No magic number at block %u: "
392 "end of journal.\n", blocknr);
396 if (sequence != transaction) {
397 fprintf (out_file, "Found sequence %u (not %u) at "
398 "block %u: end of journal.\n",
399 sequence, transaction, blocknr);
403 if (dump_descriptors) {
404 fprintf (out_file, "Found expected sequence %u, "
405 "type %u (%s) at block %u\n",
407 type_to_name(blocktype), blocknr);
411 case JFS_DESCRIPTOR_BLOCK:
412 dump_descriptor_block(out_file, source, buf, jsb,
417 case JFS_COMMIT_BLOCK:
423 case JFS_REVOKE_BLOCK:
424 dump_revoke_block(out_file, buf, jsb,
432 fprintf (out_file, "Unexpected block type %u at "
433 "block %u.\n", blocktype, blocknr);
440 static void dump_descriptor_block(FILE *out_file,
441 struct journal_source *source,
443 journal_superblock_t *jsb,
444 unsigned int *blockp, int blocksize,
449 journal_block_tag_t *tag;
450 unsigned int blocknr;
455 offset = sizeof(journal_header_t);
459 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
460 "block %u:\n", transaction, blocknr);
466 /* Work out the location of the current tag, and skip to
469 tag = (journal_block_tag_t *) tagp;
470 offset += sizeof(journal_block_tag_t);
472 /* ... and if we have gone too far, then we've reached the
473 end of this block. */
474 if (offset > blocksize)
477 tag_block = be32_to_cpu(tag->t_blocknr);
478 tag_flags = be32_to_cpu(tag->t_flags);
480 if (!(tag_flags & JFS_FLAG_SAME_UUID))
483 dump_metadata_block(out_file, source, jsb,
484 blocknr, tag_block, blocksize,
490 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
496 static void dump_revoke_block(FILE *out_file, char *buf,
497 journal_superblock_t *jsb,
498 unsigned int blocknr, int blocksize,
502 journal_revoke_header_t *header;
503 unsigned int *entry, rblock;
506 fprintf(out_file, "Dumping revoke block, sequence %u, at "
507 "block %u:\n", transaction, blocknr);
509 header = (journal_revoke_header_t *) buf;
510 offset = sizeof(journal_revoke_header_t);
511 max = be32_to_cpu(header->r_count);
513 while (offset < max) {
514 entry = (unsigned int *) (buf + offset);
515 rblock = be32_to_cpu(*entry);
516 if (dump_all || rblock == block_to_dump) {
517 fprintf(out_file, " Revoke FS block %u", rblock);
519 fprintf(out_file, "\n");
521 fprintf(out_file," at block %u, sequence %u\n",
522 blocknr, transaction);
529 static void show_extent(FILE *out_file, int start_extent, int end_extent,
532 if (start_extent >= 0 && first_block != 0)
533 fprintf(out_file, "(%d+%u): %u ",
534 start_extent, end_extent-start_extent, first_block);
537 static void show_indirect(FILE *out_file, const char *name, __u32 where)
540 fprintf(out_file, "(%s): %u ", name, where);
544 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
545 journal_superblock_t *jsb,
546 unsigned int log_blocknr,
547 unsigned int fs_blocknr,
556 || (fs_blocknr == block_to_dump)
557 || (fs_blocknr == inode_block_to_dump)
558 || (fs_blocknr == bitmap_to_dump)))
561 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
563 fprintf(out_file, "sequence %u, ", transaction);
564 fprintf(out_file, "journal block %u\n", log_blocknr);
566 /* There are two major special cases to parse:
568 * If this block is a block
569 * bitmap block, we need to give it special treatment so that we
570 * can log any allocates and deallocates which affect the
571 * block_to_dump query block.
573 * If the block is an inode block for the inode being searched
574 * for, then we need to dump the contents of that inode
575 * structure symbolically.
578 if (!(dump_contents && dump_all)
579 && fs_blocknr != block_to_dump
580 && fs_blocknr != bitmap_to_dump
581 && fs_blocknr != inode_block_to_dump)
584 retval = read_journal_block("logdump", source,
585 blocksize * log_blocknr,
586 buf, blocksize, &got);
590 if (fs_blocknr == bitmap_to_dump) {
591 struct ext2_super_block *super;
594 super = current_fs->super;
595 offset = ((fs_blocknr - super->s_first_data_block) %
596 super->s_blocks_per_group);
598 fprintf(out_file, " (block bitmap for block %u: "
601 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
604 if (fs_blocknr == inode_block_to_dump) {
605 struct ext2_inode *inode;
606 int first, prev, this, start_extent, i;
608 fprintf(out_file, " (inode block for inode %u):\n",
611 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
612 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
614 /* Dump out the direct/indirect blocks here:
615 * internal_dump_inode can only dump them from the main
616 * on-disk inode, not from the journaled copy of the
619 fprintf (out_file, " Blocks: ");
620 first = prev = start_extent = -1;
622 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
623 this = inode->i_block[i];
624 if (start_extent >= 0 && this == prev+1) {
628 show_extent(out_file, start_extent, i, first);
633 show_extent(out_file, start_extent, i, first);
634 show_indirect(out_file, "IND", inode->i_block[i++]);
635 show_indirect(out_file, "DIND", inode->i_block[i++]);
636 show_indirect(out_file, "TIND", inode->i_block[i++]);
638 fprintf(out_file, "\n");
642 do_hexdump(out_file, buf, blocksize);
646 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
654 charp = (char *) buf;
656 for (i=0; i<blocksize; i+=16) {
657 fprintf(out_file, " %04x: ", i);
658 for (j=0; j<16; j+=4)
659 fprintf(out_file, "%08x ", *intp++);
660 for (j=0; j<16; j++) {
662 if (c < ' ' || c >= 127)
664 fprintf(out_file, "%c", c);
666 fprintf(out_file, "\n");