mke2fs.c revision 45ff69ffeb700012a7c052f5e45882557a40be7e
1/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 	2003, 2004, 2005 by Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
13/* Usage: mke2fs [options] device
14 *
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
17 */
18
19#define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
20
21#include "config.h"
22#include <stdio.h>
23#include <string.h>
24#include <strings.h>
25#include <fcntl.h>
26#include <ctype.h>
27#include <time.h>
28#ifdef __linux__
29#include <sys/utsname.h>
30#endif
31#ifdef HAVE_GETOPT_H
32#include <getopt.h>
33#else
34extern char *optarg;
35extern int optind;
36#endif
37#ifdef HAVE_UNISTD_H
38#include <unistd.h>
39#endif
40#ifdef HAVE_STDLIB_H
41#include <stdlib.h>
42#endif
43#ifdef HAVE_ERRNO_H
44#include <errno.h>
45#endif
46#include <sys/ioctl.h>
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <libgen.h>
50#include <limits.h>
51#include <blkid/blkid.h>
52
53#include "ext2fs/ext2_fs.h"
54#include "ext2fs/ext2fsP.h"
55#include "et/com_err.h"
56#include "uuid/uuid.h"
57#include "e2p/e2p.h"
58#include "ext2fs/ext2fs.h"
59#include "util.h"
60#include "profile.h"
61#include "prof_err.h"
62#include "../version.h"
63#include "nls-enable.h"
64#include "quota/mkquota.h"
65
66#define STRIDE_LENGTH 8
67
68#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
69
70#ifndef __sparc__
71#define ZAP_BOOTBLOCK
72#endif
73
74#define DISCARD_STEP_MB		(2048)
75
76extern int isatty(int);
77extern FILE *fpopen(const char *cmd, const char *mode);
78
79const char * program_name = "mke2fs";
80const char * device_name /* = NULL */;
81
82/* Command line options */
83int	cflag;
84int	verbose;
85int	quiet;
86int	super_only;
87int	discard = 1;	/* attempt to discard device before fs creation */
88int	direct_io;
89int	force;
90int	noaction;
91uid_t	root_uid;
92gid_t	root_gid;
93int	journal_size;
94int	journal_flags;
95int	lazy_itable_init;
96char	*bad_blocks_filename = NULL;
97__u32	fs_stride;
98int	quotatype = -1;  /* Initialize both user and group quotas by default */
99
100struct ext2_super_block fs_param;
101char *fs_uuid = NULL;
102char *creator_os;
103char *volume_label;
104char *mount_dir;
105char *journal_device;
106int sync_kludge;	/* Set using the MKE2FS_SYNC env. option */
107char **fs_types;
108
109profile_t	profile;
110
111int sys_page_size = 4096;
112int linux_version_code = 0;
113
114static void usage(void)
115{
116	fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
117	"[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
118	"[-J journal-options]\n"
119	"\t[-G flex-group-size] [-N number-of-inodes]\n"
120	"\t[-m reserved-blocks-percentage] [-o creator-os]\n"
121	"\t[-g blocks-per-group] [-L volume-label] "
122	"[-M last-mounted-directory]\n\t[-O feature[,...]] "
123	"[-r fs-revision] [-E extended-option[,...]]\n"
124	"\t[-t fs-type] [-T usage-type ] [-U UUID] "
125	"[-jnqvDFKSV] device [blocks-count]\n"),
126		program_name);
127	exit(1);
128}
129
130static int int_log2(unsigned long long arg)
131{
132	int	l = 0;
133
134	arg >>= 1;
135	while (arg) {
136		l++;
137		arg >>= 1;
138	}
139	return l;
140}
141
142static int int_log10(unsigned long long arg)
143{
144	int	l;
145
146	for (l=0; arg ; l++)
147		arg = arg / 10;
148	return l;
149}
150
151#ifdef __linux__
152static int parse_version_number(const char *s)
153{
154	int	major, minor, rev;
155	char	*endptr;
156	const char *cp = s;
157
158	if (!s)
159		return 0;
160	major = strtol(cp, &endptr, 10);
161	if (cp == endptr || *endptr != '.')
162		return 0;
163	cp = endptr + 1;
164	minor = strtol(cp, &endptr, 10);
165	if (cp == endptr || *endptr != '.')
166		return 0;
167	cp = endptr + 1;
168	rev = strtol(cp, &endptr, 10);
169	if (cp == endptr)
170		return 0;
171	return ((((major * 256) + minor) * 256) + rev);
172}
173#endif
174
175/*
176 * Helper function for read_bb_file and test_disk
177 */
178static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
179{
180	fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
181	return;
182}
183
184/*
185 * Reads the bad blocks list from a file
186 */
187static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
188			 const char *bad_blocks_file)
189{
190	FILE		*f;
191	errcode_t	retval;
192
193	f = fopen(bad_blocks_file, "r");
194	if (!f) {
195		com_err("read_bad_blocks_file", errno,
196			_("while trying to open %s"), bad_blocks_file);
197		exit(1);
198	}
199	retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
200	fclose (f);
201	if (retval) {
202		com_err("ext2fs_read_bb_FILE", retval, "%s",
203			_("while reading in list of bad blocks from file"));
204		exit(1);
205	}
206}
207
208/*
209 * Runs the badblocks program to test the disk
210 */
211static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
212{
213	FILE		*f;
214	errcode_t	retval;
215	char		buf[1024];
216
217	sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
218		quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
219		fs->device_name, ext2fs_blocks_count(fs->super)-1);
220	if (verbose)
221		printf(_("Running command: %s\n"), buf);
222	f = popen(buf, "r");
223	if (!f) {
224		com_err("popen", errno,
225			_("while trying to run '%s'"), buf);
226		exit(1);
227	}
228	retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
229	pclose(f);
230	if (retval) {
231		com_err("ext2fs_read_bb_FILE", retval, "%s",
232			_("while processing list of bad blocks from program"));
233		exit(1);
234	}
235}
236
237static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
238{
239	dgrp_t			i;
240	blk_t			j;
241	unsigned 		must_be_good;
242	blk_t			blk;
243	badblocks_iterate	bb_iter;
244	errcode_t		retval;
245	blk_t			group_block;
246	int			group;
247	int			group_bad;
248
249	if (!bb_list)
250		return;
251
252	/*
253	 * The primary superblock and group descriptors *must* be
254	 * good; if not, abort.
255	 */
256	must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
257	for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
258		if (ext2fs_badblocks_list_test(bb_list, i)) {
259			fprintf(stderr, _("Block %d in primary "
260				"superblock/group descriptor area bad.\n"), i);
261			fprintf(stderr, _("Blocks %u through %u must be good "
262				"in order to build a filesystem.\n"),
263				fs->super->s_first_data_block, must_be_good);
264			fputs(_("Aborting....\n"), stderr);
265			exit(1);
266		}
267	}
268
269	/*
270	 * See if any of the bad blocks are showing up in the backup
271	 * superblocks and/or group descriptors.  If so, issue a
272	 * warning and adjust the block counts appropriately.
273	 */
274	group_block = fs->super->s_first_data_block +
275		fs->super->s_blocks_per_group;
276
277	for (i = 1; i < fs->group_desc_count; i++) {
278		group_bad = 0;
279		for (j=0; j < fs->desc_blocks+1; j++) {
280			if (ext2fs_badblocks_list_test(bb_list,
281						       group_block + j)) {
282				if (!group_bad)
283					fprintf(stderr,
284_("Warning: the backup superblock/group descriptors at block %u contain\n"
285"	bad blocks.\n\n"),
286						group_block);
287				group_bad++;
288				group = ext2fs_group_of_blk2(fs, group_block+j);
289				ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
290				ext2fs_group_desc_csum_set(fs, group);
291				ext2fs_free_blocks_count_add(fs->super, 1);
292			}
293		}
294		group_block += fs->super->s_blocks_per_group;
295	}
296
297	/*
298	 * Mark all the bad blocks as used...
299	 */
300	retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
301	if (retval) {
302		com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
303			_("while marking bad blocks as used"));
304		exit(1);
305	}
306	while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
307		ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
308	ext2fs_badblocks_list_iterate_end(bb_iter);
309}
310
311static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
312{
313	errcode_t	retval;
314	blk64_t		blk;
315	dgrp_t		i;
316	int		num;
317	struct ext2fs_numeric_progress_struct progress;
318
319	ext2fs_numeric_progress_init(fs, &progress,
320				     _("Writing inode tables: "),
321				     fs->group_desc_count);
322
323	for (i = 0; i < fs->group_desc_count; i++) {
324		ext2fs_numeric_progress_update(fs, &progress, i);
325
326		blk = ext2fs_inode_table_loc(fs, i);
327		num = fs->inode_blocks_per_group;
328
329		if (lazy_flag)
330			num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
331					       ext2fs_bg_itable_unused(fs, i)) *
332					      EXT2_INODE_SIZE(fs->super),
333					      EXT2_BLOCK_SIZE(fs->super));
334		if (!lazy_flag || itable_zeroed) {
335			/* The kernel doesn't need to zero the itable blocks */
336			ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
337			ext2fs_group_desc_csum_set(fs, i);
338		}
339		retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
340		if (retval) {
341			fprintf(stderr, _("\nCould not write %d "
342				  "blocks in inode table starting at %llu: %s\n"),
343				num, blk, error_message(retval));
344			exit(1);
345		}
346		if (sync_kludge) {
347			if (sync_kludge == 1)
348				sync();
349			else if ((i % sync_kludge) == 0)
350				sync();
351		}
352	}
353	ext2fs_zero_blocks2(0, 0, 0, 0, 0);
354	ext2fs_numeric_progress_close(fs, &progress,
355				      _("done                            \n"));
356}
357
358static void create_root_dir(ext2_filsys fs)
359{
360	errcode_t		retval;
361	struct ext2_inode	inode;
362
363	retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
364	if (retval) {
365		com_err("ext2fs_mkdir", retval, "%s",
366			_("while creating root dir"));
367		exit(1);
368	}
369	if (root_uid != 0 || root_gid != 0) {
370		retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
371		if (retval) {
372			com_err("ext2fs_read_inode", retval, "%s",
373				_("while reading root inode"));
374			exit(1);
375		}
376
377		inode.i_uid = root_uid;
378		ext2fs_set_i_uid_high(inode, root_uid >> 16);
379		inode.i_gid = root_gid;
380		ext2fs_set_i_gid_high(inode, root_gid >> 16);
381
382		retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
383		if (retval) {
384			com_err("ext2fs_write_inode", retval, "%s",
385				_("while setting root inode ownership"));
386			exit(1);
387		}
388	}
389}
390
391static void create_lost_and_found(ext2_filsys fs)
392{
393	unsigned int		lpf_size = 0;
394	errcode_t		retval;
395	ext2_ino_t		ino;
396	const char		*name = "lost+found";
397	int			i;
398
399	fs->umask = 077;
400	retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
401	if (retval) {
402		com_err("ext2fs_mkdir", retval, "%s",
403			_("while creating /lost+found"));
404		exit(1);
405	}
406
407	retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
408	if (retval) {
409		com_err("ext2_lookup", retval, "%s",
410			_("while looking up /lost+found"));
411		exit(1);
412	}
413
414	for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
415		/* Ensure that lost+found is at least 2 blocks, so we always
416		 * test large empty blocks for big-block filesystems.  */
417		if ((lpf_size += fs->blocksize) >= 16*1024 &&
418		    lpf_size >= 2 * fs->blocksize)
419			break;
420		retval = ext2fs_expand_dir(fs, ino);
421		if (retval) {
422			com_err("ext2fs_expand_dir", retval, "%s",
423				_("while expanding /lost+found"));
424			exit(1);
425		}
426	}
427}
428
429static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
430{
431	errcode_t	retval;
432
433	ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
434	ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
435	retval = ext2fs_update_bb_inode(fs, bb_list);
436	if (retval) {
437		com_err("ext2fs_update_bb_inode", retval, "%s",
438			_("while setting bad block inode"));
439		exit(1);
440	}
441
442}
443
444static void reserve_inodes(ext2_filsys fs)
445{
446	ext2_ino_t	i;
447
448	for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
449		ext2fs_inode_alloc_stats2(fs, i, +1, 0);
450	ext2fs_mark_ib_dirty(fs);
451}
452
453#define BSD_DISKMAGIC   (0x82564557UL)  /* The disk magic number */
454#define BSD_MAGICDISK   (0x57455682UL)  /* The disk magic number reversed */
455#define BSD_LABEL_OFFSET        64
456
457static void zap_sector(ext2_filsys fs, int sect, int nsect)
458{
459	char *buf;
460	int retval;
461	unsigned int *magic;
462
463	buf = malloc(512*nsect);
464	if (!buf) {
465		printf(_("Out of memory erasing sectors %d-%d\n"),
466		       sect, sect + nsect - 1);
467		exit(1);
468	}
469
470	if (sect == 0) {
471		/* Check for a BSD disklabel, and don't erase it if so */
472		retval = io_channel_read_blk64(fs->io, 0, -512, buf);
473		if (retval)
474			fprintf(stderr,
475				_("Warning: could not read block 0: %s\n"),
476				error_message(retval));
477		else {
478			magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
479			if ((*magic == BSD_DISKMAGIC) ||
480			    (*magic == BSD_MAGICDISK))
481				return;
482		}
483	}
484
485	memset(buf, 0, 512*nsect);
486	io_channel_set_blksize(fs->io, 512);
487	retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
488	io_channel_set_blksize(fs->io, fs->blocksize);
489	free(buf);
490	if (retval)
491		fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
492			sect, error_message(retval));
493}
494
495static void create_journal_dev(ext2_filsys fs)
496{
497	struct ext2fs_numeric_progress_struct progress;
498	errcode_t		retval;
499	char			*buf;
500	blk64_t			blk, err_blk;
501	int			c, count, err_count;
502
503	retval = ext2fs_create_journal_superblock(fs,
504				  ext2fs_blocks_count(fs->super), 0, &buf);
505	if (retval) {
506		com_err("create_journal_dev", retval, "%s",
507			_("while initializing journal superblock"));
508		exit(1);
509	}
510
511	if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
512		goto write_superblock;
513
514	ext2fs_numeric_progress_init(fs, &progress,
515				     _("Zeroing journal device: "),
516				     ext2fs_blocks_count(fs->super));
517	blk = 0;
518	count = ext2fs_blocks_count(fs->super);
519	while (count > 0) {
520		if (count > 1024)
521			c = 1024;
522		else
523			c = count;
524		retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
525		if (retval) {
526			com_err("create_journal_dev", retval,
527				_("while zeroing journal device "
528				  "(block %llu, count %d)"),
529				err_blk, err_count);
530			exit(1);
531		}
532		blk += c;
533		count -= c;
534		ext2fs_numeric_progress_update(fs, &progress, blk);
535	}
536	ext2fs_zero_blocks2(0, 0, 0, 0, 0);
537
538	ext2fs_numeric_progress_close(fs, &progress, NULL);
539write_superblock:
540	retval = io_channel_write_blk64(fs->io,
541					fs->super->s_first_data_block+1,
542					1, buf);
543	if (retval) {
544		com_err("create_journal_dev", retval, "%s",
545			_("while writing journal superblock"));
546		exit(1);
547	}
548}
549
550static void show_stats(ext2_filsys fs)
551{
552	struct ext2_super_block *s = fs->super;
553	char 			buf[80];
554        char                    *os;
555	blk64_t			group_block;
556	dgrp_t			i;
557	int			need, col_left;
558
559	if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
560		fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
561		       ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
562
563	memset(buf, 0, sizeof(buf));
564	strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
565	printf(_("Filesystem label=%s\n"), buf);
566	os = e2p_os2string(fs->super->s_creator_os);
567	if (os)
568		printf(_("OS type: %s\n"), os);
569	free(os);
570	printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
571		s->s_log_block_size);
572	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
573				       EXT4_FEATURE_RO_COMPAT_BIGALLOC))
574		printf(_("Cluster size=%u (log=%u)\n"),
575		       fs->blocksize << fs->cluster_ratio_bits,
576		       s->s_log_cluster_size);
577	else
578		printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
579		       s->s_log_cluster_size);
580	printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
581	       s->s_raid_stride, s->s_raid_stripe_width);
582	printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
583	       ext2fs_blocks_count(s));
584	printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
585		ext2fs_r_blocks_count(s),
586	       100.0 *  ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
587	printf(_("First data block=%u\n"), s->s_first_data_block);
588	if (root_uid != 0 || root_gid != 0)
589		printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
590	if (s->s_reserved_gdt_blocks)
591		printf(_("Maximum filesystem blocks=%lu\n"),
592		       (s->s_reserved_gdt_blocks + fs->desc_blocks) *
593		       EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
594	if (fs->group_desc_count > 1)
595		printf(_("%u block groups\n"), fs->group_desc_count);
596	else
597		printf(_("%u block group\n"), fs->group_desc_count);
598	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
599				       EXT4_FEATURE_RO_COMPAT_BIGALLOC))
600		printf(_("%u blocks per group, %u clusters per group\n"),
601		       s->s_blocks_per_group, s->s_clusters_per_group);
602	else
603		printf(_("%u blocks per group, %u fragments per group\n"),
604		       s->s_blocks_per_group, s->s_clusters_per_group);
605	printf(_("%u inodes per group\n"), s->s_inodes_per_group);
606
607	if (fs->group_desc_count == 1) {
608		printf("\n");
609		return;
610	}
611
612	printf("%s", _("Superblock backups stored on blocks: "));
613	group_block = s->s_first_data_block;
614	col_left = 0;
615	for (i = 1; i < fs->group_desc_count; i++) {
616		group_block += s->s_blocks_per_group;
617		if (!ext2fs_bg_has_super(fs, i))
618			continue;
619		if (i != 1)
620			printf(", ");
621		need = int_log10(group_block) + 2;
622		if (need > col_left) {
623			printf("\n\t");
624			col_left = 72;
625		}
626		col_left -= need;
627		printf("%llu", group_block);
628	}
629	printf("\n\n");
630}
631
632/*
633 * Set the S_CREATOR_OS field.  Return true if OS is known,
634 * otherwise, 0.
635 */
636static int set_os(struct ext2_super_block *sb, char *os)
637{
638	if (isdigit (*os))
639		sb->s_creator_os = atoi (os);
640	else if (strcasecmp(os, "linux") == 0)
641		sb->s_creator_os = EXT2_OS_LINUX;
642	else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
643		sb->s_creator_os = EXT2_OS_HURD;
644	else if (strcasecmp(os, "freebsd") == 0)
645		sb->s_creator_os = EXT2_OS_FREEBSD;
646	else if (strcasecmp(os, "lites") == 0)
647		sb->s_creator_os = EXT2_OS_LITES;
648	else
649		return 0;
650	return 1;
651}
652
653#define PATH_SET "PATH=/sbin"
654
655static void parse_extended_opts(struct ext2_super_block *param,
656				const char *opts)
657{
658	char	*buf, *token, *next, *p, *arg, *badopt = 0;
659	int	len;
660	int	r_usage = 0;
661
662	len = strlen(opts);
663	buf = malloc(len+1);
664	if (!buf) {
665		fprintf(stderr, "%s",
666			_("Couldn't allocate memory to parse options!\n"));
667		exit(1);
668	}
669	strcpy(buf, opts);
670	for (token = buf; token && *token; token = next) {
671		p = strchr(token, ',');
672		next = 0;
673		if (p) {
674			*p = 0;
675			next = p+1;
676		}
677		arg = strchr(token, '=');
678		if (arg) {
679			*arg = 0;
680			arg++;
681		}
682		if (strcmp(token, "mmp_update_interval") == 0) {
683			if (!arg) {
684				r_usage++;
685				badopt = token;
686				continue;
687			}
688			param->s_mmp_update_interval = strtoul(arg, &p, 0);
689			if (*p) {
690				fprintf(stderr,
691					_("Invalid mmp_update_interval: %s\n"),
692					arg);
693				r_usage++;
694				continue;
695			}
696		} else if (strcmp(token, "stride") == 0) {
697			if (!arg) {
698				r_usage++;
699				badopt = token;
700				continue;
701			}
702			param->s_raid_stride = strtoul(arg, &p, 0);
703			if (*p) {
704				fprintf(stderr,
705					_("Invalid stride parameter: %s\n"),
706					arg);
707				r_usage++;
708				continue;
709			}
710		} else if (strcmp(token, "stripe-width") == 0 ||
711			   strcmp(token, "stripe_width") == 0) {
712			if (!arg) {
713				r_usage++;
714				badopt = token;
715				continue;
716			}
717			param->s_raid_stripe_width = strtoul(arg, &p, 0);
718			if (*p) {
719				fprintf(stderr,
720					_("Invalid stripe-width parameter: %s\n"),
721					arg);
722				r_usage++;
723				continue;
724			}
725		} else if (!strcmp(token, "resize")) {
726			blk64_t resize;
727			unsigned long bpg, rsv_groups;
728			unsigned long group_desc_count, desc_blocks;
729			unsigned int gdpb, blocksize;
730			int rsv_gdb;
731
732			if (!arg) {
733				r_usage++;
734				badopt = token;
735				continue;
736			}
737
738			resize = parse_num_blocks2(arg,
739						   param->s_log_block_size);
740
741			if (resize == 0) {
742				fprintf(stderr,
743					_("Invalid resize parameter: %s\n"),
744					arg);
745				r_usage++;
746				continue;
747			}
748			if (resize <= ext2fs_blocks_count(param)) {
749				fprintf(stderr, "%s",
750					_("The resize maximum must be greater "
751					  "than the filesystem size.\n"));
752				r_usage++;
753				continue;
754			}
755
756			blocksize = EXT2_BLOCK_SIZE(param);
757			bpg = param->s_blocks_per_group;
758			if (!bpg)
759				bpg = blocksize * 8;
760			gdpb = EXT2_DESC_PER_BLOCK(param);
761			group_desc_count = (__u32) ext2fs_div64_ceil(
762				ext2fs_blocks_count(param), bpg);
763			desc_blocks = (group_desc_count +
764				       gdpb - 1) / gdpb;
765			rsv_groups = ext2fs_div64_ceil(resize, bpg);
766			rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
767				desc_blocks;
768			if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
769				rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
770
771			if (rsv_gdb > 0) {
772				if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
773					fprintf(stderr, "%s",
774	_("On-line resizing not supported with revision 0 filesystems\n"));
775					free(buf);
776					exit(1);
777				}
778				param->s_feature_compat |=
779					EXT2_FEATURE_COMPAT_RESIZE_INODE;
780
781				param->s_reserved_gdt_blocks = rsv_gdb;
782			}
783		} else if (!strcmp(token, "test_fs")) {
784			param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
785		} else if (!strcmp(token, "lazy_itable_init")) {
786			if (arg)
787				lazy_itable_init = strtoul(arg, &p, 0);
788			else
789				lazy_itable_init = 1;
790		} else if (!strcmp(token, "lazy_journal_init")) {
791			if (arg)
792				journal_flags |= strtoul(arg, &p, 0) ?
793						EXT2_MKJOURNAL_LAZYINIT : 0;
794			else
795				journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
796		} else if (!strcmp(token, "root_owner")) {
797			if (arg) {
798				root_uid = strtoul(arg, &p, 0);
799				if (*p != ':') {
800					fprintf(stderr,
801						_("Invalid root_owner: '%s'\n"),
802						arg);
803					r_usage++;
804					continue;
805				}
806				p++;
807				root_gid = strtoul(p, &p, 0);
808				if (*p) {
809					fprintf(stderr,
810						_("Invalid root_owner: '%s'\n"),
811						arg);
812					r_usage++;
813					continue;
814				}
815			} else {
816				root_uid = getuid();
817				root_gid = getgid();
818			}
819		} else if (!strcmp(token, "discard")) {
820			discard = 1;
821		} else if (!strcmp(token, "nodiscard")) {
822			discard = 0;
823		} else if (!strcmp(token, "quotatype")) {
824			if (!arg) {
825				r_usage++;
826				badopt = token;
827				continue;
828			}
829			if (!strncmp(arg, "usr", 3)) {
830				quotatype = 0;
831			} else if (!strncmp(arg, "grp", 3)) {
832				quotatype = 1;
833			} else {
834				fprintf(stderr,
835					_("Invalid quotatype parameter: %s\n"),
836					arg);
837				r_usage++;
838				continue;
839			}
840		} else {
841			r_usage++;
842			badopt = token;
843		}
844	}
845	if (r_usage) {
846		fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
847			"Extended options are separated by commas, "
848			"and may take an argument which\n"
849			"\tis set off by an equals ('=') sign.\n\n"
850			"Valid extended options are:\n"
851			"\tstride=<RAID per-disk data chunk in blocks>\n"
852			"\tstripe-width=<RAID stride * data disks in blocks>\n"
853			"\tresize=<resize maximum size in blocks>\n"
854			"\tlazy_itable_init=<0 to disable, 1 to enable>\n"
855			"\tlazy_journal_init=<0 to disable, 1 to enable>\n"
856			"\troot_uid=<uid of root directory>\n"
857			"\troot_gid=<gid of root directory>\n"
858			"\ttest_fs\n"
859			"\tdiscard\n"
860			"\tnodiscard\n"
861			"\tquotatype=<usr OR grp>\n\n"),
862			badopt ? badopt : "");
863		free(buf);
864		exit(1);
865	}
866	if (param->s_raid_stride &&
867	    (param->s_raid_stripe_width % param->s_raid_stride) != 0)
868		fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
869				  "multiple of stride %u.\n\n"),
870			param->s_raid_stripe_width, param->s_raid_stride);
871
872	free(buf);
873}
874
875static __u32 ok_features[3] = {
876	/* Compat */
877	EXT3_FEATURE_COMPAT_HAS_JOURNAL |
878		EXT2_FEATURE_COMPAT_RESIZE_INODE |
879		EXT2_FEATURE_COMPAT_DIR_INDEX |
880		EXT2_FEATURE_COMPAT_EXT_ATTR,
881	/* Incompat */
882	EXT2_FEATURE_INCOMPAT_FILETYPE|
883		EXT3_FEATURE_INCOMPAT_EXTENTS|
884		EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
885		EXT2_FEATURE_INCOMPAT_META_BG|
886		EXT4_FEATURE_INCOMPAT_FLEX_BG|
887		EXT4_FEATURE_INCOMPAT_MMP |
888		EXT4_FEATURE_INCOMPAT_64BIT,
889	/* R/O compat */
890	EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
891		EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
892		EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
893		EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
894		EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
895		EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
896		EXT4_FEATURE_RO_COMPAT_BIGALLOC|
897#ifdef CONFIG_QUOTA
898		EXT4_FEATURE_RO_COMPAT_QUOTA|
899#endif
900		0
901};
902
903
904static void syntax_err_report(const char *filename, long err, int line_num)
905{
906	fprintf(stderr,
907		_("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
908		filename, line_num, error_message(err));
909	exit(1);
910}
911
912static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
913
914static void edit_feature(const char *str, __u32 *compat_array)
915{
916	if (!str)
917		return;
918
919	if (e2p_edit_feature(str, compat_array, ok_features)) {
920		fprintf(stderr, _("Invalid filesystem option set: %s\n"),
921			str);
922		exit(1);
923	}
924}
925
926static void edit_mntopts(const char *str, __u32 *mntopts)
927{
928	if (!str)
929		return;
930
931	if (e2p_edit_mntopts(str, mntopts, ~0)) {
932		fprintf(stderr, _("Invalid mount option set: %s\n"),
933			str);
934		exit(1);
935	}
936}
937
938struct str_list {
939	char **list;
940	int num;
941	int max;
942};
943
944static errcode_t init_list(struct str_list *sl)
945{
946	sl->num = 0;
947	sl->max = 0;
948	sl->list = malloc((sl->max+1) * sizeof(char *));
949	if (!sl->list)
950		return ENOMEM;
951	sl->list[0] = 0;
952	return 0;
953}
954
955static errcode_t push_string(struct str_list *sl, const char *str)
956{
957	char **new_list;
958
959	if (sl->num >= sl->max) {
960		sl->max += 2;
961		new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
962		if (!new_list)
963			return ENOMEM;
964		sl->list = new_list;
965	}
966	sl->list[sl->num] = malloc(strlen(str)+1);
967	if (sl->list[sl->num] == 0)
968		return ENOMEM;
969	strcpy(sl->list[sl->num], str);
970	sl->num++;
971	sl->list[sl->num] = 0;
972	return 0;
973}
974
975static void print_str_list(char **list)
976{
977	char **cpp;
978
979	for (cpp = list; *cpp; cpp++) {
980		printf("'%s'", *cpp);
981		if (cpp[1])
982			fputs(", ", stdout);
983	}
984	fputc('\n', stdout);
985}
986
987/*
988 * Return TRUE if the profile has the given subsection
989 */
990static int profile_has_subsection(profile_t prof, const char *section,
991				  const char *subsection)
992{
993	void			*state;
994	const char		*names[4];
995	char			*name;
996	int			ret = 0;
997
998	names[0] = section;
999	names[1] = subsection;
1000	names[2] = 0;
1001
1002	if (profile_iterator_create(prof, names,
1003				    PROFILE_ITER_LIST_SECTION |
1004				    PROFILE_ITER_RELATIONS_ONLY, &state))
1005		return 0;
1006
1007	if ((profile_iterator(&state, &name, 0) == 0) && name) {
1008		free(name);
1009		ret = 1;
1010	}
1011
1012	profile_iterator_free(&state);
1013	return ret;
1014}
1015
1016static char **parse_fs_type(const char *fs_type,
1017			    const char *usage_types,
1018			    struct ext2_super_block *sb,
1019			    blk64_t fs_blocks_count,
1020			    char *progname)
1021{
1022	const char	*ext_type = 0;
1023	char		*parse_str;
1024	char		*profile_type = 0;
1025	char		*cp, *t;
1026	const char	*size_type;
1027	struct str_list	list;
1028	unsigned long long meg;
1029	int		is_hurd = 0;
1030
1031	if (init_list(&list))
1032		return 0;
1033
1034	if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1035			   !strcasecmp(creator_os, "hurd")))
1036		is_hurd = 1;
1037
1038	if (fs_type)
1039		ext_type = fs_type;
1040	else if (is_hurd)
1041		ext_type = "ext2";
1042	else if (!strcmp(program_name, "mke3fs"))
1043		ext_type = "ext3";
1044	else if (!strcmp(program_name, "mke4fs"))
1045		ext_type = "ext4";
1046	else if (progname) {
1047		ext_type = strrchr(progname, '/');
1048		if (ext_type)
1049			ext_type++;
1050		else
1051			ext_type = progname;
1052
1053		if (!strncmp(ext_type, "mkfs.", 5)) {
1054			ext_type += 5;
1055			if (ext_type[0] == 0)
1056				ext_type = 0;
1057		} else
1058			ext_type = 0;
1059	}
1060
1061	if (!ext_type) {
1062		profile_get_string(profile, "defaults", "fs_type", 0,
1063				   "ext2", &profile_type);
1064		ext_type = profile_type;
1065		if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1066			ext_type = "ext3";
1067	}
1068
1069
1070	if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1071	    strcmp(ext_type, "ext2")) {
1072		printf(_("\nYour mke2fs.conf file does not define the "
1073			 "%s filesystem type.\n"), ext_type);
1074		if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1075		    !strcmp(ext_type, "ext4dev")) {
1076			printf("%s", _("You probably need to install an "
1077				       "updated mke2fs.conf file.\n\n"));
1078		}
1079		if (!force) {
1080			printf("%s", _("Aborting...\n"));
1081			exit(1);
1082		}
1083	}
1084
1085	meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1086	if (fs_blocks_count < 3 * meg)
1087		size_type = "floppy";
1088	else if (fs_blocks_count < 512 * meg)
1089		size_type = "small";
1090	else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1091		size_type = "default";
1092	else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1093		size_type = "big";
1094	else
1095		size_type = "huge";
1096
1097	if (!usage_types)
1098		usage_types = size_type;
1099
1100	parse_str = malloc(strlen(usage_types)+1);
1101	if (!parse_str) {
1102		free(profile_type);
1103		free(list.list);
1104		return 0;
1105	}
1106	strcpy(parse_str, usage_types);
1107
1108	if (ext_type)
1109		push_string(&list, ext_type);
1110	cp = parse_str;
1111	while (1) {
1112		t = strchr(cp, ',');
1113		if (t)
1114			*t = '\0';
1115
1116		if (*cp) {
1117			if (profile_has_subsection(profile, "fs_types", cp))
1118				push_string(&list, cp);
1119			else if (strcmp(cp, "default") != 0)
1120				fprintf(stderr,
1121					_("\nWarning: the fs_type %s is not "
1122					  "defined in mke2fs.conf\n\n"),
1123					cp);
1124		}
1125		if (t)
1126			cp = t+1;
1127		else
1128			break;
1129	}
1130	free(parse_str);
1131	free(profile_type);
1132	if (is_hurd)
1133		push_string(&list, "hurd");
1134	return (list.list);
1135}
1136
1137static char *get_string_from_profile(char **types, const char *opt,
1138				     const char *def_val)
1139{
1140	char *ret = 0;
1141	int i;
1142
1143	for (i=0; types[i]; i++);
1144	for (i-=1; i >=0 ; i--) {
1145		profile_get_string(profile, "fs_types", types[i],
1146				   opt, 0, &ret);
1147		if (ret)
1148			return ret;
1149	}
1150	profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1151	return (ret);
1152}
1153
1154static int get_int_from_profile(char **types, const char *opt, int def_val)
1155{
1156	int ret;
1157	char **cpp;
1158
1159	profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1160	for (cpp = types; *cpp; cpp++)
1161		profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1162	return ret;
1163}
1164
1165static double get_double_from_profile(char **types, const char *opt,
1166				      double def_val)
1167{
1168	double ret;
1169	char **cpp;
1170
1171	profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1172	for (cpp = types; *cpp; cpp++)
1173		profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1174	return ret;
1175}
1176
1177static int get_bool_from_profile(char **types, const char *opt, int def_val)
1178{
1179	int ret;
1180	char **cpp;
1181
1182	profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1183	for (cpp = types; *cpp; cpp++)
1184		profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1185	return ret;
1186}
1187
1188extern const char *mke2fs_default_profile;
1189static const char *default_files[] = { "<default>", 0 };
1190
1191#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1192/*
1193 * Sets the geometry of a device (stripe/stride), and returns the
1194 * device's alignment offset, if any, or a negative error.
1195 */
1196static int get_device_geometry(const char *file,
1197			       struct ext2_super_block *fs_param,
1198			       int psector_size)
1199{
1200	int rc = -1;
1201	int blocksize;
1202	blkid_probe pr;
1203	blkid_topology tp;
1204	unsigned long min_io;
1205	unsigned long opt_io;
1206	struct stat statbuf;
1207
1208	/* Nothing to do for a regular file */
1209	if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1210		return 0;
1211
1212	pr = blkid_new_probe_from_filename(file);
1213	if (!pr)
1214		goto out;
1215
1216	tp = blkid_probe_get_topology(pr);
1217	if (!tp)
1218		goto out;
1219
1220	min_io = blkid_topology_get_minimum_io_size(tp);
1221	opt_io = blkid_topology_get_optimal_io_size(tp);
1222	blocksize = EXT2_BLOCK_SIZE(fs_param);
1223	if ((min_io == 0) && (psector_size > blocksize))
1224		min_io = psector_size;
1225	if ((opt_io == 0) && min_io)
1226		opt_io = min_io;
1227	if ((opt_io == 0) && (psector_size > blocksize))
1228		opt_io = psector_size;
1229
1230	/* setting stripe/stride to blocksize is pointless */
1231	if (min_io > blocksize)
1232		fs_param->s_raid_stride = min_io / blocksize;
1233	if (opt_io > blocksize)
1234		fs_param->s_raid_stripe_width = opt_io / blocksize;
1235
1236	rc = blkid_topology_get_alignment_offset(tp);
1237out:
1238	blkid_free_probe(pr);
1239	return rc;
1240}
1241#endif
1242
1243static void PRS(int argc, char *argv[])
1244{
1245	int		b, c;
1246	int		cluster_size = 0;
1247	char 		*tmp, **cpp;
1248	int		blocksize = 0;
1249	int		inode_ratio = 0;
1250	int		inode_size = 0;
1251	unsigned long	flex_bg_size = 0;
1252	double		reserved_ratio = -1.0;
1253	int		lsector_size = 0, psector_size = 0;
1254	int		show_version_only = 0;
1255	unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1256	errcode_t	retval;
1257	char *		oldpath = getenv("PATH");
1258	char *		extended_opts = 0;
1259	char *		fs_type = 0;
1260	char *		usage_types = 0;
1261	blk64_t		dev_size;
1262	/*
1263	 * NOTE: A few words about fs_blocks_count and blocksize:
1264	 *
1265	 * Initially, blocksize is set to zero, which implies 1024.
1266	 * If -b is specified, blocksize is updated to the user's value.
1267	 *
1268	 * Next, the device size or the user's "blocks" command line argument
1269	 * is used to set fs_blocks_count; the units are blocksize.
1270	 *
1271	 * Later, if blocksize hasn't been set and the profile specifies a
1272	 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1273	 * appropriately.  Note the change in units!
1274	 *
1275	 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1276	 */
1277	blk64_t		fs_blocks_count = 0;
1278#ifdef __linux__
1279	struct 		utsname ut;
1280#endif
1281	long		sysval;
1282	int		s_opt = -1, r_opt = -1;
1283	char		*fs_features = 0;
1284	int		use_bsize;
1285	char		*newpath;
1286	int		pathlen = sizeof(PATH_SET) + 1;
1287
1288	if (oldpath)
1289		pathlen += strlen(oldpath);
1290	newpath = malloc(pathlen);
1291	if (!newpath) {
1292		fprintf(stderr, "%s",
1293			_("Couldn't allocate memory for new PATH.\n"));
1294		exit(1);
1295	}
1296	strcpy(newpath, PATH_SET);
1297
1298	/* Update our PATH to include /sbin  */
1299	if (oldpath) {
1300		strcat (newpath, ":");
1301		strcat (newpath, oldpath);
1302	}
1303	putenv (newpath);
1304
1305	tmp = getenv("MKE2FS_SYNC");
1306	if (tmp)
1307		sync_kludge = atoi(tmp);
1308
1309	/* Determine the system page size if possible */
1310#ifdef HAVE_SYSCONF
1311#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1312#define _SC_PAGESIZE _SC_PAGE_SIZE
1313#endif
1314#ifdef _SC_PAGESIZE
1315	sysval = sysconf(_SC_PAGESIZE);
1316	if (sysval > 0)
1317		sys_page_size = sysval;
1318#endif /* _SC_PAGESIZE */
1319#endif /* HAVE_SYSCONF */
1320
1321	if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1322		config_fn[0] = tmp;
1323	profile_set_syntax_err_cb(syntax_err_report);
1324	retval = profile_init(config_fn, &profile);
1325	if (retval == ENOENT) {
1326		retval = profile_init(default_files, &profile);
1327		if (retval)
1328			goto profile_error;
1329		retval = profile_set_default(profile, mke2fs_default_profile);
1330		if (retval)
1331			goto profile_error;
1332	} else if (retval) {
1333profile_error:
1334		fprintf(stderr, _("Couldn't init profile successfully"
1335				  " (error: %ld).\n"), retval);
1336		exit(1);
1337	}
1338
1339	setbuf(stdout, NULL);
1340	setbuf(stderr, NULL);
1341	add_error_table(&et_ext2_error_table);
1342	add_error_table(&et_prof_error_table);
1343	memset(&fs_param, 0, sizeof(struct ext2_super_block));
1344	fs_param.s_rev_level = 1;  /* Create revision 1 filesystems now */
1345
1346#ifdef __linux__
1347	if (uname(&ut)) {
1348		perror("uname");
1349		exit(1);
1350	}
1351	linux_version_code = parse_version_number(ut.release);
1352	if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1353		fs_param.s_rev_level = 0;
1354#endif
1355
1356	if (argc && *argv) {
1357		program_name = get_progname(*argv);
1358
1359		/* If called as mkfs.ext3, create a journal inode */
1360		if (!strcmp(program_name, "mkfs.ext3") ||
1361		    !strcmp(program_name, "mke3fs"))
1362			journal_size = -1;
1363	}
1364
1365	while ((c = getopt (argc, argv,
1366		    "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1367		switch (c) {
1368		case 'b':
1369			blocksize = parse_num_blocks2(optarg, -1);
1370			b = (blocksize > 0) ? blocksize : -blocksize;
1371			if (b < EXT2_MIN_BLOCK_SIZE ||
1372			    b > EXT2_MAX_BLOCK_SIZE) {
1373				com_err(program_name, 0,
1374					_("invalid block size - %s"), optarg);
1375				exit(1);
1376			}
1377			if (blocksize > 4096)
1378				fprintf(stderr, _("Warning: blocksize %d not "
1379						  "usable on most systems.\n"),
1380					blocksize);
1381			if (blocksize > 0)
1382				fs_param.s_log_block_size =
1383					int_log2(blocksize >>
1384						 EXT2_MIN_BLOCK_LOG_SIZE);
1385			break;
1386		case 'c':	/* Check for bad blocks */
1387			cflag++;
1388			break;
1389		case 'C':
1390			cluster_size = parse_num_blocks2(optarg, -1);
1391			if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1392			    cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1393				com_err(program_name, 0,
1394					_("invalid cluster size - %s"),
1395					optarg);
1396				exit(1);
1397			}
1398			break;
1399		case 'D':
1400			direct_io = 1;
1401			break;
1402		case 'R':
1403			com_err(program_name, 0, "%s",
1404				_("'-R' is deprecated, use '-E' instead"));
1405			/* fallthrough */
1406		case 'E':
1407			extended_opts = optarg;
1408			break;
1409		case 'F':
1410			force++;
1411			break;
1412		case 'g':
1413			fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1414			if (*tmp) {
1415				com_err(program_name, 0, "%s",
1416				_("Illegal number for blocks per group"));
1417				exit(1);
1418			}
1419			if ((fs_param.s_blocks_per_group % 8) != 0) {
1420				com_err(program_name, 0, "%s",
1421				_("blocks per group must be multiple of 8"));
1422				exit(1);
1423			}
1424			break;
1425		case 'G':
1426			flex_bg_size = strtoul(optarg, &tmp, 0);
1427			if (*tmp) {
1428				com_err(program_name, 0, "%s",
1429					_("Illegal number for flex_bg size"));
1430				exit(1);
1431			}
1432			if (flex_bg_size < 1 ||
1433			    (flex_bg_size & (flex_bg_size-1)) != 0) {
1434				com_err(program_name, 0, "%s",
1435					_("flex_bg size must be a power of 2"));
1436				exit(1);
1437			}
1438			break;
1439		case 'i':
1440			inode_ratio = strtoul(optarg, &tmp, 0);
1441			if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1442			    inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1443			    *tmp) {
1444				com_err(program_name, 0,
1445					_("invalid inode ratio %s (min %d/max %d)"),
1446					optarg, EXT2_MIN_BLOCK_SIZE,
1447					EXT2_MAX_BLOCK_SIZE * 1024);
1448				exit(1);
1449			}
1450			break;
1451		case 'I':
1452			inode_size = strtoul(optarg, &tmp, 0);
1453			if (*tmp) {
1454				com_err(program_name, 0,
1455					_("invalid inode size - %s"), optarg);
1456				exit(1);
1457			}
1458			break;
1459		case 'j':
1460			if (!journal_size)
1461				journal_size = -1;
1462			break;
1463		case 'J':
1464			parse_journal_opts(optarg);
1465			break;
1466		case 'K':
1467			fprintf(stderr, "%s",
1468				_("Warning: -K option is deprecated and "
1469				  "should not be used anymore. Use "
1470				  "\'-E nodiscard\' extended option "
1471				  "instead!\n"));
1472			discard = 0;
1473			break;
1474		case 'l':
1475			bad_blocks_filename = realloc(bad_blocks_filename,
1476						      strlen(optarg) + 1);
1477			if (!bad_blocks_filename) {
1478				com_err(program_name, ENOMEM, "%s",
1479					_("in malloc for bad_blocks_filename"));
1480				exit(1);
1481			}
1482			strcpy(bad_blocks_filename, optarg);
1483			break;
1484		case 'L':
1485			volume_label = optarg;
1486			break;
1487		case 'm':
1488			reserved_ratio = strtod(optarg, &tmp);
1489			if ( *tmp || reserved_ratio > 50 ||
1490			     reserved_ratio < 0) {
1491				com_err(program_name, 0,
1492					_("invalid reserved blocks percent - %s"),
1493					optarg);
1494				exit(1);
1495			}
1496			break;
1497		case 'M':
1498			mount_dir = optarg;
1499			break;
1500		case 'n':
1501			noaction++;
1502			break;
1503		case 'N':
1504			num_inodes = strtoul(optarg, &tmp, 0);
1505			if (*tmp) {
1506				com_err(program_name, 0,
1507					_("bad num inodes - %s"), optarg);
1508					exit(1);
1509			}
1510			break;
1511		case 'o':
1512			creator_os = optarg;
1513			break;
1514		case 'O':
1515			fs_features = optarg;
1516			break;
1517		case 'q':
1518			quiet = 1;
1519			break;
1520		case 'r':
1521			r_opt = strtoul(optarg, &tmp, 0);
1522			if (*tmp) {
1523				com_err(program_name, 0,
1524					_("bad revision level - %s"), optarg);
1525				exit(1);
1526			}
1527			fs_param.s_rev_level = r_opt;
1528			break;
1529		case 's':	/* deprecated */
1530			s_opt = atoi(optarg);
1531			break;
1532		case 'S':
1533			super_only = 1;
1534			break;
1535		case 't':
1536			if (fs_type) {
1537				com_err(program_name, 0, "%s",
1538				    _("The -t option may only be used once"));
1539				exit(1);
1540			}
1541			fs_type = strdup(optarg);
1542			break;
1543		case 'T':
1544			if (usage_types) {
1545				com_err(program_name, 0, "%s",
1546				    _("The -T option may only be used once"));
1547				exit(1);
1548			}
1549			usage_types = strdup(optarg);
1550			break;
1551		case 'U':
1552			fs_uuid = optarg;
1553			break;
1554		case 'v':
1555			verbose = 1;
1556			break;
1557		case 'V':
1558			/* Print version number and exit */
1559			show_version_only++;
1560			break;
1561		default:
1562			usage();
1563		}
1564	}
1565	if ((optind == argc) && !show_version_only)
1566		usage();
1567	device_name = argv[optind++];
1568
1569	if (!quiet || show_version_only)
1570		fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1571			 E2FSPROGS_DATE);
1572
1573	if (show_version_only) {
1574		fprintf(stderr, _("\tUsing %s\n"),
1575			error_message(EXT2_ET_BASE));
1576		exit(0);
1577	}
1578
1579	/*
1580	 * If there's no blocksize specified and there is a journal
1581	 * device, use it to figure out the blocksize
1582	 */
1583	if (blocksize <= 0 && journal_device) {
1584		ext2_filsys	jfs;
1585		io_manager	io_ptr;
1586
1587#ifdef CONFIG_TESTIO_DEBUG
1588		if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1589			io_ptr = test_io_manager;
1590			test_io_backing_manager = unix_io_manager;
1591		} else
1592#endif
1593			io_ptr = unix_io_manager;
1594		retval = ext2fs_open(journal_device,
1595				     EXT2_FLAG_JOURNAL_DEV_OK, 0,
1596				     0, io_ptr, &jfs);
1597		if (retval) {
1598			com_err(program_name, retval,
1599				_("while trying to open journal device %s\n"),
1600				journal_device);
1601			exit(1);
1602		}
1603		if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1604			com_err(program_name, 0,
1605				_("Journal dev blocksize (%d) smaller than "
1606				  "minimum blocksize %d\n"), jfs->blocksize,
1607				-blocksize);
1608			exit(1);
1609		}
1610		blocksize = jfs->blocksize;
1611		printf(_("Using journal device's blocksize: %d\n"), blocksize);
1612		fs_param.s_log_block_size =
1613			int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1614		ext2fs_close(jfs);
1615	}
1616
1617	if (optind < argc) {
1618		fs_blocks_count = parse_num_blocks2(argv[optind++],
1619						   fs_param.s_log_block_size);
1620		if (!fs_blocks_count) {
1621			com_err(program_name, 0,
1622				_("invalid blocks '%s' on device '%s'"),
1623				argv[optind - 1], device_name);
1624			exit(1);
1625		}
1626	}
1627	if (optind < argc)
1628		usage();
1629
1630	if (!force)
1631		check_plausibility(device_name);
1632	check_mount(device_name, force, _("filesystem"));
1633
1634	/* Determine the size of the device (if possible) */
1635	if (noaction && fs_blocks_count) {
1636		dev_size = fs_blocks_count;
1637		retval = 0;
1638	} else
1639		retval = ext2fs_get_device_size2(device_name,
1640						 EXT2_BLOCK_SIZE(&fs_param),
1641						 &dev_size);
1642
1643	if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1644		com_err(program_name, retval, "%s",
1645			_("while trying to determine filesystem size"));
1646		exit(1);
1647	}
1648	if (!fs_blocks_count) {
1649		if (retval == EXT2_ET_UNIMPLEMENTED) {
1650			com_err(program_name, 0, "%s",
1651				_("Couldn't determine device size; you "
1652				"must specify\nthe size of the "
1653				"filesystem\n"));
1654			exit(1);
1655		} else {
1656			if (dev_size == 0) {
1657				com_err(program_name, 0, "%s",
1658				_("Device size reported to be zero.  "
1659				  "Invalid partition specified, or\n\t"
1660				  "partition table wasn't reread "
1661				  "after running fdisk, due to\n\t"
1662				  "a modified partition being busy "
1663				  "and in use.  You may need to reboot\n\t"
1664				  "to re-read your partition table.\n"
1665				  ));
1666				exit(1);
1667			}
1668			fs_blocks_count = dev_size;
1669			if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1670				fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1671					     EXT2_BLOCK_SIZE(&fs_param))-1));
1672		}
1673	} else if (!force && (fs_blocks_count > dev_size)) {
1674		com_err(program_name, 0, "%s",
1675			_("Filesystem larger than apparent device size."));
1676		proceed_question();
1677	}
1678
1679	if (!fs_type)
1680		profile_get_string(profile, "devices", device_name,
1681				   "fs_type", 0, &fs_type);
1682	if (!usage_types)
1683		profile_get_string(profile, "devices", device_name,
1684				   "usage_types", 0, &usage_types);
1685
1686	/*
1687	 * We have the file system (or device) size, so we can now
1688	 * determine the appropriate file system types so the fs can
1689	 * be appropriately configured.
1690	 */
1691	fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1692				 fs_blocks_count ? fs_blocks_count : dev_size,
1693				 argv[0]);
1694	if (!fs_types) {
1695		fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
1696		exit(1);
1697	}
1698
1699	/* Figure out what features should be enabled */
1700
1701	tmp = NULL;
1702	if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1703		tmp = get_string_from_profile(fs_types, "base_features",
1704		      "sparse_super,filetype,resize_inode,dir_index");
1705		edit_feature(tmp, &fs_param.s_feature_compat);
1706		free(tmp);
1707
1708		/* And which mount options as well */
1709		tmp = get_string_from_profile(fs_types, "default_mntopts",
1710					      "acl,user_xattr");
1711		edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1712		if (tmp)
1713			free(tmp);
1714
1715		for (cpp = fs_types; *cpp; cpp++) {
1716			tmp = NULL;
1717			profile_get_string(profile, "fs_types", *cpp,
1718					   "features", "", &tmp);
1719			if (tmp && *tmp)
1720				edit_feature(tmp, &fs_param.s_feature_compat);
1721			if (tmp)
1722				free(tmp);
1723		}
1724		tmp = get_string_from_profile(fs_types, "default_features",
1725					      "");
1726	}
1727	edit_feature(fs_features ? fs_features : tmp,
1728		     &fs_param.s_feature_compat);
1729	if (tmp)
1730		free(tmp);
1731
1732	/* Get the hardware sector sizes, if available */
1733	retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1734	if (retval) {
1735		com_err(program_name, retval, "%s",
1736			_("while trying to determine hardware sector size"));
1737		exit(1);
1738	}
1739	retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1740	if (retval) {
1741		com_err(program_name, retval, "%s",
1742			_("while trying to determine physical sector size"));
1743		exit(1);
1744	}
1745
1746	tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1747	if (tmp != NULL)
1748		lsector_size = atoi(tmp);
1749	tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1750	if (tmp != NULL)
1751		psector_size = atoi(tmp);
1752
1753	/* Older kernels may not have physical/logical distinction */
1754	if (!psector_size)
1755		psector_size = lsector_size;
1756
1757	if (blocksize <= 0) {
1758		use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1759
1760		if (use_bsize == -1) {
1761			use_bsize = sys_page_size;
1762			if ((linux_version_code < (2*65536 + 6*256)) &&
1763			    (use_bsize > 4096))
1764				use_bsize = 4096;
1765		}
1766		if (lsector_size && use_bsize < lsector_size)
1767			use_bsize = lsector_size;
1768		if ((blocksize < 0) && (use_bsize < (-blocksize)))
1769			use_bsize = -blocksize;
1770		blocksize = use_bsize;
1771		fs_blocks_count /= (blocksize / 1024);
1772	} else {
1773		if (blocksize < lsector_size) {			/* Impossible */
1774			com_err(program_name, EINVAL, "%s",
1775				_("while setting blocksize; too small "
1776				  "for device\n"));
1777			exit(1);
1778		} else if ((blocksize < psector_size) &&
1779			   (psector_size <= sys_page_size)) {	/* Suboptimal */
1780			fprintf(stderr, _("Warning: specified blocksize %d is "
1781				"less than device physical sectorsize %d\n"),
1782				blocksize, psector_size);
1783		}
1784	}
1785
1786	fs_param.s_log_block_size =
1787		int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1788
1789	/*
1790	 * We now need to do a sanity check of fs_blocks_count for
1791	 * 32-bit vs 64-bit block number support.
1792	 */
1793	if ((fs_blocks_count > MAX_32_NUM) &&
1794	    !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1795	    get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1796		fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1797		fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1798	}
1799	if ((fs_blocks_count > MAX_32_NUM) &&
1800	    !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1801		fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1802				  "too big to be expressed\n\t"
1803				  "in 32 bits using a blocksize of %d.\n"),
1804			program_name, fs_blocks_count, device_name,
1805			EXT2_BLOCK_SIZE(&fs_param));
1806		exit(1);
1807	}
1808
1809	ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1810
1811	if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1812		fs_types[0] = strdup("journal");
1813		fs_types[1] = 0;
1814	}
1815
1816	if (verbose) {
1817		fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1818		print_str_list(fs_types);
1819	}
1820
1821	if (r_opt == EXT2_GOOD_OLD_REV &&
1822	    (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1823	     fs_param.s_feature_ro_compat)) {
1824		fprintf(stderr, "%s", _("Filesystem features not supported "
1825					"with revision 0 filesystems\n"));
1826		exit(1);
1827	}
1828
1829	if (s_opt > 0) {
1830		if (r_opt == EXT2_GOOD_OLD_REV) {
1831			fprintf(stderr, "%s",
1832				_("Sparse superblocks not supported "
1833				  "with revision 0 filesystems\n"));
1834			exit(1);
1835		}
1836		fs_param.s_feature_ro_compat |=
1837			EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1838	} else if (s_opt == 0)
1839		fs_param.s_feature_ro_compat &=
1840			~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1841
1842	if (journal_size != 0) {
1843		if (r_opt == EXT2_GOOD_OLD_REV) {
1844			fprintf(stderr, "%s", _("Journals not supported with "
1845						"revision 0 filesystems\n"));
1846			exit(1);
1847		}
1848		fs_param.s_feature_compat |=
1849			EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1850	}
1851
1852	/* Get reserved_ratio from profile if not specified on cmd line. */
1853	if (reserved_ratio < 0.0) {
1854		reserved_ratio = get_double_from_profile(
1855					fs_types, "reserved_ratio", 5.0);
1856		if (reserved_ratio > 50 || reserved_ratio < 0) {
1857			com_err(program_name, 0,
1858				_("invalid reserved blocks percent - %lf"),
1859				reserved_ratio);
1860			exit(1);
1861		}
1862	}
1863
1864	if (fs_param.s_feature_incompat &
1865	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1866		reserved_ratio = 0;
1867		fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1868		fs_param.s_feature_compat = 0;
1869		fs_param.s_feature_ro_compat = 0;
1870 	}
1871
1872	/* Check the user's mkfs options for 64bit */
1873	if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1874	    !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
1875		printf("%s", _("Extents MUST be enabled for a 64-bit "
1876			       "filesystem.  Pass -O extents to rectify.\n"));
1877		exit(1);
1878	}
1879
1880	/* Set first meta blockgroup via an environment variable */
1881	/* (this is mostly for debugging purposes) */
1882	if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1883	    ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1884		fs_param.s_first_meta_bg = atoi(tmp);
1885	if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
1886		if (!cluster_size)
1887			cluster_size = get_int_from_profile(fs_types,
1888							    "cluster_size",
1889							    blocksize*16);
1890		fs_param.s_log_cluster_size =
1891			int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
1892		if (fs_param.s_log_cluster_size &&
1893		    fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
1894			com_err(program_name, 0, "%s",
1895				_("The cluster size may not be "
1896				  "smaller than the block size.\n"));
1897			exit(1);
1898		}
1899	} else if (cluster_size) {
1900		com_err(program_name, 0, "%s",
1901			_("specifying a cluster size requires the "
1902			  "bigalloc feature"));
1903		exit(1);
1904	} else
1905		fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1906
1907	if (inode_ratio == 0) {
1908		inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1909						   8192);
1910		if (inode_ratio < blocksize)
1911			inode_ratio = blocksize;
1912		if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1913			inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
1914	}
1915
1916#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1917	retval = get_device_geometry(device_name, &fs_param, psector_size);
1918	if (retval < 0) {
1919		fprintf(stderr,
1920			_("warning: Unable to get device geometry for %s\n"),
1921			device_name);
1922	} else if (retval) {
1923		printf(_("%s alignment is offset by %lu bytes.\n"),
1924		       device_name, retval);
1925		printf(_("This may result in very poor performance, "
1926			  "(re)-partitioning suggested.\n"));
1927	}
1928#endif
1929
1930	blocksize = EXT2_BLOCK_SIZE(&fs_param);
1931
1932	/*
1933	 * Initialize s_desc_size so that the parse_extended_opts()
1934	 * can correctly handle "-E resize=NNN" if the 64-bit option
1935	 * is set.
1936	 */
1937	if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
1938		fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
1939
1940	/* This check should happen beyond the last assignment to blocksize */
1941	if (blocksize > sys_page_size) {
1942		if (!force) {
1943			com_err(program_name, 0,
1944				_("%d-byte blocks too big for system (max %d)"),
1945				blocksize, sys_page_size);
1946			proceed_question();
1947		}
1948		fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1949				  "(max %d), forced to continue\n"),
1950			blocksize, sys_page_size);
1951	}
1952
1953	lazy_itable_init = 0;
1954	if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1955		lazy_itable_init = 1;
1956
1957	lazy_itable_init = get_bool_from_profile(fs_types,
1958						 "lazy_itable_init",
1959						 lazy_itable_init);
1960	discard = get_bool_from_profile(fs_types, "discard" , discard);
1961	journal_flags |= get_bool_from_profile(fs_types,
1962					       "lazy_journal_init", 0) ?
1963					       EXT2_MKJOURNAL_LAZYINIT : 0;
1964	journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
1965
1966	/* Get options from profile */
1967	for (cpp = fs_types; *cpp; cpp++) {
1968		tmp = NULL;
1969		profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1970			if (tmp && *tmp)
1971				parse_extended_opts(&fs_param, tmp);
1972			free(tmp);
1973	}
1974
1975	if (extended_opts)
1976		parse_extended_opts(&fs_param, extended_opts);
1977
1978	/* Can't support bigalloc feature without extents feature */
1979	if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
1980	    !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
1981		com_err(program_name, 0, "%s",
1982			_("Can't support bigalloc feature without "
1983			  "extents feature"));
1984		exit(1);
1985	}
1986
1987	if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1988	    (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1989		fprintf(stderr, "%s", _("The resize_inode and meta_bg "
1990					"features are not compatible.\n"
1991					"They can not be both enabled "
1992					"simultaneously.\n"));
1993		exit(1);
1994	}
1995
1996	if (!quiet &&
1997	    (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
1998		fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
1999				  "still under development\n"
2000				  "See https://ext4.wiki.kernel.org/"
2001				  "index.php/Bigalloc for more information\n\n"));
2002
2003	if (!quiet &&
2004	    (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
2005		fprintf(stderr, "%s", _("\nWarning: the quota feature is "
2006				  "still under development\n"
2007				  "See https://ext4.wiki.kernel.org/"
2008				  "index.php/Quota for more information\n\n"));
2009
2010	/* Since sparse_super is the default, we would only have a problem
2011	 * here if it was explicitly disabled.
2012	 */
2013	if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2014	    !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
2015		com_err(program_name, 0, "%s",
2016			_("reserved online resize blocks not supported "
2017			  "on non-sparse filesystem"));
2018		exit(1);
2019	}
2020
2021	if (fs_param.s_blocks_per_group) {
2022		if (fs_param.s_blocks_per_group < 256 ||
2023		    fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
2024			com_err(program_name, 0, "%s",
2025				_("blocks per group count out of range"));
2026			exit(1);
2027		}
2028	}
2029
2030	/*
2031	 * If the bigalloc feature is enabled, then the -g option will
2032	 * specify the number of clusters per group.
2033	 */
2034	if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2035		fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2036		fs_param.s_blocks_per_group = 0;
2037	}
2038
2039	if (inode_size == 0)
2040		inode_size = get_int_from_profile(fs_types, "inode_size", 0);
2041	if (!flex_bg_size && (fs_param.s_feature_incompat &
2042			      EXT4_FEATURE_INCOMPAT_FLEX_BG))
2043		flex_bg_size = get_int_from_profile(fs_types,
2044						    "flex_bg_size", 16);
2045	if (flex_bg_size) {
2046		if (!(fs_param.s_feature_incompat &
2047		      EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
2048			com_err(program_name, 0, "%s",
2049				_("Flex_bg feature not enabled, so "
2050				  "flex_bg size may not be specified"));
2051			exit(1);
2052		}
2053		fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2054	}
2055
2056	if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2057		if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
2058		    inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2059		    inode_size & (inode_size - 1)) {
2060			com_err(program_name, 0,
2061				_("invalid inode size %d (min %d/max %d)"),
2062				inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2063				blocksize);
2064			exit(1);
2065		}
2066		fs_param.s_inode_size = inode_size;
2067	}
2068
2069	/* Make sure number of inodes specified will fit in 32 bits */
2070	if (num_inodes == 0) {
2071		unsigned long long n;
2072		n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2073		if (n > MAX_32_NUM) {
2074			if (fs_param.s_feature_incompat &
2075			    EXT4_FEATURE_INCOMPAT_64BIT)
2076				num_inodes = MAX_32_NUM;
2077			else {
2078				com_err(program_name, 0,
2079					_("too many inodes (%llu), raise "
2080					  "inode ratio?"), n);
2081				exit(1);
2082			}
2083		}
2084	} else if (num_inodes > MAX_32_NUM) {
2085		com_err(program_name, 0,
2086			_("too many inodes (%llu), specify < 2^32 inodes"),
2087			  num_inodes);
2088		exit(1);
2089	}
2090	/*
2091	 * Calculate number of inodes based on the inode ratio
2092	 */
2093	fs_param.s_inodes_count = num_inodes ? num_inodes :
2094		(ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
2095
2096	if ((((unsigned long long)fs_param.s_inodes_count) *
2097	     (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
2098	    ((ext2fs_blocks_count(&fs_param)) *
2099	     EXT2_BLOCK_SIZE(&fs_param))) {
2100		com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2101					  "(%u) too big for a\n\t"
2102					  "filesystem with %llu blocks, "
2103					  "specify higher inode_ratio (-i)\n\t"
2104					  "or lower inode count (-N).\n"),
2105			inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2106			fs_param.s_inodes_count,
2107			(unsigned long long) ext2fs_blocks_count(&fs_param));
2108		exit(1);
2109	}
2110
2111	/*
2112	 * Calculate number of blocks to reserve
2113	 */
2114	ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2115				  ext2fs_blocks_count(&fs_param) / 100.0);
2116
2117	free(fs_type);
2118	free(usage_types);
2119}
2120
2121static int should_do_undo(const char *name)
2122{
2123	errcode_t retval;
2124	io_channel channel;
2125	__u16	s_magic;
2126	struct ext2_super_block super;
2127	io_manager manager = unix_io_manager;
2128	int csum_flag, force_undo;
2129
2130	csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2131					       EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2132	force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2133	if (!force_undo && (!csum_flag || !lazy_itable_init))
2134		return 0;
2135
2136	retval = manager->open(name, IO_FLAG_EXCLUSIVE,  &channel);
2137	if (retval) {
2138		/*
2139		 * We don't handle error cases instead we
2140		 * declare that the file system doesn't exist
2141		 * and let the rest of mke2fs take care of
2142		 * error
2143		 */
2144		retval = 0;
2145		goto open_err_out;
2146	}
2147
2148	io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2149	retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2150	if (retval) {
2151		retval = 0;
2152		goto err_out;
2153	}
2154
2155#if defined(WORDS_BIGENDIAN)
2156	s_magic = ext2fs_swab16(super.s_magic);
2157#else
2158	s_magic = super.s_magic;
2159#endif
2160
2161	if (s_magic == EXT2_SUPER_MAGIC)
2162		retval = 1;
2163
2164err_out:
2165	io_channel_close(channel);
2166
2167open_err_out:
2168
2169	return retval;
2170}
2171
2172static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2173{
2174	errcode_t retval = ENOMEM;
2175	char *tdb_dir = NULL, *tdb_file = NULL;
2176	char *dev_name, *tmp_name;
2177	int free_tdb_dir = 0;
2178
2179	/*
2180	 * Configuration via a conf file would be
2181	 * nice
2182	 */
2183	tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2184	if (!tdb_dir) {
2185		profile_get_string(profile, "defaults",
2186				   "undo_dir", 0, "/var/lib/e2fsprogs",
2187				   &tdb_dir);
2188		free_tdb_dir = 1;
2189	}
2190
2191	if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2192	    access(tdb_dir, W_OK)) {
2193		if (free_tdb_dir)
2194			free(tdb_dir);
2195		return 0;
2196	}
2197
2198	tmp_name = strdup(name);
2199	if (!tmp_name)
2200		goto errout;
2201	dev_name = basename(tmp_name);
2202	tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2203	if (!tdb_file) {
2204		free(tmp_name);
2205		goto errout;
2206	}
2207	sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2208	free(tmp_name);
2209
2210	if (!access(tdb_file, F_OK)) {
2211		if (unlink(tdb_file) < 0) {
2212			retval = errno;
2213			goto errout;
2214		}
2215	}
2216
2217	set_undo_io_backing_manager(*io_ptr);
2218	*io_ptr = undo_io_manager;
2219	retval = set_undo_io_backup_file(tdb_file);
2220	if (retval)
2221		goto errout;
2222	printf(_("Overwriting existing filesystem; this can be undone "
2223		 "using the command:\n"
2224		 "    e2undo %s %s\n\n"), tdb_file, name);
2225
2226	if (free_tdb_dir)
2227		free(tdb_dir);
2228	free(tdb_file);
2229	return 0;
2230
2231errout:
2232	if (free_tdb_dir)
2233		free(tdb_dir);
2234	free(tdb_file);
2235	com_err(program_name, retval, "%s",
2236		_("while trying to setup undo file\n"));
2237	return retval;
2238}
2239
2240static int mke2fs_discard_device(ext2_filsys fs)
2241{
2242	struct ext2fs_numeric_progress_struct progress;
2243	blk64_t blocks = ext2fs_blocks_count(fs->super);
2244	blk64_t count = DISCARD_STEP_MB;
2245	blk64_t cur;
2246	int retval = 0;
2247
2248	/*
2249	 * Let's try if discard really works on the device, so
2250	 * we do not print numeric progress resulting in failure
2251	 * afterwards.
2252	 */
2253	retval = io_channel_discard(fs->io, 0, fs->blocksize);
2254	if (retval)
2255		return retval;
2256	cur = fs->blocksize;
2257
2258	count *= (1024 * 1024);
2259	count /= fs->blocksize;
2260
2261	ext2fs_numeric_progress_init(fs, &progress,
2262				     _("Discarding device blocks: "),
2263				     blocks);
2264	while (cur < blocks) {
2265		ext2fs_numeric_progress_update(fs, &progress, cur);
2266
2267		if (cur + count > blocks)
2268			count = blocks - cur;
2269
2270		retval = io_channel_discard(fs->io, cur, count);
2271		if (retval)
2272			break;
2273		cur += count;
2274	}
2275
2276	if (retval) {
2277		ext2fs_numeric_progress_close(fs, &progress,
2278				      _("failed - "));
2279		if (!quiet)
2280			printf("%s\n",error_message(retval));
2281	} else
2282		ext2fs_numeric_progress_close(fs, &progress,
2283				      _("done                            \n"));
2284
2285	return retval;
2286}
2287
2288static void fix_cluster_bg_counts(ext2_filsys fs)
2289{
2290	blk64_t	cluster, num_clusters, tot_free;
2291	unsigned num = 0;
2292	int	grp_free, num_free, group;
2293
2294	num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2295	tot_free = num_free = group = grp_free = 0;
2296	for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2297	     cluster < num_clusters; cluster++) {
2298		if (!ext2fs_test_block_bitmap2(fs->block_map,
2299					       EXT2FS_C2B(fs, cluster))) {
2300			grp_free++;
2301			tot_free++;
2302		}
2303		num++;
2304		if ((num == fs->super->s_clusters_per_group) ||
2305		    (cluster == num_clusters-1)) {
2306			ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2307			ext2fs_group_desc_csum_set(fs, group);
2308			num = 0;
2309			grp_free = 0;
2310			group++;
2311		}
2312	}
2313	ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
2314}
2315
2316static int create_quota_inodes(ext2_filsys fs)
2317{
2318	quota_ctx_t qctx;
2319
2320	quota_init_context(&qctx, fs, -1);
2321	quota_compute_usage(qctx);
2322	quota_write_inode(qctx, quotatype);
2323	quota_release_context(&qctx);
2324
2325	return 0;
2326}
2327
2328int main (int argc, char *argv[])
2329{
2330	errcode_t	retval = 0;
2331	ext2_filsys	fs;
2332	badblocks_list	bb_list = 0;
2333	unsigned int	journal_blocks;
2334	unsigned int	i, checkinterval;
2335	int		max_mnt_count;
2336	int		val, hash_alg;
2337	int		flags;
2338	int		old_bitmaps;
2339	io_manager	io_ptr;
2340	char		tdb_string[40];
2341	char		*hash_alg_str;
2342	int		itable_zeroed = 0;
2343
2344#ifdef ENABLE_NLS
2345	setlocale(LC_MESSAGES, "");
2346	setlocale(LC_CTYPE, "");
2347	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2348	textdomain(NLS_CAT_NAME);
2349	set_com_err_gettext(gettext);
2350#endif
2351	PRS(argc, argv);
2352
2353#ifdef CONFIG_TESTIO_DEBUG
2354	if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2355		io_ptr = test_io_manager;
2356		test_io_backing_manager = unix_io_manager;
2357	} else
2358#endif
2359		io_ptr = unix_io_manager;
2360
2361	if (should_do_undo(device_name)) {
2362		retval = mke2fs_setup_tdb(device_name, &io_ptr);
2363		if (retval)
2364			exit(1);
2365	}
2366
2367	/*
2368	 * Initialize the superblock....
2369	 */
2370	flags = EXT2_FLAG_EXCLUSIVE;
2371	if (direct_io)
2372		flags |= EXT2_FLAG_DIRECT_IO;
2373	profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2374			    &old_bitmaps);
2375	if (!old_bitmaps)
2376		flags |= EXT2_FLAG_64BITS;
2377	/*
2378	 * By default, we print how many inode tables or block groups
2379	 * or whatever we've written so far.  The quiet flag disables
2380	 * this, along with a lot of other output.
2381	 */
2382	if (!quiet)
2383		flags |= EXT2_FLAG_PRINT_PROGRESS;
2384	retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2385	if (retval) {
2386		com_err(device_name, retval, "%s",
2387			_("while setting up superblock"));
2388		exit(1);
2389	}
2390
2391	/* Calculate journal blocks */
2392	if (!journal_device && ((journal_size) ||
2393		(fs_param.s_feature_compat &
2394		 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2395		journal_blocks = figure_journal_size(journal_size, fs);
2396
2397	/* Can't undo discard ... */
2398	if (!noaction && discard && (io_ptr != undo_io_manager)) {
2399		retval = mke2fs_discard_device(fs);
2400		if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2401			if (verbose)
2402				printf("%s",
2403				       _("Discard succeeded and will return "
2404					 "0s - skipping inode table wipe\n"));
2405			lazy_itable_init = 1;
2406			itable_zeroed = 1;
2407		}
2408	}
2409
2410	sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2411		32768 : fs->blocksize * 8);
2412	io_channel_set_options(fs->io, tdb_string);
2413
2414	if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2415		fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2416
2417	if ((fs_param.s_feature_incompat &
2418	     (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2419	    (fs_param.s_feature_ro_compat &
2420	     (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2421	      EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2422	      EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2423		fs->super->s_kbytes_written = 1;
2424
2425	/*
2426	 * Wipe out the old on-disk superblock
2427	 */
2428	if (!noaction)
2429		zap_sector(fs, 2, 6);
2430
2431	/*
2432	 * Parse or generate a UUID for the filesystem
2433	 */
2434	if (fs_uuid) {
2435		if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2436			com_err(device_name, 0, "could not parse UUID: %s\n",
2437				fs_uuid);
2438			exit(1);
2439		}
2440	} else
2441		uuid_generate(fs->super->s_uuid);
2442
2443	/*
2444	 * Initialize the directory index variables
2445	 */
2446	hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2447					       "half_md4");
2448	hash_alg = e2p_string2hash(hash_alg_str);
2449	free(hash_alg_str);
2450	fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2451		EXT2_HASH_HALF_MD4;
2452	uuid_generate((unsigned char *) fs->super->s_hash_seed);
2453
2454	/*
2455	 * Periodic checks can be enabled/disabled via config file.
2456	 * Note we override the kernel include file's idea of what the default
2457	 * check interval (never) should be.  It's a good idea to check at
2458	 * least *occasionally*, specially since servers will never rarely get
2459	 * to reboot, since Linux is so robust these days.  :-)
2460	 *
2461	 * 180 days (six months) seems like a good value.
2462	 */
2463#ifdef EXT2_DFL_CHECKINTERVAL
2464#undef EXT2_DFL_CHECKINTERVAL
2465#endif
2466#define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2467
2468	if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2469		fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2470		fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2471		/*
2472		 * Add "jitter" to the superblock's check interval so that we
2473		 * don't check all the filesystems at the same time.  We use a
2474		 * kludgy hack of using the UUID to derive a random jitter value
2475		 */
2476		for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2477			val += fs->super->s_uuid[i];
2478		fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2479	} else
2480		fs->super->s_max_mnt_count = -1;
2481
2482	/*
2483	 * Override the creator OS, if applicable
2484	 */
2485	if (creator_os && !set_os(fs->super, creator_os)) {
2486		com_err (program_name, 0, _("unknown os - %s"), creator_os);
2487		exit(1);
2488	}
2489
2490	/*
2491	 * For the Hurd, we will turn off filetype since it doesn't
2492	 * support it.
2493	 */
2494	if (fs->super->s_creator_os == EXT2_OS_HURD)
2495		fs->super->s_feature_incompat &=
2496			~EXT2_FEATURE_INCOMPAT_FILETYPE;
2497
2498	/*
2499	 * Set the volume label...
2500	 */
2501	if (volume_label) {
2502		memset(fs->super->s_volume_name, 0,
2503		       sizeof(fs->super->s_volume_name));
2504		strncpy(fs->super->s_volume_name, volume_label,
2505			sizeof(fs->super->s_volume_name));
2506	}
2507
2508	/*
2509	 * Set the last mount directory
2510	 */
2511	if (mount_dir) {
2512		memset(fs->super->s_last_mounted, 0,
2513		       sizeof(fs->super->s_last_mounted));
2514		strncpy(fs->super->s_last_mounted, mount_dir,
2515			sizeof(fs->super->s_last_mounted));
2516	}
2517
2518	if (!quiet || noaction)
2519		show_stats(fs);
2520
2521	if (noaction)
2522		exit(0);
2523
2524	if (fs->super->s_feature_incompat &
2525	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2526		create_journal_dev(fs);
2527		exit(ext2fs_close(fs) ? 1 : 0);
2528	}
2529
2530	if (bad_blocks_filename)
2531		read_bb_file(fs, &bb_list, bad_blocks_filename);
2532	if (cflag)
2533		test_disk(fs, &bb_list);
2534
2535	handle_bad_blocks(fs, bb_list);
2536	fs->stride = fs_stride = fs->super->s_raid_stride;
2537	if (!quiet)
2538		printf("%s", _("Allocating group tables: "));
2539	retval = ext2fs_allocate_tables(fs);
2540	if (retval) {
2541		com_err(program_name, retval, "%s",
2542			_("while trying to allocate filesystem tables"));
2543		exit(1);
2544	}
2545	if (!quiet)
2546		printf("%s", _("done                            \n"));
2547
2548	retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2549	if (retval) {
2550		com_err(program_name, retval, "%s",
2551			_("\n\twhile converting subcluster bitmap"));
2552		exit(1);
2553	}
2554
2555	if (super_only) {
2556		fs->super->s_state |= EXT2_ERROR_FS;
2557		fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2558		/*
2559		 * The command "mke2fs -S" is used to recover
2560		 * corrupted file systems, so do not mark any of the
2561		 * inodes as unused; we want e2fsck to consider all
2562		 * inodes as potentially containing recoverable data.
2563		 */
2564		if (fs->super->s_feature_ro_compat &
2565		    EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2566			for (i = 0; i < fs->group_desc_count; i++)
2567				ext2fs_bg_itable_unused_set(fs, i, 0);
2568		}
2569	} else {
2570		/* rsv must be a power of two (64kB is MD RAID sb alignment) */
2571		blk64_t rsv = 65536 / fs->blocksize;
2572		blk64_t blocks = ext2fs_blocks_count(fs->super);
2573		blk64_t start;
2574		blk64_t ret_blk;
2575
2576#ifdef ZAP_BOOTBLOCK
2577		zap_sector(fs, 0, 2);
2578#endif
2579
2580		/*
2581		 * Wipe out any old MD RAID (or other) metadata at the end
2582		 * of the device.  This will also verify that the device is
2583		 * as large as we think.  Be careful with very small devices.
2584		 */
2585		start = (blocks & ~(rsv - 1));
2586		if (start > rsv)
2587			start -= rsv;
2588		if (start > 0)
2589			retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2590						    &ret_blk, NULL);
2591
2592		if (retval) {
2593			com_err(program_name, retval,
2594				_("while zeroing block %llu at end of filesystem"),
2595				ret_blk);
2596		}
2597		write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2598		create_root_dir(fs);
2599		create_lost_and_found(fs);
2600		reserve_inodes(fs);
2601		create_bad_block_inode(fs, bb_list);
2602		if (fs->super->s_feature_compat &
2603		    EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2604			retval = ext2fs_create_resize_inode(fs);
2605			if (retval) {
2606				com_err("ext2fs_create_resize_inode", retval,
2607					"%s",
2608				_("while reserving blocks for online resize"));
2609				exit(1);
2610			}
2611		}
2612	}
2613
2614	if (journal_device) {
2615		ext2_filsys	jfs;
2616
2617		if (!force)
2618			check_plausibility(journal_device);
2619		check_mount(journal_device, force, _("journal"));
2620
2621		retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2622				     EXT2_FLAG_JOURNAL_DEV_OK, 0,
2623				     fs->blocksize, unix_io_manager, &jfs);
2624		if (retval) {
2625			com_err(program_name, retval,
2626				_("while trying to open journal device %s\n"),
2627				journal_device);
2628			exit(1);
2629		}
2630		if (!quiet) {
2631			printf(_("Adding journal to device %s: "),
2632			       journal_device);
2633			fflush(stdout);
2634		}
2635		retval = ext2fs_add_journal_device(fs, jfs);
2636		if(retval) {
2637			com_err (program_name, retval,
2638				 _("\n\twhile trying to add journal to device %s"),
2639				 journal_device);
2640			exit(1);
2641		}
2642		if (!quiet)
2643			printf("%s", _("done\n"));
2644		ext2fs_close(jfs);
2645		free(journal_device);
2646	} else if ((journal_size) ||
2647		   (fs_param.s_feature_compat &
2648		    EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2649		if (super_only) {
2650			printf("%s", _("Skipping journal creation in super-only mode\n"));
2651			fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2652			goto no_journal;
2653		}
2654
2655		if (!journal_blocks) {
2656			fs->super->s_feature_compat &=
2657				~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2658			goto no_journal;
2659		}
2660		if (!quiet) {
2661			printf(_("Creating journal (%u blocks): "),
2662			       journal_blocks);
2663			fflush(stdout);
2664		}
2665		retval = ext2fs_add_journal_inode(fs, journal_blocks,
2666						  journal_flags);
2667		if (retval) {
2668			com_err(program_name, retval, "%s",
2669				_("\n\twhile trying to create journal"));
2670			exit(1);
2671		}
2672		if (!quiet)
2673			printf("%s", _("done\n"));
2674	}
2675no_journal:
2676	if (!super_only &&
2677	    fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2678		retval = ext2fs_mmp_init(fs);
2679		if (retval) {
2680			fprintf(stderr, "%s",
2681				_("\nError while enabling multiple "
2682				  "mount protection feature."));
2683			exit(1);
2684		}
2685		if (!quiet)
2686			printf(_("Multiple mount protection is enabled "
2687				 "with update interval %d seconds.\n"),
2688			       fs->super->s_mmp_update_interval);
2689	}
2690
2691	if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2692				       EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2693		fix_cluster_bg_counts(fs);
2694	if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2695				       EXT4_FEATURE_RO_COMPAT_QUOTA))
2696		create_quota_inodes(fs);
2697
2698	if (!quiet)
2699		printf("%s", _("Writing superblocks and "
2700		       "filesystem accounting information: "));
2701	checkinterval = fs->super->s_checkinterval;
2702	max_mnt_count = fs->super->s_max_mnt_count;
2703	retval = ext2fs_close(fs);
2704	if (retval) {
2705		fprintf(stderr, "%s",
2706			_("\nWarning, had trouble writing out superblocks."));
2707	} else if (!quiet) {
2708		printf("%s", _("done\n\n"));
2709		if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2710			print_check_message(max_mnt_count, checkinterval);
2711	}
2712
2713	remove_error_table(&et_ext2_error_table);
2714	remove_error_table(&et_prof_error_table);
2715	profile_release(profile);
2716	for (i=0; fs_types[i]; i++)
2717		free(fs_types[i]);
2718	free(fs_types);
2719	return retval;
2720}
2721