blob: 74bbc8d5b79be527ae383cc4c98b58e772ba35ed [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
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000073void 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;
Theodore Ts'oa435ec32003-08-21 00:40:26 -040083 int use_sb = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000084 ext2_ino_t journal_inum;
85 struct ext2_inode journal_inode;
86 ext2_file_t journal_file;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000087 char *tmp;
Theodore Ts'o5e4f0702001-06-01 15:36:05 +000088 struct journal_source journal_source;
Theodore Ts'of3640932003-03-01 19:47:44 -050089 struct ext2_super_block *es = NULL;
90
Theodore Ts'o5e4f0702001-06-01 15:36:05 +000091 journal_source.where = 0;
92 journal_source.fd = 0;
93 journal_source.file = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000094 dump_all = 0;
95 dump_contents = 0;
96 dump_descriptors = 1;
Theodore Ts'o54434922003-12-07 01:28:50 -050097 block_to_dump = ANY_BLOCK;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000098 bitmap_to_dump = -1;
Theodore Ts'o54434922003-12-07 01:28:50 -050099 inode_block_to_dump = ANY_BLOCK;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000100 inode_to_dump = -1;
101
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400102 reset_getopt();
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400103 while ((c = getopt (argc, argv, "ab:ci:f:s")) != EOF) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000104 switch (c) {
105 case 'a':
106 dump_all++;
107 break;
108 case 'b':
109 block_to_dump = strtoul(optarg, &tmp, 0);
110 if (*tmp) {
111 com_err(argv[0], 0,
112 "Bad block number - %s", optarg);
113 return;
114 }
115 dump_descriptors = 0;
116 break;
117 case 'c':
118 dump_contents++;
119 break;
120 case 'f':
121 journal_fn = optarg;
122 break;
123 case 'i':
124 inode_spec = optarg;
125 dump_descriptors = 0;
126 break;
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400127 case 's':
128 use_sb++;
129 break;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000130 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500131 goto print_usage;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000132 }
133 }
134 if (optind != argc && optind != argc-1) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500135 goto print_usage;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000136 }
137
Theodore Ts'of3640932003-03-01 19:47:44 -0500138 if (current_fs)
139 es = current_fs->super;
140
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000141 if (inode_spec) {
142 int inode_group, group_offset, inodes_per_block;
Theodore Ts'of3640932003-03-01 19:47:44 -0500143
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000144 if (check_fs_open(argv[0]))
145 return;
146
147 inode_to_dump = string_to_inode(inode_spec);
148 if (!inode_to_dump)
149 return;
150
151 inode_group = ((inode_to_dump - 1)
Theodore Ts'of3640932003-03-01 19:47:44 -0500152 / es->s_inodes_per_group);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000153 group_offset = ((inode_to_dump - 1)
Theodore Ts'of3640932003-03-01 19:47:44 -0500154 % es->s_inodes_per_group);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000155 inodes_per_block = (current_fs->blocksize
156 / sizeof(struct ext2_inode));
157
158 inode_block_to_dump =
159 current_fs->group_desc[inode_group].bg_inode_table +
160 (group_offset / inodes_per_block);
161 inode_offset_to_dump = ((group_offset % inodes_per_block)
162 * sizeof(struct ext2_inode));
163 printf("Inode %u is at group %u, block %u, offset %u\n",
164 inode_to_dump, inode_group,
165 inode_block_to_dump, inode_offset_to_dump);
166 }
167
168 if (optind == argc) {
169 out_file = stdout;
170 } else {
171 out_fn = argv[optind];
172 out_file = fopen(out_fn, "w");
173 if (!out_file < 0) {
174 com_err(argv[0], errno, "while opening %s for logdump",
175 out_fn);
176 return;
177 }
178 }
179
Theodore Ts'o54434922003-12-07 01:28:50 -0500180 if (block_to_dump != ANY_BLOCK && current_fs != NULL) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000181 group_to_dump = ((block_to_dump -
Theodore Ts'of3640932003-03-01 19:47:44 -0500182 es->s_first_data_block)
183 / es->s_blocks_per_group);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000184 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
185 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000186
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400187 if (!journal_fn && check_fs_open(argv[0]))
188 return;
189
190 if (journal_fn) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000191 /* 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;
Theodore Ts'of3640932003-03-01 19:47:44 -0500201 } else if ((journal_inum = es->s_journal_inum)) {
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400202 if (use_sb) {
203 if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
204 com_err(argv[0], 0,
205 "no journal backup in super block\n");
206 return;
207 }
208 memset(&journal_inode, 0, sizeof(struct ext2_inode));
209 memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
210 EXT2_N_BLOCKS*4);
211 journal_inode.i_size = es->s_jnl_blocks[16];
212 journal_inode.i_links_count = 1;
213 journal_inode.i_mode = LINUX_S_IFREG | 0600;
214 } else {
215 if (debugfs_read_inode(journal_inum, &journal_inode,
216 argv[0]))
217 return;
218 }
219
220 retval = ext2fs_file_open2(current_fs, journal_inum,
221 &journal_inode, 0, &journal_file);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000222 if (retval) {
223 com_err(argv[0], retval, "while opening ext2 file");
224 return;
225 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000226 journal_source.where = JOURNAL_IS_INTERNAL;
227 journal_source.file = journal_file;
Theodore Ts'of3640932003-03-01 19:47:44 -0500228 } else {
229 char uuid[37];
230
231 uuid_unparse(es->s_journal_uuid, uuid);
232 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
233 if (!journal_fn)
234 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
235 if (!journal_fn) {
236 com_err(argv[0], 0, "filesystem has no journal");
237 return;
238 }
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400239 journal_fd = open(journal_fn, O_RDONLY, 0);
240 if (journal_fd < 0) {
241 com_err(argv[0], errno, "while opening %s for logdump",
242 journal_fn);
243 free(journal_fn);
244 return;
245 }
246 fprintf(out_file, "Using external journal found at %s\n",
247 journal_fn);
248 free(journal_fn);
249 journal_source.where = JOURNAL_IS_EXTERNAL;
250 journal_source.fd = journal_fd;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000251 }
252
253 dump_journal(argv[0], out_file, &journal_source);
254
255 if (journal_source.where == JOURNAL_IS_INTERNAL)
256 ext2fs_file_close(journal_file);
257 else
258 close(journal_fd);
259
260 if (out_file != stdout)
261 fclose(out_file);
262
263 return;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500264
265print_usage:
266 fprintf(stderr, "%s: Usage: logdump [-ac] [-b<block>] [-i<inode>]\n\t"
267 "[-f<journal_file>] [output_file]\n", argv[0]);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000268}
269
270
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000271static int read_journal_block(const char *cmd, struct journal_source *source,
272 off_t offset, char *buf, int size,
273 unsigned int *got)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000274{
275 int retval;
276
277 if (source->where == JOURNAL_IS_EXTERNAL) {
Theodore Ts'o4bb0c042001-06-01 15:22:38 +0000278 if (lseek(source->fd, offset, SEEK_SET) < 0) {
279 retval = errno;
280 com_err(cmd, retval, "while seeking in reading journal");
281 return retval;
282 }
283 retval = read(source->fd, buf, size);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000284 if (retval >= 0) {
285 *got = retval;
286 retval = 0;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400287 } else
288 retval = errno;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000289 } else {
290 retval = ext2fs_file_lseek(source->file, offset,
291 EXT2_SEEK_SET, NULL);
292 if (retval) {
293 com_err(cmd, retval, "while seeking in reading journal");
294 return retval;
295 }
296
297 retval = ext2fs_file_read(source->file, buf, size, got);
298 }
299
300 if (retval)
301 com_err(cmd, retval, "while while reading journal");
Theodore Ts'o54434922003-12-07 01:28:50 -0500302 else if (*got != (unsigned int) size) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000303 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
304 retval = -1;
305 }
306
307 return retval;
308}
309
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000310static const char *type_to_name(int btype)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000311{
312 switch (btype) {
313 case JFS_DESCRIPTOR_BLOCK:
314 return "descriptor block";
315 case JFS_COMMIT_BLOCK:
316 return "commit block";
317 case JFS_SUPERBLOCK_V1:
318 return "V1 superblock";
319 case JFS_SUPERBLOCK_V2:
320 return "V2 superblock";
321 case JFS_REVOKE_BLOCK:
322 return "revoke table";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000323 }
324 return "unrecognised type";
325}
326
327
328static void dump_journal(char *cmdname, FILE *out_file,
329 struct journal_source *source)
330{
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400331 struct ext2_super_block *sb;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000332 char jsb_buffer[1024];
333 char buf[8192];
334 journal_superblock_t *jsb;
Theodore Ts'o54434922003-12-07 01:28:50 -0500335 unsigned int blocksize = 1024;
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000336 unsigned int got;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000337 int retval;
338 __u32 magic, sequence, blocktype;
339 journal_header_t *header;
340
341 tid_t transaction;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400342 unsigned int blocknr = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000343
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400344 /* First, check to see if there's an ext2 superblock header */
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000345 retval = read_journal_block(cmdname, source, 0,
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400346 buf, 2048, &got);
347 if (retval)
348 return;
349
350 jsb = (journal_superblock_t *) buf;
351 sb = (struct ext2_super_block *) (buf+1024);
Stephen Tweedieff63f262001-09-27 15:48:56 +0100352#ifdef ENABLE_SWAPFS
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400353 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
354 ext2fs_swap_super(sb);
Stephen Tweedieff63f262001-09-27 15:48:56 +0100355#endif
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400356
357 if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
358 (sb->s_magic == EXT2_SUPER_MAGIC) &&
359 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
360 blocksize = EXT2_BLOCK_SIZE(sb);
361 blocknr = (blocksize == 1024) ? 2 : 1;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500362 uuid_unparse(sb->s_uuid, jsb_buffer);
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400363 fprintf(out_file, "Ext2 superblock header found.\n");
364 if (dump_all) {
365 fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
366 fprintf(out_file, "\tblocksize=%d\n", blocksize);
367 fprintf(out_file, "\tjournal data size %ld\n",
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500368 (long) sb->s_blocks_count);
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400369 }
370 }
371
372 /* Next, read the journal superblock */
373
374 retval = read_journal_block(cmdname, source, blocknr*blocksize,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000375 jsb_buffer, 1024, &got);
376 if (retval)
377 return;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400378
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000379 jsb = (journal_superblock_t *) jsb_buffer;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400380 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
381 fprintf(out_file,
382 "Journal superblock magic number invalid!\n");
383 return;
384 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000385 blocksize = be32_to_cpu(jsb->s_blocksize);
386 transaction = be32_to_cpu(jsb->s_sequence);
387 blocknr = be32_to_cpu(jsb->s_start);
388
389 fprintf(out_file, "Journal starts at block %u, transaction %u\n",
390 blocknr, transaction);
391
392 if (!blocknr)
393 /* Empty journal, nothing to do. */
394 return;
395
396 while (1) {
397 retval = read_journal_block(cmdname, source,
398 blocknr*blocksize, buf,
399 blocksize, &got);
400 if (retval || got != blocksize)
401 return;
402
403 header = (journal_header_t *) buf;
404
405 magic = be32_to_cpu(header->h_magic);
406 sequence = be32_to_cpu(header->h_sequence);
407 blocktype = be32_to_cpu(header->h_blocktype);
408
409 if (magic != JFS_MAGIC_NUMBER) {
410 fprintf (out_file, "No magic number at block %u: "
411 "end of journal.\n", blocknr);
412 return;
413 }
414
415 if (sequence != transaction) {
416 fprintf (out_file, "Found sequence %u (not %u) at "
417 "block %u: end of journal.\n",
418 sequence, transaction, blocknr);
419 return;
420 }
421
422 if (dump_descriptors) {
423 fprintf (out_file, "Found expected sequence %u, "
424 "type %u (%s) at block %u\n",
425 sequence, blocktype,
426 type_to_name(blocktype), blocknr);
427 }
428
429 switch (blocktype) {
430 case JFS_DESCRIPTOR_BLOCK:
431 dump_descriptor_block(out_file, source, buf, jsb,
432 &blocknr, blocksize,
433 transaction);
434 continue;
435
436 case JFS_COMMIT_BLOCK:
437 transaction++;
438 blocknr++;
439 WRAP(jsb, blocknr);
440 continue;
441
442 case JFS_REVOKE_BLOCK:
443 dump_revoke_block(out_file, buf, jsb,
444 blocknr, blocksize,
445 transaction);
446 blocknr++;
447 WRAP(jsb, blocknr);
448 continue;
449
450 default:
451 fprintf (out_file, "Unexpected block type %u at "
452 "block %u.\n", blocktype, blocknr);
453 return;
454 }
455 }
456}
457
458
459static void dump_descriptor_block(FILE *out_file,
460 struct journal_source *source,
461 char *buf,
462 journal_superblock_t *jsb,
463 unsigned int *blockp, int blocksize,
464 tid_t transaction)
465{
466 int offset;
467 char *tagp;
468 journal_block_tag_t *tag;
469 unsigned int blocknr;
470 __u32 tag_block;
471 __u32 tag_flags;
472
473
474 offset = sizeof(journal_header_t);
475 blocknr = *blockp;
476
477 if (dump_all)
478 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
479 "block %u:\n", transaction, blocknr);
480
481 ++blocknr;
482 WRAP(jsb, blocknr);
483
484 do {
485 /* Work out the location of the current tag, and skip to
486 * the next one... */
487 tagp = &buf[offset];
488 tag = (journal_block_tag_t *) tagp;
489 offset += sizeof(journal_block_tag_t);
490
491 /* ... and if we have gone too far, then we've reached the
492 end of this block. */
493 if (offset > blocksize)
494 break;
495
496 tag_block = be32_to_cpu(tag->t_blocknr);
497 tag_flags = be32_to_cpu(tag->t_flags);
498
499 if (!(tag_flags & JFS_FLAG_SAME_UUID))
500 offset += 16;
501
502 dump_metadata_block(out_file, source, jsb,
503 blocknr, tag_block, blocksize,
504 transaction);
505
506 ++blocknr;
507 WRAP(jsb, blocknr);
508
509 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
510
511 *blockp = blocknr;
512}
513
514
515static void dump_revoke_block(FILE *out_file, char *buf,
Theodore Ts'o54434922003-12-07 01:28:50 -0500516 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
517 unsigned int blocknr,
518 int blocksize EXT2FS_ATTR((unused)),
519 tid_t transaction)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000520{
521 int offset, max;
522 journal_revoke_header_t *header;
523 unsigned int *entry, rblock;
524
525 if (dump_all)
526 fprintf(out_file, "Dumping revoke block, sequence %u, at "
527 "block %u:\n", transaction, blocknr);
528
529 header = (journal_revoke_header_t *) buf;
530 offset = sizeof(journal_revoke_header_t);
531 max = be32_to_cpu(header->r_count);
532
533 while (offset < max) {
534 entry = (unsigned int *) (buf + offset);
535 rblock = be32_to_cpu(*entry);
536 if (dump_all || rblock == block_to_dump) {
537 fprintf(out_file, " Revoke FS block %u", rblock);
538 if (dump_all)
539 fprintf(out_file, "\n");
540 else
541 fprintf(out_file," at block %u, sequence %u\n",
542 blocknr, transaction);
543 }
544 offset += 4;
545 }
546}
547
548
549static void show_extent(FILE *out_file, int start_extent, int end_extent,
550 __u32 first_block)
551{
552 if (start_extent >= 0 && first_block != 0)
553 fprintf(out_file, "(%d+%u): %u ",
554 start_extent, end_extent-start_extent, first_block);
555}
556
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000557static void show_indirect(FILE *out_file, const char *name, __u32 where)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000558{
559 if (where)
560 fprintf(out_file, "(%s): %u ", name, where);
561}
562
563
564static void dump_metadata_block(FILE *out_file, struct journal_source *source,
Theodore Ts'o54434922003-12-07 01:28:50 -0500565 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000566 unsigned int log_blocknr,
567 unsigned int fs_blocknr,
568 int blocksize,
569 tid_t transaction)
570{
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000571 unsigned int got;
572 int retval;
573 char buf[8192];
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000574
575 if (!(dump_all
576 || (fs_blocknr == block_to_dump)
577 || (fs_blocknr == inode_block_to_dump)
578 || (fs_blocknr == bitmap_to_dump)))
579 return;
580
581 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
582 if (!dump_all)
583 fprintf(out_file, "sequence %u, ", transaction);
584 fprintf(out_file, "journal block %u\n", log_blocknr);
585
586 /* There are two major special cases to parse:
587 *
588 * If this block is a block
589 * bitmap block, we need to give it special treatment so that we
590 * can log any allocates and deallocates which affect the
591 * block_to_dump query block.
592 *
593 * If the block is an inode block for the inode being searched
594 * for, then we need to dump the contents of that inode
595 * structure symbolically.
596 */
597
598 if (!(dump_contents && dump_all)
599 && fs_blocknr != block_to_dump
600 && fs_blocknr != bitmap_to_dump
601 && fs_blocknr != inode_block_to_dump)
602 return;
603
604 retval = read_journal_block("logdump", source,
605 blocksize * log_blocknr,
606 buf, blocksize, &got);
607 if (retval)
608 return;
609
610 if (fs_blocknr == bitmap_to_dump) {
611 struct ext2_super_block *super;
612 int offset;
613
614 super = current_fs->super;
615 offset = ((fs_blocknr - super->s_first_data_block) %
616 super->s_blocks_per_group);
617
618 fprintf(out_file, " (block bitmap for block %u: "
619 "block is %s)\n",
620 block_to_dump,
621 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
622 }
623
624 if (fs_blocknr == inode_block_to_dump) {
625 struct ext2_inode *inode;
626 int first, prev, this, start_extent, i;
627
628 fprintf(out_file, " (inode block for inode %u):\n",
629 inode_to_dump);
630
631 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
632 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
633
634 /* Dump out the direct/indirect blocks here:
635 * internal_dump_inode can only dump them from the main
636 * on-disk inode, not from the journaled copy of the
637 * inode. */
638
639 fprintf (out_file, " Blocks: ");
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000640 first = prev = start_extent = -1;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000641
642 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
643 this = inode->i_block[i];
644 if (start_extent >= 0 && this == prev+1) {
645 prev = this;
646 continue;
647 } else {
648 show_extent(out_file, start_extent, i, first);
649 start_extent = i;
650 first = prev = this;
651 }
652 }
653 show_extent(out_file, start_extent, i, first);
654 show_indirect(out_file, "IND", inode->i_block[i++]);
655 show_indirect(out_file, "DIND", inode->i_block[i++]);
656 show_indirect(out_file, "TIND", inode->i_block[i++]);
657
658 fprintf(out_file, "\n");
659 }
660
661 if (dump_contents)
662 do_hexdump(out_file, buf, blocksize);
663
664}
665
666static void do_hexdump (FILE *out_file, char *buf, int blocksize)
667{
668 int i,j;
669 int *intp;
670 char *charp;
671 unsigned char c;
672
673 intp = (int *) buf;
674 charp = (char *) buf;
675
676 for (i=0; i<blocksize; i+=16) {
677 fprintf(out_file, " %04x: ", i);
678 for (j=0; j<16; j+=4)
679 fprintf(out_file, "%08x ", *intp++);
680 for (j=0; j<16; j++) {
681 c = *charp++;
682 if (c < ' ' || c >= 127)
683 c = '.';
684 fprintf(out_file, "%c", c);
685 }
686 fprintf(out_file, "\n");
687 }
688}
689