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 blk_t flexbg_offset(ext2_filsys fs, dgrp_t group, blk64_t start_blk,
38 ext2fs_block_bitmap bmap, int offset, int size,
41 int flexbg, flexbg_size;
42 blk_t last_blk, first_free = 0;
45 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
46 flexbg = group / flexbg_size;
48 if (size > (int) (fs->super->s_blocks_per_group / 8))
49 size = (int) fs->super->s_blocks_per_group / 8;
55 * Don't do a long search if the previous block
56 * search is still valid.
58 if (start_blk && group % flexbg_size) {
59 if (ext2fs_test_block_bitmap_range2(bmap, start_blk + elem_size,
61 return start_blk + elem_size;
64 start_blk = ext2fs_group_first_block2(fs, flexbg_size * flexbg);
65 last_grp = group | (flexbg_size - 1);
66 if (last_grp > fs->group_desc_count)
67 last_grp = fs->group_desc_count;
68 last_blk = ext2fs_group_last_block2(fs, last_grp);
70 /* Find the first available block */
71 if (ext2fs_get_free_blocks(fs, start_blk, last_blk, 1, bmap,
75 if (ext2fs_get_free_blocks(fs, first_free + offset, last_blk, size,
82 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
83 ext2fs_block_bitmap bmap)
86 blk_t group_blk, start_blk, last_blk, new_blk, blk;
88 int j, rem_grps = 0, flexbg_size = 0;
90 group_blk = ext2fs_group_first_block2(fs, group);
91 last_blk = ext2fs_group_last_block2(fs, group);
96 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
97 EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
98 fs->super->s_log_groups_per_flex) {
99 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
100 last_grp = group | (flexbg_size - 1);
101 rem_grps = last_grp - group;
102 if (last_grp > fs->group_desc_count)
103 last_grp = fs->group_desc_count;
107 * Allocate the block and inode bitmaps, if necessary
110 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
111 1, bmap, &start_blk);
114 start_blk += fs->inode_blocks_per_group;
115 start_blk += ((fs->stride * group) %
116 (last_blk - start_blk + 1));
117 if (start_blk >= last_blk)
118 start_blk = group_blk;
120 start_blk = group_blk;
123 blk64_t prev_block = 0;
125 if (group && ext2fs_block_bitmap_loc(fs, group - 1))
126 prev_block = ext2fs_block_bitmap_loc(fs, group - 1);
127 start_blk = flexbg_offset(fs, group, prev_block, bmap,
129 last_blk = ext2fs_group_last_block2(fs, last_grp);
132 if (!ext2fs_block_bitmap_loc(fs, group)) {
133 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
135 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
136 retval = ext2fs_get_free_blocks(fs, group_blk,
137 last_blk, 1, bmap, &new_blk);
140 ext2fs_mark_block_bitmap2(bmap, new_blk);
141 ext2fs_block_bitmap_loc_set(fs, group, new_blk);
143 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
144 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
145 ext2fs_free_blocks_count_add(fs->super, -1);
146 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
147 ext2fs_group_desc_csum_set(fs, gr);
152 blk64_t prev_block = 0;
153 if (group && ext2fs_inode_bitmap_loc(fs, group - 1))
154 prev_block = ext2fs_inode_bitmap_loc(fs, group - 1);
155 start_blk = flexbg_offset(fs, group, prev_block, bmap,
156 flexbg_size, rem_grps, 1);
157 last_blk = ext2fs_group_last_block2(fs, last_grp);
160 if (!ext2fs_inode_bitmap_loc(fs, group)) {
161 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
163 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
164 retval = ext2fs_get_free_blocks(fs, group_blk,
165 last_blk, 1, bmap, &new_blk);
168 ext2fs_mark_block_bitmap2(bmap, new_blk);
169 ext2fs_inode_bitmap_loc_set(fs, group, new_blk);
171 dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
172 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
173 ext2fs_free_blocks_count_add(fs->super, -1);
174 ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
175 ext2fs_group_desc_csum_set(fs, gr);
180 * Allocate the inode table
183 blk64_t prev_block = 0;
184 if (group && ext2fs_inode_table_loc(fs, group - 1))
185 prev_block = ext2fs_inode_table_loc(fs, group - 1);
186 if (last_grp == fs->group_desc_count)
187 rem_grps = last_grp - group;
188 group_blk = flexbg_offset(fs, group, prev_block, bmap,
190 fs->inode_blocks_per_group *
192 fs->inode_blocks_per_group);
193 last_blk = ext2fs_group_last_block2(fs, last_grp);
196 if (!ext2fs_inode_table_loc(fs, group)) {
197 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
198 fs->inode_blocks_per_group,
202 for (j=0, blk = new_blk;
203 j < fs->inode_blocks_per_group;
205 ext2fs_mark_block_bitmap2(bmap, blk);
207 dgrp_t gr = ext2fs_group_of_blk2(fs, blk);
208 ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
209 ext2fs_free_blocks_count_add(fs->super, -1);
210 ext2fs_bg_flags_clear(fs, gr,
211 EXT2_BG_BLOCK_UNINIT);
212 ext2fs_group_desc_csum_set(fs, gr);
215 ext2fs_inode_table_loc_set(fs, group, new_blk);
217 ext2fs_group_desc_csum_set(fs, group);
221 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
225 struct ext2fs_numeric_progress_struct progress;
227 ext2fs_numeric_progress_init(fs, &progress, NULL,
228 fs->group_desc_count);
230 for (i = 0; i < fs->group_desc_count; i++) {
231 ext2fs_numeric_progress_update(fs, &progress, i);
232 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
236 ext2fs_numeric_progress_close(fs, &progress, NULL);