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"
30 #include "ext2fs/nls.h"
34 static void htree_dump_leaf_node(ext2_filsys fs
, ext2_ino_t ino
,
35 struct ext2_inode
*inode
,
36 struct ext2_dx_root_info
* rootnode
,
37 blk64_t blk
, char *buf
)
40 struct ext2_dir_entry
*dirent
;
42 unsigned int offset
= 0;
43 char name
[EXT2_NAME_LEN
+ 1];
44 char tmp
[EXT2_NAME_LEN
+ 64];
46 ext2_dirhash_t hash
, minor_hash
;
49 int hash_flags
= inode
->i_flags
& EXT4_CASEFOLD_FL
;
52 if (ext2fs_has_feature_metadata_csum(fs
->super
))
53 csum_size
= sizeof(struct ext2_dir_entry_tail
);
55 errcode
= ext2fs_bmap2(fs
, ino
, inode
, buf
, 0, blk
, 0, &pblk
);
57 com_err("htree_dump_leaf_node", errcode
,
58 "while mapping logical block %llu\n", blk
);
62 fprintf(pager
, "Reading directory block %llu, phys %llu\n", blk
, pblk
);
63 errcode
= ext2fs_read_dir_block4(current_fs
, pblk
, buf
, 0, ino
);
65 com_err("htree_dump_leaf_node", errcode
,
66 "while reading block %llu (%llu)\n",
70 hash_alg
= rootnode
->hash_version
;
71 if ((hash_alg
<= EXT2_HASH_TEA
) &&
72 (fs
->super
->s_flags
& EXT2_FLAGS_UNSIGNED_HASH
))
75 while (offset
< fs
->blocksize
) {
76 dirent
= (struct ext2_dir_entry
*) (buf
+ offset
);
77 errcode
= ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
79 com_err("htree_dump_leaf_inode", errcode
,
80 "while getting rec_len for block %lu",
84 thislen
= ext2fs_dirent_name_len(dirent
);
85 if (((offset
+ rec_len
) > fs
->blocksize
) ||
87 ((rec_len
% 4) != 0) ||
88 ((unsigned) thislen
+ 8 > rec_len
)) {
89 fprintf(pager
, "Corrupted directory block (%llu)!\n",
93 strncpy(name
, dirent
->name
, thislen
);
95 errcode
= ext2fs_dirhash2(hash_alg
, name
, thislen
,
96 fs
->encoding
, hash_flags
,
97 fs
->super
->s_hash_seed
,
100 com_err("htree_dump_leaf_node", errcode
,
101 "while calculating hash");
102 if ((offset
== fs
->blocksize
- csum_size
) &&
103 (dirent
->inode
== 0) &&
104 (dirent
->rec_len
== csum_size
) &&
105 (dirent
->name_len
== EXT2_DIR_NAME_LEN_CSUM
)) {
106 struct ext2_dir_entry_tail
*t
;
108 t
= (struct ext2_dir_entry_tail
*) dirent
;
110 snprintf(tmp
, EXT2_NAME_LEN
+ 64,
111 "leaf block checksum: 0x%08x ",
114 snprintf(tmp
, EXT2_NAME_LEN
+ 64,
115 "%u 0x%08x-%08x (%d) %s ",
116 dirent
->inode
, hash
, minor_hash
,
119 thislen
= strlen(tmp
);
120 if (col
+ thislen
> 80) {
121 fprintf(pager
, "\n");
124 fprintf(pager
, "%s", tmp
);
128 fprintf(pager
, "\n");
132 static void htree_dump_int_block(ext2_filsys fs
, ext2_ino_t ino
,
133 struct ext2_inode
*inode
,
134 struct ext2_dx_root_info
* rootnode
,
135 blk64_t blk
, char *buf
, int level
);
138 static void htree_dump_int_node(ext2_filsys fs
, ext2_ino_t ino
,
139 struct ext2_inode
*inode
,
140 struct ext2_dx_root_info
* rootnode
,
141 struct ext2_dx_entry
*ent
,
142 char *buf
, int level
)
144 struct ext2_dx_countlimit dx_countlimit
;
145 struct ext2_dx_tail
*tail
;
150 dx_countlimit
= *((struct ext2_dx_countlimit
*) ent
);
151 count
= ext2fs_le16_to_cpu(dx_countlimit
.count
);
152 limit
= ext2fs_le16_to_cpu(dx_countlimit
.limit
);
154 fprintf(pager
, "Number of entries (count): %d\n", count
);
155 fprintf(pager
, "Number of entries (limit): %d\n", limit
);
157 remainder
= fs
->blocksize
- (limit
* sizeof(struct ext2_dx_entry
));
158 if (ent
== (struct ext2_dx_entry
*)(rootnode
+ 1))
159 remainder
-= sizeof(struct ext2_dx_root_info
) + 24;
162 if (ext2fs_has_feature_metadata_csum(fs
->super
) &&
163 remainder
== sizeof(struct ext2_dx_tail
)) {
164 tail
= (struct ext2_dx_tail
*)(ent
+ limit
);
165 fprintf(pager
, "Checksum: 0x%08x\n",
166 ext2fs_le32_to_cpu(tail
->dt_checksum
));
169 for (i
=0; i
< count
; i
++) {
170 hash
= i
? ext2fs_le32_to_cpu(ent
[i
].hash
) : 0;
171 fprintf(pager
, "Entry #%d: Hash 0x%08x%s, block %u\n", i
,
172 hash
, (hash
& 1) ? " (**)" : "",
173 ext2fs_le32_to_cpu(ent
[i
].block
));
176 fprintf(pager
, "\n");
178 for (i
=0; i
< count
; i
++) {
179 unsigned int hashval
, block
;
181 hashval
= ext2fs_le32_to_cpu(ent
[i
].hash
);
182 block
= ext2fs_le32_to_cpu(ent
[i
].block
);
183 fprintf(pager
, "Entry #%d: Hash 0x%08x, block %u\n", i
,
184 i
? hashval
: 0, block
);
186 htree_dump_int_block(fs
, ino
, inode
, rootnode
,
187 block
, buf
, level
-1);
189 htree_dump_leaf_node(fs
, ino
, inode
, rootnode
,
193 fprintf(pager
, "---------------------\n");
196 static void htree_dump_int_block(ext2_filsys fs
, ext2_ino_t ino
,
197 struct ext2_inode
*inode
,
198 struct ext2_dx_root_info
* rootnode
,
199 blk64_t blk
, char *buf
, int level
)
205 cbuf
= malloc(fs
->blocksize
);
207 fprintf(pager
, "Couldn't allocate child block.\n");
211 errcode
= ext2fs_bmap2(fs
, ino
, inode
, buf
, 0, blk
, 0, &pblk
);
213 com_err("htree_dump_int_block", errcode
,
214 "while mapping logical block %llu\n", blk
);
218 errcode
= io_channel_read_blk64(current_fs
->io
, pblk
, 1, buf
);
220 com_err("htree_dump_int_block", errcode
,
221 "while reading block %llu\n", blk
);
225 htree_dump_int_node(fs
, ino
, inode
, rootnode
,
226 (struct ext2_dx_entry
*) (buf
+8),
234 void do_htree_dump(int argc
, char *argv
[], int sci_idx
EXT2FS_ATTR((unused
)),
235 void *infop
EXT2FS_ATTR((unused
)))
238 struct ext2_inode inode
;
241 struct ext2_dx_root_info
*rootnode
;
242 struct ext2_dx_entry
*ent
;
245 if (check_fs_open(argv
[0]))
248 pager
= open_pager();
250 if (common_inode_args_process(argc
, argv
, &ino
, 0))
253 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
256 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
257 com_err(argv
[0], 0, "Not a directory");
261 if ((inode
.i_flags
& EXT2_BTREE_FL
) == 0) {
262 com_err(argv
[0], 0, "Not a hash-indexed directory");
266 buf
= malloc(2*current_fs
->blocksize
);
268 com_err(argv
[0], 0, "Couldn't allocate htree buffer");
272 errcode
= ext2fs_bmap2(current_fs
, ino
, &inode
, buf
, 0, 0, 0, &blk
);
274 com_err("do_htree_block", errcode
,
275 "while mapping logical block 0\n");
279 errcode
= io_channel_read_blk64(current_fs
->io
, blk
,
282 com_err(argv
[0], errcode
, "Error reading root node");
286 rootnode
= (struct ext2_dx_root_info
*) (buf
+ 24);
288 fprintf(pager
, "Root node dump:\n");
289 fprintf(pager
, "\t Reserved zero: %u\n", rootnode
->reserved_zero
);
290 fprintf(pager
, "\t Hash Version: %d\n", rootnode
->hash_version
);
291 fprintf(pager
, "\t Info length: %d\n", rootnode
->info_length
);
292 fprintf(pager
, "\t Indirect levels: %d\n", rootnode
->indirect_levels
);
293 fprintf(pager
, "\t Flags: %d\n", rootnode
->unused_flags
);
295 ent
= (struct ext2_dx_entry
*)
296 ((char *)rootnode
+ rootnode
->info_length
);
298 htree_dump_int_node(current_fs
, ino
, &inode
, rootnode
, ent
,
299 buf
+ current_fs
->blocksize
,
300 rootnode
->indirect_levels
);
308 * This function prints the hash of a given file.
310 void do_dx_hash(int argc
, char *argv
[], int sci_idx
EXT2FS_ATTR((unused
)),
311 void *infop
EXT2FS_ATTR((unused
)))
313 ext2_dirhash_t hash
, minor_hash
, hash_flags
;
316 int hash_version
= 0;
318 const struct nls_table
*encoding
;
320 hash_seed
[0] = hash_seed
[1] = hash_seed
[2] = hash_seed
[3] = 0;
323 while ((c
= getopt (argc
, argv
, "h:s:")) != EOF
) {
326 hash_version
= e2p_string2hash(optarg
);
327 if (hash_version
< 0)
328 hash_version
= atoi(optarg
);
331 if (uuid_parse(optarg
, (unsigned char *) hash_seed
)) {
332 fprintf(stderr
, "Invalid UUID format: %s\n",
338 hash_flags
= EXT4_CASEFOLD_FL
;
341 encoding
= nls_load_table(e2p_str2encoding(optarg
));
343 fprintf(stderr
, "Invalid encoding: %s\n",
350 if (optind
!= argc
-1) {
352 com_err(argv
[0], 0, "usage: dx_hash [-h hash_alg] "
353 "[-s hash_seed] [-c] [-e encoding] filename");
356 err
= ext2fs_dirhash2(hash_version
, argv
[optind
],
357 strlen(argv
[optind
]), encoding
, hash_flags
,
358 hash_seed
, &hash
, &minor_hash
);
361 com_err(argv
[0], err
, "while calculating hash");
364 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv
[optind
],
369 * Search for particular directory entry (useful for debugging very
370 * large hash tree directories that have lost some blocks from the
373 struct process_block_struct
{
379 static int search_dir_block(ext2_filsys fs
, blk64_t
*blocknr
,
380 e2_blkcnt_t blockcnt
, blk64_t ref_blk
,
381 int ref_offset
, void *priv_data
);
383 void do_dirsearch(int argc
, char *argv
[], int sci_idx
EXT2FS_ATTR((unused
)),
384 void *infop
EXT2FS_ATTR((unused
)))
387 struct process_block_struct pb
;
389 if (check_fs_open(argv
[0]))
393 com_err(0, 0, "Usage: dirsearch dir filename");
397 inode
= string_to_inode(argv
[1]);
401 pb
.buf
= malloc(current_fs
->blocksize
);
403 com_err("dirsearch", 0, "Couldn't allocate buffer");
406 pb
.search_name
= argv
[2];
407 pb
.len
= strlen(pb
.search_name
);
409 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, 0,
410 search_dir_block
, &pb
);
416 static int search_dir_block(ext2_filsys fs
, blk64_t
*blocknr
,
417 e2_blkcnt_t blockcnt
,
418 blk64_t ref_blk
EXT2FS_ATTR((unused
)),
419 int ref_offset
EXT2FS_ATTR((unused
)),
422 struct process_block_struct
*p
;
423 struct ext2_dir_entry
*dirent
;
425 unsigned int offset
= 0;
426 unsigned int rec_len
;
431 p
= (struct process_block_struct
*) priv_data
;
433 errcode
= io_channel_read_blk64(current_fs
->io
, *blocknr
, 1, p
->buf
);
435 com_err("search_dir_block", errcode
,
436 "while reading block %lu", (unsigned long) *blocknr
);
440 while (offset
< fs
->blocksize
) {
441 dirent
= (struct ext2_dir_entry
*) (p
->buf
+ offset
);
442 errcode
= ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
444 com_err("htree_dump_leaf_inode", errcode
,
445 "while getting rec_len for block %lu",
446 (unsigned long) *blocknr
);
450 p
->len
== ext2fs_dirent_name_len(dirent
) &&
451 strncmp(p
->search_name
, dirent
->name
,
453 printf("Entry found at logical block %lld, "
454 "phys %llu, offset %u\n", (long long)blockcnt
,
456 printf("offset %u\n", offset
);