2 * alloc_tables.c --- Allocate tables for a newly initialized
3 * filesystem. Used by mke2fs when initializing a filesystem
5 * Copyright (C) 1996 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
24 #include <sys/types.h>
30 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
31 ext2fs_block_bitmap bmap)
34 blk_t group_blk, start_blk, last_blk, new_blk, blk;
37 group_blk = fs->super->s_first_data_block +
38 (group * fs->super->s_blocks_per_group);
40 last_blk = group_blk + fs->super->s_blocks_per_group;
41 if (last_blk >= fs->super->s_blocks_count)
42 last_blk = fs->super->s_blocks_count - 1;
48 * Allocate the block and inode bitmaps, if necessary
51 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
55 start_blk += fs->inode_blocks_per_group;
56 start_blk += ((fs->stride * group) %
57 (last_blk - start_blk));
58 if (start_blk > last_blk)
59 start_blk = group_blk;
61 start_blk = group_blk;
63 if (!fs->group_desc[group].bg_block_bitmap) {
64 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
66 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
67 retval = ext2fs_get_free_blocks(fs, group_blk,
68 last_blk, 1, bmap, &new_blk);
71 ext2fs_mark_block_bitmap(bmap, new_blk);
72 fs->group_desc[group].bg_block_bitmap = new_blk;
75 if (!fs->group_desc[group].bg_inode_bitmap) {
76 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
78 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
79 retval = ext2fs_get_free_blocks(fs, group_blk,
80 last_blk, 1, bmap, &new_blk);
83 ext2fs_mark_block_bitmap(bmap, new_blk);
84 fs->group_desc[group].bg_inode_bitmap = new_blk;
88 * Allocate the inode table
90 if (!fs->group_desc[group].bg_inode_table) {
91 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
92 fs->inode_blocks_per_group,
96 for (j=0, blk = new_blk;
97 j < fs->inode_blocks_per_group;
99 ext2fs_mark_block_bitmap(bmap, blk);
100 fs->group_desc[group].bg_inode_table = new_blk;
109 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
114 for (i = 0; i < fs->group_desc_count; i++) {
115 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);