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
22 #include <sys/types.h>
34 #include "blkid/blkid.h"
36 #include <uuid/uuid.h>
38 enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
40 #define ANY_BLOCK ((blk64_t) -1)
42 static int dump_all, dump_super, dump_old, dump_contents, dump_descriptors;
43 static blk64_t block_to_dump, bitmap_to_dump, inode_block_to_dump;
44 static unsigned int group_to_dump, inode_offset_to_dump;
45 static ext2_ino_t inode_to_dump;
49 enum journal_location where;
54 static void dump_journal(char *, FILE *, struct journal_source *);
56 static void dump_descriptor_block(FILE *, struct journal_source *,
57 char *, journal_superblock_t *,
58 unsigned int *, int, tid_t);
60 static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
61 unsigned int, int, tid_t);
63 static void dump_metadata_block(FILE *, struct journal_source *,
64 journal_superblock_t*,
65 unsigned int, unsigned int, unsigned int,
68 static void do_hexdump (FILE *, char *, int);
70 #define WRAP(jsb, blocknr) \
71 if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \
72 blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \
73 be32_to_cpu((jsb)->s_first));
75 void do_logdump(int argc, char **argv)
82 char *inode_spec = NULL;
83 char *journal_fn = NULL;
86 ext2_ino_t journal_inum;
87 struct ext2_inode journal_inode;
88 ext2_file_t journal_file;
90 struct journal_source journal_source;
91 struct ext2_super_block *es = NULL;
93 journal_source.where = JOURNAL_IS_INTERNAL;
94 journal_source.fd = 0;
95 journal_source.file = 0;
100 dump_descriptors = 1;
101 block_to_dump = ANY_BLOCK;
103 inode_block_to_dump = ANY_BLOCK;
107 while ((c = getopt (argc, argv, "ab:ci:f:OsS")) != EOF) {
113 block_to_dump = strtoul(optarg, &tmp, 0);
116 "Bad block number - %s", optarg);
119 dump_descriptors = 0;
129 dump_descriptors = 0;
144 if (optind != argc && optind != argc-1) {
149 es = current_fs->super;
152 int inode_group, group_offset, inodes_per_block;
154 if (check_fs_open(argv[0]))
157 inode_to_dump = string_to_inode(inode_spec);
161 inode_group = ((inode_to_dump - 1)
162 / es->s_inodes_per_group);
163 group_offset = ((inode_to_dump - 1)
164 % es->s_inodes_per_group);
165 inodes_per_block = (current_fs->blocksize
166 / sizeof(struct ext2_inode));
168 inode_block_to_dump =
169 ext2fs_inode_table_loc(current_fs, inode_group) +
170 (group_offset / inodes_per_block);
171 inode_offset_to_dump = ((group_offset % inodes_per_block)
172 * sizeof(struct ext2_inode));
173 printf("Inode %u is at group %u, block %llu, offset %u\n",
174 inode_to_dump, inode_group,
175 inode_block_to_dump, inode_offset_to_dump);
178 if (optind == argc) {
181 out_fn = argv[optind];
182 out_file = fopen(out_fn, "w");
184 com_err(argv[0], errno, "while opening %s for logdump",
190 if (block_to_dump != ANY_BLOCK && current_fs != NULL) {
191 group_to_dump = ((block_to_dump -
192 es->s_first_data_block)
193 / es->s_blocks_per_group);
194 bitmap_to_dump = ext2fs_block_bitmap_loc(current_fs, group_to_dump);
197 if (!journal_fn && check_fs_open(argv[0]))
201 /* Set up to read journal from a regular file somewhere */
202 journal_fd = open(journal_fn, O_RDONLY, 0);
203 if (journal_fd < 0) {
204 com_err(argv[0], errno, "while opening %s for logdump",
209 journal_source.where = JOURNAL_IS_EXTERNAL;
210 journal_source.fd = journal_fd;
211 } else if ((journal_inum = es->s_journal_inum)) {
213 if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
215 "no journal backup in super block\n");
218 memset(&journal_inode, 0, sizeof(struct ext2_inode));
219 memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
221 journal_inode.i_size_high = es->s_jnl_blocks[15];
222 journal_inode.i_size = es->s_jnl_blocks[16];
223 journal_inode.i_links_count = 1;
224 journal_inode.i_mode = LINUX_S_IFREG | 0600;
226 if (debugfs_read_inode(journal_inum, &journal_inode,
231 retval = ext2fs_file_open2(current_fs, journal_inum,
232 &journal_inode, 0, &journal_file);
234 com_err(argv[0], retval, "while opening ext2 file");
237 journal_source.where = JOURNAL_IS_INTERNAL;
238 journal_source.file = journal_file;
242 uuid_unparse(es->s_journal_uuid, uuid);
243 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
245 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
247 com_err(argv[0], 0, "filesystem has no journal");
250 journal_fd = open(journal_fn, O_RDONLY, 0);
251 if (journal_fd < 0) {
252 com_err(argv[0], errno, "while opening %s for logdump",
257 fprintf(out_file, "Using external journal found at %s\n",
260 journal_source.where = JOURNAL_IS_EXTERNAL;
261 journal_source.fd = journal_fd;
264 dump_journal(argv[0], out_file, &journal_source);
266 if (journal_source.where == JOURNAL_IS_INTERNAL)
267 ext2fs_file_close(journal_file);
272 if (out_file && (out_file != stdout))
278 fprintf(stderr, "%s: Usage: logdump [-acsO] [-b<block>] [-i<filespec>]\n\t"
279 "[-f<journal_file>] [output_file]\n", argv[0]);
283 static int read_journal_block(const char *cmd, struct journal_source *source,
284 ext2_loff_t offset, char *buf, unsigned int size)
289 if (source->where == JOURNAL_IS_EXTERNAL) {
290 if (lseek(source->fd, offset, SEEK_SET) < 0) {
294 retval = read(source->fd, buf, size);
302 retval = ext2fs_file_llseek(source->file, offset,
303 EXT2_SEEK_SET, NULL);
306 com_err(cmd, retval, "while seeking in reading journal");
309 retval = ext2fs_file_read(source->file, buf, size, &got);
312 com_err(cmd, retval, "while reading journal");
317 com_err(cmd, 0, "short read (read %u, expected %u) "
318 "while reading journal", got, size);
324 static const char *type_to_name(int btype)
327 case JFS_DESCRIPTOR_BLOCK:
328 return "descriptor block";
329 case JFS_COMMIT_BLOCK:
330 return "commit block";
331 case JFS_SUPERBLOCK_V1:
332 return "V1 superblock";
333 case JFS_SUPERBLOCK_V2:
334 return "V2 superblock";
335 case JFS_REVOKE_BLOCK:
336 return "revoke table";
338 return "unrecognised type";
342 static void dump_journal(char *cmdname, FILE *out_file,
343 struct journal_source *source)
345 struct ext2_super_block *sb;
346 char jsb_buffer[1024];
348 journal_superblock_t *jsb;
349 unsigned int blocksize = 1024;
351 __u32 magic, sequence, blocktype;
352 journal_header_t *header;
354 unsigned int blocknr = 0;
356 /* First, check to see if there's an ext2 superblock header */
357 retval = read_journal_block(cmdname, source, 0, buf, 2048);
361 jsb = (journal_superblock_t *) buf;
362 sb = (struct ext2_super_block *) (buf+1024);
363 #ifdef WORDS_BIGENDIAN
364 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
365 ext2fs_swap_super(sb);
368 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
369 (sb->s_magic == EXT2_SUPER_MAGIC) &&
370 ext2fs_has_feature_journal_dev(sb)) {
371 blocksize = EXT2_BLOCK_SIZE(sb);
372 blocknr = (blocksize == 1024) ? 2 : 1;
373 uuid_unparse(sb->s_uuid, jsb_buffer);
374 fprintf(out_file, "Ext2 superblock header found.\n");
376 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
377 fprintf(out_file, "\tblocksize=%d\n", blocksize);
378 fprintf(out_file, "\tjournal data size %lu\n",
379 (unsigned long) ext2fs_blocks_count(sb));
383 /* Next, read the journal superblock */
384 retval = read_journal_block(cmdname, source,
385 ((ext2_loff_t) blocknr) * blocksize,
391 e2p_list_journal_super(out_file, jsb_buffer,
392 current_fs->blocksize, 0);
393 fputc('\n', out_file);
396 jsb = (journal_superblock_t *) jsb_buffer;
397 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
399 "Journal superblock magic number invalid!\n");
402 blocksize = be32_to_cpu(jsb->s_blocksize);
403 transaction = be32_to_cpu(jsb->s_sequence);
404 blocknr = be32_to_cpu(jsb->s_start);
406 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
407 blocknr, transaction);
410 /* Empty journal, nothing to do. */
418 retval = read_journal_block(cmdname, source,
419 ((ext2_loff_t) blocknr) * blocksize,
424 header = (journal_header_t *) buf;
426 magic = be32_to_cpu(header->h_magic);
427 sequence = be32_to_cpu(header->h_sequence);
428 blocktype = be32_to_cpu(header->h_blocktype);
430 if (magic != JFS_MAGIC_NUMBER) {
431 fprintf (out_file, "No magic number at block %u: "
432 "end of journal.\n", blocknr);
436 if (sequence != transaction) {
437 fprintf (out_file, "Found sequence %u (not %u) at "
438 "block %u: end of journal.\n",
439 sequence, transaction, blocknr);
444 if (dump_descriptors) {
445 fprintf (out_file, "Found expected sequence %u, "
446 "type %u (%s) at block %u\n",
448 type_to_name(blocktype), blocknr);
452 case JFS_DESCRIPTOR_BLOCK:
453 dump_descriptor_block(out_file, source, buf, jsb,
458 case JFS_COMMIT_BLOCK:
464 case JFS_REVOKE_BLOCK:
465 dump_revoke_block(out_file, buf, jsb,
473 fprintf (out_file, "Unexpected block type %u at "
474 "block %u.\n", blocktype, blocknr);
480 static inline size_t journal_super_tag_bytes(journal_superblock_t *jsb)
484 if (JSB_HAS_INCOMPAT_FEATURE(jsb, JFS_FEATURE_INCOMPAT_CSUM_V3))
485 return sizeof(journal_block_tag3_t);
487 sz = sizeof(journal_block_tag_t);
489 if (JSB_HAS_INCOMPAT_FEATURE(jsb, JFS_FEATURE_INCOMPAT_CSUM_V2))
492 if (JSB_HAS_INCOMPAT_FEATURE(jsb, JFS_FEATURE_INCOMPAT_64BIT))
495 return sz - sizeof(__u32);
498 static void dump_descriptor_block(FILE *out_file,
499 struct journal_source *source,
501 journal_superblock_t *jsb,
502 unsigned int *blockp, int blocksize,
505 int offset, tag_size, csum_size = 0;
507 journal_block_tag_t *tag;
508 unsigned int blocknr;
512 tag_size = journal_super_tag_bytes(jsb);
513 offset = sizeof(journal_header_t);
516 if (JSB_HAS_INCOMPAT_FEATURE(jsb, JFS_FEATURE_INCOMPAT_CSUM_V3) ||
517 JSB_HAS_INCOMPAT_FEATURE(jsb, JFS_FEATURE_INCOMPAT_CSUM_V2))
518 csum_size = sizeof(struct journal_block_tail);
521 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
522 "block %u:\n", transaction, blocknr);
528 /* Work out the location of the current tag, and skip to
531 tag = (journal_block_tag_t *) tagp;
534 /* ... and if we have gone too far, then we've reached the
535 end of this block. */
536 if (offset > blocksize - csum_size)
539 tag_block = be32_to_cpu(tag->t_blocknr);
540 tag_flags = be16_to_cpu(tag->t_flags);
542 if (!(tag_flags & JFS_FLAG_SAME_UUID))
545 dump_metadata_block(out_file, source, jsb,
546 blocknr, tag_block, tag_flags, blocksize,
552 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
558 static void dump_revoke_block(FILE *out_file, char *buf,
559 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
560 unsigned int blocknr,
561 int blocksize EXT2FS_ATTR((unused)),
565 journal_revoke_header_t *header;
566 unsigned long long rblock;
567 int tag_size = sizeof(__u32);
570 fprintf(out_file, "Dumping revoke block, sequence %u, at "
571 "block %u:\n", transaction, blocknr);
573 if (be32_to_cpu(jsb->s_feature_incompat) & JFS_FEATURE_INCOMPAT_64BIT)
574 tag_size = sizeof(__u64);
576 header = (journal_revoke_header_t *) buf;
577 offset = sizeof(journal_revoke_header_t);
578 max = be32_to_cpu(header->r_count);
580 while (offset < max) {
581 if (tag_size == sizeof(__u32)) {
582 __u32 *entry = (__u32 *) (buf + offset);
583 rblock = be32_to_cpu(*entry);
585 __u64 *entry = (__u64 *) (buf + offset);
586 rblock = ext2fs_be64_to_cpu(*entry);
588 if (dump_all || rblock == block_to_dump) {
589 fprintf(out_file, " Revoke FS block %llu", rblock);
591 fprintf(out_file, "\n");
593 fprintf(out_file," at block %u, sequence %u\n",
594 blocknr, transaction);
601 static void show_extent(FILE *out_file, int start_extent, int end_extent,
604 if (start_extent >= 0 && first_block != 0)
605 fprintf(out_file, "(%d+%u): %u ",
606 start_extent, end_extent-start_extent, first_block);
609 static void show_indirect(FILE *out_file, const char *name, __u32 where)
612 fprintf(out_file, "(%s): %u ", name, where);
616 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
617 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
618 unsigned int log_blocknr,
619 unsigned int fs_blocknr,
620 unsigned int log_tag_flags,
628 || (fs_blocknr == block_to_dump)
629 || (fs_blocknr == inode_block_to_dump)
630 || (fs_blocknr == bitmap_to_dump)))
633 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
635 fprintf(out_file, "sequence %u, ", transaction);
636 fprintf(out_file, "journal block %u (flags 0x%x)\n", log_blocknr,
639 /* There are two major special cases to parse:
641 * If this block is a block
642 * bitmap block, we need to give it special treatment so that we
643 * can log any allocates and deallocates which affect the
644 * block_to_dump query block.
646 * If the block is an inode block for the inode being searched
647 * for, then we need to dump the contents of that inode
648 * structure symbolically.
651 if (!(dump_contents && dump_all)
652 && fs_blocknr != block_to_dump
653 && fs_blocknr != bitmap_to_dump
654 && fs_blocknr != inode_block_to_dump)
657 retval = read_journal_block("logdump", source,
658 ((ext2_loff_t) log_blocknr) * blocksize,
663 if (fs_blocknr == bitmap_to_dump) {
664 struct ext2_super_block *super;
667 super = current_fs->super;
668 offset = ((block_to_dump - super->s_first_data_block) %
669 super->s_blocks_per_group);
671 fprintf(out_file, " (block bitmap for block %llu: "
674 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
677 if (fs_blocknr == inode_block_to_dump) {
678 struct ext2_inode *inode;
679 int first, prev, this, start_extent, i;
681 fprintf(out_file, " (inode block for inode %u):\n",
684 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
685 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
687 /* Dump out the direct/indirect blocks here:
688 * internal_dump_inode can only dump them from the main
689 * on-disk inode, not from the journaled copy of the
692 fprintf (out_file, " Blocks: ");
693 first = prev = start_extent = -1;
695 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
696 this = inode->i_block[i];
697 if (start_extent >= 0 && this == prev+1) {
701 show_extent(out_file, start_extent, i, first);
706 show_extent(out_file, start_extent, i, first);
707 show_indirect(out_file, "IND", inode->i_block[i++]);
708 show_indirect(out_file, "DIND", inode->i_block[i++]);
709 show_indirect(out_file, "TIND", inode->i_block[i++]);
711 fprintf(out_file, "\n");
715 do_hexdump(out_file, buf, blocksize);
719 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
727 charp = (char *) buf;
729 for (i=0; i<blocksize; i+=16) {
730 fprintf(out_file, " %04x: ", i);
731 for (j=0; j<16; j+=4)
732 fprintf(out_file, "%08x ", *intp++);
733 for (j=0; j<16; j++) {
735 if (c < ' ' || c >= 127)
737 fprintf(out_file, "%c", c);
739 fprintf(out_file, "\n");