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