blob: e7e07c46bb68108b55b396121e13a56d7bac513a [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * initialize.c --- initialize a filesystem handle given superblock
3 * parameters. Used by mke2fs when initializing a filesystem.
4 */
5
6#include <stdio.h>
7#include <string.h>
8#include <unistd.h>
9#include <stdlib.h>
10#include <fcntl.h>
11#include <time.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14
15#include <linux/fs.h>
16#include <linux/ext2_fs.h>
17
18#include "ext2fs.h"
19
20errcode_t ext2fs_initialize(const char *name, int flags,
21 struct ext2_super_block *param,
22 io_manager manager, ext2_filsys *ret_fs)
23{
24 ext2_filsys fs;
25 errcode_t retval;
26 struct ext2_super_block *super;
27 int frags_per_block;
28 int rem;
29 int overhead = 0;
30 blk_t group_block;
31 int i, j;
32
33 if (!param || !param->s_blocks_count)
34 return EINVAL;
35
36 fs = (ext2_filsys) malloc(sizeof(struct struct_ext2_filsys));
37 if (!fs)
38 return ENOMEM;
39
40 memset(fs, 0, sizeof(struct struct_ext2_filsys));
41 fs->flags = flags | EXT2_FLAG_RW;
42 retval = manager->open(name, IO_FLAG_RW, &fs->io);
43 if (retval)
44 goto cleanup;
45 fs->device_name = malloc(strlen(name)+1);
46 if (!fs->device_name) {
47 retval = ENOMEM;
48 goto cleanup;
49 }
50 strcpy(fs->device_name, name);
51 fs->super = super = malloc(SUPERBLOCK_SIZE);
52 if (!super) {
53 retval = ENOMEM;
54 goto cleanup;
55 }
56 memset(super, 0, SUPERBLOCK_SIZE);
57
58#define set_field(field, default) (super->field = param->field ? \
59 param->field : (default))
60
61 super->s_magic = EXT2_SUPER_MAGIC;
62 super->s_state = EXT2_VALID_FS;
63
64 set_field(s_log_block_size, 0); /* default blocksize: 1024 bytes */
65 set_field(s_log_frag_size, 0); /* default fragsize: 1024 bytes */
66 set_field(s_first_data_block, super->s_log_block_size ? 0 : 1);
67 set_field(s_max_mnt_count, EXT2_DFL_MAX_MNT_COUNT);
68 set_field(s_errors, EXT2_ERRORS_DEFAULT);
69
70 set_field(s_checkinterval, EXT2_DFL_CHECKINTERVAL);
71 super->s_lastcheck = time(NULL);
72
73 fs->blocksize = EXT2_BLOCK_SIZE(super);
74 fs->fragsize = EXT2_FRAG_SIZE(super);
75 frags_per_block = fs->blocksize / fs->fragsize;
76
77 set_field(s_blocks_per_group, 8192); /* default: 8192 blocks/group */
78 super->s_frags_per_group = super->s_blocks_per_group * frags_per_block;
79
80 super->s_blocks_count = param->s_blocks_count;
81
82retry:
83 set_field(s_r_blocks_count, super->s_blocks_count/20); /* 5% default */
84
85 fs->group_desc_count = (super->s_blocks_count -
86 super->s_first_data_block +
87 EXT2_BLOCKS_PER_GROUP(super) - 1)
88 / EXT2_BLOCKS_PER_GROUP(super);
89 fs->desc_blocks = (fs->group_desc_count +
90 EXT2_DESC_PER_BLOCK(super) - 1)
91 / EXT2_DESC_PER_BLOCK(super);
92
93 set_field(s_inodes_count, (super->s_blocks_count*fs->blocksize)/4096);
94
95 /*
96 * There should be at least as many inodes as the user
97 * requested. Figure out how many inodes per group that
98 * should be.
99 */
100 super->s_inodes_per_group = (super->s_inodes_count +
101 fs->group_desc_count - 1) /
102 fs->group_desc_count;
103
104 /*
105 * Make sure the number of inodes per group completely fills
106 * the inode table blocks in the descriptor. If not, add some
107 * additional inodes/group. Waste not, want not...
108 */
109 fs->inode_blocks_per_group = (super->s_inodes_per_group +
110 EXT2_INODES_PER_BLOCK(super) - 1) /
111 EXT2_INODES_PER_BLOCK(super);
112 super->s_inodes_per_group = fs->inode_blocks_per_group *
113 EXT2_INODES_PER_BLOCK(super);
114
115 /*
116 * adjust inode count to reflect the adjusted inodes_per_group
117 */
118 super->s_inodes_count = super->s_inodes_per_group *
119 fs->group_desc_count;
120 super->s_free_inodes_count = super->s_inodes_count;
121
122 /*
123 * Overhead is the number of bookkeeping blocks per group. It
124 * includes the superblock backup, the group descriptor
125 * backups, the inode bitmap, the block bitmap, and the inode
126 * table.
127 */
128 overhead = 3 + fs->desc_blocks + fs->inode_blocks_per_group;
129 super->s_free_blocks_count = super->s_blocks_count -
130 super->s_first_data_block - (overhead*fs->group_desc_count);
131
132 /*
133 * See if the last group is big enough to support the
134 * necessary data structures. If not, we need to get rid of
135 * it.
136 */
137 rem = (super->s_blocks_count - super->s_first_data_block) %
138 super->s_blocks_per_group;
139 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
140 return EXT2_ET_TOOSMALL;
141 if (rem && (rem < overhead+50)) {
142 super->s_blocks_count -= rem;
143 goto retry;
144 }
145
146 /*
147 * At this point we know how big the filesystem will be. So
148 * we can do any and all allocations that depend on the block
149 * count.
150 */
151
152 retval = ext2fs_allocate_block_bitmap(fs, &fs->block_map);
153 if (retval)
154 goto cleanup;
155
156 retval = ext2fs_allocate_inode_bitmap(fs, &fs->inode_map);
157 if (retval)
158 goto cleanup;
159
160 fs->group_desc = malloc(fs->desc_blocks * fs->blocksize);
161 if (!fs->group_desc) {
162 retval = ENOMEM;
163 goto cleanup;
164 }
165 memset(fs->group_desc, 0, fs->desc_blocks * fs->blocksize);
166
167 group_block = super->s_first_data_block;
168 for (i = 0; i < fs->group_desc_count; i++) {
169 for (j=0; j < fs->desc_blocks+1; j++)
170 ext2fs_mark_block_bitmap(fs, fs->block_map,
171 group_block + j);
172 group_block += super->s_blocks_per_group;
173 }
174
175 ext2fs_mark_super_dirty(fs);
176 ext2fs_mark_bb_dirty(fs);
177 ext2fs_mark_ib_dirty(fs);
178
179 io_channel_set_blksize(fs->io, fs->blocksize);
180
181 *ret_fs = fs;
182 return 0;
183cleanup:
184 ext2fs_free(fs);
185 return retval;
186}
187
188
189