mke2fs.c revision 3c6e91c5498771bc794fd2854153fe31c006df50
13839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
23839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * mke2fs.c - Make a ext2fs filesystem.
3efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o *
455f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
555f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o * 	2003, 2004, 2005 by Theodore Ts'o.
619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o *
719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %Begin-Header%
819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * This file may be redistributed under the terms of the GNU Public
919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * License.
1019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %End-Header%
113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/* Usage: mke2fs [options] device
14efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o *
153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * The device may be a block device or a image of one, but this isn't
16efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o * enforced (but it's not much fun on a character device :-).
173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
183839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
19ebabf2ad6d19af5c674b624bafe619dedbc94403Theodore Ts'o#define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
20ebabf2ad6d19af5c674b624bafe619dedbc94403Theodore Ts'o
21d1154eb460efe588eaed3d439c1caaca149fa362Theodore Ts'o#include "config.h"
22a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#include <stdio.h>
233839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <string.h>
24d0c537748d5a9799dd0658b0a5cceaafc28084bbTheodore Ts'o#include <strings.h>
253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <fcntl.h>
263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <ctype.h>
273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <time.h>
28756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o#ifdef __linux__
292740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o#include <sys/utsname.h>
302740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o#endif
31a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_GETOPT_H
323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <getopt.h>
33373b8337c7b6c6243810be250083fa4773891e92Theodore Ts'o#else
34373b8337c7b6c6243810be250083fa4773891e92Theodore Ts'oextern char *optarg;
35373b8337c7b6c6243810be250083fa4773891e92Theodore Ts'oextern int optind;
36a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
37a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_UNISTD_H
383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <unistd.h>
39a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
40a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_STDLIB_H
413839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <stdlib.h>
42a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
43a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_ERRNO_H
44a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#include <errno.h>
45a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <sys/ioctl.h>
47f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#include <sys/types.h>
4813b0b1231ed28aac75ba336de7a8cb3b4611ce68Eric Sandeen#include <sys/stat.h>
49b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V#include <libgen.h>
50365857912e27914afa8857af5adf74ee19ca9e03Theodore Ts'o#include <limits.h>
519ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen#include <blkid/blkid.h>
52f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
5354c637d4d29af3e6365779f8b12976abe95a4753Theodore Ts'o#include "ext2fs/ext2_fs.h"
5495fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson#include "ext2fs/ext2fsP.h"
553839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "et/com_err.h"
561e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#include "uuid/uuid.h"
57896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o#include "e2p/e2p.h"
583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "ext2fs/ext2fs.h"
5963985320384bf143eaac9857af424800d9867a1aTheodore Ts'o#include "util.h"
609dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o#include "profile.h"
61a6d8302b4873527798a77c1ba3106a04b71dfeacTheodore Ts'o#include "prof_err.h"
623839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "../version.h"
63d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#include "nls-enable.h"
641f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali#include "quota/mkquota.h"
653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#define STRIDE_LENGTH 8
673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
68493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
69493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o
706733c2fd0046c525203034f58fc0a8c69fdf480bTheodore Ts'o#ifndef __sparc__
711e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#define ZAP_BOOTBLOCK
721e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#endif
731e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
747d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner#define DISCARD_STEP_MB		(2048)
757d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner
763839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oextern int isatty(int);
77f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'oextern FILE *fpopen(const char *cmd, const char *mode);
783839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
79f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic const char * program_name = "mke2fs";
80f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic const char * device_name /* = NULL */;
813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/* Command line options */
83f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	cflag;
84f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	verbose;
85f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	quiet;
86f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	super_only;
87f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	discard = 1;	/* attempt to discard device before fs creation */
88f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	direct_io;
89f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	force;
90f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	noaction;
9165c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'ostatic int	num_backups = 2; /* number of backup bg's for sparse_super2 */
92f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic uid_t	root_uid;
93f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic gid_t	root_gid;
9416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	journal_size;
9516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'oint	journal_flags;
96f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	lazy_itable_init;
973c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'ostatic int	packed_meta_blocks;
98f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic char	*bad_blocks_filename = NULL;
99f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic __u32	fs_stride;
100f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int	quotatype = -1;  /* Initialize both user and group quotas by default */
10188ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'ostatic __u64	offset;
102b818205feb60f7a7bec5e325462294f586735215Theodore Ts'ostatic blk64_t journal_location = ~0LL;
103f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'o
104f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic struct ext2_super_block fs_param;
105f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic char *fs_uuid = NULL;
106f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic char *creator_os;
107f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic char *volume_label;
108f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic char *mount_dir;
10916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ochar *journal_device;
110f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int sync_kludge;	/* Set using the MKE2FS_SYNC env. option */
111f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic char **fs_types;
1123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
113f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic profile_t	profile;
1149dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
115f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int sys_page_size = 4096;
116f404167dda29a59d2be2882328aeb074b9899669Theodore Ts'ostatic int linux_version_code = 0;
11731e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o
11863985320384bf143eaac9857af424800d9867a1aTheodore Ts'ostatic void usage(void)
1193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
120bdd80f28d759cda94bab13af689d4aee0328dd7fTheodore Ts'o	fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
12128e2cb9e72cce9e97a4a6f738d28b0f3f31efe69Eric Sandeen	"[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
122bdd80f28d759cda94bab13af689d4aee0328dd7fTheodore Ts'o	"[-J journal-options]\n"
1238dbcbe1c4a4bdd30c6e9aa1a3df9b06f4f3cbee1Yongqiang Yang	"\t[-G flex-group-size] [-N number-of-inodes]\n"
1249ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o	"\t[-m reserved-blocks-percentage] [-o creator-os]\n"
1259ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o	"\t[-g blocks-per-group] [-L volume-label] "
126067911ae734bb5fef7c5780a639533847b5b578cAndreas Dilger	"[-M last-mounted-directory]\n\t[-O feature[,...]] "
127bdd80f28d759cda94bab13af689d4aee0328dd7fTheodore Ts'o	"[-r fs-revision] [-E extended-option[,...]]\n"
12865794cf159aa051ea3fe79db1950f562600b252eMike Frysinger	"\t[-t fs-type] [-T usage-type ] [-U UUID] "
12937c8db7b2078d0310e5676404e21cc143d8e4d56Theodore Ts'o	"[-jnqvDFKSV] device [blocks-count]\n"),
1303839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		program_name);
1313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	exit(1);
1323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
1333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
13402d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santosstatic int int_log2(unsigned long long arg)
1353839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
1363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	int	l = 0;
1373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	arg >>= 1;
1393839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	while (arg) {
1403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		l++;
1413839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		arg >>= 1;
1423839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
1433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	return l;
1443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
1453839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
14602d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santosstatic int int_log10(unsigned long long arg)
1471dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o{
1481dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	int	l;
1491dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o
1501dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	for (l=0; arg ; l++)
1511dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o		arg = arg / 10;
1521dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o	return l;
1531dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o}
1541dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o
1551d6fd6d0c3766a8165e69284c75812574a29c804Andreas Dilger#ifdef __linux__
156d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'ostatic int parse_version_number(const char *s)
157d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o{
158d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	int	major, minor, rev;
159d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	char	*endptr;
160d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	const char *cp = s;
161d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o
162d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	if (!s)
163d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o		return 0;
164d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	major = strtol(cp, &endptr, 10);
165d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	if (cp == endptr || *endptr != '.')
166d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o		return 0;
167d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	cp = endptr + 1;
168d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	minor = strtol(cp, &endptr, 10);
169d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	if (cp == endptr || *endptr != '.')
170d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o		return 0;
171d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	cp = endptr + 1;
172d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	rev = strtol(cp, &endptr, 10);
173d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	if (cp == endptr)
174d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o		return 0;
175d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	return ((((major * 256) + minor) * 256) + rev);
176d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o}
1771d6fd6d0c3766a8165e69284c75812574a29c804Andreas Dilger#endif
178d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o
17950787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o/*
1803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * Helper function for read_bb_file and test_disk
1813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
182544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'ostatic void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
1833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
1846693837e59cc7b5397a0d46d2753c309382c76f9Theodore Ts'o	fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
1853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	return;
1863839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
1873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
1893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * Reads the bad blocks list from a file
1903839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
1913839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
1923839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			 const char *bad_blocks_file)
1933839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
1943839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	FILE		*f;
1953839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t	retval;
1963839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1973839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	f = fopen(bad_blocks_file, "r");
1983839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (!f) {
1993839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		com_err("read_bad_blocks_file", errno,
200d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while trying to open %s"), bad_blocks_file);
2013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
2023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
2043839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	fclose (f);
2053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
20645ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err("ext2fs_read_bb_FILE", retval, "%s",
207d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while reading in list of bad blocks from file"));
2083839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
2093839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
2133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * Runs the badblocks program to test the disk
2143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
2153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void test_disk(ext2_filsys fs, badblocks_list *bb_list)
2163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
2173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	FILE		*f;
2183839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t	retval;
2193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	char		buf[1024];
2203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2214efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson	sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
2223ed57c27df0ba0942a19c71bc065c8eec3036567Theodore Ts'o		quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
2234efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson		fs->device_name, ext2fs_blocks_count(fs->super)-1);
2243839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (verbose)
225d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		printf(_("Running command: %s\n"), buf);
2263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	f = popen(buf, "r");
2273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (!f) {
2283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		com_err("popen", errno,
229f37ab68a26bacf4f5cc7643b8373e40292b7682aTheodore Ts'o			_("while trying to run '%s'"), buf);
2303839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
2313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
233f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	pclose(f);
2343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
23545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err("ext2fs_read_bb_FILE", retval, "%s",
236d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while processing list of bad blocks from program"));
2373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
2383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2393839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2413839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
2423839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
243544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o	dgrp_t			i;
244544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o	blk_t			j;
245544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o	unsigned 		must_be_good;
2463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	blk_t			blk;
2473839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	badblocks_iterate	bb_iter;
2483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t		retval;
249f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	blk_t			group_block;
250f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	int			group;
251f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	int			group_bad;
2523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (!bb_list)
2543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		return;
255efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
2563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
2573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * The primary superblock and group descriptors *must* be
2583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * good; if not, abort.
2593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
2603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
2613839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
262cbbf031b6edf9bdf5511af2193e44cff7fdaa66aTheodore Ts'o		if (ext2fs_badblocks_list_test(bb_list, i)) {
263d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			fprintf(stderr, _("Block %d in primary "
264d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				"superblock/group descriptor area bad.\n"), i);
265d0ff90d5202428583c78a60c3042e7b60d88bc45Eric Sandeen			fprintf(stderr, _("Blocks %u through %u must be good "
266d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				"in order to build a filesystem.\n"),
2673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				fs->super->s_first_data_block, must_be_good);
268544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o			fputs(_("Aborting....\n"), stderr);
2693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			exit(1);
2703839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
2713839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
272f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
273f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	/*
274f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	 * See if any of the bad blocks are showing up in the backup
275f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	 * superblocks and/or group descriptors.  If so, issue a
276f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	 * warning and adjust the block counts appropriately.
277f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	 */
278f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	group_block = fs->super->s_first_data_block +
279f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		fs->super->s_blocks_per_group;
280efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
281f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	for (i = 1; i < fs->group_desc_count; i++) {
28292bcc595dcb31ad15e12d8c72e6edfc70545c204Theodore Ts'o		group_bad = 0;
283f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		for (j=0; j < fs->desc_blocks+1; j++) {
284cbbf031b6edf9bdf5511af2193e44cff7fdaa66aTheodore Ts'o			if (ext2fs_badblocks_list_test(bb_list,
285cbbf031b6edf9bdf5511af2193e44cff7fdaa66aTheodore Ts'o						       group_block + j)) {
286efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o				if (!group_bad)
287f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o					fprintf(stderr,
2888deb80a5d1078cbe43eaffcdeebf0a1a549d6a54Takashi Sato_("Warning: the backup superblock/group descriptors at block %u contain\n"
289d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o"	bad blocks.\n\n"),
290f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o						group_block);
291f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				group_bad++;
2926493f8e85defc568a4ca8cdb4a53361f36fb94baTheodore Ts'o				group = ext2fs_group_of_blk2(fs, group_block+j);
293d7cca6b06f366f998ed43346f9b6fca9e163692fValerie Aurora Henson				ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
2945711ed297b1a3d94086256b5b3b891d4f77b21caTheodore Ts'o				ext2fs_group_desc_csum_set(fs, group);
2954efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson				ext2fs_free_blocks_count_add(fs->super, 1);
296f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			}
297f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		}
298f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		group_block += fs->super->s_blocks_per_group;
299f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	}
300efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
3013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
3023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * Mark all the bad blocks as used...
3033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
304cbbf031b6edf9bdf5511af2193e44cff7fdaa66aTheodore Ts'o	retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
3053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
30645ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
307d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while marking bad blocks as used"));
3083839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
3093839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
310efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
311c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o		ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
312cbbf031b6edf9bdf5511af2193e44cff7fdaa66aTheodore Ts'o	ext2fs_badblocks_list_iterate_end(bb_iter);
3133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
3143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
3153c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'ostatic errcode_t packed_allocate_tables(ext2_filsys fs)
3163c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o{
3173c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	errcode_t	retval;
3183c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	dgrp_t		i;
3193c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	blk64_t		goal = 0;
3203c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o
3213c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	for (i = 0; i < fs->group_desc_count; i++) {
3223c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		retval = ext2fs_new_block2(fs, goal, NULL, &goal);
3233c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		if (retval)
3243c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o			return retval;
3253c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		ext2fs_block_alloc_stats2(fs, goal, +1);
3263c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		ext2fs_block_bitmap_loc_set(fs, i, goal);
3273c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	}
3283c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	for (i = 0; i < fs->group_desc_count; i++) {
3293c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		retval = ext2fs_new_block2(fs, goal, NULL, &goal);
3303c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		if (retval)
3313c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o			return retval;
3323c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		ext2fs_block_alloc_stats2(fs, goal, +1);
3333c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		ext2fs_inode_bitmap_loc_set(fs, i, goal);
3343c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	}
3353c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	for (i = 0; i < fs->group_desc_count; i++) {
3363c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		blk64_t end = ext2fs_blocks_count(fs->super) - 1;
3373c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		retval = ext2fs_get_free_blocks2(fs, goal, end,
3383c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o						 fs->inode_blocks_per_group,
3393c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o						 fs->block_map, &goal);
3403c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		if (retval)
3413c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o			return retval;
3423c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		ext2fs_block_alloc_stats_range(fs, goal,
3433c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o					       fs->inode_blocks_per_group, +1);
3443c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		ext2fs_inode_table_loc_set(fs, i, goal);
3453c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	}
3463c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	return 0;
3473c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o}
3483c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o
3496fcd6f84c235f4bf2bd9770f172837da9982eb6eEric Sandeenstatic void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
35016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o{
35116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	errcode_t	retval;
35202d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos	blk64_t		blk;
353544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o	dgrp_t		i;
354e16324210286dc72a013e66d91bd3a59c3ab7538Theodore Ts'o	int		num;
35595fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	struct ext2fs_numeric_progress_struct progress;
35616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
35795fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	ext2fs_numeric_progress_init(fs, &progress,
35895fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson				     _("Writing inode tables: "),
35995fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson				     fs->group_desc_count);
3601dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o
3613839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	for (i = 0; i < fs->group_desc_count; i++) {
362c498cb11d3e72e41af760bd882675f44db8b77e7Theodore Ts'o		ext2fs_numeric_progress_update(fs, &progress, i);
363efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
364d7cca6b06f366f998ed43346f9b6fca9e163692fValerie Aurora Henson		blk = ext2fs_inode_table_loc(fs, i);
36519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		num = fs->inode_blocks_per_group;
36616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
367e16324210286dc72a013e66d91bd3a59c3ab7538Theodore Ts'o		if (lazy_flag)
368e16324210286dc72a013e66d91bd3a59c3ab7538Theodore Ts'o			num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
369e16324210286dc72a013e66d91bd3a59c3ab7538Theodore Ts'o					       ext2fs_bg_itable_unused(fs, i)) *
370e16324210286dc72a013e66d91bd3a59c3ab7538Theodore Ts'o					      EXT2_INODE_SIZE(fs->super),
371e16324210286dc72a013e66d91bd3a59c3ab7538Theodore Ts'o					      EXT2_BLOCK_SIZE(fs->super));
3726fcd6f84c235f4bf2bd9770f172837da9982eb6eEric Sandeen		if (!lazy_flag || itable_zeroed) {
373d2d22a29df4632d548149b123f197102eb39d3b0Jose R. Santos			/* The kernel doesn't need to zero the itable blocks */
374e633b58ac75f2f544b7d6572e37d4b63da31e59cEric Sandeen			ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
3755711ed297b1a3d94086256b5b3b891d4f77b21caTheodore Ts'o			ext2fs_group_desc_csum_set(fs, i);
37619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		}
37702d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
3780f2794c020b34de391f7a42e3b155ccb0ed2830fTheodore Ts'o		if (retval) {
3790f2794c020b34de391f7a42e3b155ccb0ed2830fTheodore Ts'o			fprintf(stderr, _("\nCould not write %d "
38002d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos				  "blocks in inode table starting at %llu: %s\n"),
3810f2794c020b34de391f7a42e3b155ccb0ed2830fTheodore Ts'o				num, blk, error_message(retval));
3820f2794c020b34de391f7a42e3b155ccb0ed2830fTheodore Ts'o			exit(1);
3830f2794c020b34de391f7a42e3b155ccb0ed2830fTheodore Ts'o		}
3847d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o		if (sync_kludge) {
3857d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o			if (sync_kludge == 1)
3867d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o				sync();
3877d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o			else if ((i % sync_kludge) == 0)
3887d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o				sync();
3897d5633cf6ee1be74777cf4b1adfa9738ff33f2b7Theodore Ts'o		}
3903839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
39102d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos	ext2fs_zero_blocks2(0, 0, 0, 0, 0);
39295fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	ext2fs_numeric_progress_close(fs, &progress,
39395fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson				      _("done                            \n"));
3943839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
3953839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
3963839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void create_root_dir(ext2_filsys fs)
3973839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
3985113a6e32b298671c86ae7da097bcd24540cebc9Eric Sandeen	errcode_t		retval;
399f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	struct ext2_inode	inode;
4003839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
4023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
40345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err("ext2fs_mkdir", retval, "%s",
40445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			_("while creating root dir"));
4053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
4063839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
407dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger	if (root_uid != 0 || root_gid != 0) {
408f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
409f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		if (retval) {
41045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err("ext2fs_read_inode", retval, "%s",
411d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("while reading root inode"));
412f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			exit(1);
413f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		}
414dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger
415dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger		inode.i_uid = root_uid;
416dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger		ext2fs_set_i_uid_high(inode, root_uid >> 16);
417dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger		inode.i_gid = root_gid;
418dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger		ext2fs_set_i_gid_high(inode, root_gid >> 16);
419dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger
420e27b45639aeef964035c1fa78790f761ed31c175Theodore Ts'o		retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
421f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		if (retval) {
42245ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err("ext2fs_write_inode", retval, "%s",
423d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("while setting root inode ownership"));
424f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			exit(1);
425f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		}
426f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	}
4273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
4283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4293839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void create_lost_and_found(ext2_filsys fs)
4303839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
4312d328bb76d2d63bdfdba923b54c28bd686bd8fecTheodore Ts'o	unsigned int		lpf_size = 0;
4323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t		retval;
433dfcdc32f8d6623a35a9e66f503c535e4081b7266Theodore Ts'o	ext2_ino_t		ino;
4343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	const char		*name = "lost+found";
4353839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	int			i;
4363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4376a525069a99787ef3ae1156f12230044b3568f4bTheodore Ts'o	fs->umask = 077;
4383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
4393839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
44045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err("ext2fs_mkdir", retval, "%s",
441d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while creating /lost+found"));
4423839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
4433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
4443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4453839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
4463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
44745ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err("ext2_lookup", retval, "%s",
448d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while looking up /lost+found"));
4493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
4503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
451efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
4523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
4538c7c6eb1675b327387a24bc05ac98dfc890f8073Andreas Dilger		/* Ensure that lost+found is at least 2 blocks, so we always
4548c7c6eb1675b327387a24bc05ac98dfc890f8073Andreas Dilger		 * test large empty blocks for big-block filesystems.  */
4558c7c6eb1675b327387a24bc05ac98dfc890f8073Andreas Dilger		if ((lpf_size += fs->blocksize) >= 16*1024 &&
4568c7c6eb1675b327387a24bc05ac98dfc890f8073Andreas Dilger		    lpf_size >= 2 * fs->blocksize)
45750787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			break;
4583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		retval = ext2fs_expand_dir(fs, ino);
4593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (retval) {
46045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err("ext2fs_expand_dir", retval, "%s",
461d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("while expanding /lost+found"));
4623839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			exit(1);
4633839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
4646a525069a99787ef3ae1156f12230044b3568f4bTheodore Ts'o	}
4653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
4663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
4683839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
4693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t	retval;
470efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
471463e732777d970bb8f96c7e0885c7393141c0d2dValerie Aurora Henson	ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
4725711ed297b1a3d94086256b5b3b891d4f77b21caTheodore Ts'o	ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
4733839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	retval = ext2fs_update_bb_inode(fs, bb_list);
4743839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
47545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err("ext2fs_update_bb_inode", retval, "%s",
476d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while setting bad block inode"));
4773839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
4783839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
4793839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
4813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void reserve_inodes(ext2_filsys fs)
4833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
484dfcdc32f8d6623a35a9e66f503c535e4081b7266Theodore Ts'o	ext2_ino_t	i;
4853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
4865711ed297b1a3d94086256b5b3b891d4f77b21caTheodore Ts'o	for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
4875711ed297b1a3d94086256b5b3b891d4f77b21caTheodore Ts'o		ext2fs_inode_alloc_stats2(fs, i, +1, 0);
4883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	ext2fs_mark_ib_dirty(fs);
4893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
4903839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
491756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o#define BSD_DISKMAGIC   (0x82564557UL)  /* The disk magic number */
4927ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o#define BSD_MAGICDISK   (0x57455682UL)  /* The disk magic number reversed */
493756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o#define BSD_LABEL_OFFSET        64
494756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o
49504a968579ee8125c617edee27204cf35c0a169c1Theodore Ts'ostatic void zap_sector(ext2_filsys fs, int sect, int nsect)
496f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o{
4973f85f65c98183c3871e24f285e5b3e1e97afd9f5Theodore Ts'o	char *buf;
498f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	int retval;
499756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o	unsigned int *magic;
500f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
5013f85f65c98183c3871e24f285e5b3e1e97afd9f5Theodore Ts'o	buf = malloc(512*nsect);
502568101f774f9ad61d475969e91dc90da359b5a85Andreas Dilger	if (!buf) {
5034ea7bd04390935e1f8b473c8b857e518df2e226bTheodore Ts'o		printf(_("Out of memory erasing sectors %d-%d\n"),
5044ea7bd04390935e1f8b473c8b857e518df2e226bTheodore Ts'o		       sect, sect + nsect - 1);
505568101f774f9ad61d475969e91dc90da359b5a85Andreas Dilger		exit(1);
506568101f774f9ad61d475969e91dc90da359b5a85Andreas Dilger	}
507756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o
5087ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o	if (sect == 0) {
5097ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o		/* Check for a BSD disklabel, and don't erase it if so */
51024a117abd0340d247befbf7687ffb70547fdf218Valerie Aurora Henson		retval = io_channel_read_blk64(fs->io, 0, -512, buf);
5117ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o		if (retval)
5127ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o			fprintf(stderr,
5137ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o				_("Warning: could not read block 0: %s\n"),
5147ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o				error_message(retval));
5157ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o		else {
5167ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o			magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
5177ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o			if ((*magic == BSD_DISKMAGIC) ||
5187ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o			    (*magic == BSD_MAGICDISK))
5197ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o				return;
5207ef3bb838d6fd10452d7c47303c61232bfc4a442Theodore Ts'o		}
521756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o	}
522756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o
5237098810daf7d1ba9287a51dc596d78d70d270337Theodore Ts'o	memset(buf, 0, 512*nsect);
524e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	io_channel_set_blksize(fs->io, 512);
52524a117abd0340d247befbf7687ffb70547fdf218Valerie Aurora Henson	retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
526e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	io_channel_set_blksize(fs->io, fs->blocksize);
5273f85f65c98183c3871e24f285e5b3e1e97afd9f5Theodore Ts'o	free(buf);
528f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	if (retval)
5296693837e59cc7b5397a0d46d2753c309382c76f9Theodore Ts'o		fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
5306693837e59cc7b5397a0d46d2753c309382c76f9Theodore Ts'o			sect, error_message(retval));
531f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o}
53216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
53316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'ostatic void create_journal_dev(ext2_filsys fs)
53416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o{
53595fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	struct ext2fs_numeric_progress_struct progress;
53616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	errcode_t		retval;
53716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	char			*buf;
53802d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos	blk64_t			blk, err_blk;
539b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	int			c, count, err_count;
54016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
541d6a27e0048537ab6367a6aa97fbe84a423119eeaTheodore Ts'o	retval = ext2fs_create_journal_superblock(fs,
5424efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson				  ext2fs_blocks_count(fs->super), 0, &buf);
543d6a27e0048537ab6367a6aa97fbe84a423119eeaTheodore Ts'o	if (retval) {
54445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err("create_journal_dev", retval, "%s",
545d6a27e0048537ab6367a6aa97fbe84a423119eeaTheodore Ts'o			_("while initializing journal superblock"));
546d6a27e0048537ab6367a6aa97fbe84a423119eeaTheodore Ts'o		exit(1);
547d6a27e0048537ab6367a6aa97fbe84a423119eeaTheodore Ts'o	}
5486c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger
5496c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger	if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
5506c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger		goto write_superblock;
5516c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger
55295fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	ext2fs_numeric_progress_init(fs, &progress,
55395fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson				     _("Zeroing journal device: "),
55495fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson				     ext2fs_blocks_count(fs->super));
555b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	blk = 0;
5564efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson	count = ext2fs_blocks_count(fs->super);
557b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	while (count > 0) {
558b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		if (count > 1024)
559b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V			c = 1024;
560b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		else
561b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V			c = count;
56202d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
563b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		if (retval) {
564b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V			com_err("create_journal_dev", retval,
565b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V				_("while zeroing journal device "
56602d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos				  "(block %llu, count %d)"),
567b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V				err_blk, err_count);
568b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V			exit(1);
569b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		}
570b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		blk += c;
571b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		count -= c;
572c498cb11d3e72e41af760bd882675f44db8b77e7Theodore Ts'o		ext2fs_numeric_progress_update(fs, &progress, blk);
57316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
57402d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos	ext2fs_zero_blocks2(0, 0, 0, 0, 0);
575dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o
5766c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger	ext2fs_numeric_progress_close(fs, &progress, NULL);
5776c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilgerwrite_superblock:
57824a117abd0340d247befbf7687ffb70547fdf218Valerie Aurora Henson	retval = io_channel_write_blk64(fs->io,
57924a117abd0340d247befbf7687ffb70547fdf218Valerie Aurora Henson					fs->super->s_first_data_block+1,
58024a117abd0340d247befbf7687ffb70547fdf218Valerie Aurora Henson					1, buf);
58116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (retval) {
58245ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err("create_journal_dev", retval, "%s",
58316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			_("while writing journal superblock"));
58416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		exit(1);
58516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
58616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o}
587f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o
5883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void show_stats(ext2_filsys fs)
5893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
590ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o	struct ext2_super_block *s = fs->super;
5911e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	char 			buf[80];
59263253946309651c1015947d522e2ba4b35a807a5Theodore Ts'o        char                    *os;
59302d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos	blk64_t			group_block;
594544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o	dgrp_t			i;
595544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o	int			need, col_left;
596efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
5974efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson	if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
5984efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson		fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
5994efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson		       ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
60050787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o
60150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	memset(buf, 0, sizeof(buf));
60250787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
603d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("Filesystem label=%s\n"), buf);
60474e1211b2329b6c778ac4f834cc9e38f855ebaa0Theodore Ts'o	os = e2p_os2string(fs->super->s_creator_os);
60574e1211b2329b6c778ac4f834cc9e38f855ebaa0Theodore Ts'o	if (os)
60674e1211b2329b6c778ac4f834cc9e38f855ebaa0Theodore Ts'o		printf(_("OS type: %s\n"), os);
60763253946309651c1015947d522e2ba4b35a807a5Theodore Ts'o	free(os);
608d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
60950787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		s->s_log_block_size);
610412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
611412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o				       EXT4_FEATURE_RO_COMPAT_BIGALLOC))
612412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o		printf(_("Cluster size=%u (log=%u)\n"),
6131da5ef707904cf99300a0fb36b9ae3b29dbc8bdeTheodore Ts'o		       fs->blocksize << fs->cluster_ratio_bits,
6141da5ef707904cf99300a0fb36b9ae3b29dbc8bdeTheodore Ts'o		       s->s_log_cluster_size);
615412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o	else
6161da5ef707904cf99300a0fb36b9ae3b29dbc8bdeTheodore Ts'o		printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
617412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o		       s->s_log_cluster_size);
6189ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
6199ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	       s->s_raid_stride, s->s_raid_stripe_width);
6204efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson	printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
6214efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson	       ext2fs_blocks_count(s));
6224efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson	printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
6234efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson		ext2fs_r_blocks_count(s),
6244efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson	       100.0 *  ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
625d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("First data block=%u\n"), s->s_first_data_block);
626dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger	if (root_uid != 0 || root_gid != 0)
627dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger		printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
628d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o	if (s->s_reserved_gdt_blocks)
629d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o		printf(_("Maximum filesystem blocks=%lu\n"),
630d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o		       (s->s_reserved_gdt_blocks + fs->desc_blocks) *
631f2de1d38d0819b17895977fabc52d81d0ea6ec00Valerie Clement		       EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
632d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	if (fs->group_desc_count > 1)
633c8c071a07319939dfd7ae2ff1dedec644d750debTheodore Ts'o		printf(_("%u block groups\n"), fs->group_desc_count);
634d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	else
635c8c071a07319939dfd7ae2ff1dedec644d750debTheodore Ts'o		printf(_("%u block group\n"), fs->group_desc_count);
636412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
637412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o				       EXT4_FEATURE_RO_COMPAT_BIGALLOC))
638412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o		printf(_("%u blocks per group, %u clusters per group\n"),
639412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o		       s->s_blocks_per_group, s->s_clusters_per_group);
640412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o	else
641412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o		printf(_("%u blocks per group, %u fragments per group\n"),
642412376efff3c0e0c2fea00666c2457e6f2ae1878Theodore Ts'o		       s->s_blocks_per_group, s->s_clusters_per_group);
643d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	printf(_("%u inodes per group\n"), s->s_inodes_per_group);
6443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
6453839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (fs->group_desc_count == 1) {
6463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		printf("\n");
6473839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		return;
6483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
649d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o
65045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger	printf("%s", _("Superblock backups stored on blocks: "));
6513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	group_block = s->s_first_data_block;
6523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	col_left = 0;
6533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	for (i = 1; i < fs->group_desc_count; i++) {
6543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		group_block += s->s_blocks_per_group;
655521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o		if (!ext2fs_bg_has_super(fs, i))
656521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o			continue;
6577671433a9daad089af037f84b365d640e1adf252Theodore Ts'o		if (i != 1)
6587671433a9daad089af037f84b365d640e1adf252Theodore Ts'o			printf(", ");
6596733c2fd0046c525203034f58fc0a8c69fdf480bTheodore Ts'o		need = int_log10(group_block) + 2;
6601dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o		if (need > col_left) {
6613839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			printf("\n\t");
6621dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o			col_left = 72;
6633839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
6641dde43f0c1176f61dd0bf91aff265ce8cd1c5fd6Theodore Ts'o		col_left -= need;
66502d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		printf("%llu", group_block);
6663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
6673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	printf("\n\n");
6683839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
6693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
6701e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o/*
6711e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o * Set the S_CREATOR_OS field.  Return true if OS is known,
6721e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o * otherwise, 0.
6731e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o */
6741e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'ostatic int set_os(struct ext2_super_block *sb, char *os)
6751e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o{
6761e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (isdigit (*os))
6771e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		sb->s_creator_os = atoi (os);
6781e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	else if (strcasecmp(os, "linux") == 0)
6791e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		sb->s_creator_os = EXT2_OS_LINUX;
6801e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
6811e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		sb->s_creator_os = EXT2_OS_HURD;
682ea1e8f471ea7f9e8e8eee002bdd37afee4af14acTheodore Ts'o	else if (strcasecmp(os, "freebsd") == 0)
683ea1e8f471ea7f9e8e8eee002bdd37afee4af14acTheodore Ts'o		sb->s_creator_os = EXT2_OS_FREEBSD;
684ea1e8f471ea7f9e8e8eee002bdd37afee4af14acTheodore Ts'o	else if (strcasecmp(os, "lites") == 0)
685ea1e8f471ea7f9e8e8eee002bdd37afee4af14acTheodore Ts'o		sb->s_creator_os = EXT2_OS_LITES;
6861e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	else
6871e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		return 0;
6881e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	return 1;
6891e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o}
6901e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
691a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#define PATH_SET "PATH=/sbin"
692a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o
693efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'ostatic void parse_extended_opts(struct ext2_super_block *param,
694c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o				const char *opts)
695a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o{
6962d328bb76d2d63bdfdba923b54c28bd686bd8fecTheodore Ts'o	char	*buf, *token, *next, *p, *arg, *badopt = 0;
697a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	int	len;
698d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o	int	r_usage = 0;
699a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
700a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	len = strlen(opts);
701a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	buf = malloc(len+1);
702a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	if (!buf) {
70345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		fprintf(stderr, "%s",
704d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o			_("Couldn't allocate memory to parse options!\n"));
705a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		exit(1);
706a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	}
707a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	strcpy(buf, opts);
708a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	for (token = buf; token && *token; token = next) {
709a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		p = strchr(token, ',');
710a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		next = 0;
711a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		if (p) {
712a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			*p = 0;
713a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			next = p+1;
714d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o		}
715a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		arg = strchr(token, '=');
716a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		if (arg) {
717a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			*arg = 0;
718a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			arg++;
719a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		}
7202bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger		if (strcmp(token, "desc-size") == 0 ||
7212bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger		    strcmp(token, "desc_size") == 0) {
7222bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			int desc_size;
7232bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger
7242bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			if (!(fs_param.s_feature_incompat &
7252bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			      EXT4_FEATURE_INCOMPAT_64BIT)) {
7262bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				fprintf(stderr,
7272bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger					_("%s requires '-O 64bit'\n"), token);
7282bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				r_usage++;
7292bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				continue;
7302bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			}
7312bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			if (param->s_reserved_gdt_blocks != 0) {
7322bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				fprintf(stderr,
7332bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger					_("'%s' must be before 'resize=%u'\n"),
7342bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger					token, param->s_reserved_gdt_blocks);
7352bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				r_usage++;
7362bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				continue;
7372bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			}
7382bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			if (!arg) {
7392bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				r_usage++;
7402bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				badopt = token;
7412bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				continue;
7422bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			}
7432bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			desc_size = strtoul(arg, &p, 0);
7442bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			if (*p || (desc_size & (desc_size - 1))) {
7452bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				fprintf(stderr,
7462bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger					_("Invalid desc_size: '%s'\n"), arg);
7472bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				r_usage++;
7482bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger				continue;
7492bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			}
7502bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger			param->s_desc_size = desc_size;
75188ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o		} else if (strcmp(token, "offset") == 0) {
75288ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o			if (!arg) {
75388ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o				r_usage++;
75488ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o				badopt = token;
75588ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o				continue;
75688ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o			}
75788ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o			offset = strtoull(arg, &p, 0);
75888ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o			if (*p) {
75988ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o				fprintf(stderr, _("Invalid offset: %s\n"),
76088ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o					arg);
76188ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o				r_usage++;
76288ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o				continue;
76388ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o			}
7642bc30417541deffca795db8ec4e7f7ccb616dc3fAndreas Dilger		} else if (strcmp(token, "mmp_update_interval") == 0) {
7650f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger			if (!arg) {
7660f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger				r_usage++;
7670f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger				badopt = token;
7680f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger				continue;
7690f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger			}
7700f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger			param->s_mmp_update_interval = strtoul(arg, &p, 0);
7710f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger			if (*p) {
7720f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger				fprintf(stderr,
7730f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger					_("Invalid mmp_update_interval: %s\n"),
7740f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger					arg);
7750f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger				r_usage++;
7760f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger				continue;
7770f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger			}
77865c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o		} else if (strcmp(token, "num_backup_sb") == 0) {
77965c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o			if (!arg) {
78065c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o				r_usage++;
78165c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o				badopt = token;
78265c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o				continue;
78365c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o			}
78465c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o			num_backups = strtoul(arg, &p, 0);
78565c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o			if (*p || num_backups > 2) {
78665c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o				fprintf(stderr,
78765c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o					_("Invalid # of backup "
78865c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o					  "superbocks: %s\n"),
78965c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o					arg);
79065c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o				r_usage++;
79165c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o				continue;
79265c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o			}
7933c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		} else if (strcmp(token, "packed_meta_blocks") == 0) {
7943c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o			if (arg)
7953c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o				packed_meta_blocks = strtoul(arg, &p, 0);
7963c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o			else
7973c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o				packed_meta_blocks = 1;
7983c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o			if (packed_meta_blocks)
7993c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o				journal_location = 0;
8000f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger		} else if (strcmp(token, "stride") == 0) {
801a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			if (!arg) {
802d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o				r_usage++;
8030c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o				badopt = token;
804a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o				continue;
805a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			}
8060c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			param->s_raid_stride = strtoul(arg, &p, 0);
8075b734a0e715ba6590624247b0866e4791f717981Theodore Ts'o			if (*p) {
808d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				fprintf(stderr,
809f37ab68a26bacf4f5cc7643b8373e40292b7682aTheodore Ts'o					_("Invalid stride parameter: %s\n"),
810f37ab68a26bacf4f5cc7643b8373e40292b7682aTheodore Ts'o					arg);
811d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o				r_usage++;
812d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o				continue;
813d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o			}
8140c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o		} else if (strcmp(token, "stripe-width") == 0 ||
8150c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			   strcmp(token, "stripe_width") == 0) {
8160c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			if (!arg) {
8170c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o				r_usage++;
8180c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o				badopt = token;
8190c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o				continue;
8200c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			}
8210c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			param->s_raid_stripe_width = strtoul(arg, &p, 0);
8225b734a0e715ba6590624247b0866e4791f717981Theodore Ts'o			if (*p) {
8230c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o				fprintf(stderr,
8240c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o					_("Invalid stripe-width parameter: %s\n"),
8250c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o					arg);
8260c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o				r_usage++;
8270c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o				continue;
8280c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			}
829d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o		} else if (!strcmp(token, "resize")) {
83002d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos			blk64_t resize;
83102d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos			unsigned long bpg, rsv_groups;
832c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			unsigned long group_desc_count, desc_blocks;
833c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			unsigned int gdpb, blocksize;
834c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			int rsv_gdb;
835d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o
836d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o			if (!arg) {
837d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o				r_usage++;
8380c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o				badopt = token;
839a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o				continue;
840a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			}
841d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o
84202d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos			resize = parse_num_blocks2(arg,
84302d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos						   param->s_log_block_size);
844d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o
845d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o			if (resize == 0) {
846efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o				fprintf(stderr,
84755f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o					_("Invalid resize parameter: %s\n"),
84855f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o					arg);
849d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o				r_usage++;
850d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o				continue;
851d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o			}
8524efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson			if (resize <= ext2fs_blocks_count(param)) {
85345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				fprintf(stderr, "%s",
854f37ab68a26bacf4f5cc7643b8373e40292b7682aTheodore Ts'o					_("The resize maximum must be greater "
855f37ab68a26bacf4f5cc7643b8373e40292b7682aTheodore Ts'o					  "than the filesystem size.\n"));
856c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o				r_usage++;
857c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o				continue;
858c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			}
859d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o
860c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			blocksize = EXT2_BLOCK_SIZE(param);
861c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			bpg = param->s_blocks_per_group;
862c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			if (!bpg)
863c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o				bpg = blocksize * 8;
864f2de1d38d0819b17895977fabc52d81d0ea6ec00Valerie Clement			gdpb = EXT2_DESC_PER_BLOCK(param);
8654efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson			group_desc_count = (__u32) ext2fs_div64_ceil(
8664efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson				ext2fs_blocks_count(param), bpg);
867c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			desc_blocks = (group_desc_count +
868c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o				       gdpb - 1) / gdpb;
86902d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos			rsv_groups = ext2fs_div64_ceil(resize, bpg);
870efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o			rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
871c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o				desc_blocks;
8729b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o			if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
873c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o				rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
874c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o
875c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			if (rsv_gdb > 0) {
876b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o				if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
87745ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger					fprintf(stderr, "%s",
878b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o	_("On-line resizing not supported with revision 0 filesystems\n"));
87921400381d90a1e58443e1e2c3f52f8fd5550be9dBrian Behlendorf					free(buf);
880b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o					exit(1);
881b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o				}
882c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o				param->s_feature_compat |=
883c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o					EXT2_FEATURE_COMPAT_RESIZE_INODE;
884c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o
885c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o				param->s_reserved_gdt_blocks = rsv_gdb;
886c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o			}
8876cb27404f51f97e2665fa0e0c4c0f7bc47e698ecTheodore Ts'o		} else if (!strcmp(token, "test_fs")) {
8886cb27404f51f97e2665fa0e0c4c0f7bc47e698ecTheodore Ts'o			param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
889a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o		} else if (!strcmp(token, "lazy_itable_init")) {
89043781b9459f1b511e4cf94358593e097d8fdb795Theodore Ts'o			if (arg)
89143781b9459f1b511e4cf94358593e097d8fdb795Theodore Ts'o				lazy_itable_init = strtoul(arg, &p, 0);
89243781b9459f1b511e4cf94358593e097d8fdb795Theodore Ts'o			else
89343781b9459f1b511e4cf94358593e097d8fdb795Theodore Ts'o				lazy_itable_init = 1;
8946c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger		} else if (!strcmp(token, "lazy_journal_init")) {
8956c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger			if (arg)
8966c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger				journal_flags |= strtoul(arg, &p, 0) ?
8976c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger						EXT2_MKJOURNAL_LAZYINIT : 0;
8986c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger			else
8996c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger				journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
900dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger		} else if (!strcmp(token, "root_owner")) {
901dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger			if (arg) {
902dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger				root_uid = strtoul(arg, &p, 0);
903dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger				if (*p != ':') {
904dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger					fprintf(stderr,
905dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger						_("Invalid root_owner: '%s'\n"),
906dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger						arg);
907dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger					r_usage++;
908dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger					continue;
909dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger				}
910dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger				p++;
911dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger				root_gid = strtoul(p, &p, 0);
912dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger				if (*p) {
913dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger					fprintf(stderr,
914dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger						_("Invalid root_owner: '%s'\n"),
915dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger						arg);
916dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger					r_usage++;
917dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger					continue;
918dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger				}
919dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger			} else {
920dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger				root_uid = getuid();
921dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger				root_gid = getgid();
922dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger			}
9230bc85dfb384b470e6170268c8436fcbe58317cbeLukas Czerner		} else if (!strcmp(token, "discard")) {
9240bc85dfb384b470e6170268c8436fcbe58317cbeLukas Czerner			discard = 1;
9250bc85dfb384b470e6170268c8436fcbe58317cbeLukas Czerner		} else if (!strcmp(token, "nodiscard")) {
9260bc85dfb384b470e6170268c8436fcbe58317cbeLukas Czerner			discard = 0;
927d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali		} else if (!strcmp(token, "quotatype")) {
928d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali			if (!arg) {
929d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali				r_usage++;
930d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali				badopt = token;
931d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali				continue;
932d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali			}
933d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali			if (!strncmp(arg, "usr", 3)) {
934d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali				quotatype = 0;
935d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali			} else if (!strncmp(arg, "grp", 3)) {
936d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali				quotatype = 1;
937d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali			} else {
938d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali				fprintf(stderr,
939d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali					_("Invalid quotatype parameter: %s\n"),
940d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali					arg);
941d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali				r_usage++;
942d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali				continue;
943d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali			}
9440c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o		} else {
945d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o			r_usage++;
9460c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			badopt = token;
9470c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o		}
948a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	}
949d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o	if (r_usage) {
9500c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o		fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
951bb145b01cf5fd27d9afe03c3262d0e1a326e7ec1Theodore Ts'o			"Extended options are separated by commas, "
952a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			"and may take an argument which\n"
953a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o			"\tis set off by an equals ('=') sign.\n\n"
954bb145b01cf5fd27d9afe03c3262d0e1a326e7ec1Theodore Ts'o			"Valid extended options are:\n"
95588ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o			"\tmmp_update_interval=<interval>\n"
95665c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o			"\tnum_backup_sb=<0|1|2>\n"
9570c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			"\tstride=<RAID per-disk data chunk in blocks>\n"
9580c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			"\tstripe-width=<RAID stride * data disks in blocks>\n"
95988ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o			"\toffset=<offset to create the file system>\n"
960a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o			"\tresize=<resize maximum size in blocks>\n"
9613c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o			"\tpacked_meta_blocks=<0 to disable, 1 to enable>\n"
962a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o			"\tlazy_itable_init=<0 to disable, 1 to enable>\n"
9636c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger			"\tlazy_journal_init=<0 to disable, 1 to enable>\n"
964dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger			"\troot_uid=<uid of root directory>\n"
965dc9cc7006a61d0ef82ee4e1dee133998c6040765Andreas Dilger			"\troot_gid=<gid of root directory>\n"
9660bc85dfb384b470e6170268c8436fcbe58317cbeLukas Czerner			"\ttest_fs\n"
9670bc85dfb384b470e6170268c8436fcbe58317cbeLukas Czerner			"\tdiscard\n"
968d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali			"\tnodiscard\n"
969d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali			"\tquotatype=<usr OR grp>\n\n"),
9702d328bb76d2d63bdfdba923b54c28bd686bd8fecTheodore Ts'o			badopt ? badopt : "");
97121400381d90a1e58443e1e2c3f52f8fd5550be9dBrian Behlendorf		free(buf);
972a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		exit(1);
973a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	}
9740c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o	if (param->s_raid_stride &&
9750c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o	    (param->s_raid_stripe_width % param->s_raid_stride) != 0)
9760c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o		fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
9770c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o				  "multiple of stride %u.\n\n"),
9780c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o			param->s_raid_stripe_width, param->s_raid_stride);
9790c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o
98021400381d90a1e58443e1e2c3f52f8fd5550be9dBrian Behlendorf	free(buf);
981efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o}
982a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
983896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'ostatic __u32 ok_features[3] = {
984558df54458075856b3a76051648323643a56dadcTheodore Ts'o	/* Compat */
985843049c4dbfe8a0dd4332569eff0ac93529e3324Theodore Ts'o	EXT3_FEATURE_COMPAT_HAS_JOURNAL |
986d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o		EXT2_FEATURE_COMPAT_RESIZE_INODE |
987f5fa20078bfc05b554294fe9c5505375d7913e8cTheodore Ts'o		EXT2_FEATURE_COMPAT_DIR_INDEX |
98865c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o		EXT2_FEATURE_COMPAT_EXT_ATTR |
98965c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o		EXT4_FEATURE_COMPAT_SPARSE_SUPER2,
990558df54458075856b3a76051648323643a56dadcTheodore Ts'o	/* Incompat */
991558df54458075856b3a76051648323643a56dadcTheodore Ts'o	EXT2_FEATURE_INCOMPAT_FILETYPE|
992bf6b848e1247610b4fba091de87b61f4b2b92a11Theodore Ts'o		EXT3_FEATURE_INCOMPAT_EXTENTS|
993c046ac7f2e4c53e20cf1e909bbe511f91074b396Theodore Ts'o		EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
994c2d4300b8a4a13d8a78b86c386f76259f23feec2Jose R. Santos		EXT2_FEATURE_INCOMPAT_META_BG|
99502d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		EXT4_FEATURE_INCOMPAT_FLEX_BG|
9960f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger		EXT4_FEATURE_INCOMPAT_MMP |
99702d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		EXT4_FEATURE_INCOMPAT_64BIT,
998558df54458075856b3a76051648323643a56dadcTheodore Ts'o	/* R/O compat */
999558df54458075856b3a76051648323643a56dadcTheodore Ts'o	EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
10002be8fe43976537b75a6ee154ff3ba47e538b55fbTheodore Ts'o		EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
10012be8fe43976537b75a6ee154ff3ba47e538b55fbTheodore Ts'o		EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
10022be8fe43976537b75a6ee154ff3ba47e538b55fbTheodore Ts'o		EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
1003d2d22a29df4632d548149b123f197102eb39d3b0Jose R. Santos		EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
1004c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o		EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
10051f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali		EXT4_FEATURE_RO_COMPAT_BIGALLOC|
10067becb2065ffd5c4b403546d577c2421b4a840c3cTheodore Ts'o#ifdef CONFIG_QUOTA
10077becb2065ffd5c4b403546d577c2421b4a840c3cTheodore Ts'o		EXT4_FEATURE_RO_COMPAT_QUOTA|
10087becb2065ffd5c4b403546d577c2421b4a840c3cTheodore Ts'o#endif
10097becb2065ffd5c4b403546d577c2421b4a840c3cTheodore Ts'o		0
1010896938d57e7091e7a032674dfeeb91f2a17fd78bTheodore Ts'o};
1011a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
1012a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
10139dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'ostatic void syntax_err_report(const char *filename, long err, int line_num)
10149dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o{
1015efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	fprintf(stderr,
10169dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		_("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
10179dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		filename, line_num, error_message(err));
10189dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	exit(1);
10199dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o}
10209dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
1021abcfdfda7a5932cbb27ff243ed962119db2599a4Matthias Andreestatic const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
10229dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
1023efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'ostatic void edit_feature(const char *str, __u32 *compat_array)
10249dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o{
10259dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	if (!str)
10269dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		return;
10279dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
10289dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	if (e2p_edit_feature(str, compat_array, ok_features)) {
1029efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o		fprintf(stderr, _("Invalid filesystem option set: %s\n"),
10309dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o			str);
10319dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		exit(1);
10329dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	}
10339dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o}
10349dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
10356a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeenstatic void edit_mntopts(const char *str, __u32 *mntopts)
10366a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen{
10376a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen	if (!str)
10386a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen		return;
10396a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen
10406a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen	if (e2p_edit_mntopts(str, mntopts, ~0)) {
10416a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen		fprintf(stderr, _("Invalid mount option set: %s\n"),
10426a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen			str);
10436a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen		exit(1);
10446a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen	}
10456a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen}
10466a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen
10473d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'ostruct str_list {
10483d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	char **list;
10493d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	int num;
10503d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	int max;
10513d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o};
10523d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
10533d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'ostatic errcode_t init_list(struct str_list *sl)
10543d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o{
10553d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	sl->num = 0;
10563d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	sl->max = 0;
10573d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	sl->list = malloc((sl->max+1) * sizeof(char *));
10583d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (!sl->list)
10593d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		return ENOMEM;
10603d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	sl->list[0] = 0;
10613d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	return 0;
10623d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o}
10633d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
10643d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'ostatic errcode_t push_string(struct str_list *sl, const char *str)
10653d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o{
10663d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	char **new_list;
10673d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
10683d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (sl->num >= sl->max) {
10693d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		sl->max += 2;
10703d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
10713d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		if (!new_list)
10723d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			return ENOMEM;
10733d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		sl->list = new_list;
10743d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	}
10753d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	sl->list[sl->num] = malloc(strlen(str)+1);
10763d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (sl->list[sl->num] == 0)
10773d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		return ENOMEM;
10783d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	strcpy(sl->list[sl->num], str);
10793d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	sl->num++;
10803d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	sl->list[sl->num] = 0;
10813d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	return 0;
10823d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o}
10833d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
10843d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'ostatic void print_str_list(char **list)
10853d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o{
10863d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	char **cpp;
10873d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
10883d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	for (cpp = list; *cpp; cpp++) {
10893d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		printf("'%s'", *cpp);
10903d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		if (cpp[1])
10913d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			fputs(", ", stdout);
10923d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	}
10933d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	fputc('\n', stdout);
10943d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o}
10953d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
10962ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o/*
10972ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o * Return TRUE if the profile has the given subsection
10982ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o */
1099577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'ostatic int profile_has_subsection(profile_t prof, const char *section,
11002ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o				  const char *subsection)
11012ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o{
11022ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	void			*state;
11032ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	const char		*names[4];
11042ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	char			*name;
11052ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	int			ret = 0;
11062ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o
11072ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	names[0] = section;
11082ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	names[1] = subsection;
11092ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	names[2] = 0;
11102ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o
1111577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	if (profile_iterator_create(prof, names,
11122ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o				    PROFILE_ITER_LIST_SECTION |
11132ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o				    PROFILE_ITER_RELATIONS_ONLY, &state))
11142ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		return 0;
11152ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o
11162ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	if ((profile_iterator(&state, &name, 0) == 0) && name) {
11172ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		free(name);
11182ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		ret = 1;
11192ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	}
11202ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o
11212ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	profile_iterator_free(&state);
11222ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	return ret;
11232ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o}
11242ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o
11253d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'ostatic char **parse_fs_type(const char *fs_type,
11263d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			    const char *usage_types,
1127577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o			    struct ext2_super_block *sb,
1128493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o			    blk64_t fs_blocks_count,
11293d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			    char *progname)
11303d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o{
11313d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	const char	*ext_type = 0;
11323d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	char		*parse_str;
11333d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	char		*profile_type = 0;
11343d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	char		*cp, *t;
11353d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	const char	*size_type;
11363d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	struct str_list	list;
1137493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	unsigned long long meg;
11387f5601e5b5dd733c23ca73f965d37d49547150abTheodore Ts'o	int		is_hurd = 0;
11393d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
11403d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (init_list(&list))
11413d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		return 0;
11423d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
11437f5601e5b5dd733c23ca73f965d37d49547150abTheodore Ts'o	if (creator_os && (!strcasecmp(creator_os, "GNU") ||
11447f5601e5b5dd733c23ca73f965d37d49547150abTheodore Ts'o			   !strcasecmp(creator_os, "hurd")))
11457f5601e5b5dd733c23ca73f965d37d49547150abTheodore Ts'o		is_hurd = 1;
11467f5601e5b5dd733c23ca73f965d37d49547150abTheodore Ts'o
11473d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (fs_type)
11483d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		ext_type = fs_type;
11497f5601e5b5dd733c23ca73f965d37d49547150abTheodore Ts'o	else if (is_hurd)
11507f5601e5b5dd733c23ca73f965d37d49547150abTheodore Ts'o		ext_type = "ext2";
11511a71bd42003d87ff96e3e0e8c293b44c8783e9baTheodore Ts'o	else if (!strcmp(program_name, "mke3fs"))
11521a71bd42003d87ff96e3e0e8c293b44c8783e9baTheodore Ts'o		ext_type = "ext3";
1153237b7b23fe69a83a9bb1d98d014d26f146ac26a3Eric Sandeen	else if (!strcmp(program_name, "mke4fs"))
1154237b7b23fe69a83a9bb1d98d014d26f146ac26a3Eric Sandeen		ext_type = "ext4";
11553d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	else if (progname) {
11563d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		ext_type = strrchr(progname, '/');
11573d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		if (ext_type)
11583d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			ext_type++;
11593d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		else
11603d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			ext_type = progname;
11613d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
11623d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		if (!strncmp(ext_type, "mkfs.", 5)) {
11633d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			ext_type += 5;
11643d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			if (ext_type[0] == 0)
11653d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o				ext_type = 0;
11663d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		} else
11673d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			ext_type = 0;
11683d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	}
11693d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
11703d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (!ext_type) {
11713d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		profile_get_string(profile, "defaults", "fs_type", 0,
11723d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o				   "ext2", &profile_type);
11733d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		ext_type = profile_type;
11743d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		if (!strcmp(ext_type, "ext2") && (journal_size != 0))
11753d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			ext_type = "ext3";
11763d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	}
11773d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
11782ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o
11792ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	if (!profile_has_subsection(profile, "fs_types", ext_type) &&
11802ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o	    strcmp(ext_type, "ext2")) {
11812ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		printf(_("\nYour mke2fs.conf file does not define the "
11822ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o			 "%s filesystem type.\n"), ext_type);
11832ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
11842ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		    !strcmp(ext_type, "ext4dev")) {
118545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			printf("%s", _("You probably need to install an "
118645ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				       "updated mke2fs.conf file.\n\n"));
11872ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		}
11882ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		if (!force) {
118945ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			printf("%s", _("Aborting...\n"));
11902ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o			exit(1);
1191bad89b2af3ffa43c9570d93c3d9d9adacc194f9aTheodore Ts'o		}
1192bad89b2af3ffa43c9570d93c3d9d9adacc194f9aTheodore Ts'o	}
1193bad89b2af3ffa43c9570d93c3d9d9adacc194f9aTheodore Ts'o
1194577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1195493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	if (fs_blocks_count < 3 * meg)
11963d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		size_type = "floppy";
1197493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	else if (fs_blocks_count < 512 * meg)
11983d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		size_type = "small";
119922d8ce512ffcdb9d6a16e1d34f65aa37ce1ab3d3Namhyung Kim	else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
120022d8ce512ffcdb9d6a16e1d34f65aa37ce1ab3d3Namhyung Kim		size_type = "default";
120122d8ce512ffcdb9d6a16e1d34f65aa37ce1ab3d3Namhyung Kim	else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1202493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o		size_type = "big";
12033d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	else
120422d8ce512ffcdb9d6a16e1d34f65aa37ce1ab3d3Namhyung Kim		size_type = "huge";
12053d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
12063d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (!usage_types)
12073d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		usage_types = size_type;
12083d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
12094d5cf8b166bb29f0e3280bbadca9d3a8b895acd1Eric Sandeen	parse_str = malloc(strlen(usage_types)+1);
12103d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (!parse_str) {
121180a1806d617fdb14421ac6ac50a640ac1fd303b3Darrick J. Wong		free(profile_type);
12123d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		free(list.list);
12133d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		return 0;
12143d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	}
12154d5cf8b166bb29f0e3280bbadca9d3a8b895acd1Eric Sandeen	strcpy(parse_str, usage_types);
12163d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
12173d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (ext_type)
12183d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		push_string(&list, ext_type);
12193d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	cp = parse_str;
12203d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	while (1) {
12213d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		t = strchr(cp, ',');
12223d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		if (t)
12233d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			*t = '\0';
12243d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
12252ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		if (*cp) {
12260f7479b34048c6cf48a539c411552ef1542ee0b8Theodore Ts'o			if (profile_has_subsection(profile, "fs_types", cp))
12270f7479b34048c6cf48a539c411552ef1542ee0b8Theodore Ts'o				push_string(&list, cp);
12280f7479b34048c6cf48a539c411552ef1542ee0b8Theodore Ts'o			else if (strcmp(cp, "default") != 0)
12292ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o				fprintf(stderr,
12302ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o					_("\nWarning: the fs_type %s is not "
12310f7479b34048c6cf48a539c411552ef1542ee0b8Theodore Ts'o					  "defined in mke2fs.conf\n\n"),
12322ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o					cp);
12332ee4544d07af35a6a667400cca35657e37d55c3bTheodore Ts'o		}
12343d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		if (t)
12353d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			cp = t+1;
1236577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o		else
12373d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			break;
12383d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	}
12393d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	free(parse_str);
124045e338f5332a54295893dba2e32cc093d1316f60Jim Meyering	free(profile_type);
12417f5601e5b5dd733c23ca73f965d37d49547150abTheodore Ts'o	if (is_hurd)
12427f5601e5b5dd733c23ca73f965d37d49547150abTheodore Ts'o		push_string(&list, "hurd");
12433d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	return (list.list);
12443d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o}
12453d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
1246577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'ostatic char *get_string_from_profile(char **types, const char *opt,
12473d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o				     const char *def_val)
12483d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o{
12493d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	char *ret = 0;
12503d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	int i;
12513d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
1252577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	for (i=0; types[i]; i++);
12533d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	for (i-=1; i >=0 ; i--) {
1254577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o		profile_get_string(profile, "fs_types", types[i],
12553d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o				   opt, 0, &ret);
12563d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		if (ret)
12573d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			return ret;
12583d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	}
12593d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
12603d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	return (ret);
12613d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o}
12623d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
1263577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'ostatic int get_int_from_profile(char **types, const char *opt, int def_val)
12643d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o{
12653d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	int ret;
12663d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	char **cpp;
12673d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
12683d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1269577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	for (cpp = types; *cpp; cpp++)
12703d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
12713d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	return ret;
12723d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o}
12733d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
1274577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'ostatic double get_double_from_profile(char **types, const char *opt,
1275d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali				      double def_val)
1276d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali{
1277d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali	double ret;
1278d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali	char **cpp;
1279d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali
1280d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali	profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1281577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	for (cpp = types; *cpp; cpp++)
1282d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali		profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1283d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali	return ret;
1284d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali}
1285d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali
1286577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'ostatic int get_bool_from_profile(char **types, const char *opt, int def_val)
1287a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o{
1288a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o	int ret;
1289a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o	char **cpp;
1290a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o
1291a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o	profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1292577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	for (cpp = types; *cpp; cpp++)
1293a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o		profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1294a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o	return ret;
1295a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o}
12963d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o
1297d48bc60459f3552d2f229d8ec041b7eb36787541Theodore Ts'oextern const char *mke2fs_default_profile;
1298d48bc60459f3552d2f229d8ec041b7eb36787541Theodore Ts'ostatic const char *default_files[] = { "<default>", 0 };
1299d48bc60459f3552d2f229d8ec041b7eb36787541Theodore Ts'o
13009ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
13019ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen/*
13029ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen * Sets the geometry of a device (stripe/stride), and returns the
13039ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen * device's alignment offset, if any, or a negative error.
13049ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen */
13051599b470fb881313c1d44594f05d578c43825312Theodore Ts'ostatic int get_device_geometry(const char *file,
13061599b470fb881313c1d44594f05d578c43825312Theodore Ts'o			       struct ext2_super_block *fs_param,
13071599b470fb881313c1d44594f05d578c43825312Theodore Ts'o			       int psector_size)
13089ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen{
13099ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	int rc = -1;
13109ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	int blocksize;
13119ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	blkid_probe pr;
13129ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	blkid_topology tp;
13139ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	unsigned long min_io;
13149ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	unsigned long opt_io;
131513b0b1231ed28aac75ba336de7a8cb3b4611ce68Eric Sandeen	struct stat statbuf;
131613b0b1231ed28aac75ba336de7a8cb3b4611ce68Eric Sandeen
131713b0b1231ed28aac75ba336de7a8cb3b4611ce68Eric Sandeen	/* Nothing to do for a regular file */
131813b0b1231ed28aac75ba336de7a8cb3b4611ce68Eric Sandeen	if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
131913b0b1231ed28aac75ba336de7a8cb3b4611ce68Eric Sandeen		return 0;
13209ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen
13219ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	pr = blkid_new_probe_from_filename(file);
13229ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	if (!pr)
13239ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen		goto out;
13249ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen
13259ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	tp = blkid_probe_get_topology(pr);
13269ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	if (!tp)
13279ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen		goto out;
13289ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen
13299ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	min_io = blkid_topology_get_minimum_io_size(tp);
13309ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	opt_io = blkid_topology_get_optimal_io_size(tp);
13319ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	blocksize = EXT2_BLOCK_SIZE(fs_param);
13321599b470fb881313c1d44594f05d578c43825312Theodore Ts'o	if ((min_io == 0) && (psector_size > blocksize))
13331599b470fb881313c1d44594f05d578c43825312Theodore Ts'o		min_io = psector_size;
13341599b470fb881313c1d44594f05d578c43825312Theodore Ts'o	if ((opt_io == 0) && min_io)
13351599b470fb881313c1d44594f05d578c43825312Theodore Ts'o		opt_io = min_io;
13361599b470fb881313c1d44594f05d578c43825312Theodore Ts'o	if ((opt_io == 0) && (psector_size > blocksize))
13371599b470fb881313c1d44594f05d578c43825312Theodore Ts'o		opt_io = psector_size;
13389ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen
1339d568782ade9b630a6f28f24b1357fc89a6ba8286Eric Sandeen	/* setting stripe/stride to blocksize is pointless */
1340d568782ade9b630a6f28f24b1357fc89a6ba8286Eric Sandeen	if (min_io > blocksize)
1341d568782ade9b630a6f28f24b1357fc89a6ba8286Eric Sandeen		fs_param->s_raid_stride = min_io / blocksize;
1342d568782ade9b630a6f28f24b1357fc89a6ba8286Eric Sandeen	if (opt_io > blocksize)
1343d568782ade9b630a6f28f24b1357fc89a6ba8286Eric Sandeen		fs_param->s_raid_stripe_width = opt_io / blocksize;
13449ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen
13459ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	rc = blkid_topology_get_alignment_offset(tp);
13469ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeenout:
13479ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	blkid_free_probe(pr);
13489ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	return rc;
13499ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen}
13509ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen#endif
13519ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen
13523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void PRS(int argc, char *argv[])
13533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
1354c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o	int		b, c;
13552d34a25f81d6868387c2b21e8d6d25291c1069dbTheodore Ts'o	int		cluster_size = 0;
135679e62409b3a247e258d9e9206484ed8f193a183eEric Sandeen	char 		*tmp, **cpp;
13574a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		blocksize = 0;
13584a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	int		inode_ratio = 0;
1359932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger	int		inode_size = 0;
13609ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o	unsigned long	flex_bg_size = 0;
1361d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali	double		reserved_ratio = -1.0;
1362bb1158b92ed8a12ab9e9317ca6ddd97bc12447d3Theodore Ts'o	int		lsector_size = 0, psector_size = 0;
13631e6e4c5e5c8313a877881f800f07dcdd7bee5dd2Theodore Ts'o	int		show_version_only = 0;
1364de8f3a76218255e443ba57dec5d74850180fa75dAndreas Dilger	unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1365a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	errcode_t	retval;
13664a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	char *		oldpath = getenv("PATH");
1367c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o	char *		extended_opts = 0;
13684c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o	char *		fs_type = 0;
13694c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o	char *		usage_types = 0;
137002d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos	blk64_t		dev_size;
1371d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	/*
1372d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 * NOTE: A few words about fs_blocks_count and blocksize:
1373d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 *
1374d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 * Initially, blocksize is set to zero, which implies 1024.
1375d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 * If -b is specified, blocksize is updated to the user's value.
1376d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 *
1377d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 * Next, the device size or the user's "blocks" command line argument
1378d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 * is used to set fs_blocks_count; the units are blocksize.
1379d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 *
1380d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 * Later, if blocksize hasn't been set and the profile specifies a
1381d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1382d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 * appropriately.  Note the change in units!
1383d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 *
1384d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1385d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	 */
1386493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	blk64_t		fs_blocks_count = 0;
1387756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o#ifdef __linux__
13884a6005666e1f62442cb55fabd946f454605e340eTheodore Ts'o	struct 		utsname ut;
13892740156bd12747389eaf745529653b26a3a9d73dTheodore Ts'o#endif
139031e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o	long		sysval;
13919dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	int		s_opt = -1, r_opt = -1;
13929dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	char		*fs_features = 0;
13939dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	int		use_bsize;
1394642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o	char		*newpath;
1395642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o	int		pathlen = sizeof(PATH_SET) + 1;
1396642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o
1397642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o	if (oldpath)
1398642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o		pathlen += strlen(oldpath);
1399642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o	newpath = malloc(pathlen);
14009ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim	if (!newpath) {
140145ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		fprintf(stderr, "%s",
140245ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			_("Couldn't allocate memory for new PATH.\n"));
14039ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim		exit(1);
14049ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim	}
1405642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o	strcpy(newpath, PATH_SET);
140614bbcbc31c3aa413b866f49001c517ca9e83d7d8Theodore Ts'o
14073839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/* Update our PATH to include /sbin  */
1408a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	if (oldpath) {
1409a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		strcat (newpath, ":");
1410a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		strcat (newpath, oldpath);
1411642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o	}
1412642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o	putenv (newpath);
14133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
141416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	tmp = getenv("MKE2FS_SYNC");
141516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (tmp)
141616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		sync_kludge = atoi(tmp);
141731e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o
141831e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o	/* Determine the system page size if possible */
141931e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o#ifdef HAVE_SYSCONF
142031e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
142131e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o#define _SC_PAGESIZE _SC_PAGE_SIZE
142231e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o#endif
142331e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o#ifdef _SC_PAGESIZE
142431e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o	sysval = sysconf(_SC_PAGESIZE);
1425aab6fe735d7658cf0e61f2d7bbcffcd3f3d40601Andreas Dilger	if (sysval > 0)
142631e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o		sys_page_size = sysval;
142731e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o#endif /* _SC_PAGESIZE */
142831e29a12d1e22745c74afe47bf172a3c73280dd9Theodore Ts'o#endif /* HAVE_SYSCONF */
14299dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
14309dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
14319dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		config_fn[0] = tmp;
14329dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	profile_set_syntax_err_cb(syntax_err_report);
1433d48bc60459f3552d2f229d8ec041b7eb36787541Theodore Ts'o	retval = profile_init(config_fn, &profile);
1434d48bc60459f3552d2f229d8ec041b7eb36787541Theodore Ts'o	if (retval == ENOENT) {
14359ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim		retval = profile_init(default_files, &profile);
14369ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim		if (retval)
14379ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim			goto profile_error;
14389ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim		retval = profile_set_default(profile, mke2fs_default_profile);
14399ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim		if (retval)
14409ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim			goto profile_error;
14419ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim	} else if (retval) {
14429ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kimprofile_error:
14439ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim		fprintf(stderr, _("Couldn't init profile successfully"
14449ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim				  " (error: %ld).\n"), retval);
14459ec68af7b61bf1ac88f20ae6ed6b1d45417195aaNamhyung Kim		exit(1);
1446d48bc60459f3552d2f229d8ec041b7eb36787541Theodore Ts'o	}
1447efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
14483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	setbuf(stdout, NULL);
14493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	setbuf(stderr, NULL);
1450a6d8302b4873527798a77c1ba3106a04b71dfeacTheodore Ts'o	add_error_table(&et_ext2_error_table);
1451a6d8302b4873527798a77c1ba3106a04b71dfeacTheodore Ts'o	add_error_table(&et_prof_error_table);
14529b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o	memset(&fs_param, 0, sizeof(struct ext2_super_block));
14539b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o	fs_param.s_rev_level = 1;  /* Create revision 1 filesystems now */
1454843049c4dbfe8a0dd4332569eff0ac93529e3324Theodore Ts'o
1455756df353e8ffd010fcca8cef8972cf27006edb6aTheodore Ts'o#ifdef __linux__
145614bbcbc31c3aa413b866f49001c517ca9e83d7d8Theodore Ts'o	if (uname(&ut)) {
145714bbcbc31c3aa413b866f49001c517ca9e83d7d8Theodore Ts'o		perror("uname");
145814bbcbc31c3aa413b866f49001c517ca9e83d7d8Theodore Ts'o		exit(1);
145914bbcbc31c3aa413b866f49001c517ca9e83d7d8Theodore Ts'o	}
1460d99225ecceb580d497c649daa4894fa2e10019adTheodore Ts'o	linux_version_code = parse_version_number(ut.release);
14619dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	if (linux_version_code && linux_version_code < (2*65536 + 2*256))
14629b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o		fs_param.s_rev_level = 0;
146314bbcbc31c3aa413b866f49001c517ca9e83d7d8Theodore Ts'o#endif
14640072f8de528310d82700b1a6a381895b4ca93c10Andreas Dilger
14650072f8de528310d82700b1a6a381895b4ca93c10Andreas Dilger	if (argc && *argv) {
14660072f8de528310d82700b1a6a381895b4ca93c10Andreas Dilger		program_name = get_progname(*argv);
14670072f8de528310d82700b1a6a381895b4ca93c10Andreas Dilger
14680072f8de528310d82700b1a6a381895b4ca93c10Andreas Dilger		/* If called as mkfs.ext3, create a journal inode */
14691a71bd42003d87ff96e3e0e8c293b44c8783e9baTheodore Ts'o		if (!strcmp(program_name, "mkfs.ext3") ||
14701a71bd42003d87ff96e3e0e8c293b44c8783e9baTheodore Ts'o		    !strcmp(program_name, "mke3fs"))
14710072f8de528310d82700b1a6a381895b4ca93c10Andreas Dilger			journal_size = -1;
14720072f8de528310d82700b1a6a381895b4ca93c10Andreas Dilger	}
14730072f8de528310d82700b1a6a381895b4ca93c10Andreas Dilger
14741e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	while ((c = getopt (argc, argv,
147537c8db7b2078d0310e5676404e21cc143d8e4d56Theodore Ts'o		    "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
14763839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		switch (c) {
14773839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'b':
147886a985e74987b08090ecbacf3d02c8d80594c809Theodore Ts'o			blocksize = parse_num_blocks2(optarg, -1);
1479c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o			b = (blocksize > 0) ? blocksize : -blocksize;
1480c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o			if (b < EXT2_MIN_BLOCK_SIZE ||
148186a985e74987b08090ecbacf3d02c8d80594c809Theodore Ts'o			    b > EXT2_MAX_BLOCK_SIZE) {
1482d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				com_err(program_name, 0,
1483f37ab68a26bacf4f5cc7643b8373e40292b7682aTheodore Ts'o					_("invalid block size - %s"), optarg);
14843839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
14853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
1486932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger			if (blocksize > 4096)
1487932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger				fprintf(stderr, _("Warning: blocksize %d not "
1488932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger						  "usable on most systems.\n"),
1489932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger					blocksize);
1490efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o			if (blocksize > 0)
14919b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o				fs_param.s_log_block_size =
1492c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o					int_log2(blocksize >>
1493c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o						 EXT2_MIN_BLOCK_LOG_SIZE);
14943839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
1495b10fd5e84ec7ea0e843b09e7c44784f072b8fdb7Theodore Ts'o		case 'c':	/* Check for bad blocks */
14963ed57c27df0ba0942a19c71bc065c8eec3036567Theodore Ts'o			cflag++;
14973839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
1498c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o		case 'C':
149986a985e74987b08090ecbacf3d02c8d80594c809Theodore Ts'o			cluster_size = parse_num_blocks2(optarg, -1);
15004b20ea2691b3408340036d16e62867d5c52078e3Theodore Ts'o			if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
150186a985e74987b08090ecbacf3d02c8d80594c809Theodore Ts'o			    cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1502d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				com_err(program_name, 0,
150328e2cb9e72cce9e97a4a6f738d28b0f3f31efe69Eric Sandeen					_("invalid cluster size - %s"),
15043839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					optarg);
15053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
15063839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
15073839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
150837c8db7b2078d0310e5676404e21cc143d8e4d56Theodore Ts'o		case 'D':
150937c8db7b2078d0310e5676404e21cc143d8e4d56Theodore Ts'o			direct_io = 1;
151037c8db7b2078d0310e5676404e21cc143d8e4d56Theodore Ts'o			break;
15112a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'R':
151245ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err(program_name, 0, "%s",
15132a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger				_("'-R' is deprecated, use '-E' instead"));
15142a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			/* fallthrough */
15152a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'E':
15162a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			extended_opts = optarg;
15172a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			break;
15182a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'F':
15192a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			force++;
15202a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			break;
15213839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'g':
15229b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o			fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1523f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			if (*tmp) {
152445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				com_err(program_name, 0, "%s",
152545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				_("Illegal number for blocks per group"));
1526f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				exit(1);
1527f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			}
15289b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o			if ((fs_param.s_blocks_per_group % 8) != 0) {
152945ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				com_err(program_name, 0, "%s",
1530d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("blocks per group must be multiple of 8"));
15313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
15323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
15333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
15349ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o		case 'G':
15359ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o			flex_bg_size = strtoul(optarg, &tmp, 0);
15369ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o			if (*tmp) {
153745ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				com_err(program_name, 0, "%s",
15389ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o					_("Illegal number for flex_bg size"));
15399ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o				exit(1);
15409ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o			}
1541d531450cdd9c9c356cce5bdb3634bccff31156f7Theodore Ts'o			if (flex_bg_size < 1 ||
15429ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o			    (flex_bg_size & (flex_bg_size-1)) != 0) {
154345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				com_err(program_name, 0, "%s",
15449ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o					_("flex_bg size must be a power of 2"));
15459ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o				exit(1);
15469ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o			}
15479ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o			break;
15483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'i':
15493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			inode_ratio = strtoul(optarg, &tmp, 0);
1550932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger			if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1551932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger			    inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
15523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			    *tmp) {
1553d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				com_err(program_name, 0,
1554bb145b01cf5fd27d9afe03c3262d0e1a326e7ec1Theodore Ts'o					_("invalid inode ratio %s (min %d/max %d)"),
1555932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger					optarg, EXT2_MIN_BLOCK_SIZE,
1556e447ba3731b8f6849e4df4c8081b73a9c99a23f4Tim Small					EXT2_MAX_BLOCK_SIZE * 1024);
15573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
15583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
15593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
15602a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'I':
15612a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			inode_size = strtoul(optarg, &tmp, 0);
15622a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			if (*tmp) {
15632a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger				com_err(program_name, 0,
15642a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger					_("invalid inode size - %s"), optarg);
15652a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger				exit(1);
15662a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			}
15672a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			break;
15682a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'j':
15692a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			if (!journal_size)
15702a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger				journal_size = -1;
15712a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			break;
1572dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o		case 'J':
1573dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o			parse_journal_opts(optarg);
1574dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o			break;
15755827d2412d0774cb6294521a0ea578975fed07ceEric Sandeen		case 'K':
157645ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			fprintf(stderr, "%s",
157745ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				_("Warning: -K option is deprecated and "
157845ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				  "should not be used anymore. Use "
157945ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				  "\'-E nodiscard\' extended option "
158045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				  "instead!\n"));
15815827d2412d0774cb6294521a0ea578975fed07ceEric Sandeen			discard = 0;
15825827d2412d0774cb6294521a0ea578975fed07ceEric Sandeen			break;
15833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'l':
158480a1806d617fdb14421ac6ac50a640ac1fd303b3Darrick J. Wong			bad_blocks_filename = realloc(bad_blocks_filename,
158580a1806d617fdb14421ac6ac50a640ac1fd303b3Darrick J. Wong						      strlen(optarg) + 1);
1586f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			if (!bad_blocks_filename) {
158745ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				com_err(program_name, ENOMEM, "%s",
1588d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o					_("in malloc for bad_blocks_filename"));
1589f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				exit(1);
1590f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			}
1591f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			strcpy(bad_blocks_filename, optarg);
15923839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
15932a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'L':
15942a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			volume_label = optarg;
15952a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			break;
15963839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		case 'm':
1597ce911145ed0fdd1918ee0c80d407c3e778dc64eeAndreas Dilger			reserved_ratio = strtod(optarg, &tmp);
15988d8224550c1f5b5c77afbf5acd95f73979276a0aTheodore Ts'o			if ( *tmp || reserved_ratio > 50 ||
15998d8224550c1f5b5c77afbf5acd95f73979276a0aTheodore Ts'o			     reserved_ratio < 0) {
16003839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				com_err(program_name, 0,
1601bb145b01cf5fd27d9afe03c3262d0e1a326e7ec1Theodore Ts'o					_("invalid reserved blocks percent - %s"),
16023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					optarg);
16033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				exit(1);
16043839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
16053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			break;
16062a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'M':
16072a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			mount_dir = optarg;
16082a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			break;
160950787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		case 'n':
161050787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			noaction++;
161150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			break;
16122a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'N':
16132a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			num_inodes = strtoul(optarg, &tmp, 0);
16142a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			if (*tmp) {
16152a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger				com_err(program_name, 0,
16162a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger					_("bad num inodes - %s"), optarg);
16172a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger					exit(1);
16182a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			}
16192a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			break;
16201e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		case 'o':
16211e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			creator_os = optarg;
16221e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			break;
16232a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'O':
16242a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			fs_features = optarg;
16252a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			break;
16262524785d5daf9457c01aa4d1a9ee1c9c20dfbfd9Andreas Dilger		case 'q':
16272524785d5daf9457c01aa4d1a9ee1c9c20dfbfd9Andreas Dilger			quiet = 1;
16282524785d5daf9457c01aa4d1a9ee1c9c20dfbfd9Andreas Dilger			break;
16297f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o		case 'r':
16309dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o			r_opt = strtoul(optarg, &tmp, 0);
16312524785d5daf9457c01aa4d1a9ee1c9c20dfbfd9Andreas Dilger			if (*tmp) {
16322524785d5daf9457c01aa4d1a9ee1c9c20dfbfd9Andreas Dilger				com_err(program_name, 0,
16332524785d5daf9457c01aa4d1a9ee1c9c20dfbfd9Andreas Dilger					_("bad revision level - %s"), optarg);
16342524785d5daf9457c01aa4d1a9ee1c9c20dfbfd9Andreas Dilger				exit(1);
16352524785d5daf9457c01aa4d1a9ee1c9c20dfbfd9Andreas Dilger			}
16369dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o			fs_param.s_rev_level = r_opt;
16377f88b04341d88c5df0360d930832c38040303b61Theodore Ts'o			break;
1638b10fd5e84ec7ea0e843b09e7c44784f072b8fdb7Theodore Ts'o		case 's':	/* deprecated */
16399dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o			s_opt = atoi(optarg);
1640521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o			break;
1641f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		case 'S':
1642f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			super_only = 1;
1643f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			break;
16443d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		case 't':
16459f7c3afaef7af307ae9c0fa3648c2e5c61f468b8Eric Sandeen			if (fs_type) {
164645ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				com_err(program_name, 0, "%s",
16479f7c3afaef7af307ae9c0fa3648c2e5c61f468b8Eric Sandeen				    _("The -t option may only be used once"));
16489f7c3afaef7af307ae9c0fa3648c2e5c61f468b8Eric Sandeen				exit(1);
16499f7c3afaef7af307ae9c0fa3648c2e5c61f468b8Eric Sandeen			}
16504c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o			fs_type = strdup(optarg);
165150787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o			break;
16523d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		case 'T':
16539f7c3afaef7af307ae9c0fa3648c2e5c61f468b8Eric Sandeen			if (usage_types) {
165445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				com_err(program_name, 0, "%s",
16559f7c3afaef7af307ae9c0fa3648c2e5c61f468b8Eric Sandeen				    _("The -T option may only be used once"));
16569f7c3afaef7af307ae9c0fa3648c2e5c61f468b8Eric Sandeen				exit(1);
16579f7c3afaef7af307ae9c0fa3648c2e5c61f468b8Eric Sandeen			}
16584c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o			usage_types = strdup(optarg);
16593d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o			break;
1660b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o		case 'U':
1661b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o			fs_uuid = optarg;
1662b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o			break;
16632a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger		case 'v':
16642a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			verbose = 1;
16652a83b3c3741bdc9a1b2af73114c778f4b21a9ab1Andreas Dilger			break;
1666818180cdfcff84b9048ecdc5dc86323f0fefba24Theodore Ts'o		case 'V':
1667818180cdfcff84b9048ecdc5dc86323f0fefba24Theodore Ts'o			/* Print version number and exit */
16681e6e4c5e5c8313a877881f800f07dcdd7bee5dd2Theodore Ts'o			show_version_only++;
16691e6e4c5e5c8313a877881f800f07dcdd7bee5dd2Theodore Ts'o			break;
16703839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		default:
16713839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			usage();
16723839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
16733839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
167455f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	if ((optind == argc) && !show_version_only)
16753839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		usage();
167655f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	device_name = argv[optind++];
1677a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o
16781e6e4c5e5c8313a877881f800f07dcdd7bee5dd2Theodore Ts'o	if (!quiet || show_version_only)
1679efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o		fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
16803879857e86ab5dc83dcb38a93868262f4fb61c4dTheodore Ts'o			 E2FSPROGS_DATE);
16813879857e86ab5dc83dcb38a93868262f4fb61c4dTheodore Ts'o
16821e6e4c5e5c8313a877881f800f07dcdd7bee5dd2Theodore Ts'o	if (show_version_only) {
1683efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o		fprintf(stderr, _("\tUsing %s\n"),
16841e6e4c5e5c8313a877881f800f07dcdd7bee5dd2Theodore Ts'o			error_message(EXT2_ET_BASE));
16851e6e4c5e5c8313a877881f800f07dcdd7bee5dd2Theodore Ts'o		exit(0);
16861e6e4c5e5c8313a877881f800f07dcdd7bee5dd2Theodore Ts'o	}
16871e6e4c5e5c8313a877881f800f07dcdd7bee5dd2Theodore Ts'o
168877dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o	/*
168977dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o	 * If there's no blocksize specified and there is a journal
169077dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o	 * device, use it to figure out the blocksize
169177dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o	 */
1692c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o	if (blocksize <= 0 && journal_device) {
169377dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o		ext2_filsys	jfs;
1694a7ccdff8e128c24564011d760a996496b0a981b3Theodore Ts'o		io_manager	io_ptr;
169577dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o
1696a7ccdff8e128c24564011d760a996496b0a981b3Theodore Ts'o#ifdef CONFIG_TESTIO_DEBUG
1697f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o		if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1698f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o			io_ptr = test_io_manager;
1699f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o			test_io_backing_manager = unix_io_manager;
1700f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o		} else
1701a7ccdff8e128c24564011d760a996496b0a981b3Theodore Ts'o#endif
1702f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o			io_ptr = unix_io_manager;
170377dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o		retval = ext2fs_open(journal_device,
170477dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o				     EXT2_FLAG_JOURNAL_DEV_OK, 0,
1705a7ccdff8e128c24564011d760a996496b0a981b3Theodore Ts'o				     0, io_ptr, &jfs);
170677dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o		if (retval) {
170777dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o			com_err(program_name, retval,
170877dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o				_("while trying to open journal device %s\n"),
170977dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o				journal_device);
171077dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o			exit(1);
171177dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o		}
1712544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o		if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1713c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o			com_err(program_name, 0,
1714ddc32a045bd3bd35968ace2dbe22179470baec0bTheodore Ts'o				_("Journal dev blocksize (%d) smaller than "
1715c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o				  "minimum blocksize %d\n"), jfs->blocksize,
1716c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o				-blocksize);
1717c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o			exit(1);
1718c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o		}
171977dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o		blocksize = jfs->blocksize;
1720f8df04bcc638585ee73cb795153f4456279b9b85Theodore Ts'o		printf(_("Using journal device's blocksize: %d\n"), blocksize);
17219b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o		fs_param.s_log_block_size =
172277dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o			int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
172377dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o		ext2fs_close(jfs);
172477dc4eb04c21664cef8d4fb048ccb952ccef778cTheodore Ts'o	}
1725dc2ec525f5619bf478d2aae1b406fca2ff4b0d21Theodore Ts'o
172655f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	if (optind < argc) {
1727493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o		fs_blocks_count = parse_num_blocks2(argv[optind++],
1728493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o						   fs_param.s_log_block_size);
1729493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o		if (!fs_blocks_count) {
1730792cce309207b44b26fd925b9ca43d9ed5518651Eric Sandeen			com_err(program_name, 0,
17319d92a201deec7bbb1911e6e5ee98abf3c83882d4Theodore Ts'o				_("invalid blocks '%s' on device '%s'"),
1732792cce309207b44b26fd925b9ca43d9ed5518651Eric Sandeen				argv[optind - 1], device_name);
173355f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o			exit(1);
173455f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		}
173555f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	}
173655f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	if (optind < argc)
173755f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		usage();
173855f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
173974becf3c0a065f8d64e07ce4d31f9fe53be91d62Theodore Ts'o	if (!force)
17408ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o		check_plausibility(device_name);
174163985320384bf143eaac9857af424800d9867a1aTheodore Ts'o	check_mount(device_name, force, _("filesystem"));
1742a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o
17439b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	/* Determine the size of the device (if possible) */
1744493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	if (noaction && fs_blocks_count) {
1745493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o		dev_size = fs_blocks_count;
174650787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		retval = 0;
1747493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	} else
174802d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		retval = ext2fs_get_device_size2(device_name,
174902d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos						 EXT2_BLOCK_SIZE(&fs_param),
175002d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos						 &dev_size);
1751efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
1752a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o	if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
175345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(program_name, retval, "%s",
1754d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while trying to determine filesystem size"));
1755a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o		exit(1);
1756a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o	}
1757493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	if (!fs_blocks_count) {
1758a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o		if (retval == EXT2_ET_UNIMPLEMENTED) {
175945ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err(program_name, 0, "%s",
1760d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("Couldn't determine device size; you "
1761a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o				"must specify\nthe size of the "
1762d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				"filesystem\n"));
1763a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o			exit(1);
176426ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o		} else {
176526ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o			if (dev_size == 0) {
176645ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				com_err(program_name, 0, "%s",
176726ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				_("Device size reported to be zero.  "
176826ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "Invalid partition specified, or\n\t"
176926ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "partition table wasn't reread "
177026ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "after running fdisk, due to\n\t"
177126ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "a modified partition being busy "
177226ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "and in use.  You may need to reboot\n\t"
177326ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  "to re-read your partition table.\n"
177426ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				  ));
177526ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o				exit(1);
177626ab531523a319682a099c9a91aa3b8aeab2835cTheodore Ts'o			}
1777493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o			fs_blocks_count = dev_size;
1778493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o			if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1779493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o				fs_blocks_count &= ~((blk64_t) ((sys_page_size /
17804efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson					     EXT2_BLOCK_SIZE(&fs_param))-1));
17814efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson		}
1782493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	} else if (!force && (fs_blocks_count > dev_size)) {
178345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(program_name, 0, "%s",
1784c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o			_("Filesystem larger than apparent device size."));
1785a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o		proceed_question();
1786a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	}
17873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
17884c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o	if (!fs_type)
17894c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o		profile_get_string(profile, "devices", device_name,
17904c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o				   "fs_type", 0, &fs_type);
17914c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o	if (!usage_types)
17924c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o		profile_get_string(profile, "devices", device_name,
17934c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o				   "usage_types", 0, &usage_types);
17944c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o
1795493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	/*
17969b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	 * We have the file system (or device) size, so we can now
17979b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	 * determine the appropriate file system types so the fs can
17989b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	 * be appropriately configured.
17999b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	 */
18009b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
18019b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o				 fs_blocks_count ? fs_blocks_count : dev_size,
18029b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o				 argv[0]);
18039b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	if (!fs_types) {
180445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
18059b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		exit(1);
18069b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	}
18079b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o
18089b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	/* Figure out what features should be enabled */
18099b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o
18109b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	tmp = NULL;
18119b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
18129b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		tmp = get_string_from_profile(fs_types, "base_features",
18139b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		      "sparse_super,filetype,resize_inode,dir_index");
18149b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		edit_feature(tmp, &fs_param.s_feature_compat);
18159b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		free(tmp);
18169b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o
18176a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen		/* And which mount options as well */
18186a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen		tmp = get_string_from_profile(fs_types, "default_mntopts",
18196a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen					      "acl,user_xattr");
18206a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen		edit_mntopts(tmp, &fs_param.s_default_mount_opts);
18216a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen		if (tmp)
18226a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen			free(tmp);
18236a426c97ec2de0968b4cde62e5eab3df605bde33Eric Sandeen
18249b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		for (cpp = fs_types; *cpp; cpp++) {
18259b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o			tmp = NULL;
18269b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o			profile_get_string(profile, "fs_types", *cpp,
18279b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o					   "features", "", &tmp);
18289b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o			if (tmp && *tmp)
18299b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o				edit_feature(tmp, &fs_param.s_feature_compat);
18309b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o			if (tmp)
18319b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o				free(tmp);
18329b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		}
18339b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		tmp = get_string_from_profile(fs_types, "default_features",
18349b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o					      "");
18359b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	}
18369b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	edit_feature(fs_features ? fs_features : tmp,
18379b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		     &fs_param.s_feature_compat);
18389b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	if (tmp)
18399b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o		free(tmp);
18409b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o
1841d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	/* Get the hardware sector sizes, if available */
1842d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1843d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	if (retval) {
184445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(program_name, retval, "%s",
1845d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			_("while trying to determine hardware sector size"));
1846d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		exit(1);
1847d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	}
1848d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1849d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	if (retval) {
185045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(program_name, retval, "%s",
1851d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			_("while trying to determine physical sector size"));
1852d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		exit(1);
1853d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	}
1854d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong
1855d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1856d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	if (tmp != NULL)
1857d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		lsector_size = atoi(tmp);
1858d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1859d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	if (tmp != NULL)
1860d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		psector_size = atoi(tmp);
1861d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong
1862d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	/* Older kernels may not have physical/logical distinction */
1863d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	if (!psector_size)
1864d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		psector_size = lsector_size;
1865d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong
1866d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	if (blocksize <= 0) {
1867d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1868d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong
1869d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		if (use_bsize == -1) {
1870d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			use_bsize = sys_page_size;
1871d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			if ((linux_version_code < (2*65536 + 6*256)) &&
1872d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			    (use_bsize > 4096))
1873d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong				use_bsize = 4096;
1874d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		}
1875d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		if (lsector_size && use_bsize < lsector_size)
1876d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			use_bsize = lsector_size;
1877d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		if ((blocksize < 0) && (use_bsize < (-blocksize)))
1878d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			use_bsize = -blocksize;
1879d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		blocksize = use_bsize;
1880d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		fs_blocks_count /= (blocksize / 1024);
1881d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	} else {
1882d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		if (blocksize < lsector_size) {			/* Impossible */
188345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err(program_name, EINVAL, "%s",
1884d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong				_("while setting blocksize; too small "
1885d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong				  "for device\n"));
1886d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			exit(1);
1887d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		} else if ((blocksize < psector_size) &&
1888d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			   (psector_size <= sys_page_size)) {	/* Suboptimal */
1889d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong			fprintf(stderr, _("Warning: specified blocksize %d is "
1890d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong				"less than device physical sectorsize %d\n"),
1891d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong				blocksize, psector_size);
1892d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		}
1893d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	}
1894d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong
1895d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong	fs_param.s_log_block_size =
1896d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong		int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1897d20403df479efb35966470920c9a5cda581126d0Darrick J. Wong
18989b27e9cc320dd7da24356561b3b12c66f2540432Theodore Ts'o	/*
1899493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	 * We now need to do a sanity check of fs_blocks_count for
1900493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	 * 32-bit vs 64-bit block number support.
1901493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	 */
1902493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	if ((fs_blocks_count > MAX_32_NUM) &&
1903493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	    !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1904493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	    get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1905493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o		fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1906493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o		fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1907493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	}
1908493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	if ((fs_blocks_count > MAX_32_NUM) &&
1909493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	    !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1910493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o		fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1911493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o				  "too big to be expressed\n\t"
1912493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o				  "in 32 bits using a blocksize of %d.\n"),
1913493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o			program_name, fs_blocks_count, device_name,
1914493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o			EXT2_BLOCK_SIZE(&fs_param));
1915493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o		exit(1);
1916493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	}
1917493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o
1918493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1919493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o
1920b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o	if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1921b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o		fs_types[0] = strdup("journal");
1922b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o		fs_types[1] = 0;
1923b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o	}
1924b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o
1925b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o	if (verbose) {
1926b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o		fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1927b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o		print_str_list(fs_types);
1928b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o	}
1929b4d5105b2527a5279cf5b885b957e1e07a53e725Theodore Ts'o
1930efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	if (r_opt == EXT2_GOOD_OLD_REV &&
1931b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o	    (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
193292fb8545b1f9bcf104075ec813ebdf5ddb27e889Theodore Ts'o	     fs_param.s_feature_ro_compat)) {
193345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		fprintf(stderr, "%s", _("Filesystem features not supported "
193445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger					"with revision 0 filesystems\n"));
19359dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		exit(1);
19369dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	}
19379dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
1938b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o	if (s_opt > 0) {
1939b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o		if (r_opt == EXT2_GOOD_OLD_REV) {
194045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			fprintf(stderr, "%s",
194145ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				_("Sparse superblocks not supported "
1942b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o				  "with revision 0 filesystems\n"));
1943b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o			exit(1);
1944b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o		}
19459dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		fs_param.s_feature_ro_compat |=
19469dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o			EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1947b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o	} else if (s_opt == 0)
19489dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		fs_param.s_feature_ro_compat &=
19499dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o			~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
19509dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
1951b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o	if (journal_size != 0) {
1952b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o		if (r_opt == EXT2_GOOD_OLD_REV) {
195345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			fprintf(stderr, "%s", _("Journals not supported with "
195445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger						"revision 0 filesystems\n"));
1955b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o			exit(1);
1956b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o		}
1957efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o		fs_param.s_feature_compat |=
19589dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o			EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1959b290d2dc35411479e09328ef8d9c6220c33f2c49Theodore Ts'o	}
19609dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
1961d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali	/* Get reserved_ratio from profile if not specified on cmd line. */
1962d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali	if (reserved_ratio < 0.0) {
1963d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali		reserved_ratio = get_double_from_profile(
1964d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali					fs_types, "reserved_ratio", 5.0);
1965d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali		if (reserved_ratio > 50 || reserved_ratio < 0) {
1966d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali			com_err(program_name, 0,
1967d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali				_("invalid reserved blocks percent - %lf"),
1968d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali				reserved_ratio);
1969d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali			exit(1);
1970d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali		}
1971d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali	}
1972d3859af33f5bbbc432dedb20e85a5ae62e0fff03Aditya Kali
1973efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	if (fs_param.s_feature_incompat &
19749dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
19759dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		reserved_ratio = 0;
19769dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
19779dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		fs_param.s_feature_compat = 0;
19789dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		fs_param.s_feature_ro_compat = 0;
19799dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o 	}
1980d94cc2eaa68d8bfad342293e73540b867a7b10d9Theodore Ts'o
1981d11f92af3563ce9a235286e6e10d1ebf29a2a4a8Darrick J. Wong	/* Check the user's mkfs options for 64bit */
1982d11f92af3563ce9a235286e6e10d1ebf29a2a4a8Darrick J. Wong	if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1983d11f92af3563ce9a235286e6e10d1ebf29a2a4a8Darrick J. Wong	    !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
198445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		printf("%s", _("Extents MUST be enabled for a 64-bit "
198545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			       "filesystem.  Pass -O extents to rectify.\n"));
1986d11f92af3563ce9a235286e6e10d1ebf29a2a4a8Darrick J. Wong		exit(1);
1987d11f92af3563ce9a235286e6e10d1ebf29a2a4a8Darrick J. Wong	}
1988d11f92af3563ce9a235286e6e10d1ebf29a2a4a8Darrick J. Wong
1989c046ac7f2e4c53e20cf1e909bbe511f91074b396Theodore Ts'o	/* Set first meta blockgroup via an environment variable */
1990c046ac7f2e4c53e20cf1e909bbe511f91074b396Theodore Ts'o	/* (this is mostly for debugging purposes) */
19919b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o	if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1992c046ac7f2e4c53e20cf1e909bbe511f91074b396Theodore Ts'o	    ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
19939b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o		fs_param.s_first_meta_bg = atoi(tmp);
1994c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o	if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
19952d34a25f81d6868387c2b21e8d6d25291c1069dbTheodore Ts'o		if (!cluster_size)
19962d34a25f81d6868387c2b21e8d6d25291c1069dbTheodore Ts'o			cluster_size = get_int_from_profile(fs_types,
19972d34a25f81d6868387c2b21e8d6d25291c1069dbTheodore Ts'o							    "cluster_size",
1998b12a0bc30152f85ccafab159fcc2f50fe6ce2551Theodore Ts'o							    blocksize*16);
19992d34a25f81d6868387c2b21e8d6d25291c1069dbTheodore Ts'o		fs_param.s_log_cluster_size =
20002d34a25f81d6868387c2b21e8d6d25291c1069dbTheodore Ts'o			int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
20014b20ea2691b3408340036d16e62867d5c52078e3Theodore Ts'o		if (fs_param.s_log_cluster_size &&
20024b20ea2691b3408340036d16e62867d5c52078e3Theodore Ts'o		    fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
200345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err(program_name, 0, "%s",
20044b20ea2691b3408340036d16e62867d5c52078e3Theodore Ts'o				_("The cluster size may not be "
20054b20ea2691b3408340036d16e62867d5c52078e3Theodore Ts'o				  "smaller than the block size.\n"));
20064b20ea2691b3408340036d16e62867d5c52078e3Theodore Ts'o			exit(1);
20074b20ea2691b3408340036d16e62867d5c52078e3Theodore Ts'o		}
20089cf69bb825c2995352502f48ea64d1be1d263995Zheng Liu	} else if (cluster_size) {
200945ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(program_name, 0, "%s",
20109cf69bb825c2995352502f48ea64d1be1d263995Zheng Liu			_("specifying a cluster size requires the "
20119cf69bb825c2995352502f48ea64d1be1d263995Zheng Liu			  "bigalloc feature"));
20129cf69bb825c2995352502f48ea64d1be1d263995Zheng Liu		exit(1);
2013c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o	} else
2014c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o		fs_param.s_log_cluster_size = fs_param.s_log_block_size;
2015c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o
20169dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	if (inode_ratio == 0) {
20173d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
20183d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o						   8192);
20199dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		if (inode_ratio < blocksize)
20209dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o			inode_ratio = blocksize;
2021c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o		if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
2022c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o			inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
20239dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	}
20249dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o
20259ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
20261599b470fb881313c1d44594f05d578c43825312Theodore Ts'o	retval = get_device_geometry(device_name, &fs_param, psector_size);
20279ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	if (retval < 0) {
20289ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen		fprintf(stderr,
202913b0b1231ed28aac75ba336de7a8cb3b4611ce68Eric Sandeen			_("warning: Unable to get device geometry for %s\n"),
20309ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen			device_name);
20319ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	} else if (retval) {
20329ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen		printf(_("%s alignment is offset by %lu bytes.\n"),
20339ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen		       device_name, retval);
20349ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen		printf(_("This may result in very poor performance, "
20359ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen			  "(re)-partitioning suggested.\n"));
20369ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen	}
20379ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen#endif
20389ed8e5fec226aa53634ed95cbeac736d90a518e5Eric Sandeen
203965c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o	num_backups = get_int_from_profile(fs_types, "num_backup_sb", 2);
204065c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o
20419b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o	blocksize = EXT2_BLOCK_SIZE(&fs_param);
2042a4396e9d5d1351c53212bf7ff10f97edeb576438Theodore Ts'o
204349cdefd3312c5ac81ec5d233d62441281ad26e55Theodore Ts'o	/*
204449cdefd3312c5ac81ec5d233d62441281ad26e55Theodore Ts'o	 * Initialize s_desc_size so that the parse_extended_opts()
204549cdefd3312c5ac81ec5d233d62441281ad26e55Theodore Ts'o	 * can correctly handle "-E resize=NNN" if the 64-bit option
204649cdefd3312c5ac81ec5d233d62441281ad26e55Theodore Ts'o	 * is set.
204749cdefd3312c5ac81ec5d233d62441281ad26e55Theodore Ts'o	 */
204849cdefd3312c5ac81ec5d233d62441281ad26e55Theodore Ts'o	if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
204949cdefd3312c5ac81ec5d233d62441281ad26e55Theodore Ts'o		fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
205049cdefd3312c5ac81ec5d233d62441281ad26e55Theodore Ts'o
205145792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev	/* This check should happen beyond the last assignment to blocksize */
205245792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev	if (blocksize > sys_page_size) {
205345792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev		if (!force) {
205445792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev			com_err(program_name, 0,
205545792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev				_("%d-byte blocks too big for system (max %d)"),
205645792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev				blocksize, sys_page_size);
205745792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev			proceed_question();
205845792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev		}
205945792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev		fprintf(stderr, _("Warning: %d-byte blocks too big for system "
206045792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev				  "(max %d), forced to continue\n"),
206145792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev			blocksize, sys_page_size);
206245792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev	}
206345792c127645fdb4b665b74dff01748e5db789c5Yury V. Zaytsev
2064210fd2c7072f2f150e2bb10b07c49472bb78e366Theodore Ts'o	lazy_itable_init = 0;
2065210fd2c7072f2f150e2bb10b07c49472bb78e366Theodore Ts'o	if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
2066210fd2c7072f2f150e2bb10b07c49472bb78e366Theodore Ts'o		lazy_itable_init = 1;
2067210fd2c7072f2f150e2bb10b07c49472bb78e366Theodore Ts'o
2068efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	lazy_itable_init = get_bool_from_profile(fs_types,
2069210fd2c7072f2f150e2bb10b07c49472bb78e366Theodore Ts'o						 "lazy_itable_init",
2070210fd2c7072f2f150e2bb10b07c49472bb78e366Theodore Ts'o						 lazy_itable_init);
20717fe5ff3c1e06c4705a7a709a7ed34f02c5a02fd8Lukas Czerner	discard = get_bool_from_profile(fs_types, "discard" , discard);
20726c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger	journal_flags |= get_bool_from_profile(fs_types,
20736c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger					       "lazy_journal_init", 0) ?
20746c54689fadc3fe0b0bcae2cc93946cb7a28b9f15Andreas Dilger					       EXT2_MKJOURNAL_LAZYINIT : 0;
2075304e11c2c1878ef392095d10bf5e93f7ea7131e4Theodore Ts'o	journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
2076efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
2077b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o	if (!journal_location_string)
2078b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o		journal_location_string = get_string_from_profile(fs_types,
2079b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o						"journal_location", "");
2080b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o	if ((journal_location == ~0ULL) && journal_location_string &&
2081b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o	    *journal_location_string)
2082b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o		journal_location = parse_num_blocks2(journal_location_string,
2083b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o						fs_param.s_log_block_size);
2084b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o	free(journal_location_string);
2085b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o
20863c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	packed_meta_blocks = get_bool_from_profile(fs_types,
20873c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o						   "packed_meta_blocks", 0);
20883c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	if (packed_meta_blocks)
20893c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		journal_location = 0;
20903c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o
20912d36358d665c87cbfb00a2d700690fa379f24956Theodore Ts'o	/* Get options from profile */
20922d36358d665c87cbfb00a2d700690fa379f24956Theodore Ts'o	for (cpp = fs_types; *cpp; cpp++) {
20932d36358d665c87cbfb00a2d700690fa379f24956Theodore Ts'o		tmp = NULL;
20942d36358d665c87cbfb00a2d700690fa379f24956Theodore Ts'o		profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
20952d36358d665c87cbfb00a2d700690fa379f24956Theodore Ts'o			if (tmp && *tmp)
20962d36358d665c87cbfb00a2d700690fa379f24956Theodore Ts'o				parse_extended_opts(&fs_param, tmp);
209745e338f5332a54295893dba2e32cc093d1316f60Jim Meyering			free(tmp);
20982d36358d665c87cbfb00a2d700690fa379f24956Theodore Ts'o	}
20992d36358d665c87cbfb00a2d700690fa379f24956Theodore Ts'o
2100c6a44136b9b82625e7b36a574ddb3a08b6727e97Theodore Ts'o	if (extended_opts)
21019b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o		parse_extended_opts(&fs_param, extended_opts);
2102d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o
2103f45011df2bea466209a4b2701f2770b24518bbacZheng Liu	/* Can't support bigalloc feature without extents feature */
2104f45011df2bea466209a4b2701f2770b24518bbacZheng Liu	if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2105f45011df2bea466209a4b2701f2770b24518bbacZheng Liu	    !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
210645ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(program_name, 0, "%s",
2107f45011df2bea466209a4b2701f2770b24518bbacZheng Liu			_("Can't support bigalloc feature without "
2108f45011df2bea466209a4b2701f2770b24518bbacZheng Liu			  "extents feature"));
2109f45011df2bea466209a4b2701f2770b24518bbacZheng Liu		exit(1);
2110f45011df2bea466209a4b2701f2770b24518bbacZheng Liu	}
2111f45011df2bea466209a4b2701f2770b24518bbacZheng Liu
2112cecfb4c04227dd5803c24b311d92a80e91b7b380Theodore Ts'o	if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2113cecfb4c04227dd5803c24b311d92a80e91b7b380Theodore Ts'o	    (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
211445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		fprintf(stderr, "%s", _("The resize_inode and meta_bg "
211545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger					"features are not compatible.\n"
211645ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger					"They can not be both enabled "
211745ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger					"simultaneously.\n"));
2118cecfb4c04227dd5803c24b311d92a80e91b7b380Theodore Ts'o		exit(1);
2119cecfb4c04227dd5803c24b311d92a80e91b7b380Theodore Ts'o	}
2120cecfb4c04227dd5803c24b311d92a80e91b7b380Theodore Ts'o
2121447da244eb31f11516d991891f985a2770080033Theodore Ts'o	if (!quiet &&
2122447da244eb31f11516d991891f985a2770080033Theodore Ts'o	    (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
212345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
212445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				  "still under development\n"
2125a713a7fe5a929c0e1307e6bded8ac66805c7e748Theodore Ts'o				  "See https://ext4.wiki.kernel.org/"
2126a713a7fe5a929c0e1307e6bded8ac66805c7e748Theodore Ts'o				  "index.php/Bigalloc for more information\n\n"));
2127a713a7fe5a929c0e1307e6bded8ac66805c7e748Theodore Ts'o
2128447da244eb31f11516d991891f985a2770080033Theodore Ts'o	if (!quiet &&
2129447da244eb31f11516d991891f985a2770080033Theodore Ts'o	    (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
213045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		fprintf(stderr, "%s", _("\nWarning: the quota feature is "
213145ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				  "still under development\n"
2132a713a7fe5a929c0e1307e6bded8ac66805c7e748Theodore Ts'o				  "See https://ext4.wiki.kernel.org/"
2133a713a7fe5a929c0e1307e6bded8ac66805c7e748Theodore Ts'o				  "index.php/Quota for more information\n\n"));
2134a713a7fe5a929c0e1307e6bded8ac66805c7e748Theodore Ts'o
2135d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o	/* Since sparse_super is the default, we would only have a problem
2136d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o	 * here if it was explicitly disabled.
2137d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o	 */
21389b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o	if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
21399b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o	    !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
214045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(program_name, 0, "%s",
2141d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o			_("reserved online resize blocks not supported "
2142d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o			  "on non-sparse filesystem"));
2143d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o		exit(1);
2144d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o	}
2145d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o
21469b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o	if (fs_param.s_blocks_per_group) {
21479b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o		if (fs_param.s_blocks_per_group < 256 ||
21489b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o		    fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
214945ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err(program_name, 0, "%s",
2150d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o				_("blocks per group count out of range"));
2151521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o			exit(1);
2152521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o		}
2153521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o	}
2154521e36857227b21e7ab47b0a97f788d2af9f9717Theodore Ts'o
21552a3b1c6ea29db5d5dfe4602d0b0adfda88068de6Theodore Ts'o	/*
21562a3b1c6ea29db5d5dfe4602d0b0adfda88068de6Theodore Ts'o	 * If the bigalloc feature is enabled, then the -g option will
21572a3b1c6ea29db5d5dfe4602d0b0adfda88068de6Theodore Ts'o	 * specify the number of clusters per group.
21582a3b1c6ea29db5d5dfe4602d0b0adfda88068de6Theodore Ts'o	 */
21592a3b1c6ea29db5d5dfe4602d0b0adfda88068de6Theodore Ts'o	if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
21602a3b1c6ea29db5d5dfe4602d0b0adfda88068de6Theodore Ts'o		fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
21612a3b1c6ea29db5d5dfe4602d0b0adfda88068de6Theodore Ts'o		fs_param.s_blocks_per_group = 0;
21622a3b1c6ea29db5d5dfe4602d0b0adfda88068de6Theodore Ts'o	}
21632a3b1c6ea29db5d5dfe4602d0b0adfda88068de6Theodore Ts'o
21643d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	if (inode_size == 0)
21653d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o		inode_size = get_int_from_profile(fs_types, "inode_size", 0);
21669ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o	if (!flex_bg_size && (fs_param.s_feature_incompat &
21679ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o			      EXT4_FEATURE_INCOMPAT_FLEX_BG))
21689ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o		flex_bg_size = get_int_from_profile(fs_types,
21699ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o						    "flex_bg_size", 16);
21709ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o	if (flex_bg_size) {
21719ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o		if (!(fs_param.s_feature_incompat &
21729ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o		      EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
217345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err(program_name, 0, "%s",
21749ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o				_("Flex_bg feature not enabled, so "
21759ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o				  "flex_bg size may not be specified"));
21769ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o			exit(1);
21779ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o		}
21789ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o		fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
21799ba400027fb00ce43d14673346bbfb6f5719985aTheodore Ts'o	}
2180067911ae734bb5fef7c5780a639533847b5b578cAndreas Dilger
2181067911ae734bb5fef7c5780a639533847b5b578cAndreas Dilger	if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2182932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger		if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
21839b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o		    inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2184932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger		    inode_size & (inode_size - 1)) {
2185932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger			com_err(program_name, 0,
2186bb145b01cf5fd27d9afe03c3262d0e1a326e7ec1Theodore Ts'o				_("invalid inode size %d (min %d/max %d)"),
2187932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger				inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2188c5290fae3937cc83de8a551db9686d1e1964e378Theodore Ts'o				blocksize);
2189932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger			exit(1);
2190932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger		}
21919b9a780f5a5823865f62f0c9fd194d262f63a06fTheodore Ts'o		fs_param.s_inode_size = inode_size;
2192932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger	}
2193932a489cdf6bc83d69e59d3f8e0a57b733799ce1Andreas Dilger
2194f335864338a6fbce8134a445ffdd0cdeb3f3f1bcEric Sandeen	/* Make sure number of inodes specified will fit in 32 bits */
2195f335864338a6fbce8134a445ffdd0cdeb3f3f1bcEric Sandeen	if (num_inodes == 0) {
2196de8f3a76218255e443ba57dec5d74850180fa75dAndreas Dilger		unsigned long long n;
21974efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson		n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2198493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o		if (n > MAX_32_NUM) {
219902d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos			if (fs_param.s_feature_incompat &
220002d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos			    EXT4_FEATURE_INCOMPAT_64BIT)
2201493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o				num_inodes = MAX_32_NUM;
220202d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos			else {
220302d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos				com_err(program_name, 0,
220484888e55438c7aa70d5866c203c06aea7576542eTheodore Ts'o					_("too many inodes (%llu), raise "
220502d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos					  "inode ratio?"), n);
220602d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos				exit(1);
220702d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos			}
2208f335864338a6fbce8134a445ffdd0cdeb3f3f1bcEric Sandeen		}
2209493024ea1d74e4cb48aac3a24111f5c8da343e9fTheodore Ts'o	} else if (num_inodes > MAX_32_NUM) {
2210f335864338a6fbce8134a445ffdd0cdeb3f3f1bcEric Sandeen		com_err(program_name, 0,
2211f335864338a6fbce8134a445ffdd0cdeb3f3f1bcEric Sandeen			_("too many inodes (%llu), specify < 2^32 inodes"),
2212de8f3a76218255e443ba57dec5d74850180fa75dAndreas Dilger			  num_inodes);
2213f335864338a6fbce8134a445ffdd0cdeb3f3f1bcEric Sandeen		exit(1);
2214f335864338a6fbce8134a445ffdd0cdeb3f3f1bcEric Sandeen	}
22153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
22163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * Calculate number of inodes based on the inode ratio
22173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
2218efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	fs_param.s_inodes_count = num_inodes ? num_inodes :
22194efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson		(ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
22203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2221577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	if ((((unsigned long long)fs_param.s_inodes_count) *
2222dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger	     (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
22234efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson	    ((ext2fs_blocks_count(&fs_param)) *
2224dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger	     EXT2_BLOCK_SIZE(&fs_param))) {
2225dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger		com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2226dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger					  "(%u) too big for a\n\t"
22274efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson					  "filesystem with %llu blocks, "
2228dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger					  "specify higher inode_ratio (-i)\n\t"
2229dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger					  "or lower inode count (-N).\n"),
2230dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger			inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2231efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o			fs_param.s_inodes_count,
22324efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson			(unsigned long long) ext2fs_blocks_count(&fs_param));
2233dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger		exit(1);
2234dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger	}
2235dcf7b091c3efb2e6218a1f139ef2823db9d7c4bbAndreas Dilger
22363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
22373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * Calculate number of blocks to reserve
22383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
22394efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson	ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
22404efbac6fed75c29d3d5f1b676b932754653a2ac5Valerie Aurora Henson				  ext2fs_blocks_count(&fs_param) / 100.0);
22414c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o
224265c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o	if (fs_param.s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
224365c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o		if (num_backups >= 1)
224465c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o			fs_param.s_backup_bgs[0] = 1;
224565c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o		if (num_backups >= 2)
224665c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o			fs_param.s_backup_bgs[1] = ~0;
224765c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o	}
224865c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o
22494c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o	free(fs_type);
22504c2b28ab67dec1e1c4173a0e7e785dae09d161d2Theodore Ts'o	free(usage_types);
22513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2252d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o
2253b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.Vstatic int should_do_undo(const char *name)
2254b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V{
2255b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	errcode_t retval;
2256b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	io_channel channel;
2257b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	__u16	s_magic;
2258b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	struct ext2_super_block super;
2259b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	io_manager manager = unix_io_manager;
2260b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	int csum_flag, force_undo;
2261b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2262b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2263b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V					       EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2264b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2265b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	if (!force_undo && (!csum_flag || !lazy_itable_init))
2266b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		return 0;
2267b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2268b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	retval = manager->open(name, IO_FLAG_EXCLUSIVE,  &channel);
2269b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	if (retval) {
2270b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		/*
2271b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		 * We don't handle error cases instead we
2272b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		 * declare that the file system doesn't exist
2273b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		 * and let the rest of mke2fs take care of
2274b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		 * error
2275b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		 */
2276b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		retval = 0;
2277b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		goto open_err_out;
2278b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	}
2279b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2280b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
228124a117abd0340d247befbf7687ffb70547fdf218Valerie Aurora Henson	retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2282b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	if (retval) {
2283b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		retval = 0;
2284b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		goto err_out;
2285b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	}
2286b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2287b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V#if defined(WORDS_BIGENDIAN)
2288b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	s_magic = ext2fs_swab16(super.s_magic);
2289b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V#else
2290b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	s_magic = super.s_magic;
2291b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V#endif
2292b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2293b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	if (s_magic == EXT2_SUPER_MAGIC)
2294b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		retval = 1;
2295b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2296b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.Verr_out:
2297b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	io_channel_close(channel);
2298b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2299b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.Vopen_err_out:
2300b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2301b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	return retval;
2302b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V}
2303b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2304b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.Vstatic int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2305b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V{
23068a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o	errcode_t retval = ENOMEM;
230730295f16a95b873965d1650b24fb6f5b82bde675Eric Sandeen	char *tdb_dir = NULL, *tdb_file = NULL;
2308577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	char *dev_name, *tmp_name;
230930295f16a95b873965d1650b24fb6f5b82bde675Eric Sandeen	int free_tdb_dir = 0;
2310b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2311b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	/*
2312b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	 * Configuration via a conf file would be
2313b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	 * nice
2314b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	 */
2315b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
231630295f16a95b873965d1650b24fb6f5b82bde675Eric Sandeen	if (!tdb_dir) {
2317b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		profile_get_string(profile, "defaults",
2318b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V				   "undo_dir", 0, "/var/lib/e2fsprogs",
2319b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V				   &tdb_dir);
232030295f16a95b873965d1650b24fb6f5b82bde675Eric Sandeen		free_tdb_dir = 1;
232130295f16a95b873965d1650b24fb6f5b82bde675Eric Sandeen	}
2322b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2323b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
232480a1806d617fdb14421ac6ac50a640ac1fd303b3Darrick J. Wong	    access(tdb_dir, W_OK)) {
232580a1806d617fdb14421ac6ac50a640ac1fd303b3Darrick J. Wong		if (free_tdb_dir)
232680a1806d617fdb14421ac6ac50a640ac1fd303b3Darrick J. Wong			free(tdb_dir);
2327b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		return 0;
232880a1806d617fdb14421ac6ac50a640ac1fd303b3Darrick J. Wong	}
2329b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2330b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	tmp_name = strdup(name);
23318a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o	if (!tmp_name)
23328a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o		goto errout;
2333577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	dev_name = basename(tmp_name);
2334577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
23358a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o	if (!tdb_file) {
23368a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o		free(tmp_name);
23378a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o		goto errout;
23388a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o	}
2339577c773a60fa7d167f5aaf717fadd20fe507fb97Theodore Ts'o	sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
23408a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o	free(tmp_name);
2341b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2342250bf9a7032fa13dad8eede903d17f3ec2e917deTheodore Ts'o	if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2343250bf9a7032fa13dad8eede903d17f3ec2e917deTheodore Ts'o		retval = errno;
2344250bf9a7032fa13dad8eede903d17f3ec2e917deTheodore Ts'o		goto errout;
2345b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	}
2346b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
2347b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	set_undo_io_backing_manager(*io_ptr);
2348b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	*io_ptr = undo_io_manager;
23498a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o	retval = set_undo_io_backup_file(tdb_file);
23508a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o	if (retval)
23518a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o		goto errout;
2352b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	printf(_("Overwriting existing filesystem; this can be undone "
2353b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		 "using the command:\n"
2354b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		 "    e2undo %s %s\n\n"), tdb_file, name);
235579e62409b3a247e258d9e9206484ed8f193a183eEric Sandeen
235630295f16a95b873965d1650b24fb6f5b82bde675Eric Sandeen	if (free_tdb_dir)
235730295f16a95b873965d1650b24fb6f5b82bde675Eric Sandeen		free(tdb_dir);
2358f203bbdbec396e3279bf249ae9be96e6b00bf6f2Theodore Ts'o	free(tdb_file);
23598a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o	return 0;
23608a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o
23618a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'oerrout:
236230295f16a95b873965d1650b24fb6f5b82bde675Eric Sandeen	if (free_tdb_dir)
236330295f16a95b873965d1650b24fb6f5b82bde675Eric Sandeen		free(tdb_dir);
23648a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o	free(tdb_file);
236545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger	com_err(program_name, retval, "%s",
23668a1cf3c243cf8447d814c4e3432fcf6282aa40e9Theodore Ts'o		_("while trying to setup undo file\n"));
2367b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	return retval;
2368b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V}
2369b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
23707d9e31655fca48e9d6c2647ad443124113508b73Lukas Czernerstatic int mke2fs_discard_device(ext2_filsys fs)
23717d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner{
23727d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	struct ext2fs_numeric_progress_struct progress;
23737d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	blk64_t blocks = ext2fs_blocks_count(fs->super);
23747d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	blk64_t count = DISCARD_STEP_MB;
2375d2bfdc7ff15ce7b6b40c087021528ce190ef43c3Lukas Czerner	blk64_t cur;
23767d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	int retval = 0;
23777d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner
2378d2bfdc7ff15ce7b6b40c087021528ce190ef43c3Lukas Czerner	/*
2379d2bfdc7ff15ce7b6b40c087021528ce190ef43c3Lukas Czerner	 * Let's try if discard really works on the device, so
2380d2bfdc7ff15ce7b6b40c087021528ce190ef43c3Lukas Czerner	 * we do not print numeric progress resulting in failure
2381d2bfdc7ff15ce7b6b40c087021528ce190ef43c3Lukas Czerner	 * afterwards.
2382d2bfdc7ff15ce7b6b40c087021528ce190ef43c3Lukas Czerner	 */
2383d2bfdc7ff15ce7b6b40c087021528ce190ef43c3Lukas Czerner	retval = io_channel_discard(fs->io, 0, fs->blocksize);
2384aa07cb79b0a38d9d8407c5631624ef8534bdde3fTheodore Ts'o	if (retval)
2385aa07cb79b0a38d9d8407c5631624ef8534bdde3fTheodore Ts'o		return retval;
2386d2bfdc7ff15ce7b6b40c087021528ce190ef43c3Lukas Czerner	cur = fs->blocksize;
2387aa07cb79b0a38d9d8407c5631624ef8534bdde3fTheodore Ts'o
23887d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	count *= (1024 * 1024);
23897d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	count /= fs->blocksize;
23907d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner
23917d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	ext2fs_numeric_progress_init(fs, &progress,
23927d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner				     _("Discarding device blocks: "),
23937d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner				     blocks);
23947d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	while (cur < blocks) {
2395c498cb11d3e72e41af760bd882675f44db8b77e7Theodore Ts'o		ext2fs_numeric_progress_update(fs, &progress, cur);
23967d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner
23977d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner		if (cur + count > blocks)
23987d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner			count = blocks - cur;
23997d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner
2400aa07cb79b0a38d9d8407c5631624ef8534bdde3fTheodore Ts'o		retval = io_channel_discard(fs->io, cur, count);
24017d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner		if (retval)
24027d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner			break;
24037d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner		cur += count;
24047d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	}
24057d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner
24067d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	if (retval) {
24077d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner		ext2fs_numeric_progress_close(fs, &progress,
24087d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner				      _("failed - "));
24097d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner		if (!quiet)
24107d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner			printf("%s\n",error_message(retval));
24117d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	} else
24127d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner		ext2fs_numeric_progress_close(fs, &progress,
24137d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner				      _("done                            \n"));
24147d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner
24157d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner	return retval;
24167d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner}
24177d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner
241896367ad3bc849220651b20f41340b48e07e82b04Andreas Dilgerstatic void fix_cluster_bg_counts(ext2_filsys fs)
2419c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o{
24206e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	blk64_t		block, num_blocks, last_block, next;
24216e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	blk64_t		tot_free = 0;
24226e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	errcode_t	retval;
24236e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	dgrp_t		group = 0;
24246e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	int		grp_free = 0;
24256e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o
24266e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	num_blocks = ext2fs_blocks_count(fs->super);
24276e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	last_block = ext2fs_group_last_block2(fs, group);
24286e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	block = fs->super->s_first_data_block;
24296e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	while (block < num_blocks) {
24306e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
24316e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o						block, last_block, &next);
24326e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		if (retval == 0)
24336e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o			block = next;
24346e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		else {
24356e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o			block = last_block + 1;
24366e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o			goto next_bg;
2437c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o		}
24386e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o
24396e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		retval = ext2fs_find_first_set_block_bitmap2(fs->block_map,
24406e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o						block, last_block, &next);
24416e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		if (retval)
24426e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o			next = last_block + 1;
24436e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		grp_free += EXT2FS_NUM_B2C(fs, next - block);
24446e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		tot_free += next - block;
24456e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		block = next;
24466e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o
24476e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		if (block > last_block) {
24486e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o		next_bg:
2449c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o			ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2450c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o			ext2fs_group_desc_csum_set(fs, group);
2451c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o			grp_free = 0;
2452c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o			group++;
24536e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o			last_block = ext2fs_group_last_block2(fs, group);
2454c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o		}
2455c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o	}
24566e49e6a3e5ded98948e057037e617c8873d3adecTheodore Ts'o	ext2fs_free_blocks_count_set(fs->super, tot_free);
2457c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o}
2458c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o
24591f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kalistatic int create_quota_inodes(ext2_filsys fs)
24601f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali{
24611f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali	quota_ctx_t qctx;
24621f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali
2463a86d55da8bf41335aa2fc5ec16ca63859d918e81Aditya Kali	quota_init_context(&qctx, fs, -1);
2464a86d55da8bf41335aa2fc5ec16ca63859d918e81Aditya Kali	quota_compute_usage(qctx);
2465d678fef0d78fb3c5546cd9398698ddc0106618f6Aditya Kali	quota_write_inode(qctx, quotatype);
2466a86d55da8bf41335aa2fc5ec16ca63859d918e81Aditya Kali	quota_release_context(&qctx);
24671f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali
2468a86d55da8bf41335aa2fc5ec16ca63859d918e81Aditya Kali	return 0;
24691f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali}
24701f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali
24713839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oint main (int argc, char *argv[])
24723839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
24733839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	errcode_t	retval = 0;
24743839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	ext2_filsys	fs;
24753839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	badblocks_list	bb_list = 0;
2476d4e0b1c6f5aa8c6a248d9149ed5634a310952411Theodore Ts'o	unsigned int	journal_blocks;
247714b283ae565930144ef5ace12483d602cc3e7539Theodore Ts'o	unsigned int	i, checkinterval;
247814b283ae565930144ef5ace12483d602cc3e7539Theodore Ts'o	int		max_mnt_count;
2479d5f57d9553d4c855e282dbd3edf1ad85ab716d33Theodore Ts'o	int		val, hash_alg;
2480463e732777d970bb8f96c7e0885c7393141c0d2dValerie Aurora Henson	int		flags;
2481463e732777d970bb8f96c7e0885c7393141c0d2dValerie Aurora Henson	int		old_bitmaps;
2482a7ccdff8e128c24564011d760a996496b0a981b3Theodore Ts'o	io_manager	io_ptr;
248388ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o	char		opt_string[40];
2484d5f57d9553d4c855e282dbd3edf1ad85ab716d33Theodore Ts'o	char		*hash_alg_str;
24856fcd6f84c235f4bf2bd9770f172837da9982eb6eEric Sandeen	int		itable_zeroed = 0;
2486d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o
2487d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#ifdef ENABLE_NLS
2488d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	setlocale(LC_MESSAGES, "");
248914308a5398984842e808faa3ff2dd6a1c52d90bdTheodore Ts'o	setlocale(LC_CTYPE, "");
2490d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2491d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	textdomain(NLS_CAT_NAME);
24929d4507c5b61007df968638623aa1b4c47dae6cf9Theodore Ts'o	set_com_err_gettext(gettext);
2493d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#endif
24943839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	PRS(argc, argv);
24953839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2496a7ccdff8e128c24564011d760a996496b0a981b3Theodore Ts'o#ifdef CONFIG_TESTIO_DEBUG
2497f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o	if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2498f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o		io_ptr = test_io_manager;
2499f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o		test_io_backing_manager = unix_io_manager;
2500f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o	} else
2501a7ccdff8e128c24564011d760a996496b0a981b3Theodore Ts'o#endif
2502f38cf3cb34addaa53d1f855d7607b151082a4229Theodore Ts'o		io_ptr = unix_io_manager;
2503a7ccdff8e128c24564011d760a996496b0a981b3Theodore Ts'o
2504b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	if (should_do_undo(device_name)) {
2505b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		retval = mke2fs_setup_tdb(device_name, &io_ptr);
2506b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		if (retval)
2507b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V			exit(1);
2508b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V	}
2509b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V
25103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	/*
25113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 * Initialize the superblock....
25123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	 */
2513463e732777d970bb8f96c7e0885c7393141c0d2dValerie Aurora Henson	flags = EXT2_FLAG_EXCLUSIVE;
251437c8db7b2078d0310e5676404e21cc143d8e4d56Theodore Ts'o	if (direct_io)
251537c8db7b2078d0310e5676404e21cc143d8e4d56Theodore Ts'o		flags |= EXT2_FLAG_DIRECT_IO;
2516463e732777d970bb8f96c7e0885c7393141c0d2dValerie Aurora Henson	profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2517463e732777d970bb8f96c7e0885c7393141c0d2dValerie Aurora Henson			    &old_bitmaps);
2518463e732777d970bb8f96c7e0885c7393141c0d2dValerie Aurora Henson	if (!old_bitmaps)
2519463e732777d970bb8f96c7e0885c7393141c0d2dValerie Aurora Henson		flags |= EXT2_FLAG_64BITS;
252095fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	/*
252195fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	 * By default, we print how many inode tables or block groups
252295fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	 * or whatever we've written so far.  The quiet flag disables
252395fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	 * this, along with a lot of other output.
252495fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	 */
252595fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	if (!quiet)
252695fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson		flags |= EXT2_FLAG_PRINT_PROGRESS;
2527463e732777d970bb8f96c7e0885c7393141c0d2dValerie Aurora Henson	retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
25283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (retval) {
252945ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(device_name, retval, "%s",
253045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			_("while setting up superblock"));
25313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit(1);
25323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
253395fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson
2534fd73a1d254556fce65d03e74ee5a97304f496caaAshish Sangwan	/* Calculate journal blocks */
2535fd73a1d254556fce65d03e74ee5a97304f496caaAshish Sangwan	if (!journal_device && ((journal_size) ||
2536fd73a1d254556fce65d03e74ee5a97304f496caaAshish Sangwan		(fs_param.s_feature_compat &
2537fd73a1d254556fce65d03e74ee5a97304f496caaAshish Sangwan		 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2538fd73a1d254556fce65d03e74ee5a97304f496caaAshish Sangwan		journal_blocks = figure_journal_size(journal_size, fs);
2539fd73a1d254556fce65d03e74ee5a97304f496caaAshish Sangwan
25405827d2412d0774cb6294521a0ea578975fed07ceEric Sandeen	/* Can't undo discard ... */
25418185ab9f38f0e9cd06feab9d8e59d059bde84bf6Andreas Dilger	if (!noaction && discard && (io_ptr != undo_io_manager)) {
25427d9e31655fca48e9d6c2647ad443124113508b73Lukas Czerner		retval = mke2fs_discard_device(fs);
2543d866599ab4955858b1541f0891b1b165ba66493aLukas Czerner		if (!retval && io_channel_discard_zeroes_data(fs->io)) {
25446fcd6f84c235f4bf2bd9770f172837da9982eb6eEric Sandeen			if (verbose)
254545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				printf("%s",
254645ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				       _("Discard succeeded and will return "
254745ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger					 "0s - skipping inode table wipe\n"));
25486fcd6f84c235f4bf2bd9770f172837da9982eb6eEric Sandeen			lazy_itable_init = 1;
25496fcd6f84c235f4bf2bd9770f172837da9982eb6eEric Sandeen			itable_zeroed = 1;
25506fcd6f84c235f4bf2bd9770f172837da9982eb6eEric Sandeen		}
25516fcd6f84c235f4bf2bd9770f172837da9982eb6eEric Sandeen	}
25525827d2412d0774cb6294521a0ea578975fed07ceEric Sandeen
255388ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o	sprintf(opt_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2554b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V		32768 : fs->blocksize * 8);
255588ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o	io_channel_set_options(fs->io, opt_string);
255688ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o	if (offset) {
255788ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o		sprintf(opt_string, "offset=%llu", offset);
255888ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o		io_channel_set_options(fs->io, opt_string);
255988ee023be20b4ae1c795b6c4800947f7bf6c6c0cTheodore Ts'o	}
25603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
25616cb27404f51f97e2665fa0e0c4c0f7bc47e698ecTheodore Ts'o	if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
25626cb27404f51f97e2665fa0e0c4c0f7bc47e698ecTheodore Ts'o		fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
25636cb27404f51f97e2665fa0e0c4c0f7bc47e698ecTheodore Ts'o
2564b7c5b4030870b31d73019d9d9ec55d550772590bTheodore Ts'o	if ((fs_param.s_feature_incompat &
2565b7c5b4030870b31d73019d9d9ec55d550772590bTheodore Ts'o	     (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2566b7c5b4030870b31d73019d9d9ec55d550772590bTheodore Ts'o	    (fs_param.s_feature_ro_compat &
2567b7c5b4030870b31d73019d9d9ec55d550772590bTheodore Ts'o	     (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2568b7c5b4030870b31d73019d9d9ec55d550772590bTheodore Ts'o	      EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2569b7c5b4030870b31d73019d9d9ec55d550772590bTheodore Ts'o	      EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2570b7c5b4030870b31d73019d9d9ec55d550772590bTheodore Ts'o		fs->super->s_kbytes_written = 1;
2571b7c5b4030870b31d73019d9d9ec55d550772590bTheodore Ts'o
25721e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	/*
2573e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	 * Wipe out the old on-disk superblock
2574e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	 */
257504a968579ee8125c617edee27204cf35c0a169c1Theodore Ts'o	if (!noaction)
257604a968579ee8125c617edee27204cf35c0a169c1Theodore Ts'o		zap_sector(fs, 2, 6);
2577e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o
2578e41784eb0e47ff5e460b635c86a0cef7250b48b4Theodore Ts'o	/*
2579b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o	 * Parse or generate a UUID for the filesystem
25801e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 */
2581b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o	if (fs_uuid) {
2582b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o		if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2583b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o			com_err(device_name, 0, "could not parse UUID: %s\n",
2584b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o				fs_uuid);
2585b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o			exit(1);
2586b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o		}
2587b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o	} else
2588b0afdda1bc044026336009576fbe6b72884140cbTheodore Ts'o		uuid_generate(fs->super->s_uuid);
25891e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
25901e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	/*
2591843049c4dbfe8a0dd4332569eff0ac93529e3324Theodore Ts'o	 * Initialize the directory index variables
2592843049c4dbfe8a0dd4332569eff0ac93529e3324Theodore Ts'o	 */
2593d5f57d9553d4c855e282dbd3edf1ad85ab716d33Theodore Ts'o	hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2594d5f57d9553d4c855e282dbd3edf1ad85ab716d33Theodore Ts'o					       "half_md4");
2595d5f57d9553d4c855e282dbd3edf1ad85ab716d33Theodore Ts'o	hash_alg = e2p_string2hash(hash_alg_str);
25965a2db04637c8494f6f600994f98fe0df23c4524cTheodore Ts'o	free(hash_alg_str);
2597d5f57d9553d4c855e282dbd3edf1ad85ab716d33Theodore Ts'o	fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2598d5f57d9553d4c855e282dbd3edf1ad85ab716d33Theodore Ts'o		EXT2_HASH_HALF_MD4;
2599843049c4dbfe8a0dd4332569eff0ac93529e3324Theodore Ts'o	uuid_generate((unsigned char *) fs->super->s_hash_seed);
2600843049c4dbfe8a0dd4332569eff0ac93529e3324Theodore Ts'o
2601843049c4dbfe8a0dd4332569eff0ac93529e3324Theodore Ts'o	/*
26023daf592646b668133079e2200c1e776085f2ffafEric Sandeen	 * Periodic checks can be enabled/disabled via config file.
26033daf592646b668133079e2200c1e776085f2ffafEric Sandeen	 * Note we override the kernel include file's idea of what the default
26043daf592646b668133079e2200c1e776085f2ffafEric Sandeen	 * check interval (never) should be.  It's a good idea to check at
26053daf592646b668133079e2200c1e776085f2ffafEric Sandeen	 * least *occasionally*, specially since servers will never rarely get
26063daf592646b668133079e2200c1e776085f2ffafEric Sandeen	 * to reboot, since Linux is so robust these days.  :-)
26073daf592646b668133079e2200c1e776085f2ffafEric Sandeen	 *
26083daf592646b668133079e2200c1e776085f2ffafEric Sandeen	 * 180 days (six months) seems like a good value.
260944c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	 */
26103daf592646b668133079e2200c1e776085f2ffafEric Sandeen#ifdef EXT2_DFL_CHECKINTERVAL
26113daf592646b668133079e2200c1e776085f2ffafEric Sandeen#undef EXT2_DFL_CHECKINTERVAL
26123daf592646b668133079e2200c1e776085f2ffafEric Sandeen#endif
26133daf592646b668133079e2200c1e776085f2ffafEric Sandeen#define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
26143daf592646b668133079e2200c1e776085f2ffafEric Sandeen
26153daf592646b668133079e2200c1e776085f2ffafEric Sandeen	if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
26163daf592646b668133079e2200c1e776085f2ffafEric Sandeen		fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
26173daf592646b668133079e2200c1e776085f2ffafEric Sandeen		fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
26183daf592646b668133079e2200c1e776085f2ffafEric Sandeen		/*
26193daf592646b668133079e2200c1e776085f2ffafEric Sandeen		 * Add "jitter" to the superblock's check interval so that we
26203daf592646b668133079e2200c1e776085f2ffafEric Sandeen		 * don't check all the filesystems at the same time.  We use a
26213daf592646b668133079e2200c1e776085f2ffafEric Sandeen		 * kludgy hack of using the UUID to derive a random jitter value
26223daf592646b668133079e2200c1e776085f2ffafEric Sandeen		 */
26233daf592646b668133079e2200c1e776085f2ffafEric Sandeen		for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
26243daf592646b668133079e2200c1e776085f2ffafEric Sandeen			val += fs->super->s_uuid[i];
26253daf592646b668133079e2200c1e776085f2ffafEric Sandeen		fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
262614b283ae565930144ef5ace12483d602cc3e7539Theodore Ts'o	} else
262714b283ae565930144ef5ace12483d602cc3e7539Theodore Ts'o		fs->super->s_max_mnt_count = -1;
262844c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o
262944c09c0454925e6e7756e4dc51aac6acbf5020aeTheodore Ts'o	/*
26301e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 * Override the creator OS, if applicable
26311e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 */
26321e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (creator_os && !set_os(fs->super, creator_os)) {
2633d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		com_err (program_name, 0, _("unknown os - %s"), creator_os);
26341e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		exit(1);
26351e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	}
26361e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
26371e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	/*
26384ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	 * For the Hurd, we will turn off filetype since it doesn't
26394ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	 * support it.
26404ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	 */
26414ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	if (fs->super->s_creator_os == EXT2_OS_HURD)
26424ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o		fs->super->s_feature_incompat &=
26434ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o			~EXT2_FEATURE_INCOMPAT_FILETYPE;
26444ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o
26454ea0a1109d761e9caaa53f6fa9e036321826c5faTheodore Ts'o	/*
26461e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 * Set the volume label...
26471e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 */
26481e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (volume_label) {
2649ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		memset(fs->super->s_volume_name, 0,
2650ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		       sizeof(fs->super->s_volume_name));
2651ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		strncpy(fs->super->s_volume_name, volume_label,
2652ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o			sizeof(fs->super->s_volume_name));
26531e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	}
26541e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
26551e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	/*
26561e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 * Set the last mount directory
26571e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	 */
26581e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (mount_dir) {
2659ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		memset(fs->super->s_last_mounted, 0,
2660ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		       sizeof(fs->super->s_last_mounted));
2661ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o		strncpy(fs->super->s_last_mounted, mount_dir,
2662ef9abe5f2529b3a42b46bf64c5f4232f86e7b390Theodore Ts'o			sizeof(fs->super->s_last_mounted));
26631e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	}
2664efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
266550787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	if (!quiet || noaction)
26663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		show_stats(fs);
26673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
266850787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o	if (noaction)
266950787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o		exit(0);
267050787ea22edd8b4662203daf3569411d9dcf4287Theodore Ts'o
267116ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (fs->super->s_feature_incompat &
267216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
267316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		create_journal_dev(fs);
2674568101f774f9ad61d475969e91dc90da359b5a85Andreas Dilger		exit(ext2fs_close(fs) ? 1 : 0);
267516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	}
267616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o
26773839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (bad_blocks_filename)
26783839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		read_bb_file(fs, &bb_list, bad_blocks_filename);
26793839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (cflag)
26803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		test_disk(fs, &bb_list);
26813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	handle_bad_blocks(fs, bb_list);
268265c6c3e06f72e76ddb69222b3be1713d870eb782Theodore Ts'o
26830c17cb25f24730dca138a976a390f105d2191736Theodore Ts'o	fs->stride = fs_stride = fs->super->s_raid_stride;
268495fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	if (!quiet)
268545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		printf("%s", _("Allocating group tables: "));
26863c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
26873c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	    packed_meta_blocks)
26883c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		retval = packed_allocate_tables(fs);
26893c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o	else
26903c6e91c5498771bc794fd2854153fe31c006df50Theodore Ts'o		retval = ext2fs_allocate_tables(fs);
269119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (retval) {
269245ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(program_name, retval, "%s",
2693d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			_("while trying to allocate filesystem tables"));
269419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		exit(1);
269519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	}
269695fd65bb7fcf84e8d1e207f84b9d5a9f99626a38Valerie Aurora Henson	if (!quiet)
269745ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		printf("%s", _("done                            \n"));
2698c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o
2699c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o	retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2700c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o	if (retval) {
270145ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		com_err(program_name, retval, "%s",
2702c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o			_("\n\twhile converting subcluster bitmap"));
2703c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o		exit(1);
2704c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o	}
2705c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o
2706f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	if (super_only) {
2707f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		fs->super->s_state |= EXT2_ERROR_FS;
2708f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2709ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o		/*
2710ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o		 * The command "mke2fs -S" is used to recover
2711ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o		 * corrupted file systems, so do not mark any of the
2712ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o		 * inodes as unused; we want e2fsck to consider all
2713ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o		 * inodes as potentially containing recoverable data.
2714ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o		 */
2715ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o		if (fs->super->s_feature_ro_compat &
2716ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o		    EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
271730ac1ce7df719e40b0c3c612696ada7c9ebbaed2Theodore Ts'o			for (i = 0; i < fs->group_desc_count; i++)
2718ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o				ext2fs_bg_itable_unused_set(fs, i, 0);
2719ba9e0afc5ab0d0ff26880e37ac71c281b644ce00Theodore Ts'o		}
2720f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	} else {
272159f27247f81fb662ef3b2589b3cceb198ff4dbcaAndreas Dilger		/* rsv must be a power of two (64kB is MD RAID sb alignment) */
272202d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		blk64_t rsv = 65536 / fs->blocksize;
272302d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		blk64_t blocks = ext2fs_blocks_count(fs->super);
272402d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		blk64_t start;
272502d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos		blk64_t ret_blk;
27261400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o
27271400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o#ifdef ZAP_BOOTBLOCK
272804a968579ee8125c617edee27204cf35c0a169c1Theodore Ts'o		zap_sector(fs, 0, 2);
27291400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o#endif
273059f27247f81fb662ef3b2589b3cceb198ff4dbcaAndreas Dilger
273159f27247f81fb662ef3b2589b3cceb198ff4dbcaAndreas Dilger		/*
27321400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o		 * Wipe out any old MD RAID (or other) metadata at the end
27331400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o		 * of the device.  This will also verify that the device is
273459f27247f81fb662ef3b2589b3cceb198ff4dbcaAndreas Dilger		 * as large as we think.  Be careful with very small devices.
27351400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o		 */
273659f27247f81fb662ef3b2589b3cceb198ff4dbcaAndreas Dilger		start = (blocks & ~(rsv - 1));
273759f27247f81fb662ef3b2589b3cceb198ff4dbcaAndreas Dilger		if (start > rsv)
273859f27247f81fb662ef3b2589b3cceb198ff4dbcaAndreas Dilger			start -= rsv;
273959f27247f81fb662ef3b2589b3cceb198ff4dbcaAndreas Dilger		if (start > 0)
274002d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos			retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2741b626b39a8c87dfb6d973b4ad7eca1eefa659d3d6Aneesh Kumar K.V						    &ret_blk, NULL);
274259f27247f81fb662ef3b2589b3cceb198ff4dbcaAndreas Dilger
27431400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o		if (retval) {
27441400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o			com_err(program_name, retval,
274502d6f47e9647d3155a38c8676c2da6ea773d9b68Jose R. Santos				_("while zeroing block %llu at end of filesystem"),
27461400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o				ret_blk);
27471400bbb60bf2667ab5514abd074baf4959fe475aTheodore Ts'o		}
27486fcd6f84c235f4bf2bd9770f172837da9982eb6eEric Sandeen		write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2749f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		create_root_dir(fs);
2750f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		create_lost_and_found(fs);
2751f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		reserve_inodes(fs);
2752f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o		create_bad_block_inode(fs, bb_list);
2753efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o		if (fs->super->s_feature_compat &
2754ea774315631accb05e2407765c6b448d648d14d6Theodore Ts'o		    EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2755ea774315631accb05e2407765c6b448d648d14d6Theodore Ts'o			retval = ext2fs_create_resize_inode(fs);
2756ea774315631accb05e2407765c6b448d648d14d6Theodore Ts'o			if (retval) {
2757ea774315631accb05e2407765c6b448d648d14d6Theodore Ts'o				com_err("ext2fs_create_resize_inode", retval,
275845ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger					"%s",
2759d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o				_("while reserving blocks for online resize"));
2760ea774315631accb05e2407765c6b448d648d14d6Theodore Ts'o				exit(1);
2761ea774315631accb05e2407765c6b448d648d14d6Theodore Ts'o			}
2762d323f8fb369089b97d6f3bf0f8d64ceeab0b10f5Theodore Ts'o		}
2763f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o	}
27648ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o
276516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o	if (journal_device) {
276616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		ext2_filsys	jfs;
2767efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
27688ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o		if (!force)
2769efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o			check_plausibility(journal_device);
277063985320384bf143eaac9857af424800d9867a1aTheodore Ts'o		check_mount(journal_device, force, _("journal"));
27718ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o
277216ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
277316ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				     EXT2_FLAG_JOURNAL_DEV_OK, 0,
277416ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				     fs->blocksize, unix_io_manager, &jfs);
277516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if (retval) {
277616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			com_err(program_name, retval,
277716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				_("while trying to open journal device %s\n"),
277816ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o				journal_device);
277916ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o			exit(1);
278016ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		}
278193345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		if (!quiet) {
2782efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o			printf(_("Adding journal to device %s: "),
27838ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o			       journal_device);
278493345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o			fflush(stdout);
278593345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		}
278616ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		retval = ext2fs_add_journal_device(fs, jfs);
278716ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		if(retval) {
2788efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o			com_err (program_name, retval,
2789efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o				 _("\n\twhile trying to add journal to device %s"),
27908ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o				 journal_device);
27918ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o			exit(1);
27928ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o		}
27938ddaa66bfef067f1a6ba02698a61a0fd3a26e618Theodore Ts'o		if (!quiet)
279445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			printf("%s", _("done\n"));
279516ed5b3af43c72f60991222b9d7ab65cf53f203dTheodore Ts'o		ext2fs_close(jfs);
27962d15576dfe8ffd8521a6f4211cef3d2a663dc379Andreas Dilger		free(journal_device);
27979dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o	} else if ((journal_size) ||
2798efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o		   (fs_param.s_feature_compat &
27999dc6ad1ecb0ba3caf14e05279f1cc3cea52095a2Theodore Ts'o		    EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2800a620baddee647faf42c49ee2e04ee3f667149d68Theodore Ts'o		if (super_only) {
280145ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			printf("%s", _("Skipping journal creation in super-only mode\n"));
2802a620baddee647faf42c49ee2e04ee3f667149d68Theodore Ts'o			fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2803a620baddee647faf42c49ee2e04ee3f667149d68Theodore Ts'o			goto no_journal;
2804a620baddee647faf42c49ee2e04ee3f667149d68Theodore Ts'o		}
2805a620baddee647faf42c49ee2e04ee3f667149d68Theodore Ts'o
280693345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		if (!journal_blocks) {
280793345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o			fs->super->s_feature_compat &=
280893345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o				~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
280993345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o			goto no_journal;
281093345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		}
281193345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		if (!quiet) {
2812d4e0b1c6f5aa8c6a248d9149ed5634a310952411Theodore Ts'o			printf(_("Creating journal (%u blocks): "),
281316ad33346dd4666edd9d46d417245432c87057efTheodore Ts'o			       journal_blocks);
281493345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o			fflush(stdout);
281593345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o		}
2816b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o		retval = ext2fs_add_journal_inode2(fs, journal_blocks,
2817b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o						   journal_location,
2818b818205feb60f7a7bec5e325462294f586735215Theodore Ts'o						   journal_flags);
281985ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o		if (retval) {
282045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			com_err(program_name, retval, "%s",
282145ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				_("\n\twhile trying to create journal"));
282285ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o			exit(1);
282385ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o		}
282485ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o		if (!quiet)
282545ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			printf("%s", _("done\n"));
282685ef4ae87a8d27d8e3a734f8b3964d6b8f39867bTheodore Ts'o	}
282793345d1588606ccc558406202bff8f86360ab03fTheodore Ts'ono_journal:
28280f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger	if (!super_only &&
28290f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger	    fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
28300f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger		retval = ext2fs_mmp_init(fs);
28310f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger		if (retval) {
283245ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger			fprintf(stderr, "%s",
283345ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				_("\nError while enabling multiple "
283445ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger				  "mount protection feature."));
28350f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger			exit(1);
28360f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger		}
28370f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger		if (!quiet)
28380f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger			printf(_("Multiple mount protection is enabled "
28390f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger				 "with update interval %d seconds.\n"),
28400f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger			       fs->super->s_mmp_update_interval);
28410f5eba7501f467f757792ee449d16c9259b994fdAndreas Dilger	}
284293345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o
2843c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o	if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2844c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o				       EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2845c6ed60cdeb1355a884f635ac8118c8f330e2ba68Theodore Ts'o		fix_cluster_bg_counts(fs);
28461f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali	if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
28471f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali				       EXT4_FEATURE_RO_COMPAT_QUOTA))
28481f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali		create_quota_inodes(fs);
28491f5d7a890e8b2ad03ee91fd891b0b5b4327da030Aditya Kali
28503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (!quiet)
285145ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		printf("%s", _("Writing superblocks and "
2852d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		       "filesystem accounting information: "));
2853faa2dcdad0f5af3945a70bdecc8713b3a74cdf9cLukas Czerner	checkinterval = fs->super->s_checkinterval;
2854faa2dcdad0f5af3945a70bdecc8713b3a74cdf9cLukas Czerner	max_mnt_count = fs->super->s_max_mnt_count;
2855faa2dcdad0f5af3945a70bdecc8713b3a74cdf9cLukas Czerner	retval = ext2fs_close(fs);
28565d45d80375943be8917302a502f3f28d2df8d16dTheodore Ts'o	if (retval) {
285745ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		fprintf(stderr, "%s",
28586693837e59cc7b5397a0d46d2753c309382c76f9Theodore Ts'o			_("\nWarning, had trouble writing out superblocks."));
2859faa2dcdad0f5af3945a70bdecc8713b3a74cdf9cLukas Czerner	} else if (!quiet) {
286045ff69ffeb700012a7c052f5e45882557a40be7eAndreas Dilger		printf("%s", _("done\n\n"));
28611cca86f5d886eeda2d3afb1206419ef4642c30baTheodore Ts'o		if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2862faa2dcdad0f5af3945a70bdecc8713b3a74cdf9cLukas Czerner			print_check_message(max_mnt_count, checkinterval);
286393345d1588606ccc558406202bff8f86360ab03fTheodore Ts'o	}
2864faa2dcdad0f5af3945a70bdecc8713b3a74cdf9cLukas Czerner
2865a6d8302b4873527798a77c1ba3106a04b71dfeacTheodore Ts'o	remove_error_table(&et_ext2_error_table);
2866a6d8302b4873527798a77c1ba3106a04b71dfeacTheodore Ts'o	remove_error_table(&et_prof_error_table);
28673d43836fda5321dd2f286eef77d238d1da792ce2Theodore Ts'o	profile_release(profile);
28685a2db04637c8494f6f600994f98fe0df23c4524cTheodore Ts'o	for (i=0; fs_types[i]; i++)
28695a2db04637c8494f6f600994f98fe0df23c4524cTheodore Ts'o		free(fs_types[i]);
28705a2db04637c8494f6f600994f98fe0df23c4524cTheodore Ts'o	free(fs_types);
2871faa2dcdad0f5af3945a70bdecc8713b3a74cdf9cLukas Czerner	return retval;
28723839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2873