alloc_tables.c revision 9f8046fc6dfc13eee2f5c363214e60b533872cac
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 30errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group, 31 ext2fs_block_bitmap bmap) 32{ 33 errcode_t retval; 34 blk_t group_blk, start_blk, last_blk, new_blk, blk; 35 int j; 36 37 group_blk = fs->super->s_first_data_block + 38 (group * fs->super->s_blocks_per_group); 39 40 last_blk = group_blk + fs->super->s_blocks_per_group; 41 if (last_blk >= fs->super->s_blocks_count) 42 last_blk = fs->super->s_blocks_count - 1; 43 44 start_blk = group_blk + 3 + fs->desc_blocks; 45 if (start_blk > last_blk) 46 start_blk = group_blk; 47 48 if (!bmap) 49 bmap = fs->block_map; 50 51 /* 52 * Allocate the inode table 53 */ 54 if (!fs->group_desc[group].bg_inode_table) { 55 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk, 56 fs->inode_blocks_per_group, 57 bmap, &new_blk); 58 if (retval) 59 return retval; 60 for (j=0, blk = new_blk; 61 j < fs->inode_blocks_per_group; 62 j++, blk++) 63 ext2fs_mark_block_bitmap(bmap, blk); 64 fs->group_desc[group].bg_inode_table = new_blk; 65 } 66 67 /* 68 * Allocate the block and inode bitmaps, if necessary 69 */ 70 if (fs->stride) { 71 start_blk += fs->inode_blocks_per_group; 72 start_blk += ((fs->stride * group) % 73 (last_blk - start_blk)); 74 if (start_blk > last_blk) 75 /* should never happen */ 76 start_blk = group_blk; 77 } else 78 start_blk = group_blk; 79 80 if (!fs->group_desc[group].bg_block_bitmap) { 81 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk, 82 1, bmap, &new_blk); 83 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL) 84 retval = ext2fs_get_free_blocks(fs, group_blk, 85 last_blk, 1, bmap, &new_blk); 86 if (retval) 87 return retval; 88 ext2fs_mark_block_bitmap(bmap, new_blk); 89 fs->group_desc[group].bg_block_bitmap = new_blk; 90 } 91 92 if (!fs->group_desc[group].bg_inode_bitmap) { 93 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk, 94 1, bmap, &new_blk); 95 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL) 96 retval = ext2fs_get_free_blocks(fs, group_blk, 97 last_blk, 1, bmap, &new_blk); 98 if (retval) 99 return retval; 100 ext2fs_mark_block_bitmap(bmap, new_blk); 101 fs->group_desc[group].bg_inode_bitmap = new_blk; 102 } 103 return 0; 104} 105 106 107 108errcode_t ext2fs_allocate_tables(ext2_filsys fs) 109{ 110 errcode_t retval; 111 dgrp_t i; 112 113 for (i = 0; i < fs->group_desc_count; i++) { 114 retval = ext2fs_allocate_group_table(fs, i, fs->block_map); 115 if (retval) 116 return retval; 117 } 118 return 0; 119} 120 121