1 /*
2 * htree.c --- hash tree routines
3 *
4 * Copyright (C) 2002 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8 #include "config.h"
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <time.h>
15 #ifdef HAVE_ERRNO_H
16 #include <errno.h>
17 #endif
18 #include <sys/types.h>
19 #ifdef HAVE_GETOPT_H
20 #include <getopt.h>
21 #else
22 extern int optind;
23 extern char *optarg;
24 #endif
25
26 #include "debugfs.h"
27 #include "uuid/uuid.h"
28 #include "e2p/e2p.h"
29
30 static FILE *pager;
31
htree_dump_leaf_node(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,struct ext2_dx_root_info * rootnode,blk64_t blk,char * buf)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)
36 {
37 errcode_t errcode;
38 struct ext2_dir_entry *dirent;
39 int thislen, col = 0;
40 unsigned int offset = 0;
41 char name[EXT2_NAME_LEN + 1];
42 char tmp[EXT2_NAME_LEN + 64];
43 blk64_t pblk;
44 ext2_dirhash_t hash, minor_hash;
45 unsigned int rec_len;
46 int hash_alg;
47 int hash_flags = inode->i_flags & EXT4_CASEFOLD_FL;
48 int csum_size = 0;
49
50 if (ext2fs_has_feature_metadata_csum(fs->super))
51 csum_size = sizeof(struct ext2_dir_entry_tail);
52
53 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
54 if (errcode) {
55 com_err("htree_dump_leaf_node", errcode,
56 "while mapping logical block %llu\n",
57 (unsigned long long) blk);
58 return;
59 }
60
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);
64 if (errcode) {
65 com_err("htree_dump_leaf_node", errcode,
66 "while reading block %llu (%llu)\n",
67 (unsigned long long) blk, (unsigned long long) pblk);
68 return;
69 }
70 hash_alg = rootnode->hash_version;
71 if ((hash_alg <= EXT2_HASH_TEA) &&
72 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
73 hash_alg += 3;
74
75 while (offset < fs->blocksize) {
76 dirent = (struct ext2_dir_entry *) (buf + offset);
77 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
78 if (errcode) {
79 com_err("htree_dump_leaf_inode", errcode,
80 "while getting rec_len for block %lu",
81 (unsigned long) blk);
82 return;
83 }
84 thislen = ext2fs_dirent_name_len(dirent);
85 if (((offset + rec_len) > fs->blocksize) ||
86 (rec_len < 8) ||
87 ((rec_len % 4) != 0) ||
88 ((unsigned) thislen + 8 > rec_len)) {
89 fprintf(pager, "Corrupted directory block (%llu)!\n",
90 (unsigned long long) blk);
91 break;
92 }
93 strncpy(name, dirent->name, thislen);
94 name[thislen] = '\0';
95 errcode = ext2fs_dirhash2(hash_alg, name, thislen,
96 fs->encoding, hash_flags,
97 fs->super->s_hash_seed,
98 &hash, &minor_hash);
99 if (errcode)
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;
107
108 t = (struct ext2_dir_entry_tail *) dirent;
109
110 snprintf(tmp, EXT2_NAME_LEN + 64,
111 "leaf block checksum: 0x%08x ",
112 t->det_checksum);
113 } else {
114 snprintf(tmp, EXT2_NAME_LEN + 64,
115 "%u 0x%08x-%08x (%d) %s ",
116 dirent->inode, hash, minor_hash,
117 rec_len, name);
118 }
119 thislen = strlen(tmp);
120 if (col + thislen > 80) {
121 fprintf(pager, "\n");
122 col = 0;
123 }
124 fprintf(pager, "%s", tmp);
125 col += thislen;
126 offset += rec_len;
127 }
128 fprintf(pager, "\n");
129 }
130
131
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);
136
137
htree_dump_int_node(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,struct ext2_dx_root_info * rootnode,struct ext2_dx_entry * ent,__u32 crc,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)
143 {
144 struct ext2_dx_countlimit dx_countlimit;
145 struct ext2_dx_tail *tail;
146 int hash, i;
147 int limit, count;
148 int remainder;
149
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);
153
154 fprintf(pager, "Number of entries (count): %d\n", count);
155 fprintf(pager, "Number of entries (limit): %d\n", limit);
156
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;
160 else
161 remainder -= 8;
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);
169 fputc('\n', pager);
170 }
171
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));
177 }
178
179 fprintf(pager, "\n");
180
181 for (i=0; i < count; i++) {
182 unsigned int hashval, block;
183
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);
188 if (level)
189 htree_dump_int_block(fs, ino, inode, rootnode,
190 block, buf, level-1);
191 else
192 htree_dump_leaf_node(fs, ino, inode, rootnode,
193 block, buf);
194 }
195
196 fprintf(pager, "---------------------\n");
197 }
198
htree_dump_int_block(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,struct ext2_dx_root_info * rootnode,blk64_t blk,char * buf,int level)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)
203 {
204 char *cbuf;
205 errcode_t errcode;
206 blk64_t pblk;
207 __u32 crc;
208
209 cbuf = malloc(fs->blocksize);
210 if (!cbuf) {
211 fprintf(pager, "Couldn't allocate child block.\n");
212 return;
213 }
214
215 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
216 if (errcode) {
217 com_err("htree_dump_int_block", errcode,
218 "while mapping logical block %llu\n",
219 (unsigned long long) blk);
220 goto errout;
221 }
222
223 errcode = io_channel_read_blk64(current_fs->io, pblk, 1, buf);
224 if (errcode) {
225 com_err("htree_dump_int_block", errcode,
226 "while reading block %llu\n",
227 (unsigned long long) blk);
228 goto errout;
229 }
230
231 errcode = ext2fs_dx_csum(current_fs, ino,
232 (struct ext2_dir_entry *) buf, &crc, NULL);
233 if (errcode) {
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;
238 }
239 htree_dump_int_node(fs, ino, inode, rootnode,
240 (struct ext2_dx_entry *) (buf+8),
241 crc, cbuf, level);
242 errout:
243 free(cbuf);
244 }
245
246
247
do_htree_dump(int argc,char * argv[],int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))248 void do_htree_dump(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
249 void *infop EXT2FS_ATTR((unused)))
250 {
251 ext2_ino_t ino;
252 struct ext2_inode inode;
253 blk64_t blk;
254 char *buf = NULL;
255 struct ext2_dx_root_info *rootnode;
256 struct ext2_dx_entry *ent;
257 errcode_t errcode;
258 __u32 crc;
259
260 if (check_fs_open(argv[0]))
261 return;
262
263 pager = open_pager();
264
265 if (common_inode_args_process(argc, argv, &ino, 0))
266 goto errout;
267
268 if (debugfs_read_inode(ino, &inode, argv[1]))
269 goto errout;
270
271 if (!LINUX_S_ISDIR(inode.i_mode)) {
272 com_err(argv[0], 0, "Not a directory");
273 goto errout;
274 }
275
276 if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
277 com_err(argv[0], 0, "Not a hash-indexed directory");
278 goto errout;
279 }
280
281 buf = malloc(2*current_fs->blocksize);
282 if (!buf) {
283 com_err(argv[0], 0, "Couldn't allocate htree buffer");
284 goto errout;
285 }
286
287 errcode = ext2fs_bmap2(current_fs, ino, &inode, buf, 0, 0, 0, &blk);
288 if (errcode) {
289 com_err("do_htree_block", errcode,
290 "while mapping logical block 0\n");
291 goto errout;
292 }
293
294 errcode = io_channel_read_blk64(current_fs->io, blk,
295 1, buf);
296 if (errcode) {
297 com_err(argv[0], errcode, "Error reading root node");
298 goto errout;
299 }
300
301 rootnode = (struct ext2_dx_root_info *) (buf + 24);
302
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);
309
310 ent = (struct ext2_dx_entry *)
311 ((char *)rootnode + rootnode->info_length);
312
313 errcode = ext2fs_dx_csum(current_fs, ino,
314 (struct ext2_dir_entry *) buf, &crc, NULL);
315 if (errcode) {
316 com_err("htree_dump_int_block", errcode,
317 "while calculating checksum for htree root\n");
318 crc = (unsigned int) -1;
319 }
320 htree_dump_int_node(current_fs, ino, &inode, rootnode, ent, crc,
321 buf + current_fs->blocksize,
322 rootnode->indirect_levels);
323
324 errout:
325 free(buf);
326 close_pager(pager);
327 }
328
329 /*
330 * This function prints the hash of a given file.
331 */
do_dx_hash(int argc,char * argv[],int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))332 void do_dx_hash(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
333 void *infop EXT2FS_ATTR((unused)))
334 {
335 ext2_dirhash_t hash, minor_hash;
336 errcode_t err;
337 int c;
338 int hash_version = 0;
339 __u32 hash_seed[4];
340 int hash_flags = 0;
341 const struct ext2fs_nls_table *encoding = NULL;
342
343 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
344
345 reset_getopt();
346 while ((c = getopt(argc, argv, "h:s:ce:")) != EOF) {
347 switch (c) {
348 case 'h':
349 hash_version = e2p_string2hash(optarg);
350 if (hash_version < 0)
351 hash_version = atoi(optarg);
352 break;
353 case 's':
354 if (uuid_parse(optarg, (unsigned char *) hash_seed)) {
355 fprintf(stderr, "Invalid UUID format: %s\n",
356 optarg);
357 return;
358 }
359 break;
360 case 'c':
361 hash_flags |= EXT4_CASEFOLD_FL;
362 break;
363 case 'e':
364 encoding = ext2fs_load_nls_table(e2p_str2encoding(optarg));
365 if (!encoding) {
366 fprintf(stderr, "Invalid encoding: %s\n",
367 optarg);
368 return;
369 }
370 break;
371 default:
372 goto print_usage;
373 }
374 }
375 if (optind != argc-1) {
376 print_usage:
377 com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
378 "[-s hash_seed] [-c] [-e encoding] filename");
379 return;
380 }
381 err = ext2fs_dirhash2(hash_version, argv[optind],
382 strlen(argv[optind]), encoding, hash_flags,
383 hash_seed, &hash, &minor_hash);
384
385 if (err) {
386 com_err(argv[0], err, "while calculating hash");
387 return;
388 }
389 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
390 hash, minor_hash);
391 }
392
393 /*
394 * Search for particular directory entry (useful for debugging very
395 * large hash tree directories that have lost some blocks from the
396 * btree index).
397 */
398 struct process_block_struct {
399 char *search_name;
400 char *buf;
401 int len;
402 };
403
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);
407
do_dirsearch(int argc,char * argv[],int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))408 void do_dirsearch(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
409 void *infop EXT2FS_ATTR((unused)))
410 {
411 ext2_ino_t inode;
412 struct process_block_struct pb;
413
414 if (check_fs_open(argv[0]))
415 return;
416
417 if (argc != 3) {
418 com_err(0, 0, "Usage: dirsearch dir filename");
419 return;
420 }
421
422 inode = string_to_inode(argv[1]);
423 if (!inode)
424 return;
425
426 pb.buf = malloc(current_fs->blocksize);
427 if (!pb.buf) {
428 com_err("dirsearch", 0, "Couldn't allocate buffer");
429 return;
430 }
431 pb.search_name = argv[2];
432 pb.len = strlen(pb.search_name);
433
434 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
435 search_dir_block, &pb);
436
437 free(pb.buf);
438 }
439
440
search_dir_block(ext2_filsys fs,blk64_t * blocknr,e2_blkcnt_t blockcnt,blk64_t ref_blk EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)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)),
445 void *priv_data)
446 {
447 struct process_block_struct *p;
448 struct ext2_dir_entry *dirent;
449 errcode_t errcode;
450 unsigned int offset = 0;
451 unsigned int rec_len;
452
453 if (blockcnt < 0)
454 return 0;
455
456 p = (struct process_block_struct *) priv_data;
457
458 errcode = io_channel_read_blk64(current_fs->io, *blocknr, 1, p->buf);
459 if (errcode) {
460 com_err("search_dir_block", errcode,
461 "while reading block %lu", (unsigned long) *blocknr);
462 return BLOCK_ABORT;
463 }
464
465 while (offset < fs->blocksize) {
466 dirent = (struct ext2_dir_entry *) (p->buf + offset);
467 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
468 if (errcode) {
469 com_err("htree_dump_leaf_inode", errcode,
470 "while getting rec_len for block %lu",
471 (unsigned long) *blocknr);
472 return BLOCK_ABORT;
473 }
474 if (dirent->inode &&
475 p->len == ext2fs_dirent_name_len(dirent) &&
476 strncmp(p->search_name, dirent->name,
477 p->len) == 0) {
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);
482 return BLOCK_ABORT;
483 }
484 offset += rec_len;
485 }
486 return 0;
487 }
488
489