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