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];
39 char tmp[EXT2_NAME_LEN + 16];
43 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
45 com_err("htree_dump_leaf_node", errcode,
46 "while mapping logical block %d\n", blk);
50 errcode = ext2fs_read_dir_block2(current_fs, pblk, buf, 0);
52 com_err("htree_dump_leaf_node", errcode,
53 "while reading block %d\n", blk);
57 while (offset < fs->blocksize) {
58 dirent = (struct ext2_dir_entry *) (buf + offset);
59 if (((offset + dirent->rec_len) > fs->blocksize) ||
60 (dirent->rec_len < 8) ||
61 ((dirent->rec_len % 4) != 0) ||
62 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
63 fprintf(pager, "Corrupted directory block (%d)!\n", blk);
66 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
67 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
68 strncpy(name, dirent->name, thislen);
70 errcode = ext2fs_dirhash(rootnode->hash_version, name,
71 thislen, fs->super->s_hash_seed,
74 com_err("htree_dump_leaf_node", errcode,
75 "while calculating hash");
76 sprintf(tmp, "%u 0x%08x (%d) %s ", dirent->inode,
77 hash, dirent->rec_len, name);
78 thislen = strlen(tmp);
79 if (col + thislen > 80) {
83 fprintf(pager, "%s", tmp);
85 offset += dirent->rec_len;
91 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
92 struct ext2_inode *inode,
93 struct ext2_dx_root_info * rootnode,
94 blk_t blk, char *buf, int level);
97 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
98 struct ext2_inode *inode,
99 struct ext2_dx_root_info * rootnode,
100 struct ext2_dx_entry *ent,
101 char *buf, int level)
103 struct ext2_dx_countlimit limit;
104 struct ext2_dx_entry e;
108 limit = *((struct ext2_dx_countlimit *) ent);
109 limit.count = ext2fs_le16_to_cpu(limit.count);
110 limit.limit = ext2fs_le16_to_cpu(limit.limit);
112 fprintf(pager, "Number of entries (count): %d\n", limit.count);
113 fprintf(pager, "Number of entries (limit): %d\n", limit.limit);
115 for (i=0; i < limit.count; i++) {
116 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
117 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %d\n", i,
118 hash, (hash & 1) ? " (**)" : "",
119 ext2fs_le32_to_cpu(ent[i].block));
122 fprintf(pager, "\n");
124 for (i=0; i < limit.count; i++) {
125 e.hash = ext2fs_le32_to_cpu(ent[i].hash);
126 e.block = ext2fs_le32_to_cpu(ent[i].block);
127 fprintf(pager, "Entry #%d: Hash 0x%08x, block %d\n", i,
128 i ? e.hash : 0, e.block);
130 htree_dump_int_block(fs, ino, inode, rootnode,
131 e.block, buf, level-1);
133 htree_dump_leaf_node(fs, ino, inode, rootnode,
137 fprintf(pager, "---------------------\n");
140 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
141 struct ext2_inode *inode,
142 struct ext2_dx_root_info * rootnode,
143 blk_t blk, char *buf, int level)
149 cbuf = malloc(fs->blocksize);
151 fprintf(pager, "Couldn't allocate child block.\n");
155 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
157 com_err("htree_dump_int_block", errcode,
158 "while mapping logical block %d\n", blk);
162 errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf);
164 com_err("htree_dump_int_block", errcode,
165 "while reading block %d\n", blk);
169 htree_dump_int_node(fs, ino, inode, rootnode,
170 (struct ext2_dx_entry *) (buf+8),
177 void do_htree_dump(int argc, char *argv[])
180 struct ext2_inode inode;
184 struct ext2_dx_root_info *rootnode;
185 struct ext2_dx_entry *ent;
186 struct ext2_dx_countlimit *limit;
189 if (check_fs_open(argv[0]))
192 pager = open_pager();
195 while ((c = getopt (argc, argv, "l")) != EOF) {
203 if (argc > optind+1) {
204 com_err(0, 0, "Usage: htree_dump [-l] file");
211 ino = string_to_inode(argv[optind]);
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 = io_channel_read_blk(current_fs->io, inode.i_block[0],
237 com_err(argv[0], errcode, "Error reading root node");
241 rootnode = (struct ext2_dx_root_info *) (buf + 24);
243 fprintf(pager, "Root node dump:\n");
244 fprintf(pager, "\t Reserved zero: %d\n", rootnode->reserved_zero);
245 fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
246 fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
247 fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
248 fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
250 ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
251 limit = (struct ext2_dx_countlimit *) ent;
253 htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
254 buf + current_fs->blocksize,
255 rootnode->indirect_levels);
264 * This function prints the hash of a given file.
266 void do_dx_hash(int argc, char *argv[])
268 ext2_dirhash_t hash, minor_hash;
271 int hash_version = 0;
274 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
277 while ((c = getopt (argc, argv, "h:")) != EOF) {
280 hash_version = atoi(optarg);
284 if (optind != argc-1) {
285 com_err(argv[0], 0, "usage: dx_hash filename");
288 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
289 hash_seed, &hash, &minor_hash);
291 com_err(argv[0], err, "while caclulating hash");
294 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
299 * Search for particular directory entry (useful for debugging very
300 * large hash tree directories that have lost some blocks from the
303 struct process_block_struct {
309 static 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);
313 void do_dirsearch(int argc, char *argv[])
316 struct process_block_struct pb;
318 if (check_fs_open(argv[0]))
322 com_err(0, 0, "Usage: dirsearch dir filename");
326 inode = string_to_inode(argv[1]);
330 pb.buf = malloc(current_fs->blocksize);
332 com_err("dirsearch", 0, "Couldn't allocate buffer");
335 pb.search_name = argv[2];
336 pb.len = strlen(pb.search_name);
338 ext2fs_block_iterate2(current_fs, inode, 0, 0, search_dir_block, &pb);
344 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
345 e2_blkcnt_t blockcnt,
346 blk_t ref_blk EXT2FS_ATTR((unused)),
347 int ref_offset EXT2FS_ATTR((unused)),
350 struct process_block_struct *p;
351 struct ext2_dir_entry *dirent;
353 unsigned int offset = 0;
358 p = (struct process_block_struct *) priv_data;
360 errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf);
362 com_err("search_dir_block", errcode,
363 "while reading block %lu", (unsigned long) *blocknr);
367 while (offset < fs->blocksize) {
368 dirent = (struct ext2_dir_entry *) (p->buf + offset);
371 p->len == (dirent->name_len & 0xFF) &&
372 strncmp(p->search_name, dirent->name,
374 printf("Entry found at logical block %lld, "
375 "phys %d, offset %d\n", blockcnt,
377 printf("offset %d\n", offset);
380 offset += dirent->rec_len;