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
22 #include <sys/types.h>
27 #include <linux/ext2_fs.h>
31 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, int group,
32 ext2fs_block_bitmap bmap)
35 blk_t group_blk, start_blk, last_blk, new_blk, blk;
38 group_blk = fs->super->s_first_data_block +
39 (group * fs->super->s_blocks_per_group);
41 last_blk = group_blk + fs->super->s_blocks_per_group;
42 if (last_blk >= fs->super->s_blocks_count)
43 last_blk = fs->super->s_blocks_count - 1;
45 start_blk = group_blk + 3 + fs->desc_blocks;
46 if (start_blk > last_blk)
47 start_blk = group_blk;
53 * Allocate the inode table
55 if (!fs->group_desc[group].bg_inode_table) {
56 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
57 fs->inode_blocks_per_group,
61 for (j=0, blk = new_blk;
62 j < fs->inode_blocks_per_group;
64 ext2fs_mark_block_bitmap(bmap, blk);
65 fs->group_desc[group].bg_inode_table = new_blk;
69 * Allocate the block and inode bitmaps, if necessary
72 start_blk += fs->inode_blocks_per_group;
73 start_blk += ((fs->stride * group) %
74 (last_blk - start_blk));
75 if (start_blk > last_blk)
76 /* should never happen */
77 start_blk = group_blk;
79 start_blk = group_blk;
81 if (!fs->group_desc[group].bg_block_bitmap) {
82 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
86 ext2fs_mark_block_bitmap(bmap, new_blk);
87 fs->group_desc[group].bg_block_bitmap = new_blk;
90 if (!fs->group_desc[group].bg_inode_bitmap) {
91 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
95 ext2fs_mark_block_bitmap(bmap, new_blk);
96 fs->group_desc[group].bg_inode_bitmap = new_blk;
103 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
108 for (i = 0; i < fs->group_desc_count; i++) {
109 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);