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