/src/e2fsprogs/lib/ext2fs/alloc_sb.c
Line | Count | Source |
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 Library |
9 | | * General Public License, version 2. |
10 | | * %End-Header% |
11 | | */ |
12 | | |
13 | | #include "config.h" |
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 | | |
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 | | */ |
44 | | int ext2fs_reserve_super_and_bgd(ext2_filsys fs, |
45 | | dgrp_t group, |
46 | | ext2fs_block_bitmap bmap) |
47 | 68.7k | { |
48 | 68.7k | blk64_t super_blk, old_desc_blk, new_desc_blk; |
49 | 68.7k | blk_t used_blks, old_desc_blocks, num_blocks; |
50 | | |
51 | 68.7k | ext2fs_super_and_bgd_loc2(fs, group, &super_blk, |
52 | 68.7k | &old_desc_blk, &new_desc_blk, &used_blks); |
53 | | |
54 | 68.7k | if (ext2fs_has_feature_meta_bg(fs->super)) |
55 | 2.77k | old_desc_blocks = fs->super->s_first_meta_bg; |
56 | 66.0k | else |
57 | 66.0k | old_desc_blocks = |
58 | 66.0k | fs->desc_blocks + fs->super->s_reserved_gdt_blocks; |
59 | | |
60 | 68.7k | if (super_blk || (group == 0)) |
61 | 23.5k | ext2fs_mark_block_bitmap2(bmap, super_blk); |
62 | 68.7k | if ((group == 0) && (fs->blocksize == 1024) && |
63 | 136 | EXT2FS_CLUSTER_RATIO(fs) > 1) |
64 | 0 | ext2fs_mark_block_bitmap2(bmap, 0); |
65 | | |
66 | 68.7k | if (old_desc_blk) { |
67 | 23.2k | num_blocks = old_desc_blocks; |
68 | 23.2k | if (old_desc_blk + num_blocks >= ext2fs_blocks_count(fs->super)) |
69 | 545 | num_blocks = ext2fs_blocks_count(fs->super) - |
70 | 545 | old_desc_blk; |
71 | 23.2k | ext2fs_mark_block_bitmap_range2(bmap, old_desc_blk, num_blocks); |
72 | 23.2k | } |
73 | 68.7k | if (new_desc_blk) |
74 | 621 | ext2fs_mark_block_bitmap2(bmap, new_desc_blk); |
75 | | |
76 | 68.7k | num_blocks = ext2fs_group_blocks_count(fs, group); |
77 | 68.7k | num_blocks -= 2 + fs->inode_blocks_per_group + used_blks; |
78 | | |
79 | 68.7k | return num_blocks ; |
80 | 68.7k | } |
81 | | |
82 | | /* |
83 | | * This function reserves the superblock and block group descriptors |
84 | | * for a given block group and returns the number of blocks used by the |
85 | | * super block and group descriptors by looking up the block bitmap. |
86 | | */ |
87 | | errcode_t ext2fs_reserve_super_and_bgd2(ext2_filsys fs, |
88 | | dgrp_t group, |
89 | | ext2fs_block_bitmap bmap, |
90 | | blk_t *desc_blocks) |
91 | 0 | { |
92 | 0 | blk64_t num_blocks; |
93 | 0 | errcode_t retval = 0; |
94 | |
|
95 | 0 | ext2fs_reserve_super_and_bgd(fs, group, bmap); |
96 | |
|
97 | 0 | retval = ext2fs_count_used_blocks(fs, |
98 | 0 | ext2fs_group_first_block2(fs, group), |
99 | 0 | ext2fs_group_last_block2(fs, group), |
100 | 0 | &num_blocks); |
101 | 0 | if (!retval) |
102 | 0 | *desc_blocks = num_blocks; |
103 | |
|
104 | 0 | return retval; |
105 | 0 | } |