blob: 4bfc91d3a02eb203791ef4c1d72cc5f8b0ff341c [file] [log] [blame]
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001/*
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
29errcode_t ext2fs_allocate_tables(ext2_filsys fs)
30{
31 errcode_t retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +000032 blk_t group_blk, start_blk, last_blk, new_blk, blk;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000033 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;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000038 /*
Theodore Ts'o521e3681997-04-29 17:48:10 +000039 * Allocate the inode table
Theodore Ts'o21c84b71997-04-29 16:15:03 +000040 */
Theodore Ts'o521e3681997-04-29 17:48:10 +000041 start_blk = group_blk + 3 + fs->desc_blocks;
42 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000043 fs->inode_blocks_per_group,
44 fs->block_map, &new_blk);
45 if (retval)
46 return retval;
47 for (j=0, blk = new_blk;
48 j < fs->inode_blocks_per_group;
49 j++, blk++)
50 ext2fs_mark_block_bitmap(fs->block_map, blk);
51 fs->group_desc[i].bg_inode_table = new_blk;
52
53 /*
Theodore Ts'o521e3681997-04-29 17:48:10 +000054 * Allocate the block and inode bitmaps
55 */
56 start_blk += fs->inode_blocks_per_group +
57 ((2 * i) % (last_blk - start_blk));
58 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
59 1, fs->block_map, &new_blk);
60 if (retval)
61 return retval;
62 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
63 fs->group_desc[i].bg_block_bitmap = new_blk;
64
65 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
66 1, fs->block_map, &new_blk);
67 if (retval)
68 return retval;
69 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
70 fs->group_desc[i].bg_inode_bitmap = new_blk;
71
72 /*
Theodore Ts'o21c84b71997-04-29 16:15:03 +000073 * Increment the start of the block group
74 */
75 group_blk += fs->super->s_blocks_per_group;
76 }
77 return 0;
78}
79