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 Library
8 * General Public License, version 2.
23 static int test_root(unsigned int a, unsigned int b)
36 int ext2fs_bg_has_super(ext2_filsys fs, dgrp_t group)
40 if (ext2fs_has_feature_sparse_super2(fs->super)) {
41 if (group == fs->super->s_backup_bgs[0] ||
42 group == fs->super->s_backup_bgs[1])
46 if ((group <= 1) || !ext2fs_has_feature_sparse_super(fs->super))
50 if (test_root(group, 3) || (test_root(group, 5)) ||
58 * ext2fs_super_and_bgd_loc2()
59 * @fs: ext2 fs pointer
60 * @group given block group
61 * @ret_super_blk: if !NULL, returns super block location
62 * @ret_old_desc_blk: if !NULL, returns location of the old block
64 * @ret_new_desc_blk: if !NULL, returns location of meta_bg block
66 * @ret_used_blks: if !NULL, returns number of blocks used by
67 * super block and group_descriptors.
69 * Returns errcode_t of 0
71 errcode_t ext2fs_super_and_bgd_loc2(ext2_filsys fs,
73 blk64_t *ret_super_blk,
74 blk64_t *ret_old_desc_blk,
75 blk64_t *ret_new_desc_blk,
78 blk64_t group_block, super_blk = 0, old_desc_blk = 0, new_desc_blk = 0;
79 unsigned int meta_bg, meta_bg_size;
81 blk64_t old_desc_blocks;
84 group_block = ext2fs_group_first_block2(fs, group);
85 if (group_block == 0 && fs->blocksize == 1024)
86 group_block = 1; /* Deal with 1024 blocksize && bigalloc */
88 if (ext2fs_has_feature_meta_bg(fs->super))
89 old_desc_blocks = fs->super->s_first_meta_bg;
92 fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
94 has_super = ext2fs_bg_has_super(fs, group);
97 super_blk = group_block;
100 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
101 meta_bg = group / meta_bg_size;
103 if (!ext2fs_has_feature_meta_bg(fs->super) ||
104 (meta_bg < fs->super->s_first_meta_bg)) {
106 old_desc_blk = group_block + 1;
107 numblocks += old_desc_blocks;
110 if (((group % meta_bg_size) == 0) ||
111 ((group % meta_bg_size) == 1) ||
112 ((group % meta_bg_size) == (meta_bg_size-1))) {
115 new_desc_blk = group_block + has_super;
121 *ret_super_blk = super_blk;
122 if (ret_old_desc_blk)
123 *ret_old_desc_blk = old_desc_blk;
124 if (ret_new_desc_blk)
125 *ret_new_desc_blk = new_desc_blk;
127 *ret_used_blks = numblocks;
133 * This function returns the location of the superblock, block group
134 * descriptors for a given block group. It currently returns the
135 * number of free blocks assuming that inode table and allocation
136 * bitmaps will be in the group. This is not necessarily the case
137 * when the flex_bg feature is enabled, so callers should take care!
138 * It was only really intended for use by mke2fs, and even there it's
141 * The ext2fs_super_and_bgd_loc2() function is 64-bit block number
142 * capable and returns the number of blocks used by super block and
145 int ext2fs_super_and_bgd_loc(ext2_filsys fs,
147 blk_t *ret_super_blk,
148 blk_t *ret_old_desc_blk,
149 blk_t *ret_new_desc_blk,
152 blk64_t ret_super_blk2;
153 blk64_t ret_old_desc_blk2;
154 blk64_t ret_new_desc_blk2;
157 unsigned int meta_bg_size;
159 ext2fs_super_and_bgd_loc2(fs, group, &ret_super_blk2,
164 numblocks = ext2fs_group_blocks_count(fs, group);
167 *ret_super_blk = (blk_t)ret_super_blk2;
168 if (ret_old_desc_blk)
169 *ret_old_desc_blk = (blk_t)ret_old_desc_blk2;
170 if (ret_new_desc_blk)
171 *ret_new_desc_blk = (blk_t)ret_new_desc_blk2;
173 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
174 *ret_meta_bg = group / meta_bg_size;
177 numblocks -= 2 + fs->inode_blocks_per_group + ret_used_blks;
183 * This function forces out the primary superblock. We need to only
184 * write out those fields which we have changed, since if the
185 * filesystem is mounted, it may have changed some of the other
188 * It takes as input a superblock which has already been byte swapped
192 static errcode_t write_primary_superblock(ext2_filsys fs,
193 struct ext2_super_block *super)
195 __u16 *old_super, *new_super;
196 int check_idx, write_idx, size;
199 if (!fs->io->manager->write_byte || !fs->orig_super) {
201 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
202 retval = io_channel_write_blk64(fs->io, 1, -SUPERBLOCK_SIZE,
204 io_channel_set_blksize(fs->io, fs->blocksize);
208 old_super = (__u16 *) fs->orig_super;
209 new_super = (__u16 *) super;
211 for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
212 if (old_super[check_idx] == new_super[check_idx])
214 write_idx = check_idx;
215 for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
216 if (old_super[check_idx] == new_super[check_idx])
218 size = 2 * (check_idx - write_idx);
220 printf("Writing %d bytes starting at %d\n",
223 retval = io_channel_write_byte(fs->io,
224 SUPERBLOCK_OFFSET + (2 * write_idx), size,
225 new_super + write_idx);
226 if (retval == EXT2_ET_UNIMPLEMENTED)
231 memcpy(fs->orig_super, super, SUPERBLOCK_SIZE);
237 * Updates the revision to EXT2_DYNAMIC_REV
239 void ext2fs_update_dynamic_rev(ext2_filsys fs)
241 struct ext2_super_block *sb = fs->super;
243 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
246 sb->s_rev_level = EXT2_DYNAMIC_REV;
247 sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
248 sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
249 /* s_uuid is handled by e2fsck already */
250 /* other fields should be left alone */
253 static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
255 struct ext2_super_block *super_shadow)
260 if (sgrp > ((1 << 16) - 1))
261 sgrp = (1 << 16) - 1;
263 super_shadow->s_block_group_nr = ext2fs_cpu_to_le16(sgrp);
265 retval = ext2fs_superblock_csum_set(fs, super_shadow);
269 return io_channel_write_blk64(fs->io, group_block, -SUPERBLOCK_SIZE,
273 errcode_t ext2fs_flush(ext2_filsys fs)
275 return ext2fs_flush2(fs, 0);
278 errcode_t ext2fs_flush2(ext2_filsys fs, int flags)
282 unsigned long fs_state;
283 __u32 feature_incompat;
284 struct ext2_super_block *super_shadow = 0;
285 struct opaque_ext2_group_desc *group_shadow = 0;
286 #ifdef WORDS_BIGENDIAN
287 struct ext2_group_desc *gdp;
291 blk64_t old_desc_blocks;
292 struct ext2fs_numeric_progress_struct progress;
294 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
296 if ((fs->flags & EXT2_FLAG_SUPER_ONLY) == 0 &&
297 !ext2fs_has_feature_journal_dev(fs->super) &&
298 fs->group_desc == NULL)
299 return EXT2_ET_NO_GDESC;
301 fs_state = fs->super->s_state;
302 feature_incompat = fs->super->s_feature_incompat;
304 fs->super->s_wtime = fs->now ? fs->now : time(NULL);
305 fs->super->s_block_group_nr = 0;
308 * If the write_bitmaps() function is present, call it to
309 * flush the bitmaps. This is done this way so that a simple
310 * program that doesn't mess with the bitmaps doesn't need to
311 * drag in the bitmaps.c code.
313 * Bitmap checksums live in the group descriptor, so the
314 * bitmaps need to be written before the descriptors.
316 if (fs->write_bitmaps) {
317 retval = fs->write_bitmaps(fs);
323 * Set the state of the FS to be non-valid. (The state has
324 * already been backed up earlier, and will be restored after
325 * we write out the backup superblocks.)
327 fs->super->s_state &= ~EXT2_VALID_FS;
328 ext2fs_clear_feature_journal_needs_recovery(fs->super);
330 /* Byte swap the superblock and the group descriptors if necessary */
331 #ifdef WORDS_BIGENDIAN
332 retval = EXT2_ET_NO_MEMORY;
333 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow);
336 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
340 memcpy(super_shadow, fs->super, sizeof(struct ext2_super_block));
341 memcpy(group_shadow, fs->group_desc, (size_t) fs->blocksize *
344 ext2fs_swap_super(super_shadow);
345 for (j = 0; j < fs->group_desc_count; j++) {
346 gdp = ext2fs_group_desc(fs, group_shadow, j);
347 ext2fs_swap_group_desc2(fs, gdp);
350 super_shadow = fs->super;
351 group_shadow = fs->group_desc;
355 * If this is an external journal device, don't write out the
356 * block group descriptors or any of the backup superblocks
358 if (ext2fs_has_feature_journal_dev(fs->super))
359 goto write_primary_superblock_only;
362 * Write out the master group descriptors, and the backup
363 * superblocks and group descriptors.
365 group_ptr = (char *) group_shadow;
366 if (ext2fs_has_feature_meta_bg(fs->super)) {
367 old_desc_blocks = fs->super->s_first_meta_bg;
368 if (old_desc_blocks > fs->desc_blocks)
369 old_desc_blocks = fs->desc_blocks;
371 old_desc_blocks = fs->desc_blocks;
373 if (fs->progress_ops && fs->progress_ops->init)
374 (fs->progress_ops->init)(fs, &progress, NULL,
375 fs->group_desc_count);
378 for (i = 0; i < fs->group_desc_count; i++) {
379 blk64_t super_blk, old_desc_blk, new_desc_blk;
381 if (fs->progress_ops && fs->progress_ops->update)
382 (fs->progress_ops->update)(fs, &progress, i);
383 ext2fs_super_and_bgd_loc2(fs, i, &super_blk, &old_desc_blk,
386 if (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) &&i && super_blk) {
387 retval = write_backup_super(fs, i, super_blk,
392 if (fs->flags & EXT2_FLAG_SUPER_ONLY)
394 if ((old_desc_blk) &&
395 (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) || (i == 0))) {
396 retval = io_channel_write_blk64(fs->io,
397 old_desc_blk, old_desc_blocks, group_ptr);
402 int meta_bg = i / EXT2_DESC_PER_BLOCK(fs->super);
404 retval = io_channel_write_blk64(fs->io, new_desc_blk,
405 1, group_ptr + (meta_bg*fs->blocksize));
411 if (fs->progress_ops && fs->progress_ops->close)
412 (fs->progress_ops->close)(fs, &progress, NULL);
414 write_primary_superblock_only:
416 * Write out master superblock. This has to be done
417 * separately, since it is located at a fixed location
418 * (SUPERBLOCK_OFFSET). We flush all other pending changes
419 * out to disk first, just to avoid a race condition with an
420 * insy-tinsy window....
423 fs->super->s_block_group_nr = 0;
424 fs->super->s_state = fs_state;
425 fs->super->s_feature_incompat = feature_incompat;
426 #ifdef WORDS_BIGENDIAN
427 *super_shadow = *fs->super;
428 ext2fs_swap_super(super_shadow);
431 retval = ext2fs_superblock_csum_set(fs, super_shadow);
435 if (!(flags & EXT2_FLAG_FLUSH_NO_SYNC)) {
436 retval = io_channel_flush(fs->io);
440 retval = write_primary_superblock(fs, super_shadow);
444 fs->flags &= ~EXT2_FLAG_DIRTY;
446 if (!(flags & EXT2_FLAG_FLUSH_NO_SYNC)) {
447 retval = io_channel_flush(fs->io);
452 fs->super->s_state = fs_state;
453 #ifdef WORDS_BIGENDIAN
455 ext2fs_free_mem(&super_shadow);
457 ext2fs_free_mem(&group_shadow);
462 errcode_t ext2fs_close_free(ext2_filsys *fs_ptr)
465 ext2_filsys fs = *fs_ptr;
467 ret = ext2fs_close2(fs, 0);
474 errcode_t ext2fs_close(ext2_filsys fs)
476 return ext2fs_close2(fs, 0);
479 errcode_t ext2fs_close2(ext2_filsys fs, int flags)
485 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
487 if (fs->write_bitmaps) {
488 retval = fs->write_bitmaps(fs);
492 if (fs->super->s_kbytes_written &&
493 fs->io->manager->get_stats)
494 fs->io->manager->get_stats(fs->io, &stats);
495 if (stats && stats->bytes_written && (fs->flags & EXT2_FLAG_RW)) {
496 fs->super->s_kbytes_written += stats->bytes_written >> 10;
497 meta_blks = fs->desc_blocks + 1;
498 if (!(fs->flags & EXT2_FLAG_SUPER_ONLY))
499 fs->super->s_kbytes_written += meta_blks /
500 (fs->blocksize / 1024);
501 if ((fs->flags & EXT2_FLAG_DIRTY) == 0)
502 fs->flags |= EXT2_FLAG_SUPER_ONLY | EXT2_FLAG_DIRTY;
504 if (fs->flags & EXT2_FLAG_DIRTY) {
505 retval = ext2fs_flush2(fs, flags);
510 retval = ext2fs_mmp_stop(fs);