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