blob: bd1fd5128749b9a2645473ab2fc4bfe23034c49f [file] [log] [blame]
Theodore Ts'odf614db2002-02-25 04:28:45 -05001/*
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 <stdio.h>
9#include <unistd.h>
10#include <stdlib.h>
11#include <ctype.h>
12#include <string.h>
13#include <time.h>
14#ifdef HAVE_ERRNO_H
15#include <errno.h>
16#endif
17#include <sys/types.h>
18#ifdef HAVE_GETOPT_H
19#include <getopt.h>
20#else
21extern int optind;
22extern char *optarg;
23#endif
Theodore Ts'odf614db2002-02-25 04:28:45 -050024
25#include "debugfs.h"
26
27static FILE *pager;
28
Theodore Ts'odf614db2002-02-25 04:28:45 -050029static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
30 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -040031 struct ext2_dx_root_info * rootnode,
Theodore Ts'odf614db2002-02-25 04:28:45 -050032 blk_t blk, char *buf)
33{
34 errcode_t errcode;
35 struct ext2_dir_entry *dirent;
Theodore Ts'o54434922003-12-07 01:28:50 -050036 int thislen, col = 0;
37 unsigned int offset = 0;
Brian Behlendorfb7729002007-03-21 15:09:15 -040038 char name[EXT2_NAME_LEN + 1];
Theodore Ts'odf614db2002-02-25 04:28:45 -050039 char tmp[EXT2_NAME_LEN + 16];
40 blk_t pblk;
Theodore Ts'o52783e02002-03-11 15:04:45 -050041 ext2_dirhash_t hash;
Theodore Ts'of77704e2006-11-11 22:32:35 -050042 int hash_alg;
Theodore Ts'odf614db2002-02-25 04:28:45 -050043
44 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
45 if (errcode) {
46 com_err("htree_dump_leaf_node", errcode,
Takashi Sato8deb80a2006-03-18 21:43:46 -050047 "while mapping logical block %u\n", blk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050048 return;
49 }
50
Theodore Ts'o8132d842002-10-02 22:07:17 -040051 errcode = ext2fs_read_dir_block2(current_fs, pblk, buf, 0);
Theodore Ts'odf614db2002-02-25 04:28:45 -050052 if (errcode) {
53 com_err("htree_dump_leaf_node", errcode,
Takashi Sato8deb80a2006-03-18 21:43:46 -050054 "while reading block %u\n", blk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050055 return;
56 }
Theodore Ts'of77704e2006-11-11 22:32:35 -050057 hash_alg = rootnode->hash_version;
58 if ((hash_alg <= EXT2_HASH_TEA) &&
59 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
60 hash_alg += 3;
Theodore Ts'odf614db2002-02-25 04:28:45 -050061
62 while (offset < fs->blocksize) {
63 dirent = (struct ext2_dir_entry *) (buf + offset);
64 if (((offset + dirent->rec_len) > fs->blocksize) ||
65 (dirent->rec_len < 8) ||
66 ((dirent->rec_len % 4) != 0) ||
67 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
Takashi Sato8deb80a2006-03-18 21:43:46 -050068 fprintf(pager, "Corrupted directory block (%u)!\n", blk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050069 break;
70 }
71 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
72 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
73 strncpy(name, dirent->name, thislen);
74 name[thislen] = '\0';
Theodore Ts'of77704e2006-11-11 22:32:35 -050075 errcode = ext2fs_dirhash(hash_alg, name,
Theodore Ts'o3e699062002-10-13 23:56:28 -040076 thislen, fs->super->s_hash_seed,
Theodore Ts'o503f9e72002-06-26 16:52:10 -040077 &hash, 0);
Theodore Ts'o52783e02002-03-11 15:04:45 -050078 if (errcode)
79 com_err("htree_dump_leaf_node", errcode,
80 "while calculating hash");
Theodore Ts'odf614db2002-02-25 04:28:45 -050081 sprintf(tmp, "%u 0x%08x (%d) %s ", dirent->inode,
Theodore Ts'o52783e02002-03-11 15:04:45 -050082 hash, dirent->rec_len, name);
Theodore Ts'odf614db2002-02-25 04:28:45 -050083 thislen = strlen(tmp);
84 if (col + thislen > 80) {
85 fprintf(pager, "\n");
86 col = 0;
87 }
88 fprintf(pager, "%s", tmp);
89 col += thislen;
90 offset += dirent->rec_len;
91 }
92 fprintf(pager, "\n");
93}
94
95
96static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
97 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -040098 struct ext2_dx_root_info * rootnode,
Theodore Ts'odf614db2002-02-25 04:28:45 -050099 blk_t blk, char *buf, int level);
100
101
102static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
103 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400104 struct ext2_dx_root_info * rootnode,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500105 struct ext2_dx_entry *ent,
106 char *buf, int level)
107{
Theodore Ts'o621732c2002-07-18 22:19:51 -0400108 struct ext2_dx_countlimit limit;
109 struct ext2_dx_entry e;
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400110 int hash, i;
Theodore Ts'o621732c2002-07-18 22:19:51 -0400111
Theodore Ts'odf614db2002-02-25 04:28:45 -0500112
Theodore Ts'o621732c2002-07-18 22:19:51 -0400113 limit = *((struct ext2_dx_countlimit *) ent);
114 limit.count = ext2fs_le16_to_cpu(limit.count);
115 limit.limit = ext2fs_le16_to_cpu(limit.limit);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500116
Theodore Ts'o621732c2002-07-18 22:19:51 -0400117 fprintf(pager, "Number of entries (count): %d\n", limit.count);
118 fprintf(pager, "Number of entries (limit): %d\n", limit.limit);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500119
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400120 for (i=0; i < limit.count; i++) {
121 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400122 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400123 hash, (hash & 1) ? " (**)" : "",
Theodore Ts'o621732c2002-07-18 22:19:51 -0400124 ext2fs_le32_to_cpu(ent[i].block));
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400125 }
Theodore Ts'odf614db2002-02-25 04:28:45 -0500126
127 fprintf(pager, "\n");
128
Theodore Ts'o621732c2002-07-18 22:19:51 -0400129 for (i=0; i < limit.count; i++) {
130 e.hash = ext2fs_le32_to_cpu(ent[i].hash);
131 e.block = ext2fs_le32_to_cpu(ent[i].block);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500132 fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
Theodore Ts'o621732c2002-07-18 22:19:51 -0400133 i ? e.hash : 0, e.block);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500134 if (level)
Theodore Ts'o3e699062002-10-13 23:56:28 -0400135 htree_dump_int_block(fs, ino, inode, rootnode,
Theodore Ts'o621732c2002-07-18 22:19:51 -0400136 e.block, buf, level-1);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500137 else
Theodore Ts'o3e699062002-10-13 23:56:28 -0400138 htree_dump_leaf_node(fs, ino, inode, rootnode,
Theodore Ts'o621732c2002-07-18 22:19:51 -0400139 e.block, buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500140 }
141
142 fprintf(pager, "---------------------\n");
143}
144
145static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
146 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400147 struct ext2_dx_root_info * rootnode,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500148 blk_t blk, char *buf, int level)
149{
150 char *cbuf;
151 errcode_t errcode;
152 blk_t pblk;
153
154 cbuf = malloc(fs->blocksize);
155 if (!cbuf) {
156 fprintf(pager, "Couldn't allocate child block.\n");
157 return;
158 }
159
160 errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
161 if (errcode) {
162 com_err("htree_dump_int_block", errcode,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500163 "while mapping logical block %u\n", blk);
Brian Behlendorf89456552007-03-21 17:53:33 -0400164 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500165 }
166
167 errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf);
168 if (errcode) {
169 com_err("htree_dump_int_block", errcode,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500170 "while reading block %u\n", blk);
Brian Behlendorf89456552007-03-21 17:53:33 -0400171 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500172 }
173
Theodore Ts'o3e699062002-10-13 23:56:28 -0400174 htree_dump_int_node(fs, ino, inode, rootnode,
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400175 (struct ext2_dx_entry *) (buf+8),
Theodore Ts'odf614db2002-02-25 04:28:45 -0500176 cbuf, level);
Brian Behlendorf89456552007-03-21 17:53:33 -0400177errout:
Theodore Ts'odf614db2002-02-25 04:28:45 -0500178 free(cbuf);
179}
180
181
182
183void do_htree_dump(int argc, char *argv[])
184{
185 ext2_ino_t ino;
186 struct ext2_inode inode;
Theodore Ts'o3e699062002-10-13 23:56:28 -0400187 int c;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500188 int long_opt;
Theodore Ts'o3e699062002-10-13 23:56:28 -0400189 char *buf = NULL;
190 struct ext2_dx_root_info *rootnode;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500191 struct ext2_dx_entry *ent;
192 struct ext2_dx_countlimit *limit;
193 errcode_t errcode;
194
195 if (check_fs_open(argv[0]))
196 return;
197
198 pager = open_pager();
199
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400200 reset_getopt();
Theodore Ts'odf614db2002-02-25 04:28:45 -0500201 while ((c = getopt (argc, argv, "l")) != EOF) {
202 switch (c) {
203 case 'l':
204 long_opt++;
205 break;
Theodore Ts'o49c6b4e2006-04-27 20:59:42 -0400206 default:
207 goto print_usage;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500208 }
209 }
210
211 if (argc > optind+1) {
Theodore Ts'o49c6b4e2006-04-27 20:59:42 -0400212 print_usage:
Theodore Ts'odf614db2002-02-25 04:28:45 -0500213 com_err(0, 0, "Usage: htree_dump [-l] file");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400214 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500215 }
216
217 if (argc == optind)
218 ino = cwd;
219 else
220 ino = string_to_inode(argv[optind]);
221 if (!ino)
Theodore Ts'o155f5772002-07-21 14:17:45 -0400222 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500223
224 if (debugfs_read_inode(ino, &inode, argv[1]))
Theodore Ts'o155f5772002-07-21 14:17:45 -0400225 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500226
227 if (!LINUX_S_ISDIR(inode.i_mode)) {
228 com_err(argv[0], 0, "Not a directory");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400229 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500230 }
231
232 if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
233 com_err(argv[0], 0, "Not a hash-indexed directory");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400234 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500235 }
236
237 buf = malloc(2*current_fs->blocksize);
238 if (!buf) {
239 com_err(argv[0], 0, "Couldn't allocate htree buffer");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400240 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500241 }
242
243 errcode = io_channel_read_blk(current_fs->io, inode.i_block[0],
244 1, buf);
245 if (errcode) {
246 com_err(argv[0], errcode, "Error reading root node");
247 goto errout;
248 }
249
Theodore Ts'o3e699062002-10-13 23:56:28 -0400250 rootnode = (struct ext2_dx_root_info *) (buf + 24);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500251
252 fprintf(pager, "Root node dump:\n");
Takashi Sato8deb80a2006-03-18 21:43:46 -0500253 fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
Theodore Ts'o3e699062002-10-13 23:56:28 -0400254 fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
255 fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
256 fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
257 fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500258
Theodore Ts'o3e699062002-10-13 23:56:28 -0400259 ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500260 limit = (struct ext2_dx_countlimit *) ent;
261
Theodore Ts'o3e699062002-10-13 23:56:28 -0400262 htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500263 buf + current_fs->blocksize,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400264 rootnode->indirect_levels);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500265
266errout:
267 if (buf)
268 free(buf);
269 close_pager(pager);
270}
271
272/*
273 * This function prints the hash of a given file.
274 */
275void do_dx_hash(int argc, char *argv[])
276{
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400277 ext2_dirhash_t hash, minor_hash;
Theodore Ts'o52783e02002-03-11 15:04:45 -0500278 errcode_t err;
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400279 int c;
280 int hash_version = 0;
281 __u32 hash_seed[4];
Theodore Ts'o52783e02002-03-11 15:04:45 -0500282
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400283 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400284
285 reset_getopt();
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400286 while ((c = getopt (argc, argv, "h:")) != EOF) {
287 switch (c) {
288 case 'h':
289 hash_version = atoi(optarg);
290 break;
Theodore Ts'o49c6b4e2006-04-27 20:59:42 -0400291 default:
292 goto print_usage;
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400293 }
294 }
295 if (optind != argc-1) {
Theodore Ts'o49c6b4e2006-04-27 20:59:42 -0400296 print_usage:
Theodore Ts'odf614db2002-02-25 04:28:45 -0500297 com_err(argv[0], 0, "usage: dx_hash filename");
298 return;
299 }
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400300 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
301 hash_seed, &hash, &minor_hash);
Theodore Ts'o52783e02002-03-11 15:04:45 -0500302 if (err) {
303 com_err(argv[0], err, "while caclulating hash");
304 return;
305 }
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400306 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
307 hash, minor_hash);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500308}
309
310/*
311 * Search for particular directory entry (useful for debugging very
312 * large hash tree directories that have lost some blocks from the
313 * btree index).
314 */
315struct process_block_struct {
316 char *search_name;
317 char *buf;
318 int len;
319};
320
321static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
322 e2_blkcnt_t blockcnt, blk_t ref_blk,
323 int ref_offset, void *priv_data);
324
325void do_dirsearch(int argc, char *argv[])
326{
327 ext2_ino_t inode;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500328 struct process_block_struct pb;
329
330 if (check_fs_open(argv[0]))
331 return;
332
333 if (argc != 3) {
334 com_err(0, 0, "Usage: dirsearch dir filename");
335 return;
336 }
337
338 inode = string_to_inode(argv[1]);
339 if (!inode)
340 return;
341
342 pb.buf = malloc(current_fs->blocksize);
343 if (!pb.buf) {
344 com_err("dirsearch", 0, "Couldn't allocate buffer");
345 return;
346 }
347 pb.search_name = argv[2];
348 pb.len = strlen(pb.search_name);
349
350 ext2fs_block_iterate2(current_fs, inode, 0, 0, search_dir_block, &pb);
351
352 free(pb.buf);
353}
354
355
356static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -0500357 e2_blkcnt_t blockcnt,
358 blk_t ref_blk EXT2FS_ATTR((unused)),
359 int ref_offset EXT2FS_ATTR((unused)),
360 void *priv_data)
Theodore Ts'odf614db2002-02-25 04:28:45 -0500361{
362 struct process_block_struct *p;
363 struct ext2_dir_entry *dirent;
364 errcode_t errcode;
Theodore Ts'o54434922003-12-07 01:28:50 -0500365 unsigned int offset = 0;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500366
367 if (blockcnt < 0)
368 return 0;
369
370 p = (struct process_block_struct *) priv_data;
371
372 errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf);
373 if (errcode) {
374 com_err("search_dir_block", errcode,
Theodore Ts'o54434922003-12-07 01:28:50 -0500375 "while reading block %lu", (unsigned long) *blocknr);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500376 return BLOCK_ABORT;
377 }
378
379 while (offset < fs->blocksize) {
380 dirent = (struct ext2_dir_entry *) (p->buf + offset);
381
382 if (dirent->inode &&
383 p->len == (dirent->name_len & 0xFF) &&
384 strncmp(p->search_name, dirent->name,
385 p->len) == 0) {
386 printf("Entry found at logical block %lld, "
Takashi Sato8deb80a2006-03-18 21:43:46 -0500387 "phys %u, offset %u\n", blockcnt,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500388 *blocknr, offset);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500389 printf("offset %u\n", offset);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500390 return BLOCK_ABORT;
391 }
392 offset += dirent->rec_len;
393 }
394 return 0;
395}
396