blob: a1441b776dd47298462877e1b4d9d757bbfcb72c [file] [log] [blame]
Theodore Ts'oda81e3f2001-03-29 20:49:58 +00001/*
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
28extern int optind;
29extern char *optarg;
30#endif
31#ifdef HAVE_OPTRESET
32extern int optreset; /* defined by BSD, but not others */
33#endif
34
35#include "debugfs.h"
36#include "jfs_user.h"
37
38enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
39
40int dump_all, dump_contents, dump_descriptors;
41unsigned int block_to_dump, group_to_dump, bitmap_to_dump;
42unsigned int inode_block_to_dump, inode_offset_to_dump, bitmap_to_dump;
43ext2_ino_t inode_to_dump;
44
45struct journal_source
46{
47 enum journal_location where;
48 int fd;
49 ext2_file_t file;
50};
51
52static void dump_journal(char *, FILE *, struct journal_source *);
53
54static void dump_descriptor_block(FILE *, struct journal_source *,
55 char *, journal_superblock_t *,
56 unsigned int *, int, tid_t);
57
58static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
59 unsigned int, int, tid_t);
60
61static void dump_metadata_block(FILE *, struct journal_source *,
62 journal_superblock_t*,
63 unsigned int, unsigned int, int, tid_t);
64
65static void do_hexdump (FILE *, char *, int);
66
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));
71
72
73void do_logdump(int argc, char **argv)
74{
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000075 int c;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000076 int retval;
77 char *out_fn;
78 FILE *out_file;
79
80 char *inode_spec = NULL;
81 char *journal_fn = NULL;
82 int journal_fd = 0;
83 ext2_ino_t journal_inum;
84 struct ext2_inode journal_inode;
85 ext2_file_t journal_file;
86
87 char *tmp;
88
89 const char *logdump_usage = ("Usage: logdump "
90 "[-ac] [-b<block>] [-i<inode>] "
91 "[-f<journal_file>] [output_file]");
92
Theodore Ts'o5e4f0702001-06-01 15:36:05 +000093 struct journal_source journal_source;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000094
95 optind = 0;
96#ifdef HAVE_OPTRESET
97 optreset = 1; /* Makes BSD getopt happy */
98#endif
Theodore Ts'o5e4f0702001-06-01 15:36:05 +000099 journal_source.where = 0;
100 journal_source.fd = 0;
101 journal_source.file = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000102 dump_all = 0;
103 dump_contents = 0;
104 dump_descriptors = 1;
105 block_to_dump = -1;
106 bitmap_to_dump = -1;
107 inode_block_to_dump = -1;
108 inode_to_dump = -1;
109
110 while ((c = getopt (argc, argv, "ab:ci:f:")) != EOF) {
111 switch (c) {
112 case 'a':
113 dump_all++;
114 break;
115 case 'b':
116 block_to_dump = strtoul(optarg, &tmp, 0);
117 if (*tmp) {
118 com_err(argv[0], 0,
119 "Bad block number - %s", optarg);
120 return;
121 }
122 dump_descriptors = 0;
123 break;
124 case 'c':
125 dump_contents++;
126 break;
127 case 'f':
128 journal_fn = optarg;
129 break;
130 case 'i':
131 inode_spec = optarg;
132 dump_descriptors = 0;
133 break;
134 default:
135 com_err(argv[0], 0, logdump_usage);
136 return;
137 }
138 }
139 if (optind != argc && optind != argc-1) {
140 com_err(argv[0], 0, logdump_usage);
141 return;
142 }
143
144 if (inode_spec) {
145 int inode_group, group_offset, inodes_per_block;
146
147 if (check_fs_open(argv[0]))
148 return;
149
150 inode_to_dump = string_to_inode(inode_spec);
151 if (!inode_to_dump)
152 return;
153
154 inode_group = ((inode_to_dump - 1)
155 / current_fs->super->s_inodes_per_group);
156 group_offset = ((inode_to_dump - 1)
157 % current_fs->super->s_inodes_per_group);
158 inodes_per_block = (current_fs->blocksize
159 / sizeof(struct ext2_inode));
160
161 inode_block_to_dump =
162 current_fs->group_desc[inode_group].bg_inode_table +
163 (group_offset / inodes_per_block);
164 inode_offset_to_dump = ((group_offset % inodes_per_block)
165 * sizeof(struct ext2_inode));
166 printf("Inode %u is at group %u, block %u, offset %u\n",
167 inode_to_dump, inode_group,
168 inode_block_to_dump, inode_offset_to_dump);
169 }
170
171 if (optind == argc) {
172 out_file = stdout;
173 } else {
174 out_fn = argv[optind];
175 out_file = fopen(out_fn, "w");
176 if (!out_file < 0) {
177 com_err(argv[0], errno, "while opening %s for logdump",
178 out_fn);
179 return;
180 }
181 }
182
183 if (block_to_dump != -1 && current_fs != NULL) {
184 group_to_dump = ((block_to_dump -
185 current_fs->super->s_first_data_block)
186 / current_fs->super->s_blocks_per_group);
187 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
188 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000189
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400190 if (!journal_fn && check_fs_open(argv[0]))
191 return;
192
193 if (journal_fn) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000194 /* Set up to read journal from a regular file somewhere */
195 journal_fd = open(journal_fn, O_RDONLY, 0);
196 if (journal_fd < 0) {
197 com_err(argv[0], errno, "while opening %s for logdump",
198 journal_fn);
199 return;
200 }
201
202 journal_source.where = JOURNAL_IS_EXTERNAL;
203 journal_source.fd = journal_fd;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400204 } else if ((journal_inum = current_fs->super->s_journal_inum)) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000205 retval = ext2fs_read_inode(current_fs, journal_inum,
206 &journal_inode);
207 if (retval) {
208 com_err(argv[0], retval,
209 "while reading inode %u", journal_inum);
210 return;
211 }
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400212
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000213 retval = ext2fs_file_open(current_fs, journal_inum,
214 0, &journal_file);
215 if (retval) {
216 com_err(argv[0], retval, "while opening ext2 file");
217 return;
218 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000219 journal_source.where = JOURNAL_IS_INTERNAL;
220 journal_source.file = journal_file;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400221 } else if ((journal_fn =
222 ext2fs_find_block_device(current_fs->super->s_journal_dev))) {
223 journal_fd = open(journal_fn, O_RDONLY, 0);
224 if (journal_fd < 0) {
225 com_err(argv[0], errno, "while opening %s for logdump",
226 journal_fn);
227 free(journal_fn);
228 return;
229 }
230 fprintf(out_file, "Using external journal found at %s\n",
231 journal_fn);
232 free(journal_fn);
233 journal_source.where = JOURNAL_IS_EXTERNAL;
234 journal_source.fd = journal_fd;
235 } else {
236 com_err(argv[0], 0, "filesystem has no journal");
237 return;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000238 }
239
240 dump_journal(argv[0], out_file, &journal_source);
241
242 if (journal_source.where == JOURNAL_IS_INTERNAL)
243 ext2fs_file_close(journal_file);
244 else
245 close(journal_fd);
246
247 if (out_file != stdout)
248 fclose(out_file);
249
250 return;
251}
252
253
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000254static int read_journal_block(const char *cmd, struct journal_source *source,
255 off_t offset, char *buf, int size,
256 unsigned int *got)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000257{
258 int retval;
259
260 if (source->where == JOURNAL_IS_EXTERNAL) {
Theodore Ts'o4bb0c042001-06-01 15:22:38 +0000261 if (lseek(source->fd, offset, SEEK_SET) < 0) {
262 retval = errno;
263 com_err(cmd, retval, "while seeking in reading journal");
264 return retval;
265 }
266 retval = read(source->fd, buf, size);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000267 if (retval >= 0) {
268 *got = retval;
269 retval = 0;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400270 } else
271 retval = errno;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000272 } else {
273 retval = ext2fs_file_lseek(source->file, offset,
274 EXT2_SEEK_SET, NULL);
275 if (retval) {
276 com_err(cmd, retval, "while seeking in reading journal");
277 return retval;
278 }
279
280 retval = ext2fs_file_read(source->file, buf, size, got);
281 }
282
283 if (retval)
284 com_err(cmd, retval, "while while reading journal");
285 else if (*got != size) {
286 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
287 retval = -1;
288 }
289
290 return retval;
291}
292
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000293static const char *type_to_name(int btype)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000294{
295 switch (btype) {
296 case JFS_DESCRIPTOR_BLOCK:
297 return "descriptor block";
298 case JFS_COMMIT_BLOCK:
299 return "commit block";
300 case JFS_SUPERBLOCK_V1:
301 return "V1 superblock";
302 case JFS_SUPERBLOCK_V2:
303 return "V2 superblock";
304 case JFS_REVOKE_BLOCK:
305 return "revoke table";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000306 }
307 return "unrecognised type";
308}
309
310
311static void dump_journal(char *cmdname, FILE *out_file,
312 struct journal_source *source)
313{
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400314 struct ext2_super_block *sb;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000315 char jsb_buffer[1024];
316 char buf[8192];
317 journal_superblock_t *jsb;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400318 int blocksize = 1024;
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000319 unsigned int got;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000320 int retval;
321 __u32 magic, sequence, blocktype;
322 journal_header_t *header;
323
324 tid_t transaction;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400325 unsigned int blocknr = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000326
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400327 /* First, check to see if there's an ext2 superblock header */
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000328 retval = read_journal_block(cmdname, source, 0,
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400329 buf, 2048, &got);
330 if (retval)
331 return;
332
333 jsb = (journal_superblock_t *) buf;
334 sb = (struct ext2_super_block *) (buf+1024);
335 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
336 ext2fs_swap_super(sb);
337
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");
345 if (dump_all) {
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 sb->s_blocks_count);
350 }
351 }
352
353 /* Next, read the journal superblock */
354
355 retval = read_journal_block(cmdname, source, blocknr*blocksize,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000356 jsb_buffer, 1024, &got);
357 if (retval)
358 return;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400359
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000360 jsb = (journal_superblock_t *) jsb_buffer;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400361 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
362 fprintf(out_file,
363 "Journal superblock magic number invalid!\n");
364 return;
365 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000366 blocksize = be32_to_cpu(jsb->s_blocksize);
367 transaction = be32_to_cpu(jsb->s_sequence);
368 blocknr = be32_to_cpu(jsb->s_start);
369
370 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
371 blocknr, transaction);
372
373 if (!blocknr)
374 /* Empty journal, nothing to do. */
375 return;
376
377 while (1) {
378 retval = read_journal_block(cmdname, source,
379 blocknr*blocksize, buf,
380 blocksize, &got);
381 if (retval || got != blocksize)
382 return;
383
384 header = (journal_header_t *) buf;
385
386 magic = be32_to_cpu(header->h_magic);
387 sequence = be32_to_cpu(header->h_sequence);
388 blocktype = be32_to_cpu(header->h_blocktype);
389
390 if (magic != JFS_MAGIC_NUMBER) {
391 fprintf (out_file, "No magic number at block %u: "
392 "end of journal.\n", blocknr);
393 return;
394 }
395
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);
400 return;
401 }
402
403 if (dump_descriptors) {
404 fprintf (out_file, "Found expected sequence %u, "
405 "type %u (%s) at block %u\n",
406 sequence, blocktype,
407 type_to_name(blocktype), blocknr);
408 }
409
410 switch (blocktype) {
411 case JFS_DESCRIPTOR_BLOCK:
412 dump_descriptor_block(out_file, source, buf, jsb,
413 &blocknr, blocksize,
414 transaction);
415 continue;
416
417 case JFS_COMMIT_BLOCK:
418 transaction++;
419 blocknr++;
420 WRAP(jsb, blocknr);
421 continue;
422
423 case JFS_REVOKE_BLOCK:
424 dump_revoke_block(out_file, buf, jsb,
425 blocknr, blocksize,
426 transaction);
427 blocknr++;
428 WRAP(jsb, blocknr);
429 continue;
430
431 default:
432 fprintf (out_file, "Unexpected block type %u at "
433 "block %u.\n", blocktype, blocknr);
434 return;
435 }
436 }
437}
438
439
440static void dump_descriptor_block(FILE *out_file,
441 struct journal_source *source,
442 char *buf,
443 journal_superblock_t *jsb,
444 unsigned int *blockp, int blocksize,
445 tid_t transaction)
446{
447 int offset;
448 char *tagp;
449 journal_block_tag_t *tag;
450 unsigned int blocknr;
451 __u32 tag_block;
452 __u32 tag_flags;
453
454
455 offset = sizeof(journal_header_t);
456 blocknr = *blockp;
457
458 if (dump_all)
459 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
460 "block %u:\n", transaction, blocknr);
461
462 ++blocknr;
463 WRAP(jsb, blocknr);
464
465 do {
466 /* Work out the location of the current tag, and skip to
467 * the next one... */
468 tagp = &buf[offset];
469 tag = (journal_block_tag_t *) tagp;
470 offset += sizeof(journal_block_tag_t);
471
472 /* ... and if we have gone too far, then we've reached the
473 end of this block. */
474 if (offset > blocksize)
475 break;
476
477 tag_block = be32_to_cpu(tag->t_blocknr);
478 tag_flags = be32_to_cpu(tag->t_flags);
479
480 if (!(tag_flags & JFS_FLAG_SAME_UUID))
481 offset += 16;
482
483 dump_metadata_block(out_file, source, jsb,
484 blocknr, tag_block, blocksize,
485 transaction);
486
487 ++blocknr;
488 WRAP(jsb, blocknr);
489
490 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
491
492 *blockp = blocknr;
493}
494
495
496static void dump_revoke_block(FILE *out_file, char *buf,
497 journal_superblock_t *jsb,
498 unsigned int blocknr, int blocksize,
499 tid_t transaction)
500{
501 int offset, max;
502 journal_revoke_header_t *header;
503 unsigned int *entry, rblock;
504
505 if (dump_all)
506 fprintf(out_file, "Dumping revoke block, sequence %u, at "
507 "block %u:\n", transaction, blocknr);
508
509 header = (journal_revoke_header_t *) buf;
510 offset = sizeof(journal_revoke_header_t);
511 max = be32_to_cpu(header->r_count);
512
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);
518 if (dump_all)
519 fprintf(out_file, "\n");
520 else
521 fprintf(out_file," at block %u, sequence %u\n",
522 blocknr, transaction);
523 }
524 offset += 4;
525 }
526}
527
528
529static void show_extent(FILE *out_file, int start_extent, int end_extent,
530 __u32 first_block)
531{
532 if (start_extent >= 0 && first_block != 0)
533 fprintf(out_file, "(%d+%u): %u ",
534 start_extent, end_extent-start_extent, first_block);
535}
536
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000537static void show_indirect(FILE *out_file, const char *name, __u32 where)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000538{
539 if (where)
540 fprintf(out_file, "(%s): %u ", name, where);
541}
542
543
544static 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,
548 int blocksize,
549 tid_t transaction)
550{
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000551 unsigned int got;
552 int retval;
553 char buf[8192];
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000554
555 if (!(dump_all
556 || (fs_blocknr == block_to_dump)
557 || (fs_blocknr == inode_block_to_dump)
558 || (fs_blocknr == bitmap_to_dump)))
559 return;
560
561 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
562 if (!dump_all)
563 fprintf(out_file, "sequence %u, ", transaction);
564 fprintf(out_file, "journal block %u\n", log_blocknr);
565
566 /* There are two major special cases to parse:
567 *
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.
572 *
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.
576 */
577
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)
582 return;
583
584 retval = read_journal_block("logdump", source,
585 blocksize * log_blocknr,
586 buf, blocksize, &got);
587 if (retval)
588 return;
589
590 if (fs_blocknr == bitmap_to_dump) {
591 struct ext2_super_block *super;
592 int offset;
593
594 super = current_fs->super;
595 offset = ((fs_blocknr - super->s_first_data_block) %
596 super->s_blocks_per_group);
597
598 fprintf(out_file, " (block bitmap for block %u: "
599 "block is %s)\n",
600 block_to_dump,
601 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
602 }
603
604 if (fs_blocknr == inode_block_to_dump) {
605 struct ext2_inode *inode;
606 int first, prev, this, start_extent, i;
607
608 fprintf(out_file, " (inode block for inode %u):\n",
609 inode_to_dump);
610
611 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
612 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
613
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
617 * inode. */
618
619 fprintf (out_file, " Blocks: ");
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000620 first = prev = start_extent = -1;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000621
622 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
623 this = inode->i_block[i];
624 if (start_extent >= 0 && this == prev+1) {
625 prev = this;
626 continue;
627 } else {
628 show_extent(out_file, start_extent, i, first);
629 start_extent = i;
630 first = prev = this;
631 }
632 }
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++]);
637
638 fprintf(out_file, "\n");
639 }
640
641 if (dump_contents)
642 do_hexdump(out_file, buf, blocksize);
643
644}
645
646static void do_hexdump (FILE *out_file, char *buf, int blocksize)
647{
648 int i,j;
649 int *intp;
650 char *charp;
651 unsigned char c;
652
653 intp = (int *) buf;
654 charp = (char *) buf;
655
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++) {
661 c = *charp++;
662 if (c < ' ' || c >= 127)
663 c = '.';
664 fprintf(out_file, "%c", c);
665 }
666 fprintf(out_file, "\n");
667 }
668}
669