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