Whamcloud - gitweb
e2fsprogs: remove checker infrastructure
[tools/e2fsprogs.git] / lib / ext2fs / alloc_tables.c
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 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 #include "ext2fsP.h"
31
32 /*
33  * This routine searches for free blocks that can allocate a full
34  * group of bitmaps or inode tables for a flexbg group.  Returns the
35  * block number with a correct offset were the bitmaps and inode
36  * tables can be allocated continously and in order.
37  */
38 static blk64_t flexbg_offset(ext2_filsys fs, dgrp_t group, blk64_t start_blk,
39                              ext2fs_block_bitmap bmap, int rem_grp,
40                              int elem_size)
41 {
42         int             flexbg, flexbg_size, size;
43         blk64_t         last_blk, first_free = 0;
44         dgrp_t          last_grp;
45
46         flexbg_size = 1 << fs->super->s_log_groups_per_flex;
47         flexbg = group / flexbg_size;
48         size = rem_grp * elem_size;
49
50         if (size > (int) (fs->super->s_blocks_per_group / 8))
51                 size = (int) fs->super->s_blocks_per_group / 8;
52
53         /*
54          * Don't do a long search if the previous block
55          * search is still valid.
56          */
57         if (start_blk && start_blk < ext2fs_blocks_count(fs->super) &&
58             ext2fs_test_block_bitmap_range2(bmap, start_blk, elem_size))
59                 return start_blk;
60
61         start_blk = ext2fs_group_first_block2(fs, flexbg_size * flexbg);
62         last_grp = group | (flexbg_size - 1);
63         if (last_grp > fs->group_desc_count-1)
64                 last_grp = fs->group_desc_count-1;
65         last_blk = ext2fs_group_last_block2(fs, last_grp);
66
67         /* Find the first available block */
68         if (ext2fs_get_free_blocks2(fs, start_blk, last_blk, size,
69                                     bmap, &first_free) == 0)
70                 return first_free;
71
72         if (ext2fs_get_free_blocks2(fs, start_blk, last_blk, elem_size,
73                                    bmap, &first_free) == 0)
74                 return first_free;
75
76         if (ext2fs_get_free_blocks2(fs, 0, last_blk, elem_size, bmap,
77                                     &first_free) == 0)
78                 return first_free;
79
80         return first_free;
81 }
82
83 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
84                                       ext2fs_block_bitmap bmap)
85 {
86         errcode_t       retval;
87         blk64_t         group_blk, start_blk, last_blk, new_blk;
88         dgrp_t          last_grp = 0;
89         int             rem_grps = 0, flexbg_size = 0;
90
91         group_blk = ext2fs_group_first_block2(fs, group);
92         last_blk = ext2fs_group_last_block2(fs, group);
93
94         if (!bmap)
95                 bmap = fs->block_map;
96
97         if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
98                                       EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
99             fs->super->s_log_groups_per_flex) {
100                 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
101                 last_grp = group | (flexbg_size - 1);
102                 if (last_grp > fs->group_desc_count-1)
103                         last_grp = fs->group_desc_count-1;
104                 rem_grps = last_grp - group + 1;
105         }
106
107         /*
108          * Allocate the block and inode bitmaps, if necessary
109          */
110         if (fs->stride) {
111                 retval = ext2fs_get_free_blocks2(fs, group_blk, last_blk,
112                                                  1, bmap, &start_blk);
113                 if (retval)
114                         return retval;
115                 start_blk += fs->inode_blocks_per_group;
116                 start_blk += ((fs->stride * group) %
117                               (last_blk - start_blk + 1));
118                 if (start_blk >= last_blk)
119                         start_blk = group_blk;
120         } else
121                 start_blk = group_blk;
122
123         if (flexbg_size) {
124                 blk64_t prev_block = 0;
125
126                 if (group % flexbg_size)
127                         prev_block = ext2fs_block_bitmap_loc(fs, group - 1) + 1;
128                 start_blk = flexbg_offset(fs, group, prev_block, bmap,
129                                           rem_grps, 1);
130                 last_blk = ext2fs_group_last_block2(fs, last_grp);
131         }
132
133         if (!ext2fs_block_bitmap_loc(fs, group)) {
134                 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
135                                                  1, bmap, &new_blk);
136                 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
137                         retval = ext2fs_get_free_blocks2(fs, group_blk,
138                                         last_blk, 1, bmap, &new_blk);
139                 if (retval)
140                         return retval;
141                 ext2fs_mark_block_bitmap2(bmap, new_blk);
142                 ext2fs_block_bitmap_loc_set(fs, group, new_blk);
143                 if (flexbg_size) {
144                         dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
145                         ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
146                         ext2fs_free_blocks_count_add(fs->super, -1);
147                         ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
148                         ext2fs_group_desc_csum_set(fs, gr);
149                 }
150         }
151
152         if (flexbg_size) {
153                 blk64_t prev_block = 0;
154                 if (group % flexbg_size)
155                         prev_block = ext2fs_inode_bitmap_loc(fs, group - 1) + 1;
156                 else
157                         prev_block = ext2fs_block_bitmap_loc(fs, group) +
158                                 flexbg_size;
159                 start_blk = flexbg_offset(fs, group, prev_block, bmap,
160                                           rem_grps, 1);
161                 last_blk = ext2fs_group_last_block2(fs, last_grp);
162         }
163
164         if (!ext2fs_inode_bitmap_loc(fs, group)) {
165                 retval = ext2fs_get_free_blocks2(fs, start_blk, last_blk,
166                                                  1, bmap, &new_blk);
167                 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
168                         retval = ext2fs_get_free_blocks2(fs, group_blk,
169                                          last_blk, 1, bmap, &new_blk);
170                 if (retval)
171                         return retval;
172                 ext2fs_mark_block_bitmap2(bmap, new_blk);
173                 ext2fs_inode_bitmap_loc_set(fs, group, new_blk);
174                 if (flexbg_size) {
175                         dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
176                         ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
177                         ext2fs_free_blocks_count_add(fs->super, -1);
178                         ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
179                         ext2fs_group_desc_csum_set(fs, gr);
180                 }
181         }
182
183         /*
184          * Allocate the inode table
185          */
186         if (flexbg_size) {
187                 blk64_t prev_block = 0;
188
189                 if (group % flexbg_size)
190                         prev_block = ext2fs_inode_table_loc(fs, group - 1) +
191                                 fs->inode_blocks_per_group;
192                 else
193                         prev_block = ext2fs_inode_bitmap_loc(fs, group) +
194                                 flexbg_size;
195
196                 group_blk = flexbg_offset(fs, group, prev_block, bmap,
197                                           rem_grps, fs->inode_blocks_per_group);
198                 last_blk = ext2fs_group_last_block2(fs, last_grp);
199         }
200
201         if (!ext2fs_inode_table_loc(fs, group)) {
202                 retval = ext2fs_get_free_blocks2(fs, group_blk, last_blk,
203                                                 fs->inode_blocks_per_group,
204                                                 bmap, &new_blk);
205                 if (retval)
206                         return retval;
207                 if (flexbg_size)
208                         ext2fs_block_alloc_stats_range(fs, new_blk,
209                                        fs->inode_blocks_per_group, +1);
210                 else
211                         ext2fs_mark_block_bitmap_range2(fs->block_map,
212                                         new_blk, fs->inode_blocks_per_group);
213                 ext2fs_inode_table_loc_set(fs, group, new_blk);
214         }
215         ext2fs_group_desc_csum_set(fs, group);
216         return 0;
217 }
218
219 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
220 {
221         errcode_t       retval;
222         dgrp_t          i;
223         struct ext2fs_numeric_progress_struct progress;
224
225         ext2fs_numeric_progress_init(fs, &progress, NULL,
226                                      fs->group_desc_count);
227
228         for (i = 0; i < fs->group_desc_count; i++) {
229                 ext2fs_numeric_progress_update(fs, &progress, i);
230                 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
231                 if (retval)
232                         return retval;
233         }
234         ext2fs_numeric_progress_close(fs, &progress, NULL);
235         return 0;
236 }
237