blob: 6a60bf7649519ceef841b84d78225843207eed8d [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'o2ecc6fe1997-04-29 17:57:00 +000038 if (last_blk >= fs->super->s_blocks_count)
39 last_blk = fs->super->s_blocks_count - 1;
40
Theodore Ts'o21c84b71997-04-29 16:15:03 +000041 /*
Theodore Ts'o521e3681997-04-29 17:48:10 +000042 * Allocate the inode table
Theodore Ts'o21c84b71997-04-29 16:15:03 +000043 */
Theodore Ts'o521e3681997-04-29 17:48:10 +000044 start_blk = group_blk + 3 + fs->desc_blocks;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000045 if (start_blk > last_blk)
46 start_blk = group_blk;
Theodore Ts'o521e3681997-04-29 17:48:10 +000047 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000048 fs->inode_blocks_per_group,
49 fs->block_map, &new_blk);
50 if (retval)
51 return retval;
52 for (j=0, blk = new_blk;
53 j < fs->inode_blocks_per_group;
54 j++, blk++)
55 ext2fs_mark_block_bitmap(fs->block_map, blk);
56 fs->group_desc[i].bg_inode_table = new_blk;
57
58 /*
Theodore Ts'o521e3681997-04-29 17:48:10 +000059 * Allocate the block and inode bitmaps
60 */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000061 if (fs->stride) {
62 start_blk += fs->inode_blocks_per_group;
63 start_blk += ((fs->stride * i) %
64 (last_blk - start_blk));
65 if (start_blk > last_blk)
66 /* should never happen */
67 start_blk = group_blk;
68 } else
69 start_blk = group_blk;
Theodore Ts'o521e3681997-04-29 17:48:10 +000070 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
71 1, fs->block_map, &new_blk);
72 if (retval)
73 return retval;
74 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
75 fs->group_desc[i].bg_block_bitmap = new_blk;
76
77 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
78 1, fs->block_map, &new_blk);
79 if (retval)
80 return retval;
81 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
82 fs->group_desc[i].bg_inode_bitmap = new_blk;
83
84 /*
Theodore Ts'o21c84b71997-04-29 16:15:03 +000085 * Increment the start of the block group
86 */
87 group_blk += fs->super->s_blocks_per_group;
88 }
89 return 0;
90}
91