2 * closefs.c --- close an ext2 filesystem
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
17 #include <linux/ext2_fs.h>
21 errcode_t ext2fs_flush(ext2_filsys fs)
27 unsigned long fs_state;
28 struct ext2_super_block *super_shadow = 0;
29 struct ext2_group_desc *group_shadow = 0;
30 struct ext2_group_desc *s, *t;
32 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
34 fs_state = fs->super->s_state;
36 fs->super->s_wtime = time(NULL);
37 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
39 if (!(super_shadow = malloc(SUPERBLOCK_SIZE)))
41 if (!(group_shadow = malloc(fs->blocksize*fs->desc_blocks)))
43 memset(group_shadow, 0, fs->blocksize*fs->desc_blocks);
45 /* swap the superblock */
46 *super_shadow = *fs->super;
47 ext2fs_swap_super(super_shadow);
49 /* swap the group descriptors */
50 for (j=0, s=fs->group_desc, t=group_shadow;
51 j < fs->group_desc_count; j++, t++, s++) {
53 ext2fs_swap_group_desc(t);
56 super_shadow = fs->super;
57 group_shadow = fs->group_desc;
61 * Write out master superblock. This has to be done
62 * separately, since it is located at a fixed location
63 * (SUPERBLOCK_OFFSET).
65 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
66 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
70 io_channel_set_blksize(fs->io, fs->blocksize);
73 * Set the state of the FS to be non-valid. (The state has
74 * already been backed up earlier, and will be restored when
77 fs->super->s_state &= ~EXT2_VALID_FS;
78 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
79 *super_shadow = *fs->super;
80 ext2fs_swap_super(super_shadow);
84 * Write out the master group descriptors, and the backup
85 * superblocks and group descriptors.
87 group_block = fs->super->s_first_data_block;
88 maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
90 for (i = 0; i < maxgroup; i++) {
92 retval = io_channel_write_blk(fs->io, group_block,
98 group_ptr = (char *) group_shadow;
99 for (j=0; j < fs->desc_blocks; j++) {
100 retval = io_channel_write_blk(fs->io,
105 group_ptr += fs->blocksize;
107 group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
111 * If the write_bitmaps() function is present, call it to
112 * flush the bitmaps. This is done this way so that a simple
113 * program that doesn't mess with the bitmaps doesn't need to
114 * drag in the bitmaps.c code.
116 if (fs->write_bitmaps) {
117 retval = fs->write_bitmaps(fs);
124 fs->super->s_state = fs_state;
125 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
134 errcode_t ext2fs_close(ext2_filsys fs)
138 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
140 if (fs->flags & EXT2_FLAG_DIRTY) {
141 retval = ext2fs_flush(fs);