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