Whamcloud - gitweb
blkid: fix ZFS device detection
[tools/e2fsprogs.git] / e2fsck / pass3.c
index e358bb2..08e0b8d 100644 (file)
@@ -53,7 +53,7 @@ static ext2fs_inode_bitmap inode_done_map = 0;
 void e2fsck_pass3(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       struct dir_info_iter *iter;
+       struct dir_info_iter *iter = NULL;
 #ifdef RESOURCE_TRACK
        struct resource_track   rtrack;
 #endif
@@ -108,12 +108,11 @@ void e2fsck_pass3(e2fsck_t ctx)
                        if (check_directory(ctx, dir->ino, &pctx))
                                goto abort_exit;
        }
-       e2fsck_dir_info_iter_end(ctx, iter);
 
        /*
         * Force the creation of /lost+found if not present
         */
-       if ((ctx->flags & E2F_OPT_READONLY) == 0)
+       if ((ctx->options & E2F_OPT_READONLY) == 0)
                e2fsck_get_lost_and_found(ctx, 1);
 
        /*
@@ -123,6 +122,8 @@ void e2fsck_pass3(e2fsck_t ctx)
        e2fsck_rehash_directories(ctx);
 
 abort_exit:
+       if (iter)
+               e2fsck_dir_info_iter_end(ctx, iter);
        e2fsck_free_dir_info(ctx);
        if (inode_loop_detect) {
                ext2fs_free_inode_bitmap(inode_loop_detect);
@@ -133,6 +134,17 @@ abort_exit:
                inode_done_map = 0;
        }
 
+       if (ctx->lnf_repair_block) {
+               ext2fs_unmark_block_bitmap2(ctx->block_found_map,
+                                           ctx->lnf_repair_block);
+               ctx->lnf_repair_block = 0;
+       }
+       if (ctx->root_repair_block) {
+               ext2fs_unmark_block_bitmap2(ctx->block_found_map,
+                                           ctx->root_repair_block);
+               ctx->root_repair_block = 0;
+       }
+
        print_resource_track(ctx, _("Pass 3"), &rtrack, ctx->fs->io);
 }
 
@@ -175,6 +187,11 @@ static void check_root(e2fsck_t ctx)
        /*
         * First, find a free block
         */
+       if (ctx->root_repair_block) {
+               blk = ctx->root_repair_block;
+               ctx->root_repair_block = 0;
+               goto skip_new_block;
+       }
        pctx.errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
        if (pctx.errcode) {
                pctx.str = "ext2fs_new_block";
@@ -183,6 +200,7 @@ static void check_root(e2fsck_t ctx)
                return;
        }
        ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
+skip_new_block:
        ext2fs_mark_block_bitmap2(fs->block_map, blk);
        ext2fs_mark_bb_dirty(fs);
 
@@ -198,9 +216,9 @@ static void check_root(e2fsck_t ctx)
                return;
        }
 
-       pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
+       pctx.errcode = ext2fs_write_dir_block3(fs, blk, block, 0);
        if (pctx.errcode) {
-               pctx.str = "ext2fs_write_dir_block";
+               pctx.str = "ext2fs_write_dir_block3";
                fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
@@ -411,6 +429,11 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
        /*
         * First, find a free block
         */
+       if (ctx->lnf_repair_block) {
+               blk = ctx->lnf_repair_block;
+               ctx->lnf_repair_block = 0;
+               goto skip_new_block;
+       }
        retval = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
        if (retval) {
                pctx.errcode = retval;
@@ -418,6 +441,7 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
                return 0;
        }
        ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
+skip_new_block:
        ext2fs_block_alloc_stats2(fs, blk, +1);
 
        /*
@@ -444,7 +468,7 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
                return 0;
        }
 
-       retval = ext2fs_write_dir_block(fs, blk, block);
+       retval = ext2fs_write_dir_block3(fs, blk, block, 0);
        ext2fs_free_mem(&block);
        if (retval) {
                pctx.errcode = retval;
@@ -712,12 +736,23 @@ static int expand_dir_proc(ext2_filsys fs,
                last_blk = *blocknr;
                return 0;
        }
-       retval = ext2fs_new_block2(fs, last_blk, ctx->block_found_map,
-                                 &new_blk);
-       if (retval) {
-               es->err = retval;
-               return BLOCK_ABORT;
+
+       if (blockcnt &&
+           (EXT2FS_B2C(fs, last_blk) == EXT2FS_B2C(fs, last_blk + 1)))
+               new_blk = last_blk + 1;
+       else {
+               last_blk &= ~EXT2FS_CLUSTER_MASK(fs);
+               retval = ext2fs_new_block2(fs, last_blk, ctx->block_found_map,
+                                         &new_blk);
+               if (retval) {
+                       es->err = retval;
+                       return BLOCK_ABORT;
+               }
+               es->newblocks++;
+               ext2fs_block_alloc_stats2(fs, new_blk, +1);
        }
+       last_blk = new_blk;
+
        if (blockcnt > 0) {
                retval = ext2fs_new_dir_block(fs, 0, 0, &block);
                if (retval) {
@@ -725,7 +760,7 @@ static int expand_dir_proc(ext2_filsys fs,
                        return BLOCK_ABORT;
                }
                es->num--;
-               retval = ext2fs_write_dir_block(fs, new_blk, block);
+               retval = ext2fs_write_dir_block3(fs, new_blk, block, 0);
        } else {
                retval = ext2fs_get_mem(fs->blocksize, &block);
                if (retval) {
@@ -742,8 +777,6 @@ static int expand_dir_proc(ext2_filsys fs,
        ext2fs_free_mem(&block);
        *blocknr = new_blk;
        ext2fs_mark_block_bitmap2(ctx->block_found_map, new_blk);
-       ext2fs_block_alloc_stats2(fs, new_blk, +1);
-       es->newblocks++;
 
        if (es->num == 0)
                return (BLOCK_CHANGED | BLOCK_ABORT);
@@ -794,8 +827,9 @@ errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
                return retval;
 
        sz = (es.last_block + 1) * fs->blocksize;
-       inode.i_size = sz;
-       inode.i_size_high = sz >> 32;
+       retval = ext2fs_inode_size_set(fs, &inode, sz);
+       if (retval)
+               return retval;
        ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
        quota_data_add(ctx->qctx, &inode, dir, es.newblocks * fs->blocksize);