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