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