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
;
47 int hash_flags
= inode
->i_flags
& EXT4_CASEFOLD_FL
;
50 if (ext2fs_has_feature_metadata_csum(fs
->super
))
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",
57 (unsigned long long) blk
);
61 fprintf(pager
, "Reading directory block %llu, phys %llu\n",
62 (unsigned long long) blk
, (unsigned long long) 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",
67 (unsigned long long) blk
, (unsigned long long) pblk
);
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",
90 (unsigned long long) blk
);
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
, __u32 crc
,
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",
166 ext2fs_le32_to_cpu(tail
->dt_checksum
));
167 if (tail
->dt_checksum
!= crc
)
168 fprintf(pager
, " --- EXPECTED: 0x%08x", crc
);
172 for (i
=0; i
< count
; i
++) {
173 hash
= i
? ext2fs_le32_to_cpu(ent
[i
].hash
) : 0;
174 fprintf(pager
, "Entry #%d: Hash 0x%08x%s, block %u\n", i
,
175 hash
, (hash
& 1) ? " (**)" : "",
176 ext2fs_le32_to_cpu(ent
[i
].block
));
179 fprintf(pager
, "\n");
181 for (i
=0; i
< count
; i
++) {
182 unsigned int hashval
, block
;
184 hashval
= ext2fs_le32_to_cpu(ent
[i
].hash
);
185 block
= ext2fs_le32_to_cpu(ent
[i
].block
);
186 fprintf(pager
, "Entry #%d: Hash 0x%08x, block %u\n", i
,
187 i
? hashval
: 0, block
);
189 htree_dump_int_block(fs
, ino
, inode
, rootnode
,
190 block
, buf
, level
-1);
192 htree_dump_leaf_node(fs
, ino
, inode
, rootnode
,
196 fprintf(pager
, "---------------------\n");
199 static void htree_dump_int_block(ext2_filsys fs
, ext2_ino_t ino
,
200 struct ext2_inode
*inode
,
201 struct ext2_dx_root_info
* rootnode
,
202 blk64_t blk
, char *buf
, int level
)
209 cbuf
= malloc(fs
->blocksize
);
211 fprintf(pager
, "Couldn't allocate child block.\n");
215 errcode
= ext2fs_bmap2(fs
, ino
, inode
, buf
, 0, blk
, 0, &pblk
);
217 com_err("htree_dump_int_block", errcode
,
218 "while mapping logical block %llu\n",
219 (unsigned long long) blk
);
223 errcode
= io_channel_read_blk64(current_fs
->io
, pblk
, 1, buf
);
225 com_err("htree_dump_int_block", errcode
,
226 "while reading block %llu\n",
227 (unsigned long long) blk
);
231 errcode
= ext2fs_dx_csum(current_fs
, ino
,
232 (struct ext2_dir_entry
*) buf
, &crc
, NULL
);
234 com_err("htree_dump_int_block", errcode
,
235 "while calculating checksum for logical block %llu\n",
236 (unsigned long long) blk
);
237 crc
= (unsigned int) -1;
239 htree_dump_int_node(fs
, ino
, inode
, rootnode
,
240 (struct ext2_dx_entry
*) (buf
+8),
248 void do_htree_dump(int argc
, char *argv
[], int sci_idx
EXT2FS_ATTR((unused
)),
249 void *infop
EXT2FS_ATTR((unused
)))
252 struct ext2_inode inode
;
255 struct ext2_dx_root_info
*rootnode
;
256 struct ext2_dx_entry
*ent
;
260 if (check_fs_open(argv
[0]))
263 pager
= open_pager();
265 if (common_inode_args_process(argc
, argv
, &ino
, 0))
268 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
271 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
272 com_err(argv
[0], 0, "Not a directory");
276 if ((inode
.i_flags
& EXT2_BTREE_FL
) == 0) {
277 com_err(argv
[0], 0, "Not a hash-indexed directory");
281 buf
= malloc(2*current_fs
->blocksize
);
283 com_err(argv
[0], 0, "Couldn't allocate htree buffer");
287 errcode
= ext2fs_bmap2(current_fs
, ino
, &inode
, buf
, 0, 0, 0, &blk
);
289 com_err("do_htree_block", errcode
,
290 "while mapping logical block 0\n");
294 errcode
= io_channel_read_blk64(current_fs
->io
, blk
,
297 com_err(argv
[0], errcode
, "Error reading root node");
301 rootnode
= (struct ext2_dx_root_info
*) (buf
+ 24);
303 fprintf(pager
, "Root node dump:\n");
304 fprintf(pager
, "\t Reserved zero: %u\n", rootnode
->reserved_zero
);
305 fprintf(pager
, "\t Hash Version: %d\n", rootnode
->hash_version
);
306 fprintf(pager
, "\t Info length: %d\n", rootnode
->info_length
);
307 fprintf(pager
, "\t Indirect levels: %d\n", rootnode
->indirect_levels
);
308 fprintf(pager
, "\t Flags: %#x\n", rootnode
->unused_flags
);
310 ent
= (struct ext2_dx_entry
*)
311 ((char *)rootnode
+ rootnode
->info_length
);
313 errcode
= ext2fs_dx_csum(current_fs
, ino
,
314 (struct ext2_dir_entry
*) buf
, &crc
, NULL
);
316 com_err("htree_dump_int_block", errcode
,
317 "while calculating checksum for htree root\n");
318 crc
= (unsigned int) -1;
320 htree_dump_int_node(current_fs
, ino
, &inode
, rootnode
, ent
, crc
,
321 buf
+ current_fs
->blocksize
,
322 rootnode
->indirect_levels
);
330 * This function prints the hash of a given file.
332 void do_dx_hash(int argc
, char *argv
[], int sci_idx
EXT2FS_ATTR((unused
)),
333 void *infop
EXT2FS_ATTR((unused
)))
335 ext2_dirhash_t hash
, minor_hash
;
338 int hash_version
= 0;
341 const struct ext2fs_nls_table
*encoding
= NULL
;
343 hash_seed
[0] = hash_seed
[1] = hash_seed
[2] = hash_seed
[3] = 0;
346 while ((c
= getopt(argc
, argv
, "h:s:ce:")) != EOF
) {
349 hash_version
= e2p_string2hash(optarg
);
350 if (hash_version
< 0)
351 hash_version
= atoi(optarg
);
354 if (uuid_parse(optarg
, (unsigned char *) hash_seed
)) {
355 fprintf(stderr
, "Invalid UUID format: %s\n",
361 hash_flags
|= EXT4_CASEFOLD_FL
;
364 encoding
= ext2fs_load_nls_table(e2p_str2encoding(optarg
));
366 fprintf(stderr
, "Invalid encoding: %s\n",
375 if (optind
!= argc
-1) {
377 com_err(argv
[0], 0, "usage: dx_hash [-h hash_alg] "
378 "[-s hash_seed] [-c] [-e encoding] filename");
381 err
= ext2fs_dirhash2(hash_version
, argv
[optind
],
382 strlen(argv
[optind
]), encoding
, hash_flags
,
383 hash_seed
, &hash
, &minor_hash
);
386 com_err(argv
[0], err
, "while calculating hash");
389 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv
[optind
],
394 * Search for particular directory entry (useful for debugging very
395 * large hash tree directories that have lost some blocks from the
398 struct process_block_struct
{
404 static int search_dir_block(ext2_filsys fs
, blk64_t
*blocknr
,
405 e2_blkcnt_t blockcnt
, blk64_t ref_blk
,
406 int ref_offset
, void *priv_data
);
408 void do_dirsearch(int argc
, char *argv
[], int sci_idx
EXT2FS_ATTR((unused
)),
409 void *infop
EXT2FS_ATTR((unused
)))
412 struct process_block_struct pb
;
414 if (check_fs_open(argv
[0]))
418 com_err(0, 0, "Usage: dirsearch dir filename");
422 inode
= string_to_inode(argv
[1]);
426 pb
.buf
= malloc(current_fs
->blocksize
);
428 com_err("dirsearch", 0, "Couldn't allocate buffer");
431 pb
.search_name
= argv
[2];
432 pb
.len
= strlen(pb
.search_name
);
434 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, 0,
435 search_dir_block
, &pb
);
441 static int search_dir_block(ext2_filsys fs
, blk64_t
*blocknr
,
442 e2_blkcnt_t blockcnt
,
443 blk64_t ref_blk
EXT2FS_ATTR((unused
)),
444 int ref_offset
EXT2FS_ATTR((unused
)),
447 struct process_block_struct
*p
;
448 struct ext2_dir_entry
*dirent
;
450 unsigned int offset
= 0;
451 unsigned int rec_len
;
456 p
= (struct process_block_struct
*) priv_data
;
458 errcode
= io_channel_read_blk64(current_fs
->io
, *blocknr
, 1, p
->buf
);
460 com_err("search_dir_block", errcode
,
461 "while reading block %lu", (unsigned long) *blocknr
);
465 while (offset
< fs
->blocksize
) {
466 dirent
= (struct ext2_dir_entry
*) (p
->buf
+ offset
);
467 errcode
= ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
469 com_err("htree_dump_leaf_inode", errcode
,
470 "while getting rec_len for block %lu",
471 (unsigned long) *blocknr
);
475 p
->len
== ext2fs_dirent_name_len(dirent
) &&
476 strncmp(p
->search_name
, dirent
->name
,
478 printf("Entry found at logical block %lld, "
479 "phys %llu, offset %u\n", (long long)blockcnt
,
480 (unsigned long long) *blocknr
, offset
);
481 printf("offset %u\n", offset
);