blob: 5bcd7ca9e0d972a009048f2d8f4cc42e7d92eae6 [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{
75 ext2_ino_t inode;
76 int c;
77 int fd;
78 int retval;
79 char *out_fn;
80 FILE *out_file;
81
82 char *inode_spec = NULL;
83 char *journal_fn = NULL;
84 int journal_fd = 0;
85 ext2_ino_t journal_inum;
86 struct ext2_inode journal_inode;
87 ext2_file_t journal_file;
88
89 char *tmp;
90
91 const char *logdump_usage = ("Usage: logdump "
92 "[-ac] [-b<block>] [-i<inode>] "
93 "[-f<journal_file>] [output_file]");
94
95 struct journal_source journal_source = {};
96
97 optind = 0;
98#ifdef HAVE_OPTRESET
99 optreset = 1; /* Makes BSD getopt happy */
100#endif
101 dump_all = 0;
102 dump_contents = 0;
103 dump_descriptors = 1;
104 block_to_dump = -1;
105 bitmap_to_dump = -1;
106 inode_block_to_dump = -1;
107 inode_to_dump = -1;
108
109 while ((c = getopt (argc, argv, "ab:ci:f:")) != EOF) {
110 switch (c) {
111 case 'a':
112 dump_all++;
113 break;
114 case 'b':
115 block_to_dump = strtoul(optarg, &tmp, 0);
116 if (*tmp) {
117 com_err(argv[0], 0,
118 "Bad block number - %s", optarg);
119 return;
120 }
121 dump_descriptors = 0;
122 break;
123 case 'c':
124 dump_contents++;
125 break;
126 case 'f':
127 journal_fn = optarg;
128 break;
129 case 'i':
130 inode_spec = optarg;
131 dump_descriptors = 0;
132 break;
133 default:
134 com_err(argv[0], 0, logdump_usage);
135 return;
136 }
137 }
138 if (optind != argc && optind != argc-1) {
139 com_err(argv[0], 0, logdump_usage);
140 return;
141 }
142
143 if (inode_spec) {
144 int inode_group, group_offset, inodes_per_block;
145
146 if (check_fs_open(argv[0]))
147 return;
148
149 inode_to_dump = string_to_inode(inode_spec);
150 if (!inode_to_dump)
151 return;
152
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));
159
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);
168 }
169
170 if (optind == argc) {
171 out_file = stdout;
172 } else {
173 out_fn = argv[optind];
174 out_file = fopen(out_fn, "w");
175 if (!out_file < 0) {
176 com_err(argv[0], errno, "while opening %s for logdump",
177 out_fn);
178 return;
179 }
180 }
181
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;
187 }
188
189 if (journal_fn) {
190
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",
195 journal_fn);
196 return;
197 }
198
199 journal_source.where = JOURNAL_IS_EXTERNAL;
200 journal_source.fd = journal_fd;
201
202 } else {
203
204 /* Set up to read journal from the open filesystem */
205 if (check_fs_open(argv[0]))
206 return;
207 journal_inum = current_fs->super->s_journal_inum;
208 if (!journal_inum) {
209 com_err(argv[0], 0, "filesystem has no journal");
210 return;
211 }
212
213 retval = ext2fs_read_inode(current_fs, journal_inum,
214 &journal_inode);
215 if (retval) {
216 com_err(argv[0], retval,
217 "while reading inode %u", journal_inum);
218 return;
219 }
220
221 retval = ext2fs_file_open(current_fs, journal_inum,
222 0, &journal_file);
223 if (retval) {
224 com_err(argv[0], retval, "while opening ext2 file");
225 return;
226 }
227
228 journal_source.where = JOURNAL_IS_INTERNAL;
229 journal_source.file = journal_file;
230 }
231
232 dump_journal(argv[0], out_file, &journal_source);
233
234 if (journal_source.where == JOURNAL_IS_INTERNAL)
235 ext2fs_file_close(journal_file);
236 else
237 close(journal_fd);
238
239 if (out_file != stdout)
240 fclose(out_file);
241
242 return;
243}
244
245
246int read_journal_block(char *cmd, struct journal_source *source, off_t offset,
247 char *buf, int size, int *got)
248{
249 int retval;
250
251 if (source->where == JOURNAL_IS_EXTERNAL) {
Theodore Ts'o4bb0c042001-06-01 15:22:38 +0000252 if (lseek(source->fd, offset, SEEK_SET) < 0) {
253 retval = errno;
254 com_err(cmd, retval, "while seeking in reading journal");
255 return retval;
256 }
257 retval = read(source->fd, buf, size);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000258 if (retval >= 0) {
259 *got = retval;
260 retval = 0;
261 }
262 retval = errno;
263 } else {
264 retval = ext2fs_file_lseek(source->file, offset,
265 EXT2_SEEK_SET, NULL);
266 if (retval) {
267 com_err(cmd, retval, "while seeking in reading journal");
268 return retval;
269 }
270
271 retval = ext2fs_file_read(source->file, buf, size, got);
272 }
273
274 if (retval)
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);
278 retval = -1;
279 }
280
281 return retval;
282}
283
284static char *type_to_name(int btype)
285{
286 switch (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";
297 default:
298 }
299 return "unrecognised type";
300}
301
302
303static void dump_journal(char *cmdname, FILE *out_file,
304 struct journal_source *source)
305{
306 char jsb_buffer[1024];
307 char buf[8192];
308 journal_superblock_t *jsb;
309 int blocksize;
310 int got;
311 int retval;
312 __u32 magic, sequence, blocktype;
313 journal_header_t *header;
314
315 tid_t transaction;
316 unsigned int blocknr;
317
318 /* First: locate the journal superblock */
319
320 retval = read_journal_block(cmdname, source, 0,
321 jsb_buffer, 1024, &got);
322 if (retval)
323 return;
324
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);
329
330 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
331 blocknr, transaction);
332
333 if (!blocknr)
334 /* Empty journal, nothing to do. */
335 return;
336
337 while (1) {
338 retval = read_journal_block(cmdname, source,
339 blocknr*blocksize, buf,
340 blocksize, &got);
341 if (retval || got != blocksize)
342 return;
343
344 header = (journal_header_t *) buf;
345
346 magic = be32_to_cpu(header->h_magic);
347 sequence = be32_to_cpu(header->h_sequence);
348 blocktype = be32_to_cpu(header->h_blocktype);
349
350 if (magic != JFS_MAGIC_NUMBER) {
351 fprintf (out_file, "No magic number at block %u: "
352 "end of journal.\n", blocknr);
353 return;
354 }
355
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);
360 return;
361 }
362
363 if (dump_descriptors) {
364 fprintf (out_file, "Found expected sequence %u, "
365 "type %u (%s) at block %u\n",
366 sequence, blocktype,
367 type_to_name(blocktype), blocknr);
368 }
369
370 switch (blocktype) {
371 case JFS_DESCRIPTOR_BLOCK:
372 dump_descriptor_block(out_file, source, buf, jsb,
373 &blocknr, blocksize,
374 transaction);
375 continue;
376
377 case JFS_COMMIT_BLOCK:
378 transaction++;
379 blocknr++;
380 WRAP(jsb, blocknr);
381 continue;
382
383 case JFS_REVOKE_BLOCK:
384 dump_revoke_block(out_file, buf, jsb,
385 blocknr, blocksize,
386 transaction);
387 blocknr++;
388 WRAP(jsb, blocknr);
389 continue;
390
391 default:
392 fprintf (out_file, "Unexpected block type %u at "
393 "block %u.\n", blocktype, blocknr);
394 return;
395 }
396 }
397}
398
399
400static void dump_descriptor_block(FILE *out_file,
401 struct journal_source *source,
402 char *buf,
403 journal_superblock_t *jsb,
404 unsigned int *blockp, int blocksize,
405 tid_t transaction)
406{
407 int offset;
408 char *tagp;
409 journal_block_tag_t *tag;
410 unsigned int blocknr;
411 __u32 tag_block;
412 __u32 tag_flags;
413
414
415 offset = sizeof(journal_header_t);
416 blocknr = *blockp;
417
418 if (dump_all)
419 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
420 "block %u:\n", transaction, blocknr);
421
422 ++blocknr;
423 WRAP(jsb, blocknr);
424
425 do {
426 /* Work out the location of the current tag, and skip to
427 * the next one... */
428 tagp = &buf[offset];
429 tag = (journal_block_tag_t *) tagp;
430 offset += sizeof(journal_block_tag_t);
431
432 /* ... and if we have gone too far, then we've reached the
433 end of this block. */
434 if (offset > blocksize)
435 break;
436
437 tag_block = be32_to_cpu(tag->t_blocknr);
438 tag_flags = be32_to_cpu(tag->t_flags);
439
440 if (!(tag_flags & JFS_FLAG_SAME_UUID))
441 offset += 16;
442
443 dump_metadata_block(out_file, source, jsb,
444 blocknr, tag_block, blocksize,
445 transaction);
446
447 ++blocknr;
448 WRAP(jsb, blocknr);
449
450 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
451
452 *blockp = blocknr;
453}
454
455
456static void dump_revoke_block(FILE *out_file, char *buf,
457 journal_superblock_t *jsb,
458 unsigned int blocknr, int blocksize,
459 tid_t transaction)
460{
461 int offset, max;
462 journal_revoke_header_t *header;
463 unsigned int *entry, rblock;
464
465 if (dump_all)
466 fprintf(out_file, "Dumping revoke block, sequence %u, at "
467 "block %u:\n", transaction, blocknr);
468
469 header = (journal_revoke_header_t *) buf;
470 offset = sizeof(journal_revoke_header_t);
471 max = be32_to_cpu(header->r_count);
472
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);
478 if (dump_all)
479 fprintf(out_file, "\n");
480 else
481 fprintf(out_file," at block %u, sequence %u\n",
482 blocknr, transaction);
483 }
484 offset += 4;
485 }
486}
487
488
489static void show_extent(FILE *out_file, int start_extent, int end_extent,
490 __u32 first_block)
491{
492 if (start_extent >= 0 && first_block != 0)
493 fprintf(out_file, "(%d+%u): %u ",
494 start_extent, end_extent-start_extent, first_block);
495}
496
497static void show_indirect(FILE *out_file, char *name, __u32 where)
498{
499 if (where)
500 fprintf(out_file, "(%s): %u ", name, where);
501}
502
503
504static 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,
508 int blocksize,
509 tid_t transaction)
510{
511 int got, retval;
512 char buf[8192];
513
514 if (!(dump_all
515 || (fs_blocknr == block_to_dump)
516 || (fs_blocknr == inode_block_to_dump)
517 || (fs_blocknr == bitmap_to_dump)))
518 return;
519
520 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
521 if (!dump_all)
522 fprintf(out_file, "sequence %u, ", transaction);
523 fprintf(out_file, "journal block %u\n", log_blocknr);
524
525 /* There are two major special cases to parse:
526 *
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.
531 *
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.
535 */
536
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)
541 return;
542
543 retval = read_journal_block("logdump", source,
544 blocksize * log_blocknr,
545 buf, blocksize, &got);
546 if (retval)
547 return;
548
549 if (fs_blocknr == bitmap_to_dump) {
550 struct ext2_super_block *super;
551 int offset;
552
553 super = current_fs->super;
554 offset = ((fs_blocknr - super->s_first_data_block) %
555 super->s_blocks_per_group);
556
557 fprintf(out_file, " (block bitmap for block %u: "
558 "block is %s)\n",
559 block_to_dump,
560 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
561 }
562
563 if (fs_blocknr == inode_block_to_dump) {
564 struct ext2_inode *inode;
565 int first, prev, this, start_extent, i;
566
567 fprintf(out_file, " (inode block for inode %u):\n",
568 inode_to_dump);
569
570 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
571 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
572
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
576 * inode. */
577
578 fprintf (out_file, " Blocks: ");
579 start_extent = -1;
580
581 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
582 this = inode->i_block[i];
583 if (start_extent >= 0 && this == prev+1) {
584 prev = this;
585 continue;
586 } else {
587 show_extent(out_file, start_extent, i, first);
588 start_extent = i;
589 first = prev = this;
590 }
591 }
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++]);
596
597 fprintf(out_file, "\n");
598 }
599
600 if (dump_contents)
601 do_hexdump(out_file, buf, blocksize);
602
603}
604
605static void do_hexdump (FILE *out_file, char *buf, int blocksize)
606{
607 int i,j;
608 int *intp;
609 char *charp;
610 unsigned char c;
611
612 intp = (int *) buf;
613 charp = (char *) buf;
614
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++) {
620 c = *charp++;
621 if (c < ' ' || c >= 127)
622 c = '.';
623 fprintf(out_file, "%c", c);
624 }
625 fprintf(out_file, "\n");
626 }
627}
628