Whamcloud - gitweb
Sync kernel's fix for potential double free in jbd2
[tools/e2fsprogs.git] / debugfs / htree.c
index 866d1c6..b567a76 100644 (file)
@@ -44,10 +44,10 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
        ext2_dirhash_t  hash, minor_hash;
        unsigned int    rec_len;
        int             hash_alg;
+       int             hash_flags = inode->i_flags & EXT4_CASEFOLD_FL;
        int             csum_size = 0;
 
-       if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
-                                      EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
+       if (ext2fs_has_feature_metadata_csum(fs->super))
                csum_size = sizeof(struct ext2_dir_entry_tail);
 
        errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
@@ -83,16 +83,17 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
                if (((offset + rec_len) > fs->blocksize) ||
                    (rec_len < 8) ||
                    ((rec_len % 4) != 0) ||
-                   (thislen + 8 > rec_len)) {
+                   ((unsigned) thislen + 8 > rec_len)) {
                        fprintf(pager, "Corrupted directory block (%llu)!\n",
                                blk);
                        break;
                }
                strncpy(name, dirent->name, thislen);
                name[thislen] = '\0';
-               errcode = ext2fs_dirhash(hash_alg, name,
-                                        thislen, fs->super->s_hash_seed,
-                                        &hash, &minor_hash);
+               errcode = ext2fs_dirhash2(hash_alg, name, thislen,
+                                         fs->encoding, hash_flags,
+                                         fs->super->s_hash_seed,
+                                         &hash, &minor_hash);
                if (errcode)
                        com_err("htree_dump_leaf_node", errcode,
                                "while calculating hash");
@@ -135,7 +136,7 @@ static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
                                struct ext2_inode *inode,
                                struct ext2_dx_root_info * rootnode,
-                               struct ext2_dx_entry *ent,
+                               struct ext2_dx_entry *ent, __u32 crc,
                                char *buf, int level)
 {
        struct ext2_dx_countlimit       dx_countlimit;
@@ -156,12 +157,14 @@ static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
                remainder -= sizeof(struct ext2_dx_root_info) + 24;
        else
                remainder -= 8;
-       if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
-                                      EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
+       if (ext2fs_has_feature_metadata_csum(fs->super) &&
            remainder == sizeof(struct ext2_dx_tail)) {
                tail = (struct ext2_dx_tail *)(ent + limit);
-               fprintf(pager, "Checksum: 0x%08x\n",
+               fprintf(pager, "Checksum: 0x%08x",
                        ext2fs_le32_to_cpu(tail->dt_checksum));
+               if (tail->dt_checksum != crc)
+                       fprintf(pager, " --- EXPECTED: 0x%08x", crc);
+               fputc('\n', pager);
        }
 
        for (i=0; i < count; i++) {
@@ -174,12 +177,12 @@ static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
        fprintf(pager, "\n");
 
        for (i=0; i < count; i++) {
-               int hash, block;
+               unsigned int hashval, block;
 
-               hash = ext2fs_le32_to_cpu(ent[i].hash);
+               hashval = ext2fs_le32_to_cpu(ent[i].hash);
                block = ext2fs_le32_to_cpu(ent[i].block);
                fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
-                      i ? hash : 0, block);
+                      i ? hashval : 0, block);
                if (level)
                        htree_dump_int_block(fs, ino, inode, rootnode,
                                             block, buf, level-1);
@@ -199,6 +202,7 @@ static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
        char            *cbuf;
        errcode_t       errcode;
        blk64_t         pblk;
+       __u32           crc;
 
        cbuf = malloc(fs->blocksize);
        if (!cbuf) {
@@ -216,20 +220,29 @@ static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
        errcode = io_channel_read_blk64(current_fs->io, pblk, 1, buf);
        if (errcode) {
                com_err("htree_dump_int_block", errcode,
-                       "while  reading block %llu\n", blk);
+                       "while reading block %llu\n", blk);
                goto errout;
        }
 
+       errcode = ext2fs_dx_csum(current_fs, ino,
+                                (struct ext2_dir_entry *) buf, &crc, NULL);
+       if (errcode) {
+               com_err("htree_dump_int_block", errcode,
+                       "while calculating checksum for logical block %llu\n",
+                       (unsigned long long) blk);
+               crc = (unsigned int) -1;
+       }
        htree_dump_int_node(fs, ino, inode, rootnode,
                            (struct ext2_dx_entry *) (buf+8),
-                           cbuf, level);
+                           crc, cbuf, level);
 errout:
        free(cbuf);
 }
 
 
 
-void do_htree_dump(int argc, char *argv[])
+void do_htree_dump(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
+                  void *infop EXT2FS_ATTR((unused)))
 {
        ext2_ino_t      ino;
        struct ext2_inode inode;
@@ -238,6 +251,7 @@ void do_htree_dump(int argc, char *argv[])
        struct          ext2_dx_root_info  *rootnode;
        struct          ext2_dx_entry *ent;
        errcode_t       errcode;
+       __u32           crc;
 
        if (check_fs_open(argv[0]))
                return;
@@ -289,9 +303,17 @@ void do_htree_dump(int argc, char *argv[])
        fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
        fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
 
-       ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
+       ent = (struct ext2_dx_entry *)
+               ((char *)rootnode + rootnode->info_length);
 
-       htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
+       errcode = ext2fs_dx_csum(current_fs, ino,
+                                (struct ext2_dir_entry *) buf, &crc, NULL);
+       if (errcode) {
+               com_err("htree_dump_int_block", errcode,
+                       "while calculating checksum for htree root\n");
+               crc = (unsigned int) -1;
+       }
+       htree_dump_int_node(current_fs, ino, &inode, rootnode, ent, crc,
                            buf + current_fs->blocksize,
                            rootnode->indirect_levels);
 
@@ -303,18 +325,21 @@ errout:
 /*
  * This function prints the hash of a given file.
  */
-void do_dx_hash(int argc, char *argv[])
+void do_dx_hash(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
+               void *infop EXT2FS_ATTR((unused)))
 {
        ext2_dirhash_t hash, minor_hash;
        errcode_t       err;
        int             c;
        int             hash_version = 0;
        __u32           hash_seed[4];
+       int             hash_flags = 0;
+       const struct ext2fs_nls_table *encoding = NULL;
 
        hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
 
        reset_getopt();
-       while ((c = getopt (argc, argv, "h:s:")) != EOF) {
+       while ((c = getopt(argc, argv, "h:s:ce:")) != EOF) {
                switch (c) {
                case 'h':
                        hash_version = e2p_string2hash(optarg);
@@ -328,6 +353,17 @@ void do_dx_hash(int argc, char *argv[])
                                return;
                        }
                        break;
+               case 'c':
+                       hash_flags |= EXT4_CASEFOLD_FL;
+                       break;
+               case 'e':
+                       encoding = ext2fs_load_nls_table(e2p_str2encoding(optarg));
+                       if (!encoding) {
+                               fprintf(stderr, "Invalid encoding: %s\n",
+                                       optarg);
+                               return;
+                       }
+                       break;
                default:
                        goto print_usage;
                }
@@ -335,13 +371,15 @@ void do_dx_hash(int argc, char *argv[])
        if (optind != argc-1) {
        print_usage:
                com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
-                       "[-s hash_seed] filename");
+                       "[-s hash_seed] [-c] [-e encoding] filename");
                return;
        }
-       err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
-                            hash_seed, &hash, &minor_hash);
+       err = ext2fs_dirhash2(hash_version, argv[optind],
+                             strlen(argv[optind]), encoding, hash_flags,
+                             hash_seed, &hash, &minor_hash);
+
        if (err) {
-               com_err(argv[0], err, "while caclulating hash");
+               com_err(argv[0], err, "while calculating hash");
                return;
        }
        printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
@@ -363,7 +401,8 @@ static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
                            e2_blkcnt_t blockcnt, blk64_t ref_blk,
                            int ref_offset, void *priv_data);
 
-void do_dirsearch(int argc, char *argv[])
+void do_dirsearch(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
+                 void *infop EXT2FS_ATTR((unused)))
 {
        ext2_ino_t      inode;
        struct process_block_struct pb;