2 * alloc_sb.c --- Allocate the superblock and block group descriptors for a
3 * newly initialized filesystem. Used by mke2fs when initializing a filesystem
5 * Copyright (C) 1994, 1995, 1996, 2003 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
24 #include <sys/types.h>
31 * This function reserves the superblock and block group descriptors
32 * for a given block group. It currently returns the number of free
33 * blocks assuming that inode table and allocation bitmaps will be in
34 * the group. This is not necessarily the case when the flex_bg
35 * feature is enabled, so callers should take care! It was only
36 * really intended for use by mke2fs, and even there it's not that
37 * useful. In the future, when we redo this function for 64-bit block
38 * numbers, we should probably return the number of blocks used by the
39 * super block and group descriptors instead.
41 * See also the comment for ext2fs_super_and_bgd_loc()
43 int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
45 ext2fs_block_bitmap bmap)
47 blk64_t super_blk, old_desc_blk, new_desc_blk;
49 int j, old_desc_blocks, num_blocks;
51 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
52 &old_desc_blk, &new_desc_blk, &used_blks);
54 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
55 old_desc_blocks = fs->super->s_first_meta_bg;
58 fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
60 if (super_blk || (group == 0))
61 ext2fs_mark_block_bitmap2(bmap, super_blk);
64 if (fs->super->s_reserved_gdt_blocks && fs->block_map == bmap)
65 ext2fs_bg_flag_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
66 for (j=0; j < old_desc_blocks; j++)
67 if (old_desc_blk + j < fs->super->s_blocks_count)
68 ext2fs_mark_block_bitmap2(bmap,
72 ext2fs_mark_block_bitmap2(bmap, new_desc_blk);
74 if (group == fs->group_desc_count-1) {
75 num_blocks = (fs->super->s_blocks_count -
76 fs->super->s_first_data_block) %
77 fs->super->s_blocks_per_group;
79 num_blocks = fs->super->s_blocks_per_group;
81 num_blocks = fs->super->s_blocks_per_group;
83 num_blocks -= 2 + fs->inode_blocks_per_group + used_blks;