Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / lib / ext2fs / closefs.c
index 3e4af7f..0483e05 100644 (file)
@@ -247,15 +247,19 @@ static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
                                    blk64_t group_block,
                                    struct ext2_super_block *super_shadow)
 {
+       errcode_t retval;
        dgrp_t  sgrp = group;
 
        if (sgrp > ((1 << 16) - 1))
                sgrp = (1 << 16) - 1;
+
+       super_shadow->s_block_group_nr = sgrp;
 #ifdef WORDS_BIGENDIAN
-       super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
-#else
-       fs->super->s_block_group_nr = sgrp;
+       ext2fs_swap_super(super_shadow);
 #endif
+       retval = ext2fs_superblock_csum_set(fs, super_shadow);
+       if (retval)
+               return retval;
 
        return io_channel_write_blk64(fs->io, group_block, -SUPERBLOCK_SIZE,
                                    super_shadow);
@@ -289,6 +293,23 @@ errcode_t ext2fs_flush2(ext2_filsys fs, int flags)
 
        fs->super->s_wtime = fs->now ? fs->now : time(NULL);
        fs->super->s_block_group_nr = 0;
+
+       /*
+        * If the write_bitmaps() function is present, call it to
+        * flush the bitmaps.  This is done this way so that a simple
+        * program that doesn't mess with the bitmaps doesn't need to
+        * drag in the bitmaps.c code.
+        *
+        * Bitmap checksums live in the group descriptor, so the
+        * bitmaps need to be written before the descriptors.
+        */
+       if (fs->write_bitmaps) {
+               retval = fs->write_bitmaps(fs);
+               if (retval)
+                       goto errout;
+       }
+
+       /* Prepare the group descriptors for writing */
 #ifdef WORDS_BIGENDIAN
        retval = EXT2_ET_NO_MEMORY;
        retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow);
@@ -298,6 +319,7 @@ errcode_t ext2fs_flush2(ext2_filsys fs, int flags)
                                  &group_shadow);
        if (retval)
                goto errout;
+       memcpy(super_shadow, fs->super, sizeof(struct ext2_super_block));
        memcpy(group_shadow, fs->group_desc, (size_t) fs->blocksize *
               fs->desc_blocks);
 
@@ -318,10 +340,6 @@ errcode_t ext2fs_flush2(ext2_filsys fs, int flags)
         */
        fs->super->s_state &= ~EXT2_VALID_FS;
        fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
-#ifdef WORDS_BIGENDIAN
-       *super_shadow = *fs->super;
-       ext2fs_swap_super(super_shadow);
-#endif
 
        /*
         * If this is an external journal device, don't write out the
@@ -341,14 +359,16 @@ errcode_t ext2fs_flush2(ext2_filsys fs, int flags)
        else
                old_desc_blocks = fs->desc_blocks;
 
-       ext2fs_numeric_progress_init(fs, &progress, NULL,
-                                    fs->group_desc_count);
+       if (fs->progress_ops && fs->progress_ops->init)
+               (fs->progress_ops->init)(fs, &progress, NULL,
+                                        fs->group_desc_count);
 
 
        for (i = 0; i < fs->group_desc_count; i++) {
                blk64_t super_blk, old_desc_blk, new_desc_blk;
 
-               ext2fs_numeric_progress_update(fs, &progress, i);
+               if (fs->progress_ops && fs->progress_ops->update)
+                       (fs->progress_ops->update)(fs, &progress, i);
                ext2fs_super_and_bgd_loc2(fs, i, &super_blk, &old_desc_blk,
                                         &new_desc_blk, 0);
 
@@ -377,19 +397,8 @@ errcode_t ext2fs_flush2(ext2_filsys fs, int flags)
                }
        }
 
-       ext2fs_numeric_progress_close(fs, &progress, NULL);
-
-       /*
-        * If the write_bitmaps() function is present, call it to
-        * flush the bitmaps.  This is done this way so that a simple
-        * program that doesn't mess with the bitmaps doesn't need to
-        * drag in the bitmaps.c code.
-        */
-       if (fs->write_bitmaps) {
-               retval = fs->write_bitmaps(fs);
-               if (retval)
-                       goto errout;
-       }
+       if (fs->progress_ops && fs->progress_ops->close)
+               (fs->progress_ops->close)(fs, &progress, NULL);
 
 write_primary_superblock_only:
        /*
@@ -408,6 +417,10 @@ write_primary_superblock_only:
        ext2fs_swap_super(super_shadow);
 #endif
 
+       retval = ext2fs_superblock_csum_set(fs, super_shadow);
+       if (retval)
+               return retval;
+
        if (!(flags & EXT2_FLAG_FLUSH_NO_SYNC))
                retval = io_channel_flush(fs->io);
        retval = write_primary_superblock(fs, super_shadow);