| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 1 | /* |
| 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 |
| 28 | extern int optind; |
| 29 | extern char *optarg; |
| 30 | #endif |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 31 | |
| 32 | #include "debugfs.h" |
| Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 33 | #include "blkid/blkid.h" |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 34 | #include "jfs_user.h" |
| Theodore Ts'o | 4ea7bd0 | 2001-12-16 23:23:37 -0500 | [diff] [blame] | 35 | #include <uuid/uuid.h> |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 36 | |
| 37 | enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL}; |
| 38 | |
| 39 | int dump_all, dump_contents, dump_descriptors; |
| 40 | unsigned int block_to_dump, group_to_dump, bitmap_to_dump; |
| 41 | unsigned int inode_block_to_dump, inode_offset_to_dump, bitmap_to_dump; |
| 42 | ext2_ino_t inode_to_dump; |
| 43 | |
| 44 | struct journal_source |
| 45 | { |
| 46 | enum journal_location where; |
| 47 | int fd; |
| 48 | ext2_file_t file; |
| 49 | }; |
| 50 | |
| 51 | static void dump_journal(char *, FILE *, struct journal_source *); |
| 52 | |
| 53 | static void dump_descriptor_block(FILE *, struct journal_source *, |
| 54 | char *, journal_superblock_t *, |
| 55 | unsigned int *, int, tid_t); |
| 56 | |
| 57 | static void dump_revoke_block(FILE *, char *, journal_superblock_t *, |
| 58 | unsigned int, int, tid_t); |
| 59 | |
| 60 | static void dump_metadata_block(FILE *, struct journal_source *, |
| 61 | journal_superblock_t*, |
| 62 | unsigned int, unsigned int, int, tid_t); |
| 63 | |
| 64 | static void do_hexdump (FILE *, char *, int); |
| 65 | |
| 66 | #define WRAP(jsb, blocknr) \ |
| 67 | if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \ |
| 68 | blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \ |
| 69 | be32_to_cpu((jsb)->s_first)); |
| 70 | |
| 71 | |
| 72 | void do_logdump(int argc, char **argv) |
| 73 | { |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 74 | int c; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 75 | int retval; |
| 76 | char *out_fn; |
| 77 | FILE *out_file; |
| 78 | |
| 79 | char *inode_spec = NULL; |
| 80 | char *journal_fn = NULL; |
| 81 | int journal_fd = 0; |
| Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame^] | 82 | int use_sb = 0; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 83 | ext2_ino_t journal_inum; |
| 84 | struct ext2_inode journal_inode; |
| 85 | ext2_file_t journal_file; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 86 | char *tmp; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 87 | const char *logdump_usage = ("Usage: logdump " |
| 88 | "[-ac] [-b<block>] [-i<inode>] " |
| 89 | "[-f<journal_file>] [output_file]"); |
| Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 90 | struct journal_source journal_source; |
| Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 91 | struct ext2_super_block *es = NULL; |
| 92 | |
| Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 93 | journal_source.where = 0; |
| 94 | journal_source.fd = 0; |
| 95 | journal_source.file = 0; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 96 | dump_all = 0; |
| 97 | dump_contents = 0; |
| 98 | dump_descriptors = 1; |
| 99 | block_to_dump = -1; |
| 100 | bitmap_to_dump = -1; |
| 101 | inode_block_to_dump = -1; |
| 102 | inode_to_dump = -1; |
| 103 | |
| Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 104 | reset_getopt(); |
| Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame^] | 105 | while ((c = getopt (argc, argv, "ab:ci:f:s")) != EOF) { |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 106 | switch (c) { |
| 107 | case 'a': |
| 108 | dump_all++; |
| 109 | break; |
| 110 | case 'b': |
| 111 | block_to_dump = strtoul(optarg, &tmp, 0); |
| 112 | if (*tmp) { |
| 113 | com_err(argv[0], 0, |
| 114 | "Bad block number - %s", optarg); |
| 115 | return; |
| 116 | } |
| 117 | dump_descriptors = 0; |
| 118 | break; |
| 119 | case 'c': |
| 120 | dump_contents++; |
| 121 | break; |
| 122 | case 'f': |
| 123 | journal_fn = optarg; |
| 124 | break; |
| 125 | case 'i': |
| 126 | inode_spec = optarg; |
| 127 | dump_descriptors = 0; |
| 128 | break; |
| Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame^] | 129 | case 's': |
| 130 | use_sb++; |
| 131 | break; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 132 | default: |
| 133 | com_err(argv[0], 0, logdump_usage); |
| 134 | return; |
| 135 | } |
| 136 | } |
| 137 | if (optind != argc && optind != argc-1) { |
| 138 | com_err(argv[0], 0, logdump_usage); |
| 139 | return; |
| 140 | } |
| 141 | |
| Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 142 | if (current_fs) |
| 143 | es = current_fs->super; |
| 144 | |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 145 | if (inode_spec) { |
| 146 | int inode_group, group_offset, inodes_per_block; |
| Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 147 | |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 148 | if (check_fs_open(argv[0])) |
| 149 | return; |
| 150 | |
| 151 | inode_to_dump = string_to_inode(inode_spec); |
| 152 | if (!inode_to_dump) |
| 153 | return; |
| 154 | |
| 155 | inode_group = ((inode_to_dump - 1) |
| Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 156 | / es->s_inodes_per_group); |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 157 | group_offset = ((inode_to_dump - 1) |
| Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 158 | % es->s_inodes_per_group); |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 159 | inodes_per_block = (current_fs->blocksize |
| 160 | / sizeof(struct ext2_inode)); |
| 161 | |
| 162 | inode_block_to_dump = |
| 163 | current_fs->group_desc[inode_group].bg_inode_table + |
| 164 | (group_offset / inodes_per_block); |
| 165 | inode_offset_to_dump = ((group_offset % inodes_per_block) |
| 166 | * sizeof(struct ext2_inode)); |
| 167 | printf("Inode %u is at group %u, block %u, offset %u\n", |
| 168 | inode_to_dump, inode_group, |
| 169 | inode_block_to_dump, inode_offset_to_dump); |
| 170 | } |
| 171 | |
| 172 | if (optind == argc) { |
| 173 | out_file = stdout; |
| 174 | } else { |
| 175 | out_fn = argv[optind]; |
| 176 | out_file = fopen(out_fn, "w"); |
| 177 | if (!out_file < 0) { |
| 178 | com_err(argv[0], errno, "while opening %s for logdump", |
| 179 | out_fn); |
| 180 | return; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (block_to_dump != -1 && current_fs != NULL) { |
| 185 | group_to_dump = ((block_to_dump - |
| Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 186 | es->s_first_data_block) |
| 187 | / es->s_blocks_per_group); |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 188 | bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap; |
| 189 | } |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 190 | |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 191 | if (!journal_fn && check_fs_open(argv[0])) |
| 192 | return; |
| 193 | |
| 194 | if (journal_fn) { |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 195 | /* Set up to read journal from a regular file somewhere */ |
| 196 | journal_fd = open(journal_fn, O_RDONLY, 0); |
| 197 | if (journal_fd < 0) { |
| 198 | com_err(argv[0], errno, "while opening %s for logdump", |
| 199 | journal_fn); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | journal_source.where = JOURNAL_IS_EXTERNAL; |
| 204 | journal_source.fd = journal_fd; |
| Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 205 | } else if ((journal_inum = es->s_journal_inum)) { |
| Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame^] | 206 | if (use_sb) { |
| 207 | if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) { |
| 208 | com_err(argv[0], 0, |
| 209 | "no journal backup in super block\n"); |
| 210 | return; |
| 211 | } |
| 212 | memset(&journal_inode, 0, sizeof(struct ext2_inode)); |
| 213 | memcpy(&journal_inode.i_block[0], es->s_jnl_blocks, |
| 214 | EXT2_N_BLOCKS*4); |
| 215 | journal_inode.i_size = es->s_jnl_blocks[16]; |
| 216 | journal_inode.i_links_count = 1; |
| 217 | journal_inode.i_mode = LINUX_S_IFREG | 0600; |
| 218 | } else { |
| 219 | if (debugfs_read_inode(journal_inum, &journal_inode, |
| 220 | argv[0])) |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | retval = ext2fs_file_open2(current_fs, journal_inum, |
| 225 | &journal_inode, 0, &journal_file); |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 226 | if (retval) { |
| 227 | com_err(argv[0], retval, "while opening ext2 file"); |
| 228 | return; |
| 229 | } |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 230 | journal_source.where = JOURNAL_IS_INTERNAL; |
| 231 | journal_source.file = journal_file; |
| Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 232 | } else { |
| 233 | char uuid[37]; |
| 234 | |
| 235 | uuid_unparse(es->s_journal_uuid, uuid); |
| 236 | journal_fn = blkid_get_devname(NULL, "UUID", uuid); |
| 237 | if (!journal_fn) |
| 238 | journal_fn = blkid_devno_to_devname(es->s_journal_dev); |
| 239 | if (!journal_fn) { |
| 240 | com_err(argv[0], 0, "filesystem has no journal"); |
| 241 | return; |
| 242 | } |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 243 | journal_fd = open(journal_fn, O_RDONLY, 0); |
| 244 | if (journal_fd < 0) { |
| 245 | com_err(argv[0], errno, "while opening %s for logdump", |
| 246 | journal_fn); |
| 247 | free(journal_fn); |
| 248 | return; |
| 249 | } |
| 250 | fprintf(out_file, "Using external journal found at %s\n", |
| 251 | journal_fn); |
| 252 | free(journal_fn); |
| 253 | journal_source.where = JOURNAL_IS_EXTERNAL; |
| 254 | journal_source.fd = journal_fd; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | dump_journal(argv[0], out_file, &journal_source); |
| 258 | |
| 259 | if (journal_source.where == JOURNAL_IS_INTERNAL) |
| 260 | ext2fs_file_close(journal_file); |
| 261 | else |
| 262 | close(journal_fd); |
| 263 | |
| 264 | if (out_file != stdout) |
| 265 | fclose(out_file); |
| 266 | |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | |
| Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 271 | static 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'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 274 | { |
| 275 | int retval; |
| 276 | |
| 277 | if (source->where == JOURNAL_IS_EXTERNAL) { |
| Theodore Ts'o | 4bb0c04 | 2001-06-01 15:22:38 +0000 | [diff] [blame] | 278 | 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'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 284 | if (retval >= 0) { |
| 285 | *got = retval; |
| 286 | retval = 0; |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 287 | } else |
| 288 | retval = errno; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 289 | } 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"); |
| 302 | else if (*got != size) { |
| 303 | 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'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 310 | static const char *type_to_name(int btype) |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 311 | { |
| 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'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 323 | } |
| 324 | return "unrecognised type"; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | static void dump_journal(char *cmdname, FILE *out_file, |
| 329 | struct journal_source *source) |
| 330 | { |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 331 | struct ext2_super_block *sb; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 332 | char jsb_buffer[1024]; |
| 333 | char buf[8192]; |
| 334 | journal_superblock_t *jsb; |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 335 | int blocksize = 1024; |
| Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 336 | unsigned int got; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 337 | int retval; |
| 338 | __u32 magic, sequence, blocktype; |
| 339 | journal_header_t *header; |
| 340 | |
| 341 | tid_t transaction; |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 342 | unsigned int blocknr = 0; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 343 | |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 344 | /* First, check to see if there's an ext2 superblock header */ |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 345 | retval = read_journal_block(cmdname, source, 0, |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 346 | buf, 2048, &got); |
| 347 | if (retval) |
| 348 | return; |
| 349 | |
| 350 | jsb = (journal_superblock_t *) buf; |
| 351 | sb = (struct ext2_super_block *) (buf+1024); |
| Stephen Tweedie | ff63f26 | 2001-09-27 15:48:56 +0100 | [diff] [blame] | 352 | #ifdef ENABLE_SWAPFS |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 353 | if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) |
| 354 | ext2fs_swap_super(sb); |
| Stephen Tweedie | ff63f26 | 2001-09-27 15:48:56 +0100 | [diff] [blame] | 355 | #endif |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 356 | |
| 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'o | 4ea7bd0 | 2001-12-16 23:23:37 -0500 | [diff] [blame] | 362 | uuid_unparse(sb->s_uuid, jsb_buffer); |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 363 | 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'o | 4ea7bd0 | 2001-12-16 23:23:37 -0500 | [diff] [blame] | 368 | (long) sb->s_blocks_count); |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | /* Next, read the journal superblock */ |
| 373 | |
| 374 | retval = read_journal_block(cmdname, source, blocknr*blocksize, |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 375 | jsb_buffer, 1024, &got); |
| 376 | if (retval) |
| 377 | return; |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 378 | |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 379 | jsb = (journal_superblock_t *) jsb_buffer; |
| Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 380 | 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'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 385 | 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 | |
| 459 | static 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 | |
| 515 | static void dump_revoke_block(FILE *out_file, char *buf, |
| 516 | journal_superblock_t *jsb, |
| 517 | unsigned int blocknr, int blocksize, |
| 518 | tid_t transaction) |
| 519 | { |
| 520 | int offset, max; |
| 521 | journal_revoke_header_t *header; |
| 522 | unsigned int *entry, rblock; |
| 523 | |
| 524 | if (dump_all) |
| 525 | fprintf(out_file, "Dumping revoke block, sequence %u, at " |
| 526 | "block %u:\n", transaction, blocknr); |
| 527 | |
| 528 | header = (journal_revoke_header_t *) buf; |
| 529 | offset = sizeof(journal_revoke_header_t); |
| 530 | max = be32_to_cpu(header->r_count); |
| 531 | |
| 532 | while (offset < max) { |
| 533 | entry = (unsigned int *) (buf + offset); |
| 534 | rblock = be32_to_cpu(*entry); |
| 535 | if (dump_all || rblock == block_to_dump) { |
| 536 | fprintf(out_file, " Revoke FS block %u", rblock); |
| 537 | if (dump_all) |
| 538 | fprintf(out_file, "\n"); |
| 539 | else |
| 540 | fprintf(out_file," at block %u, sequence %u\n", |
| 541 | blocknr, transaction); |
| 542 | } |
| 543 | offset += 4; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | |
| 548 | static void show_extent(FILE *out_file, int start_extent, int end_extent, |
| 549 | __u32 first_block) |
| 550 | { |
| 551 | if (start_extent >= 0 && first_block != 0) |
| 552 | fprintf(out_file, "(%d+%u): %u ", |
| 553 | start_extent, end_extent-start_extent, first_block); |
| 554 | } |
| 555 | |
| Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 556 | static void show_indirect(FILE *out_file, const char *name, __u32 where) |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 557 | { |
| 558 | if (where) |
| 559 | fprintf(out_file, "(%s): %u ", name, where); |
| 560 | } |
| 561 | |
| 562 | |
| 563 | static void dump_metadata_block(FILE *out_file, struct journal_source *source, |
| 564 | journal_superblock_t *jsb, |
| 565 | unsigned int log_blocknr, |
| 566 | unsigned int fs_blocknr, |
| 567 | int blocksize, |
| 568 | tid_t transaction) |
| 569 | { |
| Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 570 | unsigned int got; |
| 571 | int retval; |
| 572 | char buf[8192]; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 573 | |
| 574 | if (!(dump_all |
| 575 | || (fs_blocknr == block_to_dump) |
| 576 | || (fs_blocknr == inode_block_to_dump) |
| 577 | || (fs_blocknr == bitmap_to_dump))) |
| 578 | return; |
| 579 | |
| 580 | fprintf(out_file, " FS block %u logged at ", fs_blocknr); |
| 581 | if (!dump_all) |
| 582 | fprintf(out_file, "sequence %u, ", transaction); |
| 583 | fprintf(out_file, "journal block %u\n", log_blocknr); |
| 584 | |
| 585 | /* There are two major special cases to parse: |
| 586 | * |
| 587 | * If this block is a block |
| 588 | * bitmap block, we need to give it special treatment so that we |
| 589 | * can log any allocates and deallocates which affect the |
| 590 | * block_to_dump query block. |
| 591 | * |
| 592 | * If the block is an inode block for the inode being searched |
| 593 | * for, then we need to dump the contents of that inode |
| 594 | * structure symbolically. |
| 595 | */ |
| 596 | |
| 597 | if (!(dump_contents && dump_all) |
| 598 | && fs_blocknr != block_to_dump |
| 599 | && fs_blocknr != bitmap_to_dump |
| 600 | && fs_blocknr != inode_block_to_dump) |
| 601 | return; |
| 602 | |
| 603 | retval = read_journal_block("logdump", source, |
| 604 | blocksize * log_blocknr, |
| 605 | buf, blocksize, &got); |
| 606 | if (retval) |
| 607 | return; |
| 608 | |
| 609 | if (fs_blocknr == bitmap_to_dump) { |
| 610 | struct ext2_super_block *super; |
| 611 | int offset; |
| 612 | |
| 613 | super = current_fs->super; |
| 614 | offset = ((fs_blocknr - super->s_first_data_block) % |
| 615 | super->s_blocks_per_group); |
| 616 | |
| 617 | fprintf(out_file, " (block bitmap for block %u: " |
| 618 | "block is %s)\n", |
| 619 | block_to_dump, |
| 620 | ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR"); |
| 621 | } |
| 622 | |
| 623 | if (fs_blocknr == inode_block_to_dump) { |
| 624 | struct ext2_inode *inode; |
| 625 | int first, prev, this, start_extent, i; |
| 626 | |
| 627 | fprintf(out_file, " (inode block for inode %u):\n", |
| 628 | inode_to_dump); |
| 629 | |
| 630 | inode = (struct ext2_inode *) (buf + inode_offset_to_dump); |
| 631 | internal_dump_inode(out_file, " ", inode_to_dump, inode, 0); |
| 632 | |
| 633 | /* Dump out the direct/indirect blocks here: |
| 634 | * internal_dump_inode can only dump them from the main |
| 635 | * on-disk inode, not from the journaled copy of the |
| 636 | * inode. */ |
| 637 | |
| 638 | fprintf (out_file, " Blocks: "); |
| Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 639 | first = prev = start_extent = -1; |
| Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 640 | |
| 641 | for (i=0; i<EXT2_NDIR_BLOCKS; i++) { |
| 642 | this = inode->i_block[i]; |
| 643 | if (start_extent >= 0 && this == prev+1) { |
| 644 | prev = this; |
| 645 | continue; |
| 646 | } else { |
| 647 | show_extent(out_file, start_extent, i, first); |
| 648 | start_extent = i; |
| 649 | first = prev = this; |
| 650 | } |
| 651 | } |
| 652 | show_extent(out_file, start_extent, i, first); |
| 653 | show_indirect(out_file, "IND", inode->i_block[i++]); |
| 654 | show_indirect(out_file, "DIND", inode->i_block[i++]); |
| 655 | show_indirect(out_file, "TIND", inode->i_block[i++]); |
| 656 | |
| 657 | fprintf(out_file, "\n"); |
| 658 | } |
| 659 | |
| 660 | if (dump_contents) |
| 661 | do_hexdump(out_file, buf, blocksize); |
| 662 | |
| 663 | } |
| 664 | |
| 665 | static void do_hexdump (FILE *out_file, char *buf, int blocksize) |
| 666 | { |
| 667 | int i,j; |
| 668 | int *intp; |
| 669 | char *charp; |
| 670 | unsigned char c; |
| 671 | |
| 672 | intp = (int *) buf; |
| 673 | charp = (char *) buf; |
| 674 | |
| 675 | for (i=0; i<blocksize; i+=16) { |
| 676 | fprintf(out_file, " %04x: ", i); |
| 677 | for (j=0; j<16; j+=4) |
| 678 | fprintf(out_file, "%08x ", *intp++); |
| 679 | for (j=0; j<16; j++) { |
| 680 | c = *charp++; |
| 681 | if (c < ' ' || c >= 127) |
| 682 | c = '.'; |
| 683 | fprintf(out_file, "%c", c); |
| 684 | } |
| 685 | fprintf(out_file, "\n"); |
| 686 | } |
| 687 | } |
| 688 | |