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;
45 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
47 com_err("htree_dump_leaf_node", errcode,
48 "while mapping logical block %u\n", blk);
52 printf("Reading directory block %lu, phys %lu\n",
53 (unsigned long) blk, (unsigned long) pblk);
54 errcode = ext2fs_read_dir_block2(current_fs, pblk, buf, 0);
56 com_err("htree_dump_leaf_node", errcode,
57 "while reading block %lu (%lu)\n",
58 (unsigned long) blk, (unsigned long) pblk);
61 hash_alg = rootnode->hash_version;
62 if ((hash_alg <= EXT2_HASH_TEA) &&
63 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
66 while (offset < fs->blocksize) {
67 dirent = (struct ext2_dir_entry *) (buf + offset);
68 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
70 com_err("htree_dump_leaf_inode", errcode,
71 "while getting rec_len for block %lu",
75 if (((offset + rec_len) > fs->blocksize) ||
77 ((rec_len % 4) != 0) ||
78 (((dirent->name_len & 0xFF)+8) > rec_len)) {
79 fprintf(pager, "Corrupted directory block (%u)!\n", blk);
82 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
83 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
84 strncpy(name, dirent->name, thislen);
86 errcode = ext2fs_dirhash(hash_alg, name,
87 thislen, fs->super->s_hash_seed,
90 com_err("htree_dump_leaf_node", errcode,
91 "while calculating hash");
92 sprintf(tmp, "%u 0x%08x-%08x (%d) %s ", dirent->inode,
93 hash, minor_hash, rec_len, name);
94 thislen = strlen(tmp);
95 if (col + thislen > 80) {
99 fprintf(pager, "%s", tmp);
103 fprintf(pager, "\n");
107 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
108 struct ext2_inode *inode,
109 struct ext2_dx_root_info * rootnode,
110 blk_t blk, char *buf, int level);
113 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
114 struct ext2_inode *inode,
115 struct ext2_dx_root_info * rootnode,
116 struct ext2_dx_entry *ent,
117 char *buf, int level)
119 struct ext2_dx_countlimit limit;
120 struct ext2_dx_entry e;
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);
128 fprintf(pager, "Number of entries (count): %d\n", limit.count);
129 fprintf(pager, "Number of entries (limit): %d\n", limit.limit);
131 for (i=0; i < limit.count; i++) {
132 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
133 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
134 hash, (hash & 1) ? " (**)" : "",
135 ext2fs_le32_to_cpu(ent[i].block));
138 fprintf(pager, "\n");
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);
143 fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
144 i ? e.hash : 0, e.block);
146 htree_dump_int_block(fs, ino, inode, rootnode,
147 e.block, buf, level-1);
149 htree_dump_leaf_node(fs, ino, inode, rootnode,
153 fprintf(pager, "---------------------\n");
156 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
157 struct ext2_inode *inode,
158 struct ext2_dx_root_info * rootnode,
159 blk_t blk, char *buf, int level)
165 cbuf = malloc(fs->blocksize);
167 fprintf(pager, "Couldn't allocate child block.\n");
171 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
173 com_err("htree_dump_int_block", errcode,
174 "while mapping logical block %u\n", blk);
178 errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf);
180 com_err("htree_dump_int_block", errcode,
181 "while reading block %u\n", blk);
185 htree_dump_int_node(fs, ino, inode, rootnode,
186 (struct ext2_dx_entry *) (buf+8),
194 void do_htree_dump(int argc, char *argv[])
197 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();
213 while ((c = getopt (argc, argv, "l")) != EOF) {
223 if (argc > optind+1) {
225 com_err(0, 0, "Usage: htree_dump [-l] file");
232 ino = string_to_inode(argv[optind]);
236 if (debugfs_read_inode(ino, &inode, argv[1]))
239 if (!LINUX_S_ISDIR(inode.i_mode)) {
240 com_err(argv[0], 0, "Not a directory");
244 if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
245 com_err(argv[0], 0, "Not a hash-indexed directory");
249 buf = malloc(2*current_fs->blocksize);
251 com_err(argv[0], 0, "Couldn't allocate htree buffer");
255 errcode = ext2fs_bmap(current_fs, ino, &inode, buf, 0, 0, &blk);
257 com_err("do_htree_block", errcode,
258 "while mapping logical block 0\n");
262 errcode = io_channel_read_blk(current_fs->io, blk,
265 com_err(argv[0], errcode, "Error reading root node");
269 rootnode = (struct ext2_dx_root_info *) (buf + 24);
271 fprintf(pager, "Root node dump:\n");
272 fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
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);
278 ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
279 limit = (struct ext2_dx_countlimit *) ent;
281 htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
282 buf + current_fs->blocksize,
283 rootnode->indirect_levels);
291 * This function prints the hash of a given file.
293 void do_dx_hash(int argc, char *argv[])
295 ext2_dirhash_t hash, minor_hash;
298 int hash_version = 0;
301 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
304 while ((c = getopt (argc, argv, "h:s:")) != EOF) {
307 hash_version = e2p_string2hash(optarg);
308 if (hash_version < 0)
309 hash_version = atoi(optarg);
312 if (uuid_parse(optarg, hash_seed)) {
313 fprintf(stderr, "Invalid UUID format: %s\n",
322 if (optind != argc-1) {
324 com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
325 "[-s hash_seed] filename");
328 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
329 hash_seed, &hash, &minor_hash);
331 com_err(argv[0], err, "while caclulating hash");
334 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
339 * Search for particular directory entry (useful for debugging very
340 * large hash tree directories that have lost some blocks from the
343 struct process_block_struct {
349 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
350 e2_blkcnt_t blockcnt, blk_t ref_blk,
351 int ref_offset, void *priv_data);
353 void do_dirsearch(int argc, char *argv[])
356 struct process_block_struct pb;
358 if (check_fs_open(argv[0]))
362 com_err(0, 0, "Usage: dirsearch dir filename");
366 inode = string_to_inode(argv[1]);
370 pb.buf = malloc(current_fs->blocksize);
372 com_err("dirsearch", 0, "Couldn't allocate buffer");
375 pb.search_name = argv[2];
376 pb.len = strlen(pb.search_name);
378 ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
379 search_dir_block, &pb);
385 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
386 e2_blkcnt_t blockcnt,
387 blk_t ref_blk EXT2FS_ATTR((unused)),
388 int ref_offset EXT2FS_ATTR((unused)),
391 struct process_block_struct *p;
392 struct ext2_dir_entry *dirent;
394 unsigned int offset = 0;
395 unsigned int rec_len;
400 p = (struct process_block_struct *) priv_data;
402 errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf);
404 com_err("search_dir_block", errcode,
405 "while reading block %lu", (unsigned long) *blocknr);
409 while (offset < fs->blocksize) {
410 dirent = (struct ext2_dir_entry *) (p->buf + offset);
411 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
413 com_err("htree_dump_leaf_inode", errcode,
414 "while getting rec_len for block %lu",
415 (unsigned long) *blocknr);
419 p->len == (dirent->name_len & 0xFF) &&
420 strncmp(p->search_name, dirent->name,
422 printf("Entry found at logical block %lld, "
423 "phys %u, offset %u\n", (long long)blockcnt,
425 printf("offset %u\n", offset);