OSDN Git Service

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