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
21 #include <linux/ext2_fs.h>
25 static int test_root(int a, int b)
38 int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
40 #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
43 s = (struct ext2fs_sb *) fs->super;
44 if (!(s->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
47 if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
48 test_root(group_block, 7))
57 errcode_t ext2fs_flush(ext2_filsys fs)
63 unsigned long fs_state;
64 struct ext2_super_block *super_shadow = 0;
65 struct ext2_group_desc *group_shadow = 0;
66 struct ext2_group_desc *s, *t;
68 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
70 fs_state = fs->super->s_state;
72 fs->super->s_wtime = time(NULL);
73 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
75 if (!(super_shadow = malloc(SUPERBLOCK_SIZE)))
77 if (!(group_shadow = malloc(fs->blocksize*fs->desc_blocks)))
79 memset(group_shadow, 0, fs->blocksize*fs->desc_blocks);
81 /* swap the superblock */
82 *super_shadow = *fs->super;
83 ext2fs_swap_super(super_shadow);
85 /* swap the group descriptors */
86 for (j=0, s=fs->group_desc, t=group_shadow;
87 j < fs->group_desc_count; j++, t++, s++) {
89 ext2fs_swap_group_desc(t);
92 super_shadow = fs->super;
93 group_shadow = fs->group_desc;
97 * Write out master superblock. This has to be done
98 * separately, since it is located at a fixed location
99 * (SUPERBLOCK_OFFSET).
101 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
102 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
106 io_channel_set_blksize(fs->io, fs->blocksize);
109 * Set the state of the FS to be non-valid. (The state has
110 * already been backed up earlier, and will be restored when
113 fs->super->s_state &= ~EXT2_VALID_FS;
114 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
115 *super_shadow = *fs->super;
116 ext2fs_swap_super(super_shadow);
120 * Write out the master group descriptors, and the backup
121 * superblocks and group descriptors.
123 group_block = fs->super->s_first_data_block;
124 maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
125 fs->group_desc_count;
126 for (i = 0; i < maxgroup; i++) {
127 if (!ext2fs_bg_has_super(fs, i))
131 retval = io_channel_write_blk(fs->io, group_block,
137 group_ptr = (char *) group_shadow;
138 for (j=0; j < fs->desc_blocks; j++) {
139 retval = io_channel_write_blk(fs->io,
144 group_ptr += fs->blocksize;
147 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
151 * If the write_bitmaps() function is present, call it to
152 * flush the bitmaps. This is done this way so that a simple
153 * program that doesn't mess with the bitmaps doesn't need to
154 * drag in the bitmaps.c code.
156 if (fs->write_bitmaps) {
157 retval = fs->write_bitmaps(fs);
163 fs->super->s_state = fs_state;
164 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
173 errcode_t ext2fs_close(ext2_filsys fs)
177 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
179 if (fs->flags & EXT2_FLAG_DIRTY) {
180 retval = ext2fs_flush(fs);
184 if (fs->write_bitmaps) {
185 retval = fs->write_bitmaps(fs);
194 * This procedure frees a badblocks list.
196 void ext2fs_badblocks_list_free(ext2_badblocks_list bb)
198 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
208 * Close a directory block list
210 void ext2fs_free_dblist(ext2_dblist dblist)
212 if (!dblist || (dblist->magic != EXT2_ET_MAGIC_DBLIST))
218 if (dblist->fs && dblist->fs->dblist == dblist)
219 dblist->fs->dblist = 0;