initialize.c revision 0c17cb25f24730dca138a976a390f105d2191736
1558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch/*
2558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch * initialize.c --- initialize a filesystem handle given superblock
3558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch * 	parameters.  Used by mke2fs when initializing a filesystem.
4558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch *
5558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch * Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
6558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch *
7ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch * %Begin-Header%
80529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch * This file may be redistributed under the terms of the GNU Public
9558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch * License.
10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) * %End-Header%
11ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch */
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include <stdio.h>
14558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include <string.h>
1503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#if HAVE_UNISTD_H
16ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include <unistd.h>
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
18e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include <fcntl.h>
19ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include <time.h>
20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#if HAVE_SYS_STAT_H
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <sys/stat.h>
22e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#endif
23558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#if HAVE_SYS_TYPES_H
24558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include <sys/types.h>
25558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#endif
26558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ext2_fs.h"
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ext2fs.h"
29558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
30558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#if defined(__linux__)    &&	defined(EXT2_OS_LINUX)
31558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#define CREATOR_OS EXT2_OS_LINUX
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#else
33558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#if defined(__GNU__)     &&	defined(EXT2_OS_HURD)
34558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#define CREATOR_OS EXT2_OS_HURD
35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#else
36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#if defined(__FreeBSD__) &&	defined(EXT2_OS_FREEBSD)
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#define CREATOR_OS EXT2_OS_FREEBSD
38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#else
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#if defined(LITES) 	   &&	defined(EXT2_OS_LITES)
40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#define CREATOR_OS EXT2_OS_LITES
41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#else
42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#define CREATOR_OS EXT2_OS_LINUX /* by default */
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif /* defined(LITES) && defined(EXT2_OS_LITES) */
44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif /* defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD) */
45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif /* defined(__GNU__)     && defined(EXT2_OS_HURD) */
46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif /* defined(__linux__)   && defined(EXT2_OS_LINUX) */
47558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
48558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch/*
49558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch * Note we override the kernel include file's idea of what the default
50558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch * check interval (never) should be.  It's a good idea to check at
51558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch * least *occasionally*, specially since servers will never rarely get
52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch * to reboot, since Linux is so robust these days.  :-)
53e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch *
54e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch * 180 days (six months) seems like a good value.
55e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch */
56e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#ifdef EXT2_DFL_CHECKINTERVAL
57e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#undef EXT2_DFL_CHECKINTERVAL
58e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#endif
59e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch/*
62e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch * Calculate the number of GDT blocks to reserve for online filesystem growth.
63e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch * The absolute maximum number of GDT blocks we can reserve is determined by
64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch * the number of block pointers that can fit into a single block.
65e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch */
66e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochstatic unsigned int calc_reserved_gdt_blocks(ext2_filsys fs)
67e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch{
68e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	struct ext2_super_block *sb = fs->super;
69e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	unsigned long bpg = sb->s_blocks_per_group;
70e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	unsigned int gdpb = fs->blocksize / sizeof(struct ext2_group_desc);
71e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	unsigned long max_blocks = 0xffffffff;
72e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	unsigned long rsv_groups;
73e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	unsigned int rsv_gdb;
74e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
75e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	/* We set it at 1024x the current filesystem size, or
76e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	 * the upper block count limit (2^32), whichever is lower.
77e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	 */
78e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	if (sb->s_blocks_count < max_blocks / 1024)
79e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch		max_blocks = sb->s_blocks_count * 1024;
80e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	rsv_groups = ext2fs_div_ceil(max_blocks - sb->s_first_data_block, bpg);
81e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) - fs->desc_blocks;
82e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb))
83e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch		rsv_gdb = EXT2_ADDR_PER_BLOCK(sb);
84e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#ifdef RES_GDT_DEBUG
85e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	printf("max_blocks %lu, rsv_groups = %lu, rsv_gdb = %u\n",
86e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	       max_blocks, rsv_groups, rsv_gdb);
87e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#endif
88e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
89e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	return rsv_gdb;
90e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
91e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
92e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdocherrcode_t ext2fs_initialize(const char *name, int flags,
93e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch			    struct ext2_super_block *param,
94e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch			    io_manager manager, ext2_filsys *ret_fs)
95e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch{
96e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	ext2_filsys	fs;
97e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	errcode_t	retval;
98e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	struct ext2_super_block *super;
99e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	int		frags_per_block;
100e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	unsigned int	rem;
101e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	unsigned int	overhead = 0;
102e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	unsigned int	ipg;
103e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	dgrp_t		i;
104e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	blk_t		numblocks;
105e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	int		rsv_gdt;
106e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	int		io_flags;
107e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	char		*buf;
108e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	char		c;
109e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
110e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	if (!param || !param->s_blocks_count)
111e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch		return EXT2_ET_INVALID_ARGUMENT;
112e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
113e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci	if (retval)
115e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch		return retval;
116e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
117e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	memset(fs, 0, sizeof(struct struct_ext2_filsys));
118e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
119e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	fs->flags = flags | EXT2_FLAG_RW;
120e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	fs->umask = 022;
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifdef WORDS_BIGENDIAN
122e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	fs->flags |= EXT2_FLAG_SWAP_BYTES;
123e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#endif
124e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	io_flags = IO_FLAG_RW;
125e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch	if (flags & EXT2_FLAG_EXCLUSIVE)
126e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch		io_flags |= IO_FLAG_EXCLUSIVE;
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	retval = manager->open(name, io_flags, &fs->io);
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	if (retval)
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)		goto cleanup;
130558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch	fs->image_io = fs->io;
13103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)	fs->io->app_data = fs;
1320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
1330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	if (retval)
134ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch		goto cleanup;
135ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	strcpy(fs->device_name, name);
137ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch	retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super);
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	if (retval)
139ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch		goto cleanup;
140ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch	fs->super = super;
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)	memset(super, 0, SUPERBLOCK_SIZE);
143424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define set_field(field, default) (super->field = param->field ? \
1450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch				   param->field : (default))
1460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	super->s_magic = EXT2_SUPER_MAGIC;
1480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	super->s_state = EXT2_VALID_FS;
1490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	set_field(s_log_block_size, 0);	/* default blocksize: 1024 bytes */
151424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)	set_field(s_log_frag_size, 0); /* default fragsize: 1024 bytes */
15203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)	set_field(s_first_data_block, super->s_log_block_size ? 0 : 1);
153424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)	set_field(s_max_mnt_count, EXT2_DFL_MAX_MNT_COUNT);
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_errors, EXT2_ERRORS_DEFAULT);
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_feature_compat, 0);
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_feature_incompat, 0);
157424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)	set_field(s_feature_ro_compat, 0);
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_first_meta_bg, 0);
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_raid_stride, 0);		/* default stride size: 0 */
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_raid_stripe_width, 0);	/* default stripe width: 0 */
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_flags, 0);
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	if (super->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)		retval = EXT2_ET_UNSUPP_FEATURE;
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)		goto cleanup;
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	}
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	if (super->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)		retval = EXT2_ET_RO_UNSUPP_FEATURE;
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)		goto cleanup;
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	}
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_rev_level, EXT2_GOOD_OLD_REV);
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)		set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)		set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	}
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_checkinterval, EXT2_DFL_CHECKINTERVAL);
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	super->s_mkfs_time = super->s_lastcheck = fs->now ? fs->now : time(NULL);
17903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	super->s_creator_os = CREATOR_OS;
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	fs->blocksize = EXT2_BLOCK_SIZE(super);
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	fs->fragsize = EXT2_FRAG_SIZE(super);
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	frags_per_block = fs->blocksize / fs->fragsize;
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	/* default: (fs->blocksize*8) blocks/group, up to 2^16 (GDT limit) */
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	set_field(s_blocks_per_group, fs->blocksize * 8);
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	if (super->s_blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(super))
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)		super->s_blocks_per_group = EXT2_MAX_BLOCKS_PER_GROUP(super);
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	super->s_frags_per_group = super->s_blocks_per_group * frags_per_block;
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
192558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch	super->s_blocks_count = param->s_blocks_count;
193558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch	super->s_r_blocks_count = param->s_r_blocks_count;
1940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	if (super->s_r_blocks_count >= param->s_blocks_count) {
1950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch		retval = EXT2_ET_INVALID_ARGUMENT;
1960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch		goto cleanup;
1970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	}
1980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	/*
20003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)	 * If we're creating an external journal device, we don't need
20103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)	 * to bother with the rest.
20203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)	 */
20303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)	if (super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
20403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)		fs->group_desc_count = 0;
2050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch		ext2fs_mark_super_dirty(fs);
20603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)		*ret_fs = fs;
2070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch		return 0;
2080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	}
2090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
21003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)retry:
21103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)	fs->group_desc_count = ext2fs_div_ceil(super->s_blocks_count -
21203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)					       super->s_first_data_block,
21303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)					       EXT2_BLOCKS_PER_GROUP(super));
21403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)	if (fs->group_desc_count == 0) {
2150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch		retval = EXT2_ET_TOOSMALL;
21603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)		goto cleanup;
2170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	}
2180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
2190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch					  EXT2_DESC_PER_BLOCK(super));
2200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	i = fs->blocksize >= 4096 ? 1 : 4096 / fs->blocksize;
2220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	set_field(s_inodes_count, super->s_blocks_count / i);
2230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	/*
2250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * Make sure we have at least EXT2_FIRST_INO + 1 inodes, so
2260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * that we have enough inodes for the filesystem(!)
2270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 */
2280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	if (super->s_inodes_count < EXT2_FIRST_INODE(super)+1)
2290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch		super->s_inodes_count = EXT2_FIRST_INODE(super)+1;
2300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	/*
2320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * There should be at least as many inodes as the user
2330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * requested.  Figure out how many inodes per group that
2340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * should be.  But make sure that we don't allocate more than
2350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * one bitmap's worth of inodes each group.
2360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 */
2370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	ipg = ext2fs_div_ceil(super->s_inodes_count, fs->group_desc_count);
2380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	if (ipg > fs->blocksize * 8) {
2390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch		if (super->s_blocks_per_group >= 256) {
2400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch			/* Try again with slightly different parameters */
2410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch			super->s_blocks_per_group -= 8;
2420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch			super->s_blocks_count = param->s_blocks_count;
2430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch			super->s_frags_per_group = super->s_blocks_per_group *
2440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch				frags_per_block;
2450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch			goto retry;
2460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch		} else
2470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch			return EXT2_ET_TOO_MANY_INODES;
2480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	}
2490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	if (ipg > (unsigned) EXT2_MAX_INODES_PER_GROUP(super))
2510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch		ipg = EXT2_MAX_INODES_PER_GROUP(super);
2520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochipg_retry:
2540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	super->s_inodes_per_group = ipg;
2550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	/*
2570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * Make sure the number of inodes per group completely fills
2580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * the inode table blocks in the descriptor.  If not, add some
2590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * additional inodes/group.  Waste not, want not...
2600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 */
2610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	fs->inode_blocks_per_group = (((super->s_inodes_per_group *
2620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch					EXT2_INODE_SIZE(super)) +
2630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch				       EXT2_BLOCK_SIZE(super) - 1) /
2640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch				      EXT2_BLOCK_SIZE(super));
2650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	super->s_inodes_per_group = ((fs->inode_blocks_per_group *
2660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch				      EXT2_BLOCK_SIZE(super)) /
2670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch				     EXT2_INODE_SIZE(super));
2680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	/*
2690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * Finally, make sure the number of inodes per group is a
2700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * multiple of 8.  This is needed to simplify the bitmap
2710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 * splicing code.
2720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	 */
2730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	super->s_inodes_per_group &= ~7;
2740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch	fs->inode_blocks_per_group = (((super->s_inodes_per_group *
2750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch					EXT2_INODE_SIZE(super)) +
276558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch				       EXT2_BLOCK_SIZE(super) - 1) /
277				      EXT2_BLOCK_SIZE(super));
278
279	/*
280	 * adjust inode count to reflect the adjusted inodes_per_group
281	 */
282	if ((__u64)super->s_inodes_per_group * fs->group_desc_count > ~0U) {
283		ipg--;
284		goto ipg_retry;
285	}
286	super->s_inodes_count = super->s_inodes_per_group *
287		fs->group_desc_count;
288	super->s_free_inodes_count = super->s_inodes_count;
289
290	/*
291	 * check the number of reserved group descriptor table blocks
292	 */
293	if (super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)
294		rsv_gdt = calc_reserved_gdt_blocks(fs);
295	else
296		rsv_gdt = 0;
297	set_field(s_reserved_gdt_blocks, rsv_gdt);
298	if (super->s_reserved_gdt_blocks > EXT2_ADDR_PER_BLOCK(super)) {
299		retval = EXT2_ET_RES_GDT_BLOCKS;
300		goto cleanup;
301	}
302
303	/*
304	 * Calculate the maximum number of bookkeeping blocks per
305	 * group.  It includes the superblock, the block group
306	 * descriptors, the block bitmap, the inode bitmap, the inode
307	 * table, and the reserved gdt blocks.
308	 */
309	overhead = (int) (3 + fs->inode_blocks_per_group +
310			  fs->desc_blocks + super->s_reserved_gdt_blocks);
311
312	/* This can only happen if the user requested too many inodes */
313	if (overhead > super->s_blocks_per_group)
314		return EXT2_ET_TOO_MANY_INODES;
315
316	/*
317	 * See if the last group is big enough to support the
318	 * necessary data structures.  If not, we need to get rid of
319	 * it.  We need to recalculate the overhead for the last block
320	 * group, since it might or might not have a superblock
321	 * backup.
322	 */
323	overhead = (int) (2 + fs->inode_blocks_per_group);
324	if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
325		overhead += 1 + fs->desc_blocks + super->s_reserved_gdt_blocks;
326	rem = ((super->s_blocks_count - super->s_first_data_block) %
327	       super->s_blocks_per_group);
328	if ((fs->group_desc_count == 1) && rem && (rem < overhead))
329		return EXT2_ET_TOOSMALL;
330	if (rem && (rem < overhead+50)) {
331		super->s_blocks_count -= rem;
332		goto retry;
333	}
334
335	/*
336	 * At this point we know how big the filesystem will be.  So
337	 * we can do any and all allocations that depend on the block
338	 * count.
339	 */
340
341	retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
342	if (retval)
343		goto cleanup;
344
345	sprintf(buf, "block bitmap for %s", fs->device_name);
346	retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
347	if (retval)
348		goto cleanup;
349
350	sprintf(buf, "inode bitmap for %s", fs->device_name);
351	retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
352	if (retval)
353		goto cleanup;
354
355	ext2fs_free_mem(&buf);
356
357	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
358				&fs->group_desc);
359	if (retval)
360		goto cleanup;
361
362	memset(fs->group_desc, 0, (size_t) fs->desc_blocks * fs->blocksize);
363
364	/*
365	 * Reserve the superblock and group descriptors for each
366	 * group, and fill in the correct group statistics for group.
367	 * Note that although the block bitmap, inode bitmap, and
368	 * inode table have not been allocated (and in fact won't be
369	 * by this routine), they are accounted for nevertheless.
370	 */
371	super->s_free_blocks_count = 0;
372	for (i = 0; i < fs->group_desc_count; i++) {
373		numblocks = ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
374
375		super->s_free_blocks_count += numblocks;
376		fs->group_desc[i].bg_free_blocks_count = numblocks;
377		fs->group_desc[i].bg_free_inodes_count =
378			fs->super->s_inodes_per_group;
379		fs->group_desc[i].bg_used_dirs_count = 0;
380	}
381
382	c = (char) 255;
383	if (((int) c) == -1) {
384		super->s_flags |= EXT2_FLAGS_SIGNED_HASH;
385	} else {
386		super->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
387	}
388
389	ext2fs_mark_super_dirty(fs);
390	ext2fs_mark_bb_dirty(fs);
391	ext2fs_mark_ib_dirty(fs);
392
393	io_channel_set_blksize(fs->io, fs->blocksize);
394
395	*ret_fs = fs;
396	return 0;
397cleanup:
398	ext2fs_free(fs);
399	return retval;
400}
401