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