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;
49 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
50 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
51 csum_size = sizeof(struct ext2_dir_entry_tail);
53 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
55 com_err("htree_dump_leaf_node", errcode,
56 "while mapping logical block %llu\n", blk);
60 fprintf(pager, "Reading directory block %llu, phys %llu\n", blk, pblk);
61 errcode = ext2fs_read_dir_block4(current_fs, pblk, buf, 0, ino);
63 com_err("htree_dump_leaf_node", errcode,
64 "while reading block %llu (%llu)\n",
68 hash_alg = rootnode->hash_version;
69 if ((hash_alg <= EXT2_HASH_TEA) &&
70 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
73 while (offset < fs->blocksize) {
74 dirent = (struct ext2_dir_entry *) (buf + offset);
75 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
77 com_err("htree_dump_leaf_inode", errcode,
78 "while getting rec_len for block %lu",
82 thislen = ext2fs_dirent_name_len(dirent);
83 if (((offset + rec_len) > fs->blocksize) ||
85 ((rec_len % 4) != 0) ||
86 (thislen + 8 > rec_len)) {
87 fprintf(pager, "Corrupted directory block (%llu)!\n",
91 strncpy(name, dirent->name, thislen);
93 errcode = ext2fs_dirhash(hash_alg, name,
94 thislen, fs->super->s_hash_seed,
97 com_err("htree_dump_leaf_node", errcode,
98 "while calculating hash");
99 if ((offset == fs->blocksize - csum_size) &&
100 (dirent->inode == 0) &&
101 (dirent->rec_len == csum_size) &&
102 (dirent->name_len == EXT2_DIR_NAME_LEN_CSUM)) {
103 struct ext2_dir_entry_tail *t;
105 t = (struct ext2_dir_entry_tail *) dirent;
107 snprintf(tmp, EXT2_NAME_LEN + 64,
108 "leaf block checksum: 0x%08x ",
111 snprintf(tmp, EXT2_NAME_LEN + 64,
112 "%u 0x%08x-%08x (%d) %s ",
113 dirent->inode, hash, minor_hash,
116 thislen = strlen(tmp);
117 if (col + thislen > 80) {
118 fprintf(pager, "\n");
121 fprintf(pager, "%s", tmp);
125 fprintf(pager, "\n");
129 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
130 struct ext2_inode *inode,
131 struct ext2_dx_root_info * rootnode,
132 blk64_t blk, char *buf, int level);
135 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
136 struct ext2_inode *inode,
137 struct ext2_dx_root_info * rootnode,
138 struct ext2_dx_entry *ent,
139 char *buf, int level)
141 struct ext2_dx_countlimit dx_countlimit;
142 struct ext2_dx_tail *tail;
147 dx_countlimit = *((struct ext2_dx_countlimit *) ent);
148 count = ext2fs_le16_to_cpu(dx_countlimit.count);
149 limit = ext2fs_le16_to_cpu(dx_countlimit.limit);
151 fprintf(pager, "Number of entries (count): %d\n", count);
152 fprintf(pager, "Number of entries (limit): %d\n", limit);
154 remainder = fs->blocksize - (limit * sizeof(struct ext2_dx_entry));
155 if (ent == (struct ext2_dx_entry *)(rootnode + 1))
156 remainder -= sizeof(struct ext2_dx_root_info) + 24;
159 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
160 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
161 remainder == sizeof(struct ext2_dx_tail)) {
162 tail = (struct ext2_dx_tail *)(ent + limit);
163 fprintf(pager, "Checksum: 0x%08x\n",
164 ext2fs_le32_to_cpu(tail->dt_checksum));
167 for (i=0; i < count; i++) {
168 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
169 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
170 hash, (hash & 1) ? " (**)" : "",
171 ext2fs_le32_to_cpu(ent[i].block));
174 fprintf(pager, "\n");
176 for (i=0; i < count; i++) {
179 hash = ext2fs_le32_to_cpu(ent[i].hash);
180 block = ext2fs_le32_to_cpu(ent[i].block);
181 fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
182 i ? hash : 0, block);
184 htree_dump_int_block(fs, ino, inode, rootnode,
185 block, buf, level-1);
187 htree_dump_leaf_node(fs, ino, inode, rootnode,
191 fprintf(pager, "---------------------\n");
194 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
195 struct ext2_inode *inode,
196 struct ext2_dx_root_info * rootnode,
197 blk64_t blk, char *buf, int level)
203 cbuf = malloc(fs->blocksize);
205 fprintf(pager, "Couldn't allocate child block.\n");
209 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
211 com_err("htree_dump_int_block", errcode,
212 "while mapping logical block %llu\n", blk);
216 errcode = io_channel_read_blk64(current_fs->io, pblk, 1, buf);
218 com_err("htree_dump_int_block", errcode,
219 "while reading block %llu\n", blk);
223 htree_dump_int_node(fs, ino, inode, rootnode,
224 (struct ext2_dx_entry *) (buf+8),
232 void do_htree_dump(int argc, char *argv[])
235 struct ext2_inode inode;
238 struct ext2_dx_root_info *rootnode;
239 struct ext2_dx_entry *ent;
242 if (check_fs_open(argv[0]))
245 pager = open_pager();
247 if (common_inode_args_process(argc, argv, &ino, 0))
250 if (debugfs_read_inode(ino, &inode, argv[1]))
253 if (!LINUX_S_ISDIR(inode.i_mode)) {
254 com_err(argv[0], 0, "Not a directory");
258 if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
259 com_err(argv[0], 0, "Not a hash-indexed directory");
263 buf = malloc(2*current_fs->blocksize);
265 com_err(argv[0], 0, "Couldn't allocate htree buffer");
269 errcode = ext2fs_bmap2(current_fs, ino, &inode, buf, 0, 0, 0, &blk);
271 com_err("do_htree_block", errcode,
272 "while mapping logical block 0\n");
276 errcode = io_channel_read_blk64(current_fs->io, blk,
279 com_err(argv[0], errcode, "Error reading root node");
283 rootnode = (struct ext2_dx_root_info *) (buf + 24);
285 fprintf(pager, "Root node dump:\n");
286 fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
287 fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
288 fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
289 fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
290 fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
292 ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
294 htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
295 buf + current_fs->blocksize,
296 rootnode->indirect_levels);
304 * This function prints the hash of a given file.
306 void do_dx_hash(int argc, char *argv[])
308 ext2_dirhash_t hash, minor_hash;
311 int hash_version = 0;
314 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
317 while ((c = getopt (argc, argv, "h:s:")) != EOF) {
320 hash_version = e2p_string2hash(optarg);
321 if (hash_version < 0)
322 hash_version = atoi(optarg);
325 if (uuid_parse(optarg, (unsigned char *) hash_seed)) {
326 fprintf(stderr, "Invalid UUID format: %s\n",
335 if (optind != argc-1) {
337 com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
338 "[-s hash_seed] filename");
341 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
342 hash_seed, &hash, &minor_hash);
344 com_err(argv[0], err, "while caclulating hash");
347 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
352 * Search for particular directory entry (useful for debugging very
353 * large hash tree directories that have lost some blocks from the
356 struct process_block_struct {
362 static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
363 e2_blkcnt_t blockcnt, blk64_t ref_blk,
364 int ref_offset, void *priv_data);
366 void do_dirsearch(int argc, char *argv[])
369 struct process_block_struct pb;
371 if (check_fs_open(argv[0]))
375 com_err(0, 0, "Usage: dirsearch dir filename");
379 inode = string_to_inode(argv[1]);
383 pb.buf = malloc(current_fs->blocksize);
385 com_err("dirsearch", 0, "Couldn't allocate buffer");
388 pb.search_name = argv[2];
389 pb.len = strlen(pb.search_name);
391 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
392 search_dir_block, &pb);
398 static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
399 e2_blkcnt_t blockcnt,
400 blk64_t ref_blk EXT2FS_ATTR((unused)),
401 int ref_offset EXT2FS_ATTR((unused)),
404 struct process_block_struct *p;
405 struct ext2_dir_entry *dirent;
407 unsigned int offset = 0;
408 unsigned int rec_len;
413 p = (struct process_block_struct *) priv_data;
415 errcode = io_channel_read_blk64(current_fs->io, *blocknr, 1, p->buf);
417 com_err("search_dir_block", errcode,
418 "while reading block %lu", (unsigned long) *blocknr);
422 while (offset < fs->blocksize) {
423 dirent = (struct ext2_dir_entry *) (p->buf + offset);
424 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
426 com_err("htree_dump_leaf_inode", errcode,
427 "while getting rec_len for block %lu",
428 (unsigned long) *blocknr);
432 p->len == ext2fs_dirent_name_len(dirent) &&
433 strncmp(p->search_name, dirent->name,
435 printf("Entry found at logical block %lld, "
436 "phys %llu, offset %u\n", (long long)blockcnt,
438 printf("offset %u\n", offset);