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 |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 4 | * Authro: Stephen C. Tweedie, 2001 <sct@redhat.com> |
| 5 | * Copyright (C) 2001 Red Hat, Inc. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 6 | * Based on portions Copyright (C) 1994 Theodore Ts'o. |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 7 | * |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 8 | * This file may be redistributed under the terms of the GNU Public |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 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> |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 27 | #else |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 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 | |
Valerie Clement | 5d38ef1 | 2007-08-30 17:33:40 +0200 | [diff] [blame] | 39 | #define ANY_BLOCK ((blk_t) -1) |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 40 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 41 | int dump_all, dump_contents, dump_descriptors; |
Valerie Clement | 5d38ef1 | 2007-08-30 17:33:40 +0200 | [diff] [blame] | 42 | blk_t block_to_dump, bitmap_to_dump, inode_block_to_dump; |
| 43 | unsigned int group_to_dump, inode_offset_to_dump; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 44 | ext2_ino_t inode_to_dump; |
| 45 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 46 | struct journal_source |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 47 | { |
| 48 | enum journal_location where; |
| 49 | int fd; |
| 50 | ext2_file_t file; |
| 51 | }; |
| 52 | |
| 53 | static void dump_journal(char *, FILE *, struct journal_source *); |
| 54 | |
| 55 | static void dump_descriptor_block(FILE *, struct journal_source *, |
| 56 | char *, journal_superblock_t *, |
| 57 | unsigned int *, int, tid_t); |
| 58 | |
| 59 | static void dump_revoke_block(FILE *, char *, journal_superblock_t *, |
| 60 | unsigned int, int, tid_t); |
| 61 | |
| 62 | static void dump_metadata_block(FILE *, struct journal_source *, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 63 | journal_superblock_t*, |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame^] | 64 | unsigned int, unsigned int, unsigned int, |
| 65 | int, tid_t); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 66 | |
| 67 | static 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'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 74 | void do_logdump(int argc, char **argv) |
| 75 | { |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 76 | int c; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 77 | int retval; |
| 78 | char *out_fn; |
| 79 | FILE *out_file; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 80 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 81 | char *inode_spec = NULL; |
| 82 | char *journal_fn = NULL; |
| 83 | int journal_fd = 0; |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 84 | int use_sb = 0; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 85 | ext2_ino_t journal_inum; |
| 86 | struct ext2_inode journal_inode; |
| 87 | ext2_file_t journal_file; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 88 | char *tmp; |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 89 | struct journal_source journal_source; |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 90 | struct ext2_super_block *es = NULL; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 91 | |
Manish Katiyar | c597763 | 2008-10-01 19:55:10 -0400 | [diff] [blame] | 92 | journal_source.where = JOURNAL_IS_INTERNAL; |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 93 | journal_source.fd = 0; |
| 94 | journal_source.file = 0; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 95 | dump_all = 0; |
| 96 | dump_contents = 0; |
| 97 | dump_descriptors = 1; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 98 | block_to_dump = ANY_BLOCK; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 99 | bitmap_to_dump = -1; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 100 | inode_block_to_dump = ANY_BLOCK; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 101 | inode_to_dump = -1; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 102 | |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 103 | reset_getopt(); |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 104 | while ((c = getopt (argc, argv, "ab:ci:f:s")) != EOF) { |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 105 | 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'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 128 | case 's': |
| 129 | use_sb++; |
| 130 | break; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 131 | default: |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 132 | goto print_usage; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | if (optind != argc && optind != argc-1) { |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 136 | goto print_usage; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 139 | if (current_fs) |
| 140 | es = current_fs->super; |
| 141 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 142 | if (inode_spec) { |
| 143 | int inode_group, group_offset, inodes_per_block; |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 144 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 145 | 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'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 153 | / es->s_inodes_per_group); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 154 | group_offset = ((inode_to_dump - 1) |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 155 | % es->s_inodes_per_group); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 156 | inodes_per_block = (current_fs->blocksize |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 157 | / sizeof(struct ext2_inode)); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 158 | |
| 159 | inode_block_to_dump = |
| 160 | current_fs->group_desc[inode_group].bg_inode_table + |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 161 | (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 Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 174 | if (!out_file) { |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 175 | com_err(argv[0], errno, "while opening %s for logdump", |
| 176 | out_fn); |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 177 | goto errout; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 181 | if (block_to_dump != ANY_BLOCK && current_fs != NULL) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 182 | group_to_dump = ((block_to_dump - |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 183 | es->s_first_data_block) |
| 184 | / es->s_blocks_per_group); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 185 | bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap; |
| 186 | } |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 187 | |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 188 | if (!journal_fn && check_fs_open(argv[0])) |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 189 | goto errout; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 190 | |
| 191 | if (journal_fn) { |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 192 | /* 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 Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 197 | goto errout; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 198 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 199 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 200 | journal_source.where = JOURNAL_IS_EXTERNAL; |
| 201 | journal_source.fd = journal_fd; |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 202 | } else if ((journal_inum = es->s_journal_inum)) { |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 203 | 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 Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 207 | goto errout; |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 208 | } |
| 209 | memset(&journal_inode, 0, sizeof(struct ext2_inode)); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 210 | memcpy(&journal_inode.i_block[0], es->s_jnl_blocks, |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 211 | 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 216 | if (debugfs_read_inode(journal_inum, &journal_inode, |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 217 | argv[0])) |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 218 | goto errout; |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 219 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 220 | |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 221 | retval = ext2fs_file_open2(current_fs, journal_inum, |
| 222 | &journal_inode, 0, &journal_file); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 223 | if (retval) { |
| 224 | com_err(argv[0], retval, "while opening ext2 file"); |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 225 | goto errout; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 226 | } |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 227 | journal_source.where = JOURNAL_IS_INTERNAL; |
| 228 | journal_source.file = journal_file; |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 229 | } else { |
| 230 | char uuid[37]; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 231 | |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 232 | 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 Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 238 | goto errout; |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 239 | } |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 240 | 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 Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 245 | goto errout; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 246 | } |
| 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'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 252 | } |
| 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 Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 261 | errout: |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 262 | if (out_file != stdout) |
| 263 | fclose(out_file); |
| 264 | |
| 265 | return; |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 266 | |
| 267 | print_usage: |
Manish Katiyar | c597763 | 2008-10-01 19:55:10 -0400 | [diff] [blame] | 268 | fprintf(stderr, "%s: Usage: logdump [-acs] [-b<block>] [-i<filespec>]\n\t" |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 269 | "[-f<journal_file>] [output_file]\n", argv[0]); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 273 | static int read_journal_block(const char *cmd, struct journal_source *source, |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 274 | off_t offset, char *buf, int size, |
| 275 | unsigned int *got) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 276 | { |
| 277 | int retval; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 278 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 279 | if (source->where == JOURNAL_IS_EXTERNAL) { |
Theodore Ts'o | 4bb0c04 | 2001-06-01 15:22:38 +0000 | [diff] [blame] | 280 | 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'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 286 | if (retval >= 0) { |
| 287 | *got = retval; |
| 288 | retval = 0; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 289 | } else |
| 290 | retval = errno; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 291 | } else { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 292 | retval = ext2fs_file_lseek(source->file, offset, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 293 | EXT2_SEEK_SET, NULL); |
| 294 | if (retval) { |
| 295 | com_err(cmd, retval, "while seeking in reading journal"); |
| 296 | return retval; |
| 297 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 298 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 299 | retval = ext2fs_file_read(source->file, buf, size, got); |
| 300 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 301 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 302 | if (retval) |
| 303 | com_err(cmd, retval, "while while reading journal"); |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 304 | else if (*got != (unsigned int) size) { |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 305 | com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size); |
| 306 | retval = -1; |
| 307 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 308 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 309 | return retval; |
| 310 | } |
| 311 | |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 312 | static const char *type_to_name(int btype) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 313 | { |
| 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'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 325 | } |
| 326 | return "unrecognised type"; |
| 327 | } |
| 328 | |
| 329 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 330 | static void dump_journal(char *cmdname, FILE *out_file, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 331 | struct journal_source *source) |
| 332 | { |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 333 | struct ext2_super_block *sb; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 334 | char jsb_buffer[1024]; |
| 335 | char buf[8192]; |
| 336 | journal_superblock_t *jsb; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 337 | unsigned int blocksize = 1024; |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 338 | unsigned int got; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 339 | int retval; |
| 340 | __u32 magic, sequence, blocktype; |
| 341 | journal_header_t *header; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 342 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 343 | tid_t transaction; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 344 | unsigned int blocknr = 0; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 345 | |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 346 | /* First, check to see if there's an ext2 superblock header */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 347 | retval = read_journal_block(cmdname, source, 0, |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 348 | 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'o | 2eae093 | 2007-08-11 02:57:31 -0400 | [diff] [blame] | 354 | #ifdef WORDS_BIGENDIAN |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 355 | if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 356 | ext2fs_swap_super(sb); |
Stephen Tweedie | ff63f26 | 2001-09-27 15:48:56 +0100 | [diff] [blame] | 357 | #endif |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 358 | |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 359 | if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) && |
| 360 | (sb->s_magic == EXT2_SUPER_MAGIC) && |
| 361 | (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { |
| 362 | blocksize = EXT2_BLOCK_SIZE(sb); |
| 363 | blocknr = (blocksize == 1024) ? 2 : 1; |
Theodore Ts'o | 4ea7bd0 | 2001-12-16 23:23:37 -0500 | [diff] [blame] | 364 | uuid_unparse(sb->s_uuid, jsb_buffer); |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 365 | 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 Clement | 5d38ef1 | 2007-08-30 17:33:40 +0200 | [diff] [blame] | 369 | fprintf(out_file, "\tjournal data size %lu\n", |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 370 | (unsigned long) sb->s_blocks_count); |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 371 | } |
| 372 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 373 | |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 374 | /* Next, read the journal superblock */ |
| 375 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 376 | retval = read_journal_block(cmdname, source, blocknr*blocksize, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 377 | jsb_buffer, 1024, &got); |
| 378 | if (retval) |
| 379 | return; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 380 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 381 | jsb = (journal_superblock_t *) jsb_buffer; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 382 | 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'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 387 | 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 397 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 398 | while (1) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 399 | retval = read_journal_block(cmdname, source, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 400 | blocknr*blocksize, buf, |
| 401 | blocksize, &got); |
| 402 | if (retval || got != blocksize) |
| 403 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 404 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 405 | header = (journal_header_t *) buf; |
| 406 | |
| 407 | magic = be32_to_cpu(header->h_magic); |
| 408 | sequence = be32_to_cpu(header->h_sequence); |
| 409 | blocktype = be32_to_cpu(header->h_blocktype); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 410 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 411 | if (magic != JFS_MAGIC_NUMBER) { |
| 412 | fprintf (out_file, "No magic number at block %u: " |
| 413 | "end of journal.\n", blocknr); |
| 414 | return; |
| 415 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 416 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 417 | if (sequence != transaction) { |
| 418 | fprintf (out_file, "Found sequence %u (not %u) at " |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 419 | "block %u: end of journal.\n", |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 420 | sequence, transaction, blocknr); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | if (dump_descriptors) { |
| 425 | fprintf (out_file, "Found expected sequence %u, " |
| 426 | "type %u (%s) at block %u\n", |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 427 | sequence, blocktype, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 428 | type_to_name(blocktype), blocknr); |
| 429 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 430 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 431 | switch (blocktype) { |
| 432 | case JFS_DESCRIPTOR_BLOCK: |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 433 | dump_descriptor_block(out_file, source, buf, jsb, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 434 | &blocknr, blocksize, |
| 435 | transaction); |
| 436 | continue; |
| 437 | |
| 438 | case JFS_COMMIT_BLOCK: |
| 439 | transaction++; |
| 440 | blocknr++; |
| 441 | WRAP(jsb, blocknr); |
| 442 | continue; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 443 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 444 | case JFS_REVOKE_BLOCK: |
| 445 | dump_revoke_block(out_file, buf, jsb, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 446 | blocknr, blocksize, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 447 | transaction); |
| 448 | blocknr++; |
| 449 | WRAP(jsb, blocknr); |
| 450 | continue; |
| 451 | |
| 452 | default: |
| 453 | fprintf (out_file, "Unexpected block type %u at " |
| 454 | "block %u.\n", blocktype, blocknr); |
| 455 | return; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 461 | static void dump_descriptor_block(FILE *out_file, |
| 462 | struct journal_source *source, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 463 | char *buf, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 464 | journal_superblock_t *jsb, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 465 | unsigned int *blockp, int blocksize, |
| 466 | tid_t transaction) |
| 467 | { |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame^] | 468 | int offset, tag_size = JBD_TAG_SIZE32; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 469 | char *tagp; |
| 470 | journal_block_tag_t *tag; |
| 471 | unsigned int blocknr; |
| 472 | __u32 tag_block; |
| 473 | __u32 tag_flags; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 474 | |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame^] | 475 | if (be32_to_cpu(jsb->s_feature_incompat) & JFS_FEATURE_INCOMPAT_64BIT) |
| 476 | tag_size = JBD_TAG_SIZE64; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 477 | |
| 478 | offset = sizeof(journal_header_t); |
| 479 | blocknr = *blockp; |
| 480 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 481 | if (dump_all) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 482 | fprintf(out_file, "Dumping descriptor block, sequence %u, at " |
| 483 | "block %u:\n", transaction, blocknr); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 484 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 485 | ++blocknr; |
| 486 | WRAP(jsb, blocknr); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 487 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 488 | do { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 489 | /* Work out the location of the current tag, and skip to |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 490 | * the next one... */ |
| 491 | tagp = &buf[offset]; |
| 492 | tag = (journal_block_tag_t *) tagp; |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame^] | 493 | offset += tag_size; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 494 | |
| 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 499 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 500 | 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 506 | dump_metadata_block(out_file, source, jsb, |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame^] | 507 | blocknr, tag_block, tag_flags, blocksize, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 508 | transaction); |
| 509 | |
| 510 | ++blocknr; |
| 511 | WRAP(jsb, blocknr); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 512 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 513 | } while (!(tag_flags & JFS_FLAG_LAST_TAG)); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 514 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 515 | *blockp = blocknr; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | static void dump_revoke_block(FILE *out_file, char *buf, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 520 | journal_superblock_t *jsb EXT2FS_ATTR((unused)), |
| 521 | unsigned int blocknr, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 522 | int blocksize EXT2FS_ATTR((unused)), |
| 523 | tid_t transaction) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 524 | { |
| 525 | int offset, max; |
| 526 | journal_revoke_header_t *header; |
| 527 | unsigned int *entry, rblock; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 528 | |
| 529 | if (dump_all) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 530 | fprintf(out_file, "Dumping revoke block, sequence %u, at " |
| 531 | "block %u:\n", transaction, blocknr); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 532 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 533 | 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 | |
| 553 | static 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 557 | fprintf(out_file, "(%d+%u): %u ", |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 558 | start_extent, end_extent-start_extent, first_block); |
| 559 | } |
| 560 | |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 561 | 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] | 562 | { |
| 563 | if (where) |
| 564 | fprintf(out_file, "(%s): %u ", name, where); |
| 565 | } |
| 566 | |
| 567 | |
| 568 | static void dump_metadata_block(FILE *out_file, struct journal_source *source, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 569 | journal_superblock_t *jsb EXT2FS_ATTR((unused)), |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 570 | unsigned int log_blocknr, |
| 571 | unsigned int fs_blocknr, |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame^] | 572 | unsigned int log_tag_flags, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 573 | int blocksize, |
| 574 | tid_t transaction) |
| 575 | { |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 576 | unsigned int got; |
| 577 | int retval; |
| 578 | char buf[8192]; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 579 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 580 | 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 585 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 586 | fprintf(out_file, " FS block %u logged at ", fs_blocknr); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 587 | if (!dump_all) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 588 | fprintf(out_file, "sequence %u, ", transaction); |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame^] | 589 | fprintf(out_file, "journal block %u (flags 0x%x)\n", log_blocknr, |
| 590 | log_tag_flags); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 591 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 592 | /* There are two major special cases to parse: |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 593 | * |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 594 | * 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 597 | * block_to_dump query block. |
| 598 | * |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 599 | * 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 601 | * structure symbolically. |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 602 | */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 603 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 604 | if (!(dump_contents && dump_all) |
| 605 | && fs_blocknr != block_to_dump |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 606 | && fs_blocknr != bitmap_to_dump |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 607 | && fs_blocknr != inode_block_to_dump) |
| 608 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 609 | |
| 610 | retval = read_journal_block("logdump", source, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 611 | blocksize * log_blocknr, |
| 612 | buf, blocksize, &got); |
| 613 | if (retval) |
| 614 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 615 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 616 | if (fs_blocknr == bitmap_to_dump) { |
| 617 | struct ext2_super_block *super; |
| 618 | int offset; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 619 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 620 | super = current_fs->super; |
| 621 | offset = ((fs_blocknr - super->s_first_data_block) % |
| 622 | super->s_blocks_per_group); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 623 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 624 | fprintf(out_file, " (block bitmap for block %u: " |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 625 | "block is %s)\n", |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 626 | block_to_dump, |
| 627 | ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR"); |
| 628 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 629 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 630 | if (fs_blocknr == inode_block_to_dump) { |
| 631 | struct ext2_inode *inode; |
| 632 | int first, prev, this, start_extent, i; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 633 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 634 | fprintf(out_file, " (inode block for inode %u):\n", |
| 635 | inode_to_dump); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 636 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 637 | inode = (struct ext2_inode *) (buf + inode_offset_to_dump); |
| 638 | internal_dump_inode(out_file, " ", inode_to_dump, inode, 0); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 639 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 640 | /* 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 644 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 645 | fprintf (out_file, " Blocks: "); |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 646 | first = prev = start_extent = -1; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 647 | |
| 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 663 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 664 | fprintf(out_file, "\n"); |
| 665 | } |
| 666 | |
| 667 | if (dump_contents) |
| 668 | do_hexdump(out_file, buf, blocksize); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 669 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 678 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 679 | intp = (int *) buf; |
| 680 | charp = (char *) buf; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 681 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 682 | 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 | |