2 * closefs.c --- close an ext2 filesystem
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
19 #if EXT2_FLAT_INCLUDES
22 #include <linux/ext2_fs.h>
27 static int test_root(int a, int b)
40 int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
42 #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
45 s = (struct ext2fs_sb *) fs->super;
46 if (!(s->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
49 if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
50 test_root(group_block, 7))
59 errcode_t ext2fs_flush(ext2_filsys fs)
65 unsigned long fs_state;
66 struct ext2_super_block *super_shadow = 0;
67 struct ext2_group_desc *group_shadow = 0;
68 struct ext2_group_desc *s, *t;
70 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
72 fs_state = fs->super->s_state;
74 fs->super->s_wtime = time(NULL);
75 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
76 retval = EXT2_ET_NO_MEMORY;
77 retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
78 (void **) &super_shadow);
81 retval = ext2fs_get_mem((size_t)(fs->blocksize *
83 (void **) &group_shadow);
86 memset(group_shadow, 0, (size_t) fs->blocksize *
89 /* swap the superblock */
90 *super_shadow = *fs->super;
91 ext2fs_swap_super(super_shadow);
93 /* swap the group descriptors */
94 for (j=0, s=fs->group_desc, t=group_shadow;
95 j < fs->group_desc_count; j++, t++, s++) {
97 ext2fs_swap_group_desc(t);
100 super_shadow = fs->super;
101 group_shadow = fs->group_desc;
105 * Write out master superblock. This has to be done
106 * separately, since it is located at a fixed location
107 * (SUPERBLOCK_OFFSET).
109 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
110 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
114 io_channel_set_blksize(fs->io, fs->blocksize);
117 * Set the state of the FS to be non-valid. (The state has
118 * already been backed up earlier, and will be restored when
121 fs->super->s_state &= ~EXT2_VALID_FS;
122 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
123 *super_shadow = *fs->super;
124 ext2fs_swap_super(super_shadow);
128 * Write out the master group descriptors, and the backup
129 * superblocks and group descriptors.
131 group_block = fs->super->s_first_data_block;
132 maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
133 fs->group_desc_count;
134 for (i = 0; i < maxgroup; i++) {
135 if (!ext2fs_bg_has_super(fs, i))
139 retval = io_channel_write_blk(fs->io, group_block,
145 group_ptr = (char *) group_shadow;
146 for (j=0; j < fs->desc_blocks; j++) {
147 retval = io_channel_write_blk(fs->io,
152 group_ptr += fs->blocksize;
155 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
159 * If the write_bitmaps() function is present, call it to
160 * flush the bitmaps. This is done this way so that a simple
161 * program that doesn't mess with the bitmaps doesn't need to
162 * drag in the bitmaps.c code.
164 if (fs->write_bitmaps) {
165 retval = fs->write_bitmaps(fs);
171 * Flush the blocks out to disk
173 retval = io_channel_flush(fs->io);
175 fs->super->s_state = fs_state;
176 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
178 ext2fs_free_mem((void **) &super_shadow);
180 ext2fs_free_mem((void **) &group_shadow);
185 errcode_t ext2fs_close(ext2_filsys fs)
189 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
191 if (fs->flags & EXT2_FLAG_DIRTY) {
192 retval = ext2fs_flush(fs);
196 if (fs->write_bitmaps) {
197 retval = fs->write_bitmaps(fs);