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)
82 char *inode_spec = NULL;
83 char *journal_fn = NULL;
85 ext2_ino_t journal_inum;
86 struct ext2_inode journal_inode;
87 ext2_file_t journal_file;
91 const char *logdump_usage = ("Usage: logdump "
92 "[-ac] [-b<block>] [-i<inode>] "
93 "[-f<journal_file>] [output_file]");
95 struct journal_source journal_source = {};
99 optreset = 1; /* Makes BSD getopt happy */
103 dump_descriptors = 1;
106 inode_block_to_dump = -1;
109 while ((c = getopt (argc, argv, "ab:ci:f:")) != EOF) {
115 block_to_dump = strtoul(optarg, &tmp, 0);
118 "Bad block number - %s", optarg);
121 dump_descriptors = 0;
131 dump_descriptors = 0;
134 com_err(argv[0], 0, logdump_usage);
138 if (optind != argc && optind != argc-1) {
139 com_err(argv[0], 0, logdump_usage);
144 int inode_group, group_offset, inodes_per_block;
146 if (check_fs_open(argv[0]))
149 inode_to_dump = string_to_inode(inode_spec);
153 inode_group = ((inode_to_dump - 1)
154 / current_fs->super->s_inodes_per_group);
155 group_offset = ((inode_to_dump - 1)
156 % current_fs->super->s_inodes_per_group);
157 inodes_per_block = (current_fs->blocksize
158 / sizeof(struct ext2_inode));
160 inode_block_to_dump =
161 current_fs->group_desc[inode_group].bg_inode_table +
162 (group_offset / inodes_per_block);
163 inode_offset_to_dump = ((group_offset % inodes_per_block)
164 * sizeof(struct ext2_inode));
165 printf("Inode %u is at group %u, block %u, offset %u\n",
166 inode_to_dump, inode_group,
167 inode_block_to_dump, inode_offset_to_dump);
170 if (optind == argc) {
173 out_fn = argv[optind];
174 out_file = fopen(out_fn, "w");
176 com_err(argv[0], errno, "while opening %s for logdump",
182 if (block_to_dump != -1 && current_fs != NULL) {
183 group_to_dump = ((block_to_dump -
184 current_fs->super->s_first_data_block)
185 / current_fs->super->s_blocks_per_group);
186 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
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;
204 /* Set up to read journal from the open filesystem */
205 if (check_fs_open(argv[0]))
207 journal_inum = current_fs->super->s_journal_inum;
209 com_err(argv[0], 0, "filesystem has no journal");
213 retval = ext2fs_read_inode(current_fs, journal_inum,
216 com_err(argv[0], retval,
217 "while reading inode %u", journal_inum);
221 retval = ext2fs_file_open(current_fs, journal_inum,
224 com_err(argv[0], retval, "while opening ext2 file");
228 journal_source.where = JOURNAL_IS_INTERNAL;
229 journal_source.file = journal_file;
232 dump_journal(argv[0], out_file, &journal_source);
234 if (journal_source.where == JOURNAL_IS_INTERNAL)
235 ext2fs_file_close(journal_file);
239 if (out_file != stdout)
246 int read_journal_block(char *cmd, struct journal_source *source, off_t offset,
247 char *buf, int size, int *got)
251 if (source->where == JOURNAL_IS_EXTERNAL) {
252 if (lseek(source->fd, offset, SEEK_SET) < 0) {
254 com_err(cmd, retval, "while seeking in reading journal");
257 retval = read(source->fd, buf, size);
264 retval = ext2fs_file_lseek(source->file, offset,
265 EXT2_SEEK_SET, NULL);
267 com_err(cmd, retval, "while seeking in reading journal");
271 retval = ext2fs_file_read(source->file, buf, size, got);
275 com_err(cmd, retval, "while while reading journal");
276 else if (*got != size) {
277 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
284 static char *type_to_name(int btype)
287 case JFS_DESCRIPTOR_BLOCK:
288 return "descriptor block";
289 case JFS_COMMIT_BLOCK:
290 return "commit block";
291 case JFS_SUPERBLOCK_V1:
292 return "V1 superblock";
293 case JFS_SUPERBLOCK_V2:
294 return "V2 superblock";
295 case JFS_REVOKE_BLOCK:
296 return "revoke table";
299 return "unrecognised type";
303 static void dump_journal(char *cmdname, FILE *out_file,
304 struct journal_source *source)
306 char jsb_buffer[1024];
308 journal_superblock_t *jsb;
312 __u32 magic, sequence, blocktype;
313 journal_header_t *header;
316 unsigned int blocknr;
318 /* First: locate the journal superblock */
320 retval = read_journal_block(cmdname, source, 0,
321 jsb_buffer, 1024, &got);
325 jsb = (journal_superblock_t *) jsb_buffer;
326 blocksize = be32_to_cpu(jsb->s_blocksize);
327 transaction = be32_to_cpu(jsb->s_sequence);
328 blocknr = be32_to_cpu(jsb->s_start);
330 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
331 blocknr, transaction);
334 /* Empty journal, nothing to do. */
338 retval = read_journal_block(cmdname, source,
339 blocknr*blocksize, buf,
341 if (retval || got != blocksize)
344 header = (journal_header_t *) buf;
346 magic = be32_to_cpu(header->h_magic);
347 sequence = be32_to_cpu(header->h_sequence);
348 blocktype = be32_to_cpu(header->h_blocktype);
350 if (magic != JFS_MAGIC_NUMBER) {
351 fprintf (out_file, "No magic number at block %u: "
352 "end of journal.\n", blocknr);
356 if (sequence != transaction) {
357 fprintf (out_file, "Found sequence %u (not %u) at "
358 "block %u: end of journal.\n",
359 sequence, transaction, blocknr);
363 if (dump_descriptors) {
364 fprintf (out_file, "Found expected sequence %u, "
365 "type %u (%s) at block %u\n",
367 type_to_name(blocktype), blocknr);
371 case JFS_DESCRIPTOR_BLOCK:
372 dump_descriptor_block(out_file, source, buf, jsb,
377 case JFS_COMMIT_BLOCK:
383 case JFS_REVOKE_BLOCK:
384 dump_revoke_block(out_file, buf, jsb,
392 fprintf (out_file, "Unexpected block type %u at "
393 "block %u.\n", blocktype, blocknr);
400 static void dump_descriptor_block(FILE *out_file,
401 struct journal_source *source,
403 journal_superblock_t *jsb,
404 unsigned int *blockp, int blocksize,
409 journal_block_tag_t *tag;
410 unsigned int blocknr;
415 offset = sizeof(journal_header_t);
419 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
420 "block %u:\n", transaction, blocknr);
426 /* Work out the location of the current tag, and skip to
429 tag = (journal_block_tag_t *) tagp;
430 offset += sizeof(journal_block_tag_t);
432 /* ... and if we have gone too far, then we've reached the
433 end of this block. */
434 if (offset > blocksize)
437 tag_block = be32_to_cpu(tag->t_blocknr);
438 tag_flags = be32_to_cpu(tag->t_flags);
440 if (!(tag_flags & JFS_FLAG_SAME_UUID))
443 dump_metadata_block(out_file, source, jsb,
444 blocknr, tag_block, blocksize,
450 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
456 static void dump_revoke_block(FILE *out_file, char *buf,
457 journal_superblock_t *jsb,
458 unsigned int blocknr, int blocksize,
462 journal_revoke_header_t *header;
463 unsigned int *entry, rblock;
466 fprintf(out_file, "Dumping revoke block, sequence %u, at "
467 "block %u:\n", transaction, blocknr);
469 header = (journal_revoke_header_t *) buf;
470 offset = sizeof(journal_revoke_header_t);
471 max = be32_to_cpu(header->r_count);
473 while (offset < max) {
474 entry = (unsigned int *) (buf + offset);
475 rblock = be32_to_cpu(*entry);
476 if (dump_all || rblock == block_to_dump) {
477 fprintf(out_file, " Revoke FS block %u", rblock);
479 fprintf(out_file, "\n");
481 fprintf(out_file," at block %u, sequence %u\n",
482 blocknr, transaction);
489 static void show_extent(FILE *out_file, int start_extent, int end_extent,
492 if (start_extent >= 0 && first_block != 0)
493 fprintf(out_file, "(%d+%u): %u ",
494 start_extent, end_extent-start_extent, first_block);
497 static void show_indirect(FILE *out_file, char *name, __u32 where)
500 fprintf(out_file, "(%s): %u ", name, where);
504 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
505 journal_superblock_t *jsb,
506 unsigned int log_blocknr,
507 unsigned int fs_blocknr,
515 || (fs_blocknr == block_to_dump)
516 || (fs_blocknr == inode_block_to_dump)
517 || (fs_blocknr == bitmap_to_dump)))
520 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
522 fprintf(out_file, "sequence %u, ", transaction);
523 fprintf(out_file, "journal block %u\n", log_blocknr);
525 /* There are two major special cases to parse:
527 * If this block is a block
528 * bitmap block, we need to give it special treatment so that we
529 * can log any allocates and deallocates which affect the
530 * block_to_dump query block.
532 * If the block is an inode block for the inode being searched
533 * for, then we need to dump the contents of that inode
534 * structure symbolically.
537 if (!(dump_contents && dump_all)
538 && fs_blocknr != block_to_dump
539 && fs_blocknr != bitmap_to_dump
540 && fs_blocknr != inode_block_to_dump)
543 retval = read_journal_block("logdump", source,
544 blocksize * log_blocknr,
545 buf, blocksize, &got);
549 if (fs_blocknr == bitmap_to_dump) {
550 struct ext2_super_block *super;
553 super = current_fs->super;
554 offset = ((fs_blocknr - super->s_first_data_block) %
555 super->s_blocks_per_group);
557 fprintf(out_file, " (block bitmap for block %u: "
560 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
563 if (fs_blocknr == inode_block_to_dump) {
564 struct ext2_inode *inode;
565 int first, prev, this, start_extent, i;
567 fprintf(out_file, " (inode block for inode %u):\n",
570 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
571 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
573 /* Dump out the direct/indirect blocks here:
574 * internal_dump_inode can only dump them from the main
575 * on-disk inode, not from the journaled copy of the
578 fprintf (out_file, " Blocks: ");
581 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
582 this = inode->i_block[i];
583 if (start_extent >= 0 && this == prev+1) {
587 show_extent(out_file, start_extent, i, first);
592 show_extent(out_file, start_extent, i, first);
593 show_indirect(out_file, "IND", inode->i_block[i++]);
594 show_indirect(out_file, "DIND", inode->i_block[i++]);
595 show_indirect(out_file, "TIND", inode->i_block[i++]);
597 fprintf(out_file, "\n");
601 do_hexdump(out_file, buf, blocksize);
605 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
613 charp = (char *) buf;
615 for (i=0; i<blocksize; i+=16) {
616 fprintf(out_file, " %04x: ", i);
617 for (j=0; j<16; j+=4)
618 fprintf(out_file, "%08x ", *intp++);
619 for (j=0; j<16; j++) {
621 if (c < ' ' || c >= 127)
623 fprintf(out_file, "%c", c);
625 fprintf(out_file, "\n");