Whamcloud - gitweb
e2fsck: force all block allocations to use block_found_map
[tools/e2fsprogs.git] / e2fsck / pass3.c
index a14bc5d..f9d4aae 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
@@ -60,10 +61,7 @@ void e2fsck_pass3(e2fsck_t ctx)
        struct dir_info *dir;
        unsigned long maxdirs, count;
 
-#ifdef RESOURCE_TRACK
        init_resource_track(&rtrack, ctx->fs->io);
-#endif
-
        clear_problem_context(&pctx);
 
 #ifdef MTRACE
@@ -76,27 +74,22 @@ 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);
                ctx->flags |= E2F_FLAG_ABORT;
                goto abort_exit;
        }
-#ifdef RESOURCE_TRACK
-       if (ctx->options & E2F_OPT_TIME) {
-               e2fsck_clear_progbar(ctx);
-               print_resource_track(_("Peak memory"), &ctx->global_rtrack,
-                                    NULL);
-       }
-#endif
+       print_resource_track(ctx, _("Peak memory"), &ctx->global_rtrack, NULL);
 
        check_root(ctx);
        if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                goto abort_exit;
 
-       ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
+       ext2fs_mark_inode_bitmap2(inode_done_map, EXT2_ROOT_INO);
 
        maxdirs = e2fsck_get_num_dirinfo(ctx);
        count = 1;
@@ -111,16 +104,15 @@ void e2fsck_pass3(e2fsck_t ctx)
                        goto abort_exit;
                if (ctx->progress && (ctx->progress)(ctx, 3, count++, maxdirs))
                        goto abort_exit;
-               if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
+               if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dir->ino))
                        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);
 
        /*
@@ -130,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);
@@ -140,12 +134,7 @@ abort_exit:
                inode_done_map = 0;
        }
 
-#ifdef RESOURCE_TRACK
-       if (ctx->options & E2F_OPT_TIME2) {
-               e2fsck_clear_progbar(ctx);
-               print_resource_track(_("Pass 3"), &rtrack, ctx->fs->io);
-       }
-#endif
+       print_resource_track(ctx, _("Pass 3"), &rtrack, ctx->fs->io);
 }
 
 /*
@@ -155,20 +144,20 @@ abort_exit:
 static void check_root(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t                   blk;
+       blk64_t                 blk;
        struct ext2_inode       inode;
        char *                  block;
        struct problem_context  pctx;
 
        clear_problem_context(&pctx);
 
-       if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
+       if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO)) {
                /*
                 * If the root inode is not a directory, die here.  The
                 * user must have answered 'no' in pass1 when we
                 * offered to clear it.
                 */
-               if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
+               if (!(ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
                                               EXT2_ROOT_INO))) {
                        fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
                        ctx->flags |= E2F_FLAG_ABORT;
@@ -187,15 +176,15 @@ static void check_root(e2fsck_t ctx)
        /*
         * First, find a free block
         */
-       pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
+       pctx.errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
        if (pctx.errcode) {
                pctx.str = "ext2fs_new_block";
                fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
-       ext2fs_mark_block_bitmap(fs->block_map, blk);
+       ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
+       ext2fs_mark_block_bitmap2(fs->block_map, blk);
        ext2fs_mark_bb_dirty(fs);
 
        /*
@@ -210,9 +199,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;
@@ -248,9 +237,9 @@ static void check_root(e2fsck_t ctx)
        ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
        ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
 
-       ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
-       ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
-       ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
+       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO);
+       ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, EXT2_ROOT_INO);
+       ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_ROOT_INO);
        ext2fs_mark_ib_dirty(fs);
 }
 
@@ -288,7 +277,7 @@ static int check_directory(e2fsck_t ctx, ext2_ino_t dir,
                 * If it was marked done already, then we've reached a
                 * parent we've already checked.
                 */
-               if (ext2fs_mark_inode_bitmap(inode_done_map, ino))
+               if (ext2fs_mark_inode_bitmap2(inode_done_map, ino))
                        break;
 
                if (e2fsck_dir_info_get_parent(ctx, ino, &parent)) {
@@ -303,7 +292,7 @@ static int check_directory(e2fsck_t ctx, ext2_ino_t dir,
                 */
                if (!parent ||
                    (loop_pass &&
-                    (ext2fs_test_inode_bitmap(inode_loop_detect,
+                    (ext2fs_test_inode_bitmap2(inode_loop_detect,
                                               parent)))) {
                        pctx->ino = ino;
                        if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
@@ -319,7 +308,7 @@ static int check_directory(e2fsck_t ctx, ext2_ino_t dir,
                }
                ino = parent;
                if (loop_pass) {
-                       ext2fs_mark_inode_bitmap(inode_loop_detect, ino);
+                       ext2fs_mark_inode_bitmap2(inode_loop_detect, ino);
                } else if (parent_count++ > 2048) {
                        /*
                         * If we've run into a path depth that's
@@ -331,7 +320,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,
@@ -369,7 +358,7 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
 {
        ext2_filsys fs = ctx->fs;
        ext2_ino_t                      ino;
-       blk_t                   blk;
+       blk64_t                 blk;
        errcode_t               retval;
        struct ext2_inode       inode;
        char *                  block;
@@ -386,7 +375,7 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
        if (retval && !fix)
                return 0;
        if (!retval) {
-               if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino)) {
+               if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino)) {
                        ctx->lost_and_found = ino;
                        return ino;
                }
@@ -423,14 +412,14 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
        /*
         * First, find a free block
         */
-       retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
+       retval = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
        if (retval) {
                pctx.errcode = retval;
                fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
                return 0;
        }
-       ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
-       ext2fs_block_alloc_stats(fs, blk, +1);
+       ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
+       ext2fs_block_alloc_stats2(fs, blk, +1);
 
        /*
         * Next find a free inode.
@@ -442,8 +431,8 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
                fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
                return 0;
        }
-       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
-       ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
+       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
+       ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
        ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
 
        /*
@@ -456,7 +445,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;
@@ -502,6 +491,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
@@ -689,24 +680,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;
-       blk_t   new_blk;
-       static blk_t    last_blk = 0;
+       blk64_t new_blk;
+       static blk64_t  last_blk = 0;
        char            *block;
        errcode_t       retval;
        e2fsck_t        ctx;
@@ -722,12 +713,23 @@ static int expand_dir_proc(ext2_filsys fs,
                last_blk = *blocknr;
                return 0;
        }
-       retval = ext2fs_new_block(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) {
@@ -735,7 +737,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) {
@@ -743,7 +745,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;
@@ -751,9 +753,7 @@ static int expand_dir_proc(ext2_filsys fs,
        }
        ext2fs_free_mem(&block);
        *blocknr = new_blk;
-       ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
-       ext2fs_block_alloc_stats(fs, new_blk, +1);
-       es->newblocks++;
+       ext2fs_mark_block_bitmap2(ctx->block_found_map, new_blk);
 
        if (es->num == 0)
                return (BLOCK_CHANGED | BLOCK_ABORT);
@@ -768,6 +768,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, before, after;
 
        if (!(fs->flags & EXT2_FLAG_RW))
                return EXT2_ET_RO_FILSYS;
@@ -789,7 +790,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)
@@ -802,8 +803,11 @@ 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;
+       inode.i_size = sz;
+       inode.i_size_high = sz >> 32;
        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");