Whamcloud - gitweb
Sync kernel's fix for potential double free in jbd2
[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             hash_flags = inode->i_flags & EXT4_CASEFOLD_FL;
48         int             csum_size = 0;
49
50         if (ext2fs_has_feature_metadata_csum(fs->super))
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                     ((unsigned) 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_dirhash2(hash_alg, name, thislen,
94                                           fs->encoding, hash_flags,
95                                           fs->super->s_hash_seed,
96                                           &hash, &minor_hash);
97                 if (errcode)
98                         com_err("htree_dump_leaf_node", errcode,
99                                 "while calculating hash");
100                 if ((offset == fs->blocksize - csum_size) &&
101                     (dirent->inode == 0) &&
102                     (dirent->rec_len == csum_size) &&
103                     (dirent->name_len == EXT2_DIR_NAME_LEN_CSUM)) {
104                         struct ext2_dir_entry_tail *t;
105
106                         t = (struct ext2_dir_entry_tail *) dirent;
107
108                         snprintf(tmp, EXT2_NAME_LEN + 64,
109                                  "leaf block checksum: 0x%08x  ",
110                                  t->det_checksum);
111                 } else {
112                         snprintf(tmp, EXT2_NAME_LEN + 64,
113                                  "%u 0x%08x-%08x (%d) %s   ",
114                                  dirent->inode, hash, minor_hash,
115                                  rec_len, name);
116                 }
117                 thislen = strlen(tmp);
118                 if (col + thislen > 80) {
119                         fprintf(pager, "\n");
120                         col = 0;
121                 }
122                 fprintf(pager, "%s", tmp);
123                 col += thislen;
124                 offset += rec_len;
125         }
126         fprintf(pager, "\n");
127 }
128
129
130 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
131                                  struct ext2_inode *inode,
132                                  struct ext2_dx_root_info * rootnode,
133                                  blk64_t blk, char *buf, int level);
134
135
136 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
137                                 struct ext2_inode *inode,
138                                 struct ext2_dx_root_info * rootnode,
139                                 struct ext2_dx_entry *ent, __u32 crc,
140                                 char *buf, int level)
141 {
142         struct ext2_dx_countlimit       dx_countlimit;
143         struct ext2_dx_tail             *tail;
144         int                             hash, i;
145         int                             limit, count;
146         int                             remainder;
147
148         dx_countlimit = *((struct ext2_dx_countlimit *) ent);
149         count = ext2fs_le16_to_cpu(dx_countlimit.count);
150         limit = ext2fs_le16_to_cpu(dx_countlimit.limit);
151
152         fprintf(pager, "Number of entries (count): %d\n", count);
153         fprintf(pager, "Number of entries (limit): %d\n", limit);
154
155         remainder = fs->blocksize - (limit * 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 (ext2fs_has_feature_metadata_csum(fs->super) &&
161             remainder == sizeof(struct ext2_dx_tail)) {
162                 tail = (struct ext2_dx_tail *)(ent + limit);
163                 fprintf(pager, "Checksum: 0x%08x",
164                         ext2fs_le32_to_cpu(tail->dt_checksum));
165                 if (tail->dt_checksum != crc)
166                         fprintf(pager, " --- EXPECTED: 0x%08x", crc);
167                 fputc('\n', pager);
168         }
169
170         for (i=0; i < count; i++) {
171                 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
172                 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
173                         hash, (hash & 1) ? " (**)" : "",
174                         ext2fs_le32_to_cpu(ent[i].block));
175                 }
176
177         fprintf(pager, "\n");
178
179         for (i=0; i < count; i++) {
180                 unsigned int hashval, block;
181
182                 hashval = ext2fs_le32_to_cpu(ent[i].hash);
183                 block = ext2fs_le32_to_cpu(ent[i].block);
184                 fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
185                        i ? hashval : 0, block);
186                 if (level)
187                         htree_dump_int_block(fs, ino, inode, rootnode,
188                                              block, buf, level-1);
189                 else
190                         htree_dump_leaf_node(fs, ino, inode, rootnode,
191                                              block, buf);
192         }
193
194         fprintf(pager, "---------------------\n");
195 }
196
197 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
198                                  struct ext2_inode *inode,
199                                  struct ext2_dx_root_info * rootnode,
200                                  blk64_t blk, char *buf, int level)
201 {
202         char            *cbuf;
203         errcode_t       errcode;
204         blk64_t         pblk;
205         __u32           crc;
206
207         cbuf = malloc(fs->blocksize);
208         if (!cbuf) {
209                 fprintf(pager, "Couldn't allocate child block.\n");
210                 return;
211         }
212
213         errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
214         if (errcode) {
215                 com_err("htree_dump_int_block", errcode,
216                         "while mapping logical block %llu\n", blk);
217                 goto errout;
218         }
219
220         errcode = io_channel_read_blk64(current_fs->io, pblk, 1, buf);
221         if (errcode) {
222                 com_err("htree_dump_int_block", errcode,
223                         "while reading block %llu\n", blk);
224                 goto errout;
225         }
226
227         errcode = ext2fs_dx_csum(current_fs, ino,
228                                  (struct ext2_dir_entry *) buf, &crc, NULL);
229         if (errcode) {
230                 com_err("htree_dump_int_block", errcode,
231                         "while calculating checksum for logical block %llu\n",
232                         (unsigned long long) blk);
233                 crc = (unsigned int) -1;
234         }
235         htree_dump_int_node(fs, ino, inode, rootnode,
236                             (struct ext2_dx_entry *) (buf+8),
237                             crc, cbuf, level);
238 errout:
239         free(cbuf);
240 }
241
242
243
244 void do_htree_dump(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
245                    void *infop EXT2FS_ATTR((unused)))
246 {
247         ext2_ino_t      ino;
248         struct ext2_inode inode;
249         blk64_t         blk;
250         char            *buf = NULL;
251         struct          ext2_dx_root_info  *rootnode;
252         struct          ext2_dx_entry *ent;
253         errcode_t       errcode;
254         __u32           crc;
255
256         if (check_fs_open(argv[0]))
257                 return;
258
259         pager = open_pager();
260
261         if (common_inode_args_process(argc, argv, &ino, 0))
262                 goto errout;
263
264         if (debugfs_read_inode(ino, &inode, argv[1]))
265                 goto errout;
266
267         if (!LINUX_S_ISDIR(inode.i_mode)) {
268                 com_err(argv[0], 0, "Not a directory");
269                 goto errout;
270         }
271
272         if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
273                 com_err(argv[0], 0, "Not a hash-indexed directory");
274                 goto errout;
275         }
276
277         buf = malloc(2*current_fs->blocksize);
278         if (!buf) {
279                 com_err(argv[0], 0, "Couldn't allocate htree buffer");
280                 goto errout;
281         }
282
283         errcode = ext2fs_bmap2(current_fs, ino, &inode, buf, 0, 0, 0, &blk);
284         if (errcode) {
285                 com_err("do_htree_block", errcode,
286                         "while mapping logical block 0\n");
287                 goto errout;
288         }
289
290         errcode = io_channel_read_blk64(current_fs->io, blk,
291                                         1, buf);
292         if (errcode) {
293                 com_err(argv[0], errcode, "Error reading root node");
294                 goto errout;
295         }
296
297         rootnode = (struct ext2_dx_root_info *) (buf + 24);
298
299         fprintf(pager, "Root node dump:\n");
300         fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
301         fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
302         fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
303         fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
304         fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
305
306         ent = (struct ext2_dx_entry *)
307                 ((char *)rootnode + rootnode->info_length);
308
309         errcode = ext2fs_dx_csum(current_fs, ino,
310                                  (struct ext2_dir_entry *) buf, &crc, NULL);
311         if (errcode) {
312                 com_err("htree_dump_int_block", errcode,
313                         "while calculating checksum for htree root\n");
314                 crc = (unsigned int) -1;
315         }
316         htree_dump_int_node(current_fs, ino, &inode, rootnode, ent, crc,
317                             buf + current_fs->blocksize,
318                             rootnode->indirect_levels);
319
320 errout:
321         free(buf);
322         close_pager(pager);
323 }
324
325 /*
326  * This function prints the hash of a given file.
327  */
328 void do_dx_hash(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
329                 void *infop EXT2FS_ATTR((unused)))
330 {
331         ext2_dirhash_t hash, minor_hash;
332         errcode_t       err;
333         int             c;
334         int             hash_version = 0;
335         __u32           hash_seed[4];
336         int             hash_flags = 0;
337         const struct ext2fs_nls_table *encoding = NULL;
338
339         hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
340
341         reset_getopt();
342         while ((c = getopt(argc, argv, "h:s:ce:")) != EOF) {
343                 switch (c) {
344                 case 'h':
345                         hash_version = e2p_string2hash(optarg);
346                         if (hash_version < 0)
347                                 hash_version = atoi(optarg);
348                         break;
349                 case 's':
350                         if (uuid_parse(optarg, (unsigned char *) hash_seed)) {
351                                 fprintf(stderr, "Invalid UUID format: %s\n",
352                                         optarg);
353                                 return;
354                         }
355                         break;
356                 case 'c':
357                         hash_flags |= EXT4_CASEFOLD_FL;
358                         break;
359                 case 'e':
360                         encoding = ext2fs_load_nls_table(e2p_str2encoding(optarg));
361                         if (!encoding) {
362                                 fprintf(stderr, "Invalid encoding: %s\n",
363                                         optarg);
364                                 return;
365                         }
366                         break;
367                 default:
368                         goto print_usage;
369                 }
370         }
371         if (optind != argc-1) {
372         print_usage:
373                 com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
374                         "[-s hash_seed] [-c] [-e encoding] filename");
375                 return;
376         }
377         err = ext2fs_dirhash2(hash_version, argv[optind],
378                               strlen(argv[optind]), encoding, hash_flags,
379                               hash_seed, &hash, &minor_hash);
380
381         if (err) {
382                 com_err(argv[0], err, "while calculating hash");
383                 return;
384         }
385         printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
386                hash, minor_hash);
387 }
388
389 /*
390  * Search for particular directory entry (useful for debugging very
391  * large hash tree directories that have lost some blocks from the
392  * btree index).
393  */
394 struct process_block_struct {
395         char    *search_name;
396         char    *buf;
397         int     len;
398 };
399
400 static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
401                             e2_blkcnt_t blockcnt, blk64_t ref_blk,
402                             int ref_offset, void *priv_data);
403
404 void do_dirsearch(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
405                   void *infop EXT2FS_ATTR((unused)))
406 {
407         ext2_ino_t      inode;
408         struct process_block_struct pb;
409
410         if (check_fs_open(argv[0]))
411                 return;
412
413         if (argc != 3) {
414                 com_err(0, 0, "Usage: dirsearch dir filename");
415                 return;
416         }
417
418         inode = string_to_inode(argv[1]);
419         if (!inode)
420                 return;
421
422         pb.buf = malloc(current_fs->blocksize);
423         if (!pb.buf) {
424                 com_err("dirsearch", 0, "Couldn't allocate buffer");
425                 return;
426         }
427         pb.search_name = argv[2];
428         pb.len = strlen(pb.search_name);
429
430         ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
431                               search_dir_block, &pb);
432
433         free(pb.buf);
434 }
435
436
437 static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
438                             e2_blkcnt_t blockcnt,
439                             blk64_t ref_blk EXT2FS_ATTR((unused)),
440                             int ref_offset EXT2FS_ATTR((unused)),
441                             void *priv_data)
442 {
443         struct process_block_struct *p;
444         struct ext2_dir_entry *dirent;
445         errcode_t               errcode;
446         unsigned int            offset = 0;
447         unsigned int            rec_len;
448
449         if (blockcnt < 0)
450                 return 0;
451
452         p = (struct process_block_struct *) priv_data;
453
454         errcode = io_channel_read_blk64(current_fs->io, *blocknr, 1, p->buf);
455         if (errcode) {
456                 com_err("search_dir_block", errcode,
457                         "while reading block %lu", (unsigned long) *blocknr);
458                 return BLOCK_ABORT;
459         }
460
461         while (offset < fs->blocksize) {
462                 dirent = (struct ext2_dir_entry *) (p->buf + offset);
463                 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
464                 if (errcode) {
465                         com_err("htree_dump_leaf_inode", errcode,
466                                 "while getting rec_len for block %lu",
467                                 (unsigned long) *blocknr);
468                         return BLOCK_ABORT;
469                 }
470                 if (dirent->inode &&
471                     p->len == ext2fs_dirent_name_len(dirent) &&
472                     strncmp(p->search_name, dirent->name,
473                             p->len) == 0) {
474                         printf("Entry found at logical block %lld, "
475                                "phys %llu, offset %u\n", (long long)blockcnt,
476                                *blocknr, offset);
477                         printf("offset %u\n", offset);
478                         return BLOCK_ABORT;
479                 }
480                 offset += rec_len;
481         }
482         return 0;
483 }
484