htree.c revision 503f9e7f6eb331c5b75d7f1ad126f71bcdcfb4e3
1/* 2 * htree.c --- hash tree routines 3 * 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> 20#else 21extern int optind; 22extern char *optarg; 23#endif 24#ifdef HAVE_OPTRESET 25extern int optreset; /* defined by BSD, but not others */ 26#endif 27 28#include "debugfs.h" 29 30static FILE *pager; 31 32static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino, 33 struct ext2_inode *inode, 34 struct ext2_dx_root_info * root, 35 blk_t blk, char *buf) 36{ 37 errcode_t errcode; 38 struct ext2_dir_entry *dirent; 39 int thislen, col = 0, offset = 0; 40 char name[EXT2_NAME_LEN]; 41 char tmp[EXT2_NAME_LEN + 16]; 42 blk_t pblk; 43 ext2_dirhash_t hash; 44 45 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk); 46 if (errcode) { 47 com_err("htree_dump_leaf_node", errcode, 48 "while mapping logical block %d\n", blk); 49 return; 50 } 51 52 errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf); 53 if (errcode) { 54 com_err("htree_dump_leaf_node", errcode, 55 "while reading block %d\n", blk); 56 return; 57 } 58 59 while (offset < fs->blocksize) { 60 dirent = (struct ext2_dir_entry *) (buf + offset); 61 if (((offset + dirent->rec_len) > fs->blocksize) || 62 (dirent->rec_len < 8) || 63 ((dirent->rec_len % 4) != 0) || 64 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) { 65 fprintf(pager, "Corrupted directory block (%d)!\n", blk); 66 break; 67 } 68 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ? 69 (dirent->name_len & 0xFF) : EXT2_NAME_LEN; 70 strncpy(name, dirent->name, thislen); 71 name[thislen] = '\0'; 72 errcode = ext2fs_dirhash(root->hash_version, name, thislen, 73 fs->super->s_hash_seed, 74 &hash, 0); 75 if (errcode) 76 com_err("htree_dump_leaf_node", errcode, 77 "while calculating hash"); 78 sprintf(tmp, "%u 0x%08x (%d) %s ", dirent->inode, 79 hash, dirent->rec_len, name); 80 thislen = strlen(tmp); 81 if (col + thislen > 80) { 82 fprintf(pager, "\n"); 83 col = 0; 84 } 85 fprintf(pager, "%s", tmp); 86 col += thislen; 87 offset += dirent->rec_len; 88 } 89 fprintf(pager, "\n"); 90} 91 92 93static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino, 94 struct ext2_inode *inode, 95 struct ext2_dx_root_info * root, 96 blk_t blk, char *buf, int level); 97 98 99static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino, 100 struct ext2_inode *inode, 101 struct ext2_dx_root_info * root, 102 struct ext2_dx_entry *ent, 103 char *buf, int level) 104{ 105 struct ext2_dx_countlimit *limit; 106 int i; 107 108 limit = (struct ext2_dx_countlimit *) ent; 109 110 fprintf(pager, "Number of entries (count): %d\n", limit->count); 111 fprintf(pager, "Number of entries (limit): %d\n", limit->limit); 112 113 for (i=0; i < limit->count; i++) 114 fprintf(pager, "Entry #%d: Hash 0x%08x, block %d\n", i, 115 i ? ent[i].hash : 0, ent[i].block); 116 117 fprintf(pager, "\n"); 118 119 for (i=0; i < limit->count; i++) { 120 fprintf(pager, "Entry #%d: Hash 0x%08x, block %d\n", i, 121 i ? ent[i].hash : 0, ent[i].block); 122 if (level) 123 htree_dump_int_block(fs, ino, inode, root, 124 ent[i].block, buf, level-1); 125 else 126 htree_dump_leaf_node(fs, ino, inode, root, 127 ent[i].block, buf); 128 } 129 130 fprintf(pager, "---------------------\n"); 131} 132 133static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino, 134 struct ext2_inode *inode, 135 struct ext2_dx_root_info * root, 136 blk_t blk, char *buf, int level) 137{ 138 char *cbuf; 139 errcode_t errcode; 140 blk_t pblk; 141 142 cbuf = malloc(fs->blocksize); 143 if (!cbuf) { 144 fprintf(pager, "Couldn't allocate child block.\n"); 145 return; 146 } 147 148 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk); 149 if (errcode) { 150 com_err("htree_dump_int_block", errcode, 151 "while mapping logical block %d\n", blk); 152 return; 153 } 154 155 errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf); 156 if (errcode) { 157 com_err("htree_dump_int_block", errcode, 158 "while reading block %d\n", blk); 159 return; 160 } 161 162 htree_dump_int_node(fs, ino, inode, root, 163 (struct ext2_dx_entry *) (buf+8), 164 cbuf, level); 165 free(cbuf); 166} 167 168 169 170void do_htree_dump(int argc, char *argv[]) 171{ 172 ext2_ino_t ino; 173 struct ext2_inode inode; 174 int retval; 175 int i, c; 176 int flags; 177 int long_opt; 178 void *buf = NULL; 179 struct ext2_dx_root_info *root; 180 struct ext2_dx_entry *ent; 181 struct ext2_dx_countlimit *limit; 182 errcode_t errcode; 183 184 if (check_fs_open(argv[0])) 185 return; 186 187 pager = open_pager(); 188 189 optind = 0; 190#ifdef HAVE_OPTRESET 191 optreset = 1; /* Makes BSD getopt happy */ 192#endif 193 while ((c = getopt (argc, argv, "l")) != EOF) { 194 switch (c) { 195 case 'l': 196 long_opt++; 197 break; 198 } 199 } 200 201 if (argc > optind+1) { 202 com_err(0, 0, "Usage: htree_dump [-l] file"); 203 return; 204 } 205 206 if (argc == optind) 207 ino = cwd; 208 else 209 ino = string_to_inode(argv[optind]); 210 if (!ino) 211 return; 212 213 if (debugfs_read_inode(ino, &inode, argv[1])) 214 return; 215 216 if (!LINUX_S_ISDIR(inode.i_mode)) { 217 com_err(argv[0], 0, "Not a directory"); 218 return; 219 } 220 221 if ((inode.i_flags & EXT2_BTREE_FL) == 0) { 222 com_err(argv[0], 0, "Not a hash-indexed directory"); 223 return; 224 } 225 226 buf = malloc(2*current_fs->blocksize); 227 if (!buf) { 228 com_err(argv[0], 0, "Couldn't allocate htree buffer"); 229 return; 230 } 231 232 errcode = io_channel_read_blk(current_fs->io, inode.i_block[0], 233 1, buf); 234 if (errcode) { 235 com_err(argv[0], errcode, "Error reading root node"); 236 goto errout; 237 } 238 239 root = (struct ext2_dx_root_info *) (buf + 24); 240 241 fprintf(pager, "Root node dump:\n"); 242 fprintf(pager, "\t Reserved zero: %d\n", root->reserved_zero); 243 fprintf(pager, "\t Hash Version: %d\n", root->hash_version); 244 fprintf(pager, "\t Info length: %d\n", root->info_length); 245 fprintf(pager, "\t Indirect levels: %d\n", root->indirect_levels); 246 fprintf(pager, "\t Flags: %d\n", root->unused_flags); 247 248 ent = (struct ext2_dx_entry *) (buf + 24 + root->info_length); 249 limit = (struct ext2_dx_countlimit *) ent; 250 251 htree_dump_int_node(current_fs, ino, &inode, root, ent, 252 buf + current_fs->blocksize, 253 root->indirect_levels); 254 255errout: 256 if (buf) 257 free(buf); 258 close_pager(pager); 259} 260 261/* 262 * This function prints the hash of a given file. 263 */ 264void do_dx_hash(int argc, char *argv[]) 265{ 266 ext2_dirhash_t hash, minor_hash; 267 errcode_t err; 268 int c; 269 int hash_version = 0; 270 __u32 hash_seed[4]; 271 272 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0; 273 optind = 0; 274#ifdef HAVE_OPTRESET 275 optreset = 1; /* Makes BSD getopt happy */ 276#endif 277 while ((c = getopt (argc, argv, "h:")) != EOF) { 278 switch (c) { 279 case 'h': 280 hash_version = atoi(optarg); 281 break; 282 } 283 } 284 if (optind != argc-1) { 285 com_err(argv[0], 0, "usage: dx_hash filename"); 286 return; 287 } 288 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]), 289 hash_seed, &hash, &minor_hash); 290 if (err) { 291 com_err(argv[0], err, "while caclulating hash"); 292 return; 293 } 294 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind], 295 hash, minor_hash); 296} 297 298/* 299 * Search for particular directory entry (useful for debugging very 300 * large hash tree directories that have lost some blocks from the 301 * btree index). 302 */ 303struct process_block_struct { 304 char *search_name; 305 char *buf; 306 int len; 307}; 308 309static int search_dir_block(ext2_filsys fs, blk_t *blocknr, 310 e2_blkcnt_t blockcnt, blk_t ref_blk, 311 int ref_offset, void *priv_data); 312 313void do_dirsearch(int argc, char *argv[]) 314{ 315 ext2_ino_t inode; 316 int retval; 317 int c; 318 int flags; 319 struct process_block_struct pb; 320 321 if (check_fs_open(argv[0])) 322 return; 323 324 if (argc != 3) { 325 com_err(0, 0, "Usage: dirsearch dir filename"); 326 return; 327 } 328 329 inode = string_to_inode(argv[1]); 330 if (!inode) 331 return; 332 333 pb.buf = malloc(current_fs->blocksize); 334 if (!pb.buf) { 335 com_err("dirsearch", 0, "Couldn't allocate buffer"); 336 return; 337 } 338 pb.search_name = argv[2]; 339 pb.len = strlen(pb.search_name); 340 341 ext2fs_block_iterate2(current_fs, inode, 0, 0, search_dir_block, &pb); 342 343 free(pb.buf); 344} 345 346 347static int search_dir_block(ext2_filsys fs, blk_t *blocknr, 348 e2_blkcnt_t blockcnt, blk_t ref_blk, 349 int ref_offset, void *priv_data) 350{ 351 struct process_block_struct *p; 352 struct ext2_dir_entry *dirent; 353 errcode_t errcode; 354 int offset = 0; 355 356 if (blockcnt < 0) 357 return 0; 358 359 p = (struct process_block_struct *) priv_data; 360 361 errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf); 362 if (errcode) { 363 com_err("search_dir_block", errcode, 364 "while reading block %lu", *blocknr); 365 return BLOCK_ABORT; 366 } 367 368 while (offset < fs->blocksize) { 369 dirent = (struct ext2_dir_entry *) (p->buf + offset); 370 371 if (dirent->inode && 372 p->len == (dirent->name_len & 0xFF) && 373 strncmp(p->search_name, dirent->name, 374 p->len) == 0) { 375 printf("Entry found at logical block %lld, " 376 "phys %d, offset %d\n", blockcnt, 377 *blocknr, offset); 378 printf("offset %d\n", offset); 379 return BLOCK_ABORT; 380 } 381 offset += dirent->rec_len; 382 } 383 return 0; 384} 385 386