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