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>
25 extern int optreset; /* defined by BSD, but not others */
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 * root,
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];
45 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
47 com_err("htree_dump_leaf_node", errcode,
48 "while mapping logical block %d\n", blk);
52 errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf);
54 com_err("htree_dump_leaf_node", errcode,
55 "while reading block %d\n", blk);
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);
68 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
69 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
70 strncpy(name, dirent->name, thislen);
72 errcode = ext2fs_dirhash(root->hash_version, name, thislen,
73 fs->super->s_hash_seed,
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) {
85 fprintf(pager, "%s", tmp);
87 offset += dirent->rec_len;
93 static 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);
99 static 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)
105 struct ext2_dx_countlimit limit;
106 struct ext2_dx_entry e;
110 limit = *((struct ext2_dx_countlimit *) ent);
111 limit.count = ext2fs_le16_to_cpu(limit.count);
112 limit.limit = ext2fs_le16_to_cpu(limit.limit);
114 fprintf(pager, "Number of entries (count): %d\n", limit.count);
115 fprintf(pager, "Number of entries (limit): %d\n", limit.limit);
117 for (i=0; i < limit.count; i++)
118 fprintf(pager, "Entry #%d: Hash 0x%08x, block %d\n", i,
119 i ? ext2fs_le32_to_cpu(ent[i].hash) : 0,
120 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, root,
131 e.block, buf, level-1);
133 htree_dump_leaf_node(fs, ino, inode, root,
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 * root,
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, root,
170 (struct ext2_dx_entry *) (buf+8),
177 void do_htree_dump(int argc, char *argv[])
180 struct ext2_inode inode;
186 struct ext2_dx_root_info *root;
187 struct ext2_dx_entry *ent;
188 struct ext2_dx_countlimit *limit;
191 if (check_fs_open(argv[0]))
194 pager = open_pager();
198 optreset = 1; /* Makes BSD getopt happy */
200 while ((c = getopt (argc, argv, "l")) != EOF) {
208 if (argc > optind+1) {
209 com_err(0, 0, "Usage: htree_dump [-l] file");
216 ino = string_to_inode(argv[optind]);
220 if (debugfs_read_inode(ino, &inode, argv[1]))
223 if (!LINUX_S_ISDIR(inode.i_mode)) {
224 com_err(argv[0], 0, "Not a directory");
228 if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
229 com_err(argv[0], 0, "Not a hash-indexed directory");
233 buf = malloc(2*current_fs->blocksize);
235 com_err(argv[0], 0, "Couldn't allocate htree buffer");
239 errcode = io_channel_read_blk(current_fs->io, inode.i_block[0],
242 com_err(argv[0], errcode, "Error reading root node");
246 root = (struct ext2_dx_root_info *) (buf + 24);
248 fprintf(pager, "Root node dump:\n");
249 fprintf(pager, "\t Reserved zero: %d\n", root->reserved_zero);
250 fprintf(pager, "\t Hash Version: %d\n", root->hash_version);
251 fprintf(pager, "\t Info length: %d\n", root->info_length);
252 fprintf(pager, "\t Indirect levels: %d\n", root->indirect_levels);
253 fprintf(pager, "\t Flags: %d\n", root->unused_flags);
255 ent = (struct ext2_dx_entry *) (buf + 24 + root->info_length);
256 limit = (struct ext2_dx_countlimit *) ent;
258 htree_dump_int_node(current_fs, ino, &inode, root, ent,
259 buf + current_fs->blocksize,
260 root->indirect_levels);
269 * This function prints the hash of a given file.
271 void do_dx_hash(int argc, char *argv[])
273 ext2_dirhash_t hash, minor_hash;
276 int hash_version = 0;
279 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
282 optreset = 1; /* Makes BSD getopt happy */
284 while ((c = getopt (argc, argv, "h:")) != EOF) {
287 hash_version = atoi(optarg);
291 if (optind != argc-1) {
292 com_err(argv[0], 0, "usage: dx_hash filename");
295 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
296 hash_seed, &hash, &minor_hash);
298 com_err(argv[0], err, "while caclulating hash");
301 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
306 * Search for particular directory entry (useful for debugging very
307 * large hash tree directories that have lost some blocks from the
310 struct process_block_struct {
316 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
317 e2_blkcnt_t blockcnt, blk_t ref_blk,
318 int ref_offset, void *priv_data);
320 void do_dirsearch(int argc, char *argv[])
326 struct process_block_struct pb;
328 if (check_fs_open(argv[0]))
332 com_err(0, 0, "Usage: dirsearch dir filename");
336 inode = string_to_inode(argv[1]);
340 pb.buf = malloc(current_fs->blocksize);
342 com_err("dirsearch", 0, "Couldn't allocate buffer");
345 pb.search_name = argv[2];
346 pb.len = strlen(pb.search_name);
348 ext2fs_block_iterate2(current_fs, inode, 0, 0, search_dir_block, &pb);
354 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
355 e2_blkcnt_t blockcnt, blk_t ref_blk,
356 int ref_offset, void *priv_data)
358 struct process_block_struct *p;
359 struct ext2_dir_entry *dirent;
366 p = (struct process_block_struct *) priv_data;
368 errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf);
370 com_err("search_dir_block", errcode,
371 "while reading block %lu", *blocknr);
375 while (offset < fs->blocksize) {
376 dirent = (struct ext2_dir_entry *) (p->buf + offset);
379 p->len == (dirent->name_len & 0xFF) &&
380 strncmp(p->search_name, dirent->name,
382 printf("Entry found at logical block %lld, "
383 "phys %d, offset %d\n", blockcnt,
385 printf("offset %d\n", offset);
388 offset += dirent->rec_len;