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.
18 #include <sys/types.h>
27 #include "uuid/uuid.h"
32 static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
33 struct ext2_inode *inode,
34 struct ext2_dx_root_info * rootnode,
35 blk64_t blk, char *buf)
38 struct ext2_dir_entry *dirent;
40 unsigned int offset = 0;
41 char name[EXT2_NAME_LEN + 1];
42 char tmp[EXT2_NAME_LEN + 64];
44 ext2_dirhash_t hash, minor_hash;
48 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
50 com_err("htree_dump_leaf_node", errcode,
51 "while mapping logical block %llu\n", blk);
55 printf("Reading directory block %llu, phys %llu\n", blk, pblk);
56 errcode = ext2fs_read_dir_block2(current_fs, pblk, buf, 0);
58 com_err("htree_dump_leaf_node", errcode,
59 "while reading block %llu (%llu)\n",
63 hash_alg = rootnode->hash_version;
64 if ((hash_alg <= EXT2_HASH_TEA) &&
65 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
68 while (offset < fs->blocksize) {
69 dirent = (struct ext2_dir_entry *) (buf + offset);
70 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
72 com_err("htree_dump_leaf_inode", errcode,
73 "while getting rec_len for block %lu",
77 if (((offset + rec_len) > fs->blocksize) ||
79 ((rec_len % 4) != 0) ||
80 ((((unsigned) dirent->name_len & 0xFF)+8) > rec_len)) {
81 fprintf(pager, "Corrupted directory block (%llu)!\n",
85 thislen = dirent->name_len & 0xFF;
86 strncpy(name, dirent->name, thislen);
88 errcode = ext2fs_dirhash(hash_alg, name,
89 thislen, fs->super->s_hash_seed,
92 com_err("htree_dump_leaf_node", errcode,
93 "while calculating hash");
94 snprintf(tmp, EXT2_NAME_LEN + 64, "%u 0x%08x-%08x (%d) %s ",
95 dirent->inode, hash, minor_hash, rec_len, name);
96 thislen = strlen(tmp);
97 if (col + thislen > 80) {
101 fprintf(pager, "%s", tmp);
105 fprintf(pager, "\n");
109 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
110 struct ext2_inode *inode,
111 struct ext2_dx_root_info * rootnode,
112 blk64_t blk, char *buf, int level);
115 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
116 struct ext2_inode *inode,
117 struct ext2_dx_root_info * rootnode,
118 struct ext2_dx_entry *ent,
119 char *buf, int level)
121 struct ext2_dx_countlimit limit;
122 struct ext2_dx_entry e;
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);
130 fprintf(pager, "Number of entries (count): %d\n", limit.count);
131 fprintf(pager, "Number of entries (limit): %d\n", limit.limit);
133 for (i=0; i < limit.count; i++) {
134 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
135 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
136 hash, (hash & 1) ? " (**)" : "",
137 ext2fs_le32_to_cpu(ent[i].block));
140 fprintf(pager, "\n");
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);
145 fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
146 i ? e.hash : 0, e.block);
148 htree_dump_int_block(fs, ino, inode, rootnode,
149 e.block, buf, level-1);
151 htree_dump_leaf_node(fs, ino, inode, rootnode,
155 fprintf(pager, "---------------------\n");
158 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
159 struct ext2_inode *inode,
160 struct ext2_dx_root_info * rootnode,
161 blk64_t blk, char *buf, int level)
167 cbuf = malloc(fs->blocksize);
169 fprintf(pager, "Couldn't allocate child block.\n");
173 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
175 com_err("htree_dump_int_block", errcode,
176 "while mapping logical block %llu\n", blk);
180 errcode = io_channel_read_blk64(current_fs->io, pblk, 1, buf);
182 com_err("htree_dump_int_block", errcode,
183 "while reading block %llu\n", blk);
187 htree_dump_int_node(fs, ino, inode, rootnode,
188 (struct ext2_dx_entry *) (buf+8),
196 void do_htree_dump(int argc, char *argv[])
199 struct ext2_inode inode;
202 struct ext2_dx_root_info *rootnode;
203 struct ext2_dx_entry *ent;
204 struct ext2_dx_countlimit *limit;
207 if (check_fs_open(argv[0]))
210 pager = open_pager();
212 if (common_inode_args_process(argc, argv, &ino, 0))
215 if (debugfs_read_inode(ino, &inode, argv[1]))
218 if (!LINUX_S_ISDIR(inode.i_mode)) {
219 com_err(argv[0], 0, "Not a directory");
223 if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
224 com_err(argv[0], 0, "Not a hash-indexed directory");
228 buf = malloc(2*current_fs->blocksize);
230 com_err(argv[0], 0, "Couldn't allocate htree buffer");
234 errcode = ext2fs_bmap2(current_fs, ino, &inode, buf, 0, 0, 0, &blk);
236 com_err("do_htree_block", errcode,
237 "while mapping logical block 0\n");
241 errcode = io_channel_read_blk64(current_fs->io, blk,
244 com_err(argv[0], errcode, "Error reading root node");
248 rootnode = (struct ext2_dx_root_info *) (buf + 24);
250 fprintf(pager, "Root node dump:\n");
251 fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
252 fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
253 fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
254 fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
255 fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
257 ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
258 limit = (struct ext2_dx_countlimit *) ent;
260 htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
261 buf + current_fs->blocksize,
262 rootnode->indirect_levels);
270 * This function prints the hash of a given file.
272 void do_dx_hash(int argc, char *argv[])
274 ext2_dirhash_t hash, minor_hash;
277 int hash_version = 0;
280 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
283 while ((c = getopt (argc, argv, "h:s:")) != EOF) {
286 hash_version = e2p_string2hash(optarg);
287 if (hash_version < 0)
288 hash_version = atoi(optarg);
291 if (uuid_parse(optarg, (unsigned char *) hash_seed)) {
292 fprintf(stderr, "Invalid UUID format: %s\n",
301 if (optind != argc-1) {
303 com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
304 "[-s hash_seed] filename");
307 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
308 hash_seed, &hash, &minor_hash);
310 com_err(argv[0], err, "while caclulating hash");
313 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
318 * Search for particular directory entry (useful for debugging very
319 * large hash tree directories that have lost some blocks from the
322 struct process_block_struct {
328 static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
329 e2_blkcnt_t blockcnt, blk64_t ref_blk,
330 int ref_offset, void *priv_data);
332 void do_dirsearch(int argc, char *argv[])
335 struct process_block_struct pb;
337 if (check_fs_open(argv[0]))
341 com_err(0, 0, "Usage: dirsearch dir filename");
345 inode = string_to_inode(argv[1]);
349 pb.buf = malloc(current_fs->blocksize);
351 com_err("dirsearch", 0, "Couldn't allocate buffer");
354 pb.search_name = argv[2];
355 pb.len = strlen(pb.search_name);
357 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
358 search_dir_block, &pb);
364 static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
365 e2_blkcnt_t blockcnt,
366 blk64_t ref_blk EXT2FS_ATTR((unused)),
367 int ref_offset EXT2FS_ATTR((unused)),
370 struct process_block_struct *p;
371 struct ext2_dir_entry *dirent;
373 unsigned int offset = 0;
374 unsigned int rec_len;
379 p = (struct process_block_struct *) priv_data;
381 errcode = io_channel_read_blk64(current_fs->io, *blocknr, 1, p->buf);
383 com_err("search_dir_block", errcode,
384 "while reading block %lu", (unsigned long) *blocknr);
388 while (offset < fs->blocksize) {
389 dirent = (struct ext2_dir_entry *) (p->buf + offset);
390 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
392 com_err("htree_dump_leaf_inode", errcode,
393 "while getting rec_len for block %lu",
394 (unsigned long) *blocknr);
398 p->len == (dirent->name_len & 0xFF) &&
399 strncmp(p->search_name, dirent->name,
401 printf("Entry found at logical block %lld, "
402 "phys %llu, offset %u\n", (long long)blockcnt,
404 printf("offset %u\n", offset);