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 if (!(fs->super->s_feature_ro_compat &
43 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
46 if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
47 test_root(group_block, 7))
54 * This function forces out the primary superblock. We need to only
55 * write out those fields which we have changed, since if the
56 * filesystem is mounted, it may have changed some of the other
59 * It takes as input a superblock which has already been byte swapped
63 static errcode_t write_primary_superblock(ext2_filsys fs,
64 struct ext2_super_block *super)
66 __u16 *old_super, *new_super;
67 int check_idx, write_idx, size;
70 if (!fs->io->manager->write_byte || !fs->orig_super) {
71 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
72 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
74 io_channel_set_blksize(fs->io, fs->blocksize);
78 old_super = (__u16 *) fs->orig_super;
79 new_super = (__u16 *) super;
81 for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
82 if (old_super[check_idx] == new_super[check_idx])
84 write_idx = check_idx;
85 for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
86 if (old_super[check_idx] == new_super[check_idx])
88 size = 2 * (check_idx - write_idx);
90 printf("Writing %d bytes starting at %d\n",
93 retval = io_channel_write_byte(fs->io,
94 SUPERBLOCK_OFFSET + (2 * write_idx), size,
95 new_super + write_idx);
104 * Updates the revision to EXT2_DYNAMIC_REV
106 void ext2fs_update_dynamic_rev(ext2_filsys fs)
108 struct ext2_super_block *sb = fs->super;
110 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
113 sb->s_rev_level = EXT2_DYNAMIC_REV;
114 sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
115 sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
116 /* s_uuid is handled by e2fsck already */
117 /* other fields should be left alone */
120 errcode_t ext2fs_flush(ext2_filsys fs)
122 dgrp_t i,j,maxgroup,sgrp;
126 unsigned long fs_state;
127 struct ext2_super_block *super_shadow = 0;
128 struct ext2_group_desc *group_shadow = 0;
129 struct ext2_group_desc *s, *t;
131 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
133 fs_state = fs->super->s_state;
135 fs->super->s_wtime = time(NULL);
136 fs->super->s_block_group_nr = 0;
137 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
138 retval = EXT2_ET_NO_MEMORY;
139 retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
140 (void **) &super_shadow);
143 retval = ext2fs_get_mem((size_t)(fs->blocksize *
145 (void **) &group_shadow);
148 memset(group_shadow, 0, (size_t) fs->blocksize *
151 /* swap the superblock */
152 *super_shadow = *fs->super;
153 ext2fs_swap_super(super_shadow);
155 /* swap the group descriptors */
156 for (j=0, s=fs->group_desc, t=group_shadow;
157 j < fs->group_desc_count; j++, t++, s++) {
159 ext2fs_swap_group_desc(t);
162 super_shadow = fs->super;
163 group_shadow = fs->group_desc;
167 * Write out master superblock. This has to be done
168 * separately, since it is located at a fixed location
169 * (SUPERBLOCK_OFFSET).
171 retval = write_primary_superblock(fs, super_shadow);
176 * Set the state of the FS to be non-valid. (The state has
177 * already been backed up earlier, and will be restored when
180 fs->super->s_state &= ~EXT2_VALID_FS;
181 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
182 *super_shadow = *fs->super;
183 ext2fs_swap_super(super_shadow);
187 * Write out the master group descriptors, and the backup
188 * superblocks and group descriptors.
190 group_block = fs->super->s_first_data_block;
191 maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
192 fs->group_desc_count;
193 for (i = 0; i < maxgroup; i++) {
194 if (!ext2fs_bg_has_super(fs, i))
198 if (sgrp > ((1 << 16) - 1))
199 sgrp = (1 << 16) - 1;
200 if (fs->flags & EXT2_FLAG_SWAP_BYTES)
201 super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
203 fs->super->s_block_group_nr = sgrp;
206 retval = io_channel_write_blk(fs->io, group_block,
212 if (fs->flags & EXT2_FLAG_SUPER_ONLY)
214 group_ptr = (char *) group_shadow;
215 for (j=0; j < fs->desc_blocks; j++) {
216 retval = io_channel_write_blk(fs->io,
221 group_ptr += fs->blocksize;
224 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
226 fs->super->s_block_group_nr = 0;
229 * If the write_bitmaps() function is present, call it to
230 * flush the bitmaps. This is done this way so that a simple
231 * program that doesn't mess with the bitmaps doesn't need to
232 * drag in the bitmaps.c code.
234 if (fs->write_bitmaps) {
235 retval = fs->write_bitmaps(fs);
241 * Flush the blocks out to disk
243 retval = io_channel_flush(fs->io);
245 fs->super->s_state = fs_state;
246 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
248 ext2fs_free_mem((void **) &super_shadow);
250 ext2fs_free_mem((void **) &group_shadow);
255 errcode_t ext2fs_close(ext2_filsys fs)
259 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
261 if (fs->flags & EXT2_FLAG_DIRTY) {
262 retval = ext2fs_flush(fs);
266 if (fs->write_bitmaps) {
267 retval = fs->write_bitmaps(fs);