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)
61 dgrp_t i,j,maxgroup,sgrp;
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;
69 struct ext2fs_sb *sb, *sb_shadow;
71 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
73 fs_state = fs->super->s_state;
75 fs->super->s_wtime = time(NULL);
76 sb = (struct ext2fs_sb *) fs->super;
77 sb->s_block_group_nr = 0;
78 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
79 retval = EXT2_ET_NO_MEMORY;
80 retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
81 (void **) &super_shadow);
84 retval = ext2fs_get_mem((size_t)(fs->blocksize *
86 (void **) &group_shadow);
89 memset(group_shadow, 0, (size_t) fs->blocksize *
92 /* swap the superblock */
93 *super_shadow = *fs->super;
94 ext2fs_swap_super(super_shadow);
95 sb_shadow = (struct ext2fs_sb *) super_shadow;
97 /* swap the group descriptors */
98 for (j=0, s=fs->group_desc, t=group_shadow;
99 j < fs->group_desc_count; j++, t++, s++) {
101 ext2fs_swap_group_desc(t);
104 super_shadow = fs->super;
105 sb_shadow = (struct ext2fs_sb *) fs->super;
106 group_shadow = fs->group_desc;
110 * Write out master superblock. This has to be done
111 * separately, since it is located at a fixed location
112 * (SUPERBLOCK_OFFSET).
114 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
115 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
119 io_channel_set_blksize(fs->io, fs->blocksize);
122 * Set the state of the FS to be non-valid. (The state has
123 * already been backed up earlier, and will be restored when
126 fs->super->s_state &= ~EXT2_VALID_FS;
127 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
128 *super_shadow = *fs->super;
129 ext2fs_swap_super(super_shadow);
133 * Write out the master group descriptors, and the backup
134 * superblocks and group descriptors.
136 group_block = fs->super->s_first_data_block;
137 maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
138 fs->group_desc_count;
139 for (i = 0; i < maxgroup; i++) {
140 if (!ext2fs_bg_has_super(fs, i))
144 if (sgrp > ((1 << 16) - 1))
145 sgrp = (1 << 16) - 1;
146 if (fs->flags & EXT2_FLAG_SWAP_BYTES)
147 sb_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
149 sb->s_block_group_nr = sgrp;
152 retval = io_channel_write_blk(fs->io, group_block,
158 group_ptr = (char *) group_shadow;
159 for (j=0; j < fs->desc_blocks; j++) {
160 retval = io_channel_write_blk(fs->io,
165 group_ptr += fs->blocksize;
168 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
170 sb->s_block_group_nr = 0;
173 * If the write_bitmaps() function is present, call it to
174 * flush the bitmaps. This is done this way so that a simple
175 * program that doesn't mess with the bitmaps doesn't need to
176 * drag in the bitmaps.c code.
178 if (fs->write_bitmaps) {
179 retval = fs->write_bitmaps(fs);
185 * Flush the blocks out to disk
187 retval = io_channel_flush(fs->io);
189 fs->super->s_state = fs_state;
190 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
192 ext2fs_free_mem((void **) &super_shadow);
194 ext2fs_free_mem((void **) &group_shadow);
199 errcode_t ext2fs_close(ext2_filsys fs)
203 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
205 if (fs->flags & EXT2_FLAG_DIRTY) {
206 retval = ext2fs_flush(fs);
210 if (fs->write_bitmaps) {
211 retval = fs->write_bitmaps(fs);