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.
25 #include <sys/types.h>
33 * This routine searches for free blocks that can allocate a full
34 * group of bitmaps or inode tables for a flexbg group. Returns the
35 * block number with a correct offset were the bitmaps and inode
36 * tables can be allocated continously and in order.
38 static blk64_t flexbg_offset(ext2_filsys fs, dgrp_t group, blk64_t start_blk,
39 ext2fs_block_bitmap bmap, int rem_grp,
42 int flexbg, flexbg_size, size;
43 blk64_t last_blk, first_free = 0;
46 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
47 flexbg = group / flexbg_size;
48 size = rem_grp * elem_size;
50 if (size > (int) (fs->super->s_blocks_per_group / 4))
51 size = (int) fs->super->s_blocks_per_group / 4;
54 * Don't do a long search if the previous block search is still valid,
55 * but skip minor obstructions such as group descriptor backups.
57 if (start_blk && start_blk < ext2fs_blocks_count(fs->super) &&
58 ext2fs_get_free_blocks2(fs, start_blk, start_blk + size, elem_size,
59 bmap, &first_free) == 0)
62 start_blk = ext2fs_group_first_block2(fs, flexbg_size * flexbg);
63 last_grp = group | (flexbg_size - 1);
64 if (last_grp > fs->group_desc_count-1)
65 last_grp = fs->group_desc_count-1;
66 last_blk = ext2fs_group_last_block2(fs, last_grp);
68 /* Find the first available block */
69 if (ext2fs_get_free_blocks2(fs, start_blk, last_blk, size,
70 bmap, &first_free) == 0)
73 if (ext2fs_get_free_blocks2(fs, start_blk, last_blk, elem_size,
74 bmap, &first_free) == 0)
77 if (ext2fs_get_free_blocks2(fs, 0, last_blk, elem_size, bmap,
84 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
85 ext2fs_block_bitmap bmap)
88 blk64_t group_blk, start_blk, last_blk, new_blk;
90 int rem_grps = 0, flexbg_size = 0, table_offset = 0;
92 group_blk = ext2fs_group_first_block2(fs, group);
93 last_blk = ext2fs_group_last_block2(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 if (last_grp > fs->group_desc_count-1)
104 last_grp = fs->group_desc_count-1;
105 rem_grps = last_grp - group + 1;
109 * Allocate the block and inode bitmaps, if necessary
112 retval = ext2fs_get_free_blocks2(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;
125 blk64_t prev_block = 0;
127 table_offset = flexbg_size;
128 if (group % flexbg_size)
129 prev_block = ext2fs_block_bitmap_loc(fs, group - 1) + 1;
130 else if (last_grp == fs->group_desc_count-1) {
132 * If we are allocating for the last flex_bg
133 * keep the metadata tables contiguous
135 table_offset = last_grp & (flexbg_size - 1);
136 if (table_offset == 0)
137 table_offset = flexbg_size;
141 /* FIXME: Take backup group descriptor blocks into account
142 * if the flexbg allocations will grow to overlap them... */
143 start_blk = flexbg_offset(fs, group, prev_block, bmap,
145 last_blk = ext2fs_group_last_block2(fs, last_grp);
148 if (!ext2fs_block_bitmap_loc(fs, group)) {
149 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
151 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
152 retval = ext2fs_get_free_blocks2(fs, group_blk,
153 last_blk, 1, bmap, &new_blk);
156 ext2fs_mark_block_bitmap2(bmap, new_blk);
157 ext2fs_block_bitmap_loc_set(fs, group, new_blk);
159 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
160 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
161 ext2fs_free_blocks_count_add(fs->super, -1);
162 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
163 ext2fs_group_desc_csum_set(fs, gr);
168 blk64_t prev_block = 0;
169 if (group % flexbg_size)
170 prev_block = ext2fs_inode_bitmap_loc(fs, group - 1) + 1;
172 prev_block = ext2fs_block_bitmap_loc(fs, group) +
174 /* FIXME: Take backup group descriptor blocks into account
175 * if the flexbg allocations will grow to overlap them... */
176 start_blk = flexbg_offset(fs, group, prev_block, bmap,
178 last_blk = ext2fs_group_last_block2(fs, last_grp);
181 if (!ext2fs_inode_bitmap_loc(fs, group)) {
182 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
184 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
185 retval = ext2fs_get_free_blocks2(fs, group_blk,
186 last_blk, 1, bmap, &new_blk);
189 ext2fs_mark_block_bitmap2(bmap, new_blk);
190 ext2fs_inode_bitmap_loc_set(fs, group, new_blk);
192 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
193 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
194 ext2fs_free_blocks_count_add(fs->super, -1);
195 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
196 ext2fs_group_desc_csum_set(fs, gr);
201 * Allocate the inode table
204 blk64_t prev_block = 0;
206 if (group % flexbg_size)
207 prev_block = ext2fs_inode_table_loc(fs, group - 1) +
208 fs->inode_blocks_per_group;
210 prev_block = ext2fs_inode_bitmap_loc(fs, group) +
213 /* FIXME: Take backup group descriptor blocks into account
214 * if the flexbg allocations will grow to overlap them... */
215 group_blk = flexbg_offset(fs, group, prev_block, bmap,
216 rem_grps, fs->inode_blocks_per_group);
217 last_blk = ext2fs_group_last_block2(fs, last_grp);
220 if (!ext2fs_inode_table_loc(fs, group)) {
221 retval = ext2fs_get_free_blocks2(fs, group_blk, last_blk,
222 fs->inode_blocks_per_group,
227 ext2fs_block_alloc_stats_range(fs, new_blk,
228 fs->inode_blocks_per_group, +1);
230 ext2fs_mark_block_bitmap_range2(fs->block_map,
231 new_blk, fs->inode_blocks_per_group);
232 ext2fs_inode_table_loc_set(fs, group, new_blk);
234 ext2fs_group_desc_csum_set(fs, group);
238 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
242 struct ext2fs_numeric_progress_struct progress;
244 ext2fs_numeric_progress_init(fs, &progress, NULL,
245 fs->group_desc_count);
247 for (i = 0; i < fs->group_desc_count; i++) {
248 ext2fs_numeric_progress_update(fs, &progress, i);
249 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
253 ext2fs_numeric_progress_close(fs, &progress, NULL);