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>
31 * This routine searches for free blocks that can allocate a full
32 * group of bitmaps or inode tables for a flexbg group. Returns the
33 * block number with a correct offset were the bitmaps and inode
34 * tables can be allocated continously and in order.
36 static blk_t flexbg_offset(ext2_filsys fs, dgrp_t group, blk_t start_blk,
37 ext2fs_block_bitmap bmap, int offset, int size)
39 int flexbg, flexbg_size, elem_size;
40 blk_t last_blk, first_free = 0;
43 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
44 flexbg = group / flexbg_size;
46 if (size > (int) (fs->super->s_blocks_per_group / 8))
47 size = (int) fs->super->s_blocks_per_group / 8;
53 * Don't do a long search if the previous block
54 * search is still valid.
56 if (start_blk && group % flexbg_size) {
57 if (size > flexbg_size)
58 elem_size = fs->inode_blocks_per_group;
61 if (ext2fs_test_block_bitmap_range(bmap, start_blk + elem_size,
63 return start_blk + elem_size;
66 start_blk = ext2fs_group_first_block(fs, flexbg_size * flexbg);
67 last_grp = group | (flexbg_size - 1);
68 if (last_grp > fs->group_desc_count)
69 last_grp = fs->group_desc_count;
70 last_blk = ext2fs_group_last_block(fs, last_grp);
72 /* Find the first available block */
73 if (ext2fs_get_free_blocks(fs, start_blk, last_blk, 1, bmap,
77 if (ext2fs_get_free_blocks(fs, first_free + offset, last_blk, size,
84 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
85 ext2fs_block_bitmap bmap)
88 blk_t group_blk, start_blk, last_blk, new_blk, blk;
90 int j, rem_grps = 0, flexbg_size = 0;
92 group_blk = ext2fs_group_first_block(fs, group);
93 last_blk = ext2fs_group_last_block(fs, group);
98 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
99 EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
100 fs->super->s_log_groups_per_flex) {
101 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
102 last_grp = group | (flexbg_size - 1);
103 rem_grps = last_grp - group;
104 if (last_grp > fs->group_desc_count)
105 last_grp = fs->group_desc_count;
109 * Allocate the block and inode bitmaps, if necessary
112 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
113 1, bmap, &start_blk);
116 start_blk += fs->inode_blocks_per_group;
117 start_blk += ((fs->stride * group) %
118 (last_blk - start_blk + 1));
119 if (start_blk >= last_blk)
120 start_blk = group_blk;
122 start_blk = group_blk;
126 if (group && fs->group_desc[group-1].bg_block_bitmap)
127 prev_block = fs->group_desc[group-1].bg_block_bitmap;
128 start_blk = flexbg_offset(fs, group, prev_block, bmap,
130 last_blk = ext2fs_group_last_block(fs, last_grp);
133 if (!fs->group_desc[group].bg_block_bitmap) {
134 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
136 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
137 retval = ext2fs_get_free_blocks(fs, group_blk,
138 last_blk, 1, bmap, &new_blk);
141 ext2fs_mark_block_bitmap(bmap, new_blk);
142 fs->group_desc[group].bg_block_bitmap = new_blk;
144 dgrp_t gr = ext2fs_group_of_blk(fs, new_blk);
145 fs->group_desc[gr].bg_free_blocks_count--;
146 fs->super->s_free_blocks_count--;
147 fs->group_desc[gr].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
148 ext2fs_group_desc_csum_set(fs, gr);
154 if (group && fs->group_desc[group-1].bg_inode_bitmap)
155 prev_block = fs->group_desc[group-1].bg_inode_bitmap;
156 start_blk = flexbg_offset(fs, group, prev_block, bmap,
157 flexbg_size, rem_grps);
158 last_blk = ext2fs_group_last_block(fs, last_grp);
161 if (!fs->group_desc[group].bg_inode_bitmap) {
162 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
164 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
165 retval = ext2fs_get_free_blocks(fs, group_blk,
166 last_blk, 1, bmap, &new_blk);
169 ext2fs_mark_block_bitmap(bmap, new_blk);
170 fs->group_desc[group].bg_inode_bitmap = new_blk;
172 dgrp_t gr = ext2fs_group_of_blk(fs, new_blk);
173 fs->group_desc[gr].bg_free_blocks_count--;
174 fs->super->s_free_blocks_count--;
175 fs->group_desc[gr].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
176 ext2fs_group_desc_csum_set(fs, gr);
181 * Allocate the inode table
185 if (group && fs->group_desc[group-1].bg_inode_table)
186 prev_block = fs->group_desc[group-1].bg_inode_table;
187 group_blk = flexbg_offset(fs, group, prev_block, bmap,
189 fs->inode_blocks_per_group *
191 last_blk = ext2fs_group_last_block(fs, last_grp);
194 if (!fs->group_desc[group].bg_inode_table) {
195 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
196 fs->inode_blocks_per_group,
200 for (j=0, blk = new_blk;
201 j < fs->inode_blocks_per_group;
203 ext2fs_mark_block_bitmap(bmap, blk);
205 dgrp_t gr = ext2fs_group_of_blk(fs, blk);
206 fs->group_desc[gr].bg_free_blocks_count--;
207 fs->super->s_free_blocks_count--;
208 fs->group_desc[gr].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
209 ext2fs_group_desc_csum_set(fs, gr);
212 fs->group_desc[group].bg_inode_table = new_blk;
214 ext2fs_group_desc_csum_set(fs, group);
218 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
223 for (i = 0; i < fs->group_desc_count; i++) {
224 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);