Whamcloud - gitweb
Fix potential 2**32-1 overflow by using e2p_percent()
[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 %u\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 %u\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 (%u)!\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 %u\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 %u\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 %u\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                 default:
201                         goto print_usage;
202                 }
203         }
204
205         if (argc > optind+1) {
206         print_usage:
207                 com_err(0, 0, "Usage: htree_dump [-l] file");
208                 goto errout;
209         }
210
211         if (argc == optind)
212                 ino = cwd;
213         else
214                 ino = string_to_inode(argv[optind]);
215         if (!ino)
216                 goto errout;
217
218         if (debugfs_read_inode(ino, &inode, argv[1]))
219                 goto errout;
220
221         if (!LINUX_S_ISDIR(inode.i_mode)) {
222                 com_err(argv[0], 0, "Not a directory");
223                 goto errout;
224         }
225         
226         if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
227                 com_err(argv[0], 0, "Not a hash-indexed directory");
228                 goto errout;
229         }
230
231         buf = malloc(2*current_fs->blocksize);
232         if (!buf) {
233                 com_err(argv[0], 0, "Couldn't allocate htree buffer");
234                 goto errout;
235         }
236
237         errcode = io_channel_read_blk(current_fs->io, inode.i_block[0], 
238                                       1, buf);
239         if (errcode) {
240                 com_err(argv[0], errcode, "Error reading root node");
241                 goto errout;
242         }
243
244         rootnode = (struct ext2_dx_root_info *) (buf + 24);
245
246         fprintf(pager, "Root node dump:\n");
247         fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
248         fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
249         fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
250         fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
251         fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
252
253         ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
254         limit = (struct ext2_dx_countlimit *) ent;
255
256         htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
257                             buf + current_fs->blocksize,
258                             rootnode->indirect_levels);
259
260 errout:
261         if (buf)
262                 free(buf);
263         close_pager(pager);
264 }
265
266 /*
267  * This function prints the hash of a given file.
268  */
269 void do_dx_hash(int argc, char *argv[])
270 {
271         ext2_dirhash_t hash, minor_hash;
272         errcode_t       err;
273         int             c;
274         int             hash_version = 0;
275         __u32           hash_seed[4];
276         
277         hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
278
279         reset_getopt();
280         while ((c = getopt (argc, argv, "h:")) != EOF) {
281                 switch (c) {
282                 case 'h':
283                         hash_version = atoi(optarg);
284                         break;
285                 default:
286                         goto print_usage;
287                 }
288         }
289         if (optind != argc-1) {
290         print_usage:
291                 com_err(argv[0], 0, "usage: dx_hash filename");
292                 return;
293         }
294         err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
295                              hash_seed, &hash, &minor_hash);
296         if (err) {
297                 com_err(argv[0], err, "while caclulating hash");
298                 return;
299         }
300         printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
301                hash, minor_hash);
302 }
303
304 /*
305  * Search for particular directory entry (useful for debugging very
306  * large hash tree directories that have lost some blocks from the
307  * btree index).
308  */
309 struct process_block_struct {
310         char    *search_name;
311         char    *buf;
312         int     len;
313 };
314
315 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
316                             e2_blkcnt_t blockcnt, blk_t ref_blk, 
317                             int ref_offset, void *priv_data);
318
319 void do_dirsearch(int argc, char *argv[])
320 {
321         ext2_ino_t      inode;
322         struct process_block_struct pb;
323         
324         if (check_fs_open(argv[0]))
325                 return;
326
327         if (argc != 3) {
328                 com_err(0, 0, "Usage: dirsearch dir filename");
329                 return;
330         }
331
332         inode = string_to_inode(argv[1]);
333         if (!inode)
334                 return;
335
336         pb.buf = malloc(current_fs->blocksize);
337         if (!pb.buf) {
338                 com_err("dirsearch", 0, "Couldn't allocate buffer");
339                 return;
340         }
341         pb.search_name = argv[2];
342         pb.len = strlen(pb.search_name);
343         
344         ext2fs_block_iterate2(current_fs, inode, 0, 0, search_dir_block, &pb);
345
346         free(pb.buf);
347 }
348
349
350 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
351                             e2_blkcnt_t blockcnt, 
352                             blk_t ref_blk EXT2FS_ATTR((unused)),
353                             int ref_offset EXT2FS_ATTR((unused)),
354                             void *priv_data)
355 {
356         struct process_block_struct *p;
357         struct ext2_dir_entry *dirent;
358         errcode_t               errcode;
359         unsigned int            offset = 0;
360
361         if (blockcnt < 0)
362                 return 0;
363
364         p = (struct process_block_struct *) priv_data;
365
366         errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf);
367         if (errcode) {
368                 com_err("search_dir_block", errcode,
369                         "while reading block %lu", (unsigned long) *blocknr);
370                 return BLOCK_ABORT;
371         }
372
373         while (offset < fs->blocksize) {
374                 dirent = (struct ext2_dir_entry *) (p->buf + offset);
375
376                 if (dirent->inode &&
377                     p->len == (dirent->name_len & 0xFF) && 
378                     strncmp(p->search_name, dirent->name,
379                             p->len) == 0) {
380                         printf("Entry found at logical block %lld, "
381                                "phys %u, offset %u\n", blockcnt,
382                                *blocknr, offset);
383                         printf("offset %u\n", offset);
384                         return BLOCK_ABORT;
385                 }
386                 offset += dirent->rec_len;
387         }
388         return 0;
389 }
390