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))
60 * This function forces out the primary superblock. We need to only
61 * write out those fields which we have changed, since if the
62 * filesystem is mounted, it may have changed some of the other
65 * It takes as input a superblock which has already been byte swapped
69 static errcode_t write_primary_superblock(ext2_filsys fs,
70 struct ext2_super_block *super)
72 __u16 *old_super, *new_super;
73 int check_idx, write_idx, size;
76 if (!fs->io->manager->write_byte || !fs->orig_super) {
77 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
78 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
80 io_channel_set_blksize(fs->io, fs->blocksize);
84 old_super = (__u16 *) fs->orig_super;
85 new_super = (__u16 *) super;
87 for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
88 if (old_super[check_idx] == new_super[check_idx])
90 write_idx = check_idx;
91 for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
92 if (old_super[check_idx] == new_super[check_idx])
94 size = 2 * (check_idx - write_idx);
96 printf("Writing %d bytes starting at %d\n",
99 retval = io_channel_write_byte(fs->io,
100 SUPERBLOCK_OFFSET + (2 * write_idx), size,
101 new_super + write_idx);
110 * Updates the revision to EXT2_DYNAMIC_REV
112 void ext2fs_update_fs_dynamic_rev(ext2_filsys fs)
114 struct ext2_super_block *sb = fs->super;
116 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
119 sb->s_rev_level = EXT2_DYNAMIC_REV;
120 sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
121 sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
122 /* s_uuid is handled by e2fsck already */
123 /* other fields should be left alone */
126 errcode_t ext2fs_flush(ext2_filsys fs)
128 dgrp_t i,j,maxgroup,sgrp;
132 unsigned long fs_state;
133 struct ext2_super_block *super_shadow = 0;
134 struct ext2_group_desc *group_shadow = 0;
135 struct ext2_group_desc *s, *t;
136 struct ext2fs_sb *sb, *sb_shadow;
138 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
140 fs_state = fs->super->s_state;
142 fs->super->s_wtime = time(NULL);
143 sb = (struct ext2fs_sb *) fs->super;
144 sb->s_block_group_nr = 0;
145 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
146 retval = EXT2_ET_NO_MEMORY;
147 retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
148 (void **) &super_shadow);
151 retval = ext2fs_get_mem((size_t)(fs->blocksize *
153 (void **) &group_shadow);
156 memset(group_shadow, 0, (size_t) fs->blocksize *
159 /* swap the superblock */
160 *super_shadow = *fs->super;
161 ext2fs_swap_super(super_shadow);
162 sb_shadow = (struct ext2fs_sb *) super_shadow;
164 /* swap the group descriptors */
165 for (j=0, s=fs->group_desc, t=group_shadow;
166 j < fs->group_desc_count; j++, t++, s++) {
168 ext2fs_swap_group_desc(t);
171 super_shadow = fs->super;
172 sb_shadow = (struct ext2fs_sb *) fs->super;
173 group_shadow = fs->group_desc;
177 * Write out master superblock. This has to be done
178 * separately, since it is located at a fixed location
179 * (SUPERBLOCK_OFFSET).
181 retval = write_primary_superblock(fs, super_shadow);
186 * Set the state of the FS to be non-valid. (The state has
187 * already been backed up earlier, and will be restored when
190 fs->super->s_state &= ~EXT2_VALID_FS;
191 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
192 *super_shadow = *fs->super;
193 ext2fs_swap_super(super_shadow);
197 * Write out the master group descriptors, and the backup
198 * superblocks and group descriptors.
200 group_block = fs->super->s_first_data_block;
201 maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
202 fs->group_desc_count;
203 for (i = 0; i < maxgroup; i++) {
204 if (!ext2fs_bg_has_super(fs, i))
208 if (sgrp > ((1 << 16) - 1))
209 sgrp = (1 << 16) - 1;
210 if (fs->flags & EXT2_FLAG_SWAP_BYTES)
211 sb_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
213 sb->s_block_group_nr = sgrp;
216 retval = io_channel_write_blk(fs->io, group_block,
222 group_ptr = (char *) group_shadow;
223 for (j=0; j < fs->desc_blocks; j++) {
224 retval = io_channel_write_blk(fs->io,
229 group_ptr += fs->blocksize;
232 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
234 sb->s_block_group_nr = 0;
237 * If the write_bitmaps() function is present, call it to
238 * flush the bitmaps. This is done this way so that a simple
239 * program that doesn't mess with the bitmaps doesn't need to
240 * drag in the bitmaps.c code.
242 if (fs->write_bitmaps) {
243 retval = fs->write_bitmaps(fs);
249 * Flush the blocks out to disk
251 retval = io_channel_flush(fs->io);
253 fs->super->s_state = fs_state;
254 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
256 ext2fs_free_mem((void **) &super_shadow);
258 ext2fs_free_mem((void **) &group_shadow);
263 errcode_t ext2fs_close(ext2_filsys fs)
267 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
269 if (fs->flags & EXT2_FLAG_DIRTY) {
270 retval = ext2fs_flush(fs);
274 if (fs->write_bitmaps) {
275 retval = fs->write_bitmaps(fs);