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 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 12 | #include "config.h" |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #include <unistd.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <ctype.h> |
| 17 | #include <string.h> |
| 18 | #include <time.h> |
| 19 | #ifdef HAVE_ERRNO_H |
| 20 | #include <errno.h> |
| 21 | #endif |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <utime.h> |
| 26 | #ifdef HAVE_GETOPT_H |
| 27 | #include <getopt.h> |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 28 | #else |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 29 | extern int optind; |
| 30 | extern char *optarg; |
| 31 | #endif |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 32 | |
| 33 | #include "debugfs.h" |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 34 | #include "blkid/blkid.h" |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 35 | #include "jfs_user.h" |
Theodore Ts'o | 4ea7bd0 | 2001-12-16 23:23:37 -0500 | [diff] [blame] | 36 | #include <uuid/uuid.h> |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 37 | |
| 38 | enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL}; |
| 39 | |
Darrick J. Wong | 4dbfd79 | 2013-10-07 09:51:48 -0400 | [diff] [blame] | 40 | #define ANY_BLOCK ((blk64_t) -1) |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 41 | |
Theodore Ts'o | f404167 | 2013-12-16 18:56:36 -0500 | [diff] [blame] | 42 | static int dump_all, dump_contents, dump_descriptors; |
| 43 | static blk64_t block_to_dump, bitmap_to_dump, inode_block_to_dump; |
| 44 | static unsigned int group_to_dump, inode_offset_to_dump; |
| 45 | static ext2_ino_t inode_to_dump; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 46 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 47 | struct journal_source |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 48 | { |
| 49 | enum journal_location where; |
| 50 | int fd; |
| 51 | ext2_file_t file; |
| 52 | }; |
| 53 | |
| 54 | static void dump_journal(char *, FILE *, struct journal_source *); |
| 55 | |
| 56 | static void dump_descriptor_block(FILE *, struct journal_source *, |
| 57 | char *, journal_superblock_t *, |
| 58 | unsigned int *, int, tid_t); |
| 59 | |
| 60 | static void dump_revoke_block(FILE *, char *, journal_superblock_t *, |
| 61 | unsigned int, int, tid_t); |
| 62 | |
| 63 | static void dump_metadata_block(FILE *, struct journal_source *, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 64 | journal_superblock_t*, |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame] | 65 | unsigned int, unsigned int, unsigned int, |
| 66 | int, tid_t); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 67 | |
| 68 | static void do_hexdump (FILE *, char *, int); |
| 69 | |
| 70 | #define WRAP(jsb, blocknr) \ |
| 71 | if (blocknr >= be32_to_cpu((jsb)->s_maxlen)) \ |
| 72 | blocknr -= (be32_to_cpu((jsb)->s_maxlen) - \ |
| 73 | be32_to_cpu((jsb)->s_first)); |
| 74 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 75 | void do_logdump(int argc, char **argv) |
| 76 | { |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 77 | int c; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 78 | int retval; |
| 79 | char *out_fn; |
| 80 | FILE *out_file; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 81 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 82 | char *inode_spec = NULL; |
| 83 | char *journal_fn = NULL; |
| 84 | int journal_fd = 0; |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 85 | int use_sb = 0; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 86 | ext2_ino_t journal_inum; |
| 87 | struct ext2_inode journal_inode; |
| 88 | ext2_file_t journal_file; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 89 | char *tmp; |
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; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 92 | |
Manish Katiyar | c597763 | 2008-10-01 19:55:10 -0400 | [diff] [blame] | 93 | journal_source.where = JOURNAL_IS_INTERNAL; |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 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; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 99 | block_to_dump = ANY_BLOCK; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 100 | bitmap_to_dump = -1; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 101 | inode_block_to_dump = ANY_BLOCK; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 102 | inode_to_dump = -1; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 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: |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 133 | goto print_usage; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | if (optind != argc && optind != argc-1) { |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 137 | goto print_usage; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 140 | if (current_fs) |
| 141 | es = current_fs->super; |
| 142 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 143 | if (inode_spec) { |
| 144 | int inode_group, group_offset, inodes_per_block; |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 145 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 146 | if (check_fs_open(argv[0])) |
| 147 | return; |
| 148 | |
| 149 | inode_to_dump = string_to_inode(inode_spec); |
| 150 | if (!inode_to_dump) |
| 151 | return; |
| 152 | |
| 153 | inode_group = ((inode_to_dump - 1) |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 154 | / es->s_inodes_per_group); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 155 | group_offset = ((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 | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 157 | inodes_per_block = (current_fs->blocksize |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 158 | / sizeof(struct ext2_inode)); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 159 | |
| 160 | inode_block_to_dump = |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 161 | ext2fs_inode_table_loc(current_fs, inode_group) + |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 162 | (group_offset / inodes_per_block); |
| 163 | inode_offset_to_dump = ((group_offset % inodes_per_block) |
| 164 | * sizeof(struct ext2_inode)); |
Darrick J. Wong | 4dbfd79 | 2013-10-07 09:51:48 -0400 | [diff] [blame] | 165 | printf("Inode %u is at group %u, block %llu, offset %u\n", |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 166 | inode_to_dump, inode_group, |
| 167 | inode_block_to_dump, inode_offset_to_dump); |
| 168 | } |
| 169 | |
| 170 | if (optind == argc) { |
| 171 | out_file = stdout; |
| 172 | } else { |
| 173 | out_fn = argv[optind]; |
| 174 | out_file = fopen(out_fn, "w"); |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 175 | if (!out_file) { |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 176 | com_err(argv[0], errno, "while opening %s for logdump", |
| 177 | out_fn); |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 178 | goto errout; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 182 | if (block_to_dump != ANY_BLOCK && current_fs != NULL) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 183 | group_to_dump = ((block_to_dump - |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 184 | es->s_first_data_block) |
| 185 | / es->s_blocks_per_group); |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 186 | bitmap_to_dump = ext2fs_block_bitmap_loc(current_fs, group_to_dump); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 187 | } |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 188 | |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 189 | if (!journal_fn && check_fs_open(argv[0])) |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 190 | goto errout; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 191 | |
| 192 | if (journal_fn) { |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 193 | /* Set up to read journal from a regular file somewhere */ |
| 194 | journal_fd = open(journal_fn, O_RDONLY, 0); |
| 195 | if (journal_fd < 0) { |
| 196 | com_err(argv[0], errno, "while opening %s for logdump", |
| 197 | journal_fn); |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 198 | goto errout; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 199 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 200 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 201 | journal_source.where = JOURNAL_IS_EXTERNAL; |
| 202 | journal_source.fd = journal_fd; |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 203 | } else if ((journal_inum = es->s_journal_inum)) { |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 204 | if (use_sb) { |
| 205 | if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) { |
| 206 | com_err(argv[0], 0, |
| 207 | "no journal backup in super block\n"); |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 208 | goto errout; |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 209 | } |
| 210 | memset(&journal_inode, 0, sizeof(struct ext2_inode)); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 211 | memcpy(&journal_inode.i_block[0], es->s_jnl_blocks, |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 212 | EXT2_N_BLOCKS*4); |
Andreas Dilger | 931b58e | 2011-06-11 12:17:29 -0400 | [diff] [blame] | 213 | journal_inode.i_size_high = es->s_jnl_blocks[15]; |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 214 | journal_inode.i_size = es->s_jnl_blocks[16]; |
| 215 | journal_inode.i_links_count = 1; |
| 216 | journal_inode.i_mode = LINUX_S_IFREG | 0600; |
| 217 | } else { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 218 | if (debugfs_read_inode(journal_inum, &journal_inode, |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 219 | argv[0])) |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 220 | goto errout; |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 221 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 222 | |
Theodore Ts'o | a435ec3 | 2003-08-21 00:40:26 -0400 | [diff] [blame] | 223 | retval = ext2fs_file_open2(current_fs, journal_inum, |
| 224 | &journal_inode, 0, &journal_file); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 225 | if (retval) { |
| 226 | com_err(argv[0], retval, "while opening ext2 file"); |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 227 | goto errout; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 228 | } |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 229 | journal_source.where = JOURNAL_IS_INTERNAL; |
| 230 | journal_source.file = journal_file; |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 231 | } else { |
| 232 | char uuid[37]; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 233 | |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 234 | uuid_unparse(es->s_journal_uuid, uuid); |
| 235 | journal_fn = blkid_get_devname(NULL, "UUID", uuid); |
| 236 | if (!journal_fn) |
| 237 | journal_fn = blkid_devno_to_devname(es->s_journal_dev); |
| 238 | if (!journal_fn) { |
| 239 | com_err(argv[0], 0, "filesystem has no journal"); |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 240 | goto errout; |
Theodore Ts'o | f364093 | 2003-03-01 19:47:44 -0500 | [diff] [blame] | 241 | } |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 242 | journal_fd = open(journal_fn, O_RDONLY, 0); |
| 243 | if (journal_fd < 0) { |
| 244 | com_err(argv[0], errno, "while opening %s for logdump", |
| 245 | journal_fn); |
| 246 | free(journal_fn); |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 247 | goto errout; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 248 | } |
| 249 | fprintf(out_file, "Using external journal found at %s\n", |
| 250 | journal_fn); |
| 251 | free(journal_fn); |
| 252 | journal_source.where = JOURNAL_IS_EXTERNAL; |
| 253 | journal_source.fd = journal_fd; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | dump_journal(argv[0], out_file, &journal_source); |
| 257 | |
| 258 | if (journal_source.where == JOURNAL_IS_INTERNAL) |
| 259 | ext2fs_file_close(journal_file); |
| 260 | else |
| 261 | close(journal_fd); |
| 262 | |
Brian Behlendorf | 0bed54f | 2007-03-23 23:01:09 -0400 | [diff] [blame] | 263 | errout: |
Eric Sandeen | dad0bab | 2009-06-26 13:57:39 -0500 | [diff] [blame] | 264 | if (out_file && (out_file != stdout)) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 265 | fclose(out_file); |
| 266 | |
| 267 | return; |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 268 | |
| 269 | print_usage: |
Manish Katiyar | c597763 | 2008-10-01 19:55:10 -0400 | [diff] [blame] | 270 | 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] | 271 | "[-f<journal_file>] [output_file]\n", argv[0]); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 275 | static int read_journal_block(const char *cmd, struct journal_source *source, |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 276 | off_t offset, char *buf, unsigned int size) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 277 | { |
| 278 | int retval; |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 279 | unsigned int got; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 280 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 281 | if (source->where == JOURNAL_IS_EXTERNAL) { |
Theodore Ts'o | 4bb0c04 | 2001-06-01 15:22:38 +0000 | [diff] [blame] | 282 | if (lseek(source->fd, offset, SEEK_SET) < 0) { |
| 283 | retval = errno; |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 284 | goto seek_err; |
Theodore Ts'o | 4bb0c04 | 2001-06-01 15:22:38 +0000 | [diff] [blame] | 285 | } |
| 286 | retval = read(source->fd, buf, size); |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 287 | if (retval < 0) { |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 288 | retval = errno; |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 289 | goto read_err; |
| 290 | } |
| 291 | got = retval; |
| 292 | retval = 0; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 293 | } else { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 294 | retval = ext2fs_file_lseek(source->file, offset, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 295 | EXT2_SEEK_SET, NULL); |
| 296 | if (retval) { |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 297 | seek_err: |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 298 | com_err(cmd, retval, "while seeking in reading journal"); |
| 299 | return retval; |
| 300 | } |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 301 | retval = ext2fs_file_read(source->file, buf, size, &got); |
| 302 | if (retval) { |
| 303 | read_err: |
| 304 | com_err(cmd, retval, "while reading journal"); |
| 305 | return retval; |
| 306 | } |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 307 | } |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 308 | if (got != size) { |
| 309 | com_err(cmd, 0, "short read (read %u, expected %u) " |
| 310 | "while reading journal", got, size); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 311 | retval = -1; |
| 312 | } |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 313 | return retval; |
| 314 | } |
| 315 | |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 316 | static const char *type_to_name(int btype) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 317 | { |
| 318 | switch (btype) { |
| 319 | case JFS_DESCRIPTOR_BLOCK: |
| 320 | return "descriptor block"; |
| 321 | case JFS_COMMIT_BLOCK: |
| 322 | return "commit block"; |
| 323 | case JFS_SUPERBLOCK_V1: |
| 324 | return "V1 superblock"; |
| 325 | case JFS_SUPERBLOCK_V2: |
| 326 | return "V2 superblock"; |
| 327 | case JFS_REVOKE_BLOCK: |
| 328 | return "revoke table"; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 329 | } |
| 330 | return "unrecognised type"; |
| 331 | } |
| 332 | |
| 333 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 334 | static void dump_journal(char *cmdname, FILE *out_file, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 335 | struct journal_source *source) |
| 336 | { |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 337 | struct ext2_super_block *sb; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 338 | char jsb_buffer[1024]; |
| 339 | char buf[8192]; |
| 340 | journal_superblock_t *jsb; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 341 | unsigned int blocksize = 1024; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 342 | int retval; |
| 343 | __u32 magic, sequence, blocktype; |
| 344 | journal_header_t *header; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 345 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 346 | tid_t transaction; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 347 | unsigned int blocknr = 0; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 348 | |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 349 | /* First, check to see if there's an ext2 superblock header */ |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 350 | retval = read_journal_block(cmdname, source, 0, buf, 2048); |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 351 | if (retval) |
| 352 | return; |
| 353 | |
| 354 | jsb = (journal_superblock_t *) buf; |
| 355 | sb = (struct ext2_super_block *) (buf+1024); |
Theodore Ts'o | 2eae093 | 2007-08-11 02:57:31 -0400 | [diff] [blame] | 356 | #ifdef WORDS_BIGENDIAN |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 357 | if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 358 | ext2fs_swap_super(sb); |
Stephen Tweedie | ff63f26 | 2001-09-27 15:48:56 +0100 | [diff] [blame] | 359 | #endif |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 360 | |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 361 | if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) && |
| 362 | (sb->s_magic == EXT2_SUPER_MAGIC) && |
| 363 | (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { |
| 364 | blocksize = EXT2_BLOCK_SIZE(sb); |
| 365 | blocknr = (blocksize == 1024) ? 2 : 1; |
Theodore Ts'o | 4ea7bd0 | 2001-12-16 23:23:37 -0500 | [diff] [blame] | 366 | uuid_unparse(sb->s_uuid, jsb_buffer); |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 367 | fprintf(out_file, "Ext2 superblock header found.\n"); |
| 368 | if (dump_all) { |
| 369 | fprintf(out_file, "\tuuid=%s\n", jsb_buffer); |
| 370 | fprintf(out_file, "\tblocksize=%d\n", blocksize); |
Valerie Clement | 5d38ef1 | 2007-08-30 17:33:40 +0200 | [diff] [blame] | 371 | fprintf(out_file, "\tjournal data size %lu\n", |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 372 | (unsigned long) ext2fs_blocks_count(sb)); |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 373 | } |
| 374 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 375 | |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 376 | /* Next, read the journal superblock */ |
| 377 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 378 | retval = read_journal_block(cmdname, source, blocknr*blocksize, |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 379 | jsb_buffer, 1024); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 380 | if (retval) |
| 381 | return; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 382 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 383 | jsb = (journal_superblock_t *) jsb_buffer; |
Theodore Ts'o | 5faba3a | 2001-08-12 03:38:51 -0400 | [diff] [blame] | 384 | if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) { |
| 385 | fprintf(out_file, |
| 386 | "Journal superblock magic number invalid!\n"); |
| 387 | return; |
| 388 | } |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 389 | blocksize = be32_to_cpu(jsb->s_blocksize); |
| 390 | transaction = be32_to_cpu(jsb->s_sequence); |
| 391 | blocknr = be32_to_cpu(jsb->s_start); |
| 392 | |
| 393 | fprintf(out_file, "Journal starts at block %u, transaction %u\n", |
| 394 | blocknr, transaction); |
| 395 | |
| 396 | if (!blocknr) |
| 397 | /* Empty journal, nothing to do. */ |
| 398 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 399 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 400 | while (1) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 401 | retval = read_journal_block(cmdname, source, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 402 | blocknr*blocksize, buf, |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 403 | blocksize); |
| 404 | if (retval) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 405 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 406 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 407 | header = (journal_header_t *) buf; |
| 408 | |
| 409 | magic = be32_to_cpu(header->h_magic); |
| 410 | sequence = be32_to_cpu(header->h_sequence); |
| 411 | blocktype = be32_to_cpu(header->h_blocktype); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 412 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 413 | if (magic != JFS_MAGIC_NUMBER) { |
| 414 | fprintf (out_file, "No magic number at block %u: " |
| 415 | "end of journal.\n", blocknr); |
| 416 | return; |
| 417 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 418 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 419 | if (sequence != transaction) { |
| 420 | fprintf (out_file, "Found sequence %u (not %u) at " |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 421 | "block %u: end of journal.\n", |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 422 | sequence, transaction, blocknr); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | if (dump_descriptors) { |
| 427 | fprintf (out_file, "Found expected sequence %u, " |
| 428 | "type %u (%s) at block %u\n", |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 429 | sequence, blocktype, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 430 | type_to_name(blocktype), blocknr); |
| 431 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 432 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 433 | switch (blocktype) { |
| 434 | case JFS_DESCRIPTOR_BLOCK: |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 435 | dump_descriptor_block(out_file, source, buf, jsb, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 436 | &blocknr, blocksize, |
| 437 | transaction); |
| 438 | continue; |
| 439 | |
| 440 | case JFS_COMMIT_BLOCK: |
| 441 | transaction++; |
| 442 | blocknr++; |
| 443 | WRAP(jsb, blocknr); |
| 444 | continue; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 445 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 446 | case JFS_REVOKE_BLOCK: |
| 447 | dump_revoke_block(out_file, buf, jsb, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 448 | blocknr, blocksize, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 449 | transaction); |
| 450 | blocknr++; |
| 451 | WRAP(jsb, blocknr); |
| 452 | continue; |
| 453 | |
| 454 | default: |
| 455 | fprintf (out_file, "Unexpected block type %u at " |
| 456 | "block %u.\n", blocktype, blocknr); |
| 457 | return; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 463 | static void dump_descriptor_block(FILE *out_file, |
| 464 | struct journal_source *source, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 465 | char *buf, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 466 | journal_superblock_t *jsb, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 467 | unsigned int *blockp, int blocksize, |
| 468 | tid_t transaction) |
| 469 | { |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame] | 470 | int offset, tag_size = JBD_TAG_SIZE32; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 471 | char *tagp; |
| 472 | journal_block_tag_t *tag; |
| 473 | unsigned int blocknr; |
| 474 | __u32 tag_block; |
| 475 | __u32 tag_flags; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 476 | |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame] | 477 | if (be32_to_cpu(jsb->s_feature_incompat) & JFS_FEATURE_INCOMPAT_64BIT) |
| 478 | tag_size = JBD_TAG_SIZE64; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 479 | |
| 480 | offset = sizeof(journal_header_t); |
| 481 | blocknr = *blockp; |
| 482 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 483 | if (dump_all) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 484 | fprintf(out_file, "Dumping descriptor block, sequence %u, at " |
| 485 | "block %u:\n", transaction, blocknr); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 486 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 487 | ++blocknr; |
| 488 | WRAP(jsb, blocknr); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 489 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 490 | do { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 491 | /* Work out the location of the current tag, and skip to |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 492 | * the next one... */ |
| 493 | tagp = &buf[offset]; |
| 494 | tag = (journal_block_tag_t *) tagp; |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame] | 495 | offset += tag_size; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 496 | |
| 497 | /* ... and if we have gone too far, then we've reached the |
| 498 | end of this block. */ |
| 499 | if (offset > blocksize) |
| 500 | break; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 501 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 502 | tag_block = be32_to_cpu(tag->t_blocknr); |
| 503 | tag_flags = be32_to_cpu(tag->t_flags); |
| 504 | |
| 505 | if (!(tag_flags & JFS_FLAG_SAME_UUID)) |
| 506 | offset += 16; |
| 507 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 508 | dump_metadata_block(out_file, source, jsb, |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame] | 509 | blocknr, tag_block, tag_flags, blocksize, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 510 | transaction); |
| 511 | |
| 512 | ++blocknr; |
| 513 | WRAP(jsb, blocknr); |
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 | } while (!(tag_flags & JFS_FLAG_LAST_TAG)); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 516 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 517 | *blockp = blocknr; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | static void dump_revoke_block(FILE *out_file, char *buf, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 522 | journal_superblock_t *jsb EXT2FS_ATTR((unused)), |
| 523 | unsigned int blocknr, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 524 | int blocksize EXT2FS_ATTR((unused)), |
| 525 | tid_t transaction) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 526 | { |
| 527 | int offset, max; |
| 528 | journal_revoke_header_t *header; |
| 529 | unsigned int *entry, rblock; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 530 | |
| 531 | if (dump_all) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 532 | fprintf(out_file, "Dumping revoke block, sequence %u, at " |
| 533 | "block %u:\n", transaction, blocknr); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 534 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 535 | header = (journal_revoke_header_t *) buf; |
| 536 | offset = sizeof(journal_revoke_header_t); |
| 537 | max = be32_to_cpu(header->r_count); |
| 538 | |
| 539 | while (offset < max) { |
| 540 | entry = (unsigned int *) (buf + offset); |
| 541 | rblock = be32_to_cpu(*entry); |
| 542 | if (dump_all || rblock == block_to_dump) { |
| 543 | fprintf(out_file, " Revoke FS block %u", rblock); |
| 544 | if (dump_all) |
| 545 | fprintf(out_file, "\n"); |
| 546 | else |
| 547 | fprintf(out_file," at block %u, sequence %u\n", |
| 548 | blocknr, transaction); |
| 549 | } |
| 550 | offset += 4; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | |
| 555 | static void show_extent(FILE *out_file, int start_extent, int end_extent, |
| 556 | __u32 first_block) |
| 557 | { |
| 558 | if (start_extent >= 0 && first_block != 0) |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 559 | fprintf(out_file, "(%d+%u): %u ", |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 560 | start_extent, end_extent-start_extent, first_block); |
| 561 | } |
| 562 | |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 563 | 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] | 564 | { |
| 565 | if (where) |
| 566 | fprintf(out_file, "(%s): %u ", name, where); |
| 567 | } |
| 568 | |
| 569 | |
| 570 | 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] | 571 | journal_superblock_t *jsb EXT2FS_ATTR((unused)), |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 572 | unsigned int log_blocknr, |
| 573 | unsigned int fs_blocknr, |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame] | 574 | unsigned int log_tag_flags, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 575 | int blocksize, |
| 576 | tid_t transaction) |
| 577 | { |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 578 | int retval; |
| 579 | char buf[8192]; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 580 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 581 | if (!(dump_all |
| 582 | || (fs_blocknr == block_to_dump) |
| 583 | || (fs_blocknr == inode_block_to_dump) |
| 584 | || (fs_blocknr == bitmap_to_dump))) |
| 585 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 586 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 587 | fprintf(out_file, " FS block %u logged at ", fs_blocknr); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 588 | if (!dump_all) |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 589 | fprintf(out_file, "sequence %u, ", transaction); |
Theodore Ts'o | 98446d7 | 2009-01-18 22:48:16 -0500 | [diff] [blame] | 590 | fprintf(out_file, "journal block %u (flags 0x%x)\n", log_blocknr, |
| 591 | log_tag_flags); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 592 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 593 | /* There are two major special cases to parse: |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 594 | * |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 595 | * If this block is a block |
| 596 | * bitmap block, we need to give it special treatment so that we |
| 597 | * can log any allocates and deallocates which affect the |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 598 | * block_to_dump query block. |
| 599 | * |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 600 | * If the block is an inode block for the inode being searched |
| 601 | * for, then we need to dump the contents of that inode |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 602 | * structure symbolically. |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 603 | */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 604 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 605 | if (!(dump_contents && dump_all) |
| 606 | && fs_blocknr != block_to_dump |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 607 | && fs_blocknr != bitmap_to_dump |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 608 | && fs_blocknr != inode_block_to_dump) |
| 609 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 610 | |
| 611 | retval = read_journal_block("logdump", source, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 612 | blocksize * log_blocknr, |
Theodore Ts'o | b34b94d | 2014-01-03 09:39:33 -0500 | [diff] [blame] | 613 | buf, blocksize); |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 614 | if (retval) |
| 615 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 616 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 617 | if (fs_blocknr == bitmap_to_dump) { |
| 618 | struct ext2_super_block *super; |
| 619 | int offset; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 620 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 621 | super = current_fs->super; |
Theodore Ts'o | de2c477 | 2010-03-14 19:02:57 -0400 | [diff] [blame] | 622 | offset = ((block_to_dump - super->s_first_data_block) % |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 623 | super->s_blocks_per_group); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 624 | |
Darrick J. Wong | 4dbfd79 | 2013-10-07 09:51:48 -0400 | [diff] [blame] | 625 | fprintf(out_file, " (block bitmap for block %llu: " |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 626 | "block is %s)\n", |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 627 | block_to_dump, |
| 628 | ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR"); |
| 629 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 630 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 631 | if (fs_blocknr == inode_block_to_dump) { |
| 632 | struct ext2_inode *inode; |
| 633 | int first, prev, this, start_extent, i; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 634 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 635 | fprintf(out_file, " (inode block for inode %u):\n", |
| 636 | inode_to_dump); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 637 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 638 | inode = (struct ext2_inode *) (buf + inode_offset_to_dump); |
| 639 | internal_dump_inode(out_file, " ", inode_to_dump, inode, 0); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 640 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 641 | /* Dump out the direct/indirect blocks here: |
| 642 | * internal_dump_inode can only dump them from the main |
| 643 | * on-disk inode, not from the journaled copy of the |
| 644 | * inode. */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 645 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 646 | fprintf (out_file, " Blocks: "); |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 647 | first = prev = start_extent = -1; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 648 | |
| 649 | for (i=0; i<EXT2_NDIR_BLOCKS; i++) { |
| 650 | this = inode->i_block[i]; |
| 651 | if (start_extent >= 0 && this == prev+1) { |
| 652 | prev = this; |
| 653 | continue; |
| 654 | } else { |
| 655 | show_extent(out_file, start_extent, i, first); |
| 656 | start_extent = i; |
| 657 | first = prev = this; |
| 658 | } |
| 659 | } |
| 660 | show_extent(out_file, start_extent, i, first); |
| 661 | show_indirect(out_file, "IND", inode->i_block[i++]); |
| 662 | show_indirect(out_file, "DIND", inode->i_block[i++]); |
| 663 | show_indirect(out_file, "TIND", inode->i_block[i++]); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 664 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 665 | fprintf(out_file, "\n"); |
| 666 | } |
| 667 | |
| 668 | if (dump_contents) |
| 669 | do_hexdump(out_file, buf, blocksize); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 670 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static void do_hexdump (FILE *out_file, char *buf, int blocksize) |
| 674 | { |
| 675 | int i,j; |
| 676 | int *intp; |
| 677 | char *charp; |
| 678 | unsigned char c; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 679 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 680 | intp = (int *) buf; |
| 681 | charp = (char *) buf; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 682 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 683 | for (i=0; i<blocksize; i+=16) { |
| 684 | fprintf(out_file, " %04x: ", i); |
| 685 | for (j=0; j<16; j+=4) |
| 686 | fprintf(out_file, "%08x ", *intp++); |
| 687 | for (j=0; j<16; j++) { |
| 688 | c = *charp++; |
| 689 | if (c < ' ' || c >= 127) |
| 690 | c = '.'; |
| 691 | fprintf(out_file, "%c", c); |
| 692 | } |
| 693 | fprintf(out_file, "\n"); |
| 694 | } |
| 695 | } |
| 696 | |