Whamcloud - gitweb
Speed up journal recovery in userspace by avoiding a pointless
[tools/e2fsprogs.git] / lib / ext2fs / closefs.c
index dca4a1a..f52c343 100644 (file)
 #include <time.h>
 #include <string.h>
 
-#if EXT2_FLAT_INCLUDES
 #include "ext2_fs.h"
-#else
-#include <linux/ext2_fs.h>
-#endif
-
 #include "ext2fsP.h"
 
 static int test_root(int a, int b)
@@ -39,11 +34,8 @@ static int test_root(int a, int b)
 
 int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
 {
-#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
-       struct ext2fs_sb        *s;
-
-       s = (struct ext2fs_sb *) fs->super;
-       if (!(s->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
+       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)) ||
@@ -51,14 +43,79 @@ int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
                return 1;
        
        return 0;
-#else
-       return 1;
+}
+
+/*
+ * This function forces out the primary superblock.  We need to only
+ * write out those fields which we have changed, since if the
+ * filesystem is mounted, it may have changed some of the other
+ * fields.
+ *
+ * It takes as input a superblock which has already been byte swapped
+ * (if necessary).
+ *
+ */
+static errcode_t write_primary_superblock(ext2_filsys fs,
+                                         struct ext2_super_block *super)
+{
+       __u16           *old_super, *new_super;
+       int             check_idx, write_idx, size;
+       errcode_t       retval;
+
+       if (!fs->io->manager->write_byte || !fs->orig_super) {
+               io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
+               retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
+                                             super);
+               io_channel_set_blksize(fs->io, fs->blocksize);
+               return retval;
+       }
+
+       old_super = (__u16 *) fs->orig_super;
+       new_super = (__u16 *) super;
+
+       for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
+               if (old_super[check_idx] == new_super[check_idx])
+                       continue;
+               write_idx = check_idx;
+               for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
+                       if (old_super[check_idx] == new_super[check_idx])
+                               break;
+               size = 2 * (check_idx - write_idx);
+#if 0
+               printf("Writing %d bytes starting at %d\n",
+                      size, write_idx*2);
 #endif
+               retval = io_channel_write_byte(fs->io,
+                              SUPERBLOCK_OFFSET + (2 * write_idx), size,
+                                              new_super + write_idx);
+               if (retval)
+                       return retval;
+       }
+       memcpy(fs->orig_super, super, SUPERBLOCK_SIZE);
+       return 0;
+}
+
+
+/*
+ * Updates the revision to EXT2_DYNAMIC_REV
+ */
+void ext2fs_update_dynamic_rev(ext2_filsys fs)
+{
+       struct ext2_super_block *sb = fs->super;
+
+       if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
+               return;
+
+       sb->s_rev_level = EXT2_DYNAMIC_REV;
+       sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
+       sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
+       /* s_uuid is handled by e2fsck already */
+       /* other fields should be left alone */
 }
 
 errcode_t ext2fs_flush(ext2_filsys fs)
 {
-       dgrp_t          i,j,maxgroup;
+       dgrp_t          i,j,maxgroup,sgrp;
        blk_t           group_block;
        errcode_t       retval;
        char            *group_ptr;
@@ -72,6 +129,8 @@ errcode_t ext2fs_flush(ext2_filsys fs)
        fs_state = fs->super->s_state;
 
        fs->super->s_wtime = time(NULL);
+       fs->super->s_block_group_nr = 0;
+#ifdef EXT2FS_ENABLE_SWAPFS
        if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
                retval = EXT2_ET_NO_MEMORY;
                retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
@@ -100,18 +159,29 @@ errcode_t ext2fs_flush(ext2_filsys fs)
                super_shadow = fs->super;
                group_shadow = fs->group_desc;
        }
+#else
+       super_shadow = fs->super;
+       group_shadow = fs->group_desc;
+#endif
        
        /*
         * Write out master superblock.  This has to be done
         * separately, since it is located at a fixed location
         * (SUPERBLOCK_OFFSET).
         */
-       io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
-       retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
-                                     super_shadow);
+       retval = write_primary_superblock(fs, super_shadow);
        if (retval)
                goto errout;
-       io_channel_set_blksize(fs->io, fs->blocksize);
+
+       /*
+        * 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) {
+               retval = 0;
+               goto errout;
+       }
 
        /*
         * Set the state of the FS to be non-valid.  (The state has
@@ -119,10 +189,12 @@ errcode_t ext2fs_flush(ext2_filsys fs)
         * we exit.)
         */
        fs->super->s_state &= ~EXT2_VALID_FS;
+#ifdef EXT2FS_ENABLE_SWAPFS
        if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
                *super_shadow = *fs->super;
                ext2fs_swap_super(super_shadow);
        }
+#endif
 
        /*
         * Write out the master group descriptors, and the backup
@@ -135,6 +207,16 @@ errcode_t ext2fs_flush(ext2_filsys fs)
                if (!ext2fs_bg_has_super(fs, i))
                        goto next_group;
 
+               sgrp = i;
+               if (sgrp > ((1 << 16) - 1))
+                       sgrp = (1 << 16) - 1;
+#ifdef EXT2FS_ENABLE_SWAPFS
+               if (fs->flags & EXT2_FLAG_SWAP_BYTES)
+                       super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
+               else
+#endif
+                       fs->super->s_block_group_nr = sgrp;
+
                if (i !=0 ) {
                        retval = io_channel_write_blk(fs->io, group_block,
                                                      -SUPERBLOCK_SIZE,
@@ -142,6 +224,8 @@ errcode_t ext2fs_flush(ext2_filsys fs)
                        if (retval)
                                goto errout;
                }
+               if (fs->flags & EXT2_FLAG_SUPER_ONLY)
+                       goto next_group;
                group_ptr = (char *) group_shadow;
                for (j=0; j < fs->desc_blocks; j++) {
                        retval = io_channel_write_blk(fs->io,
@@ -154,6 +238,7 @@ errcode_t ext2fs_flush(ext2_filsys fs)
        next_group:
                group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
        }
+       fs->super->s_block_group_nr = 0;
 
        /*
         * If the write_bitmaps() function is present, call it to
@@ -167,6 +252,8 @@ errcode_t ext2fs_flush(ext2_filsys fs)
                        goto errout;
        }
 
+       fs->flags &= ~EXT2_FLAG_DIRTY;
+
        /*
         * Flush the blocks out to disk
         */