e2fsprogs/lib/ext2fs/initialize.c

364 lines
11 KiB
C
Raw Normal View History

1997-04-26 17:21:57 +04:00
/*
* initialize.c --- initialize a filesystem handle given superblock
* parameters. Used by mke2fs when initializing a filesystem.
1997-04-29 18:53:37 +04:00
*
* Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
*
1997-04-29 20:17:09 +04:00
* %Begin-Header%
1997-04-29 18:53:37 +04:00
* This file may be redistributed under the terms of the GNU Public
* License.
1997-04-29 20:17:09 +04:00
* %End-Header%
1997-04-26 17:21:57 +04:00
*/
#include <stdio.h>
#include <string.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 <fcntl.h>
#include <time.h>
#if HAVE_SYS_STAT_H
1997-04-26 17:21:57 +04:00
#include <sys/stat.h>
#endif
#if HAVE_SYS_TYPES_H
1997-04-26 17:21:57 +04:00
#include <sys/types.h>
#endif
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-26 17:21:57 +04:00
#include "ext2fs.h"
1997-04-26 17:58:21 +04:00
#if defined(__linux__) && defined(EXT2_OS_LINUX)
#define CREATOR_OS EXT2_OS_LINUX
#elif defined(__GNU__) && defined(EXT2_OS_HURD)
1997-04-26 17:58:21 +04:00
#define CREATOR_OS EXT2_OS_HURD
#elif defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD)
#define CREATOR_OS EXT2_OS_FREEBSD
#elif defined(LITES) && defined(EXT2_OS_LITES)
#define CREATOR_OS EXT2_OS_LITES
#else
#define CREATOR_OS EXT2_OS_LINUX /* by default */
#endif
1997-04-29 18:53:37 +04:00
/*
* Note we override the kernel include file's idea of what the default
* check interval (never) should be. It's a good idea to check at
* least *occasionally*, specially since servers will never rarely get
* to reboot, since Linux is so robust these days. :-)
*
* 180 days (six months) seems like a good value.
*/
#ifdef EXT2_DFL_CHECKINTERVAL
#undef EXT2_DFL_CHECKINTERVAL
#endif
#define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
1997-04-29 18:53:37 +04:00
1997-04-26 17:21:57 +04:00
errcode_t ext2fs_initialize(const char *name, int flags,
struct ext2_super_block *param,
io_manager manager, ext2_filsys *ret_fs)
{
ext2_filsys fs;
errcode_t retval;
struct ext2_super_block *super;
int frags_per_block;
int rem;
int overhead = 0;
blk_t group_block;
int ipg;
1997-04-26 17:21:57 +04:00
int i, j;
blk_t numblocks;
1997-04-26 17:34:30 +04:00
char *buf;
int meta_bg_size, meta_bg, has_super, old_desc_blocks;
1997-04-26 17:21:57 +04:00
if (!param || !param->s_blocks_count)
return EXT2_ET_INVALID_ARGUMENT;
1997-04-26 17:21:57 +04:00
retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys),
(void **) &fs);
if (retval)
return retval;
1997-04-26 17:21:57 +04:00
memset(fs, 0, sizeof(struct struct_ext2_filsys));
1997-04-26 17:34:30 +04:00
fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
fs->flags = flags | EXT2_FLAG_RW;
fs->umask = 022;
#ifdef WORDS_BIGENDIAN
fs->flags |= EXT2_FLAG_SWAP_BYTES;
#endif
1997-04-26 17:21:57 +04:00
retval = manager->open(name, IO_FLAG_RW, &fs->io);
if (retval)
goto cleanup;
fs->io->app_data = fs;
retval = ext2fs_get_mem(strlen(name)+1, (void **) &fs->device_name);
if (retval)
1997-04-26 17:21:57 +04:00
goto cleanup;
1997-04-26 17:21:57 +04:00
strcpy(fs->device_name, name);
retval = ext2fs_get_mem(SUPERBLOCK_SIZE, (void **) &super);
if (retval)
1997-04-26 17:21:57 +04:00
goto cleanup;
fs->super = super;
1997-04-26 17:21:57 +04:00
memset(super, 0, SUPERBLOCK_SIZE);
#define set_field(field, default) (super->field = param->field ? \
param->field : (default))
super->s_magic = EXT2_SUPER_MAGIC;
super->s_state = EXT2_VALID_FS;
set_field(s_log_block_size, 0); /* default blocksize: 1024 bytes */
set_field(s_log_frag_size, 0); /* default fragsize: 1024 bytes */
set_field(s_first_data_block, super->s_log_block_size ? 0 : 1);
set_field(s_max_mnt_count, EXT2_DFL_MAX_MNT_COUNT);
set_field(s_errors, EXT2_ERRORS_DEFAULT);
1997-04-30 01:26:48 +04:00
set_field(s_feature_compat, 0);
set_field(s_feature_incompat, 0);
set_field(s_feature_ro_compat, 0);
set_field(s_first_meta_bg, 0);
1997-04-30 01:26:48 +04:00
if (super->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)
1997-04-29 21:48:10 +04:00
return EXT2_ET_UNSUPP_FEATURE;
1997-04-30 01:26:48 +04:00
if (super->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)
1997-04-29 21:48:10 +04:00
return EXT2_ET_RO_UNSUPP_FEATURE;
1997-04-26 18:48:50 +04:00
set_field(s_rev_level, EXT2_GOOD_OLD_REV);
if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
}
1997-04-26 17:21:57 +04:00
set_field(s_checkinterval, EXT2_DFL_CHECKINTERVAL);
super->s_mkfs_time = super->s_lastcheck = time(NULL);
1997-04-26 17:21:57 +04:00
1997-04-26 17:58:21 +04:00
super->s_creator_os = CREATOR_OS;
1997-04-26 17:34:30 +04:00
1997-04-26 17:21:57 +04:00
fs->blocksize = EXT2_BLOCK_SIZE(super);
fs->fragsize = EXT2_FRAG_SIZE(super);
frags_per_block = fs->blocksize / fs->fragsize;
/* default: (fs->blocksize*8) blocks/group, up to 2^16 (GDT limit) */
set_field(s_blocks_per_group, fs->blocksize * 8);
if (super->s_blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(super))
super->s_blocks_per_group = EXT2_MAX_BLOCKS_PER_GROUP(super);
1997-04-26 17:21:57 +04:00
super->s_frags_per_group = super->s_blocks_per_group * frags_per_block;
super->s_blocks_count = param->s_blocks_count;
1997-04-26 17:34:30 +04:00
super->s_r_blocks_count = param->s_r_blocks_count;
if (super->s_r_blocks_count >= param->s_blocks_count) {
retval = EXT2_ET_INVALID_ARGUMENT;
1997-04-26 17:34:30 +04:00
goto cleanup;
}
1997-04-26 17:21:57 +04:00
/*
* If we're creating an external journal device, we don't need
* to bother with the rest.
*/
if (super->s_feature_incompat &
EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
fs->group_desc_count = 0;
ext2fs_mark_super_dirty(fs);
*ret_fs = fs;
return 0;
}
1997-04-26 17:21:57 +04:00
retry:
fs->group_desc_count = (super->s_blocks_count -
super->s_first_data_block +
EXT2_BLOCKS_PER_GROUP(super) - 1)
/ EXT2_BLOCKS_PER_GROUP(super);
1997-04-26 18:48:50 +04:00
if (fs->group_desc_count == 0)
return EXT2_ET_TOOSMALL;
1997-04-26 17:21:57 +04:00
fs->desc_blocks = (fs->group_desc_count +
EXT2_DESC_PER_BLOCK(super) - 1)
/ EXT2_DESC_PER_BLOCK(super);
i = fs->blocksize >= 4096 ? 1 : 4096 / fs->blocksize;
set_field(s_inodes_count, super->s_blocks_count / i);
1997-04-26 17:21:57 +04:00
/*
* Make sure we have at least EXT2_FIRST_INO + 1 inodes, so
* that we have enough inodes for the filesystem(!)
*/
if (super->s_inodes_count < EXT2_FIRST_INODE(super)+1)
super->s_inodes_count = EXT2_FIRST_INODE(super)+1;
1997-04-26 17:21:57 +04:00
/*
* There should be at least as many inodes as the user
* requested. Figure out how many inodes per group that
1997-04-26 17:58:21 +04:00
* should be. But make sure that we don't allocate more than
* one bitmap's worth of inodes each group.
1997-04-26 17:21:57 +04:00
*/
ipg = (super->s_inodes_count + fs->group_desc_count - 1) /
fs->group_desc_count;
if (ipg > fs->blocksize * 8)
ipg = fs->blocksize * 8;
if (ipg > EXT2_MAX_INODES_PER_GROUP(super))
ipg = EXT2_MAX_INODES_PER_GROUP(super);
super->s_inodes_per_group = ipg;
if (super->s_inodes_count > ipg * fs->group_desc_count)
super->s_inodes_count = ipg * fs->group_desc_count;
1997-04-26 17:21:57 +04:00
/*
* Make sure the number of inodes per group completely fills
* the inode table blocks in the descriptor. If not, add some
* additional inodes/group. Waste not, want not...
*/
1997-04-26 18:48:50 +04:00
fs->inode_blocks_per_group = (((super->s_inodes_per_group *
EXT2_INODE_SIZE(super)) +
EXT2_BLOCK_SIZE(super) - 1) /
EXT2_BLOCK_SIZE(super));
super->s_inodes_per_group = ((fs->inode_blocks_per_group *
EXT2_BLOCK_SIZE(super)) /
EXT2_INODE_SIZE(super));
/*
* Finally, make sure the number of inodes per group is a
* multiple of 8. This is needed to simplify the bitmap
* splicing code.
*/
super->s_inodes_per_group &= ~7;
fs->inode_blocks_per_group = (((super->s_inodes_per_group *
EXT2_INODE_SIZE(super)) +
EXT2_BLOCK_SIZE(super) - 1) /
EXT2_BLOCK_SIZE(super));
1997-04-26 17:21:57 +04:00
/*
* adjust inode count to reflect the adjusted inodes_per_group
*/
super->s_inodes_count = super->s_inodes_per_group *
fs->group_desc_count;
super->s_free_inodes_count = super->s_inodes_count;
1997-04-29 21:57:00 +04:00
/*
* Overhead is the number of bookkeeping blocks per group. It
* includes the superblock backup, the group descriptor
* backups, the inode bitmap, the block bitmap, and the inode
* table.
*
* XXX Not all block groups need the descriptor blocks, but
* being clever is tricky...
*/
overhead = (int) (3 + fs->desc_blocks + fs->inode_blocks_per_group);
1997-04-29 21:57:00 +04:00
1997-04-26 17:21:57 +04:00
/*
* See if the last group is big enough to support the
* necessary data structures. If not, we need to get rid of
* it.
*/
rem = (int) ((super->s_blocks_count - super->s_first_data_block) %
super->s_blocks_per_group);
1997-04-26 17:21:57 +04:00
if ((fs->group_desc_count == 1) && rem && (rem < overhead))
return EXT2_ET_TOOSMALL;
if (rem && (rem < overhead+50)) {
super->s_blocks_count -= rem;
goto retry;
}
/*
* At this point we know how big the filesystem will be. So
* we can do any and all allocations that depend on the block
* count.
*/
retval = ext2fs_get_mem(strlen(fs->device_name) + 80,
(void **) &buf);
if (retval)
1997-04-26 17:34:30 +04:00
goto cleanup;
sprintf(buf, "block bitmap for %s", fs->device_name);
retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
1997-04-26 17:21:57 +04:00
if (retval)
goto cleanup;
1997-04-26 17:34:30 +04:00
sprintf(buf, "inode bitmap for %s", fs->device_name);
1997-04-29 19:29:49 +04:00
retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
1997-04-26 17:21:57 +04:00
if (retval)
goto cleanup;
ext2fs_free_mem((void **) &buf);
1997-04-26 17:34:30 +04:00
retval = ext2fs_get_mem((size_t) fs->desc_blocks * fs->blocksize,
(void **) &fs->group_desc);
if (retval)
1997-04-26 17:21:57 +04:00
goto cleanup;
memset(fs->group_desc, 0, (size_t) fs->desc_blocks * fs->blocksize);
1997-04-26 17:21:57 +04:00
1997-04-26 17:34:30 +04:00
/*
* Reserve the superblock and group descriptors for each
* group, and fill in the correct group statistics for group.
* Note that although the block bitmap, inode bitmap, and
* inode table have not been allocated (and in fact won't be
* by this routine), they are accounted for nevertheless.
*/
1997-04-26 17:21:57 +04:00
group_block = super->s_first_data_block;
1997-04-29 21:48:10 +04:00
super->s_free_blocks_count = 0;
if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
old_desc_blocks = fs->super->s_first_meta_bg;
else
old_desc_blocks = fs->desc_blocks;
1997-04-26 17:21:57 +04:00
for (i = 0; i < fs->group_desc_count; i++) {
1997-04-26 17:34:30 +04:00
if (i == fs->group_desc_count-1) {
numblocks = (fs->super->s_blocks_count -
fs->super->s_first_data_block) %
fs->super->s_blocks_per_group;
if (!numblocks)
numblocks = fs->super->s_blocks_per_group;
} else
numblocks = fs->super->s_blocks_per_group;
1997-04-29 21:48:10 +04:00
has_super = ext2fs_bg_has_super(fs, i);
if (has_super) {
ext2fs_mark_block_bitmap(fs->block_map, group_block);
numblocks--;
}
meta_bg_size = (fs->blocksize /
sizeof (struct ext2_group_desc));
meta_bg = i / 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) {
for (j=0; j < old_desc_blocks; j++)
ext2fs_mark_block_bitmap(fs->block_map,
group_block + j + 1);
numblocks -= old_desc_blocks;
}
} else {
if (has_super)
has_super = 1;
if (((i % meta_bg_size) == 0) ||
((i % meta_bg_size) == 1) ||
((i % meta_bg_size) == (meta_bg_size-1))) {
1997-04-29 21:48:10 +04:00
ext2fs_mark_block_bitmap(fs->block_map,
group_block + has_super);
numblocks--;
}
1997-04-29 21:48:10 +04:00
}
numblocks -= 2 + fs->inode_blocks_per_group;
1997-04-26 17:34:30 +04:00
1997-04-29 21:48:10 +04:00
super->s_free_blocks_count += numblocks;
1997-04-26 17:34:30 +04:00
fs->group_desc[i].bg_free_blocks_count = numblocks;
fs->group_desc[i].bg_free_inodes_count =
fs->super->s_inodes_per_group;
fs->group_desc[i].bg_used_dirs_count = 0;
1997-04-26 17:21:57 +04:00
group_block += super->s_blocks_per_group;
}
ext2fs_mark_super_dirty(fs);
ext2fs_mark_bb_dirty(fs);
ext2fs_mark_ib_dirty(fs);
io_channel_set_blksize(fs->io, fs->blocksize);
*ret_fs = fs;
return 0;
cleanup:
ext2fs_free(fs);
return retval;
}