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 #include <unistd.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <time.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #if HAVE_ERRNO_H
22 #include <errno.h>
23 #endif
24
25 #include <linux/ext2_fs.h>
26
27 #include "ext2fs.h"
28
29 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
30 {
31         errcode_t       retval;
32         blk_t           group_blk, last_blk, new_blk, blk;
33         int             i, j;
34
35         group_blk = fs->super->s_first_data_block;
36         for (i = 0; i < fs->group_desc_count; i++) {
37                 last_blk = group_blk + fs->super->s_blocks_per_group;
38                 
39                 /*
40                  * Allocate the block bitmap
41                  */
42                 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
43                                                 1, fs->block_map, &new_blk);
44                 if (retval)
45                         return retval;
46                 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
47                 fs->group_desc[i].bg_block_bitmap = new_blk;
48
49                 /*
50                  * ... and the inode bitmap
51                  */
52                 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
53                                                 1, fs->block_map, &new_blk);
54                 if (retval)
55                         return retval;
56                 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
57                 fs->group_desc[i].bg_inode_bitmap = new_blk;
58
59                 /*
60                  * Finally, allocate the inode table
61                  */
62                 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
63                                                 fs->inode_blocks_per_group,
64                                                 fs->block_map, &new_blk);
65                 if (retval)
66                         return retval;
67                 for (j=0, blk = new_blk;
68                      j < fs->inode_blocks_per_group;
69                      j++, blk++)
70                         ext2fs_mark_block_bitmap(fs->block_map, blk);
71                 fs->group_desc[i].bg_inode_table = new_blk;
72
73                 /*
74                  * Increment the start of the block group
75                  */
76                 group_blk += fs->super->s_blocks_per_group;
77         }
78         return 0;
79 }
80