Whamcloud - gitweb
7af3d1b3bfcf66822082dfbe0fe2abf201474165
[tools/e2fsprogs.git] / debugfs / htree.c
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 <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 
21 extern int optind;
22 extern char *optarg;
23 #endif
24
25 #include "debugfs.h"
26
27 static FILE *pager;
28
29 static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
30                                  struct ext2_inode *inode,
31                                  struct ext2_dx_root_info * rootnode,
32                                  blk_t blk, char *buf)
33 {
34         errcode_t       errcode;
35         struct ext2_dir_entry *dirent;
36         int             thislen, col = 0;
37         unsigned int    offset = 0;
38         char            name[EXT2_NAME_LEN];
39         char            tmp[EXT2_NAME_LEN + 16];
40         blk_t           pblk;
41         ext2_dirhash_t  hash;
42         
43         errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
44         if (errcode) {
45                 com_err("htree_dump_leaf_node", errcode,
46                         "while mapping logical block %d\n", blk);
47                 return;
48         }
49
50         errcode = ext2fs_read_dir_block2(current_fs, pblk, buf, 0);
51         if (errcode) {
52                 com_err("htree_dump_leaf_node", errcode,
53                         "while  reading block %d\n", blk);
54                 return;
55         }
56
57         while (offset < fs->blocksize) {
58                 dirent = (struct ext2_dir_entry *) (buf + offset);
59                 if (((offset + dirent->rec_len) > fs->blocksize) ||
60                     (dirent->rec_len < 8) ||
61                     ((dirent->rec_len % 4) != 0) ||
62                     (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
63                         fprintf(pager, "Corrupted directory block (%d)!\n", blk);
64                         break;
65                 }
66                 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
67                         (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
68                 strncpy(name, dirent->name, thislen);
69                 name[thislen] = '\0';
70                 errcode = ext2fs_dirhash(rootnode->hash_version, name,
71                                          thislen, fs->super->s_hash_seed,
72                                          &hash, 0);
73                 if (errcode)
74                         com_err("htree_dump_leaf_node", errcode,
75                                 "while calculating hash");
76                 sprintf(tmp, "%u 0x%08x (%d) %s   ", dirent->inode,
77                         hash, dirent->rec_len, name);
78                 thislen = strlen(tmp);
79                 if (col + thislen > 80) {
80                         fprintf(pager, "\n");
81                         col = 0;
82                 }
83                 fprintf(pager, "%s", tmp);
84                 col += thislen;
85                 offset += dirent->rec_len;
86         }
87         fprintf(pager, "\n");
88 }
89
90
91 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
92                                  struct ext2_inode *inode,
93                                  struct ext2_dx_root_info * rootnode,
94                                  blk_t blk, char *buf, int level);
95
96
97 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
98                                 struct ext2_inode *inode,
99                                 struct ext2_dx_root_info * rootnode,
100                                 struct ext2_dx_entry *ent, 
101                                 char *buf, int level)
102 {
103         struct ext2_dx_countlimit       limit;
104         struct ext2_dx_entry            e;
105         int                             hash, i;
106         
107
108         limit = *((struct ext2_dx_countlimit *) ent);
109         limit.count = ext2fs_le16_to_cpu(limit.count);
110         limit.limit = ext2fs_le16_to_cpu(limit.limit);
111
112         fprintf(pager, "Number of entries (count): %d\n", limit.count);
113         fprintf(pager, "Number of entries (limit): %d\n", limit.limit);
114
115         for (i=0; i < limit.count; i++) {
116                 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
117                 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %d\n", i,
118                         hash, (hash & 1) ? " (**)" : "",
119                         ext2fs_le32_to_cpu(ent[i].block));
120                 }
121
122         fprintf(pager, "\n");
123
124         for (i=0; i < limit.count; i++) {
125                 e.hash = ext2fs_le32_to_cpu(ent[i].hash);
126                 e.block = ext2fs_le32_to_cpu(ent[i].block);
127                 fprintf(pager, "Entry #%d: Hash 0x%08x, block %d\n", i,
128                        i ? e.hash : 0, e.block);
129                 if (level)
130                         htree_dump_int_block(fs, ino, inode, rootnode,
131                                              e.block, buf, level-1);
132                 else
133                         htree_dump_leaf_node(fs, ino, inode, rootnode,
134                                              e.block, buf);
135         }
136
137         fprintf(pager, "---------------------\n");
138 }
139
140 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
141                                  struct ext2_inode *inode,
142                                  struct ext2_dx_root_info * rootnode,
143                                  blk_t blk, char *buf, int level)
144 {
145         char            *cbuf;
146         errcode_t       errcode;
147         blk_t           pblk;
148
149         cbuf = malloc(fs->blocksize);
150         if (!cbuf) {
151                 fprintf(pager, "Couldn't allocate child block.\n");
152                 return;
153         }
154         
155         errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
156         if (errcode) {
157                 com_err("htree_dump_int_block", errcode,
158                         "while mapping logical block %d\n", blk);
159                 return;
160         }
161
162         errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf);
163         if (errcode) {
164                 com_err("htree_dump_int_block", errcode,
165                         "while  reading block %d\n", blk);
166                 return;
167         }
168
169         htree_dump_int_node(fs, ino, inode, rootnode,
170                             (struct ext2_dx_entry *) (buf+8),
171                             cbuf, level);
172         free(cbuf);
173 }
174
175
176
177 void do_htree_dump(int argc, char *argv[])
178 {
179         ext2_ino_t      ino;
180         struct ext2_inode inode;
181         int             c;
182         int             long_opt;
183         char            *buf = NULL;
184         struct          ext2_dx_root_info  *rootnode;
185         struct          ext2_dx_entry *ent;
186         struct          ext2_dx_countlimit *limit;
187         errcode_t       errcode;
188
189         if (check_fs_open(argv[0]))
190                 return;
191
192         pager = open_pager();
193
194         reset_getopt();
195         while ((c = getopt (argc, argv, "l")) != EOF) {
196                 switch (c) {
197                 case 'l':
198                         long_opt++;
199                         break;
200                 }
201         }
202
203         if (argc > optind+1) {
204                 com_err(0, 0, "Usage: htree_dump [-l] file");
205                 goto errout;
206         }
207
208         if (argc == optind)
209                 ino = cwd;
210         else
211                 ino = string_to_inode(argv[optind]);
212         if (!ino)
213                 goto errout;
214
215         if (debugfs_read_inode(ino, &inode, argv[1]))
216                 goto errout;
217
218         if (!LINUX_S_ISDIR(inode.i_mode)) {
219                 com_err(argv[0], 0, "Not a directory");
220                 goto errout;
221         }
222         
223         if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
224                 com_err(argv[0], 0, "Not a hash-indexed directory");
225                 goto errout;
226         }
227
228         buf = malloc(2*current_fs->blocksize);
229         if (!buf) {
230                 com_err(argv[0], 0, "Couldn't allocate htree buffer");
231                 goto errout;
232         }
233
234         errcode = io_channel_read_blk(current_fs->io, inode.i_block[0], 
235                                       1, buf);
236         if (errcode) {
237                 com_err(argv[0], errcode, "Error reading root node");
238                 goto errout;
239         }
240
241         rootnode = (struct ext2_dx_root_info *) (buf + 24);
242
243         fprintf(pager, "Root node dump:\n");
244         fprintf(pager, "\t Reserved zero: %d\n", rootnode->reserved_zero);
245         fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
246         fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
247         fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
248         fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
249
250         ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
251         limit = (struct ext2_dx_countlimit *) ent;
252
253         htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
254                             buf + current_fs->blocksize,
255                             rootnode->indirect_levels);
256
257 errout:
258         if (buf)
259                 free(buf);
260         close_pager(pager);
261 }
262
263 /*
264  * This function prints the hash of a given file.
265  */
266 void do_dx_hash(int argc, char *argv[])
267 {
268         ext2_dirhash_t hash, minor_hash;
269         errcode_t       err;
270         int             c;
271         int             hash_version = 0;
272         __u32           hash_seed[4];
273         
274         hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
275
276         reset_getopt();
277         while ((c = getopt (argc, argv, "h:")) != EOF) {
278                 switch (c) {
279                 case 'h':
280                         hash_version = atoi(optarg);
281                         break;
282                 }
283         }
284         if (optind != argc-1) {
285                 com_err(argv[0], 0, "usage: dx_hash filename");
286                 return;
287         }
288         err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
289                              hash_seed, &hash, &minor_hash);
290         if (err) {
291                 com_err(argv[0], err, "while caclulating hash");
292                 return;
293         }
294         printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
295                hash, minor_hash);
296 }
297
298 /*
299  * Search for particular directory entry (useful for debugging very
300  * large hash tree directories that have lost some blocks from the
301  * btree index).
302  */
303 struct process_block_struct {
304         char    *search_name;
305         char    *buf;
306         int     len;
307 };
308
309 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
310                             e2_blkcnt_t blockcnt, blk_t ref_blk, 
311                             int ref_offset, void *priv_data);
312
313 void do_dirsearch(int argc, char *argv[])
314 {
315         ext2_ino_t      inode;
316         struct process_block_struct pb;
317         
318         if (check_fs_open(argv[0]))
319                 return;
320
321         if (argc != 3) {
322                 com_err(0, 0, "Usage: dirsearch dir filename");
323                 return;
324         }
325
326         inode = string_to_inode(argv[1]);
327         if (!inode)
328                 return;
329
330         pb.buf = malloc(current_fs->blocksize);
331         if (!pb.buf) {
332                 com_err("dirsearch", 0, "Couldn't allocate buffer");
333                 return;
334         }
335         pb.search_name = argv[2];
336         pb.len = strlen(pb.search_name);
337         
338         ext2fs_block_iterate2(current_fs, inode, 0, 0, search_dir_block, &pb);
339
340         free(pb.buf);
341 }
342
343
344 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
345                             e2_blkcnt_t blockcnt, 
346                             blk_t ref_blk EXT2FS_ATTR((unused)),
347                             int ref_offset EXT2FS_ATTR((unused)),
348                             void *priv_data)
349 {
350         struct process_block_struct *p;
351         struct ext2_dir_entry *dirent;
352         errcode_t               errcode;
353         unsigned int            offset = 0;
354
355         if (blockcnt < 0)
356                 return 0;
357
358         p = (struct process_block_struct *) priv_data;
359
360         errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf);
361         if (errcode) {
362                 com_err("search_dir_block", errcode,
363                         "while reading block %lu", (unsigned long) *blocknr);
364                 return BLOCK_ABORT;
365         }
366
367         while (offset < fs->blocksize) {
368                 dirent = (struct ext2_dir_entry *) (p->buf + offset);
369
370                 if (dirent->inode &&
371                     p->len == (dirent->name_len & 0xFF) && 
372                     strncmp(p->search_name, dirent->name,
373                             p->len) == 0) {
374                         printf("Entry found at logical block %lld, "
375                                "phys %d, offset %d\n", blockcnt,
376                                *blocknr, offset);
377                         printf("offset %d\n", offset);
378                         return BLOCK_ABORT;
379                 }
380                 offset += dirent->rec_len;
381         }
382         return 0;
383 }
384