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