]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - e2fsck/rehash.c
Merge branch 'maint' into next
[thirdparty/e2fsprogs.git] / e2fsck / rehash.c
index e37e8714316ea3d8b4612c2863a84a2654319840..7c4ab0836482dd33ad0a669ad2e8107a800de2c4 100644 (file)
@@ -71,9 +71,12 @@ int e2fsck_dir_will_be_rehashed(e2fsck_t ctx, ext2_ino_t ino)
        return ext2fs_u32_list_test(ctx->dirs_to_hash, ino);
 }
 
+#undef REHASH_DEBUG
+
 struct fill_dir_struct {
        char *buf;
        struct ext2_inode *inode;
+       ext2_ino_t ino;
        errcode_t err;
        e2fsck_t ctx;
        struct hash_entry *harray;
@@ -120,8 +123,9 @@ static int fill_dir_block(ext2_filsys fs,
                fd->err = EXT2_ET_DIR_CORRUPTED;
                return BLOCK_ABORT;
        }
+
        dir = (fd->buf+offset);
-       if (HOLE_BLKADDR(*block_nr)) {
+       if (*block_nr == 0) {
                memset(dir, 0, fs->blocksize);
                dirent = (struct ext2_dir_entry *) dir;
                (void) ext2fs_set_rec_len(fs, fs->blocksize, dirent);
@@ -206,9 +210,8 @@ static EXT2_QSORT_TYPE name_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;
-       unsigned int he_a_len, he_b_len;
+       unsigned int he_a_len, he_b_len, min_len;
        int     ret;
-       int     min_len;
 
        he_a_len = ext2fs_dirent_name_len(he_a->dir);
        he_b_len = ext2fs_dirent_name_len(he_b->dir);
@@ -216,7 +219,7 @@ static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
        if (min_len > he_b_len)
                min_len = he_b_len;
 
-       ret = strncmp(he_a->dir->name, he_b->dir->name, min_len);
+       ret = memcmp(he_a->dir->name, he_b->dir->name, min_len);
        if (ret == 0) {
                if (he_a_len > he_b_len)
                        ret = 1;
@@ -305,7 +308,7 @@ static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
  */
 static void mutate_name(char *str, unsigned int *len)
 {
-       int     i;
+       int i;
        unsigned int l = *len;
 
        /*
@@ -316,7 +319,7 @@ static void mutate_name(char *str, unsigned int *len)
                if (!isdigit(str[i]))
                        break;
        }
-       if ((i == l-1) || (str[i] != '~')) {
+       if ((i == (int)l - 1) || (str[i] != '~')) {
                if (((l-1) & 3) < 2)
                        l += 2;
                else
@@ -383,7 +386,7 @@ static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
                if (!ent->dir->inode ||
                    (ext2fs_dirent_name_len(ent->dir) !=
                     ext2fs_dirent_name_len(prev->dir)) ||
-                   strncmp(ent->dir->name, prev->dir->name,
+                   memcmp(ent->dir->name, prev->dir->name,
                             ext2fs_dirent_name_len(ent->dir)))
                        continue;
                pctx.dirent = ent->dir;
@@ -400,8 +403,8 @@ static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
                for (j=0; j < fd->num_array; j++) {
                        if ((i==j) ||
                            (new_len !=
-                            ext2fs_dirent_name_len(fd->harray[j].dir)) ||
-                           strncmp(new_name, fd->harray[j].dir->name, new_len))
+                            (unsigned) ext2fs_dirent_name_len(fd->harray[j].dir)) ||
+                           memcmp(new_name, fd->harray[j].dir->name, new_len))
                                continue;
                        mutate_name(new_name, &new_len);
 
@@ -412,7 +415,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);
                        ext2fs_dirent_set_name_len(ent->dir, new_len);
-                       ext2fs_dirhash(hash_alg, ent->dir->name, new_len,
+                       ext2fs_dirhash(hash_alg, new_name, new_len,
                                       fs->super->s_hash_seed,
                                       &ent->hash, &ent->minor_hash);
                        fixed++;
@@ -446,8 +449,7 @@ static errcode_t copy_dir_entries(e2fsck_t ctx,
                        ctx->htree_slack_percentage = 20;
        }
 
-       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);
 
        outdir->max = 0;
@@ -542,7 +544,7 @@ static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
        int                             filetype = 0;
        int                             csum_size = 0;
 
-       if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
+       if (ext2fs_has_feature_filetype(fs->super))
                filetype = EXT2_FT_DIR;
 
        memset(buf, 0, fs->blocksize);
@@ -567,8 +569,7 @@ static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
        root->indirect_levels = 0;
        root->unused_flags = 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_dx_tail);
 
        limits = (struct ext2_dx_countlimit *) (buf+32);
@@ -591,8 +592,7 @@ static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
        dir->inode = 0;
        (void) ext2fs_set_rec_len(fs, fs->blocksize, dir);
 
-       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_dx_tail);
 
        limits = (struct ext2_dx_countlimit *) (buf+8);
@@ -603,6 +603,43 @@ static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
        return (struct ext2_dx_entry *) limits;
 }
 
+static int alloc_blocks(ext2_filsys fs,
+                       struct ext2_dx_countlimit **limit,
+                       struct ext2_dx_entry **prev_ent,
+                       struct ext2_dx_entry **next_ent,
+                       int *prev_offset, int *next_offset,
+                       struct out_dir *outdir, int i,
+                       int *prev_count, int *next_count)
+{
+       errcode_t       retval;
+       char            *block_start;
+
+       if (*limit)
+               (*limit)->limit = (*limit)->count =
+                       ext2fs_cpu_to_le16((*limit)->limit);
+       *prev_ent = (struct ext2_dx_entry *) (outdir->buf + *prev_offset);
+       (*prev_ent)->block = ext2fs_cpu_to_le32(outdir->num);
+
+       if (i != 1)
+               (*prev_ent)->hash =
+                       ext2fs_cpu_to_le32(outdir->hashes[i]);
+
+       retval = get_next_block(fs, outdir, &block_start);
+       if (retval)
+               return retval;
+
+       *next_ent = set_int_node(fs, block_start);
+       *limit = (struct ext2_dx_countlimit *)(*next_ent);
+       if (next_offset)
+               *next_offset = ((char *) *next_ent - outdir->buf);
+
+       *next_count = (*limit)->limit;
+       (*prev_offset) += sizeof(struct ext2_dx_entry);
+       (*prev_count)--;
+
+       return 0;
+}
+
 /*
  * This function takes the leaf nodes which have been written in
  * outdir, and populates the root node and any necessary interior nodes.
@@ -612,13 +649,12 @@ static errcode_t calculate_tree(ext2_filsys fs,
                                ext2_ino_t ino,
                                ext2_ino_t parent)
 {
-       struct ext2_dx_root_info        *root_info;
-       struct ext2_dx_entry            *root, *dx_ent = 0;
-       struct ext2_dx_countlimit       *root_limit, *limit;
+       struct ext2_dx_root_info        *root_info;
+       struct ext2_dx_entry            *root, *int_ent, *dx_ent = 0;
+       struct ext2_dx_countlimit       *root_limit, *int_limit, *limit;
        errcode_t                       retval;
-       char                            * block_start;
-       int                             i, c1, c2, nblks;
-       int                             limit_offset, root_offset;
+       int                             i, c1, c2, c3, nblks;
+       int                             limit_offset, int_offset, root_offset;
 
        root_info = set_root_node(fs, outdir->buf, ino, parent);
        root_offset = limit_offset = ((char *) root_info - outdir->buf) +
@@ -628,7 +664,7 @@ static errcode_t calculate_tree(ext2_filsys fs,
        nblks = outdir->num;
 
        /* Write out the pointer blocks */
-       if (nblks-1 <= c1) {
+       if (nblks - 1 <= c1) {
                /* Just write out the root block, and we're done */
                root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
                for (i=1; i < nblks; i++) {
@@ -639,31 +675,20 @@ static errcode_t calculate_tree(ext2_filsys fs,
                        root++;
                        c1--;
                }
-       } else {
+       } else if (nblks - 1 <= ext2fs_htree_intnode_maxrecs(fs, c1)) {
                c2 = 0;
-               limit = 0;
+               limit = NULL;
                root_info->indirect_levels = 1;
                for (i=1; i < nblks; i++) {
-                       if (c1 == 0)
+                       if (c2 == 0 && c1 == 0)
                                return ENOSPC;
                        if (c2 == 0) {
-                               if (limit)
-                                       limit->limit = limit->count =
-               ext2fs_cpu_to_le16(limit->limit);
-                               root = (struct ext2_dx_entry *)
-                                       (outdir->buf + root_offset);
-                               root->block = ext2fs_cpu_to_le32(outdir->num);
-                               if (i != 1)
-                                       root->hash =
-                       ext2fs_cpu_to_le32(outdir->hashes[i]);
-                               if ((retval =  get_next_block(fs, outdir,
-                                                             &block_start)))
+                               retval = alloc_blocks(fs, &limit, &root,
+                                                     &dx_ent, &root_offset,
+                                                     NULL, outdir, i, &c1,
+                                                     &c2);
+                               if (retval)
                                        return retval;
-                               dx_ent = set_int_node(fs, block_start);
-                               limit = (struct ext2_dx_countlimit *) dx_ent;
-                               c2 = limit->limit;
-                               root_offset += sizeof(struct ext2_dx_entry);
-                               c1--;
                        }
                        dx_ent->block = ext2fs_cpu_to_le32(i);
                        if (c2 != limit->limit)
@@ -674,6 +699,45 @@ static errcode_t calculate_tree(ext2_filsys fs,
                }
                limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
                limit->limit = ext2fs_cpu_to_le16(limit->limit);
+       } else {
+               c2 = 0;
+               c3 = 0;
+               limit = NULL;
+               int_limit = 0;
+               root_info->indirect_levels = 2;
+               for (i = 1; i < nblks; i++) {
+                       if (c3 == 0 && c2 == 0 && c1 == 0)
+                               return ENOSPC;
+                       if (c3 == 0 && c2 == 0) {
+                               retval = alloc_blocks(fs, &int_limit, &root,
+                                                     &int_ent, &root_offset,
+                                                     &int_offset, outdir, i,
+                                                     &c1, &c2);
+                               if (retval)
+                                       return retval;
+                       }
+                       if (c3 == 0) {
+                               retval = alloc_blocks(fs, &limit, &int_ent,
+                                                     &dx_ent, &int_offset,
+                                                     NULL, outdir, i, &c2,
+                                                     &c3);
+                               if (retval)
+                                       return retval;
+
+                       }
+                       dx_ent->block = ext2fs_cpu_to_le32(i);
+                       if (c3 != limit->limit)
+                               dx_ent->hash =
+                                       ext2fs_cpu_to_le32(outdir->hashes[i]);
+                       dx_ent++;
+                       c3--;
+               }
+               int_limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
+               int_limit->limit = ext2fs_cpu_to_le16(limit->limit);
+
+               limit->count = ext2fs_cpu_to_le16(limit->limit - c3);
+               limit->limit = ext2fs_cpu_to_le16(limit->limit);
+
        }
        root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
        root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
@@ -685,8 +749,8 @@ static errcode_t calculate_tree(ext2_filsys fs,
 struct write_dir_struct {
        struct out_dir *outdir;
        errcode_t       err;
+       ext2_ino_t      ino;
        e2fsck_t        ctx;
-       blk64_t         cleared;
        ext2_ino_t      dir;
 };
 
@@ -701,13 +765,17 @@ static int write_dir_block(ext2_filsys fs,
                           void *priv_data)
 {
        struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
-       blk64_t blk;
        char    *dir, *buf = 0;
 
-       if (*block_nr == 0)
-               return 0;
-       if (blockcnt < 0)
+#ifdef REHASH_DEBUG
+       printf("%u: write_dir_block %lld:%lld", wd->ino, blockcnt, *block_nr);
+#endif
+       if ((*block_nr == 0) || (blockcnt < 0)) {
+#ifdef REHASH_DEBUG
+               printf(" - skip\n");
+#endif
                return 0;
+       }
        if (blockcnt < wd->outdir->num)
                dir = wd->outdir->buf + (blockcnt * fs->blocksize);
        else if (wd->ctx->lost_and_found == wd->dir) {
@@ -718,26 +786,20 @@ static int write_dir_block(ext2_filsys fs,
                dir = buf;
                wd->outdir->num++;
        } else {
-               /* We don't need this block, so release it */
-               e2fsck_read_bitmaps(wd->ctx);
-               blk = *block_nr;
-               /*
-                * In theory, we only release blocks from the end of the
-                * directory file, so it's fine to clobber a whole cluster at
-                * once.
-                */
-               if (blk % EXT2FS_CLUSTER_RATIO(fs) == 0) {
-                       ext2fs_block_alloc_stats2(fs, blk, -1);
-                       wd->cleared++;
-               }
-               *block_nr = 0;
-               return BLOCK_CHANGED;
+               /* Don't free blocks at the end of the directory, they
+                * will be truncated by the caller. */
+#ifdef REHASH_DEBUG
+               printf(" - not freed\n");
+#endif
+               return 0;
        }
-
        wd->err = ext2fs_write_dir_block4(fs, *block_nr, dir, 0, wd->dir);
        if (buf)
                ext2fs_free_mem(&buf);
 
+#ifdef REHASH_DEBUG
+       printf(" - write (%d)\n", wd->err);
+#endif
        if (wd->err)
                return BLOCK_ABORT;
        return 0;
@@ -745,11 +807,11 @@ static int write_dir_block(ext2_filsys fs,
 
 static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
                                 struct out_dir *outdir,
-                                ext2_ino_t ino, int compress)
+                                ext2_ino_t ino, struct ext2_inode *inode,
+                                int compress)
 {
        struct write_dir_struct wd;
        errcode_t       retval;
-       struct ext2_inode       inode;
 
        retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
        if (retval)
@@ -757,71 +819,68 @@ static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
 
        wd.outdir = outdir;
        wd.err = 0;
+       wd.ino = ino;
        wd.ctx = ctx;
-       wd.cleared = 0;
        wd.dir = ino;
 
-       retval = ext2fs_block_iterate3(fs, ino, 0, 0,
+       retval = ext2fs_block_iterate3(fs, ino, 0, NULL,
                                       write_dir_block, &wd);
        if (retval)
                return retval;
        if (wd.err)
                return wd.err;
 
-       e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
+       e2fsck_read_inode(ctx, ino, inode, "rehash_dir");
        if (compress)
-               inode.i_flags &= ~EXT2_INDEX_FL;
+               inode->i_flags &= ~EXT2_INDEX_FL;
        else
-               inode.i_flags |= EXT2_INDEX_FL;
-       retval = ext2fs_inode_size_set(fs, &inode,
-                                      outdir->num * fs->blocksize);
+               inode->i_flags |= EXT2_INDEX_FL;
+#ifdef REHASH_DEBUG
+       printf("%u: set inode size to %u blocks = %u bytes\n",
+              ino, outdir->num, outdir->num * fs->blocksize);
+#endif
+       retval = ext2fs_inode_size_set(fs, inode, (ext2_off64_t)outdir->num *
+                                                  fs->blocksize);
        if (retval)
                return retval;
-       ext2fs_iblk_sub_blocks(fs, &inode, wd.cleared);
-       e2fsck_write_inode(ctx, ino, &inode, "rehash_dir");
 
-       return 0;
+       /* ext2fs_punch() calls ext2fs_write_inode() which writes the size */
+       return ext2fs_punch(fs, ino, inode, NULL, outdir->num, ~0ULL);
 }
 
-errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
+errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino,
+                           struct problem_context *pctx)
 {
        ext2_filsys             fs = ctx->fs;
        errcode_t               retval;
        struct ext2_inode       inode;
        char                    *dir_buf = 0;
-       struct fill_dir_struct  fd;
-       struct out_dir          outdir;
+       struct fill_dir_struct  fd = { NULL, NULL, 0, 0, 0, NULL,
+                                      0, 0, 0, 0, 0, 0 };
+       struct out_dir          outdir = { 0, 0, 0, 0 };
 
-       outdir.max = outdir.num = 0;
-       outdir.buf = 0;
-       outdir.hashes = 0;
        e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
 
-       if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
-                                     EXT4_FEATURE_INCOMPAT_INLINE_DATA) &&
+       if (ext2fs_has_feature_inline_data(fs->super) &&
           (inode.i_flags & EXT4_INLINE_DATA_FL))
                return 0;
 
        retval = ENOMEM;
-       fd.harray = 0;
        dir_buf = malloc(inode.i_size);
        if (!dir_buf)
                goto errout;
 
        fd.max_array = inode.i_size / 32;
-       fd.num_array = 0;
        fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
        if (!fd.harray)
                goto errout;
 
+       fd.ino = ino;
        fd.ctx = ctx;
        fd.buf = dir_buf;
        fd.inode = &inode;
-       fd.err = 0;
-       fd.dir_size = 0;
-       fd.compress = 0;
        fd.dir = ino;
-       if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
+       if (!ext2fs_has_feature_dir_index(fs->super) ||
            (inode.i_size / fs->blocksize) < 2)
                fd.compress = 1;
        fd.parent = 0;
@@ -893,10 +952,14 @@ resort:
                        goto errout;
        }
 
-       retval = write_directory(ctx, fs, &outdir, ino, fd.compress);
+       retval = write_directory(ctx, fs, &outdir, ino, &inode, fd.compress);
        if (retval)
                goto errout;
 
+       if (ctx->options & E2F_OPT_CONVERT_BMAP)
+               retval = e2fsck_rebuild_extents_later(ctx, ino);
+       else
+               retval = e2fsck_check_rebuild_extents(ctx, ino, &inode, pctx);
 errout:
        free(dir_buf);
        free(fd.harray);
@@ -961,7 +1024,7 @@ void e2fsck_rehash_directories(e2fsck_t ctx)
 #if 0
                fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
 #endif
-               pctx.errcode = e2fsck_rehash_dir(ctx, ino);
+               pctx.errcode = e2fsck_rehash_dir(ctx, ino, &pctx);
                if (pctx.errcode) {
                        end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
                        fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);