blob: 69539e240a94ecc6e0cc68ba8161036bd968765c [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
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'oda81e3f2001-03-29 20:49:58 +00004 * Authro: Stephen C. Tweedie, 2001 <sct@redhat.com>
5 * Copyright (C) 2001 Red Hat, Inc.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04006 * Based on portions Copyright (C) 1994 Theodore Ts'o.
Theodore Ts'oda81e3f2001-03-29 20:49:58 +00007 *
Theodore Ts'oefc6f622008-08-27 23:07:54 -04008 * This file may be redistributed under the terms of the GNU Public
Theodore Ts'oda81e3f2001-03-29 20:49:58 +00009 * 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>
Theodore Ts'oefc6f622008-08-27 23:07:54 -040027#else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000028extern 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
Valerie Clement5d38ef12007-08-30 17:33:40 +020039#define ANY_BLOCK ((blk_t) -1)
Theodore Ts'o54434922003-12-07 01:28:50 -050040
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000041int dump_all, dump_contents, dump_descriptors;
Valerie Clement5d38ef12007-08-30 17:33:40 +020042blk_t block_to_dump, bitmap_to_dump, inode_block_to_dump;
43unsigned int group_to_dump, inode_offset_to_dump;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000044ext2_ino_t inode_to_dump;
45
Theodore Ts'oefc6f622008-08-27 23:07:54 -040046struct journal_source
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000047{
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 *,
Theodore Ts'oefc6f622008-08-27 23:07:54 -040063 journal_superblock_t*,
Theodore Ts'o98446d72009-01-18 22:48:16 -050064 unsigned int, unsigned int, unsigned int,
65 int, tid_t);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000066
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
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000074void 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;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040080
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000081 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'o5e4f0702001-06-01 15:36:05 +000089 struct journal_source journal_source;
Theodore Ts'of3640932003-03-01 19:47:44 -050090 struct ext2_super_block *es = NULL;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040091
Manish Katiyarc5977632008-10-01 19:55:10 -040092 journal_source.where = JOURNAL_IS_INTERNAL;
Theodore Ts'o5e4f0702001-06-01 15:36:05 +000093 journal_source.fd = 0;
94 journal_source.file = 0;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000095 dump_all = 0;
96 dump_contents = 0;
97 dump_descriptors = 1;
Theodore Ts'o54434922003-12-07 01:28:50 -050098 block_to_dump = ANY_BLOCK;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +000099 bitmap_to_dump = -1;
Theodore Ts'o54434922003-12-07 01:28:50 -0500100 inode_block_to_dump = ANY_BLOCK;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000101 inode_to_dump = -1;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400102
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400103 reset_getopt();
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400104 while ((c = getopt (argc, argv, "ab:ci:f:s")) != EOF) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000105 switch (c) {
106 case 'a':
107 dump_all++;
108 break;
109 case 'b':
110 block_to_dump = strtoul(optarg, &tmp, 0);
111 if (*tmp) {
112 com_err(argv[0], 0,
113 "Bad block number - %s", optarg);
114 return;
115 }
116 dump_descriptors = 0;
117 break;
118 case 'c':
119 dump_contents++;
120 break;
121 case 'f':
122 journal_fn = optarg;
123 break;
124 case 'i':
125 inode_spec = optarg;
126 dump_descriptors = 0;
127 break;
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400128 case 's':
129 use_sb++;
130 break;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000131 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500132 goto print_usage;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000133 }
134 }
135 if (optind != argc && optind != argc-1) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500136 goto print_usage;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000137 }
138
Theodore Ts'of3640932003-03-01 19:47:44 -0500139 if (current_fs)
140 es = current_fs->super;
141
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000142 if (inode_spec) {
143 int inode_group, group_offset, inodes_per_block;
Theodore Ts'of3640932003-03-01 19:47:44 -0500144
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000145 if (check_fs_open(argv[0]))
146 return;
147
148 inode_to_dump = string_to_inode(inode_spec);
149 if (!inode_to_dump)
150 return;
151
152 inode_group = ((inode_to_dump - 1)
Theodore Ts'of3640932003-03-01 19:47:44 -0500153 / es->s_inodes_per_group);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000154 group_offset = ((inode_to_dump - 1)
Theodore Ts'of3640932003-03-01 19:47:44 -0500155 % es->s_inodes_per_group);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400156 inodes_per_block = (current_fs->blocksize
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000157 / sizeof(struct ext2_inode));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400158
159 inode_block_to_dump =
160 current_fs->group_desc[inode_group].bg_inode_table +
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000161 (group_offset / inodes_per_block);
162 inode_offset_to_dump = ((group_offset % inodes_per_block)
163 * sizeof(struct ext2_inode));
164 printf("Inode %u is at group %u, block %u, offset %u\n",
165 inode_to_dump, inode_group,
166 inode_block_to_dump, inode_offset_to_dump);
167 }
168
169 if (optind == argc) {
170 out_file = stdout;
171 } else {
172 out_fn = argv[optind];
173 out_file = fopen(out_fn, "w");
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400174 if (!out_file) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000175 com_err(argv[0], errno, "while opening %s for logdump",
176 out_fn);
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400177 goto errout;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000178 }
179 }
180
Theodore Ts'o54434922003-12-07 01:28:50 -0500181 if (block_to_dump != ANY_BLOCK && current_fs != NULL) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400182 group_to_dump = ((block_to_dump -
Theodore Ts'of3640932003-03-01 19:47:44 -0500183 es->s_first_data_block)
184 / es->s_blocks_per_group);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000185 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
186 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000187
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400188 if (!journal_fn && check_fs_open(argv[0]))
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400189 goto errout;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400190
191 if (journal_fn) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000192 /* Set up to read journal from a regular file somewhere */
193 journal_fd = open(journal_fn, O_RDONLY, 0);
194 if (journal_fd < 0) {
195 com_err(argv[0], errno, "while opening %s for logdump",
196 journal_fn);
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400197 goto errout;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000198 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400199
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000200 journal_source.where = JOURNAL_IS_EXTERNAL;
201 journal_source.fd = journal_fd;
Theodore Ts'of3640932003-03-01 19:47:44 -0500202 } else if ((journal_inum = es->s_journal_inum)) {
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400203 if (use_sb) {
204 if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
205 com_err(argv[0], 0,
206 "no journal backup in super block\n");
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400207 goto errout;
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400208 }
209 memset(&journal_inode, 0, sizeof(struct ext2_inode));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400210 memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400211 EXT2_N_BLOCKS*4);
212 journal_inode.i_size = es->s_jnl_blocks[16];
213 journal_inode.i_links_count = 1;
214 journal_inode.i_mode = LINUX_S_IFREG | 0600;
215 } else {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400216 if (debugfs_read_inode(journal_inum, &journal_inode,
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400217 argv[0]))
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400218 goto errout;
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400219 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400220
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400221 retval = ext2fs_file_open2(current_fs, journal_inum,
222 &journal_inode, 0, &journal_file);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000223 if (retval) {
224 com_err(argv[0], retval, "while opening ext2 file");
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400225 goto errout;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000226 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000227 journal_source.where = JOURNAL_IS_INTERNAL;
228 journal_source.file = journal_file;
Theodore Ts'of3640932003-03-01 19:47:44 -0500229 } else {
230 char uuid[37];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400231
Theodore Ts'of3640932003-03-01 19:47:44 -0500232 uuid_unparse(es->s_journal_uuid, uuid);
233 journal_fn = blkid_get_devname(NULL, "UUID", uuid);
234 if (!journal_fn)
235 journal_fn = blkid_devno_to_devname(es->s_journal_dev);
236 if (!journal_fn) {
237 com_err(argv[0], 0, "filesystem has no journal");
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400238 goto errout;
Theodore Ts'of3640932003-03-01 19:47:44 -0500239 }
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400240 journal_fd = open(journal_fn, O_RDONLY, 0);
241 if (journal_fd < 0) {
242 com_err(argv[0], errno, "while opening %s for logdump",
243 journal_fn);
244 free(journal_fn);
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400245 goto errout;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400246 }
247 fprintf(out_file, "Using external journal found at %s\n",
248 journal_fn);
249 free(journal_fn);
250 journal_source.where = JOURNAL_IS_EXTERNAL;
251 journal_source.fd = journal_fd;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000252 }
253
254 dump_journal(argv[0], out_file, &journal_source);
255
256 if (journal_source.where == JOURNAL_IS_INTERNAL)
257 ext2fs_file_close(journal_file);
258 else
259 close(journal_fd);
260
Brian Behlendorf0bed54f2007-03-23 23:01:09 -0400261errout:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000262 if (out_file != stdout)
263 fclose(out_file);
264
265 return;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500266
267print_usage:
Manish Katiyarc5977632008-10-01 19:55:10 -0400268 fprintf(stderr, "%s: Usage: logdump [-acs] [-b<block>] [-i<filespec>]\n\t"
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500269 "[-f<journal_file>] [output_file]\n", argv[0]);
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000270}
271
272
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400273static int read_journal_block(const char *cmd, struct journal_source *source,
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000274 off_t offset, char *buf, int size,
275 unsigned int *got)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000276{
277 int retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400278
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000279 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 {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400292 retval = ext2fs_file_lseek(source->file, offset,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000293 EXT2_SEEK_SET, NULL);
294 if (retval) {
295 com_err(cmd, retval, "while seeking in reading journal");
296 return retval;
297 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400298
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000299 retval = ext2fs_file_read(source->file, buf, size, got);
300 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400301
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000302 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 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400308
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000309 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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400330static void dump_journal(char *cmdname, FILE *out_file,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000331 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;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400342
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000343 tid_t transaction;
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400344 unsigned int blocknr = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400345
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400346 /* First, check to see if there's an ext2 superblock header */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400347 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);
Theodore Ts'o2eae0932007-08-11 02:57:31 -0400354#ifdef WORDS_BIGENDIAN
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400355 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400356 ext2fs_swap_super(sb);
Stephen Tweedieff63f262001-09-27 15:48:56 +0100357#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400358
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400359 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);
Valerie Clement5d38ef12007-08-30 17:33:40 +0200369 fprintf(out_file, "\tjournal data size %lu\n",
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400370 (unsigned long) sb->s_blocks_count);
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400371 }
372 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400373
Theodore Ts'o5faba3a2001-08-12 03:38:51 -0400374 /* Next, read the journal superblock */
375
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400376 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;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400397
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000398 while (1) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400399 retval = read_journal_block(cmdname, source,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000400 blocknr*blocksize, buf,
401 blocksize, &got);
402 if (retval || got != blocksize)
403 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400404
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000405 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);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400410
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000411 if (magic != JFS_MAGIC_NUMBER) {
412 fprintf (out_file, "No magic number at block %u: "
413 "end of journal.\n", blocknr);
414 return;
415 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400416
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000417 if (sequence != transaction) {
418 fprintf (out_file, "Found sequence %u (not %u) at "
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400419 "block %u: end of journal.\n",
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000420 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",
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400427 sequence, blocktype,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000428 type_to_name(blocktype), blocknr);
429 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400430
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000431 switch (blocktype) {
432 case JFS_DESCRIPTOR_BLOCK:
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400433 dump_descriptor_block(out_file, source, buf, jsb,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000434 &blocknr, blocksize,
435 transaction);
436 continue;
437
438 case JFS_COMMIT_BLOCK:
439 transaction++;
440 blocknr++;
441 WRAP(jsb, blocknr);
442 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400443
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000444 case JFS_REVOKE_BLOCK:
445 dump_revoke_block(out_file, buf, jsb,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400446 blocknr, blocksize,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000447 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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400461static void dump_descriptor_block(FILE *out_file,
462 struct journal_source *source,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000463 char *buf,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400464 journal_superblock_t *jsb,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000465 unsigned int *blockp, int blocksize,
466 tid_t transaction)
467{
Theodore Ts'o98446d72009-01-18 22:48:16 -0500468 int offset, tag_size = JBD_TAG_SIZE32;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000469 char *tagp;
470 journal_block_tag_t *tag;
471 unsigned int blocknr;
472 __u32 tag_block;
473 __u32 tag_flags;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400474
Theodore Ts'o98446d72009-01-18 22:48:16 -0500475 if (be32_to_cpu(jsb->s_feature_incompat) & JFS_FEATURE_INCOMPAT_64BIT)
476 tag_size = JBD_TAG_SIZE64;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000477
478 offset = sizeof(journal_header_t);
479 blocknr = *blockp;
480
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400481 if (dump_all)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000482 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
483 "block %u:\n", transaction, blocknr);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400484
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000485 ++blocknr;
486 WRAP(jsb, blocknr);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400487
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000488 do {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400489 /* Work out the location of the current tag, and skip to
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000490 * the next one... */
491 tagp = &buf[offset];
492 tag = (journal_block_tag_t *) tagp;
Theodore Ts'o98446d72009-01-18 22:48:16 -0500493 offset += tag_size;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000494
495 /* ... and if we have gone too far, then we've reached the
496 end of this block. */
497 if (offset > blocksize)
498 break;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400499
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000500 tag_block = be32_to_cpu(tag->t_blocknr);
501 tag_flags = be32_to_cpu(tag->t_flags);
502
503 if (!(tag_flags & JFS_FLAG_SAME_UUID))
504 offset += 16;
505
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400506 dump_metadata_block(out_file, source, jsb,
Theodore Ts'o98446d72009-01-18 22:48:16 -0500507 blocknr, tag_block, tag_flags, blocksize,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000508 transaction);
509
510 ++blocknr;
511 WRAP(jsb, blocknr);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400512
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000513 } while (!(tag_flags & JFS_FLAG_LAST_TAG));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400514
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000515 *blockp = blocknr;
516}
517
518
519static void dump_revoke_block(FILE *out_file, char *buf,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400520 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
521 unsigned int blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -0500522 int blocksize EXT2FS_ATTR((unused)),
523 tid_t transaction)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000524{
525 int offset, max;
526 journal_revoke_header_t *header;
527 unsigned int *entry, rblock;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400528
529 if (dump_all)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000530 fprintf(out_file, "Dumping revoke block, sequence %u, at "
531 "block %u:\n", transaction, blocknr);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400532
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000533 header = (journal_revoke_header_t *) buf;
534 offset = sizeof(journal_revoke_header_t);
535 max = be32_to_cpu(header->r_count);
536
537 while (offset < max) {
538 entry = (unsigned int *) (buf + offset);
539 rblock = be32_to_cpu(*entry);
540 if (dump_all || rblock == block_to_dump) {
541 fprintf(out_file, " Revoke FS block %u", rblock);
542 if (dump_all)
543 fprintf(out_file, "\n");
544 else
545 fprintf(out_file," at block %u, sequence %u\n",
546 blocknr, transaction);
547 }
548 offset += 4;
549 }
550}
551
552
553static void show_extent(FILE *out_file, int start_extent, int end_extent,
554 __u32 first_block)
555{
556 if (start_extent >= 0 && first_block != 0)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400557 fprintf(out_file, "(%d+%u): %u ",
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000558 start_extent, end_extent-start_extent, first_block);
559}
560
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000561static void show_indirect(FILE *out_file, const char *name, __u32 where)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000562{
563 if (where)
564 fprintf(out_file, "(%s): %u ", name, where);
565}
566
567
568static void dump_metadata_block(FILE *out_file, struct journal_source *source,
Theodore Ts'o54434922003-12-07 01:28:50 -0500569 journal_superblock_t *jsb EXT2FS_ATTR((unused)),
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400570 unsigned int log_blocknr,
571 unsigned int fs_blocknr,
Theodore Ts'o98446d72009-01-18 22:48:16 -0500572 unsigned int log_tag_flags,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000573 int blocksize,
574 tid_t transaction)
575{
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000576 unsigned int got;
577 int retval;
578 char buf[8192];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400579
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000580 if (!(dump_all
581 || (fs_blocknr == block_to_dump)
582 || (fs_blocknr == inode_block_to_dump)
583 || (fs_blocknr == bitmap_to_dump)))
584 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400585
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000586 fprintf(out_file, " FS block %u logged at ", fs_blocknr);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400587 if (!dump_all)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000588 fprintf(out_file, "sequence %u, ", transaction);
Theodore Ts'o98446d72009-01-18 22:48:16 -0500589 fprintf(out_file, "journal block %u (flags 0x%x)\n", log_blocknr,
590 log_tag_flags);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400591
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000592 /* There are two major special cases to parse:
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400593 *
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000594 * If this block is a block
595 * bitmap block, we need to give it special treatment so that we
596 * can log any allocates and deallocates which affect the
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400597 * block_to_dump query block.
598 *
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000599 * If the block is an inode block for the inode being searched
600 * for, then we need to dump the contents of that inode
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400601 * structure symbolically.
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000602 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400603
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000604 if (!(dump_contents && dump_all)
605 && fs_blocknr != block_to_dump
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400606 && fs_blocknr != bitmap_to_dump
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000607 && fs_blocknr != inode_block_to_dump)
608 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400609
610 retval = read_journal_block("logdump", source,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000611 blocksize * log_blocknr,
612 buf, blocksize, &got);
613 if (retval)
614 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400615
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000616 if (fs_blocknr == bitmap_to_dump) {
617 struct ext2_super_block *super;
618 int offset;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400619
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000620 super = current_fs->super;
621 offset = ((fs_blocknr - super->s_first_data_block) %
622 super->s_blocks_per_group);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400623
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000624 fprintf(out_file, " (block bitmap for block %u: "
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400625 "block is %s)\n",
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000626 block_to_dump,
627 ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
628 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400629
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000630 if (fs_blocknr == inode_block_to_dump) {
631 struct ext2_inode *inode;
632 int first, prev, this, start_extent, i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400633
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000634 fprintf(out_file, " (inode block for inode %u):\n",
635 inode_to_dump);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400636
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000637 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
638 internal_dump_inode(out_file, " ", inode_to_dump, inode, 0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400639
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000640 /* Dump out the direct/indirect blocks here:
641 * internal_dump_inode can only dump them from the main
642 * on-disk inode, not from the journaled copy of the
643 * inode. */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400644
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000645 fprintf (out_file, " Blocks: ");
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000646 first = prev = start_extent = -1;
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000647
648 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
649 this = inode->i_block[i];
650 if (start_extent >= 0 && this == prev+1) {
651 prev = this;
652 continue;
653 } else {
654 show_extent(out_file, start_extent, i, first);
655 start_extent = i;
656 first = prev = this;
657 }
658 }
659 show_extent(out_file, start_extent, i, first);
660 show_indirect(out_file, "IND", inode->i_block[i++]);
661 show_indirect(out_file, "DIND", inode->i_block[i++]);
662 show_indirect(out_file, "TIND", inode->i_block[i++]);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400663
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000664 fprintf(out_file, "\n");
665 }
666
667 if (dump_contents)
668 do_hexdump(out_file, buf, blocksize);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400669
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000670}
671
672static void do_hexdump (FILE *out_file, char *buf, int blocksize)
673{
674 int i,j;
675 int *intp;
676 char *charp;
677 unsigned char c;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400678
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000679 intp = (int *) buf;
680 charp = (char *) buf;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400681
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000682 for (i=0; i<blocksize; i+=16) {
683 fprintf(out_file, " %04x: ", i);
684 for (j=0; j<16; j+=4)
685 fprintf(out_file, "%08x ", *intp++);
686 for (j=0; j<16; j++) {
687 c = *charp++;
688 if (c < ' ' || c >= 127)
689 c = '.';
690 fprintf(out_file, "%c", c);
691 }
692 fprintf(out_file, "\n");
693 }
694}
695