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 Library
9 * General Public License, version 2.
24 #include <sys/types.h>
32 * This routine searches for free blocks that can allocate a full
33 * group of bitmaps or inode tables for a flexbg group. Returns the
34 * block number with a correct offset were the bitmaps and inode
35 * tables can be allocated continously and in order.
37 static blk64_t flexbg_offset(ext2_filsys fs, dgrp_t group, blk64_t start_blk,
38 ext2fs_block_bitmap bmap, int rem_grp,
41 int flexbg, flexbg_size, size;
42 blk64_t last_blk, first_free = 0;
45 size = rem_grp * elem_size;
47 if (size > (int) (fs->super->s_blocks_per_group / 8))
48 size = (int) fs->super->s_blocks_per_group / 8;
51 * Don't do a long search if the previous block
52 * search is still valid.
54 if (start_blk && ext2fs_test_block_bitmap_range2(bmap, start_blk,
58 start_blk = ext2fs_group_first_block2(fs, flexbg_size * flexbg);
59 last_grp = group | (flexbg_size - 1);
60 if (last_grp > fs->group_desc_count-1)
61 last_grp = fs->group_desc_count-1;
62 last_blk = ext2fs_group_last_block2(fs, last_grp);
64 /* Find the first available block */
65 if (ext2fs_get_free_blocks2(fs, start_blk, last_blk, size,
66 bmap, &first_free) == 0)
69 if (ext2fs_get_free_blocks2(fs, start_blk, last_blk, elem_size,
70 bmap, &first_free) == 0)
73 if (ext2fs_get_free_blocks2(fs, 0, last_blk, elem_size, bmap,
80 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
81 ext2fs_block_bitmap bmap)
84 blk64_t group_blk, start_blk, last_blk, new_blk, blk;
86 int j, rem_grps = 0, flexbg_size = 0;
88 group_blk = ext2fs_group_first_block2(fs, group);
89 last_blk = ext2fs_group_last_block2(fs, group);
94 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
95 EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
96 fs->super->s_log_groups_per_flex) {
97 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
98 last_grp = group | (flexbg_size - 1);
99 if (last_grp > fs->group_desc_count-1)
100 last_grp = fs->group_desc_count-1;
101 rem_grps = last_grp - group + 1;
105 * Allocate the block and inode bitmaps, if necessary
108 retval = ext2fs_get_free_blocks2(fs, group_blk, last_blk,
109 1, bmap, &start_blk);
112 start_blk += fs->inode_blocks_per_group;
113 start_blk += ((fs->stride * group) %
114 (last_blk - start_blk + 1));
115 if (start_blk >= last_blk)
116 start_blk = group_blk;
118 start_blk = group_blk;
121 blk64_t prev_block = 0;
123 if (group % flexbg_size)
124 prev_block = ext2fs_block_bitmap_loc(fs, group - 1) + 1;
125 start_blk = flexbg_offset(fs, group, prev_block, bmap,
127 last_blk = ext2fs_group_last_block2(fs, last_grp);
130 if (!ext2fs_block_bitmap_loc(fs, group)) {
131 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
133 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
134 retval = ext2fs_get_free_blocks2(fs, group_blk,
135 last_blk, 1, bmap, &new_blk);
138 ext2fs_mark_block_bitmap2(bmap, new_blk);
139 ext2fs_block_bitmap_loc_set(fs, group, new_blk);
141 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
142 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
143 ext2fs_free_blocks_count_add(fs->super, -1);
144 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
145 ext2fs_group_desc_csum_set(fs, gr);
150 blk64_t prev_block = 0;
151 if (group % flexbg_size)
152 prev_block = ext2fs_inode_bitmap_loc(fs, group - 1) + 1;
154 prev_block = ext2fs_block_bitmap_loc(fs, group) +
156 start_blk = flexbg_offset(fs, group, prev_block, bmap,
158 last_blk = ext2fs_group_last_block2(fs, last_grp);
161 if (!ext2fs_inode_bitmap_loc(fs, group)) {
162 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
164 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
165 retval = ext2fs_get_free_blocks2(fs, group_blk,
166 last_blk, 1, bmap, &new_blk);
169 ext2fs_mark_block_bitmap2(bmap, new_blk);
170 ext2fs_inode_bitmap_loc_set(fs, group, new_blk);
172 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
173 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
174 ext2fs_free_blocks_count_add(fs->super, -1);
175 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
176 ext2fs_group_desc_csum_set(fs, gr);
181 * Allocate the inode table
184 blk64_t prev_block = 0;
186 if (group % flexbg_size)
187 prev_block = ext2fs_inode_table_loc(fs, group - 1) +
188 fs->inode_blocks_per_group;
190 prev_block = ext2fs_inode_bitmap_loc(fs, group) +
193 group_blk = flexbg_offset(fs, group, prev_block, bmap,
194 rem_grps, fs->inode_blocks_per_group);
195 last_blk = ext2fs_group_last_block2(fs, last_grp);
198 if (!ext2fs_inode_table_loc(fs, group)) {
199 retval = ext2fs_get_free_blocks2(fs, group_blk, last_blk,
200 fs->inode_blocks_per_group,
204 for (j=0, blk = new_blk;
205 j < fs->inode_blocks_per_group;
207 ext2fs_mark_block_bitmap2(bmap, blk);
209 dgrp_t gr = ext2fs_group_of_blk2(fs, blk);
210 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
211 ext2fs_free_blocks_count_add(fs->super, -1);
212 ext2fs_bg_flags_clear(fs, gr,
213 EXT2_BG_BLOCK_UNINIT);
214 ext2fs_group_desc_csum_set(fs, gr);
217 ext2fs_inode_table_loc_set(fs, group, new_blk);
219 ext2fs_group_desc_csum_set(fs, group);
223 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
227 struct ext2fs_numeric_progress_struct progress;
229 ext2fs_numeric_progress_init(fs, &progress, NULL,
230 fs->group_desc_count);
232 for (i = 0; i < fs->group_desc_count; i++) {
233 ext2fs_numeric_progress_update(fs, &progress, i);
234 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
238 ext2fs_numeric_progress_close(fs, &progress, NULL);