Home | History | Annotate | Download | only in ext2fs
      1 /*
      2  * alloc_sb.c --- Allocate the superblock and block group descriptors for a
      3  * newly initialized filesystem.  Used by mke2fs when initializing a filesystem
      4  *
      5  * Copyright (C) 1994, 1995, 1996, 2003 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 function reserves the superblock and block group descriptors
     32  * for a given block group.  It currently returns the number of free
     33  * blocks assuming that inode table and allocation bitmaps will be in
     34  * the group.  This is not necessarily the case when the flex_bg
     35  * feature is enabled, so callers should take care!  It was only
     36  * really intended for use by mke2fs, and even there it's not that
     37  * useful.  In the future, when we redo this function for 64-bit block
     38  * numbers, we should probably return the number of blocks used by the
     39  * super block and group descriptors instead.
     40  *
     41  * See also the comment for ext2fs_super_and_bgd_loc()
     42  */
     43 int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
     44 				 dgrp_t group,
     45 				 ext2fs_block_bitmap bmap)
     46 {
     47 	blk_t	super_blk, old_desc_blk, new_desc_blk;
     48 	int	j, old_desc_blocks, num_blocks;
     49 
     50 	num_blocks = ext2fs_super_and_bgd_loc(fs, group, &super_blk,
     51 					      &old_desc_blk, &new_desc_blk, 0);
     52 
     53 	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
     54 		old_desc_blocks = fs->super->s_first_meta_bg;
     55 	else
     56 		old_desc_blocks =
     57 			fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
     58 
     59 	if (super_blk || (group == 0))
     60 		ext2fs_mark_block_bitmap(bmap, super_blk);
     61 
     62 	if (old_desc_blk) {
     63 		if (fs->super->s_reserved_gdt_blocks && fs->block_map == bmap)
     64 			fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
     65 		for (j=0; j < old_desc_blocks; j++)
     66 			if (old_desc_blk + j < fs->super->s_blocks_count)
     67 				ext2fs_mark_block_bitmap(bmap,
     68 							 old_desc_blk + j);
     69 	}
     70 	if (new_desc_blk)
     71 		ext2fs_mark_block_bitmap(bmap, new_desc_blk);
     72 
     73 	return num_blocks;
     74 }
     75