Whamcloud - gitweb
Remove trailing whitespace for the entire source tree
[tools/e2fsprogs.git] / e2fsck / rehash.c
index 727e08c..6c24bdd 100644 (file)
@@ -1,13 +1,13 @@
 /*
  * rehash.c --- rebuild hash tree directories
- * 
+ *
  * Copyright (C) 2002 Theodore Ts'o
  *
  * %Begin-Header%
  * This file may be redistributed under the terms of the GNU Public
  * License.
  * %End-Header%
- * 
+ *
  * This algorithm is designed for simplicity of implementation and to
  * pack the directory as much as possible.  It however requires twice
  * as much memory as the size of the directory.  The maximum size
@@ -36,7 +36,7 @@
  *    --------------------------------------------------------
  *                  ^ ptr    ^ptr
  *                tail new   head old
- * 
+ *
  * This is going to be a pain in the tuckus to implement, and will
  * require a lot more disk accesses.  So I'm going to skip it for now;
  * it's only really going to be an issue for really, really big
@@ -66,6 +66,7 @@ struct fill_dir_struct {
 struct hash_entry {
        ext2_dirhash_t  hash;
        ext2_dirhash_t  minor_hash;
+       ino_t           ino;
        struct ext2_dir_entry   *dir;
 };
 
@@ -88,7 +89,8 @@ static int fill_dir_block(ext2_filsys fs,
        struct ext2_dir_entry   *dirent;
        char                    *dir;
        unsigned int            offset, dir_offset;
-       
+       int                     rec_len, hash_alg;
+
        if (blockcnt < 0)
                return 0;
 
@@ -107,18 +109,24 @@ static int fill_dir_block(ext2_filsys fs,
                if (fd->err)
                        return BLOCK_ABORT;
        }
+       hash_alg = fs->super->s_def_hash_version;
+       if ((hash_alg <= EXT2_HASH_TEA) &&
+           (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
+               hash_alg += 3;
        /* While the directory block is "hot", index it. */
        dir_offset = 0;
        while (dir_offset < fs->blocksize) {
                dirent = (struct ext2_dir_entry *) (dir + dir_offset);
-               if (((dir_offset + dirent->rec_len) > fs->blocksize) ||
-                   (dirent->rec_len < 8) ||
-                   ((dirent->rec_len % 4) != 0) ||
-                   (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
+               rec_len = (dirent->rec_len || fs->blocksize < 65536) ?
+                       dirent->rec_len : 65536;
+               if (((dir_offset + rec_len) > fs->blocksize) ||
+                   (rec_len < 8) ||
+                   ((rec_len % 4) != 0) ||
+                   (((dirent->name_len & 0xFF)+8) > rec_len)) {
                        fd->err = EXT2_ET_DIR_CORRUPTED;
                        return BLOCK_ABORT;
                }
-               dir_offset += dirent->rec_len;
+               dir_offset += rec_len;
                if (dirent->inode == 0)
                        continue;
                if (!fd->compress && ((dirent->name_len&0xFF) == 1) &&
@@ -142,11 +150,11 @@ static int fill_dir_block(ext2_filsys fs,
                ent = fd->harray + fd->num_array++;
                ent->dir = dirent;
                fd->dir_size += EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
+               ent->ino = dirent->inode;
                if (fd->compress)
                        ent->hash = ent->minor_hash = 0;
                else {
-                       fd->err = ext2fs_dirhash(fs->super->s_def_hash_version,
-                                                dirent->name,
+                       fd->err = ext2fs_dirhash(hash_alg, dirent->name,
                                                 dirent->name_len & 0xFF,
                                                 fs->super->s_hash_seed,
                                                 &ent->hash, &ent->minor_hash);
@@ -154,11 +162,20 @@ static int fill_dir_block(ext2_filsys fs,
                                return BLOCK_ABORT;
                }
        }
-       
+
        return 0;
 }
 
 /* Used for sorting the hash entry */
+static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b)
+{
+       const struct hash_entry *he_a = (const struct hash_entry *) a;
+       const struct hash_entry *he_b = (const struct hash_entry *) b;
+
+       return (he_a->ino - he_b->ino);
+}
+
+/* Used for sorting the hash entry */
 static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
 {
        const struct hash_entry *he_a = (const struct hash_entry *) a;
@@ -188,7 +205,7 @@ static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
        const struct hash_entry *he_a = (const struct hash_entry *) a;
        const struct hash_entry *he_b = (const struct hash_entry *) b;
        int     ret;
-       
+
        if (he_a->hash > he_b->hash)
                ret = 1;
        else if (he_a->hash < he_b->hash)
@@ -204,7 +221,7 @@ static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
        return ret;
 }
 
-static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir, 
+static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
                                int blocks)
 {
        void                    *new_mem;
@@ -263,7 +280,7 @@ static void mutate_name(char *str, __u16 *len)
 {
        int     i;
        __u16   l = *len & 0xFF, h = *len & 0xff00;
-       
+
        /*
         * First check to see if it looks the name has been mutated
         * already
@@ -306,7 +323,7 @@ static void mutate_name(char *str, __u16 *len)
                } else {
                        if (str[0] == '~')
                                str[0] = 'a';
-                       else 
+                       else
                                str[0]++;
                }
                break;
@@ -323,10 +340,16 @@ static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
        int                     fixed = 0;
        char                    new_name[256];
        __u16                   new_len;
-       
+       int                     hash_alg;
+
        clear_problem_context(&pctx);
        pctx.ino = ino;
 
+       hash_alg = fs->super->s_def_hash_version;
+       if ((hash_alg <= EXT2_HASH_TEA) &&
+           (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
+               hash_alg += 3;
+
        for (i=1; i < fd->num_array; i++) {
                ent = fd->harray + i;
                prev = ent - 1;
@@ -355,7 +378,7 @@ static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
                                     new_len & 0xFF)))
                                continue;
                        mutate_name(new_name, &new_len);
-                       
+
                        j = -1;
                }
                new_name[new_len & 0xFF] = 0;
@@ -363,8 +386,7 @@ static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
                if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
                        memcpy(ent->dir->name, new_name, new_len & 0xFF);
                        ent->dir->name_len = new_len;
-                       ext2fs_dirhash(fs->super->s_def_hash_version,
-                                      ent->dir->name,
+                       ext2fs_dirhash(hash_alg, ent->dir->name,
                                       ent->dir->name_len & 0xFF,
                                       fs->super->s_hash_seed,
                                       &ent->hash, &ent->minor_hash);
@@ -386,7 +408,7 @@ static errcode_t copy_dir_entries(ext2_filsys fs,
        int                     i, rec_len, left;
        ext2_dirhash_t          prev_hash;
        int                     offset;
-       
+
        outdir->max = 0;
        retval = alloc_size_dir(fs, outdir,
                                (fd->dir_size / fs->blocksize) + 2);
@@ -451,7 +473,7 @@ static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
 
        if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
                filetype = EXT2_FT_DIR << 8;
-       
+
        memset(buf, 0, fs->blocksize);
        dir = (struct ext2_dir_entry *) buf;
        dir->inode = ino;
@@ -464,7 +486,7 @@ static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
        dir->name[1] = '.';
        dir->name_len = 2 | filetype;
        dir->rec_len = fs->blocksize - 12;
-       
+
        root = (struct ext2_dx_root_info *) (buf+24);
        root->reserved_zero = 0;
        root->hash_version = fs->super->s_def_hash_version;
@@ -489,7 +511,7 @@ static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
        dir = (struct ext2_dir_entry *) buf;
        dir->inode = 0;
        dir->rec_len = fs->blocksize;
-       
+
        limits = (struct ext2_dx_countlimit *) (buf+8);
        limits->limit = (fs->blocksize - 8) / sizeof(struct ext2_dx_entry);
        limits->count = 0;
@@ -513,7 +535,7 @@ static errcode_t calculate_tree(ext2_filsys fs,
        char                            * block_start;
        int                             i, c1, c2, nblks;
        int                             limit_offset, root_offset;
-       
+
        root_info = set_root_node(fs, outdir->buf, ino, parent);
        root_offset = limit_offset = ((char *) root_info - outdir->buf) +
                root_info->info_length;
@@ -542,7 +564,7 @@ static errcode_t calculate_tree(ext2_filsys fs,
                                return ENOSPC;
                        if (c2 == 0) {
                                if (limit)
-                                       limit->limit = limit->count = 
+                                       limit->limit = limit->count =
                ext2fs_cpu_to_le16(limit->limit);
                                root = (struct ext2_dx_entry *)
                                        (outdir->buf + root_offset);
@@ -590,7 +612,7 @@ static int write_dir_block(ext2_filsys fs,
                           blk_t        *block_nr,
                           e2_blkcnt_t blockcnt,
                           blk_t ref_block EXT2FS_ATTR((unused)),
-                          int ref_offset EXT2FS_ATTR((unused)), 
+                          int ref_offset EXT2FS_ATTR((unused)),
                           void *priv_data)
 {
        struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
@@ -648,7 +670,7 @@ static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
        else
                inode.i_flags |= EXT2_INDEX_FL;
        inode.i_size = outdir->num * fs->blocksize;
-       inode.i_blocks -= (fs->blocksize / 512) * wd.cleared;
+       ext2fs_iblk_sub_blocks(fs, &inode, wd.cleared);
        e2fsck_write_inode(ctx, ino, &inode, "rehash_dir");
 
        return 0;
@@ -662,7 +684,7 @@ errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
        char                    *dir_buf = 0;
        struct fill_dir_struct  fd;
        struct out_dir          outdir;
-       
+
        outdir.max = outdir.num = 0;
        outdir.buf = 0;
        outdir.hashes = 0;
@@ -708,7 +730,7 @@ errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
 resort:
        if (fd.compress)
                qsort(fd.harray+2, fd.num_array-2,
-                     sizeof(struct hash_entry), name_cmp);
+                     sizeof(struct hash_entry), ino_cmp);
        else
                qsort(fd.harray, fd.num_array,
                      sizeof(struct hash_entry), hash_cmp);
@@ -731,7 +753,7 @@ resort:
        retval = copy_dir_entries(fs, &fd, &outdir);
        if (retval)
                goto errout;
-       
+
        free(dir_buf); dir_buf = 0;
 
        if (!fd.compress) {
@@ -740,7 +762,7 @@ resort:
                if (retval)
                        goto errout;
        }
-       
+
        retval = write_directory(ctx, fs, &outdir, ino, fd.compress);
        if (retval)
                goto errout;
@@ -763,12 +785,13 @@ void e2fsck_rehash_directories(e2fsck_t ctx)
 #endif
        struct dir_info         *dir;
        ext2_u32_iterate        iter;
+       struct dir_info_iter *  dirinfo_iter = 0;
        ext2_ino_t              ino;
        errcode_t               retval;
-       int                     i, cur, max, all_dirs, dir_index, first = 1;
+       int                     cur, max, all_dirs, dir_index, first = 1;
 
 #ifdef RESOURCE_TRACK
-       init_resource_track(&rtrack);
+       init_resource_track(&rtrack, ctx->fs->io);
 #endif
 
        all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
@@ -777,16 +800,16 @@ void e2fsck_rehash_directories(e2fsck_t ctx)
                return;
 
        e2fsck_get_lost_and_found(ctx, 0);
-               
+
        clear_problem_context(&pctx);
 
        dir_index = ctx->fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX;
        cur = 0;
        if (all_dirs) {
-               i = 0;
+               dirinfo_iter = e2fsck_dir_info_iter_begin(ctx);
                max = e2fsck_get_num_dirinfo(ctx);
        } else {
-               retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash, 
+               retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
                                                       &iter);
                if (retval) {
                        pctx.errcode = retval;
@@ -797,7 +820,8 @@ void e2fsck_rehash_directories(e2fsck_t ctx)
        }
        while (1) {
                if (all_dirs) {
-                       if ((dir = e2fsck_dir_info_iter(ctx, &i)) == 0)
+                       if ((dir = e2fsck_dir_info_iter(ctx,
+                                                       dirinfo_iter)) == 0)
                                break;
                        ino = dir->ino;
                } else {
@@ -824,9 +848,11 @@ void e2fsck_rehash_directories(e2fsck_t ctx)
                               100.0 * (float) (++cur) / (float) max, ino);
        }
        end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
-       if (!all_dirs)
+       if (all_dirs)
+               e2fsck_dir_info_iter_end(ctx, dirinfo_iter);
+       else
                ext2fs_u32_list_iterate_end(iter);
-       
+
        if (ctx->dirs_to_hash)
                ext2fs_u32_list_free(ctx->dirs_to_hash);
        ctx->dirs_to_hash = 0;
@@ -834,7 +860,7 @@ void e2fsck_rehash_directories(e2fsck_t ctx)
 #ifdef RESOURCE_TRACK
        if (ctx->options & E2F_OPT_TIME2) {
                e2fsck_clear_progbar(ctx);
-               print_resource_track("Pass 3A", &rtrack);
+               print_resource_track("Pass 3A", &rtrack, ctx->fs->io);
        }
 #endif
 }