mke2fs.c revision 4efbac6fed75c29d3d5f1b676b932754653a2ac5
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 <stdio.h>
22#include <string.h>
23#include <strings.h>
24#include <fcntl.h>
25#include <ctype.h>
26#include <time.h>
27#ifdef __linux__
28#include <sys/utsname.h>
29#endif
30#ifdef HAVE_GETOPT_H
31#include <getopt.h>
32#else
33extern char *optarg;
34extern int optind;
35#endif
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
39#ifdef HAVE_STDLIB_H
40#include <stdlib.h>
41#endif
42#ifdef HAVE_ERRNO_H
43#include <errno.h>
44#endif
45#ifdef HAVE_MNTENT_H
46#include <mntent.h>
47#endif
48#include <sys/ioctl.h>
49#include <sys/types.h>
50#include <libgen.h>
51#include <limits.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
65#define STRIDE_LENGTH 8
66
67#ifndef __sparc__
68#define ZAP_BOOTBLOCK
69#endif
70
71extern int isatty(int);
72extern FILE *fpopen(const char *cmd, const char *mode);
73
74const char * program_name = "mke2fs";
75const char * device_name /* = NULL */;
76
77/* Command line options */
78int	cflag;
79int	verbose;
80int	quiet;
81int	super_only;
82int	force;
83int	noaction;
84int	journal_size;
85int	journal_flags;
86int	lazy_itable_init;
87char	*bad_blocks_filename;
88__u32	fs_stride;
89
90struct ext2_super_block fs_param;
91char *fs_uuid = NULL;
92char *creator_os;
93char *volume_label;
94char *mount_dir;
95char *journal_device;
96int sync_kludge;	/* Set using the MKE2FS_SYNC env. option */
97char **fs_types;
98
99profile_t	profile;
100
101int sys_page_size = 4096;
102int linux_version_code = 0;
103
104static void usage(void)
105{
106	fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
107	"[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
108	"[-J journal-options]\n"
109	"\t[-G meta group size] [-N number-of-inodes]\n"
110	"\t[-m reserved-blocks-percentage] [-o creator-os]\n"
111	"\t[-g blocks-per-group] [-L volume-label] "
112	"[-M last-mounted-directory]\n\t[-O feature[,...]] "
113	"[-r fs-revision] [-E extended-option[,...]]\n"
114	"\t[-T fs-type] [-U UUID] [-jnqvFSV] device [blocks-count]\n"),
115		program_name);
116	exit(1);
117}
118
119static int int_log2(int arg)
120{
121	int	l = 0;
122
123	arg >>= 1;
124	while (arg) {
125		l++;
126		arg >>= 1;
127	}
128	return l;
129}
130
131static int int_log10(unsigned int arg)
132{
133	int	l;
134
135	for (l=0; arg ; l++)
136		arg = arg / 10;
137	return l;
138}
139
140static int parse_version_number(const char *s)
141{
142	int	major, minor, rev;
143	char	*endptr;
144	const char *cp = s;
145
146	if (!s)
147		return 0;
148	major = strtol(cp, &endptr, 10);
149	if (cp == endptr || *endptr != '.')
150		return 0;
151	cp = endptr + 1;
152	minor = strtol(cp, &endptr, 10);
153	if (cp == endptr || *endptr != '.')
154		return 0;
155	cp = endptr + 1;
156	rev = strtol(cp, &endptr, 10);
157	if (cp == endptr)
158		return 0;
159	return ((((major * 256) + minor) * 256) + rev);
160}
161
162/*
163 * Helper function for read_bb_file and test_disk
164 */
165static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
166{
167	fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
168	return;
169}
170
171/*
172 * Reads the bad blocks list from a file
173 */
174static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
175			 const char *bad_blocks_file)
176{
177	FILE		*f;
178	errcode_t	retval;
179
180	f = fopen(bad_blocks_file, "r");
181	if (!f) {
182		com_err("read_bad_blocks_file", errno,
183			_("while trying to open %s"), bad_blocks_file);
184		exit(1);
185	}
186	retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
187	fclose (f);
188	if (retval) {
189		com_err("ext2fs_read_bb_FILE", retval,
190			_("while reading in list of bad blocks from file"));
191		exit(1);
192	}
193}
194
195/*
196 * Runs the badblocks program to test the disk
197 */
198static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
199{
200	FILE		*f;
201	errcode_t	retval;
202	char		buf[1024];
203
204	sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
205		quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
206		fs->device_name, ext2fs_blocks_count(fs->super)-1);
207	if (verbose)
208		printf(_("Running command: %s\n"), buf);
209	f = popen(buf, "r");
210	if (!f) {
211		com_err("popen", errno,
212			_("while trying to run '%s'"), buf);
213		exit(1);
214	}
215	retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
216	pclose(f);
217	if (retval) {
218		com_err("ext2fs_read_bb_FILE", retval,
219			_("while processing list of bad blocks from program"));
220		exit(1);
221	}
222}
223
224static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
225{
226	dgrp_t			i;
227	blk_t			j;
228	unsigned 		must_be_good;
229	blk_t			blk;
230	badblocks_iterate	bb_iter;
231	errcode_t		retval;
232	blk_t			group_block;
233	int			group;
234	int			group_bad;
235
236	if (!bb_list)
237		return;
238
239	/*
240	 * The primary superblock and group descriptors *must* be
241	 * good; if not, abort.
242	 */
243	must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
244	for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
245		if (ext2fs_badblocks_list_test(bb_list, i)) {
246			fprintf(stderr, _("Block %d in primary "
247				"superblock/group descriptor area bad.\n"), i);
248			fprintf(stderr, _("Blocks %u through %u must be good "
249				"in order to build a filesystem.\n"),
250				fs->super->s_first_data_block, must_be_good);
251			fputs(_("Aborting....\n"), stderr);
252			exit(1);
253		}
254	}
255
256	/*
257	 * See if any of the bad blocks are showing up in the backup
258	 * superblocks and/or group descriptors.  If so, issue a
259	 * warning and adjust the block counts appropriately.
260	 */
261	group_block = fs->super->s_first_data_block +
262		fs->super->s_blocks_per_group;
263
264	for (i = 1; i < fs->group_desc_count; i++) {
265		group_bad = 0;
266		for (j=0; j < fs->desc_blocks+1; j++) {
267			if (ext2fs_badblocks_list_test(bb_list,
268						       group_block + j)) {
269				if (!group_bad)
270					fprintf(stderr,
271_("Warning: the backup superblock/group descriptors at block %u contain\n"
272"	bad blocks.\n\n"),
273						group_block);
274				group_bad++;
275				group = ext2fs_group_of_blk(fs, group_block+j);
276				fs->group_desc[group].bg_free_blocks_count++;
277				ext2fs_group_desc_csum_set(fs, group);
278				ext2fs_free_blocks_count_add(fs->super, 1);
279			}
280		}
281		group_block += fs->super->s_blocks_per_group;
282	}
283
284	/*
285	 * Mark all the bad blocks as used...
286	 */
287	retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
288	if (retval) {
289		com_err("ext2fs_badblocks_list_iterate_begin", retval,
290			_("while marking bad blocks as used"));
291		exit(1);
292	}
293	while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
294		ext2fs_mark_block_bitmap2(fs->block_map, blk);
295	ext2fs_badblocks_list_iterate_end(bb_iter);
296}
297
298static void write_inode_tables(ext2_filsys fs, int lazy_flag)
299{
300	errcode_t	retval;
301	blk_t		blk;
302	dgrp_t		i;
303	int		num, ipb;
304	struct ext2fs_numeric_progress_struct progress;
305
306	ext2fs_numeric_progress_init(fs, &progress,
307				     _("Writing inode tables: "),
308				     fs->group_desc_count);
309
310	for (i = 0; i < fs->group_desc_count; i++) {
311		ext2fs_numeric_progress_update(fs, &progress, i);
312
313		blk = fs->group_desc[i].bg_inode_table;
314		num = fs->inode_blocks_per_group;
315
316		if (lazy_flag) {
317			ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
318			num = ((((fs->super->s_inodes_per_group -
319				  fs->group_desc[i].bg_itable_unused) *
320				 EXT2_INODE_SIZE(fs->super)) +
321				EXT2_BLOCK_SIZE(fs->super) - 1) /
322			       EXT2_BLOCK_SIZE(fs->super));
323		} else {
324			/* The kernel doesn't need to zero the itable blocks */
325			ext2fs_bg_flag_set(fs, i, EXT2_BG_INODE_ZEROED);
326			ext2fs_group_desc_csum_set(fs, i);
327		}
328		retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
329		if (retval) {
330			fprintf(stderr, _("\nCould not write %d "
331				  "blocks in inode table starting at %u: %s\n"),
332				num, blk, error_message(retval));
333			exit(1);
334		}
335		if (sync_kludge) {
336			if (sync_kludge == 1)
337				sync();
338			else if ((i % sync_kludge) == 0)
339				sync();
340		}
341	}
342	ext2fs_zero_blocks(0, 0, 0, 0, 0);
343	ext2fs_numeric_progress_close(fs, &progress,
344				      _("done                            \n"));
345}
346
347static void create_root_dir(ext2_filsys fs)
348{
349	errcode_t		retval;
350	struct ext2_inode	inode;
351	__u32			uid, gid;
352
353	retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
354	if (retval) {
355		com_err("ext2fs_mkdir", retval, _("while creating root dir"));
356		exit(1);
357	}
358	if (geteuid()) {
359		retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
360		if (retval) {
361			com_err("ext2fs_read_inode", retval,
362				_("while reading root inode"));
363			exit(1);
364		}
365		uid = getuid();
366		inode.i_uid = uid;
367		ext2fs_set_i_uid_high(inode, uid >> 16);
368		if (uid) {
369			gid = getgid();
370			inode.i_gid = gid;
371			ext2fs_set_i_gid_high(inode, gid >> 16);
372		}
373		retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
374		if (retval) {
375			com_err("ext2fs_write_inode", retval,
376				_("while setting root inode ownership"));
377			exit(1);
378		}
379	}
380}
381
382static void create_lost_and_found(ext2_filsys fs)
383{
384	unsigned int		lpf_size = 0;
385	errcode_t		retval;
386	ext2_ino_t		ino;
387	const char		*name = "lost+found";
388	int			i;
389
390	fs->umask = 077;
391	retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
392	if (retval) {
393		com_err("ext2fs_mkdir", retval,
394			_("while creating /lost+found"));
395		exit(1);
396	}
397
398	retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
399	if (retval) {
400		com_err("ext2_lookup", retval,
401			_("while looking up /lost+found"));
402		exit(1);
403	}
404
405	for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
406		/* Ensure that lost+found is at least 2 blocks, so we always
407		 * test large empty blocks for big-block filesystems.  */
408		if ((lpf_size += fs->blocksize) >= 16*1024 &&
409		    lpf_size >= 2 * fs->blocksize)
410			break;
411		retval = ext2fs_expand_dir(fs, ino);
412		if (retval) {
413			com_err("ext2fs_expand_dir", retval,
414				_("while expanding /lost+found"));
415			exit(1);
416		}
417	}
418}
419
420static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
421{
422	errcode_t	retval;
423
424	ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
425	ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
426	retval = ext2fs_update_bb_inode(fs, bb_list);
427	if (retval) {
428		com_err("ext2fs_update_bb_inode", retval,
429			_("while setting bad block inode"));
430		exit(1);
431	}
432
433}
434
435static void reserve_inodes(ext2_filsys fs)
436{
437	ext2_ino_t	i;
438
439	for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
440		ext2fs_inode_alloc_stats2(fs, i, +1, 0);
441	ext2fs_mark_ib_dirty(fs);
442}
443
444#define BSD_DISKMAGIC   (0x82564557UL)  /* The disk magic number */
445#define BSD_MAGICDISK   (0x57455682UL)  /* The disk magic number reversed */
446#define BSD_LABEL_OFFSET        64
447
448static void zap_sector(ext2_filsys fs, int sect, int nsect)
449{
450	char *buf;
451	int retval;
452	unsigned int *magic;
453
454	buf = malloc(512*nsect);
455	if (!buf) {
456		printf(_("Out of memory erasing sectors %d-%d\n"),
457		       sect, sect + nsect - 1);
458		exit(1);
459	}
460
461	if (sect == 0) {
462		/* Check for a BSD disklabel, and don't erase it if so */
463		retval = io_channel_read_blk64(fs->io, 0, -512, buf);
464		if (retval)
465			fprintf(stderr,
466				_("Warning: could not read block 0: %s\n"),
467				error_message(retval));
468		else {
469			magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
470			if ((*magic == BSD_DISKMAGIC) ||
471			    (*magic == BSD_MAGICDISK))
472				return;
473		}
474	}
475
476	memset(buf, 0, 512*nsect);
477	io_channel_set_blksize(fs->io, 512);
478	retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
479	io_channel_set_blksize(fs->io, fs->blocksize);
480	free(buf);
481	if (retval)
482		fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
483			sect, error_message(retval));
484}
485
486static void create_journal_dev(ext2_filsys fs)
487{
488	struct ext2fs_numeric_progress_struct progress;
489	errcode_t		retval;
490	char			*buf;
491	blk_t			blk, err_blk;
492	int			c, count, err_count;
493
494	retval = ext2fs_create_journal_superblock(fs,
495				  ext2fs_blocks_count(fs->super), 0, &buf);
496	if (retval) {
497		com_err("create_journal_dev", retval,
498			_("while initializing journal superblock"));
499		exit(1);
500	}
501	ext2fs_numeric_progress_init(fs, &progress,
502				     _("Zeroing journal device: "),
503				     ext2fs_blocks_count(fs->super));
504	blk = 0;
505	count = ext2fs_blocks_count(fs->super);
506	while (count > 0) {
507		if (count > 1024)
508			c = 1024;
509		else
510			c = count;
511		retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
512		if (retval) {
513			com_err("create_journal_dev", retval,
514				_("while zeroing journal device "
515				  "(block %u, count %d)"),
516				err_blk, err_count);
517			exit(1);
518		}
519		blk += c;
520		count -= c;
521		ext2fs_numeric_progress_update(fs, &progress, blk);
522	}
523	ext2fs_zero_blocks(0, 0, 0, 0, 0);
524
525	retval = io_channel_write_blk64(fs->io,
526					fs->super->s_first_data_block+1,
527					1, buf);
528	if (retval) {
529		com_err("create_journal_dev", retval,
530			_("while writing journal superblock"));
531		exit(1);
532	}
533	ext2fs_numeric_progress_close(fs, &progress, NULL);
534}
535
536static void show_stats(ext2_filsys fs)
537{
538	struct ext2_super_block *s = fs->super;
539	char 			buf[80];
540        char                    *os;
541	blk_t			group_block;
542	dgrp_t			i;
543	int			need, col_left;
544
545	if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
546		fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
547		       ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
548
549	memset(buf, 0, sizeof(buf));
550	strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
551	printf(_("Filesystem label=%s\n"), buf);
552	fputs(_("OS type: "), stdout);
553        os = e2p_os2string(fs->super->s_creator_os);
554	fputs(os, stdout);
555	free(os);
556	printf("\n");
557	printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
558		s->s_log_block_size);
559	printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
560		s->s_log_frag_size);
561	printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
562	       ext2fs_blocks_count(s));
563	printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
564		ext2fs_r_blocks_count(s),
565	       100.0 *  ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
566	printf(_("First data block=%u\n"), s->s_first_data_block);
567	if (s->s_reserved_gdt_blocks)
568		printf(_("Maximum filesystem blocks=%lu\n"),
569		       (s->s_reserved_gdt_blocks + fs->desc_blocks) *
570		       EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
571	if (fs->group_desc_count > 1)
572		printf(_("%u block groups\n"), fs->group_desc_count);
573	else
574		printf(_("%u block group\n"), fs->group_desc_count);
575	printf(_("%u blocks per group, %u fragments per group\n"),
576	       s->s_blocks_per_group, s->s_frags_per_group);
577	printf(_("%u inodes per group\n"), s->s_inodes_per_group);
578
579	if (fs->group_desc_count == 1) {
580		printf("\n");
581		return;
582	}
583
584	printf(_("Superblock backups stored on blocks: "));
585	group_block = s->s_first_data_block;
586	col_left = 0;
587	for (i = 1; i < fs->group_desc_count; i++) {
588		group_block += s->s_blocks_per_group;
589		if (!ext2fs_bg_has_super(fs, i))
590			continue;
591		if (i != 1)
592			printf(", ");
593		need = int_log10(group_block) + 2;
594		if (need > col_left) {
595			printf("\n\t");
596			col_left = 72;
597		}
598		col_left -= need;
599		printf("%u", group_block);
600	}
601	printf("\n\n");
602}
603
604/*
605 * Set the S_CREATOR_OS field.  Return true if OS is known,
606 * otherwise, 0.
607 */
608static int set_os(struct ext2_super_block *sb, char *os)
609{
610	if (isdigit (*os))
611		sb->s_creator_os = atoi (os);
612	else if (strcasecmp(os, "linux") == 0)
613		sb->s_creator_os = EXT2_OS_LINUX;
614	else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
615		sb->s_creator_os = EXT2_OS_HURD;
616	else if (strcasecmp(os, "freebsd") == 0)
617		sb->s_creator_os = EXT2_OS_FREEBSD;
618	else if (strcasecmp(os, "lites") == 0)
619		sb->s_creator_os = EXT2_OS_LITES;
620	else
621		return 0;
622	return 1;
623}
624
625#define PATH_SET "PATH=/sbin"
626
627static void parse_extended_opts(struct ext2_super_block *param,
628				const char *opts)
629{
630	char	*buf, *token, *next, *p, *arg, *badopt = 0;
631	int	len;
632	int	r_usage = 0;
633
634	len = strlen(opts);
635	buf = malloc(len+1);
636	if (!buf) {
637		fprintf(stderr,
638			_("Couldn't allocate memory to parse options!\n"));
639		exit(1);
640	}
641	strcpy(buf, opts);
642	for (token = buf; token && *token; token = next) {
643		p = strchr(token, ',');
644		next = 0;
645		if (p) {
646			*p = 0;
647			next = p+1;
648		}
649		arg = strchr(token, '=');
650		if (arg) {
651			*arg = 0;
652			arg++;
653		}
654		if (strcmp(token, "stride") == 0) {
655			if (!arg) {
656				r_usage++;
657				badopt = token;
658				continue;
659			}
660			param->s_raid_stride = strtoul(arg, &p, 0);
661			if (*p || (param->s_raid_stride == 0)) {
662				fprintf(stderr,
663					_("Invalid stride parameter: %s\n"),
664					arg);
665				r_usage++;
666				continue;
667			}
668		} else if (strcmp(token, "stripe-width") == 0 ||
669			   strcmp(token, "stripe_width") == 0) {
670			if (!arg) {
671				r_usage++;
672				badopt = token;
673				continue;
674			}
675			param->s_raid_stripe_width = strtoul(arg, &p, 0);
676			if (*p || (param->s_raid_stripe_width == 0)) {
677				fprintf(stderr,
678					_("Invalid stripe-width parameter: %s\n"),
679					arg);
680				r_usage++;
681				continue;
682			}
683		} else if (!strcmp(token, "resize")) {
684			unsigned long resize, bpg, rsv_groups;
685			unsigned long group_desc_count, desc_blocks;
686			unsigned int gdpb, blocksize;
687			int rsv_gdb;
688
689			if (!arg) {
690				r_usage++;
691				badopt = token;
692				continue;
693			}
694
695			resize = parse_num_blocks(arg,
696						  param->s_log_block_size);
697
698			if (resize == 0) {
699				fprintf(stderr,
700					_("Invalid resize parameter: %s\n"),
701					arg);
702				r_usage++;
703				continue;
704			}
705			if (resize <= ext2fs_blocks_count(param)) {
706				fprintf(stderr,
707					_("The resize maximum must be greater "
708					  "than the filesystem size.\n"));
709				r_usage++;
710				continue;
711			}
712
713			blocksize = EXT2_BLOCK_SIZE(param);
714			bpg = param->s_blocks_per_group;
715			if (!bpg)
716				bpg = blocksize * 8;
717			gdpb = EXT2_DESC_PER_BLOCK(param);
718			group_desc_count = (__u32) ext2fs_div64_ceil(
719				ext2fs_blocks_count(param), bpg);
720			desc_blocks = (group_desc_count +
721				       gdpb - 1) / gdpb;
722			rsv_groups = ext2fs_div_ceil(resize, bpg);
723			rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
724				desc_blocks;
725			if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
726				rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
727
728			if (rsv_gdb > 0) {
729				if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
730					fprintf(stderr,
731	_("On-line resizing not supported with revision 0 filesystems\n"));
732					free(buf);
733					exit(1);
734				}
735				param->s_feature_compat |=
736					EXT2_FEATURE_COMPAT_RESIZE_INODE;
737
738				param->s_reserved_gdt_blocks = rsv_gdb;
739			}
740		} else if (!strcmp(token, "test_fs")) {
741			param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
742		} else if (!strcmp(token, "lazy_itable_init")) {
743			if (arg)
744				lazy_itable_init = strtoul(arg, &p, 0);
745			else
746				lazy_itable_init = 1;
747		} else {
748			r_usage++;
749			badopt = token;
750		}
751	}
752	if (r_usage) {
753		fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
754			"Extended options are separated by commas, "
755			"and may take an argument which\n"
756			"\tis set off by an equals ('=') sign.\n\n"
757			"Valid extended options are:\n"
758			"\tstride=<RAID per-disk data chunk in blocks>\n"
759			"\tstripe-width=<RAID stride * data disks in blocks>\n"
760			"\tresize=<resize maximum size in blocks>\n"
761			"\tlazy_itable_init=<0 to disable, 1 to enable>\n"
762			"\ttest_fs\n\n"),
763			badopt ? badopt : "");
764		free(buf);
765		exit(1);
766	}
767	if (param->s_raid_stride &&
768	    (param->s_raid_stripe_width % param->s_raid_stride) != 0)
769		fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
770				  "multiple of stride %u.\n\n"),
771			param->s_raid_stripe_width, param->s_raid_stride);
772
773	free(buf);
774}
775
776static __u32 ok_features[3] = {
777	/* Compat */
778	EXT3_FEATURE_COMPAT_HAS_JOURNAL |
779		EXT2_FEATURE_COMPAT_RESIZE_INODE |
780		EXT2_FEATURE_COMPAT_DIR_INDEX |
781		EXT2_FEATURE_COMPAT_EXT_ATTR,
782	/* Incompat */
783	EXT2_FEATURE_INCOMPAT_FILETYPE|
784		EXT3_FEATURE_INCOMPAT_EXTENTS|
785		EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
786		EXT2_FEATURE_INCOMPAT_META_BG|
787		EXT4_FEATURE_INCOMPAT_FLEX_BG,
788	/* R/O compat */
789	EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
790		EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
791		EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
792		EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
793		EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
794		EXT4_FEATURE_RO_COMPAT_GDT_CSUM
795};
796
797
798static void syntax_err_report(const char *filename, long err, int line_num)
799{
800	fprintf(stderr,
801		_("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
802		filename, line_num, error_message(err));
803	exit(1);
804}
805
806static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
807
808static void edit_feature(const char *str, __u32 *compat_array)
809{
810	if (!str)
811		return;
812
813	if (e2p_edit_feature(str, compat_array, ok_features)) {
814		fprintf(stderr, _("Invalid filesystem option set: %s\n"),
815			str);
816		exit(1);
817	}
818}
819
820struct str_list {
821	char **list;
822	int num;
823	int max;
824};
825
826static errcode_t init_list(struct str_list *sl)
827{
828	sl->num = 0;
829	sl->max = 0;
830	sl->list = malloc((sl->max+1) * sizeof(char *));
831	if (!sl->list)
832		return ENOMEM;
833	sl->list[0] = 0;
834	return 0;
835}
836
837static errcode_t push_string(struct str_list *sl, const char *str)
838{
839	char **new_list;
840
841	if (sl->num >= sl->max) {
842		sl->max += 2;
843		new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
844		if (!new_list)
845			return ENOMEM;
846		sl->list = new_list;
847	}
848	sl->list[sl->num] = malloc(strlen(str)+1);
849	if (sl->list[sl->num] == 0)
850		return ENOMEM;
851	strcpy(sl->list[sl->num], str);
852	sl->num++;
853	sl->list[sl->num] = 0;
854	return 0;
855}
856
857static void print_str_list(char **list)
858{
859	char **cpp;
860
861	for (cpp = list; *cpp; cpp++) {
862		printf("'%s'", *cpp);
863		if (cpp[1])
864			fputs(", ", stdout);
865	}
866	fputc('\n', stdout);
867}
868
869static char **parse_fs_type(const char *fs_type,
870			    const char *usage_types,
871			    struct ext2_super_block *fs_param,
872			    char *progname)
873{
874	const char	*ext_type = 0;
875	char		*parse_str;
876	char		*profile_type = 0;
877	char		*cp, *t;
878	const char	*size_type;
879	struct str_list	list;
880	unsigned long	meg;
881	int		is_hurd = 0;
882
883	if (init_list(&list))
884		return 0;
885
886	if (creator_os && (!strcasecmp(creator_os, "GNU") ||
887			   !strcasecmp(creator_os, "hurd")))
888		is_hurd = 1;
889
890	if (fs_type)
891		ext_type = fs_type;
892	else if (is_hurd)
893		ext_type = "ext2";
894	else if (!strcmp(program_name, "mke3fs"))
895		ext_type = "ext3";
896	else if (progname) {
897		ext_type = strrchr(progname, '/');
898		if (ext_type)
899			ext_type++;
900		else
901			ext_type = progname;
902
903		if (!strncmp(ext_type, "mkfs.", 5)) {
904			ext_type += 5;
905			if (ext_type[0] == 0)
906				ext_type = 0;
907		} else
908			ext_type = 0;
909	}
910
911	if (!ext_type) {
912		profile_get_string(profile, "defaults", "fs_type", 0,
913				   "ext2", &profile_type);
914		ext_type = profile_type;
915		if (!strcmp(ext_type, "ext2") && (journal_size != 0))
916			ext_type = "ext3";
917	}
918
919	if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
920	    !strcmp(ext_type, "ext4dev")) {
921		profile_get_string(profile, "fs_types", ext_type, "features",
922				   0, &t);
923		if (!t) {
924			printf(_("\nWarning!  Your mke2fs.conf file does "
925				 "not define the %s filesystem type.\n"),
926			       ext_type);
927			printf(_("You probably need to install an updated "
928				 "mke2fs.conf file.\n\n"));
929			sleep(5);
930		}
931	}
932
933	meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
934	if (ext2fs_blocks_count(fs_param) < 3 * meg)
935		size_type = "floppy";
936	else if (ext2fs_blocks_count(fs_param) < 512 * meg)
937		size_type = "small";
938	else
939		size_type = "default";
940
941	if (!usage_types)
942		usage_types = size_type;
943
944	parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
945	if (!parse_str) {
946		free(list.list);
947		return 0;
948	}
949	if (usage_types)
950		strcpy(parse_str, usage_types);
951	else
952		*parse_str = '\0';
953
954	if (ext_type)
955		push_string(&list, ext_type);
956	cp = parse_str;
957	while (1) {
958		t = strchr(cp, ',');
959		if (t)
960			*t = '\0';
961
962		if (*cp)
963			push_string(&list, cp);
964		if (t)
965			cp = t+1;
966		else {
967			cp = "";
968			break;
969		}
970	}
971	free(parse_str);
972	free(profile_type);
973	if (is_hurd)
974		push_string(&list, "hurd");
975	return (list.list);
976}
977
978static char *get_string_from_profile(char **fs_types, const char *opt,
979				     const char *def_val)
980{
981	char *ret = 0;
982	int i;
983
984	for (i=0; fs_types[i]; i++);
985	for (i-=1; i >=0 ; i--) {
986		profile_get_string(profile, "fs_types", fs_types[i],
987				   opt, 0, &ret);
988		if (ret)
989			return ret;
990	}
991	profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
992	return (ret);
993}
994
995static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
996{
997	int ret;
998	char **cpp;
999
1000	profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1001	for (cpp = fs_types; *cpp; cpp++)
1002		profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1003	return ret;
1004}
1005
1006static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1007{
1008	int ret;
1009	char **cpp;
1010
1011	profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1012	for (cpp = fs_types; *cpp; cpp++)
1013		profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1014	return ret;
1015}
1016
1017extern const char *mke2fs_default_profile;
1018static const char *default_files[] = { "<default>", 0 };
1019
1020static void PRS(int argc, char *argv[])
1021{
1022	int		b, c;
1023	int		size;
1024	char 		*tmp, **cpp;
1025	int		blocksize = 0;
1026	int		inode_ratio = 0;
1027	int		inode_size = 0;
1028	unsigned long	flex_bg_size = 0;
1029	double		reserved_ratio = 5.0;
1030	int		sector_size = 0;
1031	int		show_version_only = 0;
1032	unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1033	errcode_t	retval;
1034	char *		oldpath = getenv("PATH");
1035	char *		extended_opts = 0;
1036	const char *	fs_type = 0;
1037	const char *	usage_types = 0;
1038	blk_t		dev_size;
1039#ifdef __linux__
1040	struct 		utsname ut;
1041#endif
1042	long		sysval;
1043	int		s_opt = -1, r_opt = -1;
1044	char		*fs_features = 0;
1045	int		use_bsize;
1046	char		*newpath;
1047	int		pathlen = sizeof(PATH_SET) + 1;
1048
1049	if (oldpath)
1050		pathlen += strlen(oldpath);
1051	newpath = malloc(pathlen);
1052	strcpy(newpath, PATH_SET);
1053
1054	/* Update our PATH to include /sbin  */
1055	if (oldpath) {
1056		strcat (newpath, ":");
1057		strcat (newpath, oldpath);
1058	}
1059	putenv (newpath);
1060
1061	tmp = getenv("MKE2FS_SYNC");
1062	if (tmp)
1063		sync_kludge = atoi(tmp);
1064
1065	/* Determine the system page size if possible */
1066#ifdef HAVE_SYSCONF
1067#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1068#define _SC_PAGESIZE _SC_PAGE_SIZE
1069#endif
1070#ifdef _SC_PAGESIZE
1071	sysval = sysconf(_SC_PAGESIZE);
1072	if (sysval > 0)
1073		sys_page_size = sysval;
1074#endif /* _SC_PAGESIZE */
1075#endif /* HAVE_SYSCONF */
1076
1077	if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1078		config_fn[0] = tmp;
1079	profile_set_syntax_err_cb(syntax_err_report);
1080	retval = profile_init(config_fn, &profile);
1081	if (retval == ENOENT) {
1082		profile_init(default_files, &profile);
1083		profile_set_default(profile, mke2fs_default_profile);
1084	}
1085
1086	setbuf(stdout, NULL);
1087	setbuf(stderr, NULL);
1088	add_error_table(&et_ext2_error_table);
1089	add_error_table(&et_prof_error_table);
1090	memset(&fs_param, 0, sizeof(struct ext2_super_block));
1091	fs_param.s_rev_level = 1;  /* Create revision 1 filesystems now */
1092
1093#ifdef __linux__
1094	if (uname(&ut)) {
1095		perror("uname");
1096		exit(1);
1097	}
1098	linux_version_code = parse_version_number(ut.release);
1099	if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1100		fs_param.s_rev_level = 0;
1101#endif
1102
1103	if (argc && *argv) {
1104		program_name = get_progname(*argv);
1105
1106		/* If called as mkfs.ext3, create a journal inode */
1107		if (!strcmp(program_name, "mkfs.ext3") ||
1108		    !strcmp(program_name, "mke3fs"))
1109			journal_size = -1;
1110	}
1111
1112	while ((c = getopt (argc, argv,
1113		    "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:U:V")) != EOF) {
1114		switch (c) {
1115		case 'b':
1116			blocksize = strtol(optarg, &tmp, 0);
1117			b = (blocksize > 0) ? blocksize : -blocksize;
1118			if (b < EXT2_MIN_BLOCK_SIZE ||
1119			    b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1120				com_err(program_name, 0,
1121					_("invalid block size - %s"), optarg);
1122				exit(1);
1123			}
1124			if (blocksize > 4096)
1125				fprintf(stderr, _("Warning: blocksize %d not "
1126						  "usable on most systems.\n"),
1127					blocksize);
1128			if (blocksize > 0)
1129				fs_param.s_log_block_size =
1130					int_log2(blocksize >>
1131						 EXT2_MIN_BLOCK_LOG_SIZE);
1132			break;
1133		case 'c':	/* Check for bad blocks */
1134			cflag++;
1135			break;
1136		case 'f':
1137			size = strtoul(optarg, &tmp, 0);
1138			if (size < EXT2_MIN_BLOCK_SIZE ||
1139			    size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1140				com_err(program_name, 0,
1141					_("invalid fragment size - %s"),
1142					optarg);
1143				exit(1);
1144			}
1145			fs_param.s_log_frag_size =
1146				int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1147			fprintf(stderr, _("Warning: fragments not supported.  "
1148			       "Ignoring -f option\n"));
1149			break;
1150		case 'g':
1151			fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1152			if (*tmp) {
1153				com_err(program_name, 0,
1154					_("Illegal number for blocks per group"));
1155				exit(1);
1156			}
1157			if ((fs_param.s_blocks_per_group % 8) != 0) {
1158				com_err(program_name, 0,
1159				_("blocks per group must be multiple of 8"));
1160				exit(1);
1161			}
1162			break;
1163		case 'G':
1164			flex_bg_size = strtoul(optarg, &tmp, 0);
1165			if (*tmp) {
1166				com_err(program_name, 0,
1167					_("Illegal number for flex_bg size"));
1168				exit(1);
1169			}
1170			if (flex_bg_size < 2 ||
1171			    (flex_bg_size & (flex_bg_size-1)) != 0) {
1172				com_err(program_name, 0,
1173					_("flex_bg size must be a power of 2"));
1174				exit(1);
1175			}
1176			break;
1177		case 'i':
1178			inode_ratio = strtoul(optarg, &tmp, 0);
1179			if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1180			    inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1181			    *tmp) {
1182				com_err(program_name, 0,
1183					_("invalid inode ratio %s (min %d/max %d)"),
1184					optarg, EXT2_MIN_BLOCK_SIZE,
1185					EXT2_MAX_BLOCK_SIZE);
1186				exit(1);
1187			}
1188			break;
1189		case 'J':
1190			parse_journal_opts(optarg);
1191			break;
1192		case 'j':
1193			if (!journal_size)
1194				journal_size = -1;
1195			break;
1196		case 'l':
1197			bad_blocks_filename = malloc(strlen(optarg)+1);
1198			if (!bad_blocks_filename) {
1199				com_err(program_name, ENOMEM,
1200					_("in malloc for bad_blocks_filename"));
1201				exit(1);
1202			}
1203			strcpy(bad_blocks_filename, optarg);
1204			break;
1205		case 'm':
1206			reserved_ratio = strtod(optarg, &tmp);
1207			if ( *tmp || reserved_ratio > 50 ||
1208			     reserved_ratio < 0) {
1209				com_err(program_name, 0,
1210					_("invalid reserved blocks percent - %s"),
1211					optarg);
1212				exit(1);
1213			}
1214			break;
1215		case 'n':
1216			noaction++;
1217			break;
1218		case 'o':
1219			creator_os = optarg;
1220			break;
1221		case 'q':
1222			quiet = 1;
1223			break;
1224		case 'r':
1225			r_opt = strtoul(optarg, &tmp, 0);
1226			if (*tmp) {
1227				com_err(program_name, 0,
1228					_("bad revision level - %s"), optarg);
1229				exit(1);
1230			}
1231			fs_param.s_rev_level = r_opt;
1232			break;
1233		case 's':	/* deprecated */
1234			s_opt = atoi(optarg);
1235			break;
1236		case 'I':
1237			inode_size = strtoul(optarg, &tmp, 0);
1238			if (*tmp) {
1239				com_err(program_name, 0,
1240					_("invalid inode size - %s"), optarg);
1241				exit(1);
1242			}
1243			break;
1244		case 'v':
1245			verbose = 1;
1246			break;
1247		case 'F':
1248			force++;
1249			break;
1250		case 'L':
1251			volume_label = optarg;
1252			break;
1253		case 'M':
1254			mount_dir = optarg;
1255			break;
1256		case 'N':
1257			num_inodes = strtoul(optarg, &tmp, 0);
1258			if (*tmp) {
1259				com_err(program_name, 0,
1260					_("bad num inodes - %s"), optarg);
1261					exit(1);
1262			}
1263			break;
1264		case 'O':
1265			fs_features = optarg;
1266			break;
1267		case 'E':
1268		case 'R':
1269			extended_opts = optarg;
1270			break;
1271		case 'S':
1272			super_only = 1;
1273			break;
1274		case 't':
1275			fs_type = optarg;
1276			break;
1277		case 'T':
1278			usage_types = optarg;
1279			break;
1280		case 'U':
1281			fs_uuid = optarg;
1282			break;
1283		case 'V':
1284			/* Print version number and exit */
1285			show_version_only++;
1286			break;
1287		default:
1288			usage();
1289		}
1290	}
1291	if ((optind == argc) && !show_version_only)
1292		usage();
1293	device_name = argv[optind++];
1294
1295	if (!quiet || show_version_only)
1296		fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1297			 E2FSPROGS_DATE);
1298
1299	if (show_version_only) {
1300		fprintf(stderr, _("\tUsing %s\n"),
1301			error_message(EXT2_ET_BASE));
1302		exit(0);
1303	}
1304
1305	/*
1306	 * If there's no blocksize specified and there is a journal
1307	 * device, use it to figure out the blocksize
1308	 */
1309	if (blocksize <= 0 && journal_device) {
1310		ext2_filsys	jfs;
1311		io_manager	io_ptr;
1312
1313#ifdef CONFIG_TESTIO_DEBUG
1314		if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1315			io_ptr = test_io_manager;
1316			test_io_backing_manager = unix_io_manager;
1317		} else
1318#endif
1319			io_ptr = unix_io_manager;
1320		retval = ext2fs_open(journal_device,
1321				     EXT2_FLAG_JOURNAL_DEV_OK, 0,
1322				     0, io_ptr, &jfs);
1323		if (retval) {
1324			com_err(program_name, retval,
1325				_("while trying to open journal device %s\n"),
1326				journal_device);
1327			exit(1);
1328		}
1329		if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1330			com_err(program_name, 0,
1331				_("Journal dev blocksize (%d) smaller than "
1332				  "minimum blocksize %d\n"), jfs->blocksize,
1333				-blocksize);
1334			exit(1);
1335		}
1336		blocksize = jfs->blocksize;
1337		printf(_("Using journal device's blocksize: %d\n"), blocksize);
1338		fs_param.s_log_block_size =
1339			int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1340		ext2fs_close(jfs);
1341	}
1342
1343	if (blocksize > sys_page_size) {
1344		if (!force) {
1345			com_err(program_name, 0,
1346				_("%d-byte blocks too big for system (max %d)"),
1347				blocksize, sys_page_size);
1348			proceed_question();
1349		}
1350		fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1351				  "(max %d), forced to continue\n"),
1352			blocksize, sys_page_size);
1353	}
1354	if (optind < argc) {
1355		ext2fs_blocks_count_set(&fs_param, parse_num_blocks(argv[optind++],
1356				fs_param.s_log_block_size));
1357		if (!ext2fs_blocks_count(&fs_param)) {
1358			com_err(program_name, 0, _("invalid blocks count - %s"),
1359				argv[optind - 1]);
1360			exit(1);
1361		}
1362	}
1363	if (optind < argc)
1364		usage();
1365
1366	if (!force)
1367		check_plausibility(device_name);
1368	check_mount(device_name, force, _("filesystem"));
1369
1370	fs_param.s_log_frag_size = fs_param.s_log_block_size;
1371
1372	if (noaction && ext2fs_blocks_count(&fs_param)) {
1373		dev_size = ext2fs_blocks_count(&fs_param);
1374		retval = 0;
1375	} else {
1376	retry:
1377		retval = ext2fs_get_device_size(device_name,
1378						EXT2_BLOCK_SIZE(&fs_param),
1379						&dev_size);
1380		if ((retval == EFBIG) &&
1381		    (blocksize == 0) &&
1382		    (fs_param.s_log_block_size == 0)) {
1383			fs_param.s_log_block_size = 2;
1384			blocksize = 4096;
1385			goto retry;
1386		}
1387	}
1388
1389	if (retval == EFBIG) {
1390		blk64_t	big_dev_size;
1391
1392		if (blocksize < 4096) {
1393			fs_param.s_log_block_size = 2;
1394			blocksize = 4096;
1395		}
1396		retval = ext2fs_get_device_size2(device_name,
1397				 EXT2_BLOCK_SIZE(&fs_param), &big_dev_size);
1398		if (retval)
1399			goto get_size_failure;
1400		if (big_dev_size == (1ULL << 32)) {
1401			dev_size = (blk_t) (big_dev_size - 1);
1402			goto got_size;
1403		}
1404		fprintf(stderr, _("%s: Size of device %s too big "
1405				  "to be expressed in 32 bits\n\t"
1406				  "using a blocksize of %d.\n"),
1407			program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1408		exit(1);
1409	}
1410get_size_failure:
1411	if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1412		com_err(program_name, retval,
1413			_("while trying to determine filesystem size"));
1414		exit(1);
1415	}
1416got_size:
1417	if (!ext2fs_blocks_count(&fs_param)) {
1418		if (retval == EXT2_ET_UNIMPLEMENTED) {
1419			com_err(program_name, 0,
1420				_("Couldn't determine device size; you "
1421				"must specify\nthe size of the "
1422				"filesystem\n"));
1423			exit(1);
1424		} else {
1425			if (dev_size == 0) {
1426				com_err(program_name, 0,
1427				_("Device size reported to be zero.  "
1428				  "Invalid partition specified, or\n\t"
1429				  "partition table wasn't reread "
1430				  "after running fdisk, due to\n\t"
1431				  "a modified partition being busy "
1432				  "and in use.  You may need to reboot\n\t"
1433				  "to re-read your partition table.\n"
1434				  ));
1435				exit(1);
1436			}
1437			ext2fs_blocks_count_set(&fs_param, dev_size);
1438			if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param)) {
1439				blk64_t tmp = ext2fs_blocks_count(&fs_param);
1440
1441				tmp &= ~((blk64_t) ((sys_page_size /
1442					     EXT2_BLOCK_SIZE(&fs_param))-1));
1443				ext2fs_blocks_count_set(&fs_param, tmp);
1444			}
1445		}
1446	} else if (!force && (ext2fs_blocks_count(&fs_param) > dev_size)) {
1447		com_err(program_name, 0,
1448			_("Filesystem larger than apparent device size."));
1449		proceed_question();
1450	}
1451
1452	fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1453	if (!fs_types) {
1454		fprintf(stderr, _("Failed to parse fs types list\n"));
1455		exit(1);
1456	}
1457
1458	/* Figure out what features should be enabled */
1459
1460	tmp = NULL;
1461	if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1462		tmp = get_string_from_profile(fs_types, "base_features",
1463		      "sparse_super,filetype,resize_inode,dir_index");
1464		edit_feature(tmp, &fs_param.s_feature_compat);
1465		free(tmp);
1466
1467		for (cpp = fs_types; *cpp; cpp++) {
1468			tmp = NULL;
1469			profile_get_string(profile, "fs_types", *cpp,
1470					   "features", "", &tmp);
1471			if (tmp && *tmp)
1472				edit_feature(tmp, &fs_param.s_feature_compat);
1473			free(tmp);
1474		}
1475		tmp = get_string_from_profile(fs_types, "default_features",
1476					      "");
1477	}
1478	edit_feature(fs_features ? fs_features : tmp,
1479		     &fs_param.s_feature_compat);
1480	free(tmp);
1481
1482	if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1483		fs_types[0] = strdup("journal");
1484		fs_types[1] = 0;
1485	}
1486
1487	if (verbose) {
1488		fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1489		print_str_list(fs_types);
1490	}
1491
1492	if (r_opt == EXT2_GOOD_OLD_REV &&
1493	    (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1494	     fs_param.s_feature_ro_compat)) {
1495		fprintf(stderr, _("Filesystem features not supported "
1496				  "with revision 0 filesystems\n"));
1497		exit(1);
1498	}
1499
1500	if (s_opt > 0) {
1501		if (r_opt == EXT2_GOOD_OLD_REV) {
1502			fprintf(stderr, _("Sparse superblocks not supported "
1503				  "with revision 0 filesystems\n"));
1504			exit(1);
1505		}
1506		fs_param.s_feature_ro_compat |=
1507			EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1508	} else if (s_opt == 0)
1509		fs_param.s_feature_ro_compat &=
1510			~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1511
1512	if (journal_size != 0) {
1513		if (r_opt == EXT2_GOOD_OLD_REV) {
1514			fprintf(stderr, _("Journals not supported "
1515				  "with revision 0 filesystems\n"));
1516			exit(1);
1517		}
1518		fs_param.s_feature_compat |=
1519			EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1520	}
1521
1522	if (fs_param.s_feature_incompat &
1523	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1524		reserved_ratio = 0;
1525		fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1526		fs_param.s_feature_compat = 0;
1527		fs_param.s_feature_ro_compat = 0;
1528 	}
1529
1530	if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1531	    (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1532		fprintf(stderr, _("The resize_inode and meta_bg features "
1533				  "are not compatible.\n"
1534				  "They can not be both enabled "
1535				  "simultaneously.\n"));
1536		exit(1);
1537	}
1538
1539	/* Set first meta blockgroup via an environment variable */
1540	/* (this is mostly for debugging purposes) */
1541	if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1542	    ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1543		fs_param.s_first_meta_bg = atoi(tmp);
1544
1545	/* Get the hardware sector size, if available */
1546	retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1547	if (retval) {
1548		com_err(program_name, retval,
1549			_("while trying to determine hardware sector size"));
1550		exit(1);
1551	}
1552
1553	if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1554		sector_size = atoi(tmp);
1555
1556	if (blocksize <= 0) {
1557		use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1558
1559		if (use_bsize == -1) {
1560			use_bsize = sys_page_size;
1561			if ((linux_version_code < (2*65536 + 6*256)) &&
1562			    (use_bsize > 4096))
1563				use_bsize = 4096;
1564		}
1565		if (sector_size && use_bsize < sector_size)
1566			use_bsize = sector_size;
1567		if ((blocksize < 0) && (use_bsize < (-blocksize)))
1568			use_bsize = -blocksize;
1569		blocksize = use_bsize;
1570		ext2fs_blocks_count_set(&fs_param,
1571					ext2fs_blocks_count(&fs_param) /
1572					(blocksize / 1024));
1573	}
1574
1575	if (inode_ratio == 0) {
1576		inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1577						   8192);
1578		if (inode_ratio < blocksize)
1579			inode_ratio = blocksize;
1580	}
1581
1582	fs_param.s_log_frag_size = fs_param.s_log_block_size =
1583		int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1584
1585	blocksize = EXT2_BLOCK_SIZE(&fs_param);
1586
1587	lazy_itable_init = get_bool_from_profile(fs_types,
1588						 "lazy_itable_init", 0);
1589
1590	/* Get options from profile */
1591	for (cpp = fs_types; *cpp; cpp++) {
1592		tmp = NULL;
1593		profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1594			if (tmp && *tmp)
1595				parse_extended_opts(&fs_param, tmp);
1596			free(tmp);
1597	}
1598
1599	if (extended_opts)
1600		parse_extended_opts(&fs_param, extended_opts);
1601
1602	/* Since sparse_super is the default, we would only have a problem
1603	 * here if it was explicitly disabled.
1604	 */
1605	if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1606	    !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1607		com_err(program_name, 0,
1608			_("reserved online resize blocks not supported "
1609			  "on non-sparse filesystem"));
1610		exit(1);
1611	}
1612
1613	if (fs_param.s_blocks_per_group) {
1614		if (fs_param.s_blocks_per_group < 256 ||
1615		    fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1616			com_err(program_name, 0,
1617				_("blocks per group count out of range"));
1618			exit(1);
1619		}
1620	}
1621
1622	if (inode_size == 0)
1623		inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1624	if (!flex_bg_size && (fs_param.s_feature_incompat &
1625			      EXT4_FEATURE_INCOMPAT_FLEX_BG))
1626		flex_bg_size = get_int_from_profile(fs_types,
1627						    "flex_bg_size", 16);
1628	if (flex_bg_size) {
1629		if (!(fs_param.s_feature_incompat &
1630		      EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1631			com_err(program_name, 0,
1632				_("Flex_bg feature not enabled, so "
1633				  "flex_bg size may not be specified"));
1634			exit(1);
1635		}
1636		fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1637	}
1638
1639	if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1640		if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1641		    inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1642		    inode_size & (inode_size - 1)) {
1643			com_err(program_name, 0,
1644				_("invalid inode size %d (min %d/max %d)"),
1645				inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1646				blocksize);
1647			exit(1);
1648		}
1649		fs_param.s_inode_size = inode_size;
1650	}
1651
1652	/* Make sure number of inodes specified will fit in 32 bits */
1653	if (num_inodes == 0) {
1654		unsigned long long n;
1655		n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
1656		if (n > ~0U) {
1657			com_err(program_name, 0,
1658			    _("too many inodes (%llu), raise inode ratio?"), n);
1659			exit(1);
1660		}
1661	} else if (num_inodes > ~0U) {
1662		com_err(program_name, 0,
1663			_("too many inodes (%llu), specify < 2^32 inodes"),
1664			  num_inodes);
1665		exit(1);
1666	}
1667	/*
1668	 * Calculate number of inodes based on the inode ratio
1669	 */
1670	fs_param.s_inodes_count = num_inodes ? num_inodes :
1671		(ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
1672
1673	if ((((long long)fs_param.s_inodes_count) *
1674	     (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1675	    ((ext2fs_blocks_count(&fs_param)) *
1676	     EXT2_BLOCK_SIZE(&fs_param))) {
1677		com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1678					  "(%u) too big for a\n\t"
1679					  "filesystem with %llu blocks, "
1680					  "specify higher inode_ratio (-i)\n\t"
1681					  "or lower inode count (-N).\n"),
1682			inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1683			fs_param.s_inodes_count,
1684			(unsigned long long) ext2fs_blocks_count(&fs_param));
1685		exit(1);
1686	}
1687
1688	/*
1689	 * Calculate number of blocks to reserve
1690	 */
1691	ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
1692				  ext2fs_blocks_count(&fs_param) / 100.0);
1693}
1694
1695static int should_do_undo(const char *name)
1696{
1697	errcode_t retval;
1698	io_channel channel;
1699	__u16	s_magic;
1700	struct ext2_super_block super;
1701	io_manager manager = unix_io_manager;
1702	int csum_flag, force_undo;
1703
1704	csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1705					       EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1706	force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1707	if (!force_undo && (!csum_flag || !lazy_itable_init))
1708		return 0;
1709
1710	retval = manager->open(name, IO_FLAG_EXCLUSIVE,  &channel);
1711	if (retval) {
1712		/*
1713		 * We don't handle error cases instead we
1714		 * declare that the file system doesn't exist
1715		 * and let the rest of mke2fs take care of
1716		 * error
1717		 */
1718		retval = 0;
1719		goto open_err_out;
1720	}
1721
1722	io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1723	retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
1724	if (retval) {
1725		retval = 0;
1726		goto err_out;
1727	}
1728
1729#if defined(WORDS_BIGENDIAN)
1730	s_magic = ext2fs_swab16(super.s_magic);
1731#else
1732	s_magic = super.s_magic;
1733#endif
1734
1735	if (s_magic == EXT2_SUPER_MAGIC)
1736		retval = 1;
1737
1738err_out:
1739	io_channel_close(channel);
1740
1741open_err_out:
1742
1743	return retval;
1744}
1745
1746static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1747{
1748	errcode_t retval = 0;
1749	char *tdb_dir, *tdb_file;
1750	char *device_name, *tmp_name;
1751
1752	/*
1753	 * Configuration via a conf file would be
1754	 * nice
1755	 */
1756	tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1757	if (!tdb_dir)
1758		profile_get_string(profile, "defaults",
1759				   "undo_dir", 0, "/var/lib/e2fsprogs",
1760				   &tdb_dir);
1761
1762	if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1763	    access(tdb_dir, W_OK))
1764		return 0;
1765
1766	tmp_name = strdup(name);
1767	if (!tmp_name) {
1768	alloc_fn_fail:
1769		com_err(program_name, ENOMEM,
1770			_("Couldn't allocate memory for tdb filename\n"));
1771		return ENOMEM;
1772	}
1773	device_name = basename(tmp_name);
1774	tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
1775	if (!tdb_file)
1776		goto alloc_fn_fail;
1777	sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1778
1779	if (!access(tdb_file, F_OK)) {
1780		if (unlink(tdb_file) < 0) {
1781			retval = errno;
1782			com_err(program_name, retval,
1783				_("while trying to delete %s"),
1784				tdb_file);
1785			free(tdb_file);
1786			return retval;
1787		}
1788	}
1789
1790	set_undo_io_backing_manager(*io_ptr);
1791	*io_ptr = undo_io_manager;
1792	set_undo_io_backup_file(tdb_file);
1793	printf(_("Overwriting existing filesystem; this can be undone "
1794		 "using the command:\n"
1795		 "    e2undo %s %s\n\n"), tdb_file, name);
1796
1797	free(tdb_file);
1798	free(tmp_name);
1799	return retval;
1800}
1801
1802int main (int argc, char *argv[])
1803{
1804	errcode_t	retval = 0;
1805	ext2_filsys	fs;
1806	badblocks_list	bb_list = 0;
1807	unsigned int	journal_blocks;
1808	unsigned int	i;
1809	int		val, hash_alg;
1810	int		flags;
1811	int		old_bitmaps;
1812	io_manager	io_ptr;
1813	char		tdb_string[40];
1814	char		*hash_alg_str;
1815
1816#ifdef ENABLE_NLS
1817	setlocale(LC_MESSAGES, "");
1818	setlocale(LC_CTYPE, "");
1819	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1820	textdomain(NLS_CAT_NAME);
1821#endif
1822	PRS(argc, argv);
1823
1824#ifdef CONFIG_TESTIO_DEBUG
1825	if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1826		io_ptr = test_io_manager;
1827		test_io_backing_manager = unix_io_manager;
1828	} else
1829#endif
1830		io_ptr = unix_io_manager;
1831
1832	if (should_do_undo(device_name)) {
1833		retval = mke2fs_setup_tdb(device_name, &io_ptr);
1834		if (retval)
1835			exit(1);
1836	}
1837
1838	/*
1839	 * Initialize the superblock....
1840	 */
1841	flags = EXT2_FLAG_EXCLUSIVE;
1842	profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
1843			    &old_bitmaps);
1844	if (!old_bitmaps)
1845		flags |= EXT2_FLAG_64BITS;
1846	/*
1847	 * By default, we print how many inode tables or block groups
1848	 * or whatever we've written so far.  The quiet flag disables
1849	 * this, along with a lot of other output.
1850	 */
1851	if (!quiet)
1852		flags |= EXT2_FLAG_PRINT_PROGRESS;
1853	retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
1854	if (retval) {
1855		com_err(device_name, retval, _("while setting up superblock"));
1856		exit(1);
1857	}
1858
1859	sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1860		32768 : fs->blocksize * 8);
1861	io_channel_set_options(fs->io, tdb_string);
1862
1863	if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1864		fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1865
1866	if ((fs_param.s_feature_incompat &
1867	     (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
1868	    (fs_param.s_feature_ro_compat &
1869	     (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
1870	      EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
1871	      EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
1872		fs->super->s_kbytes_written = 1;
1873
1874	/*
1875	 * Wipe out the old on-disk superblock
1876	 */
1877	if (!noaction)
1878		zap_sector(fs, 2, 6);
1879
1880	/*
1881	 * Parse or generate a UUID for the filesystem
1882	 */
1883	if (fs_uuid) {
1884		if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
1885			com_err(device_name, 0, "could not parse UUID: %s\n",
1886				fs_uuid);
1887			exit(1);
1888		}
1889	} else
1890		uuid_generate(fs->super->s_uuid);
1891
1892	/*
1893	 * Initialize the directory index variables
1894	 */
1895	hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
1896					       "half_md4");
1897	hash_alg = e2p_string2hash(hash_alg_str);
1898	fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
1899		EXT2_HASH_HALF_MD4;
1900	uuid_generate((unsigned char *) fs->super->s_hash_seed);
1901
1902	/*
1903	 * Add "jitter" to the superblock's check interval so that we
1904	 * don't check all the filesystems at the same time.  We use a
1905	 * kludgy hack of using the UUID to derive a random jitter value.
1906	 */
1907	for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1908		val += fs->super->s_uuid[i];
1909	fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1910
1911	/*
1912	 * Override the creator OS, if applicable
1913	 */
1914	if (creator_os && !set_os(fs->super, creator_os)) {
1915		com_err (program_name, 0, _("unknown os - %s"), creator_os);
1916		exit(1);
1917	}
1918
1919	/*
1920	 * For the Hurd, we will turn off filetype since it doesn't
1921	 * support it.
1922	 */
1923	if (fs->super->s_creator_os == EXT2_OS_HURD)
1924		fs->super->s_feature_incompat &=
1925			~EXT2_FEATURE_INCOMPAT_FILETYPE;
1926
1927	/*
1928	 * Set the volume label...
1929	 */
1930	if (volume_label) {
1931		memset(fs->super->s_volume_name, 0,
1932		       sizeof(fs->super->s_volume_name));
1933		strncpy(fs->super->s_volume_name, volume_label,
1934			sizeof(fs->super->s_volume_name));
1935	}
1936
1937	/*
1938	 * Set the last mount directory
1939	 */
1940	if (mount_dir) {
1941		memset(fs->super->s_last_mounted, 0,
1942		       sizeof(fs->super->s_last_mounted));
1943		strncpy(fs->super->s_last_mounted, mount_dir,
1944			sizeof(fs->super->s_last_mounted));
1945	}
1946
1947	if (!quiet || noaction)
1948		show_stats(fs);
1949
1950	if (noaction)
1951		exit(0);
1952
1953	if (fs->super->s_feature_incompat &
1954	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1955		create_journal_dev(fs);
1956		exit(ext2fs_close(fs) ? 1 : 0);
1957	}
1958
1959	if (bad_blocks_filename)
1960		read_bb_file(fs, &bb_list, bad_blocks_filename);
1961	if (cflag)
1962		test_disk(fs, &bb_list);
1963
1964	handle_bad_blocks(fs, bb_list);
1965	fs->stride = fs_stride = fs->super->s_raid_stride;
1966	if (!quiet)
1967		printf(_("Allocating group tables: "));
1968	retval = ext2fs_allocate_tables(fs);
1969	if (retval) {
1970		com_err(program_name, retval,
1971			_("while trying to allocate filesystem tables"));
1972		exit(1);
1973	}
1974	if (!quiet)
1975		printf(_("done                            \n"));
1976	if (super_only) {
1977		fs->super->s_state |= EXT2_ERROR_FS;
1978		fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1979	} else {
1980		/* rsv must be a power of two (64kB is MD RAID sb alignment) */
1981		unsigned int rsv = 65536 / fs->blocksize;
1982		unsigned long blocks = ext2fs_blocks_count(fs->super);
1983		unsigned long start;
1984		blk_t ret_blk;
1985
1986#ifdef ZAP_BOOTBLOCK
1987		zap_sector(fs, 0, 2);
1988#endif
1989
1990		/*
1991		 * Wipe out any old MD RAID (or other) metadata at the end
1992		 * of the device.  This will also verify that the device is
1993		 * as large as we think.  Be careful with very small devices.
1994		 */
1995		start = (blocks & ~(rsv - 1));
1996		if (start > rsv)
1997			start -= rsv;
1998		if (start > 0)
1999			retval = ext2fs_zero_blocks(fs, start, blocks - start,
2000						    &ret_blk, NULL);
2001
2002		if (retval) {
2003			com_err(program_name, retval,
2004				_("while zeroing block %u at end of filesystem"),
2005				ret_blk);
2006		}
2007		write_inode_tables(fs, lazy_itable_init);
2008		create_root_dir(fs);
2009		create_lost_and_found(fs);
2010		reserve_inodes(fs);
2011		create_bad_block_inode(fs, bb_list);
2012		if (fs->super->s_feature_compat &
2013		    EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2014			retval = ext2fs_create_resize_inode(fs);
2015			if (retval) {
2016				com_err("ext2fs_create_resize_inode", retval,
2017				_("while reserving blocks for online resize"));
2018				exit(1);
2019			}
2020		}
2021	}
2022
2023	if (journal_device) {
2024		ext2_filsys	jfs;
2025
2026		if (!force)
2027			check_plausibility(journal_device);
2028		check_mount(journal_device, force, _("journal"));
2029
2030		retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2031				     EXT2_FLAG_JOURNAL_DEV_OK, 0,
2032				     fs->blocksize, unix_io_manager, &jfs);
2033		if (retval) {
2034			com_err(program_name, retval,
2035				_("while trying to open journal device %s\n"),
2036				journal_device);
2037			exit(1);
2038		}
2039		if (!quiet) {
2040			printf(_("Adding journal to device %s: "),
2041			       journal_device);
2042			fflush(stdout);
2043		}
2044		retval = ext2fs_add_journal_device(fs, jfs);
2045		if(retval) {
2046			com_err (program_name, retval,
2047				 _("\n\twhile trying to add journal to device %s"),
2048				 journal_device);
2049			exit(1);
2050		}
2051		if (!quiet)
2052			printf(_("done\n"));
2053		ext2fs_close(jfs);
2054		free(journal_device);
2055	} else if ((journal_size) ||
2056		   (fs_param.s_feature_compat &
2057		    EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2058		journal_blocks = figure_journal_size(journal_size, fs);
2059
2060		if (super_only) {
2061			printf(_("Skipping journal creation in super-only mode\n"));
2062			fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2063			goto no_journal;
2064		}
2065
2066		if (!journal_blocks) {
2067			fs->super->s_feature_compat &=
2068				~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2069			goto no_journal;
2070		}
2071		if (!quiet) {
2072			printf(_("Creating journal (%u blocks): "),
2073			       journal_blocks);
2074			fflush(stdout);
2075		}
2076		retval = ext2fs_add_journal_inode(fs, journal_blocks,
2077						  journal_flags);
2078		if (retval) {
2079			com_err (program_name, retval,
2080				 _("\n\twhile trying to create journal"));
2081			exit(1);
2082		}
2083		if (!quiet)
2084			printf(_("done\n"));
2085	}
2086no_journal:
2087
2088	if (!quiet)
2089		printf(_("Writing superblocks and "
2090		       "filesystem accounting information: "));
2091	retval = ext2fs_flush(fs);
2092	if (retval) {
2093		fprintf(stderr,
2094			_("\nWarning, had trouble writing out superblocks."));
2095	}
2096	if (!quiet) {
2097		printf(_("done\n\n"));
2098		if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2099			print_check_message(fs);
2100	}
2101	val = ext2fs_close(fs);
2102	remove_error_table(&et_ext2_error_table);
2103	remove_error_table(&et_prof_error_table);
2104	profile_release(profile);
2105	return (retval || val) ? 1 : 0;
2106}
2107