2 * initialize.c --- initialize a filesystem handle given superblock
3 * parameters. Used by mke2fs when initializing a filesystem.
5 * Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
25 #include <sys/types.h>
35 #if defined(__linux__) && defined(EXT2_OS_LINUX)
36 #define CREATOR_OS EXT2_OS_LINUX
38 #if defined(__GNU__) && defined(EXT2_OS_HURD)
39 #define CREATOR_OS EXT2_OS_HURD
41 #if defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD)
42 #define CREATOR_OS EXT2_OS_FREEBSD
44 #if defined(LITES) && defined(EXT2_OS_LITES)
45 #define CREATOR_OS EXT2_OS_LITES
47 #define CREATOR_OS EXT2_OS_LINUX /* by default */
48 #endif /* defined(LITES) && defined(EXT2_OS_LITES) */
49 #endif /* defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD) */
50 #endif /* defined(__GNU__) && defined(EXT2_OS_HURD) */
51 #endif /* defined(__linux__) && defined(EXT2_OS_LINUX) */
54 * Calculate the number of GDT blocks to reserve for online filesystem growth.
55 * The absolute maximum number of GDT blocks we can reserve is determined by
56 * the number of block pointers that can fit into a single block.
58 static unsigned int calc_reserved_gdt_blocks(ext2_filsys fs
)
60 struct ext2_super_block
*sb
= fs
->super
;
61 unsigned long bpg
= sb
->s_blocks_per_group
;
62 unsigned int gdpb
= EXT2_DESC_PER_BLOCK(sb
);
63 unsigned long max_blocks
= 0xffffffff;
64 unsigned long rsv_groups
;
67 /* We set it at 1024x the current filesystem size, or
68 * the upper block count limit (2^32), whichever is lower.
70 if (ext2fs_blocks_count(sb
) < max_blocks
/ 1024)
71 max_blocks
= ext2fs_blocks_count(sb
) * 1024;
73 * ext2fs_div64_ceil() is unnecessary because max_blocks is
74 * max _GDT_ blocks, which is limited to 32 bits.
76 rsv_groups
= ext2fs_div_ceil(max_blocks
- sb
->s_first_data_block
, bpg
);
77 rsv_gdb
= ext2fs_div_ceil(rsv_groups
, gdpb
) - fs
->desc_blocks
;
78 if (rsv_gdb
> EXT2_ADDR_PER_BLOCK(sb
))
79 rsv_gdb
= EXT2_ADDR_PER_BLOCK(sb
);
81 printf("max_blocks %lu, rsv_groups = %lu, rsv_gdb = %u\n",
82 max_blocks
, rsv_groups
, rsv_gdb
);
88 errcode_t
ext2fs_initialize(const char *name
, int flags
,
89 struct ext2_super_block
*param
,
90 io_manager manager
, ext2_filsys
*ret_fs
)
94 struct ext2_super_block
*super
;
96 unsigned int overhead
= 0;
106 unsigned reserved_inos
;
109 double reserved_ratio
;
112 if (!param
|| !ext2fs_blocks_count(param
))
113 return EXT2_ET_INVALID_ARGUMENT
;
115 retval
= ext2fs_get_mem(sizeof(struct struct_ext2_filsys
), &fs
);
119 memset(fs
, 0, sizeof(struct struct_ext2_filsys
));
120 fs
->magic
= EXT2_ET_MAGIC_EXT2FS_FILSYS
;
121 fs
->flags
= flags
| EXT2_FLAG_RW
;
123 fs
->default_bitmap_type
= EXT2FS_BMAP64_RBTREE
;
124 #ifdef WORDS_BIGENDIAN
125 fs
->flags
|= EXT2_FLAG_SWAP_BYTES
;
128 time_env
= getenv("E2FSPROGS_FAKE_TIME");
130 fs
->now
= strtoul(time_env
, NULL
, 0);
132 io_flags
= IO_FLAG_RW
;
133 if (flags
& EXT2_FLAG_EXCLUSIVE
)
134 io_flags
|= IO_FLAG_EXCLUSIVE
;
135 if (flags
& EXT2_FLAG_DIRECT_IO
)
136 io_flags
|= IO_FLAG_DIRECT_IO
;
137 io_flags
|= O_BINARY
;
138 retval
= manager
->open(name
, io_flags
, &fs
->io
);
141 fs
->image_io
= fs
->io
;
142 fs
->io
->app_data
= fs
;
143 retval
= ext2fs_get_mem(strlen(name
)+1, &fs
->device_name
);
147 strcpy(fs
->device_name
, name
);
148 retval
= ext2fs_get_mem(SUPERBLOCK_SIZE
, &super
);
153 memset(super
, 0, SUPERBLOCK_SIZE
);
155 #define set_field(field, default) (super->field = param->field ? \
156 param->field : (default))
157 #define assign_field(field) (super->field = param->field)
159 super
->s_magic
= EXT2_SUPER_MAGIC
;
160 super
->s_state
= EXT2_VALID_FS
;
162 bigalloc_flag
= ext2fs_has_feature_bigalloc(param
);
164 assign_field(s_log_block_size
);
167 set_field(s_log_cluster_size
, super
->s_log_block_size
+4);
168 if (super
->s_log_block_size
> super
->s_log_cluster_size
) {
169 retval
= EXT2_ET_INVALID_ARGUMENT
;
173 super
->s_log_cluster_size
= super
->s_log_block_size
;
175 set_field(s_first_data_block
, super
->s_log_cluster_size
? 0 : 1);
176 set_field(s_max_mnt_count
, 0);
177 set_field(s_errors
, EXT2_ERRORS_DEFAULT
);
178 set_field(s_feature_compat
, 0);
179 set_field(s_feature_incompat
, 0);
180 set_field(s_feature_ro_compat
, 0);
181 set_field(s_default_mount_opts
, 0);
182 set_field(s_first_meta_bg
, 0);
183 set_field(s_raid_stride
, 0); /* default stride size: 0 */
184 set_field(s_raid_stripe_width
, 0); /* default stripe width: 0 */
185 set_field(s_log_groups_per_flex
, 0);
186 set_field(s_flags
, 0);
187 assign_field(s_backup_bgs
[0]);
188 assign_field(s_backup_bgs
[1]);
190 assign_field(s_encoding
);
191 assign_field(s_encoding_flags
);
193 if (ext2fs_has_feature_fname_encoding(param
))
194 fs
->encoding
= ext2fs_load_nls_table(param
->s_encoding
);
196 if (super
->s_feature_incompat
& ~EXT2_LIB_FEATURE_INCOMPAT_SUPP
) {
197 retval
= EXT2_ET_UNSUPP_FEATURE
;
200 if (super
->s_feature_ro_compat
& ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP
) {
201 retval
= EXT2_ET_RO_UNSUPP_FEATURE
;
205 set_field(s_rev_level
, EXT2_GOOD_OLD_REV
);
206 if (super
->s_rev_level
>= EXT2_DYNAMIC_REV
) {
207 set_field(s_first_ino
, EXT2_GOOD_OLD_FIRST_INO
);
208 set_field(s_inode_size
, EXT2_GOOD_OLD_INODE_SIZE
);
209 if (super
->s_inode_size
>= sizeof(struct ext2_inode_large
)) {
210 int extra_isize
= sizeof(struct ext2_inode_large
) -
211 EXT2_GOOD_OLD_INODE_SIZE
;
212 set_field(s_min_extra_isize
, extra_isize
);
213 set_field(s_want_extra_isize
, extra_isize
);
216 super
->s_first_ino
= EXT2_GOOD_OLD_FIRST_INO
;
217 super
->s_inode_size
= EXT2_GOOD_OLD_INODE_SIZE
;
220 set_field(s_checkinterval
, 0);
221 super
->s_mkfs_time
= super
->s_lastcheck
= fs
->now
? fs
->now
: time(NULL
);
223 super
->s_creator_os
= CREATOR_OS
;
225 fs
->fragsize
= fs
->blocksize
= EXT2_BLOCK_SIZE(super
);
226 fs
->cluster_ratio_bits
= super
->s_log_cluster_size
-
227 super
->s_log_block_size
;
230 unsigned long long bpg
;
232 if (param
->s_blocks_per_group
&&
233 param
->s_clusters_per_group
&&
234 ((param
->s_clusters_per_group
* EXT2FS_CLUSTER_RATIO(fs
)) !=
235 param
->s_blocks_per_group
)) {
236 retval
= EXT2_ET_INVALID_ARGUMENT
;
239 if (param
->s_clusters_per_group
)
240 assign_field(s_clusters_per_group
);
241 else if (param
->s_blocks_per_group
)
242 super
->s_clusters_per_group
=
243 param
->s_blocks_per_group
/
244 EXT2FS_CLUSTER_RATIO(fs
);
245 else if (super
->s_log_cluster_size
+ 15 < 32)
246 super
->s_clusters_per_group
= fs
->blocksize
* 8;
248 super
->s_clusters_per_group
= (fs
->blocksize
- 1) * 8;
249 if (super
->s_clusters_per_group
> EXT2_MAX_CLUSTERS_PER_GROUP(super
))
250 super
->s_clusters_per_group
= EXT2_MAX_CLUSTERS_PER_GROUP(super
);
252 (unsigned long long) super
->s_clusters_per_group
);
253 if (bpg
>= (((unsigned long long) 1) << 32)) {
254 retval
= EXT2_ET_INVALID_ARGUMENT
;
257 super
->s_blocks_per_group
= bpg
;
259 set_field(s_blocks_per_group
, fs
->blocksize
* 8);
260 if (super
->s_blocks_per_group
> EXT2_MAX_BLOCKS_PER_GROUP(super
))
261 super
->s_blocks_per_group
= EXT2_MAX_BLOCKS_PER_GROUP(super
);
262 super
->s_clusters_per_group
= super
->s_blocks_per_group
;
265 ext2fs_blocks_count_set(super
, ext2fs_blocks_count(param
) &
266 ~((blk64_t
) EXT2FS_CLUSTER_MASK(fs
)));
267 ext2fs_r_blocks_count_set(super
, ext2fs_r_blocks_count(param
));
268 if (ext2fs_r_blocks_count(super
) >= ext2fs_blocks_count(param
)) {
269 retval
= EXT2_ET_INVALID_ARGUMENT
;
273 set_field(s_mmp_update_interval
, 0);
276 * If we're creating an external journal device, we don't need
277 * to bother with the rest.
279 if (ext2fs_has_feature_journal_dev(super
)) {
280 fs
->group_desc_count
= 0;
281 ext2fs_mark_super_dirty(fs
);
287 fs
->group_desc_count
= (dgrp_t
) ext2fs_div64_ceil(
288 ext2fs_blocks_count(super
) - super
->s_first_data_block
,
289 EXT2_BLOCKS_PER_GROUP(super
));
290 if (fs
->group_desc_count
== 0) {
291 retval
= EXT2_ET_TOOSMALL
;
295 set_field(s_desc_size
,
296 ext2fs_has_feature_64bit(super
) ?
297 EXT2_MIN_DESC_SIZE_64BIT
: 0);
299 fs
->desc_blocks
= ext2fs_div_ceil(fs
->group_desc_count
,
300 EXT2_DESC_PER_BLOCK(super
));
302 i
= fs
->blocksize
>= 4096 ? 1 : 4096 / fs
->blocksize
;
304 if (ext2fs_has_feature_64bit(super
) &&
305 (ext2fs_blocks_count(super
) / i
) >= (1ULL << 32))
306 set_field(s_inodes_count
, ~0U);
308 set_field(s_inodes_count
, ext2fs_blocks_count(super
) / i
);
311 * Make sure we have at least EXT2_FIRST_INO + 1 inodes, so
312 * that we have enough inodes for the filesystem(!)
314 if (super
->s_inodes_count
< EXT2_FIRST_INODE(super
)+1)
315 super
->s_inodes_count
= EXT2_FIRST_INODE(super
)+1;
318 * There should be at least as many inodes as the user
319 * requested. Figure out how many inodes per group that
320 * should be. But make sure that we don't allocate more than
321 * one bitmap's worth of inodes each group.
323 ipg
= ext2fs_div_ceil(super
->s_inodes_count
, fs
->group_desc_count
);
324 if (ipg
> fs
->blocksize
* 8) {
325 if (!bigalloc_flag
&& super
->s_blocks_per_group
>= 256) {
326 /* Try again with slightly different parameters */
327 super
->s_blocks_per_group
-= 8;
328 ext2fs_blocks_count_set(super
,
329 ext2fs_blocks_count(param
));
330 super
->s_clusters_per_group
= super
->s_blocks_per_group
;
333 retval
= EXT2_ET_TOO_MANY_INODES
;
338 if (ipg
> (unsigned) EXT2_MAX_INODES_PER_GROUP(super
))
339 ipg
= EXT2_MAX_INODES_PER_GROUP(super
);
342 super
->s_inodes_per_group
= ipg
;
345 * Make sure the number of inodes per group completely fills
346 * the inode table blocks in the descriptor. If not, add some
347 * additional inodes/group. Waste not, want not...
349 fs
->inode_blocks_per_group
= (((super
->s_inodes_per_group
*
350 EXT2_INODE_SIZE(super
)) +
351 EXT2_BLOCK_SIZE(super
) - 1) /
352 EXT2_BLOCK_SIZE(super
));
353 super
->s_inodes_per_group
= ((fs
->inode_blocks_per_group
*
354 EXT2_BLOCK_SIZE(super
)) /
355 EXT2_INODE_SIZE(super
));
357 * Finally, make sure the number of inodes per group is a
358 * multiple of 8. This is needed to simplify the bitmap
361 if (super
->s_inodes_per_group
< 8)
362 super
->s_inodes_per_group
= 8;
363 super
->s_inodes_per_group
&= ~7;
364 fs
->inode_blocks_per_group
= (((super
->s_inodes_per_group
*
365 EXT2_INODE_SIZE(super
)) +
366 EXT2_BLOCK_SIZE(super
) - 1) /
367 EXT2_BLOCK_SIZE(super
));
370 * adjust inode count to reflect the adjusted inodes_per_group
372 if ((__u64
)super
->s_inodes_per_group
* fs
->group_desc_count
> ~0U) {
376 super
->s_inodes_count
= super
->s_inodes_per_group
*
377 fs
->group_desc_count
;
378 super
->s_free_inodes_count
= super
->s_inodes_count
;
381 * check the number of reserved group descriptor table blocks
383 if (ext2fs_has_feature_resize_inode(super
))
384 rsv_gdt
= calc_reserved_gdt_blocks(fs
);
387 set_field(s_reserved_gdt_blocks
, rsv_gdt
);
388 if (super
->s_reserved_gdt_blocks
> EXT2_ADDR_PER_BLOCK(super
)) {
389 retval
= EXT2_ET_RES_GDT_BLOCKS
;
392 /* Enable meta_bg if we'd lose more than 3/4 of a BG to GDT blocks. */
393 if (super
->s_reserved_gdt_blocks
+ fs
->desc_blocks
>
394 super
->s_blocks_per_group
* 3 / 4) {
395 ext2fs_set_feature_meta_bg(fs
->super
);
396 ext2fs_clear_feature_resize_inode(fs
->super
);
397 set_field(s_reserved_gdt_blocks
, 0);
401 * Calculate the maximum number of bookkeeping blocks per
402 * group. It includes the superblock, the block group
403 * descriptors, the block bitmap, the inode bitmap, the inode
404 * table, and the reserved gdt blocks.
406 overhead
= (int) (3 + fs
->inode_blocks_per_group
+
407 super
->s_reserved_gdt_blocks
);
409 if (ext2fs_has_feature_meta_bg(fs
->super
))
412 overhead
+= fs
->desc_blocks
;
414 /* This can only happen if the user requested too many inodes */
415 if (overhead
> super
->s_blocks_per_group
) {
416 retval
= EXT2_ET_TOO_MANY_INODES
;
421 * See if the last group is big enough to support the
422 * necessary data structures. If not, we need to get rid of
423 * it. We need to recalculate the overhead for the last block
424 * group, since it might or might not have a superblock
427 overhead
= (int) (2 + fs
->inode_blocks_per_group
);
429 if (ext2fs_has_feature_sparse_super2(super
)) {
431 * We have to do this manually since
432 * super->s_backup_bgs hasn't been set up yet.
434 if (fs
->group_desc_count
== 2)
435 has_bg
= param
->s_backup_bgs
[0] != 0;
437 has_bg
= param
->s_backup_bgs
[1] != 0;
439 has_bg
= ext2fs_bg_has_super(fs
, fs
->group_desc_count
- 1);
441 overhead
+= 1 + fs
->desc_blocks
+ super
->s_reserved_gdt_blocks
;
442 rem
= ((ext2fs_blocks_count(super
) - super
->s_first_data_block
) %
443 super
->s_blocks_per_group
);
444 if ((fs
->group_desc_count
== 1) && rem
&& (rem
< overhead
)) {
445 retval
= EXT2_ET_TOOSMALL
;
448 if (rem
&& (rem
< overhead
+50)) {
449 ext2fs_blocks_count_set(super
, ext2fs_blocks_count(super
) -
452 * If blocks count is changed, we need to recalculate
453 * reserved blocks count not to exceed 50%.
455 reserved_ratio
= 100.0 * ext2fs_r_blocks_count(param
) /
456 ext2fs_blocks_count(param
);
457 ext2fs_r_blocks_count_set(super
, reserved_ratio
*
458 ext2fs_blocks_count(super
) / 100.0);
464 * At this point we know how big the filesystem will be. So
465 * we can do any and all allocations that depend on the block
469 /* Set up the locations of the backup superblocks */
470 if (ext2fs_has_feature_sparse_super2(super
)) {
471 if (super
->s_backup_bgs
[0] >= fs
->group_desc_count
)
472 super
->s_backup_bgs
[0] = fs
->group_desc_count
- 1;
473 if (super
->s_backup_bgs
[1] >= fs
->group_desc_count
)
474 super
->s_backup_bgs
[1] = fs
->group_desc_count
- 1;
475 if (super
->s_backup_bgs
[0] == super
->s_backup_bgs
[1])
476 super
->s_backup_bgs
[1] = 0;
477 if (super
->s_backup_bgs
[0] > super
->s_backup_bgs
[1]) {
478 __u32 t
= super
->s_backup_bgs
[0];
479 super
->s_backup_bgs
[0] = super
->s_backup_bgs
[1];
480 super
->s_backup_bgs
[1] = t
;
484 retval
= ext2fs_get_mem(strlen(fs
->device_name
) + 80, &buf
);
488 strcpy(buf
, "block bitmap for ");
489 strcat(buf
, fs
->device_name
);
490 retval
= ext2fs_allocate_subcluster_bitmap(fs
, buf
, &fs
->block_map
);
494 strcpy(buf
, "inode bitmap for ");
495 strcat(buf
, fs
->device_name
);
496 retval
= ext2fs_allocate_inode_bitmap(fs
, buf
, &fs
->inode_map
);
500 ext2fs_free_mem(&buf
);
502 retval
= ext2fs_get_array(fs
->desc_blocks
, fs
->blocksize
,
507 memset(fs
->group_desc
, 0, (size_t) fs
->desc_blocks
* fs
->blocksize
);
510 * Reserve the superblock and group descriptors for each
511 * group, and fill in the correct group statistics for group.
512 * Note that although the block bitmap, inode bitmap, and
513 * inode table have not been allocated (and in fact won't be
514 * by this routine), they are accounted for nevertheless.
516 * If FLEX_BG meta-data grouping is used, only account for the
517 * superblock and group descriptors (the inode tables and
518 * bitmaps will be accounted for when allocated).
521 csum_flag
= ext2fs_has_group_desc_csum(fs
);
522 reserved_inos
= super
->s_first_ino
;
523 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
525 * Don't set the BLOCK_UNINIT group for the last group
526 * because the block bitmap needs to be padded.
529 if (i
!= fs
->group_desc_count
- 1)
530 ext2fs_bg_flags_set(fs
, i
,
531 EXT2_BG_BLOCK_UNINIT
);
532 ext2fs_bg_flags_set(fs
, i
, EXT2_BG_INODE_UNINIT
);
533 numblocks
= super
->s_inodes_per_group
;
535 if (numblocks
> reserved_inos
) {
536 numblocks
-= reserved_inos
;
539 reserved_inos
-= numblocks
;
543 ext2fs_bg_itable_unused_set(fs
, i
, numblocks
);
545 numblocks
= ext2fs_reserve_super_and_bgd(fs
, i
, fs
->block_map
);
546 if (fs
->super
->s_log_groups_per_flex
)
547 numblocks
+= 2 + fs
->inode_blocks_per_group
;
549 free_blocks
+= numblocks
;
550 ext2fs_bg_free_blocks_count_set(fs
, i
, numblocks
);
551 ext2fs_bg_free_inodes_count_set(fs
, i
, fs
->super
->s_inodes_per_group
);
552 ext2fs_bg_used_dirs_count_set(fs
, i
, 0);
553 ext2fs_group_desc_csum_set(fs
, i
);
555 free_blocks
&= ~EXT2FS_CLUSTER_MASK(fs
);
556 ext2fs_free_blocks_count_set(super
, free_blocks
);
559 if (((int) c
) == -1) {
560 super
->s_flags
|= EXT2_FLAGS_SIGNED_HASH
;
562 super
->s_flags
|= EXT2_FLAGS_UNSIGNED_HASH
;
565 ext2fs_mark_super_dirty(fs
);
566 ext2fs_mark_bb_dirty(fs
);
567 ext2fs_mark_ib_dirty(fs
);
569 io_channel_set_blksize(fs
->io
, fs
->blocksize
);