Whamcloud - gitweb
Add an option to debugfs to open filesystems in exclusive mode
[tools/e2fsprogs.git] / lib / ext2fs / closefs.c
index 6fe012c..58f1a8b 100644 (file)
 #include "ext2_fs.h"
 #include "ext2fsP.h"
 
+static int test_root(int a, int b)
+{
+       if (a == 0)
+               return 1;
+       while (1) {
+               if (a == 1)
+                       return 1;
+               if (a % b)
+                       return 0;
+               a = a / b;
+       }
+}
+
+int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
+{
+       if (!(fs->super->s_feature_ro_compat &
+             EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
+               return 1;
+
+       if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
+           test_root(group_block, 7))
+               return 1;
+
+       return 0;
+}
+
 int ext2fs_super_and_bgd_loc(ext2_filsys fs, 
                             dgrp_t group,
                             blk_t *ret_super_blk,
@@ -193,7 +219,7 @@ errcode_t ext2fs_flush(ext2_filsys fs)
 
        fs_state = fs->super->s_state;
 
-       fs->super->s_wtime = time(NULL);
+       fs->super->s_wtime = fs->now ? fs->now : time(NULL);
        fs->super->s_block_group_nr = 0;
 #ifdef EXT2FS_ENABLE_SWAPFS
        if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
@@ -225,14 +251,6 @@ errcode_t ext2fs_flush(ext2_filsys fs)
 #endif
        
        /*
-        * If this is an external journal device, don't write out the
-        * block group descriptors or any of the backup superblocks
-        */
-       if (fs->super->s_feature_incompat &
-           EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
-               goto write_primary_superblock_only;
-
-       /*
         * Set the state of the FS to be non-valid.  (The state has
         * already been backed up earlier, and will be restored after
         * we write out the backup superblocks.)
@@ -246,6 +264,14 @@ errcode_t ext2fs_flush(ext2_filsys fs)
 #endif
 
        /*
+        * If this is an external journal device, don't write out the
+        * block group descriptors or any of the backup superblocks
+        */
+       if (fs->super->s_feature_incompat &
+           EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
+               goto write_primary_superblock_only;
+
+       /*
         * Write out the master group descriptors, and the backup
         * superblocks and group descriptors.
         */
@@ -285,14 +311,6 @@ errcode_t ext2fs_flush(ext2_filsys fs)
                                goto errout;
                }
        }
-       fs->super->s_block_group_nr = 0;
-       fs->super->s_state = fs_state;
-#ifdef EXT2FS_ENABLE_SWAPFS
-       if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
-               *super_shadow = *fs->super;
-               ext2fs_swap_super(super_shadow);
-       }
-#endif
 
        /*
         * If the write_bitmaps() function is present, call it to
@@ -314,6 +332,16 @@ write_primary_superblock_only:
         * out to disk first, just to avoid a race condition with an
         * insy-tinsy window....
         */
+
+       fs->super->s_block_group_nr = 0;
+       fs->super->s_state = fs_state;
+#ifdef EXT2FS_ENABLE_SWAPFS
+       if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
+               *super_shadow = *fs->super;
+               ext2fs_swap_super(super_shadow);
+       }
+#endif
+
        retval = io_channel_flush(fs->io);
        retval = write_primary_superblock(fs, super_shadow);
        if (retval)