e2fsprogs/lib/ext2fs/closefs.c

266 lines
6.1 KiB
C
Raw Normal View History

1997-04-26 17:21:57 +04:00
/*
* closefs.c --- close an ext2 filesystem
*
1997-04-29 20:15:03 +04:00
* Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
1997-04-26 17:21:57 +04:00
*/
#include <stdio.h>
#if HAVE_UNISTD_H
1997-04-26 17:21:57 +04:00
#include <unistd.h>
#endif
1997-04-26 17:21:57 +04:00
#include <time.h>
1997-04-26 17:58:21 +04:00
#include <string.h>
1997-04-26 17:21:57 +04:00
Many files: inode.c (ext2fs_open_inode_scan): Initialize the group variables so that we don't need to call get_next_blockgroup() the first time around. Saves a bit of time, and prevents us from needing to assign -1 to current_group (which is an unsigned value). icount.c (insert_icount_el): Cast the estimated number of inodes from a float to an ino_t. alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c, bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c, check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c, ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c, icount.c, initialize.c, inline.c, inode.c, irel_ma.c, ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c, newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c, rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c, tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c, version.c: If EXT2_FLAT_INCLUDES is defined, then assume all of the ext2-specific header files are in a flat directory. block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast all assignments from void * to be compatible with C++. closefs.c (ext2fs_flush): Add a call to io_channel_flush() to make sure the contents of the disk are flushed to disk. dblist.c (ext2fs_add_dir_block): Change new to be new_entry to avoid C++ namespace clash. bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to avoid C++ namespace clash. ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c, irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c, ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c, unlink.c: Change private to be priv_data (to avoid C++ namespace clash)
1998-01-19 17:47:53 +03:00
#if EXT2_FLAT_INCLUDES
#include "ext2_fs.h"
#else
1997-04-26 17:21:57 +04:00
#include <linux/ext2_fs.h>
Many files: inode.c (ext2fs_open_inode_scan): Initialize the group variables so that we don't need to call get_next_blockgroup() the first time around. Saves a bit of time, and prevents us from needing to assign -1 to current_group (which is an unsigned value). icount.c (insert_icount_el): Cast the estimated number of inodes from a float to an ino_t. alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c, bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c, check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c, ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c, icount.c, initialize.c, inline.c, inode.c, irel_ma.c, ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c, newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c, rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c, tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c, version.c: If EXT2_FLAT_INCLUDES is defined, then assume all of the ext2-specific header files are in a flat directory. block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast all assignments from void * to be compatible with C++. closefs.c (ext2fs_flush): Add a call to io_channel_flush() to make sure the contents of the disk are flushed to disk. dblist.c (ext2fs_add_dir_block): Change new to be new_entry to avoid C++ namespace clash. bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to avoid C++ namespace clash. ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c, irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c, ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c, unlink.c: Change private to be priv_data (to avoid C++ namespace clash)
1998-01-19 17:47:53 +03:00
#endif
1997-04-26 17:21:57 +04:00
1997-04-29 20:15:03 +04:00
#include "ext2fsP.h"
1997-04-26 17:21:57 +04:00
1997-04-29 21:48:10 +04:00
static int test_root(int a, int b)
{
if (a == 0)
return 1;
while (1) {
if (a == 1)
return 1;
if (a % b)
return 0;
a = a / b;
}
}
int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
{
#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
struct ext2fs_sb *s;
s = (struct ext2fs_sb *) fs->super;
if (!(s->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
return 1;
if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
test_root(group_block, 7))
return 1;
return 0;
#else
return 1;
#endif
}
/*
* This function forces out the primary superblock. We need to only
* write out those fields which we have changed, since if the
* filesystem is mounted, it may have changed some of the other
* fields.
*
* It takes as input a superblock which has already been byte swapped
* (if necessary).
*
*/
static errcode_t write_primary_superblock(ext2_filsys fs,
struct ext2_super_block *super)
{
__u16 *old_super, *new_super;
int check_idx, write_idx, size;
errcode_t retval;
if (!fs->io->manager->write_byte || !fs->orig_super) {
io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
super);
io_channel_set_blksize(fs->io, fs->blocksize);
return retval;
}
old_super = (__u16 *) fs->orig_super;
new_super = (__u16 *) super;
for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
if (old_super[check_idx] == new_super[check_idx])
continue;
write_idx = check_idx;
for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
if (old_super[check_idx] == new_super[check_idx])
break;
size = 2 * (check_idx - write_idx);
#if 0
printf("Writing %d bytes starting at %d\n",
size, write_idx*2);
#endif
retval = io_channel_write_byte(fs->io,
SUPERBLOCK_OFFSET + (2 * write_idx), size,
new_super + write_idx);
if (retval)
return retval;
}
return 0;
}
1997-04-26 17:21:57 +04:00
errcode_t ext2fs_flush(ext2_filsys fs)
{
dgrp_t i,j,maxgroup,sgrp;
blk_t group_block;
1997-04-26 17:21:57 +04:00
errcode_t retval;
char *group_ptr;
1997-04-26 17:34:30 +04:00
unsigned long fs_state;
1997-04-26 17:58:21 +04:00
struct ext2_super_block *super_shadow = 0;
struct ext2_group_desc *group_shadow = 0;
struct ext2_group_desc *s, *t;
struct ext2fs_sb *sb, *sb_shadow;
1997-04-26 17:21:57 +04:00
1997-04-26 17:34:30 +04:00
EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1997-04-26 17:58:21 +04:00
fs_state = fs->super->s_state;
fs->super->s_wtime = time(NULL);
sb = (struct ext2fs_sb *) fs->super;
sb->s_block_group_nr = 0;
1997-04-29 19:29:49 +04:00
if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
retval = EXT2_ET_NO_MEMORY;
retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
(void **) &super_shadow);
if (retval)
1997-04-26 17:58:21 +04:00
goto errout;
retval = ext2fs_get_mem((size_t)(fs->blocksize *
fs->desc_blocks),
(void **) &group_shadow);
if (retval)
1997-04-26 17:58:21 +04:00
goto errout;
memset(group_shadow, 0, (size_t) fs->blocksize *
fs->desc_blocks);
1997-04-26 17:58:21 +04:00
/* swap the superblock */
*super_shadow = *fs->super;
ext2fs_swap_super(super_shadow);
sb_shadow = (struct ext2fs_sb *) super_shadow;
1997-04-26 17:58:21 +04:00
/* swap the group descriptors */
for (j=0, s=fs->group_desc, t=group_shadow;
j < fs->group_desc_count; j++, t++, s++) {
*t = *s;
ext2fs_swap_group_desc(t);
}
} else {
super_shadow = fs->super;
sb_shadow = (struct ext2fs_sb *) fs->super;
1997-04-26 17:58:21 +04:00
group_shadow = fs->group_desc;
}
1997-04-26 17:21:57 +04:00
/*
* Write out master superblock. This has to be done
* separately, since it is located at a fixed location
* (SUPERBLOCK_OFFSET).
*/
retval = write_primary_superblock(fs, super_shadow);
1997-04-26 17:21:57 +04:00
if (retval)
1997-04-26 17:58:21 +04:00
goto errout;
1997-04-26 17:21:57 +04:00
1997-04-26 17:34:30 +04:00
/*
1997-04-26 17:58:21 +04:00
* Set the state of the FS to be non-valid. (The state has
* already been backed up earlier, and will be restored when
* we exit.)
1997-04-26 17:34:30 +04:00
*/
fs->super->s_state &= ~EXT2_VALID_FS;
1997-04-29 19:29:49 +04:00
if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
1997-04-26 17:58:21 +04:00
*super_shadow = *fs->super;
ext2fs_swap_super(super_shadow);
}
1997-04-26 17:34:30 +04:00
1997-04-26 17:21:57 +04:00
/*
* Write out the master group descriptors, and the backup
* superblocks and group descriptors.
*/
group_block = fs->super->s_first_data_block;
1997-04-29 19:29:49 +04:00
maxgroup = (fs->flags & EXT2_FLAG_MASTER_SB_ONLY) ? 1 :
fs->group_desc_count;
for (i = 0; i < maxgroup; i++) {
1997-04-29 21:48:10 +04:00
if (!ext2fs_bg_has_super(fs, i))
goto next_group;
sgrp = i;
if (sgrp > ((1 << 16) - 1))
sgrp = (1 << 16) - 1;
if (fs->flags & EXT2_FLAG_SWAP_BYTES)
sb_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
else
sb->s_block_group_nr = sgrp;
1997-04-26 17:21:57 +04:00
if (i !=0 ) {
retval = io_channel_write_blk(fs->io, group_block,
-SUPERBLOCK_SIZE,
1997-04-26 17:58:21 +04:00
super_shadow);
if (retval)
goto errout;
1997-04-26 17:21:57 +04:00
}
1997-04-26 17:58:21 +04:00
group_ptr = (char *) group_shadow;
1997-04-26 17:21:57 +04:00
for (j=0; j < fs->desc_blocks; j++) {
retval = io_channel_write_blk(fs->io,
group_block+1+j, 1,
group_ptr);
1997-04-26 17:58:21 +04:00
if (retval)
goto errout;
1997-04-26 17:21:57 +04:00
group_ptr += fs->blocksize;
}
1997-04-29 21:48:10 +04:00
next_group:
1997-04-26 17:21:57 +04:00
group_block += EXT2_BLOCKS_PER_GROUP(fs->super);
}
sb->s_block_group_nr = 0;
1997-04-26 17:21:57 +04:00
/*
* If the write_bitmaps() function is present, call it to
* flush the bitmaps. This is done this way so that a simple
* program that doesn't mess with the bitmaps doesn't need to
* drag in the bitmaps.c code.
*/
if (fs->write_bitmaps) {
retval = fs->write_bitmaps(fs);
if (retval)
1997-04-26 17:58:21 +04:00
goto errout;
1997-04-26 17:21:57 +04:00
}
Many files: inode.c (ext2fs_open_inode_scan): Initialize the group variables so that we don't need to call get_next_blockgroup() the first time around. Saves a bit of time, and prevents us from needing to assign -1 to current_group (which is an unsigned value). icount.c (insert_icount_el): Cast the estimated number of inodes from a float to an ino_t. alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c, bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c, check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c, ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c, icount.c, initialize.c, inline.c, inode.c, irel_ma.c, ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c, newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c, rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c, tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c, version.c: If EXT2_FLAT_INCLUDES is defined, then assume all of the ext2-specific header files are in a flat directory. block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast all assignments from void * to be compatible with C++. closefs.c (ext2fs_flush): Add a call to io_channel_flush() to make sure the contents of the disk are flushed to disk. dblist.c (ext2fs_add_dir_block): Change new to be new_entry to avoid C++ namespace clash. bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to avoid C++ namespace clash. ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c, irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c, ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c, unlink.c: Change private to be priv_data (to avoid C++ namespace clash)
1998-01-19 17:47:53 +03:00
/*
* Flush the blocks out to disk
*/
retval = io_channel_flush(fs->io);
1997-04-26 17:58:21 +04:00
errout:
fs->super->s_state = fs_state;
1997-04-29 19:29:49 +04:00
if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
1997-04-26 17:58:21 +04:00
if (super_shadow)
ext2fs_free_mem((void **) &super_shadow);
1997-04-26 17:58:21 +04:00
if (group_shadow)
ext2fs_free_mem((void **) &group_shadow);
1997-04-26 17:58:21 +04:00
}
return retval;
1997-04-26 17:21:57 +04:00
}
errcode_t ext2fs_close(ext2_filsys fs)
{
errcode_t retval;
1997-04-26 17:34:30 +04:00
EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1997-04-26 17:21:57 +04:00
if (fs->flags & EXT2_FLAG_DIRTY) {
retval = ext2fs_flush(fs);
if (retval)
return retval;
}
1997-04-29 20:15:03 +04:00
if (fs->write_bitmaps) {
retval = fs->write_bitmaps(fs);
if (retval)
return retval;
}
1997-04-26 17:21:57 +04:00
ext2fs_free(fs);
return 0;
}
1997-04-29 20:15:03 +04:00