1 /*
2 * logdump.c --- dump the contents of the journal out to a file
3 *
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.
7 *
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 */
11
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <time.h>
18 #ifdef HAVE_ERRNO_H
19 #include <errno.h>
20 #endif
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <utime.h>
25 #ifdef HAVE_GETOPT_H
26 #include <getopt.h>
27 #else
28 extern int optind;
29 extern char *optarg;
30 #endif
31
32 #include "debugfs.h"
33 #include "blkid/blkid.h"
34 #include "jfs_user.h"
35 #include <uuid/uuid.h>
36
37 enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
38
39 #define ANY_BLOCK ((blk64_t) -1)
40
41 static int dump_all, dump_contents, dump_descriptors;
42 static blk64_t block_to_dump, bitmap_to_dump, inode_block_to_dump;
43 static unsigned int group_to_dump, inode_offset_to_dump;
44 static ext2_ino_t inode_to_dump;
45
46 struct journal_source
47 {
48 enum journal_location where;
49 int fd;
50 ext2_file_t file;
51 };
52
53 static void dump_journal(char *, FILE *, struct journal_source *);
54
55 static void dump_descriptor_block(FILE *, struct journal_source *,
56 char *, journal_superblock_t *,
57 unsigned int *, int, tid_t);
58
59 static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
60 unsigned int, int, tid_t);
61
62 static void dump_metadata_block(FILE *, struct journal_source *,
63 journal_superblock_t*,
64 unsigned int, unsigned int, unsigned int,
65 int, tid_t);
66
67 static void do_hexdump (FILE *, char *, int);
68
69 #define WRAP(jsb, blocknr) \
70 if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \
71 blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \
72 be32_to_cpu((jsb)->s_first));
73
do_logdump(int argc,char ** argv)74 void do_logdump(int argc, char **argv)
75 {
76 int c;
77 int retval;
78 char *out_fn;
79 FILE *out_file;
80
81 char *inode_spec = NULL;
82 char *journal_fn = NULL;
83 int journal_fd = 0;
84 int use_sb = 0;
85 ext2_ino_t journal_inum;
86 struct ext2_inode journal_inode;
87 ext2_file_t journal_file;
88 char *tmp;
89 struct journal_source journal_source;
90 struct ext2_super_block *es = NULL;
91
92 journal_source.where = JOURNAL_IS_INTERNAL;
93 journal_source.fd = 0;
94 journal_source.file = 0;
95 dump_all = 0;
96 dump_contents = 0;
97 dump_descriptors = 1;
98 block_to_dump = ANY_BLOCK;
99 bitmap_to_dump = -1;
100 inode_block_to_dump = ANY_BLOCK;
101 inode_to_dump = -1;
102
103 reset_getopt();
104 while ((c = getopt (argc, argv, "ab:ci:f:s")) != EOF) {
105 switch (c) {
106 case 'a':
107 dump_all++;
108 break;
109 case 'b':
110 block_to_dump = strtoul(optarg, &tmp, 0);
111 if (*tmp) {
112 com_err(argv[0], 0,
113 "Bad block number - %s", optarg);
114 return;
115 }
116 dump_descriptors = 0;
117 break;
118 case 'c':
119 dump_contents++;
120 break;
121 case 'f':
122 journal_fn = optarg;
123 break;
124 case 'i':
125 inode_spec = optarg;
126 dump_descriptors = 0;
127 break;
128 case 's':
129 use_sb++;
130 break;
131 default:
132 goto print_usage;
133 }
134 }
135 if (optind != argc && optind != argc-1) {
136 goto print_usage;
137 }
138
139 if (current_fs)
140 es = current_fs->super;
141
142 if (inode_spec) {
143 int inode_group, group_offset, inodes_per_block;
144
145 if (check_fs_open(argv[0]))
146 return;
147
148 inode_to_dump = string_to_inode(inode_spec);
149 if (!inode_to_dump)
150 return;
151
152 inode_group = ((inode_to_dump - 1)
153 / es->s_inodes_per_group);
154 group_offset = ((inode_to_dump - 1)
155 % es->s_inodes_per_group);
156 inodes_per_block = (current_fs->blocksize
157 / sizeof(struct ext2_inode));
158
159 inode_block_to_dump =
160 ext2fs_inode_table_loc(current_fs, inode_group) +
161 (group_offset / inodes_per_block);
162 inode_offset_to_dump = ((group_offset % inodes_per_block)
163 * sizeof(struct ext2_inode));
164 printf("Inode %u is at group %u, block %llu, offset %u\n",
165 inode_to_dump, inode_group,
166 inode_block_to_dump, inode_offset_to_dump);
167 }
168
169 if (optind == argc) {
170 out_file = stdout;
171 } else {
172 out_fn = argv[optind];
173 out_file = fopen(out_fn, "w");
174 if (!out_file) {
175 com_err(argv[0], errno, "while opening %s for logdump",
176 out_fn);
177 goto errout;
178 }
179 }
180
181 if (block_to_dump != ANY_BLOCK && current_fs != NULL) {
182 group_to_dump = ((block_to_dump -
183 es->s_first_data_block)
184 / es->s_blocks_per_group);
185 bitmap_to_dump = ext2fs_block_bitmap_loc(current_fs, group_to_dump);
186 }
187
188 if (!journal_fn && check_fs_open(argv[0]))
189 goto errout;
190
191 if (journal_fn) {
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",
196 journal_fn);
197 goto errout;
198 }
199
200 journal_source.where = JOURNAL_IS_EXTERNAL;
201 journal_source.fd = journal_fd;
202 } else if ((journal_inum = es->s_journal_inum)) {
203 if (use_sb) {
204 if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
205 com_err(argv[0], 0,
206 "no journal backup in super block\n");
207 goto errout;
208 }
209 memset(&journal_inode, 0, sizeof(struct ext2_inode));
210 memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
211 EXT2_N_BLOCKS*4);
212 journal_inode.i_size_high = es->s_jnl_blocks[15];
213 journal_inode.i_size = es->s_jnl_blocks[16];
214 journal_inode.i_links_count = 1;
215 journal_inode.i_mode = LINUX_S_IFREG | 0600;
216 } else {
217 if (debugfs_read_inode(journal_inum, &journal_inode,
218 argv[0]))
219 goto errout;
220 }
221
222 retval = ext2fs_file_open2(current_fs, journal_inum,
223 &journal_inode, 0, &journal_file);
224 if (retval) {
225 com_err(argv[0], retval, "while opening ext2 file");
226 goto errout;
227 }
228 journal_source.where = JOURNAL_IS_INTERNAL;
229 journal_source.file = journal_file;
230 } else {
231 char uuid[37];
232
233 uuid_unparse(es->s_journal_uuid, uuid);
234 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
235 if (!journal_fn)
236 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
237 if (!journal_fn) {
238 com_err(argv[0], 0, "filesystem has no journal");
239 goto errout;
240 }
241 journal_fd = open(journal_fn, O_RDONLY, 0);
242 if (journal_fd < 0) {
243 com_err(argv[0], errno, "while opening %s for logdump",
244 journal_fn);
245 free(journal_fn);
246 goto errout;
247 }
248 fprintf(out_file, "Using external journal found at %s\n",
249 journal_fn);
250 free(journal_fn);
251 journal_source.where = JOURNAL_IS_EXTERNAL;
252 journal_source.fd = journal_fd;
253 }
254
255 dump_journal(argv[0], out_file, &journal_source);
256
257 if (journal_source.where == JOURNAL_IS_INTERNAL)
258 ext2fs_file_close(journal_file);
259 else
260 close(journal_fd);
261
262 errout:
263 if (out_file && (out_file != stdout))
264 fclose(out_file);
265
266 return;
267
268 print_usage:
269 fprintf(stderr, "%s: Usage: logdump [-acs] [-b<block>] [-i<filespec>]\n\t"
270 "[-f<journal_file>] [output_file]\n", argv[0]);
271 }
272
273
read_journal_block(const char * cmd,struct journal_source * source,off_t offset,char * buf,int size,unsigned int * got)274 static int read_journal_block(const char *cmd, struct journal_source *source,
275 off_t offset, char *buf, int size,
276 unsigned int *got)
277 {
278 int retval;
279
280 if (source->where == JOURNAL_IS_EXTERNAL) {
281 if (lseek(source->fd, offset, SEEK_SET) < 0) {
282 retval = errno;
283 com_err(cmd, retval, "while seeking in reading journal");
284 return retval;
285 }
286 retval = read(source->fd, buf, size);
287 if (retval >= 0) {
288 *got = retval;
289 retval = 0;
290 } else
291 retval = errno;
292 } else {
293 retval = ext2fs_file_lseek(source->file, offset,
294 EXT2_SEEK_SET, NULL);
295 if (retval) {
296 com_err(cmd, retval, "while seeking in reading journal");
297 return retval;
298 }
299
300 retval = ext2fs_file_read(source->file, buf, size, got);
301 }
302
303 if (retval)
304 com_err(cmd, retval, "while reading journal");
305 else if (*got != (unsigned int) size) {
306 com_err(cmd, 0, "short read (read %d, expected %d) "
307 "while reading journal", *got, size);
308 retval = -1;
309 }
310
311 return retval;
312 }
313
type_to_name(int btype)314 static const char *type_to_name(int btype)
315 {
316 switch (btype) {
317 case JFS_DESCRIPTOR_BLOCK:
318 return "descriptor block";
319 case JFS_COMMIT_BLOCK:
320 return "commit block";
321 case JFS_SUPERBLOCK_V1:
322 return "V1 superblock";
323 case JFS_SUPERBLOCK_V2:
324 return "V2 superblock";
325 case JFS_REVOKE_BLOCK:
326 return "revoke table";
327 }
328 return "unrecognised type";
329 }
330
331
dump_journal(char * cmdname,FILE * out_file,struct journal_source * source)332 static void dump_journal(char *cmdname, FILE *out_file,
333 struct journal_source *source)
334 {
335 struct ext2_super_block *sb;
336 char jsb_buffer[1024];
337 char buf[8192];
338 journal_superblock_t *jsb;
339 unsigned int blocksize = 1024;
340 unsigned int got;
341 int retval;
342 __u32 magic, sequence, blocktype;
343 journal_header_t *header;
344
345 tid_t transaction;
346 unsigned int blocknr = 0;
347
348 /* First, check to see if there's an ext2 superblock header */
349 retval = read_journal_block(cmdname, source, 0,
350 buf, 2048, &got);
351 if (retval)
352 return;
353
354 jsb = (journal_superblock_t *) buf;
355 sb = (struct ext2_super_block *) (buf+1024);
356 #ifdef WORDS_BIGENDIAN
357 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
358 ext2fs_swap_super(sb);
359 #endif
360
361 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
362 (sb->s_magic == EXT2_SUPER_MAGIC) &&
363 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
364 blocksize = EXT2_BLOCK_SIZE(sb);
365 blocknr = (blocksize == 1024) ? 2 : 1;
366 uuid_unparse(sb->s_uuid, jsb_buffer);
367 fprintf(out_file, "Ext2 superblock header found.\n");
368 if (dump_all) {
369 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
370 fprintf(out_file, "\tblocksize=%d\n", blocksize);
371 fprintf(out_file, "\tjournal data size %lu\n",
372 (unsigned long) ext2fs_blocks_count(sb));
373 }
374 }
375
376 /* Next, read the journal superblock */
377
378 retval = read_journal_block(cmdname, source, blocknr*blocksize,
379 jsb_buffer, 1024, &got);
380 if (retval)
381 return;
382
383 jsb = (journal_superblock_t *) jsb_buffer;
384 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
385 fprintf(out_file,
386 "Journal superblock magic number invalid!\n");
387 return;
388 }
389 blocksize = be32_to_cpu(jsb->s_blocksize);
390 transaction = be32_to_cpu(jsb->s_sequence);
391 blocknr = be32_to_cpu(jsb->s_start);
392
393 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
394 blocknr, transaction);
395
396 if (!blocknr)
397 /* Empty journal, nothing to do. */
398 return;
399
400 while (1) {
401 retval = read_journal_block(cmdname, source,
402 blocknr*blocksize, buf,
403 blocksize, &got);
404 if (retval || got != blocksize)
405 return;
406
407 header = (journal_header_t *) buf;
408
409 magic = be32_to_cpu(header->h_magic);
410 sequence = be32_to_cpu(header->h_sequence);
411 blocktype = be32_to_cpu(header->h_blocktype);
412
413 if (magic != JFS_MAGIC_NUMBER) {
414 fprintf (out_file, "No magic number at block %u: "
415 "end of journal.\n", blocknr);
416 return;
417 }
418
419 if (sequence != transaction) {
420 fprintf (out_file, "Found sequence %u (not %u) at "
421 "block %u: end of journal.\n",
422 sequence, transaction, blocknr);
423 return;
424 }
425
426 if (dump_descriptors) {
427 fprintf (out_file, "Found expected sequence %u, "
428 "type %u (%s) at block %u\n",
429 sequence, blocktype,
430 type_to_name(blocktype), blocknr);
431 }
432
433 switch (blocktype) {
434 case JFS_DESCRIPTOR_BLOCK:
435 dump_descriptor_block(out_file, source, buf, jsb,
436 &blocknr, blocksize,
437 transaction);
438 continue;
439
440 case JFS_COMMIT_BLOCK:
441 transaction++;
442 blocknr++;
443 WRAP(jsb, blocknr);
444 continue;
445
446 case JFS_REVOKE_BLOCK:
447 dump_revoke_block(out_file, buf, jsb,
448 blocknr, blocksize,
449 transaction);
450 blocknr++;
451 WRAP(jsb, blocknr);
452 continue;
453
454 default:
455 fprintf (out_file, "Unexpected block type %u at "
456 "block %u.\n", blocktype, blocknr);
457 return;
458 }
459 }
460 }
461
462
dump_descriptor_block(FILE * out_file,struct journal_source * source,char * buf,journal_superblock_t * jsb,unsigned int * blockp,int blocksize,tid_t transaction)463 static void dump_descriptor_block(FILE *out_file,
464 struct journal_source *source,
465 char *buf,
466 journal_superblock_t *jsb,
467 unsigned int *blockp, int blocksize,
468 tid_t transaction)
469 {
470 int offset, tag_size = JBD_TAG_SIZE32;
471 char *tagp;
472 journal_block_tag_t *tag;
473 unsigned int blocknr;
474 __u32 tag_block;
475 __u32 tag_flags;
476
477 if (be32_to_cpu(jsb->s_feature_incompat) & JFS_FEATURE_INCOMPAT_64BIT)
478 tag_size = JBD_TAG_SIZE64;
479
480 offset = sizeof(journal_header_t);
481 blocknr = *blockp;
482
483 if (dump_all)
484 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
485 "block %u:\n", transaction, blocknr);
486
487 ++blocknr;
488 WRAP(jsb, blocknr);
489
490 do {
491 /* Work out the location of the current tag, and skip to
492 * the next one... */
493 tagp = &buf[offset];
494 tag = (journal_block_tag_t *) tagp;
495 offset += tag_size;
496
497 /* ... and if we have gone too far, then we've reached the
498 end of this block. */
499 if (offset > blocksize)
500 break;
501
502 tag_block = be32_to_cpu(tag->t_blocknr);
503 tag_flags = be32_to_cpu(tag->t_flags);
504
505 if (!(tag_flags & JFS_FLAG_SAME_UUID))
506 offset += 16;
507
508 dump_metadata_block(out_file, source, jsb,
509 blocknr, tag_block, tag_flags, blocksize,
510 transaction);
511
512 ++blocknr;
513 WRAP(jsb, blocknr);
514
515 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
516
517 *blockp = blocknr;
518 }
519
520
dump_revoke_block(FILE * out_file,char * buf,journal_superblock_t * jsb EXT2FS_ATTR ((unused)),unsigned int blocknr,int blocksize EXT2FS_ATTR ((unused)),tid_t transaction)521 static void dump_revoke_block(FILE *out_file, char *buf,
522 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
523 unsigned int blocknr,
524 int blocksize EXT2FS_ATTR((unused)),
525 tid_t transaction)
526 {
527 int offset, max;
528 journal_revoke_header_t *header;
529 unsigned int *entry, rblock;
530
531 if (dump_all)
532 fprintf(out_file, "Dumping revoke block, sequence %u, at "
533 "block %u:\n", transaction, blocknr);
534
535 header = (journal_revoke_header_t *) buf;
536 offset = sizeof(journal_revoke_header_t);
537 max = be32_to_cpu(header->r_count);
538
539 while (offset < max) {
540 entry = (unsigned int *) (buf + offset);
541 rblock = be32_to_cpu(*entry);
542 if (dump_all || rblock == block_to_dump) {
543 fprintf(out_file, " Revoke FS block %u", rblock);
544 if (dump_all)
545 fprintf(out_file, "\n");
546 else
547 fprintf(out_file," at block %u, sequence %u\n",
548 blocknr, transaction);
549 }
550 offset += 4;
551 }
552 }
553
554
show_extent(FILE * out_file,int start_extent,int end_extent,__u32 first_block)555 static void show_extent(FILE *out_file, int start_extent, int end_extent,
556 __u32 first_block)
557 {
558 if (start_extent >= 0 && first_block != 0)
559 fprintf(out_file, "(%d+%u): %u ",
560 start_extent, end_extent-start_extent, first_block);
561 }
562
show_indirect(FILE * out_file,const char * name,__u32 where)563 static void show_indirect(FILE *out_file, const char *name, __u32 where)
564 {
565 if (where)
566 fprintf(out_file, "(%s): %u ", name, where);
567 }
568
569
dump_metadata_block(FILE * out_file,struct journal_source * source,journal_superblock_t * jsb EXT2FS_ATTR ((unused)),unsigned int log_blocknr,unsigned int fs_blocknr,unsigned int log_tag_flags,int blocksize,tid_t transaction)570 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
571 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
572 unsigned int log_blocknr,
573 unsigned int fs_blocknr,
574 unsigned int log_tag_flags,
575 int blocksize,
576 tid_t transaction)
577 {
578 unsigned int got;
579 int retval;
580 char buf[8192];
581
582 if (!(dump_all
583 || (fs_blocknr == block_to_dump)
584 || (fs_blocknr == inode_block_to_dump)
585 || (fs_blocknr == bitmap_to_dump)))
586 return;
587
588 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
589 if (!dump_all)
590 fprintf(out_file, "sequence %u, ", transaction);
591 fprintf(out_file, "journal block %u (flags 0x%x)\n", log_blocknr,
592 log_tag_flags);
593
594 /* There are two major special cases to parse:
595 *
596 * If this block is a block
597 * bitmap block, we need to give it special treatment so that we
598 * can log any allocates and deallocates which affect the
599 * block_to_dump query block.
600 *
601 * If the block is an inode block for the inode being searched
602 * for, then we need to dump the contents of that inode
603 * structure symbolically.
604 */
605
606 if (!(dump_contents && dump_all)
607 && fs_blocknr != block_to_dump
608 && fs_blocknr != bitmap_to_dump
609 && fs_blocknr != inode_block_to_dump)
610 return;
611
612 retval = read_journal_block("logdump", source,
613 blocksize * log_blocknr,
614 buf, blocksize, &got);
615 if (retval)
616 return;
617
618 if (fs_blocknr == bitmap_to_dump) {
619 struct ext2_super_block *super;
620 int offset;
621
622 super = current_fs->super;
623 offset = ((block_to_dump - super->s_first_data_block) %
624 super->s_blocks_per_group);
625
626 fprintf(out_file, " (block bitmap for block %llu: "
627 "block is %s)\n",
628 block_to_dump,
629 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
630 }
631
632 if (fs_blocknr == inode_block_to_dump) {
633 struct ext2_inode *inode;
634 int first, prev, this, start_extent, i;
635
636 fprintf(out_file, " (inode block for inode %u):\n",
637 inode_to_dump);
638
639 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
640 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
641
642 /* Dump out the direct/indirect blocks here:
643 * internal_dump_inode can only dump them from the main
644 * on-disk inode, not from the journaled copy of the
645 * inode. */
646
647 fprintf (out_file, " Blocks: ");
648 first = prev = start_extent = -1;
649
650 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
651 this = inode->i_block[i];
652 if (start_extent >= 0 && this == prev+1) {
653 prev = this;
654 continue;
655 } else {
656 show_extent(out_file, start_extent, i, first);
657 start_extent = i;
658 first = prev = this;
659 }
660 }
661 show_extent(out_file, start_extent, i, first);
662 show_indirect(out_file, "IND", inode->i_block[i++]);
663 show_indirect(out_file, "DIND", inode->i_block[i++]);
664 show_indirect(out_file, "TIND", inode->i_block[i++]);
665
666 fprintf(out_file, "\n");
667 }
668
669 if (dump_contents)
670 do_hexdump(out_file, buf, blocksize);
671
672 }
673
do_hexdump(FILE * out_file,char * buf,int blocksize)674 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
675 {
676 int i,j;
677 int *intp;
678 char *charp;
679 unsigned char c;
680
681 intp = (int *) buf;
682 charp = (char *) buf;
683
684 for (i=0; i<blocksize; i+=16) {
685 fprintf(out_file, " %04x: ", i);
686 for (j=0; j<16; j+=4)
687 fprintf(out_file, "%08x ", *intp++);
688 for (j=0; j<16; j++) {
689 c = *charp++;
690 if (c < ' ' || c >= 127)
691 c = '.';
692 fprintf(out_file, "%c", c);
693 }
694 fprintf(out_file, "\n");
695 }
696 }
697
698