Whamcloud - gitweb
Clarify copyright license status of lib/et and lib/ss in the COPYING file
[tools/e2fsprogs.git] / e2fsck / super.c
index 227a025..ae40531 100644 (file)
@@ -60,7 +60,7 @@ struct process_block_struct {
        struct problem_context *pctx;
        int             truncating;
        int             truncate_offset;
-       blk_t           truncate_block;
+       e2_blkcnt_t     truncate_block;
        int             truncated_blocks;
        int             abort;
        errcode_t       errcode;
@@ -68,7 +68,9 @@ struct process_block_struct {
 
 static int release_inode_block(ext2_filsys fs,
                               blk_t    *block_nr,
-                              int blockcnt,
+                              e2_blkcnt_t blockcnt,
+                              blk_t    ref_blk EXT2FS_ATTR((unused)),
+                              int      ref_offset EXT2FS_ATTR((unused)),
                               void *priv_data)
 {
        struct process_block_struct *pb;
@@ -152,10 +154,7 @@ static int release_inode_block(ext2_filsys fs,
                retval |= BLOCK_CHANGED;
        }
        
-       ext2fs_unmark_block_bitmap(fs->block_map, blk);
-       fs->group_desc[ext2fs_group_of_blk(fs, blk)].bg_free_blocks_count++;
-       fs->super->s_free_blocks_count++;
-       
+       ext2fs_block_alloc_stats(fs, blk, -1);
        return retval;
 }
                
@@ -168,9 +167,10 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
                                struct ext2_inode *inode, char *block_buf,
                                struct problem_context *pctx)
 {
+       struct process_block_struct     pb;
        ext2_filsys                     fs = ctx->fs;
        errcode_t                       retval;
-       struct process_block_struct     pb;
+       __u32                           count;
 
        if (!ext2fs_inode_has_valid_blocks(inode))
                return 0;
@@ -182,7 +182,7 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
        pb.pctx = pctx;
        if (inode->i_links_count) {
                pb.truncating = 1;
-               pb.truncate_block = (blk_t)
+               pb.truncate_block = (e2_blkcnt_t)
                        ((((long long)inode->i_size_high << 32) +
                          inode->i_size + fs->blocksize - 1) /
                         fs->blocksize);
@@ -193,7 +193,7 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
                pb.truncate_offset = 0;
        }
        pb.truncated_blocks = 0;
-       retval = ext2fs_block_iterate(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE, 
+       retval = ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE, 
                                      block_buf, release_inode_block, &pb);
        if (retval) {
                com_err("release_inode_blocks", retval,
@@ -211,7 +211,23 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
                inode->i_blocks -= pb.truncated_blocks *
                        (fs->blocksize / 512);
 
-       ext2fs_mark_bb_dirty(fs);
+       if (inode->i_file_acl) {
+               retval = ext2fs_adjust_ea_refcount(fs, inode->i_file_acl,
+                                                  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"),
+                               ino);
+                       return 1;
+               }
+               if (count == 0)
+                       ext2fs_block_alloc_stats(fs, inode->i_file_acl, -1);
+               inode->i_file_acl = 0;
+       }
        return 0;
 }
 
@@ -222,7 +238,6 @@ static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
 static int release_orphan_inodes(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       int group;
        ext2_ino_t      ino, next_ino;
        struct ext2_inode inode;
        struct problem_context pctx;
@@ -281,41 +296,161 @@ static int release_orphan_inodes(e2fsck_t ctx)
                        goto return_abort;
 
                if (!inode.i_links_count) {
-                       ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
-                       ext2fs_mark_ib_dirty(fs);
-                       group = ext2fs_group_of_ino(fs, ino);
-                       fs->group_desc[group].bg_free_inodes_count++;
-                       fs->super->s_free_inodes_count++;
-                       if (LINUX_S_ISDIR(inode.i_mode))
-                               fs->group_desc[group].bg_used_dirs_count--;
-                       
-                       inode.i_dtime = time(0);
+                       ext2fs_inode_alloc_stats2(fs, ino, -1,
+                                                 LINUX_S_ISDIR(inode.i_mode));
+                       inode.i_dtime = ctx->now;
                } else {
                        inode.i_dtime = 0;
                }
                e2fsck_write_inode(ctx, ino, &inode, "delete_file");
                ino = next_ino;
        }
-       ext2fs_free_mem((void **) &block_buf);
+       ext2fs_free_mem(&block_buf);
        return 0;
 return_abort:
-       ext2fs_free_mem((void **) &block_buf);
+       ext2fs_free_mem(&block_buf);
        return 1;
 }
 
+/*
+ * Check the resize inode to make sure it is sane.  We check both for
+ * the case where on-line resizing is not enabled (in which case the
+ * resize inode should be cleared) as well as the case where on-line
+ * resizing is enabled.
+ */
+void check_resize_inode(e2fsck_t ctx)
+{
+       ext2_filsys fs = ctx->fs;
+       struct ext2_inode inode;
+       struct problem_context  pctx;
+       int             i, j, gdt_off, ind_off;
+       blk_t           blk, pblk, expect;
+       __u32           *dind_buf = 0, *ind_buf;
+       errcode_t       retval;
+
+       clear_problem_context(&pctx);
+
+       /* 
+        * If the resize inode feature isn't set, then 
+        * s_reserved_gdt_blocks must be zero.
+        */
+       if (!(fs->super->s_feature_compat & 
+             EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
+               if (fs->super->s_reserved_gdt_blocks) {
+                       pctx.num = fs->super->s_reserved_gdt_blocks;
+                       if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
+                                       &pctx)) {
+                               fs->super->s_reserved_gdt_blocks = 0;
+                               ext2fs_mark_super_dirty(fs);
+                       }
+               }
+       }
+
+       /* Read the resize inode */
+       pctx.ino = EXT2_RESIZE_INO;
+       retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
+       if (retval) {
+               if (fs->super->s_feature_compat & 
+                   EXT2_FEATURE_COMPAT_RESIZE_INODE)
+                       ctx->flags |= E2F_FLAG_RESIZE_INODE;
+               return;
+       }
+
+       /* 
+        * If the resize inode feature isn't set, check to make sure 
+        * the resize inode is cleared; then we're done.
+        */
+       if (!(fs->super->s_feature_compat & 
+             EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
+               for (i=0; i < EXT2_N_BLOCKS; i++) {
+                       if (inode.i_block[i])
+                               break;
+               }
+               if ((i < EXT2_N_BLOCKS) &&
+                   fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
+                       memset(&inode, 0, sizeof(inode));
+                       e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
+                                          "clear_resize");
+               }
+               return;
+       }
+
+       /* 
+        * The resize inode feature is enabled; check to make sure the
+        * only block in use is the double indirect block
+        */
+       blk = inode.i_block[EXT2_DIND_BLOCK];
+       for (i=0; i < EXT2_N_BLOCKS; i++) {
+               if (i != EXT2_DIND_BLOCK && inode.i_block[i])
+                       break;
+       }
+       if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
+           !(inode.i_mode & LINUX_S_IFREG) ||
+           (blk < fs->super->s_first_data_block ||
+            blk >= fs->super->s_blocks_count)) {
+       resize_inode_invalid:
+               if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
+                       memset(&inode, 0, sizeof(inode));
+                       e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
+                                          "clear_resize");
+                       ctx->flags |= E2F_FLAG_RESIZE_INODE;
+               }
+               if (!(ctx->options & E2F_OPT_READONLY)) {
+                       fs->super->s_state &= ~EXT2_VALID_FS;
+                       ext2fs_mark_super_dirty(fs);
+               }
+               goto cleanup;
+       }
+       dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
+                                                   "resize dind buffer");
+       ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
+
+       retval = ext2fs_read_ind_block(fs, blk, dind_buf);
+       if (retval)
+               goto resize_inode_invalid;
+
+       gdt_off = fs->desc_blocks;
+       pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
+       for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4; 
+            i++, gdt_off++, pblk++) {
+               gdt_off %= fs->blocksize/4;
+               if (dind_buf[gdt_off] != pblk)
+                       goto resize_inode_invalid;
+               retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
+               if (retval) 
+                       goto resize_inode_invalid;
+               ind_off = 0;
+               for (j = 1; j < fs->group_desc_count; j++) {
+                       if (!ext2fs_bg_has_super(fs, j))
+                               continue;
+                       expect = pblk + (j * fs->super->s_blocks_per_group);
+                       if (ind_buf[ind_off] != expect)
+                               goto resize_inode_invalid;
+                       ind_off++;
+               }
+       }
+
+cleanup:
+       if (dind_buf)
+               ext2fs_free_mem(&dind_buf);
+
+ }
 
 void check_super_block(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
        blk_t   first_block, last_block;
        struct ext2_super_block *sb = fs->super;
+       struct ext2_group_desc *gd;
        blk_t   blocks_per_group = fs->super->s_blocks_per_group;
        blk_t   bpg_max;
        int     inodes_per_block;
        int     ipg_max;
+       int     inode_size;
        dgrp_t  i;
        blk_t   should_be;
        struct problem_context  pctx;
+       __u32   free_blocks = 0, free_inodes = 0;
 
        inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
        ipg_max = inodes_per_block * (blocks_per_group - 4);
@@ -356,8 +491,22 @@ void check_super_block(e2fsck_t ctx)
        check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
                          MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
        check_super_value(ctx, "r_blocks_count", sb->s_r_blocks_count,
-                         MAX_CHECK, 0, sb->s_blocks_count / 4);
-
+                         MAX_CHECK, 0, sb->s_blocks_count / 2);
+       check_super_value(ctx, "reserved_gdt_blocks", 
+                         sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
+                         fs->blocksize/4);
+       inode_size = EXT2_INODE_SIZE(sb);
+       check_super_value(ctx, "inode_size",
+                         inode_size, MIN_CHECK | MAX_CHECK,
+                         EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
+       if (inode_size & (inode_size - 1)) {
+               pctx.num = inode_size;
+               pctx.str = "inode_size";
+               fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
+               ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
+               return;
+       }
+               
        if (!ctx->num_blocks) {
                pctx.errcode = e2fsck_get_device_size(ctx);
                if (pctx.errcode && pctx.errcode != EXT2_ET_UNIMPLEMENTED) {
@@ -376,7 +525,7 @@ void check_super_block(e2fsck_t ctx)
                }
        }
 
-       if (sb->s_log_block_size != sb->s_log_frag_size) {
+       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);
@@ -416,54 +565,84 @@ void check_super_block(e2fsck_t ctx)
        /*
         * Verify the group descriptors....
         */
-       first_block =  fs->super->s_first_data_block;
-       last_block = first_block + blocks_per_group;
+       first_block =  sb->s_first_data_block;
 
-       for (i = 0; i < fs->group_desc_count; i++) {
+       for (i = 0, gd=fs->group_desc; i < fs->group_desc_count; i++, gd++) {
                pctx.group = i;
-               
-               if (i == fs->group_desc_count - 1)
-                       last_block = fs->super->s_blocks_count;
-               if ((fs->group_desc[i].bg_block_bitmap < first_block) ||
-                   (fs->group_desc[i].bg_block_bitmap >= last_block)) {
-                       pctx.blk = fs->group_desc[i].bg_block_bitmap;
+
+               first_block = ext2fs_group_first_block(fs, i);
+               last_block = ext2fs_group_last_block(fs, i);
+
+               if ((gd->bg_block_bitmap < first_block) ||
+                   (gd->bg_block_bitmap > last_block)) {
+                       pctx.blk = gd->bg_block_bitmap;
                        if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
-                               fs->group_desc[i].bg_block_bitmap = 0;
+                               gd->bg_block_bitmap = 0;
                }
-               if (fs->group_desc[i].bg_block_bitmap == 0) {
+               if (gd->bg_block_bitmap == 0) {
                        ctx->invalid_block_bitmap_flag[i]++;
                        ctx->invalid_bitmaps++;
                }
-               if ((fs->group_desc[i].bg_inode_bitmap < first_block) ||
-                   (fs->group_desc[i].bg_inode_bitmap >= last_block)) {
-                       pctx.blk = fs->group_desc[i].bg_inode_bitmap;
+               if ((gd->bg_inode_bitmap < first_block) ||
+                   (gd->bg_inode_bitmap > last_block)) {
+                       pctx.blk = gd->bg_inode_bitmap;
                        if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
-                               fs->group_desc[i].bg_inode_bitmap = 0;
+                               gd->bg_inode_bitmap = 0;
                }
-               if (fs->group_desc[i].bg_inode_bitmap == 0) {
+               if (gd->bg_inode_bitmap == 0) {
                        ctx->invalid_inode_bitmap_flag[i]++;
                        ctx->invalid_bitmaps++;
                }
-               if ((fs->group_desc[i].bg_inode_table < first_block) ||
-                   ((fs->group_desc[i].bg_inode_table +
-                     fs->inode_blocks_per_group - 1) >= last_block)) {
-                       pctx.blk = fs->group_desc[i].bg_inode_table;
+               if ((gd->bg_inode_table < first_block) ||
+                   ((gd->bg_inode_table +
+                     fs->inode_blocks_per_group - 1) > last_block)) {
+                       pctx.blk = gd->bg_inode_table;
                        if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
-                               fs->group_desc[i].bg_inode_table = 0;
+                               gd->bg_inode_table = 0;
                }
-               if (fs->group_desc[i].bg_inode_table == 0) {
+               if (gd->bg_inode_table == 0) {
                        ctx->invalid_inode_table_flag[i]++;
                        ctx->invalid_bitmaps++;
                }
-               first_block += fs->super->s_blocks_per_group;
-               last_block += fs->super->s_blocks_per_group;
+               free_blocks += gd->bg_free_blocks_count;
+               free_inodes += gd->bg_free_inodes_count;
+
+               if ((gd->bg_free_blocks_count > sb->s_blocks_per_group) ||
+                   (gd->bg_free_inodes_count > sb->s_inodes_per_group) ||
+                   (gd->bg_used_dirs_count > sb->s_inodes_per_group))
+                       ext2fs_unmark_valid(fs);
+
        }
+
+       /*
+        * 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 != sb->s_free_blocks_count) ||
+           (free_inodes != sb->s_free_inodes_count)) {
+               if (ctx->options & E2F_OPT_READONLY)
+                       ext2fs_unmark_valid(fs);
+               else {
+                       sb->s_free_blocks_count = free_blocks;
+                       sb->s_free_inodes_count = free_inodes;
+                       ext2fs_mark_super_dirty(fs);
+               }
+       }
+       
+       if ((sb->s_free_blocks_count > sb->s_blocks_count) ||
+           (sb->s_free_inodes_count > sb->s_inodes_count))
+               ext2fs_unmark_valid(fs);
+
+
        /*
         * If we have invalid bitmaps, set the error state of the
         * filesystem.
         */
        if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
-               fs->super->s_state &= ~EXT2_VALID_FS;
+               sb->s_state &= ~EXT2_VALID_FS;
                ext2fs_mark_super_dirty(fs);
        }
 
@@ -477,10 +656,11 @@ void check_super_block(e2fsck_t ctx)
                if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
                        uuid_generate(sb->s_uuid);
                        ext2fs_mark_super_dirty(fs);
+                       fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
                }
        }
 #endif
-       
+
        /*
         * For the Hurd, check to see if the filetype option is set,
         * since it doesn't support it.
@@ -513,6 +693,8 @@ void check_super_block(e2fsck_t ctx)
                ext2fs_mark_super_dirty(fs);
        }
 
+       check_resize_inode(ctx);
+
        /*
         * Clean up any orphan inodes, if present.
         */
@@ -521,11 +703,34 @@ void check_super_block(e2fsck_t ctx)
                ext2fs_mark_super_dirty(fs);
        }
 
+       /* 
+        * Check to see if the superblock last mount time or last
+        * write time is in the future.
+        */
+       if (fs->super->s_mtime > ctx->now) {
+               pctx.num = fs->super->s_mtime;
+               if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_MOUNT, &pctx)) {
+                       fs->super->s_mtime = ctx->now;
+                       ext2fs_mark_super_dirty(fs);
+               }
+       }
+       if (fs->super->s_wtime > ctx->now) {
+               pctx.num = fs->super->s_wtime;
+               if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_WRITE, &pctx)) {
+                       fs->super->s_wtime = ctx->now;
+                       ext2fs_mark_super_dirty(fs);
+               }
+       }
+
        /*
         * Move the ext3 journal file, if necessary.
         */
        e2fsck_move_ext3_journal(ctx);
-       return;
-}
 
+       /*
+        * Fix journal hint, if necessary
+        */
+       e2fsck_fix_ext3_journal_hint(ctx);
 
+       return;
+}