2 * htree.c --- hash tree routines
4 * Copyright (C) 2002 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
17 #include <sys/types.h>
29 static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
30 struct ext2_inode *inode,
31 struct ext2_dx_root_info * rootnode,
35 struct ext2_dir_entry *dirent;
37 unsigned int offset = 0;
38 char name[EXT2_NAME_LEN + 1];
39 char tmp[EXT2_NAME_LEN + 16];
41 ext2_dirhash_t hash, minor_hash;
42 int rec_len, hash_alg;
44 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
46 com_err("htree_dump_leaf_node", errcode,
47 "while mapping logical block %u\n", blk);
51 printf("Reading directory block %lu, phys %lu\n", blk, pblk);
52 errcode = ext2fs_read_dir_block2(current_fs, pblk, buf, 0);
54 com_err("htree_dump_leaf_node", errcode,
55 "while reading block %lu (%lu)\n", blk, pblk);
58 hash_alg = rootnode->hash_version;
59 if ((hash_alg <= EXT2_HASH_TEA) &&
60 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
63 while (offset < fs->blocksize) {
64 dirent = (struct ext2_dir_entry *) (buf + offset);
65 rec_len = (dirent->rec_len || fs->blocksize < 65536) ?
66 dirent->rec_len : 65536;
67 if (((offset + rec_len) > fs->blocksize) ||
69 ((rec_len % 4) != 0) ||
70 (((dirent->name_len & 0xFF)+8) > rec_len)) {
71 fprintf(pager, "Corrupted directory block (%u)!\n", blk);
74 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
75 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
76 strncpy(name, dirent->name, thislen);
78 errcode = ext2fs_dirhash(hash_alg, name,
79 thislen, fs->super->s_hash_seed,
82 com_err("htree_dump_leaf_node", errcode,
83 "while calculating hash");
84 sprintf(tmp, "%u 0x%08x-%08x (%d) %s ", dirent->inode,
85 hash, minor_hash, rec_len, name);
86 thislen = strlen(tmp);
87 if (col + thislen > 80) {
91 fprintf(pager, "%s", tmp);
99 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
100 struct ext2_inode *inode,
101 struct ext2_dx_root_info * rootnode,
102 blk_t blk, char *buf, int level);
105 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
106 struct ext2_inode *inode,
107 struct ext2_dx_root_info * rootnode,
108 struct ext2_dx_entry *ent,
109 char *buf, int level)
111 struct ext2_dx_countlimit limit;
112 struct ext2_dx_entry e;
116 limit = *((struct ext2_dx_countlimit *) ent);
117 limit.count = ext2fs_le16_to_cpu(limit.count);
118 limit.limit = ext2fs_le16_to_cpu(limit.limit);
120 fprintf(pager, "Number of entries (count): %d\n", limit.count);
121 fprintf(pager, "Number of entries (limit): %d\n", limit.limit);
123 for (i=0; i < limit.count; i++) {
124 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
125 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
126 hash, (hash & 1) ? " (**)" : "",
127 ext2fs_le32_to_cpu(ent[i].block));
130 fprintf(pager, "\n");
132 for (i=0; i < limit.count; i++) {
133 e.hash = ext2fs_le32_to_cpu(ent[i].hash);
134 e.block = ext2fs_le32_to_cpu(ent[i].block);
135 fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
136 i ? e.hash : 0, e.block);
138 htree_dump_int_block(fs, ino, inode, rootnode,
139 e.block, buf, level-1);
141 htree_dump_leaf_node(fs, ino, inode, rootnode,
145 fprintf(pager, "---------------------\n");
148 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
149 struct ext2_inode *inode,
150 struct ext2_dx_root_info * rootnode,
151 blk_t blk, char *buf, int level)
157 cbuf = malloc(fs->blocksize);
159 fprintf(pager, "Couldn't allocate child block.\n");
163 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
165 com_err("htree_dump_int_block", errcode,
166 "while mapping logical block %u\n", blk);
170 errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf);
172 com_err("htree_dump_int_block", errcode,
173 "while reading block %u\n", blk);
177 htree_dump_int_node(fs, ino, inode, rootnode,
178 (struct ext2_dx_entry *) (buf+8),
186 void do_htree_dump(int argc, char *argv[])
189 struct ext2_inode inode;
194 struct ext2_dx_root_info *rootnode;
195 struct ext2_dx_entry *ent;
196 struct ext2_dx_countlimit *limit;
199 if (check_fs_open(argv[0]))
202 pager = open_pager();
205 while ((c = getopt (argc, argv, "l")) != EOF) {
215 if (argc > optind+1) {
217 com_err(0, 0, "Usage: htree_dump [-l] file");
224 ino = string_to_inode(argv[optind]);
228 if (debugfs_read_inode(ino, &inode, argv[1]))
231 if (!LINUX_S_ISDIR(inode.i_mode)) {
232 com_err(argv[0], 0, "Not a directory");
236 if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
237 com_err(argv[0], 0, "Not a hash-indexed directory");
241 buf = malloc(2*current_fs->blocksize);
243 com_err(argv[0], 0, "Couldn't allocate htree buffer");
247 errcode = ext2fs_bmap(current_fs, ino, &inode, buf, 0, 0, &blk);
249 com_err("do_htree_block", errcode,
250 "while mapping logical block 0\n");
254 errcode = io_channel_read_blk(current_fs->io, blk,
257 com_err(argv[0], errcode, "Error reading root node");
261 rootnode = (struct ext2_dx_root_info *) (buf + 24);
263 fprintf(pager, "Root node dump:\n");
264 fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
265 fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
266 fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
267 fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
268 fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
270 ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
271 limit = (struct ext2_dx_countlimit *) ent;
273 htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
274 buf + current_fs->blocksize,
275 rootnode->indirect_levels);
284 * This function prints the hash of a given file.
286 void do_dx_hash(int argc, char *argv[])
288 ext2_dirhash_t hash, minor_hash;
291 int hash_version = 0;
294 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
297 while ((c = getopt (argc, argv, "h:s:")) != EOF) {
300 hash_version = e2p_string2hash(optarg);
301 if (hash_version < 0)
302 hash_version = atoi(optarg);
305 if (uuid_parse(optarg, hash_seed)) {
306 fprintf(stderr, "Invalid UUID format: %s\n",
315 if (optind != argc-1) {
317 com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
318 "[-s hash_seed] filename");
321 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
322 hash_seed, &hash, &minor_hash);
324 com_err(argv[0], err, "while caclulating hash");
327 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
332 * Search for particular directory entry (useful for debugging very
333 * large hash tree directories that have lost some blocks from the
336 struct process_block_struct {
342 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
343 e2_blkcnt_t blockcnt, blk_t ref_blk,
344 int ref_offset, void *priv_data);
346 void do_dirsearch(int argc, char *argv[])
349 struct process_block_struct pb;
351 if (check_fs_open(argv[0]))
355 com_err(0, 0, "Usage: dirsearch dir filename");
359 inode = string_to_inode(argv[1]);
363 pb.buf = malloc(current_fs->blocksize);
365 com_err("dirsearch", 0, "Couldn't allocate buffer");
368 pb.search_name = argv[2];
369 pb.len = strlen(pb.search_name);
371 ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
372 search_dir_block, &pb);
378 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
379 e2_blkcnt_t blockcnt,
380 blk_t ref_blk EXT2FS_ATTR((unused)),
381 int ref_offset EXT2FS_ATTR((unused)),
384 struct process_block_struct *p;
385 struct ext2_dir_entry *dirent;
387 unsigned int offset = 0;
393 p = (struct process_block_struct *) priv_data;
395 errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf);
397 com_err("search_dir_block", errcode,
398 "while reading block %lu", (unsigned long) *blocknr);
402 while (offset < fs->blocksize) {
403 dirent = (struct ext2_dir_entry *) (p->buf + offset);
404 rec_len = (dirent->rec_len || fs->blocksize < 65536) ?
405 dirent->rec_len : 65536;
407 p->len == (dirent->name_len & 0xFF) &&
408 strncmp(p->search_name, dirent->name,
410 printf("Entry found at logical block %lld, "
411 "phys %u, offset %u\n", (long long)blockcnt,
413 printf("offset %u\n", offset);