blob: 7c60e2bf5fcfc854cc7d9ffee543c70547b4fb47 [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * alloc_tables.c --- Allocate tables for a newly initialized
4 * filesystem. Used by mke2fs when initializing a filesystem
5 *
6 * Copyright (C) 1996 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
12 */
13
14#include <stdio.h>
15#include <string.h>
16#if HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19#include <fcntl.h>
20#include <time.h>
21#if HAVE_SYS_STAT_H
22#include <sys/stat.h>
23#endif
24#if HAVE_SYS_TYPES_H
25#include <sys/types.h>
26#endif
27
28#include "ext2_fs.h"
29#include "ext2fs.h"
30
31errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t 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 if (!bmap)
46 bmap = fs->block_map;
47
48 /*
49 * Allocate the block and inode bitmaps, if necessary
50 */
51 if (fs->stride) {
52 start_blk = group_blk + fs->inode_blocks_per_group;
53 start_blk += ((fs->stride * group) %
54 (last_blk - start_blk));
55 if (start_blk > last_blk)
56 start_blk = group_blk;
57 } else
58 start_blk = group_blk;
59
60 if (!fs->group_desc[group].bg_block_bitmap) {
61 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
62 1, bmap, &new_blk);
63 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
64 retval = ext2fs_get_free_blocks(fs, group_blk,
65 last_blk, 1, bmap, &new_blk);
66 if (retval)
67 return retval;
68 ext2fs_mark_block_bitmap(bmap, new_blk);
69 fs->group_desc[group].bg_block_bitmap = new_blk;
70 }
71
72 if (!fs->group_desc[group].bg_inode_bitmap) {
73 retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
74 1, bmap, &new_blk);
75 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
76 retval = ext2fs_get_free_blocks(fs, group_blk,
77 last_blk, 1, bmap, &new_blk);
78 if (retval)
79 return retval;
80 ext2fs_mark_block_bitmap(bmap, new_blk);
81 fs->group_desc[group].bg_inode_bitmap = new_blk;
82 }
83
84 /*
85 * Allocate the inode table
86 */
87 if (!fs->group_desc[group].bg_inode_table) {
88 retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
89 fs->inode_blocks_per_group,
90 bmap, &new_blk);
91 if (retval)
92 return retval;
93 for (j=0, blk = new_blk;
94 j < fs->inode_blocks_per_group;
95 j++, blk++)
96 ext2fs_mark_block_bitmap(bmap, blk);
97 fs->group_desc[group].bg_inode_table = new_blk;
98 }
99
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000100 return 0;
101}
102
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000103errcode_t ext2fs_allocate_tables(ext2_filsys fs)
104{
105 errcode_t retval;
106 dgrp_t 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}