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>
27 #if EXT2_FLAT_INCLUDES
30 #include <linux/ext2_fs.h>
35 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, int group,
36 ext2fs_block_bitmap bmap)
39 blk_t group_blk, start_blk, last_blk, new_blk, blk;
42 group_blk = fs->super->s_first_data_block +
43 (group * fs->super->s_blocks_per_group);
45 last_blk = group_blk + fs->super->s_blocks_per_group;
46 if (last_blk >= fs->super->s_blocks_count)
47 last_blk = fs->super->s_blocks_count - 1;
49 start_blk = group_blk + 3 + fs->desc_blocks;
50 if (start_blk > last_blk)
51 start_blk = group_blk;
57 * Allocate the inode table
59 if (!fs->group_desc[group].bg_inode_table) {
60 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
61 fs->inode_blocks_per_group,
65 for (j=0, blk = new_blk;
66 j < fs->inode_blocks_per_group;
68 ext2fs_mark_block_bitmap(bmap, blk);
69 fs->group_desc[group].bg_inode_table = new_blk;
73 * Allocate the block and inode bitmaps, if necessary
76 start_blk += fs->inode_blocks_per_group;
77 start_blk += ((fs->stride * group) %
78 (last_blk - start_blk));
79 if (start_blk > last_blk)
80 /* should never happen */
81 start_blk = group_blk;
83 start_blk = group_blk;
85 if (!fs->group_desc[group].bg_block_bitmap) {
86 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
88 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
89 retval = ext2fs_get_free_blocks(fs, group_blk,
90 last_blk, 1, bmap, &new_blk);
93 ext2fs_mark_block_bitmap(bmap, new_blk);
94 fs->group_desc[group].bg_block_bitmap = new_blk;
97 if (!fs->group_desc[group].bg_inode_bitmap) {
98 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
100 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
101 retval = ext2fs_get_free_blocks(fs, group_blk,
102 last_blk, 1, bmap, &new_blk);
105 ext2fs_mark_block_bitmap(bmap, new_blk);
106 fs->group_desc[group].bg_inode_bitmap = new_blk;
113 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
118 for (i = 0; i < fs->group_desc_count; i++) {
119 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);