blob: 7428b0d4747bd4b1b97b72fe13afc81922344b9a [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>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000014#if HAVE_ERRNO_H
15#include <errno.h>
16#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000017
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <linux/ext2_fs.h>
19
20#include "ext2fs.h"
21
Theodore Ts'o50e1e101997-04-26 13:58:21 +000022#if defined(__linux__) && defined(EXT2_OS_LINUX)
23#define CREATOR_OS EXT2_OS_LINUX
24#elif defined(__gnu__) && defined(EXT2_OS_HURD)
25#define CREATOR_OS EXT2_OS_HURD
26#elif defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD)
27#define CREATOR_OS EXT2_OS_FREEBSD
28#elif defined(LITES) && defined(EXT2_OS_LITES)
29#define CREATOR_OS EXT2_OS_LITES
30#else
31#define CREATOR_OS EXT2_OS_LINUX /* by default */
32#endif
33
Theodore Ts'o3839e651997-04-26 13:21:57 +000034errcode_t ext2fs_initialize(const char *name, int flags,
35 struct ext2_super_block *param,
36 io_manager manager, ext2_filsys *ret_fs)
37{
38 ext2_filsys fs;
39 errcode_t retval;
40 struct ext2_super_block *super;
41 int frags_per_block;
42 int rem;
43 int overhead = 0;
44 blk_t group_block;
45 int i, j;
Theodore Ts'of3db3561997-04-26 13:34:30 +000046 int numblocks;
47 char *buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +000048
49 if (!param || !param->s_blocks_count)
50 return EINVAL;
51
52 fs = (ext2_filsys) malloc(sizeof(struct struct_ext2_filsys));
53 if (!fs)
54 return ENOMEM;
55
56 memset(fs, 0, sizeof(struct struct_ext2_filsys));
Theodore Ts'of3db3561997-04-26 13:34:30 +000057 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
Theodore Ts'o3839e651997-04-26 13:21:57 +000058 fs->flags = flags | EXT2_FLAG_RW;
59 retval = manager->open(name, IO_FLAG_RW, &fs->io);
60 if (retval)
61 goto cleanup;
62 fs->device_name = malloc(strlen(name)+1);
63 if (!fs->device_name) {
64 retval = ENOMEM;
65 goto cleanup;
66 }
67 strcpy(fs->device_name, name);
68 fs->super = super = malloc(SUPERBLOCK_SIZE);
69 if (!super) {
70 retval = ENOMEM;
71 goto cleanup;
72 }
73 memset(super, 0, SUPERBLOCK_SIZE);
74
75#define set_field(field, default) (super->field = param->field ? \
76 param->field : (default))
77
78 super->s_magic = EXT2_SUPER_MAGIC;
79 super->s_state = EXT2_VALID_FS;
80
81 set_field(s_log_block_size, 0); /* default blocksize: 1024 bytes */
82 set_field(s_log_frag_size, 0); /* default fragsize: 1024 bytes */
83 set_field(s_first_data_block, super->s_log_block_size ? 0 : 1);
84 set_field(s_max_mnt_count, EXT2_DFL_MAX_MNT_COUNT);
85 set_field(s_errors, EXT2_ERRORS_DEFAULT);
86
87 set_field(s_checkinterval, EXT2_DFL_CHECKINTERVAL);
88 super->s_lastcheck = time(NULL);
89
Theodore Ts'o50e1e101997-04-26 13:58:21 +000090 super->s_creator_os = CREATOR_OS;
Theodore Ts'of3db3561997-04-26 13:34:30 +000091
Theodore Ts'o3839e651997-04-26 13:21:57 +000092 fs->blocksize = EXT2_BLOCK_SIZE(super);
93 fs->fragsize = EXT2_FRAG_SIZE(super);
94 frags_per_block = fs->blocksize / fs->fragsize;
95
96 set_field(s_blocks_per_group, 8192); /* default: 8192 blocks/group */
97 super->s_frags_per_group = super->s_blocks_per_group * frags_per_block;
98
99 super->s_blocks_count = param->s_blocks_count;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000100 super->s_r_blocks_count = param->s_r_blocks_count;
101 if (super->s_r_blocks_count >= param->s_blocks_count) {
102 retval = EINVAL;
103 goto cleanup;
104 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105
106retry:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107 fs->group_desc_count = (super->s_blocks_count -
108 super->s_first_data_block +
109 EXT2_BLOCKS_PER_GROUP(super) - 1)
110 / EXT2_BLOCKS_PER_GROUP(super);
111 fs->desc_blocks = (fs->group_desc_count +
112 EXT2_DESC_PER_BLOCK(super) - 1)
113 / EXT2_DESC_PER_BLOCK(super);
114
115 set_field(s_inodes_count, (super->s_blocks_count*fs->blocksize)/4096);
116
117 /*
118 * There should be at least as many inodes as the user
119 * requested. Figure out how many inodes per group that
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000120 * should be. But make sure that we don't allocate more than
121 * one bitmap's worth of inodes
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122 */
123 super->s_inodes_per_group = (super->s_inodes_count +
124 fs->group_desc_count - 1) /
125 fs->group_desc_count;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000126 if (super->s_inodes_per_group > fs->blocksize*8)
127 super->s_inodes_per_group = fs->blocksize*8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128
129 /*
130 * Make sure the number of inodes per group completely fills
131 * the inode table blocks in the descriptor. If not, add some
132 * additional inodes/group. Waste not, want not...
133 */
134 fs->inode_blocks_per_group = (super->s_inodes_per_group +
135 EXT2_INODES_PER_BLOCK(super) - 1) /
136 EXT2_INODES_PER_BLOCK(super);
137 super->s_inodes_per_group = fs->inode_blocks_per_group *
138 EXT2_INODES_PER_BLOCK(super);
139
140 /*
141 * adjust inode count to reflect the adjusted inodes_per_group
142 */
143 super->s_inodes_count = super->s_inodes_per_group *
144 fs->group_desc_count;
145 super->s_free_inodes_count = super->s_inodes_count;
146
147 /*
148 * Overhead is the number of bookkeeping blocks per group. It
149 * includes the superblock backup, the group descriptor
150 * backups, the inode bitmap, the block bitmap, and the inode
151 * table.
152 */
153 overhead = 3 + fs->desc_blocks + fs->inode_blocks_per_group;
154 super->s_free_blocks_count = super->s_blocks_count -
155 super->s_first_data_block - (overhead*fs->group_desc_count);
156
157 /*
158 * See if the last group is big enough to support the
159 * necessary data structures. If not, we need to get rid of
160 * it.
161 */
162 rem = (super->s_blocks_count - super->s_first_data_block) %
163 super->s_blocks_per_group;
164 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
165 return EXT2_ET_TOOSMALL;
166 if (rem && (rem < overhead+50)) {
167 super->s_blocks_count -= rem;
168 goto retry;
169 }
170
171 /*
172 * At this point we know how big the filesystem will be. So
173 * we can do any and all allocations that depend on the block
174 * count.
175 */
176
Theodore Ts'of3db3561997-04-26 13:34:30 +0000177 buf = malloc(strlen(fs->device_name) + 80);
178 if (!buf) {
179 retval = ENOMEM;
180 goto cleanup;
181 }
182
183 sprintf(buf, "block bitmap for %s", fs->device_name);
184 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185 if (retval)
186 goto cleanup;
187
Theodore Ts'of3db3561997-04-26 13:34:30 +0000188 sprintf(buf, "inode bitmap for %s", fs->device_name);
189 retval = ext2fs_allocate_inode_bitmap(fs, 0, &fs->inode_map);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190 if (retval)
191 goto cleanup;
192
Theodore Ts'of3db3561997-04-26 13:34:30 +0000193 free(buf);
194
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 fs->group_desc = malloc(fs->desc_blocks * fs->blocksize);
196 if (!fs->group_desc) {
197 retval = ENOMEM;
198 goto cleanup;
199 }
200 memset(fs->group_desc, 0, fs->desc_blocks * fs->blocksize);
201
Theodore Ts'of3db3561997-04-26 13:34:30 +0000202 /*
203 * Reserve the superblock and group descriptors for each
204 * group, and fill in the correct group statistics for group.
205 * Note that although the block bitmap, inode bitmap, and
206 * inode table have not been allocated (and in fact won't be
207 * by this routine), they are accounted for nevertheless.
208 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 group_block = super->s_first_data_block;
210 for (i = 0; i < fs->group_desc_count; i++) {
211 for (j=0; j < fs->desc_blocks+1; j++)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000212 ext2fs_mark_block_bitmap(fs->block_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000213 group_block + j);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000214
215 if (i == fs->group_desc_count-1) {
216 numblocks = (fs->super->s_blocks_count -
217 fs->super->s_first_data_block) %
218 fs->super->s_blocks_per_group;
219 if (!numblocks)
220 numblocks = fs->super->s_blocks_per_group;
221 } else
222 numblocks = fs->super->s_blocks_per_group;
223 numblocks -= 3 + fs->desc_blocks + fs->inode_blocks_per_group;
224
225 fs->group_desc[i].bg_free_blocks_count = numblocks;
226 fs->group_desc[i].bg_free_inodes_count =
227 fs->super->s_inodes_per_group;
228 fs->group_desc[i].bg_used_dirs_count = 0;
229
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230 group_block += super->s_blocks_per_group;
231 }
232
233 ext2fs_mark_super_dirty(fs);
234 ext2fs_mark_bb_dirty(fs);
235 ext2fs_mark_ib_dirty(fs);
236
237 io_channel_set_blksize(fs->io, fs->blocksize);
238
239 *ret_fs = fs;
240 return 0;
241cleanup:
242 ext2fs_free(fs);
243 return retval;
244}
245
246
247