]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - e2fsck/pass1b.c
e2fsck: use errcode_t to suppress some -Wconversion warnings
[thirdparty/e2fsprogs.git] / e2fsck / pass1b.c
index 9bef36872a782d8cd9c4a9b8cf76ef590a85de9f..a3b880cf40b7a29292fb720465ae5446774a99b3 100644 (file)
@@ -27,6 +27,7 @@
  *
  */
 
+#include "config.h"
 #include <time.h>
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
@@ -53,9 +54,9 @@ typedef long intptr_t;
 /* Define an extension to the ext2 library's block count information */
 #define BLOCK_COUNT_EXTATTR    (-5)
 
-struct block_el {
-       blk64_t block;
-       struct block_el *next;
+struct cluster_el {
+       blk64_t cluster;
+       struct cluster_el *next;
 };
 
 struct inode_el {
@@ -63,7 +64,7 @@ struct inode_el {
        struct inode_el *next;
 };
 
-struct dup_block {
+struct dup_cluster {
        int             num_bad;
        struct inode_el *inode_list;
 };
@@ -79,7 +80,7 @@ struct dup_inode {
        ext2_ino_t              dir;
        int                     num_dupblocks;
        struct ext2_inode       inode;
-       struct block_el         *block_list;
+       struct cluster_el       *cluster_list;
 };
 
 static int process_pass1b_block(ext2_filsys fs, blk64_t        *blocknr,
@@ -87,9 +88,10 @@ static int process_pass1b_block(ext2_filsys fs, blk64_t      *blocknr,
                                int ref_offset, void *priv_data);
 static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
                        struct dup_inode *dp, char *block_buf);
-static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
-                     struct dup_inode *dp, char* block_buf);
-static int check_if_fs_block(e2fsck_t ctx, blk64_t test_blk);
+static errcode_t clone_file(e2fsck_t ctx, ext2_ino_t ino,
+                           struct dup_inode *dp, char* block_buf);
+static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block);
+static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster);
 
 static void pass1b(e2fsck_t ctx, char *block_buf);
 static void pass1c(e2fsck_t ctx, char *block_buf);
@@ -98,7 +100,7 @@ static void pass1d(e2fsck_t ctx, char *block_buf);
 static int dup_inode_count = 0;
 static int dup_inode_founddir = 0;
 
-static dict_t blk_dict, ino_dict;
+static dict_t clstr_dict, ino_dict;
 
 static ext2fs_inode_bitmap inode_dup_map;
 
@@ -115,24 +117,24 @@ static int dict_int_cmp(const void *a, const void *b)
 /*
  * Add a duplicate block record
  */
-static void add_dupe(e2fsck_t ctx, ext2_ino_t ino, blk64_t blk,
+static void add_dupe(e2fsck_t ctx, ext2_ino_t ino, blk64_t cluster,
                     struct ext2_inode *inode)
 {
        dnode_t *n;
-       struct dup_block        *db;
+       struct dup_cluster      *db;
        struct dup_inode        *di;
-       struct block_el         *blk_el;
+       struct cluster_el       *cluster_el;
        struct inode_el         *ino_el;
 
-       n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(blk));
+       n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(cluster));
        if (n)
-               db = (struct dup_block *) dnode_get(n);
+               db = (struct dup_cluster *) dnode_get(n);
        else {
-               db = (struct dup_block *) e2fsck_allocate_memory(ctx,
-                        sizeof(struct dup_block), "duplicate block header");
+               db = (struct dup_cluster *) e2fsck_allocate_memory(ctx,
+                       sizeof(struct dup_cluster), "duplicate cluster header");
                db->num_bad = 0;
                db->inode_list = 0;
-               dict_alloc_insert(&blk_dict, INT_TO_VOIDPTR(blk), db);
+               dict_alloc_insert(&clstr_dict, INT_TO_VOIDPTR(cluster), db);
        }
        ino_el = (struct inode_el *) e2fsck_allocate_memory(ctx,
                         sizeof(struct inode_el), "inode element");
@@ -154,15 +156,15 @@ static void add_dupe(e2fsck_t ctx, ext2_ino_t ino, blk64_t blk,
                        di->dir = 0;
 
                di->num_dupblocks = 0;
-               di->block_list = 0;
+               di->cluster_list = 0;
                di->inode = *inode;
                dict_alloc_insert(&ino_dict, INT_TO_VOIDPTR(ino), di);
        }
-       blk_el = (struct block_el *) e2fsck_allocate_memory(ctx,
-                        sizeof(struct block_el), "block element");
-       blk_el->block = blk;
-       blk_el->next = di->block_list;
-       di->block_list = blk_el;
+       cluster_el = (struct cluster_el *) e2fsck_allocate_memory(ctx,
+                        sizeof(struct cluster_el), "cluster element");
+       cluster_el->cluster = cluster;
+       cluster_el->next = di->cluster_list;
+       di->cluster_list = cluster_el;
        di->num_dupblocks++;
 }
 
@@ -173,10 +175,10 @@ static void inode_dnode_free(dnode_t *node,
                             void *context EXT2FS_ATTR((unused)))
 {
        struct dup_inode        *di;
-       struct block_el         *p, *next;
+       struct cluster_el               *p, *next;
 
        di = (struct dup_inode *) dnode_get(node);
-       for (p = di->block_list; p; p = next) {
+       for (p = di->cluster_list; p; p = next) {
                next = p->next;
                free(p);
        }
@@ -185,20 +187,20 @@ static void inode_dnode_free(dnode_t *node,
 }
 
 /*
- * Free a duplicate block record
+ * Free a duplicate cluster record
  */
-static void block_dnode_free(dnode_t *node,
-                            void *context EXT2FS_ATTR((unused)))
+static void cluster_dnode_free(dnode_t *node,
+                              void *context EXT2FS_ATTR((unused)))
 {
-       struct dup_block        *db;
+       struct dup_cluster      *dc;
        struct inode_el         *p, *next;
 
-       db = (struct dup_block *) dnode_get(node);
-       for (p = db->inode_list; p; p = next) {
+       dc = (struct dup_cluster *) dnode_get(node);
+       for (p = dc->inode_list; p; p = next) {
                next = p->next;
                free(p);
        }
-       free(db);
+       free(dc);
        free(node);
 }
 
@@ -216,8 +218,10 @@ void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
 
        clear_problem_context(&pctx);
 
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
-                     _("multiply claimed inode map"), &inode_dup_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
+                       _("multiply claimed inode map"),
+                       EXT2FS_BMAP64_RBTREE, "inode_dup_map",
+                       &inode_dup_map);
        if (pctx.errcode) {
                fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
@@ -225,9 +229,9 @@ void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
        }
 
        dict_init(&ino_dict, DICTCOUNT_T_MAX, dict_int_cmp);
-       dict_init(&blk_dict, DICTCOUNT_T_MAX, dict_int_cmp);
+       dict_init(&clstr_dict, DICTCOUNT_T_MAX, dict_int_cmp);
        dict_set_allocator(&ino_dict, NULL, inode_dnode_free, NULL);
-       dict_set_allocator(&blk_dict, NULL, block_dnode_free, NULL);
+       dict_set_allocator(&clstr_dict, NULL, cluster_dnode_free, NULL);
 
        init_resource_track(&rtrack, ctx->fs->io);
        pass1b(ctx, block_buf);
@@ -246,7 +250,7 @@ void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
         * don't need anymore.
         */
        dict_free_nodes(&ino_dict);
-       dict_free_nodes(&blk_dict);
+       dict_free_nodes(&clstr_dict);
        ext2fs_free_inode_bitmap(inode_dup_map);
 }
 
@@ -257,6 +261,7 @@ struct process_block_struct {
        e2fsck_t        ctx;
        ext2_ino_t      ino;
        int             dup_blocks;
+       blk64_t         cur_cluster;
        struct ext2_inode *inode;
        struct problem_context *pctx;
 };
@@ -264,7 +269,7 @@ struct process_block_struct {
 static void pass1b(e2fsck_t ctx, char *block_buf)
 {
        ext2_filsys fs = ctx->fs;
-       ext2_ino_t ino;
+       ext2_ino_t ino = 0;
        struct ext2_inode inode;
        ext2_inode_scan scan;
        struct process_block_struct pb;
@@ -286,6 +291,10 @@ static void pass1b(e2fsck_t ctx, char *block_buf)
        pb.pctx = &pctx;
        pctx.str = "pass1b";
        while (1) {
+               if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
+                       if (e2fsck_mmp_update(fs))
+                               fatal_error(ctx, 0);
+               }
                pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
                if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
                        continue;
@@ -304,19 +313,20 @@ static void pass1b(e2fsck_t ctx, char *block_buf)
                pb.ino = ino;
                pb.dup_blocks = 0;
                pb.inode = &inode;
+               pb.cur_cluster = ~0;
 
-               if (ext2fs_inode_has_valid_blocks(&inode) ||
+               if (ext2fs_inode_has_valid_blocks2(fs, &inode) ||
                    (ino == EXT2_BAD_INO))
                        pctx.errcode = ext2fs_block_iterate3(fs, ino,
                                             BLOCK_FLAG_READ_ONLY, block_buf,
                                             process_pass1b_block, &pb);
                /* If the feature is not set, attrs will be cleared later anyway */
                if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
-                   ext2fs_file_acl_block(&inode)) {
-                       blk64_t blk = ext2fs_file_acl_block(&inode);
+                   ext2fs_file_acl_block(fs, &inode)) {
+                       blk64_t blk = ext2fs_file_acl_block(fs, &inode);
                        process_pass1b_block(fs, &blk,
                                             BLOCK_COUNT_EXTATTR, 0, 0, &pb);
-                       ext2fs_file_acl_block_set(&inode, blk);
+                       ext2fs_file_acl_block_set(fs, &inode, blk);
                }
                if (pb.dup_blocks) {
                        end_problem_latch(ctx, PR_LATCH_DBLOCK);
@@ -333,21 +343,23 @@ static void pass1b(e2fsck_t ctx, char *block_buf)
 
 static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
                                blk64_t *block_nr,
-                               e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
+                               e2_blkcnt_t blockcnt,
                                blk64_t ref_blk EXT2FS_ATTR((unused)),
                                int ref_offset EXT2FS_ATTR((unused)),
                                void *priv_data)
 {
        struct process_block_struct *p;
        e2fsck_t ctx;
+       blk64_t lc;
 
        if (HOLE_BLKADDR(*block_nr))
                return 0;
        p = (struct process_block_struct *) priv_data;
        ctx = p->ctx;
+       lc = EXT2FS_B2C(fs, blockcnt);
 
        if (!ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr))
-               return 0;
+               goto finish;
 
        /* OK, this is a duplicate block */
        if (p->ino != EXT2_BAD_INO) {
@@ -357,8 +369,11 @@ static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
        p->dup_blocks++;
        ext2fs_mark_inode_bitmap2(inode_dup_map, p->ino);
 
-       add_dupe(ctx, p->ino, *block_nr, p->inode);
+       if (lc != p->cur_cluster)
+               add_dupe(ctx, p->ino, EXT2FS_B2C(fs, *block_nr), p->inode);
 
+finish:
+       p->cur_cluster = lc;
        return 0;
 }
 
@@ -433,7 +448,7 @@ static void pass1d(e2fsck_t ctx, char *block_buf)
 {
        ext2_filsys fs = ctx->fs;
        struct dup_inode        *p, *t;
-       struct dup_block        *q;
+       struct dup_cluster      *q;
        ext2_ino_t              *shared, ino;
        int     shared_len;
        int     i;
@@ -441,7 +456,7 @@ static void pass1d(e2fsck_t ctx, char *block_buf)
        int     meta_data = 0;
        struct problem_context pctx;
        dnode_t *n, *m;
-       struct block_el *s;
+       struct cluster_el       *s;
        struct inode_el *r;
 
        clear_problem_context(&pctx);
@@ -469,14 +484,15 @@ static void pass1d(e2fsck_t ctx, char *block_buf)
                 * belonging to this inode, and then search each block
                 * get the list of inodes, and merge them together.
                 */
-               for (s = p->block_list; s; s = s->next) {
-                       m = dict_lookup(&blk_dict, INT_TO_VOIDPTR(s->block));
+               for (s = p->cluster_list; s; s = s->next) {
+                       m = dict_lookup(&clstr_dict,
+                                       INT_TO_VOIDPTR(s->cluster));
                        if (!m)
                                continue; /* Should never happen... */
-                       q = (struct dup_block *) dnode_get(m);
+                       q = (struct dup_cluster *) dnode_get(m);
                        if (q->num_bad > 1)
                                file_ok = 0;
-                       if (check_if_fs_block(ctx, s->block)) {
+                       if (check_if_fs_cluster(ctx, s->cluster)) {
                                file_ok = 0;
                                meta_data = 1;
                        }
@@ -550,25 +566,30 @@ static void pass1d(e2fsck_t ctx, char *block_buf)
  * Drop the refcount on the dup_block structure, and clear the entry
  * in the block_dup_map if appropriate.
  */
-static void decrement_badcount(e2fsck_t ctx, blk64_t block, struct dup_block *p)
+static void decrement_badcount(e2fsck_t ctx, blk64_t block,
+                              struct dup_cluster *p)
 {
        p->num_bad--;
        if (p->num_bad <= 0 ||
-           (p->num_bad == 1 && !check_if_fs_block(ctx, block)))
+           (p->num_bad == 1 && !check_if_fs_block(ctx, block))) {
+               if (check_if_fs_cluster(ctx, EXT2FS_B2C(ctx->fs, block)))
+                       return;
                ext2fs_unmark_block_bitmap2(ctx->block_dup_map, block);
+       }
 }
 
 static int delete_file_block(ext2_filsys fs,
                             blk64_t    *block_nr,
-                            e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
+                            e2_blkcnt_t blockcnt,
                             blk64_t ref_block EXT2FS_ATTR((unused)),
                             int ref_offset EXT2FS_ATTR((unused)),
                             void *priv_data)
 {
        struct process_block_struct *pb;
-       struct dup_block *p;
+       struct dup_cluster *p;
        dnode_t *n;
        e2fsck_t ctx;
+       blk64_t c, lc;
 
        pb = (struct process_block_struct *) priv_data;
        ctx = pb->ctx;
@@ -576,11 +597,14 @@ static int delete_file_block(ext2_filsys fs,
        if (HOLE_BLKADDR(*block_nr))
                return 0;
 
+       c = EXT2FS_B2C(fs, *block_nr);
+       lc = EXT2FS_B2C(fs, blockcnt);
        if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
-               n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
+               n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(c));
                if (n) {
-                       p = (struct dup_block *) dnode_get(n);
-                       decrement_badcount(ctx, *block_nr, p);
+                       p = (struct dup_cluster *) dnode_get(n);
+                       if (lc != pb->cur_cluster)
+                               decrement_badcount(ctx, *block_nr, p);
                } else
                        com_err("delete_file_block", 0,
                            _("internal error: can't find dup_blk for %llu\n"),
@@ -588,7 +612,9 @@ static int delete_file_block(ext2_filsys fs,
        } else {
                ext2fs_unmark_block_bitmap2(ctx->block_found_map, *block_nr);
                ext2fs_block_alloc_stats2(fs, *block_nr, -1);
+               pb->dup_blocks++;
        }
+       pb->cur_cluster = lc;
 
        return 0;
 }
@@ -598,41 +624,45 @@ static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
 {
        ext2_filsys fs = ctx->fs;
        struct process_block_struct pb;
-       struct ext2_inode       inode;
        struct problem_context  pctx;
        unsigned int            count;
 
        clear_problem_context(&pctx);
        pctx.ino = pb.ino = ino;
-       pb.dup_blocks = dp->num_dupblocks;
+       pb.dup_blocks = 0;
        pb.ctx = ctx;
        pctx.str = "delete_file";
+       pb.cur_cluster = ~0;
 
-       e2fsck_read_inode(ctx, ino, &inode, "delete_file");
-       if (ext2fs_inode_has_valid_blocks(&inode))
-               pctx.errcode = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_READ_ONLY,
-                                                    block_buf, delete_file_block, &pb);
+       if (ext2fs_inode_has_valid_blocks2(fs, &dp->inode))
+               pctx.errcode = ext2fs_block_iterate3(fs, ino,
+                                                    BLOCK_FLAG_READ_ONLY,
+                                                    block_buf,
+                                                    delete_file_block, &pb);
        if (pctx.errcode)
                fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
        if (ctx->inode_bad_map)
                ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
-       ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
+       ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(dp->inode.i_mode));
+       quota_data_sub(ctx->qctx, &dp->inode, ino,
+                      pb.dup_blocks * fs->blocksize);
+       quota_data_inodes(ctx->qctx, &dp->inode, ino, -1);
 
        /* Inode may have changed by block_iterate, so reread it */
-       e2fsck_read_inode(ctx, ino, &inode, "delete_file");
-       e2fsck_clear_inode(ctx, ino, &inode, 0, "delete_file");
-       if (ext2fs_file_acl_block(&inode) &&
+       e2fsck_read_inode(ctx, ino, &dp->inode, "delete_file");
+       e2fsck_clear_inode(ctx, ino, &dp->inode, 0, "delete_file");
+       if (ext2fs_file_acl_block(fs, &dp->inode) &&
            (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
                count = 1;
                pctx.errcode = ext2fs_adjust_ea_refcount2(fs,
-                                                  ext2fs_file_acl_block(&inode),
+                                       ext2fs_file_acl_block(fs, &dp->inode),
                                                   block_buf, -1, &count);
                if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
                        pctx.errcode = 0;
                        count = 1;
                }
                if (pctx.errcode) {
-                       pctx.blk = ext2fs_file_acl_block(&inode);
+                       pctx.blk = ext2fs_file_acl_block(fs, &dp->inode);
                        fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
                }
                /*
@@ -643,17 +673,20 @@ static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
                 */
                if ((count == 0) ||
                    ext2fs_test_block_bitmap2(ctx->block_dup_map,
-                                             ext2fs_file_acl_block(&inode))) {
-                       blk64_t blk = ext2fs_file_acl_block(&inode);
+                                       ext2fs_file_acl_block(fs, &dp->inode))) {
+                       blk64_t blk = ext2fs_file_acl_block(fs, &dp->inode);
                        delete_file_block(fs, &blk,
                                          BLOCK_COUNT_EXTATTR, 0, 0, &pb);
-                       ext2fs_file_acl_block_set(&inode, blk);
+                       ext2fs_file_acl_block_set(fs, &dp->inode, blk);
+                       quota_data_sub(ctx->qctx, &dp->inode, ino, fs->blocksize);
                }
        }
 }
 
 struct clone_struct {
        errcode_t       errcode;
+       blk64_t         dup_cluster;
+       blk64_t         alloc_block;
        ext2_ino_t      dir;
        char    *buf;
        e2fsck_t ctx;
@@ -666,68 +699,88 @@ static int clone_file_block(ext2_filsys fs,
                            int ref_offset EXT2FS_ATTR((unused)),
                            void *priv_data)
 {
-       struct dup_block *p;
+       struct dup_cluster *p;
        blk64_t new_block;
        errcode_t       retval;
        struct clone_struct *cs = (struct clone_struct *) priv_data;
        dnode_t *n;
        e2fsck_t ctx;
+       blk64_t c;
+       int is_meta = 0;
 
        ctx = cs->ctx;
 
        if (HOLE_BLKADDR(*block_nr))
                return 0;
 
+       c = EXT2FS_B2C(fs, blockcnt);
+       if (check_if_fs_cluster(ctx, EXT2FS_B2C(fs, *block_nr)))
+               is_meta = 1;
+
+       if (c == cs->dup_cluster && cs->alloc_block) {
+               new_block = cs->alloc_block;
+               goto got_block;
+       }
+
        if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
-               n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
-               if (n) {
-                       p = (struct dup_block *) dnode_get(n);
-                       retval = ext2fs_new_block2(fs, 0, ctx->block_found_map,
-                                                 &new_block);
+               n = dict_lookup(&clstr_dict,
+                               INT_TO_VOIDPTR(EXT2FS_B2C(fs, *block_nr)));
+               if (!n) {
+                       com_err("clone_file_block", 0,
+                           _("internal error: can't find dup_blk for %llu\n"),
+                               *block_nr);
+                       return 0;
+               }
+
+               p = (struct dup_cluster *) dnode_get(n);
+               if (!is_meta)
+                       decrement_badcount(ctx, *block_nr, p);
+
+               cs->dup_cluster = c;
+
+               retval = ext2fs_new_block2(fs, 0, ctx->block_found_map,
+                                          &new_block);
+               if (retval) {
+                       cs->errcode = retval;
+                       return BLOCK_ABORT;
+               }
+               cs->alloc_block = new_block;
+
+       got_block:
+               new_block &= ~EXT2FS_CLUSTER_MASK(fs);
+               new_block += EXT2FS_CLUSTER_MASK(fs) & blockcnt;
+               if (cs->dir && (blockcnt >= 0)) {
+                       retval = ext2fs_set_dir_block2(fs->dblist,
+                                       cs->dir, new_block, blockcnt);
                        if (retval) {
                                cs->errcode = retval;
                                return BLOCK_ABORT;
                        }
-                       if (cs->dir && (blockcnt >= 0)) {
-                               retval = ext2fs_set_dir_block2(fs->dblist,
-                                     cs->dir, new_block, blockcnt);
-                               if (retval) {
-                                       cs->errcode = retval;
-                                       return BLOCK_ABORT;
-                               }
-                       }
+               }
 #if 0
-                       printf("Cloning block %u to %u\n", *block_nr,
-                              new_block);
+               printf("Cloning block #%lld from %llu to %llu\n",
+                      blockcnt, *block_nr, new_block);
 #endif
-                       retval = io_channel_read_blk64(fs->io, *block_nr, 1,
-                                                    cs->buf);
-                       if (retval) {
-                               cs->errcode = retval;
-                               return BLOCK_ABORT;
-                       }
-                       retval = io_channel_write_blk64(fs->io, new_block, 1,
-                                                     cs->buf);
-                       if (retval) {
-                               cs->errcode = retval;
-                               return BLOCK_ABORT;
-                       }
-                       decrement_badcount(ctx, *block_nr, p);
-                       *block_nr = new_block;
-                       ext2fs_mark_block_bitmap2(ctx->block_found_map,
-                                                new_block);
-                       ext2fs_mark_block_bitmap2(fs->block_map, new_block);
-                       return BLOCK_CHANGED;
-               } else
-                       com_err("clone_file_block", 0,
-                           _("internal error: can't find dup_blk for %llu\n"),
-                               *block_nr);
+               retval = io_channel_read_blk64(fs->io, *block_nr, 1, cs->buf);
+               if (retval) {
+                       cs->errcode = retval;
+                       return BLOCK_ABORT;
+               }
+               retval = io_channel_write_blk64(fs->io, new_block, 1, cs->buf);
+               if (retval) {
+                       cs->errcode = retval;
+                       return BLOCK_ABORT;
+               }
+               *block_nr = new_block;
+               ext2fs_mark_block_bitmap2(ctx->block_found_map, new_block);
+               ext2fs_mark_block_bitmap2(fs->block_map, new_block);
+               return BLOCK_CHANGED;
        }
        return 0;
 }
 
-static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
-                     struct dup_inode *dp, char* block_buf)
+static errcode_t clone_file(e2fsck_t ctx, ext2_ino_t ino,
+                           struct dup_inode *dp, char* block_buf)
 {
        ext2_filsys fs = ctx->fs;
        errcode_t       retval;
@@ -736,12 +789,14 @@ static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
        blk64_t         blk, new_blk;
        dnode_t         *n;
        struct inode_el *ino_el;
-       struct dup_block        *db;
+       struct dup_cluster      *dc;
        struct dup_inode        *di;
 
        clear_problem_context(&pctx);
        cs.errcode = 0;
        cs.dir = 0;
+       cs.dup_cluster = ~0;
+       cs.alloc_block = 0;
        cs.ctx = ctx;
        retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
        if (retval)
@@ -752,7 +807,7 @@ static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
 
        pctx.ino = ino;
        pctx.str = "clone_file";
-       if (ext2fs_inode_has_valid_blocks(&dp->inode))
+       if (ext2fs_inode_has_valid_blocks2(fs, &dp->inode))
                pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
                                                     clone_file_block, &cs);
        ext2fs_mark_bb_dirty(fs);
@@ -769,19 +824,20 @@ static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
        }
        /* The inode may have changed on disk, so we have to re-read it */
        e2fsck_read_inode(ctx, ino, &dp->inode, "clone file EA");
-       blk = ext2fs_file_acl_block(&dp->inode);
+       blk = ext2fs_file_acl_block(fs, &dp->inode);
        new_blk = blk;
        if (blk && (clone_file_block(fs, &new_blk,
                                     BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
                    BLOCK_CHANGED)) {
-               ext2fs_file_acl_block_set(&dp->inode, new_blk);
+               ext2fs_file_acl_block_set(fs, &dp->inode, new_blk);
                e2fsck_write_inode(ctx, ino, &dp->inode, "clone file EA");
                /*
                 * If we cloned the EA block, find all other inodes
                 * which refered to that EA block, and modify
                 * them to point to the new EA block.
                 */
-               n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(blk));
+               n = dict_lookup(&clstr_dict,
+                               INT_TO_VOIDPTR(EXT2FS_B2C(fs, blk)));
                if (!n) {
                        com_err("clone_file", 0,
                                _("internal error: couldn't lookup EA "
@@ -789,8 +845,8 @@ static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
                        retval = 0; /* OK to stumble on... */
                        goto errout;
                }
-               db = (struct dup_block *) dnode_get(n);
-               for (ino_el = db->inode_list; ino_el; ino_el = ino_el->next) {
+               dc = (struct dup_cluster *) dnode_get(n);
+               for (ino_el = dc->inode_list; ino_el; ino_el = ino_el->next) {
                        if (ino_el->inode == ino)
                                continue;
                        n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
@@ -803,12 +859,12 @@ static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
                                goto errout;
                        }
                        di = (struct dup_inode *) dnode_get(n);
-                       if (ext2fs_file_acl_block(&di->inode) == blk) {
-                               ext2fs_file_acl_block_set(&di->inode,
-                                         ext2fs_file_acl_block(&dp->inode));
+                       if (ext2fs_file_acl_block(fs, &di->inode) == blk) {
+                               ext2fs_file_acl_block_set(fs, &di->inode,
+                                       ext2fs_file_acl_block(fs, &dp->inode));
                                e2fsck_write_inode(ctx, ino_el->inode,
                                           &di->inode, "clone file EA");
-                               decrement_badcount(ctx, blk, db);
+                               decrement_badcount(ctx, blk, dc);
                        }
                }
        }
@@ -854,3 +910,45 @@ static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block)
        }
        return 0;
 }
+
+/*
+ * This routine returns 1 if a cluster overlaps with one of the superblocks,
+ * group descriptors, inode bitmaps, or block bitmaps.
+ */
+static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster)
+{
+       ext2_filsys fs = ctx->fs;
+       blk64_t first_block;
+       dgrp_t  i;
+
+       first_block = fs->super->s_first_data_block;
+       for (i = 0; i < fs->group_desc_count; i++) {
+
+               /* Check superblocks/block group descriptors */
+               if (ext2fs_bg_has_super(fs, i)) {
+                       if (cluster >= EXT2FS_B2C(fs, first_block) &&
+                           (cluster <= EXT2FS_B2C(fs, first_block +
+                                                  fs->desc_blocks)))
+                               return 1;
+               }
+
+               /* Check the inode table */
+               if ((ext2fs_inode_table_loc(fs, i)) &&
+                   (cluster >= EXT2FS_B2C(fs,
+                                          ext2fs_inode_table_loc(fs, i))) &&
+                   (cluster <= EXT2FS_B2C(fs,
+                                          ext2fs_inode_table_loc(fs, i) +
+                                          fs->inode_blocks_per_group - 1)))
+                       return 1;
+
+               /* Check the bitmap blocks */
+               if ((cluster == EXT2FS_B2C(fs,
+                                          ext2fs_block_bitmap_loc(fs, i))) ||
+                   (cluster == EXT2FS_B2C(fs,
+                                          ext2fs_inode_bitmap_loc(fs, i))))
+                       return 1;
+
+               first_block += fs->super->s_blocks_per_group;
+       }
+       return 0;
+}