Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * htree.c --- hash tree routines |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 4 | * Copyright (C) 2002 Theodore Ts'o. This file may be redistributed |
| 5 | * under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <unistd.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <ctype.h> |
| 12 | #include <string.h> |
| 13 | #include <time.h> |
| 14 | #ifdef HAVE_ERRNO_H |
| 15 | #include <errno.h> |
| 16 | #endif |
| 17 | #include <sys/types.h> |
| 18 | #ifdef HAVE_GETOPT_H |
| 19 | #include <getopt.h> |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 20 | #else |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 21 | extern int optind; |
| 22 | extern char *optarg; |
| 23 | #endif |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 24 | |
| 25 | #include "debugfs.h" |
| 26 | |
| 27 | static FILE *pager; |
| 28 | |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 29 | static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino, |
| 30 | struct ext2_inode *inode, |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 31 | struct ext2_dx_root_info * rootnode, |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 32 | blk_t blk, char *buf) |
| 33 | { |
| 34 | errcode_t errcode; |
| 35 | struct ext2_dir_entry *dirent; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 36 | int thislen, col = 0; |
| 37 | unsigned int offset = 0; |
Brian Behlendorf | b772900 | 2007-03-21 15:09:15 -0400 | [diff] [blame] | 38 | char name[EXT2_NAME_LEN + 1]; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 39 | char tmp[EXT2_NAME_LEN + 16]; |
| 40 | blk_t pblk; |
Theodore Ts'o | 226515d | 2008-08-08 11:56:07 -0400 | [diff] [blame] | 41 | ext2_dirhash_t hash, minor_hash; |
Theodore Ts'o | 8a48035 | 2009-06-21 21:07:38 -0400 | [diff] [blame^] | 42 | unsigned int rec_len; |
| 43 | int hash_alg; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 44 | |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 45 | errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk); |
| 46 | if (errcode) { |
| 47 | com_err("htree_dump_leaf_node", errcode, |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 48 | "while mapping logical block %u\n", blk); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
Theodore Ts'o | 2af6fe0 | 2009-04-22 15:08:36 -0400 | [diff] [blame] | 52 | printf("Reading directory block %lu, phys %lu\n", |
| 53 | (unsigned long) blk, (unsigned long) pblk); |
Theodore Ts'o | 8132d84 | 2002-10-02 22:07:17 -0400 | [diff] [blame] | 54 | errcode = ext2fs_read_dir_block2(current_fs, pblk, buf, 0); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 55 | if (errcode) { |
| 56 | com_err("htree_dump_leaf_node", errcode, |
Theodore Ts'o | 2af6fe0 | 2009-04-22 15:08:36 -0400 | [diff] [blame] | 57 | "while reading block %lu (%lu)\n", |
| 58 | (unsigned long) blk, (unsigned long) pblk); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 59 | return; |
| 60 | } |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 61 | hash_alg = rootnode->hash_version; |
| 62 | if ((hash_alg <= EXT2_HASH_TEA) && |
| 63 | (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)) |
| 64 | hash_alg += 3; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 65 | |
| 66 | while (offset < fs->blocksize) { |
| 67 | dirent = (struct ext2_dir_entry *) (buf + offset); |
Theodore Ts'o | 8a48035 | 2009-06-21 21:07:38 -0400 | [diff] [blame^] | 68 | errcode = ext2fs_get_rec_len(fs, dirent, &rec_len); |
| 69 | if (errcode) { |
| 70 | com_err("htree_dump_leaf_inode", errcode, |
| 71 | "while getting rec_len for block %lu", |
| 72 | (unsigned long) blk); |
| 73 | return; |
| 74 | } |
Theodore Ts'o | 5dd77db | 2008-08-25 21:08:19 -0400 | [diff] [blame] | 75 | if (((offset + rec_len) > fs->blocksize) || |
| 76 | (rec_len < 8) || |
| 77 | ((rec_len % 4) != 0) || |
| 78 | (((dirent->name_len & 0xFF)+8) > rec_len)) { |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 79 | fprintf(pager, "Corrupted directory block (%u)!\n", blk); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 80 | break; |
| 81 | } |
| 82 | thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ? |
| 83 | (dirent->name_len & 0xFF) : EXT2_NAME_LEN; |
| 84 | strncpy(name, dirent->name, thislen); |
| 85 | name[thislen] = '\0'; |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 86 | errcode = ext2fs_dirhash(hash_alg, name, |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 87 | thislen, fs->super->s_hash_seed, |
Theodore Ts'o | 226515d | 2008-08-08 11:56:07 -0400 | [diff] [blame] | 88 | &hash, &minor_hash); |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 89 | if (errcode) |
| 90 | com_err("htree_dump_leaf_node", errcode, |
| 91 | "while calculating hash"); |
Theodore Ts'o | 226515d | 2008-08-08 11:56:07 -0400 | [diff] [blame] | 92 | sprintf(tmp, "%u 0x%08x-%08x (%d) %s ", dirent->inode, |
Theodore Ts'o | 5dd77db | 2008-08-25 21:08:19 -0400 | [diff] [blame] | 93 | hash, minor_hash, rec_len, name); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 94 | thislen = strlen(tmp); |
| 95 | if (col + thislen > 80) { |
| 96 | fprintf(pager, "\n"); |
| 97 | col = 0; |
| 98 | } |
| 99 | fprintf(pager, "%s", tmp); |
| 100 | col += thislen; |
Theodore Ts'o | 5dd77db | 2008-08-25 21:08:19 -0400 | [diff] [blame] | 101 | offset += rec_len; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 102 | } |
| 103 | fprintf(pager, "\n"); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino, |
| 108 | struct ext2_inode *inode, |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 109 | struct ext2_dx_root_info * rootnode, |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 110 | blk_t blk, char *buf, int level); |
| 111 | |
| 112 | |
| 113 | static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino, |
| 114 | struct ext2_inode *inode, |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 115 | struct ext2_dx_root_info * rootnode, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 116 | struct ext2_dx_entry *ent, |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 117 | char *buf, int level) |
| 118 | { |
Theodore Ts'o | 621732c | 2002-07-18 22:19:51 -0400 | [diff] [blame] | 119 | struct ext2_dx_countlimit limit; |
| 120 | struct ext2_dx_entry e; |
Theodore Ts'o | 42e5b5f | 2002-09-22 15:27:28 -0400 | [diff] [blame] | 121 | int hash, i; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 122 | |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 123 | |
Theodore Ts'o | 621732c | 2002-07-18 22:19:51 -0400 | [diff] [blame] | 124 | limit = *((struct ext2_dx_countlimit *) ent); |
| 125 | limit.count = ext2fs_le16_to_cpu(limit.count); |
| 126 | limit.limit = ext2fs_le16_to_cpu(limit.limit); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 127 | |
Theodore Ts'o | 621732c | 2002-07-18 22:19:51 -0400 | [diff] [blame] | 128 | fprintf(pager, "Number of entries (count): %d\n", limit.count); |
| 129 | fprintf(pager, "Number of entries (limit): %d\n", limit.limit); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 130 | |
Theodore Ts'o | 42e5b5f | 2002-09-22 15:27:28 -0400 | [diff] [blame] | 131 | for (i=0; i < limit.count; i++) { |
| 132 | hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0; |
Eric Sandeen | d0ff90d | 2006-09-12 14:56:15 -0400 | [diff] [blame] | 133 | fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i, |
Theodore Ts'o | 42e5b5f | 2002-09-22 15:27:28 -0400 | [diff] [blame] | 134 | hash, (hash & 1) ? " (**)" : "", |
Theodore Ts'o | 621732c | 2002-07-18 22:19:51 -0400 | [diff] [blame] | 135 | ext2fs_le32_to_cpu(ent[i].block)); |
Theodore Ts'o | 42e5b5f | 2002-09-22 15:27:28 -0400 | [diff] [blame] | 136 | } |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 137 | |
| 138 | fprintf(pager, "\n"); |
| 139 | |
Theodore Ts'o | 621732c | 2002-07-18 22:19:51 -0400 | [diff] [blame] | 140 | for (i=0; i < limit.count; i++) { |
| 141 | e.hash = ext2fs_le32_to_cpu(ent[i].hash); |
| 142 | e.block = ext2fs_le32_to_cpu(ent[i].block); |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 143 | fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i, |
Theodore Ts'o | 621732c | 2002-07-18 22:19:51 -0400 | [diff] [blame] | 144 | i ? e.hash : 0, e.block); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 145 | if (level) |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 146 | htree_dump_int_block(fs, ino, inode, rootnode, |
Theodore Ts'o | 621732c | 2002-07-18 22:19:51 -0400 | [diff] [blame] | 147 | e.block, buf, level-1); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 148 | else |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 149 | htree_dump_leaf_node(fs, ino, inode, rootnode, |
Theodore Ts'o | 621732c | 2002-07-18 22:19:51 -0400 | [diff] [blame] | 150 | e.block, buf); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | fprintf(pager, "---------------------\n"); |
| 154 | } |
| 155 | |
| 156 | static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino, |
| 157 | struct ext2_inode *inode, |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 158 | struct ext2_dx_root_info * rootnode, |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 159 | blk_t blk, char *buf, int level) |
| 160 | { |
| 161 | char *cbuf; |
| 162 | errcode_t errcode; |
| 163 | blk_t pblk; |
| 164 | |
| 165 | cbuf = malloc(fs->blocksize); |
| 166 | if (!cbuf) { |
| 167 | fprintf(pager, "Couldn't allocate child block.\n"); |
| 168 | return; |
| 169 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 170 | |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 171 | errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk); |
| 172 | if (errcode) { |
| 173 | com_err("htree_dump_int_block", errcode, |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 174 | "while mapping logical block %u\n", blk); |
Brian Behlendorf | 8945655 | 2007-03-21 17:53:33 -0400 | [diff] [blame] | 175 | goto errout; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf); |
| 179 | if (errcode) { |
| 180 | com_err("htree_dump_int_block", errcode, |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 181 | "while reading block %u\n", blk); |
Brian Behlendorf | 8945655 | 2007-03-21 17:53:33 -0400 | [diff] [blame] | 182 | goto errout; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 183 | } |
| 184 | |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 185 | htree_dump_int_node(fs, ino, inode, rootnode, |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 186 | (struct ext2_dx_entry *) (buf+8), |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 187 | cbuf, level); |
Brian Behlendorf | 8945655 | 2007-03-21 17:53:33 -0400 | [diff] [blame] | 188 | errout: |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 189 | free(cbuf); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | |
| 194 | void do_htree_dump(int argc, char *argv[]) |
| 195 | { |
| 196 | ext2_ino_t ino; |
| 197 | struct ext2_inode inode; |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 198 | int c; |
Theodore Ts'o | d904dd2 | 2007-03-29 00:26:10 -0400 | [diff] [blame] | 199 | int long_opt = 0; |
Theodore Ts'o | 226515d | 2008-08-08 11:56:07 -0400 | [diff] [blame] | 200 | blk_t blk; |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 201 | char *buf = NULL; |
| 202 | struct ext2_dx_root_info *rootnode; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 203 | struct ext2_dx_entry *ent; |
| 204 | struct ext2_dx_countlimit *limit; |
| 205 | errcode_t errcode; |
| 206 | |
| 207 | if (check_fs_open(argv[0])) |
| 208 | return; |
| 209 | |
| 210 | pager = open_pager(); |
| 211 | |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 212 | reset_getopt(); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 213 | while ((c = getopt (argc, argv, "l")) != EOF) { |
| 214 | switch (c) { |
| 215 | case 'l': |
| 216 | long_opt++; |
| 217 | break; |
Theodore Ts'o | 49c6b4e | 2006-04-27 20:59:42 -0400 | [diff] [blame] | 218 | default: |
| 219 | goto print_usage; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | if (argc > optind+1) { |
Theodore Ts'o | 49c6b4e | 2006-04-27 20:59:42 -0400 | [diff] [blame] | 224 | print_usage: |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 225 | com_err(0, 0, "Usage: htree_dump [-l] file"); |
Theodore Ts'o | 155f577 | 2002-07-21 14:17:45 -0400 | [diff] [blame] | 226 | goto errout; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if (argc == optind) |
| 230 | ino = cwd; |
| 231 | else |
| 232 | ino = string_to_inode(argv[optind]); |
| 233 | if (!ino) |
Theodore Ts'o | 155f577 | 2002-07-21 14:17:45 -0400 | [diff] [blame] | 234 | goto errout; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 235 | |
| 236 | if (debugfs_read_inode(ino, &inode, argv[1])) |
Theodore Ts'o | 155f577 | 2002-07-21 14:17:45 -0400 | [diff] [blame] | 237 | goto errout; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 238 | |
| 239 | if (!LINUX_S_ISDIR(inode.i_mode)) { |
| 240 | com_err(argv[0], 0, "Not a directory"); |
Theodore Ts'o | 155f577 | 2002-07-21 14:17:45 -0400 | [diff] [blame] | 241 | goto errout; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 242 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 243 | |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 244 | if ((inode.i_flags & EXT2_BTREE_FL) == 0) { |
| 245 | com_err(argv[0], 0, "Not a hash-indexed directory"); |
Theodore Ts'o | 155f577 | 2002-07-21 14:17:45 -0400 | [diff] [blame] | 246 | goto errout; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | buf = malloc(2*current_fs->blocksize); |
| 250 | if (!buf) { |
| 251 | com_err(argv[0], 0, "Couldn't allocate htree buffer"); |
Theodore Ts'o | 155f577 | 2002-07-21 14:17:45 -0400 | [diff] [blame] | 252 | goto errout; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Theodore Ts'o | 226515d | 2008-08-08 11:56:07 -0400 | [diff] [blame] | 255 | errcode = ext2fs_bmap(current_fs, ino, &inode, buf, 0, 0, &blk); |
| 256 | if (errcode) { |
| 257 | com_err("do_htree_block", errcode, |
| 258 | "while mapping logical block 0\n"); |
| 259 | goto errout; |
| 260 | } |
| 261 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 262 | errcode = io_channel_read_blk(current_fs->io, blk, |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 263 | 1, buf); |
| 264 | if (errcode) { |
| 265 | com_err(argv[0], errcode, "Error reading root node"); |
| 266 | goto errout; |
| 267 | } |
| 268 | |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 269 | rootnode = (struct ext2_dx_root_info *) (buf + 24); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 270 | |
| 271 | fprintf(pager, "Root node dump:\n"); |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 272 | fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero); |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 273 | fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version); |
| 274 | fprintf(pager, "\t Info length: %d\n", rootnode->info_length); |
| 275 | fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels); |
| 276 | fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 277 | |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 278 | ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 279 | limit = (struct ext2_dx_countlimit *) ent; |
| 280 | |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 281 | htree_dump_int_node(current_fs, ino, &inode, rootnode, ent, |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 282 | buf + current_fs->blocksize, |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 283 | rootnode->indirect_levels); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 284 | |
| 285 | errout: |
Jim Meyering | 45e338f | 2009-02-23 18:07:50 +0100 | [diff] [blame] | 286 | free(buf); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 287 | close_pager(pager); |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * This function prints the hash of a given file. |
| 292 | */ |
| 293 | void do_dx_hash(int argc, char *argv[]) |
| 294 | { |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 295 | ext2_dirhash_t hash, minor_hash; |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 296 | errcode_t err; |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 297 | int c; |
| 298 | int hash_version = 0; |
| 299 | __u32 hash_seed[4]; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 300 | |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 301 | hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0; |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 302 | |
| 303 | reset_getopt(); |
Theodore Ts'o | f8bd551 | 2008-09-10 11:30:46 -0400 | [diff] [blame] | 304 | while ((c = getopt (argc, argv, "h:s:")) != EOF) { |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 305 | switch (c) { |
| 306 | case 'h': |
Theodore Ts'o | f8bd551 | 2008-09-10 11:30:46 -0400 | [diff] [blame] | 307 | hash_version = e2p_string2hash(optarg); |
| 308 | if (hash_version < 0) |
| 309 | hash_version = atoi(optarg); |
| 310 | break; |
| 311 | case 's': |
| 312 | if (uuid_parse(optarg, hash_seed)) { |
| 313 | fprintf(stderr, "Invalid UUID format: %s\n", |
| 314 | optarg); |
| 315 | return; |
| 316 | } |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 317 | break; |
Theodore Ts'o | 49c6b4e | 2006-04-27 20:59:42 -0400 | [diff] [blame] | 318 | default: |
| 319 | goto print_usage; |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | if (optind != argc-1) { |
Theodore Ts'o | 49c6b4e | 2006-04-27 20:59:42 -0400 | [diff] [blame] | 323 | print_usage: |
Theodore Ts'o | f8bd551 | 2008-09-10 11:30:46 -0400 | [diff] [blame] | 324 | com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] " |
| 325 | "[-s hash_seed] filename"); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 326 | return; |
| 327 | } |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 328 | err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]), |
| 329 | hash_seed, &hash, &minor_hash); |
Theodore Ts'o | 52783e0 | 2002-03-11 15:04:45 -0500 | [diff] [blame] | 330 | if (err) { |
| 331 | com_err(argv[0], err, "while caclulating hash"); |
| 332 | return; |
| 333 | } |
Theodore Ts'o | 503f9e7 | 2002-06-26 16:52:10 -0400 | [diff] [blame] | 334 | printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind], |
| 335 | hash, minor_hash); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Search for particular directory entry (useful for debugging very |
| 340 | * large hash tree directories that have lost some blocks from the |
| 341 | * btree index). |
| 342 | */ |
| 343 | struct process_block_struct { |
| 344 | char *search_name; |
| 345 | char *buf; |
| 346 | int len; |
| 347 | }; |
| 348 | |
| 349 | static int search_dir_block(ext2_filsys fs, blk_t *blocknr, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 350 | e2_blkcnt_t blockcnt, blk_t ref_blk, |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 351 | int ref_offset, void *priv_data); |
| 352 | |
| 353 | void do_dirsearch(int argc, char *argv[]) |
| 354 | { |
| 355 | ext2_ino_t inode; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 356 | struct process_block_struct pb; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 357 | |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 358 | if (check_fs_open(argv[0])) |
| 359 | return; |
| 360 | |
| 361 | if (argc != 3) { |
| 362 | com_err(0, 0, "Usage: dirsearch dir filename"); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | inode = string_to_inode(argv[1]); |
| 367 | if (!inode) |
| 368 | return; |
| 369 | |
| 370 | pb.buf = malloc(current_fs->blocksize); |
| 371 | if (!pb.buf) { |
| 372 | com_err("dirsearch", 0, "Couldn't allocate buffer"); |
| 373 | return; |
| 374 | } |
| 375 | pb.search_name = argv[2]; |
| 376 | pb.len = strlen(pb.search_name); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 377 | |
Theodore Ts'o | 43323be | 2008-02-07 14:37:17 -0500 | [diff] [blame] | 378 | ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0, |
| 379 | search_dir_block, &pb); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 380 | |
| 381 | free(pb.buf); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | static int search_dir_block(ext2_filsys fs, blk_t *blocknr, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 386 | e2_blkcnt_t blockcnt, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 387 | blk_t ref_blk EXT2FS_ATTR((unused)), |
| 388 | int ref_offset EXT2FS_ATTR((unused)), |
| 389 | void *priv_data) |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 390 | { |
| 391 | struct process_block_struct *p; |
| 392 | struct ext2_dir_entry *dirent; |
| 393 | errcode_t errcode; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 394 | unsigned int offset = 0; |
Theodore Ts'o | 8a48035 | 2009-06-21 21:07:38 -0400 | [diff] [blame^] | 395 | unsigned int rec_len; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 396 | |
| 397 | if (blockcnt < 0) |
| 398 | return 0; |
| 399 | |
| 400 | p = (struct process_block_struct *) priv_data; |
| 401 | |
| 402 | errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf); |
| 403 | if (errcode) { |
| 404 | com_err("search_dir_block", errcode, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 405 | "while reading block %lu", (unsigned long) *blocknr); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 406 | return BLOCK_ABORT; |
| 407 | } |
| 408 | |
| 409 | while (offset < fs->blocksize) { |
| 410 | dirent = (struct ext2_dir_entry *) (p->buf + offset); |
Theodore Ts'o | 8a48035 | 2009-06-21 21:07:38 -0400 | [diff] [blame^] | 411 | errcode = ext2fs_get_rec_len(fs, dirent, &rec_len); |
| 412 | if (errcode) { |
| 413 | com_err("htree_dump_leaf_inode", errcode, |
| 414 | "while getting rec_len for block %lu", |
| 415 | (unsigned long) *blocknr); |
| 416 | return; |
| 417 | } |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 418 | if (dirent->inode && |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 419 | p->len == (dirent->name_len & 0xFF) && |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 420 | strncmp(p->search_name, dirent->name, |
| 421 | p->len) == 0) { |
| 422 | printf("Entry found at logical block %lld, " |
Andreas Dilger | de8f3a7 | 2007-05-25 11:18:11 -0400 | [diff] [blame] | 423 | "phys %u, offset %u\n", (long long)blockcnt, |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 424 | *blocknr, offset); |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 425 | printf("offset %u\n", offset); |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 426 | return BLOCK_ABORT; |
| 427 | } |
Theodore Ts'o | 5dd77db | 2008-08-25 21:08:19 -0400 | [diff] [blame] | 428 | offset += rec_len; |
Theodore Ts'o | df614db | 2002-02-25 04:28:45 -0500 | [diff] [blame] | 429 | } |
| 430 | return 0; |
| 431 | } |
| 432 | |