Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 1 | /* |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 2 | * alloc_sb.c --- Allocate the superblock and block group descriptors for a |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 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% |
Theodore Ts'o | 543547a | 2010-05-17 21:31:56 -0400 | [diff] [blame] | 8 | * This file may be redistributed under the terms of the GNU Library |
| 9 | * General Public License, version 2. |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 10 | * %End-Header% |
| 11 | */ |
| 12 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 13 | #include "config.h" |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #if HAVE_UNISTD_H |
| 17 | #include <unistd.h> |
| 18 | #endif |
| 19 | #include <fcntl.h> |
| 20 | #include <time.h> |
| 21 | #if HAVE_SYS_STAT_H |
| 22 | #include <sys/stat.h> |
| 23 | #endif |
| 24 | #if HAVE_SYS_TYPES_H |
| 25 | #include <sys/types.h> |
| 26 | #endif |
| 27 | |
| 28 | #include "ext2_fs.h" |
| 29 | #include "ext2fs.h" |
| 30 | |
Theodore Ts'o | 4a56850 | 2008-04-20 19:22:49 -0400 | [diff] [blame] | 31 | /* |
| 32 | * This function reserves the superblock and block group descriptors |
| 33 | * for a given block group. It currently returns the number of free |
| 34 | * blocks assuming that inode table and allocation bitmaps will be in |
| 35 | * the group. This is not necessarily the case when the flex_bg |
| 36 | * feature is enabled, so callers should take care! It was only |
| 37 | * really intended for use by mke2fs, and even there it's not that |
| 38 | * useful. In the future, when we redo this function for 64-bit block |
| 39 | * numbers, we should probably return the number of blocks used by the |
| 40 | * super block and group descriptors instead. |
| 41 | * |
| 42 | * See also the comment for ext2fs_super_and_bgd_loc() |
| 43 | */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 44 | int ext2fs_reserve_super_and_bgd(ext2_filsys fs, |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 45 | dgrp_t group, |
| 46 | ext2fs_block_bitmap bmap) |
| 47 | { |
Jose R. Santos | 20f2ccb | 2009-07-11 21:29:30 -0400 | [diff] [blame] | 48 | blk64_t super_blk, old_desc_blk, new_desc_blk; |
| 49 | blk_t used_blks; |
Andreas Dilger | b55705e | 2013-01-27 22:29:01 -0500 | [diff] [blame] | 50 | int old_desc_blocks, num_blocks; |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 51 | |
Jose R. Santos | 20f2ccb | 2009-07-11 21:29:30 -0400 | [diff] [blame] | 52 | ext2fs_super_and_bgd_loc2(fs, group, &super_blk, |
| 53 | &old_desc_blk, &new_desc_blk, &used_blks); |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 54 | |
| 55 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
| 56 | old_desc_blocks = fs->super->s_first_meta_bg; |
| 57 | else |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 58 | old_desc_blocks = |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 59 | fs->desc_blocks + fs->super->s_reserved_gdt_blocks; |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 60 | |
| 61 | if (super_blk || (group == 0)) |
Valerie Aurora Henson | 8f82ef9 | 2009-08-05 00:27:10 -0400 | [diff] [blame] | 62 | ext2fs_mark_block_bitmap2(bmap, super_blk); |
Theodore Ts'o | 25567a7 | 2011-07-05 13:42:07 -0400 | [diff] [blame] | 63 | if ((group == 0) && (fs->blocksize == 1024) && |
| 64 | EXT2FS_CLUSTER_RATIO(fs) > 1) |
| 65 | ext2fs_mark_block_bitmap2(bmap, 0); |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 66 | |
| 67 | if (old_desc_blk) { |
Theodore Ts'o | 5711ed2 | 2008-04-21 01:29:01 -0400 | [diff] [blame] | 68 | if (fs->super->s_reserved_gdt_blocks && fs->block_map == bmap) |
Eric Sandeen | e633b58 | 2009-10-25 21:41:32 -0400 | [diff] [blame] | 69 | ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT); |
Theodore Ts'o | a0ba54e | 2013-01-03 08:35:25 -0500 | [diff] [blame] | 70 | num_blocks = old_desc_blocks; |
| 71 | if (old_desc_blk + num_blocks >= ext2fs_blocks_count(fs->super)) |
| 72 | num_blocks = ext2fs_blocks_count(fs->super) - |
| 73 | old_desc_blk; |
| 74 | ext2fs_mark_block_bitmap_range2(bmap, old_desc_blk, num_blocks); |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 75 | } |
| 76 | if (new_desc_blk) |
Valerie Aurora Henson | 8f82ef9 | 2009-08-05 00:27:10 -0400 | [diff] [blame] | 77 | ext2fs_mark_block_bitmap2(bmap, new_desc_blk); |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 78 | |
Eric Sandeen | 98f4547 | 2011-09-16 09:21:53 -0400 | [diff] [blame] | 79 | num_blocks = ext2fs_group_blocks_count(fs, group); |
Jose R. Santos | 20f2ccb | 2009-07-11 21:29:30 -0400 | [diff] [blame] | 80 | num_blocks -= 2 + fs->inode_blocks_per_group + used_blks; |
| 81 | |
| 82 | return num_blocks ; |
Theodore Ts'o | ef344e1 | 2003-11-21 09:02:21 -0500 | [diff] [blame] | 83 | } |