mke2fs.c revision 2537b6d0c1aa9710a05cdfafc281b4884c67bdb2
13839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
23839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * mke2fs.c - Make a ext2fs filesystem.
33839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o *
419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o *
619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %Begin-Header%
719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * This file may be redistributed under the terms of the GNU Public
819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * License.
919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %End-Header%
103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/* Usage: mke2fs [options] device
133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o *
143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * The device may be a block device or a image of one, but this isn't
153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * enforced (but it's not much fun on a character device :-).
163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
18a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#include <stdio.h>
193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <string.h>
203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <fcntl.h>
213839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <ctype.h>
223839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <time.h>
232740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o#ifdef linux
242740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o#include <sys/utsname.h>
252740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o#endif
26a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_GETOPT_H
273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <getopt.h>
28373b8337c7b6c6243810be250083fa4773891e92Theodore Ts'o#else
29373b8337c7b6c6243810be250083fa4773891e92Theodore Ts'oextern char *optarg;
30373b8337c7b6c6243810be250083fa4773891e92Theodore Ts'oextern int optind;
31a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
32a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_UNISTD_H
333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <unistd.h>
34a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
35a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_STDLIB_H
363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <stdlib.h>
37a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
38a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_ERRNO_H
39a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#include <errno.h>
40a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
41a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_MNTENT_H
423839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <mntent.h>
43a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <sys/ioctl.h>
45f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#include <sys/types.h>
46f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
47f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#include <linux/ext2_fs.h>
483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "et/com_err.h"
501e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#include "uuid/uuid.h"
51896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o#include "e2p/e2p.h"
523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "ext2fs/ext2fs.h"
5363985320384bf143eaac9857af424800d9867a1aTheodore Ts'o#include "util.h"
543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "../version.h"
55d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#include "nls-enable.h"
563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#define STRIDE_LENGTH 8
583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
596733c2fd0046c525203034f58fc0a8c69fdf480bTheodore Ts'o#ifndef __sparc__
601e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#define ZAP_BOOTBLOCK
611e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#endif
621e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
633839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oextern int isatty(int);
64f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'oextern FILE *fpopen(const char *cmd, const char *mode);
653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oconst char * program_name = "mke2fs";
67d48755e97f7d4cb06092921bff0d3681d30a8acaTheodore Ts'oconst char * device_name /* = NULL */;
683839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/* Command line options */
7016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	cflag;
7116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	verbose;
7216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	quiet;
7316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	super_only;
7416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	force;
7516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	noaction;
7616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	journal_size;
7716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	journal_flags;
7816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ochar	*bad_blocks_filename;
7916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o__u32	fs_stride;
803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostruct ext2_super_block param;
8216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ochar *creator_os;
8316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ochar *volume_label;
8416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ochar *mount_dir;
8516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ochar *journal_device;
8616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint sync_kludge;	/* Set using the MKE2FS_SYNC env. option */
873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
8863985320384bf143eaac9857af424800d9867a1aTheodore Ts'ostatic void usage(void)
893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
90d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
91dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	"[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
925515e6b47de9ff941164a546dd9a74e9c6da3367Theodore Ts'o	" [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
935515e6b47de9ff941164a546dd9a74e9c6da3367Theodore Ts'o	"[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
9418160d26c0fcf8dbe990977c0231ebbc79fc9508Theodore Ts'o	"[-M last-mounted-directory] [-O feature[,...]]\n\t"
9518160d26c0fcf8dbe990977c0231ebbc79fc9508Theodore Ts'o	"[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
96d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	"[-qvSV] device [blocks-count]\n"),
973839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		program_name);
983839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	exit(1);
993839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
1003839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1016733c2fd0046c525203034f58fc0a8c69fdf480bTheodore Ts'ostatic int int_log2(int arg)
1023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
1033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	int	l = 0;
1043839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	arg >>= 1;
1063839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	while (arg) {
1073839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		l++;
1083839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		arg >>= 1;
1093839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
1103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	return l;
1113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
1123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1136733c2fd0046c525203034f58fc0a8c69fdf480bTheodore Ts'ostatic int int_log10(unsigned int arg)
1141dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o{
1151dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	int	l;
1161dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o
1171dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	for (l=0; arg ; l++)
1181dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o		arg = arg / 10;
1191dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	return l;
1201dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o}
1211dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o
1223839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
12350787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o * This function sets the default parameters for a filesystem
12450787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o *
1252740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o * The type is specified by the user.  The size is the maximum size
1262740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o * (in megabytes) for which a set of parameters applies, with a size
1272740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o * of zero meaning that it is the default parameter for the type.
1282740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o * Note that order is important in the table below.
12950787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o */
13050787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'ostatic char default_str[] = "default";
13150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'ostruct mke2fs_defaults {
1324a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	const char	*type;
1334a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		size;
1344a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		blocksize;
1354a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		inode_ratio;
13650787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o} settings[] = {
13750787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	{ default_str, 0, 4096, 8192 },
13850787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	{ default_str, 512, 1024, 4096 },
13950787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	{ default_str, 3, 1024, 8192 },
140dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	{ "journal", 0, 4096, 8192 },
14150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	{ "news", 0, 4096, 4096 },
14244c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	{ "largefile", 0, 4096, 1024 * 1024 },
14344c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	{ "largefile4", 0, 4096, 4096 * 1024 },
14450787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	{ 0, 0, 0, 0},
14550787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o};
14650787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o
147ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'ostatic void set_fs_defaults(char *fs_type, struct ext2_super_block *super,
14850787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			    int blocksize, int *inode_ratio)
14950787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o{
15050787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	int	megs;
15150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	int	ratio = 0;
15250787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	struct mke2fs_defaults *p;
15350787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o
1549094f28440b6c504a80d97aeea2dab543f97978bTheodore Ts'o	megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
15550787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		1024);
15650787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	if (inode_ratio)
15750787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		ratio = *inode_ratio;
15850787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	if (!fs_type)
15950787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		fs_type = default_str;
16050787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	for (p = settings; p->type; p++) {
16150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		if ((strcmp(p->type, fs_type) != 0) &&
16250787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		    (strcmp(p->type, default_str) != 0))
16350787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			continue;
16450787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		if ((p->size != 0) &&
16550787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		    (megs > p->size))
16650787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			continue;
16750787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		if (ratio == 0)
16850787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			*inode_ratio = p->inode_ratio;
16950787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		if (blocksize == 0) {
1709094f28440b6c504a80d97aeea2dab543f97978bTheodore Ts'o			super->s_log_frag_size = super->s_log_block_size =
1716733c2fd0046c525203034f58fc0a8c69fdf480bTheodore Ts'o				int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
17250787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		}
17350787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	}
17450787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	if (blocksize == 0)
1759094f28440b6c504a80d97aeea2dab543f97978bTheodore Ts'o		super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
17650787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o}
17750787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o
17850787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o/*
1793839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * Helper function for read_bb_file and test_disk
1803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
1813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void invalid_block(ext2_filsys fs, blk_t blk)
1823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
183d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("Bad block %u out of range; ignored.\n"), blk);
1843839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	return;
1853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
1863839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
1883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * Reads the bad blocks list from a file
1893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
1903839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
1913839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			 const char *bad_blocks_file)
1923839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
1933839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	FILE		*f;
1943839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t	retval;
1953839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1963839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	f = fopen(bad_blocks_file, "r");
1973839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (!f) {
1983839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		com_err("read_bad_blocks_file", errno,
199d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while trying to open %s"), bad_blocks_file);
2003839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
2013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
2033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	fclose (f);
2043839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
2053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		com_err("ext2fs_read_bb_FILE", retval,
206d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while reading in list of bad blocks from file"));
2073839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
2083839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2093839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
2123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * Runs the badblocks program to test the disk
2133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
2143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void test_disk(ext2_filsys fs, badblocks_list *bb_list)
2153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
2163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	FILE		*f;
2173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t	retval;
2183839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	char		buf[1024];
2193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
220f635d7f65bd002984ce9a202d491d4f187b996b2Theodore Ts'o	sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
221f635d7f65bd002984ce9a202d491d4f187b996b2Theodore Ts'o		quiet ? "" : "-s ", fs->device_name,
2223839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		fs->super->s_blocks_count);
2233839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (verbose)
224d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		printf(_("Running command: %s\n"), buf);
2253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	f = popen(buf, "r");
2263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (!f) {
2273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		com_err("popen", errno,
228d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while trying run '%s'"), buf);
2293839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
2303839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
232f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	pclose(f);
2333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
2343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		com_err("ext2fs_read_bb_FILE", retval,
235d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while processing list of bad blocks from program"));
2363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
2373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2393839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
2413839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
242f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	int			i, j;
2433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	int			must_be_good;
2443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	blk_t			blk;
2453839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	badblocks_iterate	bb_iter;
2463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t		retval;
247f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	blk_t			group_block;
248f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	int			group;
249f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	int			group_bad;
2503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (!bb_list)
2523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		return;
2533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
2553839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * The primary superblock and group descriptors *must* be
2563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * good; if not, abort.
2573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
2583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
2593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
2603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (badblocks_list_test(bb_list, i)) {
261d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			fprintf(stderr, _("Block %d in primary "
262d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				"superblock/group descriptor area bad.\n"), i);
263d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			fprintf(stderr, _("Blocks %d through %d must be good "
264d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				"in order to build a filesystem.\n"),
2653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				fs->super->s_first_data_block, must_be_good);
266d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			fprintf(stderr, _("Aborting....\n"));
2673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			exit(1);
2683839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
2693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
270f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
271f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	/*
272f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	 * See if any of the bad blocks are showing up in the backup
273f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	 * superblocks and/or group descriptors.  If so, issue a
274f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	 * warning and adjust the block counts appropriately.
275f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	 */
276f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	group_block = fs->super->s_first_data_block +
277f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		fs->super->s_blocks_per_group;
278f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
279f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	for (i = 1; i < fs->group_desc_count; i++) {
28092bcc595dcb31ad15e12d8c72e6edfc70545c204Theodore Ts'o		group_bad = 0;
281f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		for (j=0; j < fs->desc_blocks+1; j++) {
282f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			if (badblocks_list_test(bb_list, group_block +
283f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o						j)) {
284f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				if (!group_bad)
285f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o					fprintf(stderr,
286d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o_("Warning: the backup superblock/group descriptors at block %d contain\n"
287d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o"	bad blocks.\n\n"),
288f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o						group_block);
289f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				group_bad++;
290f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				group = ext2fs_group_of_blk(fs, group_block+j);
291f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				fs->group_desc[group].bg_free_blocks_count++;
292f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				fs->super->s_free_blocks_count++;
293f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			}
294f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		}
295f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		group_block += fs->super->s_blocks_per_group;
296f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	}
2973839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2983839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
2993839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * Mark all the bad blocks as used...
3003839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
3013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
3023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
3033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		com_err("badblocks_list_iterate_begin", retval,
304d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while marking bad blocks as used"));
3053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
3063839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
3073839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	while (badblocks_list_iterate(bb_iter, &blk))
308f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		ext2fs_mark_block_bitmap(fs->block_map, blk);
3093839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	badblocks_list_iterate_end(bb_iter);
3103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
3113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
31216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o/*
31316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o * These functions implement a generalized progress meter.
31416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o */
31516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ostruct progress_struct {
31616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	char		format[20];
31716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	char		backup[80];
31816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	__u32		max;
31916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o};
3207d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o
32116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ostatic void progress_init(struct progress_struct *progress,
32216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			  char *label,__u32 max)
32316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o{
32416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	int	i;
3253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
32616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	memset(progress, 0, sizeof(struct progress_struct));
32716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (quiet)
32816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		return;
3291dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o
3301dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	/*
3311dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	 * Figure out how many digits we need
3321dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	 */
33316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	i = int_log10(max);
33416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	sprintf(progress->format, "%%%dd/%%%dld", i, i);
33516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	memset(progress->backup, '\b', sizeof(progress->backup)-1);
33616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	progress->backup[sizeof(progress->backup)-1] = 0;
33716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if ((2*i)+1 < sizeof(progress->backup))
33816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		progress->backup[(2*i)+1] = 0;
33916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	progress->max = max;
34016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
34116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	fputs(label, stdout);
34216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	fflush(stdout);
34316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o}
34416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
34516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ostatic void progress_update(struct progress_struct *progress, __u32 val)
34616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o{
34716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (progress->format[0] == 0)
34816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		return;
34916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	printf(progress->format, val, progress->max);
35016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	fputs(progress->backup, stdout);
35116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o}
35216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
35316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ostatic void progress_close(struct progress_struct *progress)
35416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o{
35516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (progress->format[0] == 0)
35616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		return;
35716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	fputs(_("done                            \n"), stdout);
35816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o}
35916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
36016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
36116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o/*
36216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o * Helper function which zeros out _num_ blocks starting at _blk_.  In
36316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o * case of an error, the details of the error is returned via _ret_blk_
36416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o * and _ret_count_ if they are non-NULL pointers.  Returns 0 on
36516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o * success, and an error code on an error.
36616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o *
36716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o * As a special case, if the first argument is NULL, then it will
36816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o * attempt to free the static zeroizing buffer.  (This is to keep
36916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o * programs that check for memory leaks happy.)
37016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o */
37116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ostatic errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
37216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			     struct progress_struct *progress,
37316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			     blk_t *ret_blk, int *ret_count)
37416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o{
37516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	int		j, count, next_update, next_update_incr;
37616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	static char	*buf;
37716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	errcode_t	retval;
37816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
37916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	/* If fs is null, clean up the static buffer and return */
38016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (!fs) {
38116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if (buf) {
38216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			free(buf);
38316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			buf = 0;
38416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		}
38516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		return 0;
38616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
38716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	/* Allocate the zeroizing buffer if necessary */
38816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (!buf) {
38916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		buf = malloc(fs->blocksize * STRIDE_LENGTH);
39016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if (!buf) {
39116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			com_err("malloc", ENOMEM,
39216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				_("while allocating zeroizing buffer"));
39316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			exit(1);
39416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		}
39516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
39616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
39716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	/* OK, do the write loop */
39816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	next_update = 0;
39916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	next_update_incr = num / 100;
40016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (next_update_incr < 1)
40116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		next_update_incr = 1;
40216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
40316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if (num-j > STRIDE_LENGTH)
40416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			count = STRIDE_LENGTH;
40516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		else
40616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			count = num - j;
40716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		retval = io_channel_write_blk(fs->io, blk, count, buf);
40816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if (retval) {
40916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			if (ret_count)
41016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				*ret_count = count;
41116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			if (ret_blk)
41216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				*ret_blk = blk;
41316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			return retval;
41416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		}
41516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if (progress && j > next_update) {
41616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			next_update += num / 100;
41716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			progress_update(progress, blk);
41816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		}
41916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
42016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	return 0;
42116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o}
42216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
42316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ostatic void write_inode_tables(ext2_filsys fs)
42416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o{
42516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	errcode_t	retval;
42616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	blk_t		blk;
42716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	int		i, num;
42816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	struct progress_struct progress;
42916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
43016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (quiet)
43116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		memset(&progress, 0, sizeof(progress));
43216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	else
43316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		progress_init(&progress, _("Writing inode tables: "),
43416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			      fs->group_desc_count);
4351dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o
4363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	for (i = 0; i < fs->group_desc_count; i++) {
43716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		progress_update(&progress, i);
4383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
43919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		blk = fs->group_desc[i].bg_inode_table;
44019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		num = fs->inode_blocks_per_group;
44116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
44216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		retval = zero_blocks(fs, blk, num, 0, &blk, &num);
44316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if (retval) {
44416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			printf(_("\nCould not write %d blocks "
44516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				 "in inode table starting at %d: %s\n"),
44616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			       num, blk, error_message(retval));
44716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			exit(1);
44819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		}
4497d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o		if (sync_kludge) {
4507d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o			if (sync_kludge == 1)
4517d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o				sync();
4527d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o			else if ((i % sync_kludge) == 0)
4537d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o				sync();
4547d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o		}
4553839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
45616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	zero_blocks(0, 0, 0, 0, 0, 0);
45716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	progress_close(&progress);
4583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
4593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void create_root_dir(ext2_filsys fs)
4613839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
4623839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t	retval;
463f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	struct ext2_inode	inode;
4643839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
4663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
467d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		com_err("ext2fs_mkdir", retval, _("while creating root dir"));
4683839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
4693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
470f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	if (geteuid()) {
471f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
472f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		if (retval) {
473f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			com_err("ext2fs_read_inode", retval,
474d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("while reading root inode"));
475f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			exit(1);
476f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		}
47719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		inode.i_uid = getuid();
47819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		if (inode.i_uid)
47919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			inode.i_gid = getgid();
480f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
481f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		if (retval) {
482f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			com_err("ext2fs_write_inode", retval,
483d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("while setting root inode ownership"));
484f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			exit(1);
485f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		}
486f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	}
4873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
4883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void create_lost_and_found(ext2_filsys fs)
4903839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
4913839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t		retval;
492dfcdc32f8d6623a35a9e66f503c535e4081b7266Theodore Ts'o	ext2_ino_t		ino;
4933839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	const char		*name = "lost+found";
4943839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	int			i;
49550787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	int			lpf_size = 0;
4963839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4973839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
4983839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
499d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		com_err("ext2fs_mkdir", retval,
500d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while creating /lost+found"));
5013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
5023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
5033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
5043839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
5053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
506d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		com_err("ext2_lookup", retval,
507d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while looking up /lost+found"));
5083839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
5093839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
5103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
5113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
51250787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		if ((lpf_size += fs->blocksize) >= 16*1024)
51350787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			break;
5143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		retval = ext2fs_expand_dir(fs, ino);
5153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (retval) {
5163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			com_err("ext2fs_expand_dir", retval,
517d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("while expanding /lost+found"));
5183839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			exit(1);
5193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
5203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
5213839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
5223839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
5233839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
5243839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
5253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t	retval;
5263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
527f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
5283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	fs->group_desc[0].bg_free_inodes_count--;
5293839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	fs->super->s_free_inodes_count--;
5303839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_update_bb_inode(fs, bb_list);
5313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
5323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		com_err("ext2fs_update_bb_inode", retval,
533d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while setting bad block inode"));
5343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
5353839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
5363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
5373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
5383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
5393839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void reserve_inodes(ext2_filsys fs)
5403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
541dfcdc32f8d6623a35a9e66f503c535e4081b7266Theodore Ts'o	ext2_ino_t	i;
542dfcdc32f8d6623a35a9e66f503c535e4081b7266Theodore Ts'o	int		group;
5433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
5447f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o	for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
545f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		ext2fs_mark_inode_bitmap(fs->inode_map, i);
5463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		group = ext2fs_group_of_ino(fs, i);
5473839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		fs->group_desc[group].bg_free_inodes_count--;
5483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		fs->super->s_free_inodes_count--;
5493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
5503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	ext2fs_mark_ib_dirty(fs);
5513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
5523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
553e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'ostatic void zap_sector(ext2_filsys fs, int sect)
554f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o{
555f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	char buf[512];
556f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	int retval;
557f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
558f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	memset(buf, 0, 512);
559f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
560e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	io_channel_set_blksize(fs->io, 512);
561e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	retval = io_channel_write_blk(fs->io, sect, -512, buf);
562e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	io_channel_set_blksize(fs->io, fs->blocksize);
563f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	if (retval)
564e294cf2f6409aeede04316b1268f3e78a83509d0Theodore Ts'o		printf(_("Warning: could not erase sector %d: %s\n"), sect,
565f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		       error_message(retval));
566f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o}
56716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
56816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ostatic void create_journal_dev(ext2_filsys fs)
56916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o{
57016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	struct progress_struct progress;
57116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	errcode_t		retval;
57216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	char			*buf;
573dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	blk_t			blk;
574dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	int			count;
57516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
57616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (quiet)
57716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		memset(&progress, 0, sizeof(progress));
57816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	else
57916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		progress_init(&progress, _("Zeroing journal device: "),
58016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			      fs->super->s_blocks_count);
58116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
58216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
58316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			     &progress, &blk, &count);
58416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (retval) {
58516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		com_err("create_journal_dev", retval,
58616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			"while zeroing journal device (block %u, count %d",
58716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			blk, count);
58816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		exit(1);
58916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
590dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	zero_blocks(0, 0, 0, 0, 0, 0);
591dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o
59216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	retval = ext2fs_create_journal_superblock(fs,
59316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				  fs->super->s_blocks_count, 0, &buf);
59416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (retval) {
59516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		com_err("create_journal_dev", retval,
59616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			_("while initialization journal superblock"));
59716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		exit(1);
59816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
599dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	retval = io_channel_write_blk(fs->io,
600dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o				      fs->super->s_first_data_block+1,
601dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o				      1, buf);
60216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (retval) {
60316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		com_err("create_journal_dev", retval,
60416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			_("while writing journal superblock"));
60516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		exit(1);
60616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
60716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	progress_close(&progress);
60816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o}
609f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
6103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void show_stats(ext2_filsys fs)
6113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
612ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o	struct ext2_super_block *s = fs->super;
6131e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	char 			buf[80];
6143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	blk_t			group_block;
6151dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	int			i, need, col_left;
6163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
6173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (param.s_blocks_count != s->s_blocks_count)
618d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		printf(_("warning: %d blocks unused.\n\n"),
6193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		       param.s_blocks_count - s->s_blocks_count);
62050787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o
62150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	memset(buf, 0, sizeof(buf));
62250787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
623d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("Filesystem label=%s\n"), buf);
624d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("OS type: "));
6251e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	switch (fs->super->s_creator_os) {
6261e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	    case EXT2_OS_LINUX: printf ("Linux"); break;
627ad6783df0c9d06e5c298167de34aec8e6e25e6c2Theodore Ts'o	    case EXT2_OS_HURD:  printf ("GNU/Hurd");   break;
6281e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	    case EXT2_OS_MASIX: printf ("Masix"); break;
629d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	    default:		printf (_("(unknown os)"));
6301e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o        }
63150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	printf("\n");
632d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
63350787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		s->s_log_block_size);
634d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
63550787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		s->s_log_frag_size);
636d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
6373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	       s->s_blocks_count);
638d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
6393839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		s->s_r_blocks_count,
6403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	       100.0 * s->s_r_blocks_count / s->s_blocks_count);
641d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("First data block=%u\n"), s->s_first_data_block);
642d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	if (fs->group_desc_count > 1)
643c8c071a07319939dfd7ae2ff1dedec644d750debTheodore Ts'o		printf(_("%u block groups\n"), fs->group_desc_count);
644d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	else
645c8c071a07319939dfd7ae2ff1dedec644d750debTheodore Ts'o		printf(_("%u block group\n"), fs->group_desc_count);
646d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("%u blocks per group, %u fragments per group\n"),
6473839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	       s->s_blocks_per_group, s->s_frags_per_group);
648d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("%u inodes per group\n"), s->s_inodes_per_group);
6493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
6503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (fs->group_desc_count == 1) {
6513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		printf("\n");
6523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		return;
6533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
6543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
655d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("Superblock backups stored on blocks: "));
6563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	group_block = s->s_first_data_block;
6573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	col_left = 0;
6583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	for (i = 1; i < fs->group_desc_count; i++) {
6593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		group_block += s->s_blocks_per_group;
660521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o		if (!ext2fs_bg_has_super(fs, i))
661521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o			continue;
6627671433a9daad089af037f84b365d640e1adf252Theodore Ts'o		if (i != 1)
6637671433a9daad089af037f84b365d640e1adf252Theodore Ts'o			printf(", ");
6646733c2fd0046c525203034f58fc0a8c69fdf480bTheodore Ts'o		need = int_log10(group_block) + 2;
6651dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o		if (need > col_left) {
6663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			printf("\n\t");
6671dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o			col_left = 72;
6683839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
6691dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o		col_left -= need;
670a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		printf("%u", group_block);
6713839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
6723839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	printf("\n\n");
6733839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
6743839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
6751e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o/*
6761e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o * Set the S_CREATOR_OS field.  Return true if OS is known,
6771e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o * otherwise, 0.
6781e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o */
6791e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'ostatic int set_os(struct ext2_super_block *sb, char *os)
6801e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o{
6811e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (isdigit (*os))
6821e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		sb->s_creator_os = atoi (os);
6831e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	else if (strcasecmp(os, "linux") == 0)
6841e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		sb->s_creator_os = EXT2_OS_LINUX;
6851e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
6861e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		sb->s_creator_os = EXT2_OS_HURD;
6871e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	else if (strcasecmp(os, "masix") == 0)
6881e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		sb->s_creator_os = EXT2_OS_MASIX;
6891e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	else
6901e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		return 0;
6911e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	return 1;
6921e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o}
6931e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
694a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#define PATH_SET "PATH=/sbin"
695a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o
696d163b0948731e6d84bd3efe75075a2d0a8009272Theodore Ts'ostatic void parse_raid_opts(const char *opts)
697a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o{
698a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	char	*buf, *token, *next, *p, *arg;
699a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	int	len;
700a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	int	raid_usage = 0;
701a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
702a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	len = strlen(opts);
703a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	buf = malloc(len+1);
704a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	if (!buf) {
705d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		fprintf(stderr, _("Couldn't allocate memory to parse "
706d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			"raid options!\n"));
707a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		exit(1);
708a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	}
709a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	strcpy(buf, opts);
710a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	for (token = buf; token && *token; token = next) {
711a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		p = strchr(token, ',');
712a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		next = 0;
713a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		if (p) {
714a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			*p = 0;
715a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			next = p+1;
716a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		}
717a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		arg = strchr(token, '=');
718a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		if (arg) {
719a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			*arg = 0;
720a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			arg++;
721a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		}
722a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		if (strcmp(token, "stride") == 0) {
723a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			if (!arg) {
724a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o				raid_usage++;
725a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o				continue;
726a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			}
727a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			fs_stride = strtoul(arg, &p, 0);
728a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			if (*p || (fs_stride == 0)) {
729d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				fprintf(stderr,
730d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o					_("Invalid stride parameter.\n"));
731a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o				raid_usage++;
732a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o				continue;
733a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			}
734a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		} else
735a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			raid_usage++;
736a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	}
737a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	if (raid_usage) {
738d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		fprintf(stderr, _("\nBad raid options specified.\n\n"
739a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			"Raid options are separated by commas, "
740a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			"and may take an argument which\n"
741a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			"\tis set off by an equals ('=') sign.\n\n"
742a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			"Valid raid options are:\n"
743d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			"\tstride=<stride length in blocks>\n\n"));
744a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		exit(1);
745a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	}
746a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o}
747a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
748896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'ostatic __u32 ok_features[3] = {
749dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	EXT3_FEATURE_COMPAT_HAS_JOURNAL,	/* Compat */
75016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	EXT2_FEATURE_INCOMPAT_FILETYPE|		/* Incompat */
75116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
752896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o	EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER	/* R/O compat */
753896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o};
754a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
755a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
7563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void PRS(int argc, char *argv[])
7573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
7584a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		c;
7594a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		size;
7604a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	char *		tmp;
76180c22c90145bc495502210ac4502cca8228f997bTheodore Ts'o	blk_t		group_blk_max = 8192;
7624a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		blocksize = 0;
7634a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		inode_ratio = 0;
7644a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		reserved_ratio = 5;
765dfcdc32f8d6623a35a9e66f503c535e4081b7266Theodore Ts'o	ext2_ino_t	num_inodes = 0;
766a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	errcode_t	retval;
767f6f6583239203d0b017285f5f7acdd21ec7afc05Theodore Ts'o	int		sparse_option = 1;
7684a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	char *		oldpath = getenv("PATH");
7694a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	char *		raid_opts = 0;
7704a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	char *		fs_type = 0;
771f6f6583239203d0b017285f5f7acdd21ec7afc05Theodore Ts'o	const char *	feature_set = "filetype";
7724a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	blk_t		dev_size;
7732740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o#ifdef linux
7744a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	struct 		utsname ut;
7752740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o
7762740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o	if (uname(&ut)) {
7772740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o		perror("uname");
7782740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o		exit(1);
7792740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o	}
780896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o	if ((ut.release[0] == '1') ||
781896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o	    (ut.release[0] == '2' && ut.release[1] == '.' &&
782896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o	     ut.release[2] < '2' && ut.release[3] == '.'))
78380c22c90145bc495502210ac4502cca8228f997bTheodore Ts'o		feature_set = NULL;
7842740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o#endif
7853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/* Update our PATH to include /sbin  */
786a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	if (oldpath) {
787a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		char *newpath;
788a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o
789a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
790a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		strcpy (newpath, PATH_SET);
791a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		strcat (newpath, ":");
792a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		strcat (newpath, oldpath);
793a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		putenv (newpath);
794a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	} else
795a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		putenv (PATH_SET);
7963839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
79716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	tmp = getenv("MKE2FS_SYNC");
79816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (tmp)
79916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		sync_kludge = atoi(tmp);
80016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
8013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	setbuf(stdout, NULL);
8023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	setbuf(stderr, NULL);
8033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	initialize_ext2_error_table();
8043839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	memset(&param, 0, sizeof(struct ext2_super_block));
80550787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	param.s_rev_level = 1;  /* Create revision 1 filesystems now */
8063839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
807d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
8083839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		 E2FSPROGS_VERSION, E2FSPROGS_DATE,
8093839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		 EXT2FS_VERSION, EXT2FS_DATE);
8103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (argc && *argv)
8113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		program_name = *argv;
8121e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	while ((c = getopt (argc, argv,
813dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o		    "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF)
8143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		switch (c) {
8153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'b':
81650787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			blocksize = strtoul(optarg, &tmp, 0);
81750787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			if (blocksize < 1024 || blocksize > 4096 || *tmp) {
818d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				com_err(program_name, 0,
819d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o					_("bad block size - %s"), optarg);
8203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
8213839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
8223839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			param.s_log_block_size =
8236733c2fd0046c525203034f58fc0a8c69fdf480bTheodore Ts'o				int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
82480c22c90145bc495502210ac4502cca8228f997bTheodore Ts'o			group_blk_max = blocksize * 8;
8253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
8263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'c':
8273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 't':	/* Check for bad blocks */
8283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			cflag = 1;
8293839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
8303839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'f':
8313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			size = strtoul(optarg, &tmp, 0);
8323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			if (size < 1024 || size > 4096 || *tmp) {
833d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				com_err(program_name, 0,
834d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o					_("bad fragment size - %s"),
8353839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					optarg);
8363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
8373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
8383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			param.s_log_frag_size =
8396733c2fd0046c525203034f58fc0a8c69fdf480bTheodore Ts'o				int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
840d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			printf(_("Warning: fragments not supported.  "
841d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			       "Ignoring -f option\n"));
8423839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
8433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'g':
8443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
845f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			if (*tmp) {
846f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				com_err(program_name, 0,
847d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o					_("Illegal number for blocks per group"));
848f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				exit(1);
849f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			}
850f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			if ((param.s_blocks_per_group % 8) != 0) {
851f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				com_err(program_name, 0,
852d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("blocks per group must be multiple of 8"));
8533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
8543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
8553839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
8563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'i':
8573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			inode_ratio = strtoul(optarg, &tmp, 0);
85844c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o			if (inode_ratio < 1024 || inode_ratio > 4096 * 1024 ||
8593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			    *tmp) {
860d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				com_err(program_name, 0,
861d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o					_("bad inode ratio - %s"), optarg);
8623839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
8633839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
8643839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
865dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o		case 'J':
866dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o			parse_journal_opts(optarg);
867dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o			break;
86885ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o		case 'j':
869dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o			if (!journal_size)
870dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o				journal_size = -1;
8718ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o			break;
8723839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'l':
873f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			bad_blocks_filename = malloc(strlen(optarg)+1);
874f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			if (!bad_blocks_filename) {
875f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				com_err(program_name, ENOMEM,
876d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o					_("in malloc for bad_blocks_filename"));
877f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				exit(1);
878f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			}
879f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			strcpy(bad_blocks_filename, optarg);
8803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
8813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'm':
8823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			reserved_ratio = strtoul(optarg, &tmp, 0);
8833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			if (reserved_ratio > 50 || *tmp) {
8843839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				com_err(program_name, 0,
885d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o					_("bad reserved blocks percent - %s"),
8863839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					optarg);
8873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
8883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
8893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
89050787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		case 'n':
89150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			noaction++;
89250787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			break;
8931e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		case 'o':
8941e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			creator_os = optarg;
8951e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			break;
8967f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o		case 'r':
8977f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o			param.s_rev_level = atoi(optarg);
8987f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o			break;
899521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o		case 's':
900521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o			sparse_option = atoi(optarg);
901521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o			break;
9027f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o#ifdef EXT2_DYNAMIC_REV
9037f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o		case 'I':
9047f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o			param.s_inode_size = atoi(optarg);
9057f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o			break;
9067f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o#endif
9075515e6b47de9ff941164a546dd9a74e9c6da3367Theodore Ts'o		case 'N':
9085515e6b47de9ff941164a546dd9a74e9c6da3367Theodore Ts'o			num_inodes = atoi(optarg);
9095515e6b47de9ff941164a546dd9a74e9c6da3367Theodore Ts'o			break;
9103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'v':
9113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			verbose = 1;
9123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
9133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'q':
9143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			quiet = 1;
9153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
91674becf3c0a065f8d64e07ce4d31f9fe53be91d62Theodore Ts'o		case 'F':
91774becf3c0a065f8d64e07ce4d31f9fe53be91d62Theodore Ts'o			force = 1;
91874becf3c0a065f8d64e07ce4d31f9fe53be91d62Theodore Ts'o			break;
9191e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		case 'L':
9201e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			volume_label = optarg;
9211e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			break;
9221e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		case 'M':
9231e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			mount_dir = optarg;
9241e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			break;
925896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o		case 'O':
926896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o			feature_set = optarg;
927896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o			break;
928a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		case 'R':
929a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			raid_opts = optarg;
930a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			break;
931f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		case 'S':
932f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			super_only = 1;
933f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			break;
93450787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		case 'T':
93550787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			fs_type = optarg;
93650787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			break;
937818180cdfcff84b9048ecdc5dc86323f0fefba24Theodore Ts'o		case 'V':
938818180cdfcff84b9048ecdc5dc86323f0fefba24Theodore Ts'o			/* Print version number and exit */
939d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			fprintf(stderr, _("\tUsing %s\n"),
940818180cdfcff84b9048ecdc5dc86323f0fefba24Theodore Ts'o				error_message(EXT2_ET_BASE));
941818180cdfcff84b9048ecdc5dc86323f0fefba24Theodore Ts'o			exit(0);
9423839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		default:
9433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			usage();
9443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
9453839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (optind == argc)
9463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		usage();
9473839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	device_name = argv[optind];
9483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	optind++;
9493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (optind < argc) {
950e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o		unsigned long tmp2  = strtoul(argv[optind++], &tmp, 0);
951e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o
952e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o		if ((*tmp) || (tmp2 > 0xfffffffful)) {
953d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			com_err(program_name, 0, _("bad blocks count - %s"),
9543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				argv[optind - 1]);
9553839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			exit(1);
9563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
957e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o		param.s_blocks_count = tmp2;
9583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
9593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (optind < argc)
9603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		usage();
961a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o
962a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	if (raid_opts)
963a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		parse_raid_opts(raid_opts);
964a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
965dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	/* Parse the user-supplied feature_set, if any. */
966dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	if (feature_set && !strncasecmp(feature_set, "none", 4))
967dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o		feature_set = NULL;
968dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	if (feature_set && e2p_edit_feature(feature_set,
969dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o					    &param.s_feature_compat,
970dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o					    ok_features)) {
971dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o		fprintf(stderr, _("Invalid filesystem option set: %s\n"),
972dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o			feature_set);
973dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o		exit(1);
974dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	}
975dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o
97674becf3c0a065f8d64e07ce4d31f9fe53be91d62Theodore Ts'o	if (!force)
9778ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o		check_plausibility(device_name);
97863985320384bf143eaac9857af424800d9867a1aTheodore Ts'o	check_mount(device_name, force, _("filesystem"));
979a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o
9803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	param.s_log_frag_size = param.s_log_block_size;
9813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
98250787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	if (noaction && param.s_blocks_count) {
98350787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		dev_size = param.s_blocks_count;
98450787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		retval = 0;
98550787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	} else
98650787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		retval = ext2fs_get_device_size(device_name,
98750787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o						EXT2_BLOCK_SIZE(&param),
98850787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o						&dev_size);
989a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o	if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
990a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o		com_err(program_name, retval,
991d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while trying to determine filesystem size"));
992a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o		exit(1);
993a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o	}
994a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	if (!param.s_blocks_count) {
995a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o		if (retval == EXT2_ET_UNIMPLEMENTED) {
996a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o			com_err(program_name, 0,
997d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("Couldn't determine device size; you "
998a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o				"must specify\nthe size of the "
999d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				"filesystem\n"));
1000a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o			exit(1);
100126ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o		} else {
100226ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o			if (dev_size == 0) {
100326ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				com_err(program_name, 0,
100426ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				_("Device size reported to be zero.  "
100526ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "Invalid partition specified, or\n\t"
100626ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "partition table wasn't reread "
100726ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "after running fdisk, due to\n\t"
100826ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "a modified partition being busy "
100926ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "and in use.  You may need to reboot\n\t"
101026ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "to re-read your partition table.\n"
101126ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  ));
101226ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				exit(1);
101326ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o			}
1014a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o			param.s_blocks_count = dev_size;
101526ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o		}
101626ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o
1017a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o	} else if (!force && (param.s_blocks_count > dev_size)) {
1018a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o		com_err(program_name, 0,
1019d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("Filesystem larger than apparent filesystem size."));
1020a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o		proceed_question();
1021a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	}
10223839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1023dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	/*
1024dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	 * If the user asked for HAS_JOURNAL, then make sure a journal
1025dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	 * gets created.
1026dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	 */
1027dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1028dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	    !journal_size)
1029dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o		journal_size = -1;
1030dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	if (!fs_type &&
1031dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	    (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
1032dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o		fs_type = "journal";
1033dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o	set_fs_defaults(fs_type, &param, blocksize, &inode_ratio);
103450787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o
1035521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o	if (param.s_blocks_per_group) {
1036521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o		if (param.s_blocks_per_group < 256 ||
103780c22c90145bc495502210ac4502cca8228f997bTheodore Ts'o		    param.s_blocks_per_group > group_blk_max || *tmp) {
1038521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o			com_err(program_name, 0,
1039d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("blocks per group count out of range"));
1040521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o			exit(1);
1041521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o		}
1042521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o	}
1043521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o
10443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
10453839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * Calculate number of inodes based on the inode ratio
10463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
10475515e6b47de9ff941164a546dd9a74e9c6da3367Theodore Ts'o	param.s_inodes_count = num_inodes ? num_inodes :
1048e6597048d9d1637c9f997363a7f45c74b98fd0e2Theodore Ts'o		((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
1049f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			/ inode_ratio;
10503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
10513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
10523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * Calculate number of blocks to reserve
10533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
10543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1055521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o
1056f6f6583239203d0b017285f5f7acdd21ec7afc05Theodore Ts'o	/* Turn off features not supported by the earlier filesystem version */
1057f6f6583239203d0b017285f5f7acdd21ec7afc05Theodore Ts'o	if (param.s_rev_level == 0) {
1058f6f6583239203d0b017285f5f7acdd21ec7afc05Theodore Ts'o		sparse_option = 0;
1059f6f6583239203d0b017285f5f7acdd21ec7afc05Theodore Ts'o		feature_set = NULL;
1060f6f6583239203d0b017285f5f7acdd21ec7afc05Theodore Ts'o	}
10612740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o	if (sparse_option)
1062dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o		param.s_feature_ro_compat |=
1063521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o			EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1064f6f6583239203d0b017285f5f7acdd21ec7afc05Theodore Ts'o
10653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
10663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
10673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oint main (int argc, char *argv[])
10683839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
10693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t	retval = 0;
10703839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	ext2_filsys	fs;
10713839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	badblocks_list	bb_list = 0;
107285ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o	int		journal_blocks;
107344c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	int		i, val;
1074d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o
1075d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#ifdef ENABLE_NLS
1076d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	setlocale(LC_MESSAGES, "");
1077d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1078d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	textdomain(NLS_CAT_NAME);
1079d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#endif
10803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	PRS(argc, argv);
10813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
10823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
10833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * Initialize the superblock....
10843839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
10853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_initialize(device_name, 0, &param,
10863839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				   unix_io_manager, &fs);
10873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
1088d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		com_err(device_name, retval, _("while setting up superblock"));
10893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
10903839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
10913839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
10921e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	/*
1093e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	 * Wipe out the old on-disk superblock
1094e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	 */
1095e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	zap_sector(fs, 2);
1096e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o
1097e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	/*
10981e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 * Generate a UUID for it...
10991e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 */
1100ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o	uuid_generate(fs->super->s_uuid);
11011e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
11021e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	/*
110344c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	 * Add "jitter" to the superblock's check interval so that we
110444c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	 * don't check all the filesystems at the same time.  We use a
110544c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	 * kludgy hack of using the UUID to derive a random jitter value.
110644c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	 */
110744c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
110844c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o		val += fs->super->s_uuid[i];
110944c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
111044c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o
111144c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	/*
11121e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 * Override the creator OS, if applicable
11131e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 */
11141e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (creator_os && !set_os(fs->super, creator_os)) {
1115d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		com_err (program_name, 0, _("unknown os - %s"), creator_os);
11161e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		exit(1);
11171e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	}
11181e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
11191e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	/*
11204ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	 * For the Hurd, we will turn off filetype since it doesn't
11214ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	 * support it.
11224ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	 */
11234ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	if (fs->super->s_creator_os == EXT2_OS_HURD)
11244ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o		fs->super->s_feature_incompat &=
11254ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o			~EXT2_FEATURE_INCOMPAT_FILETYPE;
11264ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o
11274ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	/*
11281e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 * Set the volume label...
11291e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 */
11301e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (volume_label) {
1131ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		memset(fs->super->s_volume_name, 0,
1132ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		       sizeof(fs->super->s_volume_name));
1133ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		strncpy(fs->super->s_volume_name, volume_label,
1134ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o			sizeof(fs->super->s_volume_name));
11351e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	}
11361e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
11371e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	/*
11381e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 * Set the last mount directory
11391e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 */
11401e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (mount_dir) {
1141ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		memset(fs->super->s_last_mounted, 0,
1142ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		       sizeof(fs->super->s_last_mounted));
1143ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		strncpy(fs->super->s_last_mounted, mount_dir,
1144ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o			sizeof(fs->super->s_last_mounted));
11451e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	}
11461e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
114750787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	if (!quiet || noaction)
11483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		show_stats(fs);
11493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
115050787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	if (noaction)
115150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		exit(0);
115250787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o
115316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (fs->super->s_feature_incompat &
115416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
115516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		create_journal_dev(fs);
115616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		ext2fs_close(fs);
115716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		exit(0);
115816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
115916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
11603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (bad_blocks_filename)
11613839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		read_bb_file(fs, &bb_list, bad_blocks_filename);
11623839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (cflag)
11633839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		test_disk(fs, &bb_list);
11643839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
11653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	handle_bad_blocks(fs, bb_list);
1166a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	fs->stride = fs_stride;
116719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	retval = ext2fs_allocate_tables(fs);
116819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (retval) {
116919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		com_err(program_name, retval,
1170d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while trying to allocate filesystem tables"));
117119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		exit(1);
117219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	}
1173f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	if (super_only) {
1174f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		fs->super->s_state |= EXT2_ERROR_FS;
1175f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1176f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	} else {
117719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		write_inode_tables(fs);
1178f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		create_root_dir(fs);
1179f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		create_lost_and_found(fs);
1180f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		reserve_inodes(fs);
1181f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		create_bad_block_inode(fs, bb_list);
11821e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#ifdef ZAP_BOOTBLOCK
1183e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o		zap_sector(fs, 0);
11841e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#endif
1185f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	}
11868ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o
118716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (journal_device) {
118816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		ext2_filsys	jfs;
118916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
11908ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o		if (!force)
11918ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o			check_plausibility(journal_device);
119263985320384bf143eaac9857af424800d9867a1aTheodore Ts'o		check_mount(journal_device, force, _("journal"));
11938ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o
119416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
119516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				     EXT2_FLAG_JOURNAL_DEV_OK, 0,
119616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				     fs->blocksize, unix_io_manager, &jfs);
119716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if (retval) {
119816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			com_err(program_name, retval,
119916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				_("while trying to open journal device %s\n"),
120016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				journal_device);
120116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			exit(1);
120216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		}
120393345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		if (!quiet) {
12048ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o			printf(_("Creating journal on device %s: "),
12058ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o			       journal_device);
120693345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o			fflush(stdout);
120793345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		}
120816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		retval = ext2fs_add_journal_device(fs, jfs);
120916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if(retval) {
12108ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o			com_err (program_name, retval,
121116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				 _("while trying to add journal to device %s"),
12128ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o				 journal_device);
12138ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o			exit(1);
12148ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o		}
12158ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o		if (!quiet)
12168ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o			printf(_("done\n"));
121716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		ext2fs_close(jfs);
12188ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o	} else if (journal_size) {
12192537b6d0c1aa9710a05cdfafc281b4884c67bdb2Theodore Ts'o		journal_blocks = figure_journal_size(journal_size, fs);
122093345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o
122193345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		if (!journal_blocks) {
122293345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o			fs->super->s_feature_compat &=
122393345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o				~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
122493345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o			goto no_journal;
122593345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		}
122693345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		if (!quiet) {
122716ad33346dd4666edd9d46d417245432c87057efTheodore Ts'o			printf(_("Creating journal (%d blocks): "),
122816ad33346dd4666edd9d46d417245432c87057efTheodore Ts'o			       journal_blocks);
122993345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o			fflush(stdout);
123093345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		}
123163985320384bf143eaac9857af424800d9867a1aTheodore Ts'o		retval = ext2fs_add_journal_inode(fs, journal_blocks,
123263985320384bf143eaac9857af424800d9867a1aTheodore Ts'o						  journal_flags);
123385ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o		if (retval) {
123485ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o			com_err (program_name, retval,
123585ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o				 _("while trying to create journal"));
123685ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o			exit(1);
123785ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o		}
123885ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o		if (!quiet)
123985ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o			printf(_("done\n"));
124085ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o	}
124193345d1588606ccc558406202bff8f86360ab03fTheodore Ts'ono_journal:
124293345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o
12433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (!quiet)
1244d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		printf(_("Writing superblocks and "
1245d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		       "filesystem accounting information: "));
12465d45d80375943be8917302a502f3f28d2df8d16dTheodore Ts'o	retval = ext2fs_flush(fs);
12475d45d80375943be8917302a502f3f28d2df8d16dTheodore Ts'o	if (retval) {
1248d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		printf(_("\nWarning, had trouble writing out superblocks."));
12495d45d80375943be8917302a502f3f28d2df8d16dTheodore Ts'o	}
125093345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o	if (!quiet) {
12512537b6d0c1aa9710a05cdfafc281b4884c67bdb2Theodore Ts'o		printf(_("done\n\n"));
12522537b6d0c1aa9710a05cdfafc281b4884c67bdb2Theodore Ts'o		printf(_("Filesystem will be checked run "
12532537b6d0c1aa9710a05cdfafc281b4884c67bdb2Theodore Ts'o			 "every %d mounts or %g days.\n"),
125493345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		       fs->super->s_max_mnt_count,
125593345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		       (double)fs->super->s_checkinterval / (3600 * 24));
125693345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o	}
12575d45d80375943be8917302a502f3f28d2df8d16dTheodore Ts'o	ext2fs_close(fs);
12583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	return 0;
12593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
1260