alloc_tables.c revision 8f82ef9860339039b54a324be137fbc09b762358
1/* 2 * alloc_tables.c --- Allocate tables for a newly initialized 3 * filesystem. Used by mke2fs when initializing a filesystem 4 * 5 * Copyright (C) 1996 Theodore Ts'o. 6 * 7 * %Begin-Header% 8 * This file may be redistributed under the terms of the GNU Public 9 * License. 10 * %End-Header% 11 */ 12 13#include <stdio.h> 14#include <string.h> 15#if HAVE_UNISTD_H 16#include <unistd.h> 17#endif 18#include <fcntl.h> 19#include <time.h> 20#if HAVE_SYS_STAT_H 21#include <sys/stat.h> 22#endif 23#if HAVE_SYS_TYPES_H 24#include <sys/types.h> 25#endif 26 27#include "ext2_fs.h" 28#include "ext2fs.h" 29 30/* 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. 35 */ 36static blk_t flexbg_offset(ext2_filsys fs, dgrp_t group, blk64_t start_blk, 37 ext2fs_block_bitmap bmap, int offset, int size, 38 int elem_size) 39{ 40 int flexbg, flexbg_size; 41 blk_t last_blk, first_free = 0; 42 dgrp_t last_grp; 43 44 flexbg_size = 1 << fs->super->s_log_groups_per_flex; 45 flexbg = group / flexbg_size; 46 47 if (size > (int) (fs->super->s_blocks_per_group / 8)) 48 size = (int) fs->super->s_blocks_per_group / 8; 49 50 if (offset) 51 offset -= 1; 52 53 /* 54 * Don't do a long search if the previous block 55 * search is still valid. 56 */ 57 if (start_blk && group % flexbg_size) { 58 if (ext2fs_test_block_bitmap_range2(bmap, start_blk + elem_size, 59 size)) 60 return start_blk + elem_size; 61 } 62 63 start_blk = ext2fs_group_first_block(fs, flexbg_size * flexbg); 64 last_grp = group | (flexbg_size - 1); 65 if (last_grp > fs->group_desc_count) 66 last_grp = fs->group_desc_count; 67 last_blk = ext2fs_group_last_block(fs, last_grp); 68 69 /* Find the first available block */ 70 if (ext2fs_get_free_blocks(fs, start_blk, last_blk, 1, bmap, 71 &first_free)) 72 return first_free; 73 74 if (ext2fs_get_free_blocks(fs, first_free + offset, last_blk, size, 75 bmap, &first_free)) 76 return first_free; 77 78 return first_free; 79} 80 81errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group, 82 ext2fs_block_bitmap bmap) 83{ 84 errcode_t retval; 85 blk_t group_blk, start_blk, last_blk, new_blk, blk; 86 dgrp_t last_grp = 0; 87 int j, rem_grps = 0, flexbg_size = 0; 88 89 group_blk = ext2fs_group_first_block(fs, group); 90 last_blk = ext2fs_group_last_block(fs, group); 91 92 if (!bmap) 93 bmap = fs->block_map; 94 95 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super, 96 EXT4_FEATURE_INCOMPAT_FLEX_BG) && 97 fs->super->s_log_groups_per_flex) { 98 flexbg_size = 1 << fs->super->s_log_groups_per_flex; 99 last_grp = group | (flexbg_size - 1); 100 rem_grps = last_grp - group; 101 if (last_grp > fs->group_desc_count) 102 last_grp = fs->group_desc_count; 103 } 104 105 /* 106 * Allocate the block and inode bitmaps, if necessary 107 */ 108 if (fs->stride) { 109 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk, 110 1, bmap, &start_blk); 111 if (retval) 112 return retval; 113 start_blk += fs->inode_blocks_per_group; 114 start_blk += ((fs->stride * group) % 115 (last_blk - start_blk + 1)); 116 if (start_blk >= last_blk) 117 start_blk = group_blk; 118 } else 119 start_blk = group_blk; 120 121 if (flexbg_size) { 122 blk64_t prev_block = 0; 123 124 if (group && fs->group_desc[group-1].bg_block_bitmap) 125 prev_block = fs->group_desc[group-1].bg_block_bitmap; 126 start_blk = flexbg_offset(fs, group, prev_block, bmap, 127 0, rem_grps, 1); 128 last_blk = ext2fs_group_last_block(fs, last_grp); 129 } 130 131 if (!fs->group_desc[group].bg_block_bitmap) { 132 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk, 133 1, bmap, &new_blk); 134 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL) 135 retval = ext2fs_get_free_blocks(fs, group_blk, 136 last_blk, 1, bmap, &new_blk); 137 if (retval) 138 return retval; 139 ext2fs_mark_block_bitmap2(bmap, new_blk); 140 fs->group_desc[group].bg_block_bitmap = new_blk; 141 if (flexbg_size) { 142 dgrp_t gr = ext2fs_group_of_blk(fs, new_blk); 143 fs->group_desc[gr].bg_free_blocks_count--; 144 fs->super->s_free_blocks_count--; 145 fs->group_desc[gr].bg_flags &= ~EXT2_BG_BLOCK_UNINIT; 146 ext2fs_group_desc_csum_set(fs, gr); 147 } 148 } 149 150 if (flexbg_size) { 151 blk_t prev_block = 0; 152 if (group && fs->group_desc[group-1].bg_inode_bitmap) 153 prev_block = fs->group_desc[group-1].bg_inode_bitmap; 154 start_blk = flexbg_offset(fs, group, prev_block, bmap, 155 flexbg_size, rem_grps, 1); 156 last_blk = ext2fs_group_last_block(fs, last_grp); 157 } 158 159 if (!fs->group_desc[group].bg_inode_bitmap) { 160 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk, 161 1, bmap, &new_blk); 162 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL) 163 retval = ext2fs_get_free_blocks(fs, group_blk, 164 last_blk, 1, bmap, &new_blk); 165 if (retval) 166 return retval; 167 ext2fs_mark_block_bitmap2(bmap, new_blk); 168 fs->group_desc[group].bg_inode_bitmap = new_blk; 169 if (flexbg_size) { 170 dgrp_t gr = ext2fs_group_of_blk(fs, new_blk); 171 fs->group_desc[gr].bg_free_blocks_count--; 172 fs->super->s_free_blocks_count--; 173 fs->group_desc[gr].bg_flags &= ~EXT2_BG_BLOCK_UNINIT; 174 ext2fs_group_desc_csum_set(fs, gr); 175 } 176 } 177 178 /* 179 * Allocate the inode table 180 */ 181 if (flexbg_size) { 182 blk_t prev_block = 0; 183 if (group && fs->group_desc[group-1].bg_inode_table) 184 prev_block = fs->group_desc[group-1].bg_inode_table; 185 group_blk = flexbg_offset(fs, group, prev_block, bmap, 186 flexbg_size * 2, 187 fs->inode_blocks_per_group * 188 rem_grps, 189 fs->inode_blocks_per_group); 190 last_blk = ext2fs_group_last_block(fs, last_grp); 191 } 192 193 if (!fs->group_desc[group].bg_inode_table) { 194 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk, 195 fs->inode_blocks_per_group, 196 bmap, &new_blk); 197 if (retval) 198 return retval; 199 for (j=0, blk = new_blk; 200 j < fs->inode_blocks_per_group; 201 j++, blk++) { 202 ext2fs_mark_block_bitmap2(bmap, blk); 203 if (flexbg_size) { 204 dgrp_t gr = ext2fs_group_of_blk(fs, blk); 205 fs->group_desc[gr].bg_free_blocks_count--; 206 fs->super->s_free_blocks_count--; 207 fs->group_desc[gr].bg_flags &= ~EXT2_BG_BLOCK_UNINIT; 208 ext2fs_group_desc_csum_set(fs, gr); 209 } 210 } 211 fs->group_desc[group].bg_inode_table = new_blk; 212 } 213 ext2fs_group_desc_csum_set(fs, group); 214 return 0; 215} 216 217errcode_t ext2fs_allocate_tables(ext2_filsys fs) 218{ 219 errcode_t retval; 220 dgrp_t i; 221 222 for (i = 0; i < fs->group_desc_count; i++) { 223 retval = ext2fs_allocate_group_table(fs, i, fs->block_map); 224 if (retval) 225 return retval; 226 } 227 return 0; 228} 229 230