1997-04-26 17:21:57 +04:00
|
|
|
/*
|
|
|
|
* closefs.c --- close an ext2 filesystem
|
2008-08-28 07:07:54 +04:00
|
|
|
*
|
1997-04-29 20:15:03 +04:00
|
|
|
* Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
|
|
|
|
*
|
|
|
|
* %Begin-Header%
|
2010-05-18 05:31:56 +04:00
|
|
|
* This file may be redistributed under the terms of the GNU Library
|
|
|
|
* General Public License, version 2.
|
1997-04-29 20:15:03 +04:00
|
|
|
* %End-Header%
|
1997-04-26 17:21:57 +04:00
|
|
|
*/
|
|
|
|
|
2011-09-19 01:34:37 +04:00
|
|
|
#include "config.h"
|
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
|
|
|
|
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
|
|
|
#include "ext2_fs.h"
|
1997-04-29 20:15:03 +04:00
|
|
|
#include "ext2fsP.h"
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2013-06-16 02:29:52 +04:00
|
|
|
static int test_root(unsigned int a, unsigned int b)
|
2004-12-16 02:06:52 +03:00
|
|
|
{
|
|
|
|
while (1) {
|
2013-06-16 02:29:52 +04:00
|
|
|
if (a < b)
|
|
|
|
return 0;
|
|
|
|
if (a == b)
|
2004-12-16 02:06:52 +03:00
|
|
|
return 1;
|
|
|
|
if (a % b)
|
|
|
|
return 0;
|
|
|
|
a = a / b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-16 02:29:52 +04:00
|
|
|
int ext2fs_bg_has_super(ext2_filsys fs, dgrp_t group)
|
2004-12-16 02:06:52 +03:00
|
|
|
{
|
2014-01-12 07:11:42 +04:00
|
|
|
if (group == 0)
|
|
|
|
return 1;
|
|
|
|
if (fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
|
|
|
|
if (group == fs->super->s_backup_bgs[0] ||
|
|
|
|
group == fs->super->s_backup_bgs[1])
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((group <= 1) || !(fs->super->s_feature_ro_compat &
|
|
|
|
EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
|
2004-12-16 02:06:52 +03:00
|
|
|
return 1;
|
2013-06-16 02:29:52 +04:00
|
|
|
if (!(group & 1))
|
|
|
|
return 0;
|
|
|
|
if (test_root(group, 3) || (test_root(group, 5)) ||
|
|
|
|
test_root(group, 7))
|
2004-12-16 02:06:52 +03:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-21 03:22:49 +04:00
|
|
|
/*
|
2009-06-02 00:15:40 +04:00
|
|
|
* ext2fs_super_and_bgd_loc2()
|
|
|
|
* @fs: ext2 fs pointer
|
|
|
|
* @group given block group
|
|
|
|
* @ret_super_blk: if !NULL, returns super block location
|
|
|
|
* @ret_old_desc_blk: if !NULL, returns location of the old block
|
|
|
|
* group descriptor
|
|
|
|
* @ret_new_desc_blk: if !NULL, returns location of meta_bg block
|
|
|
|
* group descriptor
|
|
|
|
* @ret_used_blks: if !NULL, returns number of blocks used by
|
|
|
|
* super block and group_descriptors.
|
2008-04-21 03:22:49 +04:00
|
|
|
*
|
2009-06-02 00:15:40 +04:00
|
|
|
* Returns errcode_t of 0
|
2008-04-21 03:22:49 +04:00
|
|
|
*/
|
2009-06-02 00:15:40 +04:00
|
|
|
errcode_t ext2fs_super_and_bgd_loc2(ext2_filsys fs,
|
|
|
|
dgrp_t group,
|
|
|
|
blk64_t *ret_super_blk,
|
|
|
|
blk64_t *ret_old_desc_blk,
|
|
|
|
blk64_t *ret_new_desc_blk,
|
|
|
|
blk_t *ret_used_blks)
|
2003-11-21 17:02:21 +03:00
|
|
|
{
|
2009-06-02 00:15:40 +04:00
|
|
|
blk64_t group_block, super_blk = 0, old_desc_blk = 0, new_desc_blk = 0;
|
2003-12-07 09:28:50 +03:00
|
|
|
unsigned int meta_bg, meta_bg_size;
|
2009-06-02 00:15:40 +04:00
|
|
|
blk_t numblocks = 0;
|
|
|
|
blk64_t old_desc_blocks;
|
2007-08-30 19:33:40 +04:00
|
|
|
int has_super;
|
2003-11-21 17:02:21 +03:00
|
|
|
|
2009-06-02 00:15:40 +04:00
|
|
|
group_block = ext2fs_group_first_block2(fs, group);
|
2011-07-05 21:42:07 +04:00
|
|
|
if (group_block == 0 && fs->blocksize == 1024)
|
|
|
|
group_block = 1; /* Deal with 1024 blocksize && bigalloc */
|
2003-11-21 17:02:21 +03:00
|
|
|
|
|
|
|
if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
|
|
|
|
old_desc_blocks = fs->super->s_first_meta_bg;
|
|
|
|
else
|
2008-08-28 07:07:54 +04:00
|
|
|
old_desc_blocks =
|
2004-12-15 22:39:16 +03:00
|
|
|
fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
|
2003-11-21 17:02:21 +03:00
|
|
|
|
|
|
|
has_super = ext2fs_bg_has_super(fs, group);
|
|
|
|
|
|
|
|
if (has_super) {
|
|
|
|
super_blk = group_block;
|
2009-06-02 00:15:40 +04:00
|
|
|
numblocks++;
|
2003-11-21 17:02:21 +03:00
|
|
|
}
|
2007-08-30 19:38:13 +04:00
|
|
|
meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
|
2003-11-21 17:02:21 +03:00
|
|
|
meta_bg = group / meta_bg_size;
|
|
|
|
|
|
|
|
if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
|
|
|
|
(meta_bg < fs->super->s_first_meta_bg)) {
|
|
|
|
if (has_super) {
|
|
|
|
old_desc_blk = group_block + 1;
|
2009-06-02 00:15:40 +04:00
|
|
|
numblocks += old_desc_blocks;
|
2003-11-21 17:02:21 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (((group % meta_bg_size) == 0) ||
|
|
|
|
((group % meta_bg_size) == 1) ||
|
|
|
|
((group % meta_bg_size) == (meta_bg_size-1))) {
|
|
|
|
if (has_super)
|
|
|
|
has_super = 1;
|
|
|
|
new_desc_blk = group_block + has_super;
|
2009-06-02 00:15:40 +04:00
|
|
|
numblocks++;
|
2003-11-21 17:02:21 +03:00
|
|
|
}
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2003-11-21 17:02:21 +03:00
|
|
|
if (ret_super_blk)
|
|
|
|
*ret_super_blk = super_blk;
|
|
|
|
if (ret_old_desc_blk)
|
|
|
|
*ret_old_desc_blk = old_desc_blk;
|
|
|
|
if (ret_new_desc_blk)
|
|
|
|
*ret_new_desc_blk = new_desc_blk;
|
2009-06-02 00:15:40 +04:00
|
|
|
if (ret_used_blks)
|
|
|
|
*ret_used_blks = numblocks;
|
|
|
|
|
|
|
|
return 0;
|
2003-11-21 17:02:21 +03:00
|
|
|
}
|
|
|
|
|
2009-06-02 00:15:40 +04:00
|
|
|
/*
|
|
|
|
* This function returns the location of the superblock, block group
|
|
|
|
* descriptors for a given block group. It currently returns the
|
|
|
|
* number of free blocks assuming that inode table and allocation
|
|
|
|
* bitmaps will be in the group. This is not necessarily the case
|
|
|
|
* when the flex_bg feature is enabled, so callers should take care!
|
|
|
|
* It was only really intended for use by mke2fs, and even there it's
|
|
|
|
* not that useful.
|
|
|
|
*
|
|
|
|
* The ext2fs_super_and_bgd_loc2() function is 64-bit block number
|
|
|
|
* capable and returns the number of blocks used by super block and
|
|
|
|
* group descriptors.
|
|
|
|
*/
|
|
|
|
int ext2fs_super_and_bgd_loc(ext2_filsys fs,
|
|
|
|
dgrp_t group,
|
|
|
|
blk_t *ret_super_blk,
|
|
|
|
blk_t *ret_old_desc_blk,
|
|
|
|
blk_t *ret_new_desc_blk,
|
|
|
|
int *ret_meta_bg)
|
|
|
|
{
|
|
|
|
blk64_t ret_super_blk2;
|
|
|
|
blk64_t ret_old_desc_blk2;
|
|
|
|
blk64_t ret_new_desc_blk2;
|
|
|
|
blk_t ret_used_blks;
|
|
|
|
blk_t numblocks;
|
|
|
|
unsigned int meta_bg_size;
|
|
|
|
|
|
|
|
ext2fs_super_and_bgd_loc2(fs, group, &ret_super_blk2,
|
|
|
|
&ret_old_desc_blk2,
|
|
|
|
&ret_new_desc_blk2,
|
|
|
|
&ret_used_blks);
|
|
|
|
|
2011-09-16 17:21:53 +04:00
|
|
|
numblocks = ext2fs_group_blocks_count(fs, group);
|
2009-06-02 00:15:40 +04:00
|
|
|
|
|
|
|
if (ret_super_blk)
|
|
|
|
*ret_super_blk = (blk_t)ret_super_blk2;
|
|
|
|
if (ret_old_desc_blk)
|
|
|
|
*ret_old_desc_blk = (blk_t)ret_old_desc_blk2;
|
|
|
|
if (ret_new_desc_blk)
|
|
|
|
*ret_new_desc_blk = (blk_t)ret_new_desc_blk2;
|
|
|
|
if (ret_meta_bg) {
|
|
|
|
meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
|
|
|
|
*ret_meta_bg = group / meta_bg_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
numblocks -= 2 + fs->inode_blocks_per_group + ret_used_blks;
|
|
|
|
|
|
|
|
return numblocks;
|
|
|
|
}
|
2003-11-21 17:02:21 +03:00
|
|
|
|
2000-10-27 00:24:43 +04:00
|
|
|
/*
|
|
|
|
* 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) {
|
2010-09-24 18:02:25 +04:00
|
|
|
fallback:
|
2000-10-27 00:24:43 +04:00
|
|
|
io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_write_blk64(fs->io, 1, -SUPERBLOCK_SIZE,
|
2000-10-27 00:24:43 +04:00
|
|
|
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);
|
2010-09-24 18:02:25 +04:00
|
|
|
if (retval == EXT2_ET_UNIMPLEMENTED)
|
|
|
|
goto fallback;
|
2000-10-27 00:24:43 +04:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
}
|
2001-07-10 22:27:58 +04:00
|
|
|
memcpy(fs->orig_super, super, SUPERBLOCK_SIZE);
|
2000-10-27 00:24:43 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-13 20:55:49 +03:00
|
|
|
/*
|
|
|
|
* Updates the revision to EXT2_DYNAMIC_REV
|
|
|
|
*/
|
2000-12-13 21:36:23 +03:00
|
|
|
void ext2fs_update_dynamic_rev(ext2_filsys fs)
|
2000-12-13 20:55:49 +03:00
|
|
|
{
|
|
|
|
struct ext2_super_block *sb = fs->super;
|
|
|
|
|
|
|
|
if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sb->s_rev_level = EXT2_DYNAMIC_REV;
|
|
|
|
sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
|
|
|
|
sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
|
|
|
|
/* s_uuid is handled by e2fsck already */
|
|
|
|
/* other fields should be left alone */
|
|
|
|
}
|
|
|
|
|
2002-10-20 08:38:57 +04:00
|
|
|
static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
|
2013-10-07 17:51:48 +04:00
|
|
|
blk64_t group_block,
|
2002-10-20 08:38:57 +04:00
|
|
|
struct ext2_super_block *super_shadow)
|
|
|
|
{
|
2012-08-03 04:47:44 +04:00
|
|
|
errcode_t retval;
|
2002-10-20 08:38:57 +04:00
|
|
|
dgrp_t sgrp = group;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2002-10-20 08:38:57 +04:00
|
|
|
if (sgrp > ((1 << 16) - 1))
|
|
|
|
sgrp = (1 << 16) - 1;
|
2012-08-03 04:47:44 +04:00
|
|
|
|
|
|
|
super_shadow->s_block_group_nr = sgrp;
|
2007-08-11 09:56:48 +04:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
2012-08-03 04:47:44 +04:00
|
|
|
ext2fs_swap_super(super_shadow);
|
2002-10-20 08:38:57 +04:00
|
|
|
#endif
|
2012-08-03 04:47:44 +04:00
|
|
|
retval = ext2fs_superblock_csum_set(fs, super_shadow);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
2002-10-20 08:38:57 +04:00
|
|
|
|
2009-09-08 05:14:24 +04:00
|
|
|
return io_channel_write_blk64(fs->io, group_block, -SUPERBLOCK_SIZE,
|
2002-10-20 08:38:57 +04:00
|
|
|
super_shadow);
|
|
|
|
}
|
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
errcode_t ext2fs_flush(ext2_filsys fs)
|
2011-09-24 18:50:42 +04:00
|
|
|
{
|
|
|
|
return ext2fs_flush2(fs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
errcode_t ext2fs_flush2(ext2_filsys fs, int flags)
|
1997-04-26 17:21:57 +04:00
|
|
|
{
|
2008-03-18 06:17:13 +03:00
|
|
|
dgrp_t i;
|
1997-04-26 17:21:57 +04:00
|
|
|
errcode_t retval;
|
1997-04-26 17:34:30 +04:00
|
|
|
unsigned long fs_state;
|
2007-12-16 03:39:37 +03:00
|
|
|
__u32 feature_incompat;
|
1997-04-26 17:58:21 +04:00
|
|
|
struct ext2_super_block *super_shadow = 0;
|
|
|
|
struct ext2_group_desc *group_shadow = 0;
|
2008-03-18 06:17:13 +03:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
2010-02-15 17:51:28 +03:00
|
|
|
struct ext2_group_desc *gdp;
|
2008-03-18 06:17:13 +03:00
|
|
|
dgrp_t j;
|
|
|
|
#endif
|
2003-11-21 17:02:21 +03:00
|
|
|
char *group_ptr;
|
|
|
|
int old_desc_blocks;
|
2009-08-24 03:20:03 +04:00
|
|
|
struct ext2fs_numeric_progress_struct progress;
|
2008-08-28 07:07:54 +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;
|
2007-12-16 03:39:37 +03:00
|
|
|
feature_incompat = fs->super->s_feature_incompat;
|
1997-04-26 17:58:21 +04:00
|
|
|
|
2005-09-25 04:14:51 +04:00
|
|
|
fs->super->s_wtime = fs->now ? fs->now : time(NULL);
|
2001-01-01 19:17:12 +03:00
|
|
|
fs->super->s_block_group_nr = 0;
|
2012-07-31 02:56:04 +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.
|
|
|
|
*
|
|
|
|
* Bitmap checksums live in the group descriptor, so the
|
|
|
|
* bitmaps need to be written before the descriptors.
|
|
|
|
*/
|
|
|
|
if (fs->write_bitmaps) {
|
|
|
|
retval = fs->write_bitmaps(fs);
|
|
|
|
if (retval)
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare the group descriptors for writing */
|
2007-08-11 09:56:48 +04:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
retval = EXT2_ET_NO_MEMORY;
|
|
|
|
retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow);
|
|
|
|
if (retval)
|
|
|
|
goto errout;
|
2008-08-28 07:07:54 +04:00
|
|
|
retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
|
2007-12-10 01:03:01 +03:00
|
|
|
&group_shadow);
|
2007-08-11 09:56:48 +04:00
|
|
|
if (retval)
|
|
|
|
goto errout;
|
2012-08-03 04:47:44 +04:00
|
|
|
memcpy(super_shadow, fs->super, sizeof(struct ext2_super_block));
|
2009-10-26 04:46:01 +03:00
|
|
|
memcpy(group_shadow, fs->group_desc, (size_t) fs->blocksize *
|
2007-08-11 09:56:48 +04:00
|
|
|
fs->desc_blocks);
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2007-08-11 09:56:48 +04:00
|
|
|
/* swap the group descriptors */
|
2013-12-24 01:03:41 +04:00
|
|
|
for (j = 0; j < fs->group_desc_count; j++) {
|
2009-10-26 04:46:01 +03:00
|
|
|
gdp = ext2fs_group_desc(fs, group_shadow, j);
|
|
|
|
ext2fs_swap_group_desc2(fs, gdp);
|
1997-04-26 17:58:21 +04:00
|
|
|
}
|
ChangeLog, Makefile.in, swapfs.c, unix.c:
Makefile.in: Add message.c and swapfs.c to the list of source files to
build the make depend.
swapfs.c, unix.c: Only support the -s and -S options to e2fsck if
ENABLE_SWAPFS is defined.
Many files:
ext2fs.h, bitops.h, block.c, bmap.c, closefs.c, dirblock.c, inode.c,
native.c, openfs.c, rw_bitmaps.c, swapfs.c: Only include the
byte-swapping logic if ENABLE_SWAPFS is turned on or if we're on a
big-endian machine.
initialize.c (ext2fs_initialize):Use WORDS_BIGENDIAN directly to set
EXT2_FLAG_SWAP_BYTES, instead of using ext2fs_native_flag.
native.c (ext2fs_native_flag): Use WORDS_BIGENDIAN provided by
autoconf to determine whether or not return EXT2_FLAG_SWAP_BYTES.
2001-06-11 11:00:04 +04:00
|
|
|
#else
|
|
|
|
super_shadow = fs->super;
|
Convert to use block group accessor functions
Convert direct accesses to use the following block group accessor
functions: ext2fs_block_bitmap_loc(), ext2fs_inode_bitmap_loc(),
ext2fs_inode_table_loc(), ext2fs_bg_itable_unused(),
ext2fs_block_bitmap_loc_set(), ext2fs_inode_bitmap_loc_set(),
ext2fs_inode_table_loc_set(), ext2fs_bg_free_inodes_count(),
ext2fs_ext2fs_bg_used_dirs_count(), ext2fs_bg_free_inodes_count_set(),
ext2fs_bg_free_blocks_count_set(), ext2fs_bg_used_dirs_count_set()
Signed-off-by: Valerie Aurora Henson <vaurora@redhat.com>
Signed-off-by: Nick Dokos <nicholas.dokos@hp.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-10-26 04:43:47 +03:00
|
|
|
group_shadow = ext2fs_group_desc(fs, fs->group_desc, 0);
|
ChangeLog, Makefile.in, swapfs.c, unix.c:
Makefile.in: Add message.c and swapfs.c to the list of source files to
build the make depend.
swapfs.c, unix.c: Only support the -s and -S options to e2fsck if
ENABLE_SWAPFS is defined.
Many files:
ext2fs.h, bitops.h, block.c, bmap.c, closefs.c, dirblock.c, inode.c,
native.c, openfs.c, rw_bitmaps.c, swapfs.c: Only include the
byte-swapping logic if ENABLE_SWAPFS is turned on or if we're on a
big-endian machine.
initialize.c (ext2fs_initialize):Use WORDS_BIGENDIAN directly to set
EXT2_FLAG_SWAP_BYTES, instead of using ext2fs_native_flag.
native.c (ext2fs_native_flag): Use WORDS_BIGENDIAN provided by
autoconf to determine whether or not return EXT2_FLAG_SWAP_BYTES.
2001-06-11 11:00:04 +04:00
|
|
|
#endif
|
2008-08-28 07:07:54 +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
|
2004-05-27 05:29:14 +04:00
|
|
|
* already been backed up earlier, and will be restored after
|
|
|
|
* we write out the backup superblocks.)
|
1997-04-26 17:34:30 +04:00
|
|
|
*/
|
|
|
|
fs->super->s_state &= ~EXT2_VALID_FS;
|
2007-12-16 03:39:37 +03:00
|
|
|
fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
|
1997-04-26 17:34:30 +04:00
|
|
|
|
2005-11-13 07:30:39 +03:00
|
|
|
/*
|
|
|
|
* If this is an external journal device, don't write out the
|
|
|
|
* block group descriptors or any of the backup superblocks
|
|
|
|
*/
|
|
|
|
if (fs->super->s_feature_incompat &
|
|
|
|
EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
|
|
|
|
goto write_primary_superblock_only;
|
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
/*
|
|
|
|
* Write out the master group descriptors, and the backup
|
|
|
|
* superblocks and group descriptors.
|
|
|
|
*/
|
2003-11-21 17:02:21 +03:00
|
|
|
group_ptr = (char *) group_shadow;
|
2014-08-09 20:24:54 +04:00
|
|
|
if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
|
2003-11-21 17:02:21 +03:00
|
|
|
old_desc_blocks = fs->super->s_first_meta_bg;
|
2014-08-09 20:24:54 +04:00
|
|
|
if (old_desc_blocks > fs->super->s_first_meta_bg)
|
|
|
|
old_desc_blocks = fs->desc_blocks;
|
|
|
|
} else
|
2003-11-21 17:02:21 +03:00
|
|
|
old_desc_blocks = fs->desc_blocks;
|
|
|
|
|
2012-07-31 01:16:51 +04:00
|
|
|
if (fs->progress_ops && fs->progress_ops->init)
|
|
|
|
(fs->progress_ops->init)(fs, &progress, NULL,
|
|
|
|
fs->group_desc_count);
|
2009-08-24 03:20:03 +04:00
|
|
|
|
|
|
|
|
2002-11-08 17:21:46 +03:00
|
|
|
for (i = 0; i < fs->group_desc_count; i++) {
|
2009-07-12 05:29:30 +04:00
|
|
|
blk64_t super_blk, old_desc_blk, new_desc_blk;
|
2003-11-21 17:02:21 +03:00
|
|
|
|
2012-07-31 01:16:51 +04:00
|
|
|
if (fs->progress_ops && fs->progress_ops->update)
|
|
|
|
(fs->progress_ops->update)(fs, &progress, i);
|
2009-07-12 05:29:30 +04:00
|
|
|
ext2fs_super_and_bgd_loc2(fs, i, &super_blk, &old_desc_blk,
|
|
|
|
&new_desc_blk, 0);
|
2003-11-21 17:02:21 +03:00
|
|
|
|
|
|
|
if (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) &&i && super_blk) {
|
|
|
|
retval = write_backup_super(fs, i, super_blk,
|
2002-10-20 08:38:57 +04:00
|
|
|
super_shadow);
|
1997-04-26 17:58:21 +04:00
|
|
|
if (retval)
|
|
|
|
goto errout;
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
2003-11-21 17:02:21 +03:00
|
|
|
if (fs->flags & EXT2_FLAG_SUPER_ONLY)
|
|
|
|
continue;
|
2008-08-28 07:07:54 +04:00
|
|
|
if ((old_desc_blk) &&
|
2003-11-21 17:02:21 +03:00
|
|
|
(!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) || (i == 0))) {
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_write_blk64(fs->io,
|
2003-11-21 17:02:21 +03:00
|
|
|
old_desc_blk, old_desc_blocks, group_ptr);
|
|
|
|
if (retval)
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
if (new_desc_blk) {
|
2009-07-12 05:29:30 +04:00
|
|
|
int meta_bg = i / EXT2_DESC_PER_BLOCK(fs->super);
|
|
|
|
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_write_blk64(fs->io, new_desc_blk,
|
2003-11-21 17:02:21 +03:00
|
|
|
1, group_ptr + (meta_bg*fs->blocksize));
|
|
|
|
if (retval)
|
1997-04-26 17:58:21 +04:00
|
|
|
goto errout;
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 01:16:51 +04:00
|
|
|
if (fs->progress_ops && fs->progress_ops->close)
|
|
|
|
(fs->progress_ops->close)(fs, &progress, NULL);
|
2009-08-24 03:20:03 +04:00
|
|
|
|
2004-05-27 05:29:14 +04:00
|
|
|
write_primary_superblock_only:
|
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
|
|
|
/*
|
2004-05-27 05:29:14 +04:00
|
|
|
* Write out master superblock. This has to be done
|
|
|
|
* separately, since it is located at a fixed location
|
|
|
|
* (SUPERBLOCK_OFFSET). We flush all other pending changes
|
|
|
|
* out to disk first, just to avoid a race condition with an
|
|
|
|
* insy-tinsy window....
|
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
|
|
|
*/
|
2005-11-13 07:30:39 +03:00
|
|
|
|
|
|
|
fs->super->s_block_group_nr = 0;
|
|
|
|
fs->super->s_state = fs_state;
|
2007-12-16 03:39:37 +03:00
|
|
|
fs->super->s_feature_incompat = feature_incompat;
|
2007-08-11 09:56:48 +04:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
*super_shadow = *fs->super;
|
|
|
|
ext2fs_swap_super(super_shadow);
|
2005-11-13 07:30:39 +03:00
|
|
|
#endif
|
|
|
|
|
2012-08-03 04:47:44 +04:00
|
|
|
retval = ext2fs_superblock_csum_set(fs, super_shadow);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
2011-09-24 18:50:42 +04:00
|
|
|
if (!(flags & EXT2_FLAG_FLUSH_NO_SYNC))
|
|
|
|
retval = io_channel_flush(fs->io);
|
2004-05-27 05:29:14 +04:00
|
|
|
retval = write_primary_superblock(fs, super_shadow);
|
|
|
|
if (retval)
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
fs->flags &= ~EXT2_FLAG_DIRTY;
|
|
|
|
|
2011-09-24 18:50:42 +04:00
|
|
|
if (!(flags & EXT2_FLAG_FLUSH_NO_SYNC))
|
|
|
|
retval = io_channel_flush(fs->io);
|
1997-04-26 17:58:21 +04:00
|
|
|
errout:
|
|
|
|
fs->super->s_state = fs_state;
|
2007-08-11 09:56:48 +04:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
if (super_shadow)
|
|
|
|
ext2fs_free_mem(&super_shadow);
|
|
|
|
if (group_shadow)
|
|
|
|
ext2fs_free_mem(&group_shadow);
|
|
|
|
#endif
|
1997-04-26 17:58:21 +04:00
|
|
|
return retval;
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
|
|
|
|
2014-07-06 05:06:29 +04:00
|
|
|
errcode_t ext2fs_close_free(ext2_filsys *fs_ptr)
|
|
|
|
{
|
|
|
|
errcode_t ret;
|
|
|
|
ext2_filsys fs = *fs_ptr;
|
|
|
|
|
|
|
|
ret = ext2fs_close2(fs, 0);
|
|
|
|
if (ret)
|
|
|
|
ext2fs_free(fs);
|
|
|
|
*fs_ptr = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
errcode_t ext2fs_close(ext2_filsys fs)
|
2011-09-24 18:50:42 +04:00
|
|
|
{
|
|
|
|
return ext2fs_close2(fs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
errcode_t ext2fs_close2(ext2_filsys fs, int flags)
|
1997-04-26 17:21:57 +04:00
|
|
|
{
|
|
|
|
errcode_t retval;
|
2009-03-06 03:40:20 +03:00
|
|
|
int meta_blks;
|
|
|
|
io_stats stats = 0;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-04-26 17:34:30 +04:00
|
|
|
EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
|
|
|
|
|
2009-03-06 03:40:20 +03:00
|
|
|
if (fs->write_bitmaps) {
|
|
|
|
retval = fs->write_bitmaps(fs);
|
1997-04-26 17:21:57 +04:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
}
|
2009-03-06 03:40:20 +03:00
|
|
|
if (fs->super->s_kbytes_written &&
|
|
|
|
fs->io->manager->get_stats)
|
|
|
|
fs->io->manager->get_stats(fs->io, &stats);
|
|
|
|
if (stats && stats->bytes_written && (fs->flags & EXT2_FLAG_RW)) {
|
|
|
|
fs->super->s_kbytes_written += stats->bytes_written >> 10;
|
|
|
|
meta_blks = fs->desc_blocks + 1;
|
|
|
|
if (!(fs->flags & EXT2_FLAG_SUPER_ONLY))
|
|
|
|
fs->super->s_kbytes_written += meta_blks /
|
|
|
|
(fs->blocksize / 1024);
|
2009-05-03 04:59:04 +04:00
|
|
|
if ((fs->flags & EXT2_FLAG_DIRTY) == 0)
|
|
|
|
fs->flags |= EXT2_FLAG_SUPER_ONLY | EXT2_FLAG_DIRTY;
|
2009-03-06 03:40:20 +03:00
|
|
|
}
|
|
|
|
if (fs->flags & EXT2_FLAG_DIRTY) {
|
2011-09-24 18:50:42 +04:00
|
|
|
retval = ext2fs_flush2(fs, flags);
|
1997-04-29 20:15:03 +04:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
}
|
2011-09-24 21:48:55 +04:00
|
|
|
|
|
|
|
retval = ext2fs_mmp_stop(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
|
|
|
|