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 * ext2fs_super_and_bgd_loc2()
50 * @fs: ext2 fs pointer
51 * @group given block group
52 * @ret_super_blk: if !NULL, returns super block location
53 * @ret_old_desc_blk: if !NULL, returns location of the old block
55 * @ret_new_desc_blk: if !NULL, returns location of meta_bg block
57 * @ret_used_blks: if !NULL, returns number of blocks used by
58 * super block and group_descriptors.
60 * Returns errcode_t of 0
62 errcode_t ext2fs_super_and_bgd_loc2(ext2_filsys fs,
64 blk64_t *ret_super_blk,
65 blk64_t *ret_old_desc_blk,
66 blk64_t *ret_new_desc_blk,
69 blk64_t group_block, super_blk = 0, old_desc_blk = 0, new_desc_blk = 0;
70 unsigned int meta_bg, meta_bg_size;
72 blk64_t old_desc_blocks;
75 group_block = ext2fs_group_first_block2(fs, group);
77 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
78 old_desc_blocks = fs->super->s_first_meta_bg;
81 fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
83 has_super = ext2fs_bg_has_super(fs, group);
86 super_blk = group_block;
89 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
90 meta_bg = group / meta_bg_size;
92 if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
93 (meta_bg < fs->super->s_first_meta_bg)) {
95 old_desc_blk = group_block + 1;
96 numblocks += old_desc_blocks;
99 if (((group % meta_bg_size) == 0) ||
100 ((group % meta_bg_size) == 1) ||
101 ((group % meta_bg_size) == (meta_bg_size-1))) {
104 new_desc_blk = group_block + has_super;
110 *ret_super_blk = super_blk;
111 if (ret_old_desc_blk)
112 *ret_old_desc_blk = old_desc_blk;
113 if (ret_new_desc_blk)
114 *ret_new_desc_blk = new_desc_blk;
116 *ret_used_blks = numblocks;
122 * This function returns the location of the superblock, block group
123 * descriptors for a given block group. It currently returns the
124 * number of free blocks assuming that inode table and allocation
125 * bitmaps will be in the group. This is not necessarily the case
126 * when the flex_bg feature is enabled, so callers should take care!
127 * It was only really intended for use by mke2fs, and even there it's
130 * The ext2fs_super_and_bgd_loc2() function is 64-bit block number
131 * capable and returns the number of blocks used by super block and
134 int ext2fs_super_and_bgd_loc(ext2_filsys fs,
136 blk_t *ret_super_blk,
137 blk_t *ret_old_desc_blk,
138 blk_t *ret_new_desc_blk,
141 blk64_t ret_super_blk2;
142 blk64_t ret_old_desc_blk2;
143 blk64_t ret_new_desc_blk2;
146 unsigned int meta_bg_size;
148 ext2fs_super_and_bgd_loc2(fs, group, &ret_super_blk2,
153 if (group == fs->group_desc_count-1) {
154 numblocks = (ext2fs_blocks_count(fs->super) -
155 (blk64_t) fs->super->s_first_data_block) %
156 (blk64_t) fs->super->s_blocks_per_group;
158 numblocks = fs->super->s_blocks_per_group;
160 numblocks = fs->super->s_blocks_per_group;
163 *ret_super_blk = (blk_t)ret_super_blk2;
164 if (ret_old_desc_blk)
165 *ret_old_desc_blk = (blk_t)ret_old_desc_blk2;
166 if (ret_new_desc_blk)
167 *ret_new_desc_blk = (blk_t)ret_new_desc_blk2;
169 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
170 *ret_meta_bg = group / meta_bg_size;
173 numblocks -= 2 + fs->inode_blocks_per_group + ret_used_blks;
179 * This function forces out the primary superblock. We need to only
180 * write out those fields which we have changed, since if the
181 * filesystem is mounted, it may have changed some of the other
184 * It takes as input a superblock which has already been byte swapped
188 static errcode_t write_primary_superblock(ext2_filsys fs,
189 struct ext2_super_block *super)
191 __u16 *old_super, *new_super;
192 int check_idx, write_idx, size;
195 if (!fs->io->manager->write_byte || !fs->orig_super) {
196 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
197 retval = io_channel_write_blk64(fs->io, 1, -SUPERBLOCK_SIZE,
199 io_channel_set_blksize(fs->io, fs->blocksize);
203 old_super = (__u16 *) fs->orig_super;
204 new_super = (__u16 *) super;
206 for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
207 if (old_super[check_idx] == new_super[check_idx])
209 write_idx = check_idx;
210 for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
211 if (old_super[check_idx] == new_super[check_idx])
213 size = 2 * (check_idx - write_idx);
215 printf("Writing %d bytes starting at %d\n",
218 retval = io_channel_write_byte(fs->io,
219 SUPERBLOCK_OFFSET + (2 * write_idx), size,
220 new_super + write_idx);
224 memcpy(fs->orig_super, super, SUPERBLOCK_SIZE);
230 * Updates the revision to EXT2_DYNAMIC_REV
232 void ext2fs_update_dynamic_rev(ext2_filsys fs)
234 struct ext2_super_block *sb = fs->super;
236 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
239 sb->s_rev_level = EXT2_DYNAMIC_REV;
240 sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
241 sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
242 /* s_uuid is handled by e2fsck already */
243 /* other fields should be left alone */
246 static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
248 struct ext2_super_block *super_shadow)
252 if (sgrp > ((1 << 16) - 1))
253 sgrp = (1 << 16) - 1;
254 #ifdef WORDS_BIGENDIAN
255 super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
257 fs->super->s_block_group_nr = sgrp;
260 return io_channel_write_blk64(fs->io, group_block, -SUPERBLOCK_SIZE,
264 errcode_t ext2fs_flush(ext2_filsys fs)
268 unsigned long fs_state;
269 __u32 feature_incompat;
270 struct ext2_super_block *super_shadow = 0;
271 struct ext2_group_desc *group_shadow = 0;
272 #ifdef WORDS_BIGENDIAN
273 ext2_group_desc *gdp;
278 struct ext2fs_numeric_progress_struct progress;
280 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
282 fs_state = fs->super->s_state;
283 feature_incompat = fs->super->s_feature_incompat;
285 fs->super->s_wtime = fs->now ? fs->now : time(NULL);
286 fs->super->s_block_group_nr = 0;
287 #ifdef WORDS_BIGENDIAN
288 retval = EXT2_ET_NO_MEMORY;
289 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow);
292 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
296 memcpy(group_shadow, fs->group_desc, (size_t) fs->blocksize *
299 /* swap the group descriptors */
300 for (j=0; j < fs->group_desc_count; j++) {
301 gdp = ext2fs_group_desc(fs, group_shadow, j);
302 ext2fs_swap_group_desc2(fs, gdp);
305 super_shadow = fs->super;
306 group_shadow = ext2fs_group_desc(fs, fs->group_desc, 0);
310 * Set the state of the FS to be non-valid. (The state has
311 * already been backed up earlier, and will be restored after
312 * we write out the backup superblocks.)
314 fs->super->s_state &= ~EXT2_VALID_FS;
315 fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
316 #ifdef WORDS_BIGENDIAN
317 *super_shadow = *fs->super;
318 ext2fs_swap_super(super_shadow);
322 * If this is an external journal device, don't write out the
323 * block group descriptors or any of the backup superblocks
325 if (fs->super->s_feature_incompat &
326 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
327 goto write_primary_superblock_only;
330 * Write out the master group descriptors, and the backup
331 * superblocks and group descriptors.
333 group_ptr = (char *) group_shadow;
334 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
335 old_desc_blocks = fs->super->s_first_meta_bg;
337 old_desc_blocks = fs->desc_blocks;
339 ext2fs_numeric_progress_init(fs, &progress, NULL,
340 fs->group_desc_count);
343 for (i = 0; i < fs->group_desc_count; i++) {
344 blk64_t super_blk, old_desc_blk, new_desc_blk;
346 ext2fs_numeric_progress_update(fs, &progress, i);
347 ext2fs_super_and_bgd_loc2(fs, i, &super_blk, &old_desc_blk,
350 if (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) &&i && super_blk) {
351 retval = write_backup_super(fs, i, super_blk,
356 if (fs->flags & EXT2_FLAG_SUPER_ONLY)
358 if ((old_desc_blk) &&
359 (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) || (i == 0))) {
360 retval = io_channel_write_blk64(fs->io,
361 old_desc_blk, old_desc_blocks, group_ptr);
366 int meta_bg = i / EXT2_DESC_PER_BLOCK(fs->super);
368 retval = io_channel_write_blk64(fs->io, new_desc_blk,
369 1, group_ptr + (meta_bg*fs->blocksize));
375 ext2fs_numeric_progress_close(fs, &progress, NULL);
378 * If the write_bitmaps() function is present, call it to
379 * flush the bitmaps. This is done this way so that a simple
380 * program that doesn't mess with the bitmaps doesn't need to
381 * drag in the bitmaps.c code.
383 if (fs->write_bitmaps) {
384 retval = fs->write_bitmaps(fs);
389 write_primary_superblock_only:
391 * Write out master superblock. This has to be done
392 * separately, since it is located at a fixed location
393 * (SUPERBLOCK_OFFSET). We flush all other pending changes
394 * out to disk first, just to avoid a race condition with an
395 * insy-tinsy window....
398 fs->super->s_block_group_nr = 0;
399 fs->super->s_state = fs_state;
400 fs->super->s_feature_incompat = feature_incompat;
401 #ifdef WORDS_BIGENDIAN
402 *super_shadow = *fs->super;
403 ext2fs_swap_super(super_shadow);
406 retval = io_channel_flush(fs->io);
407 retval = write_primary_superblock(fs, super_shadow);
411 fs->flags &= ~EXT2_FLAG_DIRTY;
413 retval = io_channel_flush(fs->io);
415 fs->super->s_state = fs_state;
416 #ifdef WORDS_BIGENDIAN
418 ext2fs_free_mem(&super_shadow);
420 ext2fs_free_mem(&group_shadow);
425 errcode_t ext2fs_close(ext2_filsys fs)
431 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
433 if (fs->write_bitmaps) {
434 retval = fs->write_bitmaps(fs);
438 if (fs->super->s_kbytes_written &&
439 fs->io->manager->get_stats)
440 fs->io->manager->get_stats(fs->io, &stats);
441 if (stats && stats->bytes_written && (fs->flags & EXT2_FLAG_RW)) {
442 fs->super->s_kbytes_written += stats->bytes_written >> 10;
443 meta_blks = fs->desc_blocks + 1;
444 if (!(fs->flags & EXT2_FLAG_SUPER_ONLY))
445 fs->super->s_kbytes_written += meta_blks /
446 (fs->blocksize / 1024);
447 if ((fs->flags & EXT2_FLAG_DIRTY) == 0)
448 fs->flags |= EXT2_FLAG_SUPER_ONLY | EXT2_FLAG_DIRTY;
450 if (fs->flags & EXT2_FLAG_DIRTY) {
451 retval = ext2fs_flush(fs);