blob: ab85ffef0d354a01546a56b7a3c8c51cd0da2147 [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
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000031
32#include "debugfs.h"
Theodore Ts'of3640932003-03-01 19:47:44 -050033#include "blkid/blkid.h"
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000034#include "jfs_user.h"
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -050035#include <uuid/uuid.h>
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000036
37enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
38
Theodore Ts'o54434922003-12-07 01:28:50 -050039#define ANY_BLOCK ((unsigned int) -1)
40
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000041int dump_all, dump_contents, dump_descriptors;
42unsigned int block_to_dump, group_to_dump, bitmap_to_dump;
43unsigned int inode_block_to_dump, inode_offset_to_dump, bitmap_to_dump;
44ext2_ino_t inode_to_dump;
45
46struct journal_source
47{
48 enum journal_location where;
49 int fd;
50 ext2_file_t file;
51};
52
53static void dump_journal(char *, FILE *, struct journal_source *);
54
55static void dump_descriptor_block(FILE *, struct journal_source *,
56 char *, journal_superblock_t *,
57 unsigned int *, int, tid_t);
58
59static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
60 unsigned int, int, tid_t);
61
62static void dump_metadata_block(FILE *, struct journal_source *,
63 journal_superblock_t*,
64 unsigned int, unsigned int, int, tid_t);
65
66static void do_hexdump (FILE *, char *, int);
67
68#define WRAP(jsb, blocknr) \
69 if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \
70 blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \
71 be32_to_cpu((jsb)->s_first));
72
73
74void do_logdump(int argc, char **argv)
75{
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000076 int c;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000077 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;
Theodore Ts'oa435ec32003-08-21 00:40:26 -040084 int use_sb = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000085 ext2_ino_t journal_inum;
86 struct ext2_inode journal_inode;
87 ext2_file_t journal_file;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000088 char *tmp;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000089 const char *logdump_usage = ("Usage: logdump "
90 "[-ac] [-b<block>] [-i<inode>] "
91 "[-f<journal_file>] [output_file]");
Theodore Ts'o5e4f0702001-06-01 15:36:05 +000092 struct journal_source journal_source;
Theodore Ts'of3640932003-03-01 19:47:44 -050093 struct ext2_super_block *es = NULL;
94
Theodore Ts'o5e4f0702001-06-01 15:36:05 +000095 journal_source.where = 0;
96 journal_source.fd = 0;
97 journal_source.file = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000098 dump_all = 0;
99 dump_contents = 0;
100 dump_descriptors = 1;
Theodore Ts'o54434922003-12-07 01:28:50 -0500101 block_to_dump = ANY_BLOCK;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000102 bitmap_to_dump = -1;
Theodore Ts'o54434922003-12-07 01:28:50 -0500103 inode_block_to_dump = ANY_BLOCK;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000104 inode_to_dump = -1;
105
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400106 reset_getopt();
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400107 while ((c = getopt (argc, argv, "ab:ci:f:s")) != EOF) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000108 switch (c) {
109 case 'a':
110 dump_all++;
111 break;
112 case 'b':
113 block_to_dump = strtoul(optarg, &tmp, 0);
114 if (*tmp) {
115 com_err(argv[0], 0,
116 "Bad block number - %s", optarg);
117 return;
118 }
119 dump_descriptors = 0;
120 break;
121 case 'c':
122 dump_contents++;
123 break;
124 case 'f':
125 journal_fn = optarg;
126 break;
127 case 'i':
128 inode_spec = optarg;
129 dump_descriptors = 0;
130 break;
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400131 case 's':
132 use_sb++;
133 break;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000134 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
Theodore Ts'of3640932003-03-01 19:47:44 -0500144 if (current_fs)
145 es = current_fs->super;
146
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000147 if (inode_spec) {
148 int inode_group, group_offset, inodes_per_block;
Theodore Ts'of3640932003-03-01 19:47:44 -0500149
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000150 if (check_fs_open(argv[0]))
151 return;
152
153 inode_to_dump = string_to_inode(inode_spec);
154 if (!inode_to_dump)
155 return;
156
157 inode_group = ((inode_to_dump - 1)
Theodore Ts'of3640932003-03-01 19:47:44 -0500158 / es->s_inodes_per_group);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000159 group_offset = ((inode_to_dump - 1)
Theodore Ts'of3640932003-03-01 19:47:44 -0500160 % es->s_inodes_per_group);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000161 inodes_per_block = (current_fs->blocksize
162 / sizeof(struct ext2_inode));
163
164 inode_block_to_dump =
165 current_fs->group_desc[inode_group].bg_inode_table +
166 (group_offset / inodes_per_block);
167 inode_offset_to_dump = ((group_offset % inodes_per_block)
168 * sizeof(struct ext2_inode));
169 printf("Inode %u is at group %u, block %u, offset %u\n",
170 inode_to_dump, inode_group,
171 inode_block_to_dump, inode_offset_to_dump);
172 }
173
174 if (optind == argc) {
175 out_file = stdout;
176 } else {
177 out_fn = argv[optind];
178 out_file = fopen(out_fn, "w");
179 if (!out_file < 0) {
180 com_err(argv[0], errno, "while opening %s for logdump",
181 out_fn);
182 return;
183 }
184 }
185
Theodore Ts'o54434922003-12-07 01:28:50 -0500186 if (block_to_dump != ANY_BLOCK && current_fs != NULL) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000187 group_to_dump = ((block_to_dump -
Theodore Ts'of3640932003-03-01 19:47:44 -0500188 es->s_first_data_block)
189 / es->s_blocks_per_group);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000190 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
191 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000192
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400193 if (!journal_fn && check_fs_open(argv[0]))
194 return;
195
196 if (journal_fn) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000197 /* Set up to read journal from a regular file somewhere */
198 journal_fd = open(journal_fn, O_RDONLY, 0);
199 if (journal_fd < 0) {
200 com_err(argv[0], errno, "while opening %s for logdump",
201 journal_fn);
202 return;
203 }
204
205 journal_source.where = JOURNAL_IS_EXTERNAL;
206 journal_source.fd = journal_fd;
Theodore Ts'of3640932003-03-01 19:47:44 -0500207 } else if ((journal_inum = es->s_journal_inum)) {
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400208 if (use_sb) {
209 if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
210 com_err(argv[0], 0,
211 "no journal backup in super block\n");
212 return;
213 }
214 memset(&journal_inode, 0, sizeof(struct ext2_inode));
215 memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
216 EXT2_N_BLOCKS*4);
217 journal_inode.i_size = es->s_jnl_blocks[16];
218 journal_inode.i_links_count = 1;
219 journal_inode.i_mode = LINUX_S_IFREG | 0600;
220 } else {
221 if (debugfs_read_inode(journal_inum, &journal_inode,
222 argv[0]))
223 return;
224 }
225
226 retval = ext2fs_file_open2(current_fs, journal_inum,
227 &journal_inode, 0, &journal_file);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000228 if (retval) {
229 com_err(argv[0], retval, "while opening ext2 file");
230 return;
231 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000232 journal_source.where = JOURNAL_IS_INTERNAL;
233 journal_source.file = journal_file;
Theodore Ts'of3640932003-03-01 19:47:44 -0500234 } else {
235 char uuid[37];
236
237 uuid_unparse(es->s_journal_uuid, uuid);
238 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
239 if (!journal_fn)
240 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
241 if (!journal_fn) {
242 com_err(argv[0], 0, "filesystem has no journal");
243 return;
244 }
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400245 journal_fd = open(journal_fn, O_RDONLY, 0);
246 if (journal_fd < 0) {
247 com_err(argv[0], errno, "while opening %s for logdump",
248 journal_fn);
249 free(journal_fn);
250 return;
251 }
252 fprintf(out_file, "Using external journal found at %s\n",
253 journal_fn);
254 free(journal_fn);
255 journal_source.where = JOURNAL_IS_EXTERNAL;
256 journal_source.fd = journal_fd;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000257 }
258
259 dump_journal(argv[0], out_file, &journal_source);
260
261 if (journal_source.where == JOURNAL_IS_INTERNAL)
262 ext2fs_file_close(journal_file);
263 else
264 close(journal_fd);
265
266 if (out_file != stdout)
267 fclose(out_file);
268
269 return;
270}
271
272
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000273static int read_journal_block(const char *cmd, struct journal_source *source,
274 off_t offset, char *buf, int size,
275 unsigned int *got)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000276{
277 int retval;
278
279 if (source->where == JOURNAL_IS_EXTERNAL) {
Theodore Ts'o4bb0c042001-06-01 15:22:38 +0000280 if (lseek(source->fd, offset, SEEK_SET) < 0) {
281 retval = errno;
282 com_err(cmd, retval, "while seeking in reading journal");
283 return retval;
284 }
285 retval = read(source->fd, buf, size);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000286 if (retval >= 0) {
287 *got = retval;
288 retval = 0;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400289 } else
290 retval = errno;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000291 } else {
292 retval = ext2fs_file_lseek(source->file, offset,
293 EXT2_SEEK_SET, NULL);
294 if (retval) {
295 com_err(cmd, retval, "while seeking in reading journal");
296 return retval;
297 }
298
299 retval = ext2fs_file_read(source->file, buf, size, got);
300 }
301
302 if (retval)
303 com_err(cmd, retval, "while while reading journal");
Theodore Ts'o54434922003-12-07 01:28:50 -0500304 else if (*got != (unsigned int) size) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000305 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
306 retval = -1;
307 }
308
309 return retval;
310}
311
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000312static const char *type_to_name(int btype)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000313{
314 switch (btype) {
315 case JFS_DESCRIPTOR_BLOCK:
316 return "descriptor block";
317 case JFS_COMMIT_BLOCK:
318 return "commit block";
319 case JFS_SUPERBLOCK_V1:
320 return "V1 superblock";
321 case JFS_SUPERBLOCK_V2:
322 return "V2 superblock";
323 case JFS_REVOKE_BLOCK:
324 return "revoke table";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000325 }
326 return "unrecognised type";
327}
328
329
330static void dump_journal(char *cmdname, FILE *out_file,
331 struct journal_source *source)
332{
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400333 struct ext2_super_block *sb;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000334 char jsb_buffer[1024];
335 char buf[8192];
336 journal_superblock_t *jsb;
Theodore Ts'o54434922003-12-07 01:28:50 -0500337 unsigned int blocksize = 1024;
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000338 unsigned int got;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000339 int retval;
340 __u32 magic, sequence, blocktype;
341 journal_header_t *header;
342
343 tid_t transaction;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400344 unsigned int blocknr = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000345
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400346 /* First, check to see if there's an ext2 superblock header */
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000347 retval = read_journal_block(cmdname, source, 0,
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400348 buf, 2048, &got);
349 if (retval)
350 return;
351
352 jsb = (journal_superblock_t *) buf;
353 sb = (struct ext2_super_block *) (buf+1024);
Stephen Tweedieff63f262001-09-27 15:48:56 +0100354#ifdef ENABLE_SWAPFS
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400355 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
356 ext2fs_swap_super(sb);
Stephen Tweedieff63f262001-09-27 15:48:56 +0100357#endif
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400358
359 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
360 (sb->s_magic == EXT2_SUPER_MAGIC) &&
361 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
362 blocksize = EXT2_BLOCK_SIZE(sb);
363 blocknr = (blocksize == 1024) ? 2 : 1;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500364 uuid_unparse(sb->s_uuid, jsb_buffer);
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400365 fprintf(out_file, "Ext2 superblock header found.\n");
366 if (dump_all) {
367 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
368 fprintf(out_file, "\tblocksize=%d\n", blocksize);
369 fprintf(out_file, "\tjournal data size %ld\n",
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500370 (long) sb->s_blocks_count);
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400371 }
372 }
373
374 /* Next, read the journal superblock */
375
376 retval = read_journal_block(cmdname, source, blocknr*blocksize,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000377 jsb_buffer, 1024, &got);
378 if (retval)
379 return;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400380
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000381 jsb = (journal_superblock_t *) jsb_buffer;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400382 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
383 fprintf(out_file,
384 "Journal superblock magic number invalid!\n");
385 return;
386 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000387 blocksize = be32_to_cpu(jsb->s_blocksize);
388 transaction = be32_to_cpu(jsb->s_sequence);
389 blocknr = be32_to_cpu(jsb->s_start);
390
391 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
392 blocknr, transaction);
393
394 if (!blocknr)
395 /* Empty journal, nothing to do. */
396 return;
397
398 while (1) {
399 retval = read_journal_block(cmdname, source,
400 blocknr*blocksize, buf,
401 blocksize, &got);
402 if (retval || got != blocksize)
403 return;
404
405 header = (journal_header_t *) buf;
406
407 magic = be32_to_cpu(header->h_magic);
408 sequence = be32_to_cpu(header->h_sequence);
409 blocktype = be32_to_cpu(header->h_blocktype);
410
411 if (magic != JFS_MAGIC_NUMBER) {
412 fprintf (out_file, "No magic number at block %u: "
413 "end of journal.\n", blocknr);
414 return;
415 }
416
417 if (sequence != transaction) {
418 fprintf (out_file, "Found sequence %u (not %u) at "
419 "block %u: end of journal.\n",
420 sequence, transaction, blocknr);
421 return;
422 }
423
424 if (dump_descriptors) {
425 fprintf (out_file, "Found expected sequence %u, "
426 "type %u (%s) at block %u\n",
427 sequence, blocktype,
428 type_to_name(blocktype), blocknr);
429 }
430
431 switch (blocktype) {
432 case JFS_DESCRIPTOR_BLOCK:
433 dump_descriptor_block(out_file, source, buf, jsb,
434 &blocknr, blocksize,
435 transaction);
436 continue;
437
438 case JFS_COMMIT_BLOCK:
439 transaction++;
440 blocknr++;
441 WRAP(jsb, blocknr);
442 continue;
443
444 case JFS_REVOKE_BLOCK:
445 dump_revoke_block(out_file, buf, jsb,
446 blocknr, blocksize,
447 transaction);
448 blocknr++;
449 WRAP(jsb, blocknr);
450 continue;
451
452 default:
453 fprintf (out_file, "Unexpected block type %u at "
454 "block %u.\n", blocktype, blocknr);
455 return;
456 }
457 }
458}
459
460
461static void dump_descriptor_block(FILE *out_file,
462 struct journal_source *source,
463 char *buf,
464 journal_superblock_t *jsb,
465 unsigned int *blockp, int blocksize,
466 tid_t transaction)
467{
468 int offset;
469 char *tagp;
470 journal_block_tag_t *tag;
471 unsigned int blocknr;
472 __u32 tag_block;
473 __u32 tag_flags;
474
475
476 offset = sizeof(journal_header_t);
477 blocknr = *blockp;
478
479 if (dump_all)
480 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
481 "block %u:\n", transaction, blocknr);
482
483 ++blocknr;
484 WRAP(jsb, blocknr);
485
486 do {
487 /* Work out the location of the current tag, and skip to
488 * the next one... */
489 tagp = &buf[offset];
490 tag = (journal_block_tag_t *) tagp;
491 offset += sizeof(journal_block_tag_t);
492
493 /* ... and if we have gone too far, then we've reached the
494 end of this block. */
495 if (offset > blocksize)
496 break;
497
498 tag_block = be32_to_cpu(tag->t_blocknr);
499 tag_flags = be32_to_cpu(tag->t_flags);
500
501 if (!(tag_flags & JFS_FLAG_SAME_UUID))
502 offset += 16;
503
504 dump_metadata_block(out_file, source, jsb,
505 blocknr, tag_block, blocksize,
506 transaction);
507
508 ++blocknr;
509 WRAP(jsb, blocknr);
510
511 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
512
513 *blockp = blocknr;
514}
515
516
517static void dump_revoke_block(FILE *out_file, char *buf,
Theodore Ts'o54434922003-12-07 01:28:50 -0500518 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
519 unsigned int blocknr,
520 int blocksize EXT2FS_ATTR((unused)),
521 tid_t transaction)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000522{
523 int offset, max;
524 journal_revoke_header_t *header;
525 unsigned int *entry, rblock;
526
527 if (dump_all)
528 fprintf(out_file, "Dumping revoke block, sequence %u, at "
529 "block %u:\n", transaction, blocknr);
530
531 header = (journal_revoke_header_t *) buf;
532 offset = sizeof(journal_revoke_header_t);
533 max = be32_to_cpu(header->r_count);
534
535 while (offset < max) {
536 entry = (unsigned int *) (buf + offset);
537 rblock = be32_to_cpu(*entry);
538 if (dump_all || rblock == block_to_dump) {
539 fprintf(out_file, " Revoke FS block %u", rblock);
540 if (dump_all)
541 fprintf(out_file, "\n");
542 else
543 fprintf(out_file," at block %u, sequence %u\n",
544 blocknr, transaction);
545 }
546 offset += 4;
547 }
548}
549
550
551static void show_extent(FILE *out_file, int start_extent, int end_extent,
552 __u32 first_block)
553{
554 if (start_extent >= 0 && first_block != 0)
555 fprintf(out_file, "(%d+%u): %u ",
556 start_extent, end_extent-start_extent, first_block);
557}
558
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000559static void show_indirect(FILE *out_file, const char *name, __u32 where)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000560{
561 if (where)
562 fprintf(out_file, "(%s): %u ", name, where);
563}
564
565
566static void dump_metadata_block(FILE *out_file, struct journal_source *source,
Theodore Ts'o54434922003-12-07 01:28:50 -0500567 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000568 unsigned int log_blocknr,
569 unsigned int fs_blocknr,
570 int blocksize,
571 tid_t transaction)
572{
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000573 unsigned int got;
574 int retval;
575 char buf[8192];
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000576
577 if (!(dump_all
578 || (fs_blocknr == block_to_dump)
579 || (fs_blocknr == inode_block_to_dump)
580 || (fs_blocknr == bitmap_to_dump)))
581 return;
582
583 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
584 if (!dump_all)
585 fprintf(out_file, "sequence %u, ", transaction);
586 fprintf(out_file, "journal block %u\n", log_blocknr);
587
588 /* There are two major special cases to parse:
589 *
590 * If this block is a block
591 * bitmap block, we need to give it special treatment so that we
592 * can log any allocates and deallocates which affect the
593 * block_to_dump query block.
594 *
595 * If the block is an inode block for the inode being searched
596 * for, then we need to dump the contents of that inode
597 * structure symbolically.
598 */
599
600 if (!(dump_contents && dump_all)
601 && fs_blocknr != block_to_dump
602 && fs_blocknr != bitmap_to_dump
603 && fs_blocknr != inode_block_to_dump)
604 return;
605
606 retval = read_journal_block("logdump", source,
607 blocksize * log_blocknr,
608 buf, blocksize, &got);
609 if (retval)
610 return;
611
612 if (fs_blocknr == bitmap_to_dump) {
613 struct ext2_super_block *super;
614 int offset;
615
616 super = current_fs->super;
617 offset = ((fs_blocknr - super->s_first_data_block) %
618 super->s_blocks_per_group);
619
620 fprintf(out_file, " (block bitmap for block %u: "
621 "block is %s)\n",
622 block_to_dump,
623 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
624 }
625
626 if (fs_blocknr == inode_block_to_dump) {
627 struct ext2_inode *inode;
628 int first, prev, this, start_extent, i;
629
630 fprintf(out_file, " (inode block for inode %u):\n",
631 inode_to_dump);
632
633 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
634 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
635
636 /* Dump out the direct/indirect blocks here:
637 * internal_dump_inode can only dump them from the main
638 * on-disk inode, not from the journaled copy of the
639 * inode. */
640
641 fprintf (out_file, " Blocks: ");
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000642 first = prev = start_extent = -1;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000643
644 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
645 this = inode->i_block[i];
646 if (start_extent >= 0 && this == prev+1) {
647 prev = this;
648 continue;
649 } else {
650 show_extent(out_file, start_extent, i, first);
651 start_extent = i;
652 first = prev = this;
653 }
654 }
655 show_extent(out_file, start_extent, i, first);
656 show_indirect(out_file, "IND", inode->i_block[i++]);
657 show_indirect(out_file, "DIND", inode->i_block[i++]);
658 show_indirect(out_file, "TIND", inode->i_block[i++]);
659
660 fprintf(out_file, "\n");
661 }
662
663 if (dump_contents)
664 do_hexdump(out_file, buf, blocksize);
665
666}
667
668static void do_hexdump (FILE *out_file, char *buf, int blocksize)
669{
670 int i,j;
671 int *intp;
672 char *charp;
673 unsigned char c;
674
675 intp = (int *) buf;
676 charp = (char *) buf;
677
678 for (i=0; i<blocksize; i+=16) {
679 fprintf(out_file, " %04x: ", i);
680 for (j=0; j<16; j+=4)
681 fprintf(out_file, "%08x ", *intp++);
682 for (j=0; j<16; j++) {
683 c = *charp++;
684 if (c < ' ' || c >= 127)
685 c = '.';
686 fprintf(out_file, "%c", c);
687 }
688 fprintf(out_file, "\n");
689 }
690}
691