ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

bool ext2_initialize_sb ( PEXT2_FILESYS  Ext2Sys)

Definition at line 74 of file Super.c.

Referenced by Ext2Format().

{
    int frags_per_block = 0;
    ULONG overhead      = 0;
    int rem             = 0;
    ULONG   i = 0;
    ULONG   group_block = 0;
    ULONG   numblocks = 0;
    PEXT2_SUPER_BLOCK pExt2Sb = Ext2Sys->ext2_sb;
    LARGE_INTEGER   SysTime;
    
    NtQuerySystemTime(&SysTime);

    Ext2Sys->blocksize = EXT2_BLOCK_SIZE(pExt2Sb);
    Ext2Sys->fragsize = EXT2_FRAG_SIZE(pExt2Sb);
    frags_per_block = Ext2Sys->blocksize / Ext2Sys->fragsize;

    pExt2Sb->s_magic = EXT2_SUPER_MAGIC;
    pExt2Sb->s_state = EXT2_VALID_FS;

    pExt2Sb->s_first_data_block =  (pExt2Sb->s_log_block_size) ? 0 : 1;
    pExt2Sb->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;

    pExt2Sb->s_errors = EXT2_ERRORS_DEFAULT;

    pExt2Sb->s_checkinterval = EXT2_DFL_CHECKINTERVAL;

    if (!pExt2Sb->s_rev_level)
        pExt2Sb->s_rev_level = EXT2_GOOD_OLD_REV;

    if (pExt2Sb->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);
    }

    RtlTimeToSecondsSince1970(&SysTime, &pExt2Sb->s_wtime);
    pExt2Sb->s_lastcheck = pExt2Sb->s_mtime = pExt2Sb->s_wtime;

    if (!pExt2Sb->s_blocks_per_group)
        pExt2Sb->s_blocks_per_group = Ext2Sys->blocksize * 8;

    pExt2Sb->s_frags_per_group = pExt2Sb->s_blocks_per_group *  frags_per_block;
    pExt2Sb->s_creator_os = EXT2_OS_WINNT;

    if (pExt2Sb->s_r_blocks_count >= pExt2Sb->s_blocks_count)
    {
        goto cleanup;
    }

    /*
     * If we're creating an external journal device, we don't need
     * to bother with the rest.
     */
    if (pExt2Sb->s_feature_incompat &
        EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
    {
        Ext2Sys->group_desc_count = 0;
        // ext2fs_mark_super_dirty(fs);
        return true;
    }

retry:

    Ext2Sys->group_desc_count = (pExt2Sb->s_blocks_count - pExt2Sb->s_first_data_block
        + EXT2_BLOCKS_PER_GROUP(pExt2Sb) - 1) / EXT2_BLOCKS_PER_GROUP(pExt2Sb);

    if (Ext2Sys->group_desc_count == 0)
        return false;

    Ext2Sys->desc_blocks = (Ext2Sys->group_desc_count +  EXT2_DESC_PER_BLOCK(pExt2Sb)
        - 1) / EXT2_DESC_PER_BLOCK(pExt2Sb);

    if (!pExt2Sb->s_inodes_count)
        pExt2Sb->s_inodes_count = pExt2Sb->s_blocks_count / ( inode_ratio /Ext2Sys->blocksize);

    /*
     * Make sure we have at least EXT2_FIRST_INO + 1 inodes, so
     * that we have enough inodes for the filesystem(!)
     */
    if (pExt2Sb->s_inodes_count < EXT2_FIRST_INODE(pExt2Sb)+1)
        pExt2Sb->s_inodes_count = EXT2_FIRST_INODE(pExt2Sb)+1;

    /*
     * There should be at least as many inodes as the user
     * requested.  Figure out how many inodes per group that
     * should be.  But make sure that we don't allocate more than
     * one bitmap's worth of inodes
     */
    pExt2Sb->s_inodes_per_group = (pExt2Sb->s_inodes_count + Ext2Sys->group_desc_count - 1)
        /Ext2Sys->group_desc_count;

    if (pExt2Sb->s_inodes_per_group > (ULONG)(Ext2Sys->blocksize*8))
        pExt2Sb->s_inodes_per_group = Ext2Sys->blocksize*8;

    /*
     * 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...
     */
    Ext2Sys->inode_blocks_per_group = (((pExt2Sb->s_inodes_per_group * EXT2_INODE_SIZE(pExt2Sb))
        + EXT2_BLOCK_SIZE(pExt2Sb) - 1) / EXT2_BLOCK_SIZE(pExt2Sb));

    pExt2Sb->s_inodes_per_group = ((Ext2Sys->inode_blocks_per_group * EXT2_BLOCK_SIZE(pExt2Sb))
        / EXT2_INODE_SIZE(pExt2Sb));

    /*
     * Finally, make sure the number of inodes per group is a
     * multiple of 8.  This is needed to simplify the bitmap
     * splicing code.
     */
    pExt2Sb->s_inodes_per_group &= ~7;
    Ext2Sys->inode_blocks_per_group = (((pExt2Sb->s_inodes_per_group * EXT2_INODE_SIZE(pExt2Sb))
        + EXT2_BLOCK_SIZE(pExt2Sb) - 1) / EXT2_BLOCK_SIZE(pExt2Sb));

    /*
     * adjust inode count to reflect the adjusted inodes_per_group
     */
    pExt2Sb->s_inodes_count = pExt2Sb->s_inodes_per_group * Ext2Sys->group_desc_count;
    pExt2Sb->s_free_inodes_count = pExt2Sb->s_inodes_count;

    /*
     * 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 + Ext2Sys->desc_blocks + Ext2Sys->inode_blocks_per_group);

    /*
     * 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) ((pExt2Sb->s_blocks_count - pExt2Sb->s_first_data_block) %
             pExt2Sb->s_blocks_per_group);

    if ((Ext2Sys->group_desc_count == 1) && rem && (rem < overhead))
        return false;

    if (rem && (rem < overhead+50))
    {
        pExt2Sb->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.
     */

    // Allocate block bitmap
    if(!ext2_allocate_block_bitmap(Ext2Sys))
    {
        goto cleanup;
    }

    // Allocate inode bitmap
    if(!ext2_allocate_inode_bitmap(Ext2Sys))
    {
        goto cleanup;
    }

    // Allocate gourp desc
    if(!ext2_allocate_group_desc(Ext2Sys))
    {
        goto cleanup;
    }

    /*
     * 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.
     */
    group_block = pExt2Sb->s_first_data_block;
    numblocks = 0;

    pExt2Sb->s_free_blocks_count = 0;

    for (i = 0; i < Ext2Sys->group_desc_count; i++)
    {
        if (i == Ext2Sys->group_desc_count-1)
        {
            numblocks = (pExt2Sb->s_blocks_count - pExt2Sb->s_first_data_block)
                % pExt2Sb->s_blocks_per_group;

            if (!numblocks)
                numblocks = pExt2Sb->s_blocks_per_group;
        }
        else
        {
            numblocks = pExt2Sb->s_blocks_per_group;
        }

        if (ext2_bg_has_super(pExt2Sb, i))
        {
            ULONG j;

            for (j=0; j < Ext2Sys->desc_blocks+1; j++)
                ext2_mark_bitmap(Ext2Sys->block_map, group_block + j);

            numblocks -= 1 + Ext2Sys->desc_blocks;
        }
        
        numblocks -= 2 + Ext2Sys->inode_blocks_per_group;
        
        pExt2Sb->s_free_blocks_count += numblocks;
        Ext2Sys->group_desc[i].bg_free_blocks_count = (__u16)numblocks;
        Ext2Sys->group_desc[i].bg_free_inodes_count = (__u16)pExt2Sb->s_inodes_per_group;
        Ext2Sys->group_desc[i].bg_used_dirs_count = 0;
        
        group_block += pExt2Sb->s_blocks_per_group;
    }

    return true;

cleanup:

    ext2_free_group_desc(Ext2Sys);
    ext2_free_block_bitmap(Ext2Sys);
    ext2_free_inode_bitmap(Ext2Sys);

    return false;
}

Generated on Wed May 23 2012 06:01:44 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.