Whamcloud - gitweb
blkid: fix ZFS device detection
[tools/e2fsprogs.git] / e2fsck / pass3.c
index a7d19a1..08e0b8d 100644 (file)
@@ -34,6 +34,7 @@
  *             - The dirinfo directory information cache.
  */
 
+#include "config.h"
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
@@ -52,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
@@ -73,8 +74,9 @@ void e2fsck_pass3(e2fsck_t ctx)
        /*
         * Allocate some bitmaps to do loop detection.
         */
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("inode done bitmap"),
-                                                   &inode_done_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("inode done bitmap"),
+                                       EXT2FS_BMAP64_AUTODIR,
+                                       "inode_done_map", &inode_done_map);
        if (pctx.errcode) {
                pctx.num = 2;
                fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
@@ -106,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);
 
        /*
@@ -121,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);
@@ -131,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);
 }
 
@@ -173,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";
@@ -181,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);
 
@@ -196,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;
@@ -317,7 +337,7 @@ static int check_directory(e2fsck_t ctx, ext2_ino_t dir,
                        if (inode_loop_detect)
                                ext2fs_clear_inode_bitmap(inode_loop_detect);
                        else {
-                               pctx->errcode = ext2fs_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), &inode_loop_detect);
+                               pctx->errcode = e2fsck_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), EXT2FS_BMAP64_AUTODIR, "inode_loop_detect", &inode_loop_detect);
                                if (pctx->errcode) {
                                        pctx->num = 1;
                                        fix_problem(ctx,
@@ -409,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;
@@ -416,7 +441,8 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
                return 0;
        }
        ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
-       ext2fs_block_alloc_stats(fs, blk, +1);
+skip_new_block:
+       ext2fs_block_alloc_stats2(fs, blk, +1);
 
        /*
         * Next find a free inode.
@@ -442,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;
@@ -488,6 +514,8 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
        ext2fs_icount_store(ctx->inode_count, ino, 2);
        ext2fs_icount_store(ctx->inode_link_info, ino, 2);
        ctx->lost_and_found = ino;
+       quota_data_add(ctx->qctx, &inode, ino, fs->blocksize);
+       quota_data_inodes(ctx->qctx, &inode, ino, +1);
 #if 0
        printf("/lost+found created; inode #%lu\n", ino);
 #endif
@@ -675,24 +703,24 @@ static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
  */
 
 struct expand_dir_struct {
-       int                     num;
-       int                     guaranteed_size;
-       int                     newblocks;
-       int                     last_block;
+       blk64_t                 num;
+       e2_blkcnt_t             guaranteed_size;
+       blk64_t                 newblocks;
+       blk64_t                 last_block;
        errcode_t               err;
        e2fsck_t                ctx;
 };
 
 static int expand_dir_proc(ext2_filsys fs,
-                          blk_t        *blocknr,
+                          blk64_t      *blocknr,
                           e2_blkcnt_t  blockcnt,
-                          blk_t ref_block EXT2FS_ATTR((unused)),
+                          blk64_t ref_block EXT2FS_ATTR((unused)),
                           int ref_offset EXT2FS_ATTR((unused)),
                           void *priv_data)
 {
        struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
        blk64_t new_blk;
-       static blk_t    last_blk = 0;
+       static blk64_t  last_blk = 0;
        char            *block;
        errcode_t       retval;
        e2fsck_t        ctx;
@@ -708,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) {
@@ -721,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) {
@@ -729,7 +768,7 @@ static int expand_dir_proc(ext2_filsys fs,
                        return BLOCK_ABORT;
                }
                memset(block, 0, fs->blocksize);
-               retval = io_channel_write_blk(fs->io, new_blk, 1, block);
+               retval = io_channel_write_blk64(fs->io, new_blk, 1, block);
        }
        if (retval) {
                es->err = retval;
@@ -738,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_stats(fs, new_blk, +1);
-       es->newblocks++;
 
        if (es->num == 0)
                return (BLOCK_CHANGED | BLOCK_ABORT);
@@ -754,6 +791,7 @@ errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
        errcode_t       retval;
        struct expand_dir_struct es;
        struct ext2_inode       inode;
+       blk64_t         sz;
 
        if (!(fs->flags & EXT2_FLAG_RW))
                return EXT2_ET_RO_FILSYS;
@@ -775,7 +813,7 @@ errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
        es.newblocks = 0;
        es.ctx = ctx;
 
-       retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
+       retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_APPEND,
                                       0, expand_dir_proc, &es);
 
        if (es.err)
@@ -788,8 +826,12 @@ errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
        if (retval)
                return retval;
 
-       inode.i_size = (es.last_block + 1) * fs->blocksize;
+       sz = (es.last_block + 1) * fs->blocksize;
+       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);
 
        e2fsck_write_inode(ctx, dir, &inode, "expand_directory");