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
22 static int test_root(int a, int b)
35 int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
37 if (!(fs->super->s_feature_ro_compat &
38 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
41 if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
42 test_root(group_block, 7))
49 * This function returns the location of the superblock, block group
50 * descriptors for a given block group. It currently returns the
51 * number of free blocks assuming that inode table and allocation
52 * bitmaps will be in the group. This is not necessarily the case
53 * when the flex_bg feature is enabled, so callers should take care!
54 * It was only really intended for use by mke2fs, and even there it's
55 * not that useful. In the future, when we redo this function for
56 * 64-bit block numbers, we should probably return the number of
57 * blocks used by the super block and group descriptors instead.
59 * See also the comment for ext2fs_reserve_super_and_bgd()
61 int ext2fs_super_and_bgd_loc(ext2_filsys fs,
64 blk_t *ret_old_desc_blk,
65 blk_t *ret_new_desc_blk,
68 blk_t group_block, super_blk = 0, old_desc_blk = 0, new_desc_blk = 0;
69 unsigned int meta_bg, meta_bg_size;
70 blk_t numblocks, old_desc_blocks;
73 group_block = ext2fs_group_first_block(fs, group);
75 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
76 old_desc_blocks = fs->super->s_first_meta_bg;
79 fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
81 if (group == fs->group_desc_count-1) {
82 numblocks = (fs->super->s_blocks_count -
83 fs->super->s_first_data_block) %
84 fs->super->s_blocks_per_group;
86 numblocks = fs->super->s_blocks_per_group;
88 numblocks = fs->super->s_blocks_per_group;
90 has_super = ext2fs_bg_has_super(fs, group);
93 super_blk = group_block;
96 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
97 meta_bg = group / meta_bg_size;
99 if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
100 (meta_bg < fs->super->s_first_meta_bg)) {
102 old_desc_blk = group_block + 1;
103 numblocks -= old_desc_blocks;
106 if (((group % meta_bg_size) == 0) ||
107 ((group % meta_bg_size) == 1) ||
108 ((group % meta_bg_size) == (meta_bg_size-1))) {
111 new_desc_blk = group_block + has_super;
116 numblocks -= 2 + fs->inode_blocks_per_group;
119 *ret_super_blk = super_blk;
120 if (ret_old_desc_blk)
121 *ret_old_desc_blk = old_desc_blk;
122 if (ret_new_desc_blk)
123 *ret_new_desc_blk = new_desc_blk;
125 *ret_meta_bg = meta_bg;
131 * This function forces out the primary superblock. We need to only
132 * write out those fields which we have changed, since if the
133 * filesystem is mounted, it may have changed some of the other
136 * It takes as input a superblock which has already been byte swapped
140 static errcode_t write_primary_superblock(ext2_filsys fs,
141 struct ext2_super_block *super)
143 __u16 *old_super, *new_super;
144 int check_idx, write_idx, size;
147 if (!fs->io->manager->write_byte || !fs->orig_super) {
148 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
149 retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
151 io_channel_set_blksize(fs->io, fs->blocksize);
155 old_super = (__u16 *) fs->orig_super;
156 new_super = (__u16 *) super;
158 for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
159 if (old_super[check_idx] == new_super[check_idx])
161 write_idx = check_idx;
162 for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
163 if (old_super[check_idx] == new_super[check_idx])
165 size = 2 * (check_idx - write_idx);
167 printf("Writing %d bytes starting at %d\n",
170 retval = io_channel_write_byte(fs->io,
171 SUPERBLOCK_OFFSET + (2 * write_idx), size,
172 new_super + write_idx);
176 memcpy(fs->orig_super, super, SUPERBLOCK_SIZE);
182 * Updates the revision to EXT2_DYNAMIC_REV
184 void ext2fs_update_dynamic_rev(ext2_filsys fs)
186 struct ext2_super_block *sb = fs->super;
188 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
191 sb->s_rev_level = EXT2_DYNAMIC_REV;
192 sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
193 sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
194 /* s_uuid is handled by e2fsck already */
195 /* other fields should be left alone */
198 static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
200 struct ext2_super_block *super_shadow)
204 if (sgrp > ((1 << 16) - 1))
205 sgrp = (1 << 16) - 1;
206 #ifdef WORDS_BIGENDIAN
207 super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
209 fs->super->s_block_group_nr = sgrp;
212 return io_channel_write_blk(fs->io, group_block, -SUPERBLOCK_SIZE,
217 errcode_t ext2fs_flush(ext2_filsys fs)
221 unsigned long fs_state;
222 __u32 feature_incompat;
223 struct ext2_super_block *super_shadow = 0;
224 struct ext2_group_desc *group_shadow = 0;
225 #ifdef WORDS_BIGENDIAN
226 struct ext2_group_desc *s, *t;
232 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
234 fs_state = fs->super->s_state;
235 feature_incompat = fs->super->s_feature_incompat;
237 fs->super->s_wtime = fs->now ? fs->now : time(NULL);
238 fs->super->s_block_group_nr = 0;
239 #ifdef WORDS_BIGENDIAN
240 retval = EXT2_ET_NO_MEMORY;
241 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow);
244 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
248 memset(group_shadow, 0, (size_t) fs->blocksize *
251 /* swap the group descriptors */
252 for (j=0, s=fs->group_desc, t=group_shadow;
253 j < fs->group_desc_count; j++, t++, s++) {
255 ext2fs_swap_group_desc(t);
258 super_shadow = fs->super;
259 group_shadow = fs->group_desc;
263 * Set the state of the FS to be non-valid. (The state has
264 * already been backed up earlier, and will be restored after
265 * we write out the backup superblocks.)
267 fs->super->s_state &= ~EXT2_VALID_FS;
268 fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
269 #ifdef WORDS_BIGENDIAN
270 *super_shadow = *fs->super;
271 ext2fs_swap_super(super_shadow);
275 * If this is an external journal device, don't write out the
276 * block group descriptors or any of the backup superblocks
278 if (fs->super->s_feature_incompat &
279 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
280 goto write_primary_superblock_only;
283 * Write out the master group descriptors, and the backup
284 * superblocks and group descriptors.
286 group_ptr = (char *) group_shadow;
287 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
288 old_desc_blocks = fs->super->s_first_meta_bg;
290 old_desc_blocks = fs->desc_blocks;
292 for (i = 0; i < fs->group_desc_count; i++) {
293 blk_t super_blk, old_desc_blk, new_desc_blk;
296 ext2fs_super_and_bgd_loc(fs, i, &super_blk, &old_desc_blk,
297 &new_desc_blk, &meta_bg);
299 if (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) &&i && super_blk) {
300 retval = write_backup_super(fs, i, super_blk,
305 if (fs->flags & EXT2_FLAG_SUPER_ONLY)
307 if ((old_desc_blk) &&
308 (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) || (i == 0))) {
309 retval = io_channel_write_blk(fs->io,
310 old_desc_blk, old_desc_blocks, group_ptr);
315 retval = io_channel_write_blk(fs->io, new_desc_blk,
316 1, group_ptr + (meta_bg*fs->blocksize));
323 * If the write_bitmaps() function is present, call it to
324 * flush the bitmaps. This is done this way so that a simple
325 * program that doesn't mess with the bitmaps doesn't need to
326 * drag in the bitmaps.c code.
328 if (fs->write_bitmaps) {
329 retval = fs->write_bitmaps(fs);
334 write_primary_superblock_only:
336 * Write out master superblock. This has to be done
337 * separately, since it is located at a fixed location
338 * (SUPERBLOCK_OFFSET). We flush all other pending changes
339 * out to disk first, just to avoid a race condition with an
340 * insy-tinsy window....
343 fs->super->s_block_group_nr = 0;
344 fs->super->s_state = fs_state;
345 fs->super->s_feature_incompat = feature_incompat;
346 #ifdef WORDS_BIGENDIAN
347 *super_shadow = *fs->super;
348 ext2fs_swap_super(super_shadow);
351 retval = io_channel_flush(fs->io);
352 retval = write_primary_superblock(fs, super_shadow);
356 fs->flags &= ~EXT2_FLAG_DIRTY;
358 retval = io_channel_flush(fs->io);
360 fs->super->s_state = fs_state;
361 #ifdef WORDS_BIGENDIAN
363 ext2fs_free_mem(&super_shadow);
365 ext2fs_free_mem(&group_shadow);
370 errcode_t ext2fs_close(ext2_filsys fs)
374 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
376 if (fs->flags & EXT2_FLAG_DIRTY) {
377 retval = ext2fs_flush(fs);
381 if (fs->write_bitmaps) {
382 retval = fs->write_bitmaps(fs);