blob: df11c16269901b3d9f9894954be0d9459a9c4ead [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"
Theodore Ts'of3640932003-03-01 19:47:44 -050036#include "blkid/blkid.h"
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000037#include "jfs_user.h"
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -050038#include <uuid/uuid.h>
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000039
40enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
41
42int dump_all, dump_contents, dump_descriptors;
43unsigned int block_to_dump, group_to_dump, bitmap_to_dump;
44unsigned int inode_block_to_dump, inode_offset_to_dump, bitmap_to_dump;
45ext2_ino_t inode_to_dump;
46
47struct journal_source
48{
49 enum journal_location where;
50 int fd;
51 ext2_file_t file;
52};
53
54static void dump_journal(char *, FILE *, struct journal_source *);
55
56static void dump_descriptor_block(FILE *, struct journal_source *,
57 char *, journal_superblock_t *,
58 unsigned int *, int, tid_t);
59
60static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
61 unsigned int, int, tid_t);
62
63static void dump_metadata_block(FILE *, struct journal_source *,
64 journal_superblock_t*,
65 unsigned int, unsigned int, int, tid_t);
66
67static 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
74
75void do_logdump(int argc, char **argv)
76{
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000077 int c;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000078 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;
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'oda81e3f2001-03-29 20:49:58 +000095 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
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
186 if (block_to_dump != -1 && current_fs != NULL) {
187 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'oe1018ee2002-01-03 04:55:25 -0500208 if (debugfs_read_inode(journal_inum, &journal_inode, argv[0]))
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000209 return;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400210
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000211 retval = ext2fs_file_open(current_fs, journal_inum,
212 0, &journal_file);
213 if (retval) {
214 com_err(argv[0], retval, "while opening ext2 file");
215 return;
216 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000217 journal_source.where = JOURNAL_IS_INTERNAL;
218 journal_source.file = journal_file;
Theodore Ts'of3640932003-03-01 19:47:44 -0500219 } else {
220 char uuid[37];
221
222 uuid_unparse(es->s_journal_uuid, uuid);
223 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
224 if (!journal_fn)
225 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
226 if (!journal_fn) {
227 com_err(argv[0], 0, "filesystem has no journal");
228 return;
229 }
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400230 journal_fd = open(journal_fn, O_RDONLY, 0);
231 if (journal_fd < 0) {
232 com_err(argv[0], errno, "while opening %s for logdump",
233 journal_fn);
234 free(journal_fn);
235 return;
236 }
237 fprintf(out_file, "Using external journal found at %s\n",
238 journal_fn);
239 free(journal_fn);
240 journal_source.where = JOURNAL_IS_EXTERNAL;
241 journal_source.fd = journal_fd;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000242 }
243
244 dump_journal(argv[0], out_file, &journal_source);
245
246 if (journal_source.where == JOURNAL_IS_INTERNAL)
247 ext2fs_file_close(journal_file);
248 else
249 close(journal_fd);
250
251 if (out_file != stdout)
252 fclose(out_file);
253
254 return;
255}
256
257
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000258static int read_journal_block(const char *cmd, struct journal_source *source,
259 off_t offset, char *buf, int size,
260 unsigned int *got)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000261{
262 int retval;
263
264 if (source->where == JOURNAL_IS_EXTERNAL) {
Theodore Ts'o4bb0c042001-06-01 15:22:38 +0000265 if (lseek(source->fd, offset, SEEK_SET) < 0) {
266 retval = errno;
267 com_err(cmd, retval, "while seeking in reading journal");
268 return retval;
269 }
270 retval = read(source->fd, buf, size);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000271 if (retval >= 0) {
272 *got = retval;
273 retval = 0;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400274 } else
275 retval = errno;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000276 } else {
277 retval = ext2fs_file_lseek(source->file, offset,
278 EXT2_SEEK_SET, NULL);
279 if (retval) {
280 com_err(cmd, retval, "while seeking in reading journal");
281 return retval;
282 }
283
284 retval = ext2fs_file_read(source->file, buf, size, got);
285 }
286
287 if (retval)
288 com_err(cmd, retval, "while while reading journal");
289 else if (*got != size) {
290 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
291 retval = -1;
292 }
293
294 return retval;
295}
296
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000297static const char *type_to_name(int btype)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000298{
299 switch (btype) {
300 case JFS_DESCRIPTOR_BLOCK:
301 return "descriptor block";
302 case JFS_COMMIT_BLOCK:
303 return "commit block";
304 case JFS_SUPERBLOCK_V1:
305 return "V1 superblock";
306 case JFS_SUPERBLOCK_V2:
307 return "V2 superblock";
308 case JFS_REVOKE_BLOCK:
309 return "revoke table";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000310 }
311 return "unrecognised type";
312}
313
314
315static void dump_journal(char *cmdname, FILE *out_file,
316 struct journal_source *source)
317{
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400318 struct ext2_super_block *sb;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000319 char jsb_buffer[1024];
320 char buf[8192];
321 journal_superblock_t *jsb;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400322 int blocksize = 1024;
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000323 unsigned int got;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000324 int retval;
325 __u32 magic, sequence, blocktype;
326 journal_header_t *header;
327
328 tid_t transaction;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400329 unsigned int blocknr = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000330
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400331 /* First, check to see if there's an ext2 superblock header */
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000332 retval = read_journal_block(cmdname, source, 0,
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400333 buf, 2048, &got);
334 if (retval)
335 return;
336
337 jsb = (journal_superblock_t *) buf;
338 sb = (struct ext2_super_block *) (buf+1024);
Stephen Tweedieff63f262001-09-27 15:48:56 +0100339#ifdef ENABLE_SWAPFS
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400340 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
341 ext2fs_swap_super(sb);
Stephen Tweedieff63f262001-09-27 15:48:56 +0100342#endif
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400343
344 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
345 (sb->s_magic == EXT2_SUPER_MAGIC) &&
346 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
347 blocksize = EXT2_BLOCK_SIZE(sb);
348 blocknr = (blocksize == 1024) ? 2 : 1;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500349 uuid_unparse(sb->s_uuid, jsb_buffer);
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400350 fprintf(out_file, "Ext2 superblock header found.\n");
351 if (dump_all) {
352 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
353 fprintf(out_file, "\tblocksize=%d\n", blocksize);
354 fprintf(out_file, "\tjournal data size %ld\n",
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500355 (long) sb->s_blocks_count);
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400356 }
357 }
358
359 /* Next, read the journal superblock */
360
361 retval = read_journal_block(cmdname, source, blocknr*blocksize,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000362 jsb_buffer, 1024, &got);
363 if (retval)
364 return;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400365
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000366 jsb = (journal_superblock_t *) jsb_buffer;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400367 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
368 fprintf(out_file,
369 "Journal superblock magic number invalid!\n");
370 return;
371 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000372 blocksize = be32_to_cpu(jsb->s_blocksize);
373 transaction = be32_to_cpu(jsb->s_sequence);
374 blocknr = be32_to_cpu(jsb->s_start);
375
376 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
377 blocknr, transaction);
378
379 if (!blocknr)
380 /* Empty journal, nothing to do. */
381 return;
382
383 while (1) {
384 retval = read_journal_block(cmdname, source,
385 blocknr*blocksize, buf,
386 blocksize, &got);
387 if (retval || got != blocksize)
388 return;
389
390 header = (journal_header_t *) buf;
391
392 magic = be32_to_cpu(header->h_magic);
393 sequence = be32_to_cpu(header->h_sequence);
394 blocktype = be32_to_cpu(header->h_blocktype);
395
396 if (magic != JFS_MAGIC_NUMBER) {
397 fprintf (out_file, "No magic number at block %u: "
398 "end of journal.\n", blocknr);
399 return;
400 }
401
402 if (sequence != transaction) {
403 fprintf (out_file, "Found sequence %u (not %u) at "
404 "block %u: end of journal.\n",
405 sequence, transaction, blocknr);
406 return;
407 }
408
409 if (dump_descriptors) {
410 fprintf (out_file, "Found expected sequence %u, "
411 "type %u (%s) at block %u\n",
412 sequence, blocktype,
413 type_to_name(blocktype), blocknr);
414 }
415
416 switch (blocktype) {
417 case JFS_DESCRIPTOR_BLOCK:
418 dump_descriptor_block(out_file, source, buf, jsb,
419 &blocknr, blocksize,
420 transaction);
421 continue;
422
423 case JFS_COMMIT_BLOCK:
424 transaction++;
425 blocknr++;
426 WRAP(jsb, blocknr);
427 continue;
428
429 case JFS_REVOKE_BLOCK:
430 dump_revoke_block(out_file, buf, jsb,
431 blocknr, blocksize,
432 transaction);
433 blocknr++;
434 WRAP(jsb, blocknr);
435 continue;
436
437 default:
438 fprintf (out_file, "Unexpected block type %u at "
439 "block %u.\n", blocktype, blocknr);
440 return;
441 }
442 }
443}
444
445
446static void dump_descriptor_block(FILE *out_file,
447 struct journal_source *source,
448 char *buf,
449 journal_superblock_t *jsb,
450 unsigned int *blockp, int blocksize,
451 tid_t transaction)
452{
453 int offset;
454 char *tagp;
455 journal_block_tag_t *tag;
456 unsigned int blocknr;
457 __u32 tag_block;
458 __u32 tag_flags;
459
460
461 offset = sizeof(journal_header_t);
462 blocknr = *blockp;
463
464 if (dump_all)
465 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
466 "block %u:\n", transaction, blocknr);
467
468 ++blocknr;
469 WRAP(jsb, blocknr);
470
471 do {
472 /* Work out the location of the current tag, and skip to
473 * the next one... */
474 tagp = &buf[offset];
475 tag = (journal_block_tag_t *) tagp;
476 offset += sizeof(journal_block_tag_t);
477
478 /* ... and if we have gone too far, then we've reached the
479 end of this block. */
480 if (offset > blocksize)
481 break;
482
483 tag_block = be32_to_cpu(tag->t_blocknr);
484 tag_flags = be32_to_cpu(tag->t_flags);
485
486 if (!(tag_flags & JFS_FLAG_SAME_UUID))
487 offset += 16;
488
489 dump_metadata_block(out_file, source, jsb,
490 blocknr, tag_block, blocksize,
491 transaction);
492
493 ++blocknr;
494 WRAP(jsb, blocknr);
495
496 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
497
498 *blockp = blocknr;
499}
500
501
502static void dump_revoke_block(FILE *out_file, char *buf,
503 journal_superblock_t *jsb,
504 unsigned int blocknr, int blocksize,
505 tid_t transaction)
506{
507 int offset, max;
508 journal_revoke_header_t *header;
509 unsigned int *entry, rblock;
510
511 if (dump_all)
512 fprintf(out_file, "Dumping revoke block, sequence %u, at "
513 "block %u:\n", transaction, blocknr);
514
515 header = (journal_revoke_header_t *) buf;
516 offset = sizeof(journal_revoke_header_t);
517 max = be32_to_cpu(header->r_count);
518
519 while (offset < max) {
520 entry = (unsigned int *) (buf + offset);
521 rblock = be32_to_cpu(*entry);
522 if (dump_all || rblock == block_to_dump) {
523 fprintf(out_file, " Revoke FS block %u", rblock);
524 if (dump_all)
525 fprintf(out_file, "\n");
526 else
527 fprintf(out_file," at block %u, sequence %u\n",
528 blocknr, transaction);
529 }
530 offset += 4;
531 }
532}
533
534
535static void show_extent(FILE *out_file, int start_extent, int end_extent,
536 __u32 first_block)
537{
538 if (start_extent >= 0 && first_block != 0)
539 fprintf(out_file, "(%d+%u): %u ",
540 start_extent, end_extent-start_extent, first_block);
541}
542
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000543static void show_indirect(FILE *out_file, const char *name, __u32 where)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000544{
545 if (where)
546 fprintf(out_file, "(%s): %u ", name, where);
547}
548
549
550static void dump_metadata_block(FILE *out_file, struct journal_source *source,
551 journal_superblock_t *jsb,
552 unsigned int log_blocknr,
553 unsigned int fs_blocknr,
554 int blocksize,
555 tid_t transaction)
556{
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000557 unsigned int got;
558 int retval;
559 char buf[8192];
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000560
561 if (!(dump_all
562 || (fs_blocknr == block_to_dump)
563 || (fs_blocknr == inode_block_to_dump)
564 || (fs_blocknr == bitmap_to_dump)))
565 return;
566
567 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
568 if (!dump_all)
569 fprintf(out_file, "sequence %u, ", transaction);
570 fprintf(out_file, "journal block %u\n", log_blocknr);
571
572 /* There are two major special cases to parse:
573 *
574 * If this block is a block
575 * bitmap block, we need to give it special treatment so that we
576 * can log any allocates and deallocates which affect the
577 * block_to_dump query block.
578 *
579 * If the block is an inode block for the inode being searched
580 * for, then we need to dump the contents of that inode
581 * structure symbolically.
582 */
583
584 if (!(dump_contents && dump_all)
585 && fs_blocknr != block_to_dump
586 && fs_blocknr != bitmap_to_dump
587 && fs_blocknr != inode_block_to_dump)
588 return;
589
590 retval = read_journal_block("logdump", source,
591 blocksize * log_blocknr,
592 buf, blocksize, &got);
593 if (retval)
594 return;
595
596 if (fs_blocknr == bitmap_to_dump) {
597 struct ext2_super_block *super;
598 int offset;
599
600 super = current_fs->super;
601 offset = ((fs_blocknr - super->s_first_data_block) %
602 super->s_blocks_per_group);
603
604 fprintf(out_file, " (block bitmap for block %u: "
605 "block is %s)\n",
606 block_to_dump,
607 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
608 }
609
610 if (fs_blocknr == inode_block_to_dump) {
611 struct ext2_inode *inode;
612 int first, prev, this, start_extent, i;
613
614 fprintf(out_file, " (inode block for inode %u):\n",
615 inode_to_dump);
616
617 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
618 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
619
620 /* Dump out the direct/indirect blocks here:
621 * internal_dump_inode can only dump them from the main
622 * on-disk inode, not from the journaled copy of the
623 * inode. */
624
625 fprintf (out_file, " Blocks: ");
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000626 first = prev = start_extent = -1;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000627
628 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
629 this = inode->i_block[i];
630 if (start_extent >= 0 && this == prev+1) {
631 prev = this;
632 continue;
633 } else {
634 show_extent(out_file, start_extent, i, first);
635 start_extent = i;
636 first = prev = this;
637 }
638 }
639 show_extent(out_file, start_extent, i, first);
640 show_indirect(out_file, "IND", inode->i_block[i++]);
641 show_indirect(out_file, "DIND", inode->i_block[i++]);
642 show_indirect(out_file, "TIND", inode->i_block[i++]);
643
644 fprintf(out_file, "\n");
645 }
646
647 if (dump_contents)
648 do_hexdump(out_file, buf, blocksize);
649
650}
651
652static void do_hexdump (FILE *out_file, char *buf, int blocksize)
653{
654 int i,j;
655 int *intp;
656 char *charp;
657 unsigned char c;
658
659 intp = (int *) buf;
660 charp = (char *) buf;
661
662 for (i=0; i<blocksize; i+=16) {
663 fprintf(out_file, " %04x: ", i);
664 for (j=0; j<16; j+=4)
665 fprintf(out_file, "%08x ", *intp++);
666 for (j=0; j<16; j++) {
667 c = *charp++;
668 if (c < ' ' || c >= 127)
669 c = '.';
670 fprintf(out_file, "%c", c);
671 }
672 fprintf(out_file, "\n");
673 }
674}
675