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