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