Whamcloud - gitweb
Many files:
[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 #if EXT2_FLAT_INCLUDES
28 #include "ext2_fs.h"
29 #else
30 #include <linux/ext2_fs.h>
31 #endif
32
33 #include "ext2fs.h"
34
35 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, int group,
36                                       ext2fs_block_bitmap bmap)
37 {
38         errcode_t       retval;
39         blk_t           group_blk, start_blk, last_blk, new_blk, blk;
40         int             j;
41
42         group_blk = fs->super->s_first_data_block +
43                 (group * fs->super->s_blocks_per_group);
44         
45         last_blk = group_blk + fs->super->s_blocks_per_group;
46         if (last_blk >= fs->super->s_blocks_count)
47                 last_blk = fs->super->s_blocks_count - 1;
48
49         start_blk = group_blk + 3 + fs->desc_blocks;
50         if (start_blk > last_blk)
51                 start_blk = group_blk;
52
53         if (!bmap)
54                 bmap = fs->block_map;
55         
56         /*
57          * Allocate the inode table
58          */
59         if (!fs->group_desc[group].bg_inode_table) {
60                 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
61                                                 fs->inode_blocks_per_group,
62                                                 bmap, &new_blk);
63                 if (retval)
64                         return retval;
65                 for (j=0, blk = new_blk;
66                      j < fs->inode_blocks_per_group;
67                      j++, blk++)
68                         ext2fs_mark_block_bitmap(bmap, blk);
69                 fs->group_desc[group].bg_inode_table = new_blk;
70         }
71
72         /*
73          * Allocate the block and inode bitmaps, if necessary
74          */
75         if (fs->stride) {
76                 start_blk += fs->inode_blocks_per_group;
77                 start_blk += ((fs->stride * group) %
78                               (last_blk - start_blk));
79                 if (start_blk > last_blk)
80                         /* should never happen */
81                         start_blk = group_blk;
82         } else
83                 start_blk = group_blk;
84
85         if (!fs->group_desc[group].bg_block_bitmap) {
86                 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
87                                                 1, bmap, &new_blk);
88                 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL) 
89                         retval = ext2fs_get_free_blocks(fs, group_blk,
90                                         last_blk, 1, bmap, &new_blk);
91                 if (retval)
92                         return retval;
93                 ext2fs_mark_block_bitmap(bmap, new_blk);
94                 fs->group_desc[group].bg_block_bitmap = new_blk;
95         }
96
97         if (!fs->group_desc[group].bg_inode_bitmap) {
98                 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
99                                                 1, bmap, &new_blk);
100                 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL) 
101                         retval = ext2fs_get_free_blocks(fs, group_blk,
102                                         last_blk, 1, bmap, &new_blk);
103                 if (retval)
104                         return retval;
105                 ext2fs_mark_block_bitmap(bmap, new_blk);
106                 fs->group_desc[group].bg_inode_bitmap = new_blk;
107         }
108         return 0;
109 }
110
111         
112
113 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
114 {
115         errcode_t       retval;
116         int             i;
117
118         for (i = 0; i < fs->group_desc_count; i++) {
119                 retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
120                 if (retval)
121                         return retval;
122         }
123         return 0;
124 }
125