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
20 #include <linux/ext2_fs.h>
24 static int test_root(int a, int b)
37 int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
39 #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
42 s = (struct ext2fs_sb *) fs->super;
43 if (!(s->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
46 if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
47 test_root(group_block, 7))
56 errcode_t ext2fs_flush(ext2_filsys fs)
62 unsigned long fs_state;
63 struct ext2_super_block *super_shadow = 0;
64 struct ext2_group_desc *group_shadow = 0;
65 struct ext2_group_desc *s, *t;
67 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
69 fs_state = fs->super->s_state;
71 fs->super->s_wtime = time(NULL);
72 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
73 retval = EXT2_ET_NO_MEMORY;
74 retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
75 (void **) &super_shadow);
78 retval = ext2fs_get_mem((size_t)(fs->blocksize *
80 (void **) &group_shadow);
83 memset(group_shadow, 0, (size_t) fs->blocksize *
86 /* swap the superblock */
87 *super_shadow = *fs->super;
88 ext2fs_swap_super(super_shadow);
90 /* swap the group descriptors */
91 for (j=0, s=fs->group_desc, t=group_shadow;
92 j < fs->group_desc_count; j++, t++, s++) {
94 ext2fs_swap_group_desc(t);
97 super_shadow = fs->super;
98 group_shadow = fs->group_desc;
102 * Write out master superblock. This has to be done
103 * separately, since it is located at a fixed location
104 * (SUPERBLOCK_OFFSET).
106 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
107 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
111 io_channel_set_blksize(fs->io, fs->blocksize);
114 * Set the state of the FS to be non-valid. (The state has
115 * already been backed up earlier, and will be restored when
118 fs->super->s_state &= ~EXT2_VALID_FS;
119 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
120 *super_shadow = *fs->super;
121 ext2fs_swap_super(super_shadow);
125 * Write out the master group descriptors, and the backup
126 * superblocks and group descriptors.
128 group_block = fs->super->s_first_data_block;
129 maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
130 fs->group_desc_count;
131 for (i = 0; i < maxgroup; i++) {
132 if (!ext2fs_bg_has_super(fs, i))
136 retval = io_channel_write_blk(fs->io, group_block,
142 group_ptr = (char *) group_shadow;
143 for (j=0; j < fs->desc_blocks; j++) {
144 retval = io_channel_write_blk(fs->io,
149 group_ptr += fs->blocksize;
152 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
156 * If the write_bitmaps() function is present, call it to
157 * flush the bitmaps. This is done this way so that a simple
158 * program that doesn't mess with the bitmaps doesn't need to
159 * drag in the bitmaps.c code.
161 if (fs->write_bitmaps) {
162 retval = fs->write_bitmaps(fs);
168 fs->super->s_state = fs_state;
169 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
171 ext2fs_free_mem((void **) &super_shadow);
173 ext2fs_free_mem((void **) &group_shadow);
178 errcode_t ext2fs_close(ext2_filsys fs)
182 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
184 if (fs->flags & EXT2_FLAG_DIRTY) {
185 retval = ext2fs_flush(fs);
189 if (fs->write_bitmaps) {
190 retval = fs->write_bitmaps(fs);