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>
|
1997-08-11 03:07:40 +04:00
|
|
|
#if HAVE_UNISTD_H
|
1997-04-26 17:21:57 +04:00
|
|
|
#include <unistd.h>
|
1997-08-11 03:07:40 +04:00
|
|
|
#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
|
|
|
|
|
|
|
#include <linux/ext2_fs.h>
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
errcode_t ext2fs_flush(ext2_filsys fs)
|
|
|
|
{
|
Many files:
dosio.c: New file to do DOS/BIOS disk accesses.
namei.c (open_namei): Make pathlen be of type size_t.
llseek.c: Always #include stdlib.h since it's need to define
size_t.
io.h: Use errcode_t for magic numbers.
icount.c (get_icount_el), dupfs.c (ext2fs_dup_handle), dblist.c
(dir_block_cmp): Use size_t where appropriate.
read_bb.c (ext2fs_read_bb_inode), cmp_bitmaps.c
(ext2fs_compare_inode_bitmap): Use blk_t, ino_t and size_t
where appropriate.
closefs.c (ext2fs_flush): Use dgrp_t instead of int where
appropriate.
openfs.c (ext2fs_open), check_desc.c (ext2fs_check_desc): Use blk_t
instead of int where appropriate.
rw_bitmaps.c (read_bitmaps), irel_ma.c, inode.c (ext2fs_write_inode),
initialize.c (ext2fs_initialize): brel_ma.c: Fix to make be
16-bit safe.
link.c (ext2fs_link), unlink.c (ext2fs_unlink), lookup.c (lookup_proc),
ismounted.c (ext2fs_check_if_mounted), block.c (xlate_func):
Add #pragma argsused for Turbo C.
1997-08-12 00:29:22 +04:00
|
|
|
dgrp_t i,j,maxgroup;
|
|
|
|
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;
|
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);
|
1997-04-29 19:29:49 +04:00
|
|
|
if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
|
1997-10-31 09:07:47 +03:00
|
|
|
retval = EXT2_ET_NO_MEMORY;
|
1997-10-26 06:41:24 +03:00
|
|
|
retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
|
|
|
|
(void **) &super_shadow);
|
|
|
|
if (retval)
|
1997-04-26 17:58:21 +04:00
|
|
|
goto errout;
|
1997-10-26 06:41:24 +03:00
|
|
|
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;
|
Many files:
dosio.c: New file to do DOS/BIOS disk accesses.
namei.c (open_namei): Make pathlen be of type size_t.
llseek.c: Always #include stdlib.h since it's need to define
size_t.
io.h: Use errcode_t for magic numbers.
icount.c (get_icount_el), dupfs.c (ext2fs_dup_handle), dblist.c
(dir_block_cmp): Use size_t where appropriate.
read_bb.c (ext2fs_read_bb_inode), cmp_bitmaps.c
(ext2fs_compare_inode_bitmap): Use blk_t, ino_t and size_t
where appropriate.
closefs.c (ext2fs_flush): Use dgrp_t instead of int where
appropriate.
openfs.c (ext2fs_open), check_desc.c (ext2fs_check_desc): Use blk_t
instead of int where appropriate.
rw_bitmaps.c (read_bitmaps), irel_ma.c, inode.c (ext2fs_write_inode),
initialize.c (ext2fs_initialize): brel_ma.c: Fix to make be
16-bit safe.
link.c (ext2fs_link), unlink.c (ext2fs_unlink), lookup.c (lookup_proc),
ismounted.c (ext2fs_check_if_mounted), block.c (xlate_func):
Add #pragma argsused for Turbo C.
1997-08-12 00:29:22 +04:00
|
|
|
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);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
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).
|
|
|
|
*/
|
|
|
|
io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
|
1997-04-26 17:58:21 +04:00
|
|
|
retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
|
|
|
|
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
|
|
|
io_channel_set_blksize(fs->io, fs->blocksize);
|
|
|
|
|
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;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
}
|
1997-04-26 17:58:21 +04:00
|
|
|
retval = 0;
|
|
|
|
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)
|
1997-10-26 06:41:24 +03:00
|
|
|
ext2fs_free_mem((void **) &super_shadow);
|
1997-04-26 17:58:21 +04:00
|
|
|
if (group_shadow)
|
1997-10-26 06:41:24 +03:00
|
|
|
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
|
|
|
|