blob: 6c9fda4c5b33d22cbe1bf610837f42be9bb1929f [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) {
252 retval = pread(source->fd, buf, size, offset);
253 if (retval >= 0) {
254 *got = retval;
255 retval = 0;
256 }
257 retval = errno;
258 } else {
259 retval = ext2fs_file_lseek(source->file, offset,
260 EXT2_SEEK_SET, NULL);
261 if (retval) {
262 com_err(cmd, retval, "while seeking in reading journal");
263 return retval;
264 }
265
266 retval = ext2fs_file_read(source->file, buf, size, got);
267 }
268
269 if (retval)
270 com_err(cmd, retval, "while while reading journal");
271 else if (*got != size) {
272 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
273 retval = -1;
274 }
275
276 return retval;
277}
278
279static char *type_to_name(int btype)
280{
281 switch (btype) {
282 case JFS_DESCRIPTOR_BLOCK:
283 return "descriptor block";
284 case JFS_COMMIT_BLOCK:
285 return "commit block";
286 case JFS_SUPERBLOCK_V1:
287 return "V1 superblock";
288 case JFS_SUPERBLOCK_V2:
289 return "V2 superblock";
290 case JFS_REVOKE_BLOCK:
291 return "revoke table";
292 default:
293 }
294 return "unrecognised type";
295}
296
297
298static void dump_journal(char *cmdname, FILE *out_file,
299 struct journal_source *source)
300{
301 char jsb_buffer[1024];
302 char buf[8192];
303 journal_superblock_t *jsb;
304 int blocksize;
305 int got;
306 int retval;
307 __u32 magic, sequence, blocktype;
308 journal_header_t *header;
309
310 tid_t transaction;
311 unsigned int blocknr;
312
313 /* First: locate the journal superblock */
314
315 retval = read_journal_block(cmdname, source, 0,
316 jsb_buffer, 1024, &got);
317 if (retval)
318 return;
319
320 jsb = (journal_superblock_t *) jsb_buffer;
321 blocksize = be32_to_cpu(jsb->s_blocksize);
322 transaction = be32_to_cpu(jsb->s_sequence);
323 blocknr = be32_to_cpu(jsb->s_start);
324
325 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
326 blocknr, transaction);
327
328 if (!blocknr)
329 /* Empty journal, nothing to do. */
330 return;
331
332 while (1) {
333 retval = read_journal_block(cmdname, source,
334 blocknr*blocksize, buf,
335 blocksize, &got);
336 if (retval || got != blocksize)
337 return;
338
339 header = (journal_header_t *) buf;
340
341 magic = be32_to_cpu(header->h_magic);
342 sequence = be32_to_cpu(header->h_sequence);
343 blocktype = be32_to_cpu(header->h_blocktype);
344
345 if (magic != JFS_MAGIC_NUMBER) {
346 fprintf (out_file, "No magic number at block %u: "
347 "end of journal.\n", blocknr);
348 return;
349 }
350
351 if (sequence != transaction) {
352 fprintf (out_file, "Found sequence %u (not %u) at "
353 "block %u: end of journal.\n",
354 sequence, transaction, blocknr);
355 return;
356 }
357
358 if (dump_descriptors) {
359 fprintf (out_file, "Found expected sequence %u, "
360 "type %u (%s) at block %u\n",
361 sequence, blocktype,
362 type_to_name(blocktype), blocknr);
363 }
364
365 switch (blocktype) {
366 case JFS_DESCRIPTOR_BLOCK:
367 dump_descriptor_block(out_file, source, buf, jsb,
368 &blocknr, blocksize,
369 transaction);
370 continue;
371
372 case JFS_COMMIT_BLOCK:
373 transaction++;
374 blocknr++;
375 WRAP(jsb, blocknr);
376 continue;
377
378 case JFS_REVOKE_BLOCK:
379 dump_revoke_block(out_file, buf, jsb,
380 blocknr, blocksize,
381 transaction);
382 blocknr++;
383 WRAP(jsb, blocknr);
384 continue;
385
386 default:
387 fprintf (out_file, "Unexpected block type %u at "
388 "block %u.\n", blocktype, blocknr);
389 return;
390 }
391 }
392}
393
394
395static void dump_descriptor_block(FILE *out_file,
396 struct journal_source *source,
397 char *buf,
398 journal_superblock_t *jsb,
399 unsigned int *blockp, int blocksize,
400 tid_t transaction)
401{
402 int offset;
403 char *tagp;
404 journal_block_tag_t *tag;
405 unsigned int blocknr;
406 __u32 tag_block;
407 __u32 tag_flags;
408
409
410 offset = sizeof(journal_header_t);
411 blocknr = *blockp;
412
413 if (dump_all)
414 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
415 "block %u:\n", transaction, blocknr);
416
417 ++blocknr;
418 WRAP(jsb, blocknr);
419
420 do {
421 /* Work out the location of the current tag, and skip to
422 * the next one... */
423 tagp = &buf[offset];
424 tag = (journal_block_tag_t *) tagp;
425 offset += sizeof(journal_block_tag_t);
426
427 /* ... and if we have gone too far, then we've reached the
428 end of this block. */
429 if (offset > blocksize)
430 break;
431
432 tag_block = be32_to_cpu(tag->t_blocknr);
433 tag_flags = be32_to_cpu(tag->t_flags);
434
435 if (!(tag_flags & JFS_FLAG_SAME_UUID))
436 offset += 16;
437
438 dump_metadata_block(out_file, source, jsb,
439 blocknr, tag_block, blocksize,
440 transaction);
441
442 ++blocknr;
443 WRAP(jsb, blocknr);
444
445 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
446
447 *blockp = blocknr;
448}
449
450
451static void dump_revoke_block(FILE *out_file, char *buf,
452 journal_superblock_t *jsb,
453 unsigned int blocknr, int blocksize,
454 tid_t transaction)
455{
456 int offset, max;
457 journal_revoke_header_t *header;
458 unsigned int *entry, rblock;
459
460 if (dump_all)
461 fprintf(out_file, "Dumping revoke block, sequence %u, at "
462 "block %u:\n", transaction, blocknr);
463
464 header = (journal_revoke_header_t *) buf;
465 offset = sizeof(journal_revoke_header_t);
466 max = be32_to_cpu(header->r_count);
467
468 while (offset < max) {
469 entry = (unsigned int *) (buf + offset);
470 rblock = be32_to_cpu(*entry);
471 if (dump_all || rblock == block_to_dump) {
472 fprintf(out_file, " Revoke FS block %u", rblock);
473 if (dump_all)
474 fprintf(out_file, "\n");
475 else
476 fprintf(out_file," at block %u, sequence %u\n",
477 blocknr, transaction);
478 }
479 offset += 4;
480 }
481}
482
483
484static void show_extent(FILE *out_file, int start_extent, int end_extent,
485 __u32 first_block)
486{
487 if (start_extent >= 0 && first_block != 0)
488 fprintf(out_file, "(%d+%u): %u ",
489 start_extent, end_extent-start_extent, first_block);
490}
491
492static void show_indirect(FILE *out_file, char *name, __u32 where)
493{
494 if (where)
495 fprintf(out_file, "(%s): %u ", name, where);
496}
497
498
499static void dump_metadata_block(FILE *out_file, struct journal_source *source,
500 journal_superblock_t *jsb,
501 unsigned int log_blocknr,
502 unsigned int fs_blocknr,
503 int blocksize,
504 tid_t transaction)
505{
506 int got, retval;
507 char buf[8192];
508
509 if (!(dump_all
510 || (fs_blocknr == block_to_dump)
511 || (fs_blocknr == inode_block_to_dump)
512 || (fs_blocknr == bitmap_to_dump)))
513 return;
514
515 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
516 if (!dump_all)
517 fprintf(out_file, "sequence %u, ", transaction);
518 fprintf(out_file, "journal block %u\n", log_blocknr);
519
520 /* There are two major special cases to parse:
521 *
522 * If this block is a block
523 * bitmap block, we need to give it special treatment so that we
524 * can log any allocates and deallocates which affect the
525 * block_to_dump query block.
526 *
527 * If the block is an inode block for the inode being searched
528 * for, then we need to dump the contents of that inode
529 * structure symbolically.
530 */
531
532 if (!(dump_contents && dump_all)
533 && fs_blocknr != block_to_dump
534 && fs_blocknr != bitmap_to_dump
535 && fs_blocknr != inode_block_to_dump)
536 return;
537
538 retval = read_journal_block("logdump", source,
539 blocksize * log_blocknr,
540 buf, blocksize, &got);
541 if (retval)
542 return;
543
544 if (fs_blocknr == bitmap_to_dump) {
545 struct ext2_super_block *super;
546 int offset;
547
548 super = current_fs->super;
549 offset = ((fs_blocknr - super->s_first_data_block) %
550 super->s_blocks_per_group);
551
552 fprintf(out_file, " (block bitmap for block %u: "
553 "block is %s)\n",
554 block_to_dump,
555 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
556 }
557
558 if (fs_blocknr == inode_block_to_dump) {
559 struct ext2_inode *inode;
560 int first, prev, this, start_extent, i;
561
562 fprintf(out_file, " (inode block for inode %u):\n",
563 inode_to_dump);
564
565 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
566 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
567
568 /* Dump out the direct/indirect blocks here:
569 * internal_dump_inode can only dump them from the main
570 * on-disk inode, not from the journaled copy of the
571 * inode. */
572
573 fprintf (out_file, " Blocks: ");
574 start_extent = -1;
575
576 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
577 this = inode->i_block[i];
578 if (start_extent >= 0 && this == prev+1) {
579 prev = this;
580 continue;
581 } else {
582 show_extent(out_file, start_extent, i, first);
583 start_extent = i;
584 first = prev = this;
585 }
586 }
587 show_extent(out_file, start_extent, i, first);
588 show_indirect(out_file, "IND", inode->i_block[i++]);
589 show_indirect(out_file, "DIND", inode->i_block[i++]);
590 show_indirect(out_file, "TIND", inode->i_block[i++]);
591
592 fprintf(out_file, "\n");
593 }
594
595 if (dump_contents)
596 do_hexdump(out_file, buf, blocksize);
597
598}
599
600static void do_hexdump (FILE *out_file, char *buf, int blocksize)
601{
602 int i,j;
603 int *intp;
604 char *charp;
605 unsigned char c;
606
607 intp = (int *) buf;
608 charp = (char *) buf;
609
610 for (i=0; i<blocksize; i+=16) {
611 fprintf(out_file, " %04x: ", i);
612 for (j=0; j<16; j+=4)
613 fprintf(out_file, "%08x ", *intp++);
614 for (j=0; j<16; j++) {
615 c = *charp++;
616 if (c < ' ' || c >= 127)
617 c = '.';
618 fprintf(out_file, "%c", c);
619 }
620 fprintf(out_file, "\n");
621 }
622}
623