Whamcloud - gitweb
e2fsck: allow deleting or zeroing shared blocks
[tools/e2fsprogs.git] / e2fsck / super.c
index 4c989cc..6c18d0e 100644 (file)
@@ -9,6 +9,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
@@ -54,16 +55,16 @@ struct process_block_struct {
 };
 
 static int release_inode_block(ext2_filsys fs,
-                              blk_t    *block_nr,
+                              blk64_t  *block_nr,
                               e2_blkcnt_t blockcnt,
-                              blk_t    ref_blk EXT2FS_ATTR((unused)),
+                              blk64_t  ref_blk EXT2FS_ATTR((unused)),
                               int      ref_offset EXT2FS_ATTR((unused)),
                               void *priv_data)
 {
        struct process_block_struct *pb;
        e2fsck_t                ctx;
        struct problem_context  *pctx;
-       blk_t                   blk = *block_nr;
+       blk64_t                 blk = *block_nr;
        int                     retval = 0;
 
        pb = (struct process_block_struct *) priv_data;
@@ -141,7 +142,7 @@ static int release_inode_block(ext2_filsys fs,
                retval |= BLOCK_CHANGED;
        }
 
-       ext2fs_block_alloc_stats(fs, blk, -1);
+       ext2fs_block_alloc_stats2(fs, blk, -1);
        return retval;
 }
 
@@ -159,7 +160,7 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
        errcode_t                       retval;
        __u32                           count;
 
-       if (!ext2fs_inode_has_valid_blocks(inode))
+       if (!ext2fs_inode_has_valid_blocks2(fs, inode))
                return 0;
 
        pb.buf = block_buf + 3 * ctx->fs->blocksize;
@@ -170,8 +171,7 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
        if (inode->i_links_count) {
                pb.truncating = 1;
                pb.truncate_block = (e2_blkcnt_t)
-                       ((((long long)inode->i_size_high << 32) +
-                         inode->i_size + fs->blocksize - 1) /
+                       ((EXT2_I_SIZE(inode) + fs->blocksize - 1) /
                         fs->blocksize);
                pb.truncate_offset = inode->i_size % fs->blocksize;
        } else {
@@ -180,7 +180,7 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
                pb.truncate_offset = 0;
        }
        pb.truncated_blocks = 0;
-       retval = ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
+       retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
                                      block_buf, release_inode_block, &pb);
        if (retval) {
                com_err("release_inode_blocks", retval,
@@ -197,24 +197,24 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
        if (pb.truncated_blocks)
                ext2fs_iblk_sub_blocks(fs, inode, pb.truncated_blocks);
 
-       if (ext2fs_file_acl_block(inode)) {
-               retval = ext2fs_adjust_ea_refcount(fs, ext2fs_file_acl_block(inode),
-                                                  block_buf, -1, &count);
+       if (ext2fs_file_acl_block(fs, inode)) {
+               retval = ext2fs_adjust_ea_refcount2(fs,
+                                       ext2fs_file_acl_block(fs, inode),
+                                       block_buf, -1, &count);
                if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
                        retval = 0;
                        count = 1;
                }
                if (retval) {
                        com_err("release_inode_blocks", retval,
-               _("while calling ext2fs_adjust_ea_refcount for inode %d"),
+               _("while calling ext2fs_adjust_ea_refcount2 for inode %d"),
                                ino);
                        return 1;
                }
                if (count == 0)
-                       ext2fs_block_alloc_stats(fs,
-                                                ext2fs_file_acl_block(inode),
-                                                -1);
-               ext2fs_file_acl_block_set(inode, 0);
+                       ext2fs_block_alloc_stats2(fs,
+                                       ext2fs_file_acl_block(fs, inode), -1);
+               ext2fs_file_acl_block_set(fs, inode, 0);
        }
        return 0;
 }
@@ -313,7 +313,7 @@ void check_resize_inode(e2fsck_t ctx)
        struct problem_context  pctx;
        int             i, gdt_off, ind_off;
        dgrp_t          j;
-       blk_t           blk, pblk, expect;
+       blk64_t         blk, pblk, expect;
        __u32           *dind_buf = 0, *ind_buf;
        errcode_t       retval;
 
@@ -400,6 +400,8 @@ void check_resize_inode(e2fsck_t ctx)
 
        gdt_off = fs->desc_blocks;
        pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
+       if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0)
+               pblk++; /* Deal with 1024 blocksize bigalloc fs */
        for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
             i++, gdt_off++, pblk++) {
                gdt_off %= fs->blocksize/4;
@@ -456,19 +458,20 @@ static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
 void check_super_block(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t   first_block, last_block;
+       blk64_t first_block, last_block;
        struct ext2_super_block *sb = fs->super;
        problem_t       problem;
-       blk_t   blocks_per_group = fs->super->s_blocks_per_group;
-       blk_t   bpg_max;
+       blk64_t blocks_per_group = fs->super->s_blocks_per_group;
+       __u32   bpg_max, cpg_max;
        int     inodes_per_block;
        int     ipg_max;
        int     inode_size;
        int     accept_time_fudge;
+       int     broken_system_clock;
        dgrp_t  i;
-       blk_t   should_be;
+       blk64_t should_be;
        struct problem_context  pctx;
-       blk_t   free_blocks = 0;
+       blk64_t free_blocks = 0;
        ino_t   free_inodes = 0;
        int     csum_flag, clear_test_fs_flag;
 
@@ -476,7 +479,10 @@ void check_super_block(e2fsck_t ctx)
        ipg_max = inodes_per_block * (blocks_per_group - 4);
        if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
                ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
-       bpg_max = 8 * EXT2_BLOCK_SIZE(sb);
+       cpg_max = 8 * EXT2_BLOCK_SIZE(sb);
+       if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb))
+               cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb);
+       bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs);
        if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
                bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
 
@@ -501,11 +507,13 @@ void check_super_block(e2fsck_t ctx)
        check_super_value(ctx, "log_block_size", sb->s_log_block_size,
                          MIN_CHECK | MAX_CHECK, 0,
                          EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
-       check_super_value(ctx, "log_frag_size", sb->s_log_frag_size,
-                         MIN_CHECK | MAX_CHECK, 0, sb->s_log_block_size);
-       check_super_value(ctx, "frags_per_group", sb->s_frags_per_group,
-                         MIN_CHECK | MAX_CHECK, sb->s_blocks_per_group,
-                         bpg_max);
+       check_super_value(ctx, "log_cluster_size",
+                         sb->s_log_cluster_size,
+                         MIN_CHECK | MAX_CHECK, sb->s_log_block_size,
+                         (EXT2_MAX_CLUSTER_LOG_SIZE -
+                          EXT2_MIN_CLUSTER_LOG_SIZE));
+       check_super_value(ctx, "clusters_per_group", sb->s_clusters_per_group,
+                         MIN_CHECK | MAX_CHECK, 8, cpg_max);
        check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
                          MIN_CHECK | MAX_CHECK, 8, bpg_max);
        check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
@@ -523,6 +531,14 @@ void check_super_block(e2fsck_t ctx)
        check_super_value(ctx, "inode_size",
                          inode_size, MIN_CHECK | MAX_CHECK,
                          EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
+       if (sb->s_blocks_per_group != (sb->s_clusters_per_group *
+                                      EXT2FS_CLUSTER_RATIO(fs))) {
+               pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs);
+               pctx.str = "block_size";
+               fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
+               ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
+               return;
+       }
        if (inode_size & (inode_size - 1)) {
                pctx.num = inode_size;
                pctx.str = "inode_size";
@@ -541,25 +557,8 @@ void check_super_block(e2fsck_t ctx)
                }
        }
 
-       if (sb->s_log_block_size != (__u32) sb->s_log_frag_size) {
-               pctx.blk = EXT2_BLOCK_SIZE(sb);
-               pctx.blk2 = EXT2_FRAG_SIZE(sb);
-               fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
-               ctx->flags |= E2F_FLAG_ABORT;
-               return;
-       }
-
-       should_be = sb->s_frags_per_group >>
-               (sb->s_log_block_size - sb->s_log_frag_size);
-       if (sb->s_blocks_per_group != should_be) {
-               pctx.blk = sb->s_blocks_per_group;
-               pctx.blk2 = should_be;
-               fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
-               ctx->flags |= E2F_FLAG_ABORT;
-               return;
-       }
-
-       should_be = (sb->s_log_block_size == 0) ? 1 : 0;
+       should_be = (sb->s_log_block_size == 0 &&
+                    EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0;
        if (sb->s_first_data_block != should_be) {
                pctx.blk = sb->s_first_data_block;
                pctx.blk2 = should_be;
@@ -591,8 +590,8 @@ void check_super_block(e2fsck_t ctx)
 
                if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
                                               EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
-                       first_block = ext2fs_group_first_block(fs, i);
-                       last_block = ext2fs_group_last_block(fs, i);
+                       first_block = ext2fs_group_first_block2(fs, i);
+                       last_block = ext2fs_group_last_block2(fs, i);
                }
 
                if ((ext2fs_block_bitmap_loc(fs, i) < first_block) ||
@@ -636,6 +635,8 @@ void check_super_block(e2fsck_t ctx)
 
                should_be = 0;
                if (!ext2fs_group_desc_csum_verify(fs, i)) {
+                       pctx.csum1 = ext2fs_bg_checksum(fs, i);
+                       pctx.csum2 = ext2fs_group_desc_csum(fs, i);
                        if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
                                ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
                                ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
@@ -667,15 +668,6 @@ void check_super_block(e2fsck_t ctx)
                        ext2fs_unmark_valid(fs);
                }
 
-               if (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
-                   !ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT)) {
-                       if (fix_problem(ctx, PR_0_BB_UNINIT_IB_INIT, &pctx)) {
-                               ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
-                               should_be = 1;
-                       }
-                       ext2fs_unmark_valid(fs);
-               }
-
                if (csum_flag &&
                    (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) ||
                     ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) {
@@ -694,23 +686,8 @@ void check_super_block(e2fsck_t ctx)
                        return;
        }
 
-       /*
-        * Update the global counts from the block group counts.  This
-        * is needed for an experimental patch which eliminates
-        * locking the entire filesystem when allocating blocks or
-        * inodes; if the filesystem is not unmounted cleanly, the
-        * global counts may not be accurate.
-        */
-       if ((free_blocks != ext2fs_free_blocks_count(sb)) ||
-           (free_inodes != sb->s_free_inodes_count)) {
-               if (ctx->options & E2F_OPT_READONLY)
-                       ext2fs_unmark_valid(fs);
-               else {
-                       ext2fs_free_blocks_count_set(sb, free_blocks);
-                       sb->s_free_inodes_count = free_inodes;
-                       ext2fs_mark_super_dirty(fs);
-               }
-       }
+       ctx->free_blocks = EXT2FS_C2B(fs, free_blocks);
+       ctx->free_inodes = free_inodes;
 
        if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
            (sb->s_free_inodes_count > sb->s_inodes_count))
@@ -735,7 +712,7 @@ void check_super_block(e2fsck_t ctx)
        if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
                if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
                        uuid_generate(sb->s_uuid);
-                       ext2fs_mark_super_dirty(fs);
+                       fs->flags |= EXT2_FLAG_DIRTY;
                        fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
                }
        }
@@ -753,7 +730,7 @@ void check_super_block(e2fsck_t ctx)
            (fs_proc_check("ext4") || check_for_modules("ext4"))) {
                if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
                        fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
-                       ext2fs_mark_super_dirty(fs);
+                       fs->flags |= EXT2_FLAG_DIRTY;
                        fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
                }
        }
@@ -826,28 +803,35 @@ void check_super_block(e2fsck_t ctx)
                            0, accept_time_fudge, &accept_time_fudge);
        ctx->time_fudge = accept_time_fudge ? 86400 : 0;
 
+       profile_get_boolean(ctx->profile, "options", "broken_system_clock",
+                           0, 0, &broken_system_clock);
+
        /*
         * Check to see if the superblock last mount time or last
         * write time is in the future.
         */
-       if (fs->super->s_mtime > (__u32) ctx->now) {
+       if (!broken_system_clock &&
+           !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
+           fs->super->s_mtime > (__u32) ctx->now) {
                pctx.num = fs->super->s_mtime;
                problem = PR_0_FUTURE_SB_LAST_MOUNT;
                if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
                        problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
                if (fix_problem(ctx, problem, &pctx)) {
                        fs->super->s_mtime = ctx->now;
-                       ext2fs_mark_super_dirty(fs);
+                       fs->flags |= EXT2_FLAG_DIRTY;
                }
        }
-       if (fs->super->s_wtime > (__u32) ctx->now) {
+       if (!broken_system_clock &&
+           !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
+           fs->super->s_wtime > (__u32) ctx->now) {
                pctx.num = fs->super->s_wtime;
                problem = PR_0_FUTURE_SB_LAST_WRITE;
                if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
                        problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
                if (fix_problem(ctx, problem, &pctx)) {
                        fs->super->s_wtime = ctx->now;
-                       ext2fs_mark_super_dirty(fs);
+                       fs->flags |= EXT2_FLAG_DIRTY;
                }
        }
 
@@ -866,6 +850,11 @@ void check_super_block(e2fsck_t ctx)
         */
        e2fsck_fix_dirhash_hint(ctx);
 
+       /*
+        * Hide quota inodes if necessary.
+        */
+       e2fsck_hide_quota(ctx);
+
        return;
 }
 
@@ -898,7 +887,7 @@ int check_backup_super_block(e2fsck_t ctx)
        ext2_filsys     fs = ctx->fs;
        errcode_t       retval;
        dgrp_t          g;
-       blk_t           sb;
+       blk64_t         sb;
        int             ret = 0;
        char            buf[SUPERBLOCK_SIZE];
        struct ext2_super_block *backup_sb;