Whamcloud - gitweb
e2fsck: fix incorrect interior node logical start values
[tools/e2fsprogs.git] / lib / ext2fs / closefs.c
index c505f00..973c2a2 100644 (file)
@@ -4,11 +4,12 @@
  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #if HAVE_UNISTD_H
 #include <unistd.h>
@@ -73,6 +74,8 @@ errcode_t ext2fs_super_and_bgd_loc2(ext2_filsys fs,
        int     has_super;
 
        group_block = ext2fs_group_first_block2(fs, group);
+       if (group_block == 0 && fs->blocksize == 1024)
+               group_block = 1; /* Deal with 1024 blocksize && bigalloc */
 
        if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
                old_desc_blocks = fs->super->s_first_meta_bg;
@@ -150,14 +153,7 @@ int ext2fs_super_and_bgd_loc(ext2_filsys fs,
                                        &ret_new_desc_blk2,
                                        &ret_used_blks);
 
-       if (group == fs->group_desc_count-1) {
-               numblocks = (ext2fs_blocks_count(fs->super) -
-                            (blk64_t) fs->super->s_first_data_block) %
-                       (blk64_t) fs->super->s_blocks_per_group;
-               if (!numblocks)
-                       numblocks = fs->super->s_blocks_per_group;
-       } else
-               numblocks = fs->super->s_blocks_per_group;
+       numblocks = ext2fs_group_blocks_count(fs, group);
 
        if (ret_super_blk)
                *ret_super_blk = (blk_t)ret_super_blk2;
@@ -193,6 +189,7 @@ static errcode_t write_primary_superblock(ext2_filsys fs,
        errcode_t       retval;
 
        if (!fs->io->manager->write_byte || !fs->orig_super) {
+       fallback:
                io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
                retval = io_channel_write_blk64(fs->io, 1, -SUPERBLOCK_SIZE,
                                              super);
@@ -218,6 +215,8 @@ static errcode_t write_primary_superblock(ext2_filsys fs,
                retval = io_channel_write_byte(fs->io,
                               SUPERBLOCK_OFFSET + (2 * write_idx), size,
                                               new_super + write_idx);
+               if (retval == EXT2_ET_UNIMPLEMENTED)
+                       goto fallback;
                if (retval)
                        return retval;
        }
@@ -263,6 +262,11 @@ static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
 
 errcode_t ext2fs_flush(ext2_filsys fs)
 {
+       return ext2fs_flush2(fs, 0);
+}
+
+errcode_t ext2fs_flush2(ext2_filsys fs, int flags)
+{
        dgrp_t          i;
        errcode_t       retval;
        unsigned long   fs_state;
@@ -270,7 +274,7 @@ errcode_t ext2fs_flush(ext2_filsys fs)
        struct ext2_super_block *super_shadow = 0;
        struct ext2_group_desc *group_shadow = 0;
 #ifdef WORDS_BIGENDIAN
-       struct ext2_group_desc *s, *t;
+       struct ext2_group_desc *gdp;
        dgrp_t          j;
 #endif
        char    *group_ptr;
@@ -293,18 +297,17 @@ errcode_t ext2fs_flush(ext2_filsys fs)
                                  &group_shadow);
        if (retval)
                goto errout;
-       memset(group_shadow, 0, (size_t) fs->blocksize *
+       memcpy(group_shadow, fs->group_desc, (size_t) fs->blocksize *
               fs->desc_blocks);
 
        /* swap the group descriptors */
-       for (j=0, s=fs->group_desc, t=group_shadow;
-            j < fs->group_desc_count; j++, t++, s++) {
-               *t = *s;
-               ext2fs_swap_group_desc(t);
+       for (j=0; j < fs->group_desc_count; j++) {
+               gdp = ext2fs_group_desc(fs, group_shadow, j);
+               ext2fs_swap_group_desc2(fs, gdp);
        }
 #else
        super_shadow = fs->super;
-       group_shadow = fs->group_desc;
+       group_shadow = ext2fs_group_desc(fs, fs->group_desc, 0);
 #endif
 
        /*
@@ -404,14 +407,16 @@ write_primary_superblock_only:
        ext2fs_swap_super(super_shadow);
 #endif
 
-       retval = io_channel_flush(fs->io);
+       if (!(flags & EXT2_FLAG_FLUSH_NO_SYNC))
+               retval = io_channel_flush(fs->io);
        retval = write_primary_superblock(fs, super_shadow);
        if (retval)
                goto errout;
 
        fs->flags &= ~EXT2_FLAG_DIRTY;
 
-       retval = io_channel_flush(fs->io);
+       if (!(flags & EXT2_FLAG_FLUSH_NO_SYNC))
+               retval = io_channel_flush(fs->io);
 errout:
        fs->super->s_state = fs_state;
 #ifdef WORDS_BIGENDIAN
@@ -425,6 +430,11 @@ errout:
 
 errcode_t ext2fs_close(ext2_filsys fs)
 {
+       return ext2fs_close2(fs, 0);
+}
+
+errcode_t ext2fs_close2(ext2_filsys fs, int flags)
+{
        errcode_t       retval;
        int             meta_blks;
        io_stats stats = 0;
@@ -449,10 +459,15 @@ errcode_t ext2fs_close(ext2_filsys fs)
                        fs->flags |= EXT2_FLAG_SUPER_ONLY | EXT2_FLAG_DIRTY;
        }
        if (fs->flags & EXT2_FLAG_DIRTY) {
-               retval = ext2fs_flush(fs);
+               retval = ext2fs_flush2(fs, flags);
                if (retval)
                        return retval;
        }
+
+       retval = ext2fs_mmp_stop(fs);
+       if (retval)
+               return retval;
+
        ext2fs_free(fs);
        return 0;
 }