]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/mke2fs.c
mke2fs: free tdb_dir string if it came from the profile
[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>
13b0b123 50#include <sys/stat.h>
b626b39a 51#include <libgen.h>
36585791 52#include <limits.h>
9ed8e5fe 53#include <blkid/blkid.h>
f3db3566 54
54c637d4 55#include "ext2fs/ext2_fs.h"
95fd65bb 56#include "ext2fs/ext2fsP.h"
3839e657 57#include "et/com_err.h"
1e3472c5 58#include "uuid/uuid.h"
896938d5 59#include "e2p/e2p.h"
3839e657 60#include "ext2fs/ext2fs.h"
63985320 61#include "util.h"
9dc6ad1e 62#include "profile.h"
a6d8302b 63#include "prof_err.h"
3839e657 64#include "../version.h"
d9c56d3c 65#include "nls-enable.h"
1f5d7a89 66#include "quota/mkquota.h"
3839e657
TT
67
68#define STRIDE_LENGTH 8
69
493024ea
TT
70#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
71
6733c2fd 72#ifndef __sparc__
1e3472c5
TT
73#define ZAP_BOOTBLOCK
74#endif
75
7d9e3165
LC
76#define DISCARD_STEP_MB (2048)
77
3839e657 78extern int isatty(int);
f3db3566 79extern FILE *fpopen(const char *cmd, const char *mode);
3839e657
TT
80
81const char * program_name = "mke2fs";
d48755e9 82const char * device_name /* = NULL */;
3839e657
TT
83
84/* Command line options */
16ed5b3a
TT
85int cflag;
86int verbose;
87int quiet;
88int super_only;
7fe5ff3c 89int discard = 1; /* attempt to discard device before fs creation */
16ed5b3a
TT
90int force;
91int noaction;
92int journal_size;
93int journal_flags;
7fe5ff3c 94int lazy_itable_init;
16ed5b3a
TT
95char *bad_blocks_filename;
96__u32 fs_stride;
3839e657 97
9b9a780f 98struct ext2_super_block fs_param;
b0afdda1 99char *fs_uuid = NULL;
16ed5b3a
TT
100char *creator_os;
101char *volume_label;
102char *mount_dir;
103char *journal_device;
104int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
b626b39a 105char **fs_types;
3839e657 106
9dc6ad1e
TT
107profile_t profile;
108
31e29a12 109int sys_page_size = 4096;
d99225ec 110int linux_version_code = 0;
31e29a12 111
63985320 112static void usage(void)
3839e657 113{
bdd80f28 114 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
067911ae 115 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
bdd80f28 116 "[-J journal-options]\n"
9ba40002
TT
117 "\t[-G meta group size] [-N number-of-inodes]\n"
118 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
119 "\t[-g blocks-per-group] [-L volume-label] "
067911ae 120 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
bdd80f28 121 "[-r fs-revision] [-E extended-option[,...]]\n"
5827d241 122 "\t[-T fs-type] [-U UUID] [-jnqvFKSV] device [blocks-count]\n"),
3839e657
TT
123 program_name);
124 exit(1);
125}
126
02d6f47e 127static int int_log2(unsigned long long arg)
3839e657
TT
128{
129 int l = 0;
130
131 arg >>= 1;
132 while (arg) {
133 l++;
134 arg >>= 1;
135 }
136 return l;
137}
138
02d6f47e 139static int int_log10(unsigned long long arg)
1dde43f0
TT
140{
141 int l;
142
143 for (l=0; arg ; l++)
144 arg = arg / 10;
145 return l;
146}
147
d99225ec
TT
148static int parse_version_number(const char *s)
149{
150 int major, minor, rev;
151 char *endptr;
152 const char *cp = s;
153
154 if (!s)
155 return 0;
156 major = strtol(cp, &endptr, 10);
157 if (cp == endptr || *endptr != '.')
158 return 0;
159 cp = endptr + 1;
160 minor = strtol(cp, &endptr, 10);
161 if (cp == endptr || *endptr != '.')
162 return 0;
163 cp = endptr + 1;
164 rev = strtol(cp, &endptr, 10);
165 if (cp == endptr)
166 return 0;
167 return ((((major * 256) + minor) * 256) + rev);
168}
169
3839e657
TT
170/*
171 * Helper function for read_bb_file and test_disk
172 */
54434927 173static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
3839e657 174{
6693837e 175 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
3839e657
TT
176 return;
177}
178
179/*
180 * Reads the bad blocks list from a file
181 */
182static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
183 const char *bad_blocks_file)
184{
185 FILE *f;
186 errcode_t retval;
187
188 f = fopen(bad_blocks_file, "r");
189 if (!f) {
190 com_err("read_bad_blocks_file", errno,
d9c56d3c 191 _("while trying to open %s"), bad_blocks_file);
3839e657
TT
192 exit(1);
193 }
194 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
195 fclose (f);
196 if (retval) {
197 com_err("ext2fs_read_bb_FILE", retval,
d9c56d3c 198 _("while reading in list of bad blocks from file"));
3839e657
TT
199 exit(1);
200 }
201}
202
203/*
204 * Runs the badblocks program to test the disk
205 */
206static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
207{
208 FILE *f;
209 errcode_t retval;
210 char buf[1024];
211
4efbac6f 212 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
3ed57c27 213 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
4efbac6f 214 fs->device_name, ext2fs_blocks_count(fs->super)-1);
3839e657 215 if (verbose)
d9c56d3c 216 printf(_("Running command: %s\n"), buf);
3839e657
TT
217 f = popen(buf, "r");
218 if (!f) {
219 com_err("popen", errno,
f37ab68a 220 _("while trying to run '%s'"), buf);
3839e657
TT
221 exit(1);
222 }
223 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
f3db3566 224 pclose(f);
3839e657
TT
225 if (retval) {
226 com_err("ext2fs_read_bb_FILE", retval,
d9c56d3c 227 _("while processing list of bad blocks from program"));
3839e657
TT
228 exit(1);
229 }
230}
231
232static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
233{
54434927
TT
234 dgrp_t i;
235 blk_t j;
236 unsigned must_be_good;
3839e657
TT
237 blk_t blk;
238 badblocks_iterate bb_iter;
239 errcode_t retval;
f3db3566
TT
240 blk_t group_block;
241 int group;
242 int group_bad;
3839e657
TT
243
244 if (!bb_list)
245 return;
efc6f628 246
3839e657
TT
247 /*
248 * The primary superblock and group descriptors *must* be
249 * good; if not, abort.
250 */
251 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
252 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
cbbf031b 253 if (ext2fs_badblocks_list_test(bb_list, i)) {
d9c56d3c
TT
254 fprintf(stderr, _("Block %d in primary "
255 "superblock/group descriptor area bad.\n"), i);
d0ff90d5 256 fprintf(stderr, _("Blocks %u through %u must be good "
d9c56d3c 257 "in order to build a filesystem.\n"),
3839e657 258 fs->super->s_first_data_block, must_be_good);
54434927 259 fputs(_("Aborting....\n"), stderr);
3839e657
TT
260 exit(1);
261 }
262 }
f3db3566
TT
263
264 /*
265 * See if any of the bad blocks are showing up in the backup
266 * superblocks and/or group descriptors. If so, issue a
267 * warning and adjust the block counts appropriately.
268 */
269 group_block = fs->super->s_first_data_block +
270 fs->super->s_blocks_per_group;
efc6f628 271
f3db3566 272 for (i = 1; i < fs->group_desc_count; i++) {
92bcc595 273 group_bad = 0;
f3db3566 274 for (j=0; j < fs->desc_blocks+1; j++) {
cbbf031b
TT
275 if (ext2fs_badblocks_list_test(bb_list,
276 group_block + j)) {
efc6f628 277 if (!group_bad)
f3db3566 278 fprintf(stderr,
8deb80a5 279_("Warning: the backup superblock/group descriptors at block %u contain\n"
d9c56d3c 280" bad blocks.\n\n"),
f3db3566
TT
281 group_block);
282 group_bad++;
6493f8e8 283 group = ext2fs_group_of_blk2(fs, group_block+j);
d7cca6b0 284 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
5711ed29 285 ext2fs_group_desc_csum_set(fs, group);
4efbac6f 286 ext2fs_free_blocks_count_add(fs->super, 1);
f3db3566
TT
287 }
288 }
289 group_block += fs->super->s_blocks_per_group;
290 }
efc6f628 291
3839e657
TT
292 /*
293 * Mark all the bad blocks as used...
294 */
cbbf031b 295 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
3839e657 296 if (retval) {
cbbf031b 297 com_err("ext2fs_badblocks_list_iterate_begin", retval,
d9c56d3c 298 _("while marking bad blocks as used"));
3839e657
TT
299 exit(1);
300 }
efc6f628 301 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
c6ed60cd 302 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
cbbf031b 303 ext2fs_badblocks_list_iterate_end(bb_iter);
3839e657
TT
304}
305
6fcd6f84 306static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
16ed5b3a
TT
307{
308 errcode_t retval;
02d6f47e 309 blk64_t blk;
54434927 310 dgrp_t i;
e1632421 311 int num;
95fd65bb 312 struct ext2fs_numeric_progress_struct progress;
16ed5b3a 313
95fd65bb
VAH
314 ext2fs_numeric_progress_init(fs, &progress,
315 _("Writing inode tables: "),
316 fs->group_desc_count);
1dde43f0 317
3839e657 318 for (i = 0; i < fs->group_desc_count; i++) {
95fd65bb 319 ext2fs_numeric_progress_update(fs, &progress, i);
efc6f628 320
d7cca6b0 321 blk = ext2fs_inode_table_loc(fs, i);
19c78dc0 322 num = fs->inode_blocks_per_group;
16ed5b3a 323
e1632421
TT
324 if (lazy_flag)
325 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
326 ext2fs_bg_itable_unused(fs, i)) *
327 EXT2_INODE_SIZE(fs->super),
328 EXT2_BLOCK_SIZE(fs->super));
6fcd6f84 329 if (!lazy_flag || itable_zeroed) {
d2d22a29 330 /* The kernel doesn't need to zero the itable blocks */
e633b58a 331 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
5711ed29 332 ext2fs_group_desc_csum_set(fs, i);
19c78dc0 333 }
02d6f47e 334 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
0f2794c0
TT
335 if (retval) {
336 fprintf(stderr, _("\nCould not write %d "
02d6f47e 337 "blocks in inode table starting at %llu: %s\n"),
0f2794c0
TT
338 num, blk, error_message(retval));
339 exit(1);
340 }
7d5633cf
TT
341 if (sync_kludge) {
342 if (sync_kludge == 1)
343 sync();
344 else if ((i % sync_kludge) == 0)
345 sync();
346 }
3839e657 347 }
02d6f47e 348 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
95fd65bb
VAH
349 ext2fs_numeric_progress_close(fs, &progress,
350 _("done \n"));
3839e657
TT
351}
352
353static void create_root_dir(ext2_filsys fs)
354{
5113a6e3 355 errcode_t retval;
f3db3566 356 struct ext2_inode inode;
5113a6e3 357 __u32 uid, gid;
3839e657
TT
358
359 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
360 if (retval) {
d9c56d3c 361 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
3839e657
TT
362 exit(1);
363 }
f3db3566
TT
364 if (geteuid()) {
365 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
366 if (retval) {
367 com_err("ext2fs_read_inode", retval,
d9c56d3c 368 _("while reading root inode"));
f3db3566
TT
369 exit(1);
370 }
5113a6e3
ES
371 uid = getuid();
372 inode.i_uid = uid;
15343922 373 ext2fs_set_i_uid_high(inode, uid >> 16);
5113a6e3
ES
374 if (uid) {
375 gid = getgid();
376 inode.i_gid = gid;
15343922 377 ext2fs_set_i_gid_high(inode, gid >> 16);
5113a6e3 378 }
e27b4563 379 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
f3db3566
TT
380 if (retval) {
381 com_err("ext2fs_write_inode", retval,
d9c56d3c 382 _("while setting root inode ownership"));
f3db3566
TT
383 exit(1);
384 }
385 }
3839e657
TT
386}
387
388static void create_lost_and_found(ext2_filsys fs)
389{
2d328bb7 390 unsigned int lpf_size = 0;
3839e657 391 errcode_t retval;
dfcdc32f 392 ext2_ino_t ino;
3839e657
TT
393 const char *name = "lost+found";
394 int i;
395
6a525069 396 fs->umask = 077;
3839e657
TT
397 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
398 if (retval) {
d9c56d3c
TT
399 com_err("ext2fs_mkdir", retval,
400 _("while creating /lost+found"));
3839e657
TT
401 exit(1);
402 }
403
404 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
405 if (retval) {
d9c56d3c
TT
406 com_err("ext2_lookup", retval,
407 _("while looking up /lost+found"));
3839e657
TT
408 exit(1);
409 }
efc6f628 410
3839e657 411 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
8c7c6eb1
AD
412 /* Ensure that lost+found is at least 2 blocks, so we always
413 * test large empty blocks for big-block filesystems. */
414 if ((lpf_size += fs->blocksize) >= 16*1024 &&
415 lpf_size >= 2 * fs->blocksize)
50787ea2 416 break;
3839e657
TT
417 retval = ext2fs_expand_dir(fs, ino);
418 if (retval) {
419 com_err("ext2fs_expand_dir", retval,
d9c56d3c 420 _("while expanding /lost+found"));
3839e657
TT
421 exit(1);
422 }
6a525069 423 }
3839e657
TT
424}
425
426static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
427{
428 errcode_t retval;
efc6f628 429
463e7327 430 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
5711ed29 431 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
3839e657
TT
432 retval = ext2fs_update_bb_inode(fs, bb_list);
433 if (retval) {
434 com_err("ext2fs_update_bb_inode", retval,
d9c56d3c 435 _("while setting bad block inode"));
3839e657
TT
436 exit(1);
437 }
438
439}
440
441static void reserve_inodes(ext2_filsys fs)
442{
dfcdc32f 443 ext2_ino_t i;
3839e657 444
5711ed29
TT
445 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
446 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
3839e657
TT
447 ext2fs_mark_ib_dirty(fs);
448}
449
756df353 450#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
7ef3bb83 451#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
756df353 452#define BSD_LABEL_OFFSET 64
756df353 453
04a96857 454static void zap_sector(ext2_filsys fs, int sect, int nsect)
f3db3566 455{
3f85f65c 456 char *buf;
f3db3566 457 int retval;
756df353 458 unsigned int *magic;
f3db3566 459
3f85f65c 460 buf = malloc(512*nsect);
568101f7 461 if (!buf) {
4ea7bd04
TT
462 printf(_("Out of memory erasing sectors %d-%d\n"),
463 sect, sect + nsect - 1);
568101f7
AD
464 exit(1);
465 }
756df353 466
7ef3bb83
TT
467 if (sect == 0) {
468 /* Check for a BSD disklabel, and don't erase it if so */
24a117ab 469 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
7ef3bb83
TT
470 if (retval)
471 fprintf(stderr,
472 _("Warning: could not read block 0: %s\n"),
473 error_message(retval));
474 else {
475 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
476 if ((*magic == BSD_DISKMAGIC) ||
477 (*magic == BSD_MAGICDISK))
478 return;
479 }
756df353 480 }
756df353 481
7098810d 482 memset(buf, 0, 512*nsect);
e41784eb 483 io_channel_set_blksize(fs->io, 512);
24a117ab 484 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
e41784eb 485 io_channel_set_blksize(fs->io, fs->blocksize);
3f85f65c 486 free(buf);
f3db3566 487 if (retval)
6693837e
TT
488 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
489 sect, error_message(retval));
f3db3566 490}
16ed5b3a
TT
491
492static void create_journal_dev(ext2_filsys fs)
493{
95fd65bb 494 struct ext2fs_numeric_progress_struct progress;
16ed5b3a
TT
495 errcode_t retval;
496 char *buf;
02d6f47e 497 blk64_t blk, err_blk;
b626b39a 498 int c, count, err_count;
16ed5b3a 499
d6a27e00 500 retval = ext2fs_create_journal_superblock(fs,
4efbac6f 501 ext2fs_blocks_count(fs->super), 0, &buf);
d6a27e00
TT
502 if (retval) {
503 com_err("create_journal_dev", retval,
504 _("while initializing journal superblock"));
505 exit(1);
506 }
6c54689f
AD
507
508 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
509 goto write_superblock;
510
95fd65bb
VAH
511 ext2fs_numeric_progress_init(fs, &progress,
512 _("Zeroing journal device: "),
513 ext2fs_blocks_count(fs->super));
b626b39a 514 blk = 0;
4efbac6f 515 count = ext2fs_blocks_count(fs->super);
b626b39a
AK
516 while (count > 0) {
517 if (count > 1024)
518 c = 1024;
519 else
520 c = count;
02d6f47e 521 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
b626b39a
AK
522 if (retval) {
523 com_err("create_journal_dev", retval,
524 _("while zeroing journal device "
02d6f47e 525 "(block %llu, count %d)"),
b626b39a
AK
526 err_blk, err_count);
527 exit(1);
528 }
529 blk += c;
530 count -= c;
95fd65bb 531 ext2fs_numeric_progress_update(fs, &progress, blk);
16ed5b3a 532 }
02d6f47e 533 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
dc2ec525 534
6c54689f
AD
535 ext2fs_numeric_progress_close(fs, &progress, NULL);
536write_superblock:
24a117ab
VAH
537 retval = io_channel_write_blk64(fs->io,
538 fs->super->s_first_data_block+1,
539 1, buf);
16ed5b3a
TT
540 if (retval) {
541 com_err("create_journal_dev", retval,
542 _("while writing journal superblock"));
543 exit(1);
544 }
16ed5b3a 545}
f3db3566 546
3839e657
TT
547static void show_stats(ext2_filsys fs)
548{
ef9abe5f 549 struct ext2_super_block *s = fs->super;
1e3472c5 550 char buf[80];
63253946 551 char *os;
02d6f47e 552 blk64_t group_block;
54434927
TT
553 dgrp_t i;
554 int need, col_left;
efc6f628 555
4efbac6f
VAH
556 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
557 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
558 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
50787ea2
TT
559
560 memset(buf, 0, sizeof(buf));
561 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
d9c56d3c 562 printf(_("Filesystem label=%s\n"), buf);
74e1211b
TT
563 os = e2p_os2string(fs->super->s_creator_os);
564 if (os)
565 printf(_("OS type: %s\n"), os);
63253946 566 free(os);
d9c56d3c 567 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
50787ea2 568 s->s_log_block_size);
412376ef
TT
569 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
570 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
571 printf(_("Cluster size=%u (log=%u)\n"),
1da5ef70
TT
572 fs->blocksize << fs->cluster_ratio_bits,
573 s->s_log_cluster_size);
412376ef 574 else
1da5ef70 575 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
412376ef 576 s->s_log_cluster_size);
9ed8e5fe
ES
577 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
578 s->s_raid_stride, s->s_raid_stripe_width);
4efbac6f
VAH
579 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
580 ext2fs_blocks_count(s));
581 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
582 ext2fs_r_blocks_count(s),
583 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
d9c56d3c 584 printf(_("First data block=%u\n"), s->s_first_data_block);
d323f8fb
TT
585 if (s->s_reserved_gdt_blocks)
586 printf(_("Maximum filesystem blocks=%lu\n"),
587 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
f2de1d38 588 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
d9c56d3c 589 if (fs->group_desc_count > 1)
c8c071a0 590 printf(_("%u block groups\n"), fs->group_desc_count);
d9c56d3c 591 else
c8c071a0 592 printf(_("%u block group\n"), fs->group_desc_count);
412376ef
TT
593 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
594 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
595 printf(_("%u blocks per group, %u clusters per group\n"),
596 s->s_blocks_per_group, s->s_clusters_per_group);
597 else
598 printf(_("%u blocks per group, %u fragments per group\n"),
599 s->s_blocks_per_group, s->s_clusters_per_group);
d9c56d3c 600 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
3839e657
TT
601
602 if (fs->group_desc_count == 1) {
603 printf("\n");
604 return;
605 }
d323f8fb 606
d9c56d3c 607 printf(_("Superblock backups stored on blocks: "));
3839e657
TT
608 group_block = s->s_first_data_block;
609 col_left = 0;
610 for (i = 1; i < fs->group_desc_count; i++) {
611 group_block += s->s_blocks_per_group;
521e3685
TT
612 if (!ext2fs_bg_has_super(fs, i))
613 continue;
7671433a
TT
614 if (i != 1)
615 printf(", ");
6733c2fd 616 need = int_log10(group_block) + 2;
1dde43f0 617 if (need > col_left) {
3839e657 618 printf("\n\t");
1dde43f0 619 col_left = 72;
3839e657 620 }
1dde43f0 621 col_left -= need;
02d6f47e 622 printf("%llu", group_block);
3839e657
TT
623 }
624 printf("\n\n");
625}
626
1e3472c5
TT
627/*
628 * Set the S_CREATOR_OS field. Return true if OS is known,
629 * otherwise, 0.
630 */
631static int set_os(struct ext2_super_block *sb, char *os)
632{
633 if (isdigit (*os))
634 sb->s_creator_os = atoi (os);
635 else if (strcasecmp(os, "linux") == 0)
636 sb->s_creator_os = EXT2_OS_LINUX;
637 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
638 sb->s_creator_os = EXT2_OS_HURD;
ea1e8f47
TT
639 else if (strcasecmp(os, "freebsd") == 0)
640 sb->s_creator_os = EXT2_OS_FREEBSD;
641 else if (strcasecmp(os, "lites") == 0)
642 sb->s_creator_os = EXT2_OS_LITES;
1e3472c5
TT
643 else
644 return 0;
645 return 1;
646}
647
a418d3ad
TT
648#define PATH_SET "PATH=/sbin"
649
efc6f628 650static void parse_extended_opts(struct ext2_super_block *param,
c6a44136 651 const char *opts)
a29f4d30 652{
2d328bb7 653 char *buf, *token, *next, *p, *arg, *badopt = 0;
a29f4d30 654 int len;
d323f8fb 655 int r_usage = 0;
a29f4d30
TT
656
657 len = strlen(opts);
658 buf = malloc(len+1);
659 if (!buf) {
d323f8fb
TT
660 fprintf(stderr,
661 _("Couldn't allocate memory to parse options!\n"));
a29f4d30
TT
662 exit(1);
663 }
664 strcpy(buf, opts);
665 for (token = buf; token && *token; token = next) {
666 p = strchr(token, ',');
667 next = 0;
668 if (p) {
669 *p = 0;
670 next = p+1;
d323f8fb 671 }
a29f4d30
TT
672 arg = strchr(token, '=');
673 if (arg) {
674 *arg = 0;
675 arg++;
676 }
677 if (strcmp(token, "stride") == 0) {
678 if (!arg) {
d323f8fb 679 r_usage++;
0c17cb25 680 badopt = token;
a29f4d30
TT
681 continue;
682 }
0c17cb25 683 param->s_raid_stride = strtoul(arg, &p, 0);
5b734a0e 684 if (*p) {
d9c56d3c 685 fprintf(stderr,
f37ab68a
TT
686 _("Invalid stride parameter: %s\n"),
687 arg);
d323f8fb
TT
688 r_usage++;
689 continue;
690 }
0c17cb25
TT
691 } else if (strcmp(token, "stripe-width") == 0 ||
692 strcmp(token, "stripe_width") == 0) {
693 if (!arg) {
694 r_usage++;
695 badopt = token;
696 continue;
697 }
698 param->s_raid_stripe_width = strtoul(arg, &p, 0);
5b734a0e 699 if (*p) {
0c17cb25
TT
700 fprintf(stderr,
701 _("Invalid stripe-width parameter: %s\n"),
702 arg);
703 r_usage++;
704 continue;
705 }
d323f8fb 706 } else if (!strcmp(token, "resize")) {
02d6f47e
JS
707 blk64_t resize;
708 unsigned long bpg, rsv_groups;
c6a44136
TT
709 unsigned long group_desc_count, desc_blocks;
710 unsigned int gdpb, blocksize;
711 int rsv_gdb;
d323f8fb
TT
712
713 if (!arg) {
714 r_usage++;
0c17cb25 715 badopt = token;
a29f4d30
TT
716 continue;
717 }
d323f8fb 718
02d6f47e
JS
719 resize = parse_num_blocks2(arg,
720 param->s_log_block_size);
d323f8fb
TT
721
722 if (resize == 0) {
efc6f628 723 fprintf(stderr,
55f4cbd9
TT
724 _("Invalid resize parameter: %s\n"),
725 arg);
d323f8fb
TT
726 r_usage++;
727 continue;
728 }
4efbac6f 729 if (resize <= ext2fs_blocks_count(param)) {
efc6f628 730 fprintf(stderr,
f37ab68a
TT
731 _("The resize maximum must be greater "
732 "than the filesystem size.\n"));
c6a44136
TT
733 r_usage++;
734 continue;
735 }
d323f8fb 736
c6a44136
TT
737 blocksize = EXT2_BLOCK_SIZE(param);
738 bpg = param->s_blocks_per_group;
739 if (!bpg)
740 bpg = blocksize * 8;
f2de1d38 741 gdpb = EXT2_DESC_PER_BLOCK(param);
4efbac6f
VAH
742 group_desc_count = (__u32) ext2fs_div64_ceil(
743 ext2fs_blocks_count(param), bpg);
c6a44136
TT
744 desc_blocks = (group_desc_count +
745 gdpb - 1) / gdpb;
02d6f47e 746 rsv_groups = ext2fs_div64_ceil(resize, bpg);
efc6f628 747 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
c6a44136 748 desc_blocks;
9b9a780f 749 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
c6a44136
TT
750 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
751
752 if (rsv_gdb > 0) {
b290d2dc 753 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
efc6f628 754 fprintf(stderr,
b290d2dc 755 _("On-line resizing not supported with revision 0 filesystems\n"));
21400381 756 free(buf);
b290d2dc
TT
757 exit(1);
758 }
c6a44136
TT
759 param->s_feature_compat |=
760 EXT2_FEATURE_COMPAT_RESIZE_INODE;
761
762 param->s_reserved_gdt_blocks = rsv_gdb;
763 }
6cb27404
TT
764 } else if (!strcmp(token, "test_fs")) {
765 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
a4396e9d 766 } else if (!strcmp(token, "lazy_itable_init")) {
43781b94
TT
767 if (arg)
768 lazy_itable_init = strtoul(arg, &p, 0);
769 else
770 lazy_itable_init = 1;
6c54689f
AD
771 } else if (!strcmp(token, "lazy_journal_init")) {
772 if (arg)
773 journal_flags |= strtoul(arg, &p, 0) ?
774 EXT2_MKJOURNAL_LAZYINIT : 0;
775 else
776 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
0bc85dfb
LC
777 } else if (!strcmp(token, "discard")) {
778 discard = 1;
779 } else if (!strcmp(token, "nodiscard")) {
780 discard = 0;
0c17cb25 781 } else {
d323f8fb 782 r_usage++;
0c17cb25
TT
783 badopt = token;
784 }
a29f4d30 785 }
d323f8fb 786 if (r_usage) {
0c17cb25 787 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
bb145b01 788 "Extended options are separated by commas, "
a29f4d30
TT
789 "and may take an argument which\n"
790 "\tis set off by an equals ('=') sign.\n\n"
bb145b01 791 "Valid extended options are:\n"
0c17cb25
TT
792 "\tstride=<RAID per-disk data chunk in blocks>\n"
793 "\tstripe-width=<RAID stride * data disks in blocks>\n"
a4396e9d
TT
794 "\tresize=<resize maximum size in blocks>\n"
795 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
6c54689f 796 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
0bc85dfb
LC
797 "\ttest_fs\n"
798 "\tdiscard\n"
799 "\tnodiscard\n\n"),
2d328bb7 800 badopt ? badopt : "");
21400381 801 free(buf);
a29f4d30
TT
802 exit(1);
803 }
0c17cb25
TT
804 if (param->s_raid_stride &&
805 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
806 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
807 "multiple of stride %u.\n\n"),
808 param->s_raid_stripe_width, param->s_raid_stride);
809
21400381 810 free(buf);
efc6f628 811}
a29f4d30 812
896938d5 813static __u32 ok_features[3] = {
558df544 814 /* Compat */
843049c4 815 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
d323f8fb 816 EXT2_FEATURE_COMPAT_RESIZE_INODE |
f5fa2007 817 EXT2_FEATURE_COMPAT_DIR_INDEX |
558df544
TT
818 EXT2_FEATURE_COMPAT_EXT_ATTR,
819 /* Incompat */
820 EXT2_FEATURE_INCOMPAT_FILETYPE|
bf6b848e 821 EXT3_FEATURE_INCOMPAT_EXTENTS|
c046ac7f 822 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
c2d4300b 823 EXT2_FEATURE_INCOMPAT_META_BG|
02d6f47e
JS
824 EXT4_FEATURE_INCOMPAT_FLEX_BG|
825 EXT4_FEATURE_INCOMPAT_64BIT,
558df544
TT
826 /* R/O compat */
827 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
2be8fe43
TT
828 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
829 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
830 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
d2d22a29 831 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
c6ed60cd 832 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
1f5d7a89
AK
833 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
834 EXT4_FEATURE_RO_COMPAT_QUOTA
896938d5 835};
a29f4d30
TT
836
837
9dc6ad1e
TT
838static void syntax_err_report(const char *filename, long err, int line_num)
839{
efc6f628 840 fprintf(stderr,
9dc6ad1e
TT
841 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
842 filename, line_num, error_message(err));
843 exit(1);
844}
845
abcfdfda 846static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
9dc6ad1e 847
efc6f628 848static void edit_feature(const char *str, __u32 *compat_array)
9dc6ad1e
TT
849{
850 if (!str)
851 return;
852
853 if (e2p_edit_feature(str, compat_array, ok_features)) {
efc6f628 854 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
9dc6ad1e
TT
855 str);
856 exit(1);
857 }
858}
859
6a426c97
ES
860static void edit_mntopts(const char *str, __u32 *mntopts)
861{
862 if (!str)
863 return;
864
865 if (e2p_edit_mntopts(str, mntopts, ~0)) {
866 fprintf(stderr, _("Invalid mount option set: %s\n"),
867 str);
868 exit(1);
869 }
870}
871
3d43836f
TT
872struct str_list {
873 char **list;
874 int num;
875 int max;
876};
877
878static errcode_t init_list(struct str_list *sl)
879{
880 sl->num = 0;
881 sl->max = 0;
882 sl->list = malloc((sl->max+1) * sizeof(char *));
883 if (!sl->list)
884 return ENOMEM;
885 sl->list[0] = 0;
886 return 0;
887}
888
889static errcode_t push_string(struct str_list *sl, const char *str)
890{
891 char **new_list;
892
893 if (sl->num >= sl->max) {
894 sl->max += 2;
895 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
896 if (!new_list)
897 return ENOMEM;
898 sl->list = new_list;
899 }
900 sl->list[sl->num] = malloc(strlen(str)+1);
901 if (sl->list[sl->num] == 0)
902 return ENOMEM;
903 strcpy(sl->list[sl->num], str);
904 sl->num++;
905 sl->list[sl->num] = 0;
906 return 0;
907}
908
909static void print_str_list(char **list)
910{
911 char **cpp;
912
913 for (cpp = list; *cpp; cpp++) {
914 printf("'%s'", *cpp);
915 if (cpp[1])
916 fputs(", ", stdout);
917 }
918 fputc('\n', stdout);
919}
920
2ee4544d
TT
921/*
922 * Return TRUE if the profile has the given subsection
923 */
924static int profile_has_subsection(profile_t profile, const char *section,
925 const char *subsection)
926{
927 void *state;
928 const char *names[4];
929 char *name;
930 int ret = 0;
931
932 names[0] = section;
933 names[1] = subsection;
934 names[2] = 0;
935
936 if (profile_iterator_create(profile, names,
937 PROFILE_ITER_LIST_SECTION |
938 PROFILE_ITER_RELATIONS_ONLY, &state))
939 return 0;
940
941 if ((profile_iterator(&state, &name, 0) == 0) && name) {
942 free(name);
943 ret = 1;
944 }
945
946 profile_iterator_free(&state);
947 return ret;
948}
949
3d43836f
TT
950static char **parse_fs_type(const char *fs_type,
951 const char *usage_types,
952 struct ext2_super_block *fs_param,
493024ea 953 blk64_t fs_blocks_count,
3d43836f
TT
954 char *progname)
955{
956 const char *ext_type = 0;
957 char *parse_str;
958 char *profile_type = 0;
959 char *cp, *t;
960 const char *size_type;
961 struct str_list list;
493024ea 962 unsigned long long meg;
7f5601e5 963 int is_hurd = 0;
3d43836f
TT
964
965 if (init_list(&list))
966 return 0;
967
7f5601e5
TT
968 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
969 !strcasecmp(creator_os, "hurd")))
970 is_hurd = 1;
971
3d43836f
TT
972 if (fs_type)
973 ext_type = fs_type;
7f5601e5
TT
974 else if (is_hurd)
975 ext_type = "ext2";
1a71bd42
TT
976 else if (!strcmp(program_name, "mke3fs"))
977 ext_type = "ext3";
3d43836f
TT
978 else if (progname) {
979 ext_type = strrchr(progname, '/');
980 if (ext_type)
981 ext_type++;
982 else
983 ext_type = progname;
984
985 if (!strncmp(ext_type, "mkfs.", 5)) {
986 ext_type += 5;
987 if (ext_type[0] == 0)
988 ext_type = 0;
989 } else
990 ext_type = 0;
991 }
992
993 if (!ext_type) {
994 profile_get_string(profile, "defaults", "fs_type", 0,
995 "ext2", &profile_type);
996 ext_type = profile_type;
997 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
998 ext_type = "ext3";
999 }
1000
2ee4544d
TT
1001
1002 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1003 strcmp(ext_type, "ext2")) {
1004 printf(_("\nYour mke2fs.conf file does not define the "
1005 "%s filesystem type.\n"), ext_type);
1006 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1007 !strcmp(ext_type, "ext4dev")) {
bad89b2a
TT
1008 printf(_("You probably need to install an updated "
1009 "mke2fs.conf file.\n\n"));
2ee4544d
TT
1010 }
1011 if (!force) {
1012 printf(_("Aborting...\n"));
1013 exit(1);
bad89b2a
TT
1014 }
1015 }
1016
3d43836f 1017 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
493024ea 1018 if (fs_blocks_count < 3 * meg)
3d43836f 1019 size_type = "floppy";
493024ea 1020 else if (fs_blocks_count < 512 * meg)
3d43836f 1021 size_type = "small";
22d8ce51
NK
1022 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1023 size_type = "default";
1024 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
493024ea 1025 size_type = "big";
3d43836f 1026 else
22d8ce51 1027 size_type = "huge";
3d43836f
TT
1028
1029 if (!usage_types)
1030 usage_types = size_type;
1031
4d5cf8b1 1032 parse_str = malloc(strlen(usage_types)+1);
3d43836f
TT
1033 if (!parse_str) {
1034 free(list.list);
1035 return 0;
1036 }
4d5cf8b1 1037 strcpy(parse_str, usage_types);
3d43836f
TT
1038
1039 if (ext_type)
1040 push_string(&list, ext_type);
1041 cp = parse_str;
1042 while (1) {
1043 t = strchr(cp, ',');
1044 if (t)
1045 *t = '\0';
1046
2ee4544d 1047 if (*cp) {
0f7479b3
TT
1048 if (profile_has_subsection(profile, "fs_types", cp))
1049 push_string(&list, cp);
1050 else if (strcmp(cp, "default") != 0)
2ee4544d
TT
1051 fprintf(stderr,
1052 _("\nWarning: the fs_type %s is not "
0f7479b3 1053 "defined in mke2fs.conf\n\n"),
2ee4544d 1054 cp);
2ee4544d 1055 }
3d43836f
TT
1056 if (t)
1057 cp = t+1;
1058 else {
1059 cp = "";
1060 break;
1061 }
1062 }
1063 free(parse_str);
45e338f5 1064 free(profile_type);
7f5601e5
TT
1065 if (is_hurd)
1066 push_string(&list, "hurd");
3d43836f
TT
1067 return (list.list);
1068}
1069
1070static char *get_string_from_profile(char **fs_types, const char *opt,
1071 const char *def_val)
1072{
1073 char *ret = 0;
3d43836f
TT
1074 int i;
1075
1076 for (i=0; fs_types[i]; i++);
1077 for (i-=1; i >=0 ; i--) {
1078 profile_get_string(profile, "fs_types", fs_types[i],
1079 opt, 0, &ret);
1080 if (ret)
1081 return ret;
1082 }
1083 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1084 return (ret);
1085}
1086
1087static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1088{
1089 int ret;
1090 char **cpp;
1091
1092 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1093 for (cpp = fs_types; *cpp; cpp++)
1094 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1095 return ret;
1096}
1097
d3859af3
AK
1098static double get_double_from_profile(char **fs_types, const char *opt,
1099 double def_val)
1100{
1101 double ret;
1102 char **cpp;
1103
1104 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1105 for (cpp = fs_types; *cpp; cpp++)
1106 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1107 return ret;
1108}
1109
a4396e9d
TT
1110static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1111{
1112 int ret;
1113 char **cpp;
1114
1115 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1116 for (cpp = fs_types; *cpp; cpp++)
1117 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1118 return ret;
1119}
3d43836f 1120
d48bc604
TT
1121extern const char *mke2fs_default_profile;
1122static const char *default_files[] = { "<default>", 0 };
1123
9ed8e5fe
ES
1124#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1125/*
1126 * Sets the geometry of a device (stripe/stride), and returns the
1127 * device's alignment offset, if any, or a negative error.
1128 */
1599b470
TT
1129static int get_device_geometry(const char *file,
1130 struct ext2_super_block *fs_param,
1131 int psector_size)
9ed8e5fe
ES
1132{
1133 int rc = -1;
1134 int blocksize;
1135 blkid_probe pr;
1136 blkid_topology tp;
1137 unsigned long min_io;
1138 unsigned long opt_io;
13b0b123
ES
1139 struct stat statbuf;
1140
1141 /* Nothing to do for a regular file */
1142 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1143 return 0;
9ed8e5fe
ES
1144
1145 pr = blkid_new_probe_from_filename(file);
1146 if (!pr)
1147 goto out;
1148
1149 tp = blkid_probe_get_topology(pr);
1150 if (!tp)
1151 goto out;
1152
1153 min_io = blkid_topology_get_minimum_io_size(tp);
1154 opt_io = blkid_topology_get_optimal_io_size(tp);
1155 blocksize = EXT2_BLOCK_SIZE(fs_param);
1599b470
TT
1156 if ((min_io == 0) && (psector_size > blocksize))
1157 min_io = psector_size;
1158 if ((opt_io == 0) && min_io)
1159 opt_io = min_io;
1160 if ((opt_io == 0) && (psector_size > blocksize))
1161 opt_io = psector_size;
9ed8e5fe 1162
d568782a
ES
1163 /* setting stripe/stride to blocksize is pointless */
1164 if (min_io > blocksize)
1165 fs_param->s_raid_stride = min_io / blocksize;
1166 if (opt_io > blocksize)
1167 fs_param->s_raid_stripe_width = opt_io / blocksize;
9ed8e5fe
ES
1168
1169 rc = blkid_topology_get_alignment_offset(tp);
1170out:
1171 blkid_free_probe(pr);
1172 return rc;
1173}
1174#endif
1175
3839e657
TT
1176static void PRS(int argc, char *argv[])
1177{
c5290fae 1178 int b, c;
2d34a25f 1179 int cluster_size = 0;
79e62409 1180 char *tmp, **cpp;
4a600566
TT
1181 int blocksize = 0;
1182 int inode_ratio = 0;
932a489c 1183 int inode_size = 0;
9ba40002 1184 unsigned long flex_bg_size = 0;
d3859af3 1185 double reserved_ratio = -1.0;
bb1158b9 1186 int lsector_size = 0, psector_size = 0;
1e6e4c5e 1187 int show_version_only = 0;
de8f3a76 1188 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
a418d3ad 1189 errcode_t retval;
4a600566 1190 char * oldpath = getenv("PATH");
c6a44136 1191 char * extended_opts = 0;
4c2b28ab
TT
1192 char * fs_type = 0;
1193 char * usage_types = 0;
02d6f47e 1194 blk64_t dev_size;
493024ea 1195 blk64_t fs_blocks_count = 0;
756df353 1196#ifdef __linux__
4a600566 1197 struct utsname ut;
2740156b 1198#endif
31e29a12 1199 long sysval;
9dc6ad1e
TT
1200 int s_opt = -1, r_opt = -1;
1201 char *fs_features = 0;
1202 int use_bsize;
642935c0
TT
1203 char *newpath;
1204 int pathlen = sizeof(PATH_SET) + 1;
1205
1206 if (oldpath)
1207 pathlen += strlen(oldpath);
1208 newpath = malloc(pathlen);
9ec68af7
NK
1209 if (!newpath) {
1210 fprintf(stderr, _("Couldn't allocate memory for new PATH.\n"));
1211 exit(1);
1212 }
642935c0 1213 strcpy(newpath, PATH_SET);
14bbcbc3 1214
3839e657 1215 /* Update our PATH to include /sbin */
a418d3ad 1216 if (oldpath) {
a418d3ad
TT
1217 strcat (newpath, ":");
1218 strcat (newpath, oldpath);
642935c0
TT
1219 }
1220 putenv (newpath);
3839e657 1221
16ed5b3a
TT
1222 tmp = getenv("MKE2FS_SYNC");
1223 if (tmp)
1224 sync_kludge = atoi(tmp);
31e29a12
TT
1225
1226 /* Determine the system page size if possible */
1227#ifdef HAVE_SYSCONF
1228#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1229#define _SC_PAGESIZE _SC_PAGE_SIZE
1230#endif
1231#ifdef _SC_PAGESIZE
1232 sysval = sysconf(_SC_PAGESIZE);
aab6fe73 1233 if (sysval > 0)
31e29a12
TT
1234 sys_page_size = sysval;
1235#endif /* _SC_PAGESIZE */
1236#endif /* HAVE_SYSCONF */
9dc6ad1e
TT
1237
1238 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1239 config_fn[0] = tmp;
1240 profile_set_syntax_err_cb(syntax_err_report);
d48bc604
TT
1241 retval = profile_init(config_fn, &profile);
1242 if (retval == ENOENT) {
9ec68af7
NK
1243 retval = profile_init(default_files, &profile);
1244 if (retval)
1245 goto profile_error;
1246 retval = profile_set_default(profile, mke2fs_default_profile);
1247 if (retval)
1248 goto profile_error;
1249 } else if (retval) {
1250profile_error:
1251 fprintf(stderr, _("Couldn't init profile successfully"
1252 " (error: %ld).\n"), retval);
1253 exit(1);
d48bc604 1254 }
efc6f628 1255
3839e657
TT
1256 setbuf(stdout, NULL);
1257 setbuf(stderr, NULL);
a6d8302b
TT
1258 add_error_table(&et_ext2_error_table);
1259 add_error_table(&et_prof_error_table);
9b9a780f
TT
1260 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1261 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
843049c4 1262
756df353 1263#ifdef __linux__
14bbcbc3
TT
1264 if (uname(&ut)) {
1265 perror("uname");
1266 exit(1);
1267 }
d99225ec 1268 linux_version_code = parse_version_number(ut.release);
9dc6ad1e 1269 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
9b9a780f 1270 fs_param.s_rev_level = 0;
14bbcbc3 1271#endif
0072f8de
AD
1272
1273 if (argc && *argv) {
1274 program_name = get_progname(*argv);
1275
1276 /* If called as mkfs.ext3, create a journal inode */
1a71bd42
TT
1277 if (!strcmp(program_name, "mkfs.ext3") ||
1278 !strcmp(program_name, "mke3fs"))
0072f8de
AD
1279 journal_size = -1;
1280 }
1281
1e3472c5 1282 while ((c = getopt (argc, argv,
c6ed60cd 1283 "b:cg:i:jl:m:no:qr:s:t:vC:E:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
3839e657
TT
1284 switch (c) {
1285 case 'b':
c5290fae
TT
1286 blocksize = strtol(optarg, &tmp, 0);
1287 b = (blocksize > 0) ? blocksize : -blocksize;
1288 if (b < EXT2_MIN_BLOCK_SIZE ||
1289 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
d9c56d3c 1290 com_err(program_name, 0,
f37ab68a 1291 _("invalid block size - %s"), optarg);
3839e657
TT
1292 exit(1);
1293 }
932a489c
AD
1294 if (blocksize > 4096)
1295 fprintf(stderr, _("Warning: blocksize %d not "
1296 "usable on most systems.\n"),
1297 blocksize);
efc6f628 1298 if (blocksize > 0)
9b9a780f 1299 fs_param.s_log_block_size =
c5290fae
TT
1300 int_log2(blocksize >>
1301 EXT2_MIN_BLOCK_LOG_SIZE);
3839e657 1302 break;
b10fd5e8 1303 case 'c': /* Check for bad blocks */
3ed57c27 1304 cflag++;
3839e657 1305 break;
c6ed60cd 1306 case 'C':
2d34a25f
TT
1307 cluster_size = strtoul(optarg, &tmp, 0);
1308 if (cluster_size < EXT2_MIN_CLUSTER_SIZE ||
1309 cluster_size > EXT2_MAX_CLUSTER_SIZE || *tmp) {
d9c56d3c 1310 com_err(program_name, 0,
bb145b01 1311 _("invalid fragment size - %s"),
3839e657
TT
1312 optarg);
1313 exit(1);
1314 }
3839e657
TT
1315 break;
1316 case 'g':
9b9a780f 1317 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
f3db3566
TT
1318 if (*tmp) {
1319 com_err(program_name, 0,
d9c56d3c 1320 _("Illegal number for blocks per group"));
f3db3566
TT
1321 exit(1);
1322 }
9b9a780f 1323 if ((fs_param.s_blocks_per_group % 8) != 0) {
f3db3566 1324 com_err(program_name, 0,
d9c56d3c 1325 _("blocks per group must be multiple of 8"));
3839e657
TT
1326 exit(1);
1327 }
1328 break;
9ba40002
TT
1329 case 'G':
1330 flex_bg_size = strtoul(optarg, &tmp, 0);
1331 if (*tmp) {
1332 com_err(program_name, 0,
1333 _("Illegal number for flex_bg size"));
1334 exit(1);
1335 }
d531450c 1336 if (flex_bg_size < 1 ||
9ba40002
TT
1337 (flex_bg_size & (flex_bg_size-1)) != 0) {
1338 com_err(program_name, 0,
1339 _("flex_bg size must be a power of 2"));
1340 exit(1);
1341 }
1342 break;
3839e657
TT
1343 case 'i':
1344 inode_ratio = strtoul(optarg, &tmp, 0);
932a489c
AD
1345 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1346 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
3839e657 1347 *tmp) {
d9c56d3c 1348 com_err(program_name, 0,
bb145b01 1349 _("invalid inode ratio %s (min %d/max %d)"),
932a489c 1350 optarg, EXT2_MIN_BLOCK_SIZE,
e447ba37 1351 EXT2_MAX_BLOCK_SIZE * 1024);
3839e657
TT
1352 exit(1);
1353 }
1354 break;
dc2ec525
TT
1355 case 'J':
1356 parse_journal_opts(optarg);
1357 break;
5827d241 1358 case 'K':
0bc85dfb
LC
1359 fprintf(stderr, _("Warning: -K option is deprecated and "
1360 "should not be used anymore. Use "
1361 "\'-E nodiscard\' extended option "
1362 "instead!\n"));
5827d241
ES
1363 discard = 0;
1364 break;
85ef4ae8 1365 case 'j':
dc2ec525
TT
1366 if (!journal_size)
1367 journal_size = -1;
8ddaa66b 1368 break;
3839e657 1369 case 'l':
f3db3566
TT
1370 bad_blocks_filename = malloc(strlen(optarg)+1);
1371 if (!bad_blocks_filename) {
1372 com_err(program_name, ENOMEM,
d9c56d3c 1373 _("in malloc for bad_blocks_filename"));
f3db3566
TT
1374 exit(1);
1375 }
1376 strcpy(bad_blocks_filename, optarg);
3839e657
TT
1377 break;
1378 case 'm':
ce911145 1379 reserved_ratio = strtod(optarg, &tmp);
8d822455
TT
1380 if ( *tmp || reserved_ratio > 50 ||
1381 reserved_ratio < 0) {
3839e657 1382 com_err(program_name, 0,
bb145b01 1383 _("invalid reserved blocks percent - %s"),
3839e657
TT
1384 optarg);
1385 exit(1);
1386 }
1387 break;
50787ea2
TT
1388 case 'n':
1389 noaction++;
1390 break;
1e3472c5
TT
1391 case 'o':
1392 creator_os = optarg;
1393 break;
2524785d
AD
1394 case 'q':
1395 quiet = 1;
1396 break;
7f88b043 1397 case 'r':
9dc6ad1e 1398 r_opt = strtoul(optarg, &tmp, 0);
2524785d
AD
1399 if (*tmp) {
1400 com_err(program_name, 0,
1401 _("bad revision level - %s"), optarg);
1402 exit(1);
1403 }
9dc6ad1e 1404 fs_param.s_rev_level = r_opt;
7f88b043 1405 break;
b10fd5e8 1406 case 's': /* deprecated */
9dc6ad1e 1407 s_opt = atoi(optarg);
521e3685 1408 break;
7f88b043 1409 case 'I':
932a489c
AD
1410 inode_size = strtoul(optarg, &tmp, 0);
1411 if (*tmp) {
1412 com_err(program_name, 0,
bb145b01 1413 _("invalid inode size - %s"), optarg);
932a489c
AD
1414 exit(1);
1415 }
7f88b043 1416 break;
3839e657
TT
1417 case 'v':
1418 verbose = 1;
1419 break;
74becf3c 1420 case 'F':
c16e610c 1421 force++;
74becf3c 1422 break;
1e3472c5
TT
1423 case 'L':
1424 volume_label = optarg;
1425 break;
1426 case 'M':
1427 mount_dir = optarg;
1428 break;
2524785d
AD
1429 case 'N':
1430 num_inodes = strtoul(optarg, &tmp, 0);
1431 if (*tmp) {
1432 com_err(program_name, 0,
1433 _("bad num inodes - %s"), optarg);
1434 exit(1);
1435 }
1436 break;
896938d5 1437 case 'O':
9dc6ad1e 1438 fs_features = optarg;
896938d5 1439 break;
c6a44136 1440 case 'E':
a29f4d30 1441 case 'R':
c6a44136 1442 extended_opts = optarg;
a29f4d30 1443 break;
f3db3566
TT
1444 case 'S':
1445 super_only = 1;
1446 break;
3d43836f 1447 case 't':
9f7c3afa
ES
1448 if (fs_type) {
1449 com_err(program_name, 0,
1450 _("The -t option may only be used once"));
1451 exit(1);
1452 }
4c2b28ab 1453 fs_type = strdup(optarg);
50787ea2 1454 break;
3d43836f 1455 case 'T':
9f7c3afa
ES
1456 if (usage_types) {
1457 com_err(program_name, 0,
1458 _("The -T option may only be used once"));
1459 exit(1);
1460 }
4c2b28ab 1461 usage_types = strdup(optarg);
3d43836f 1462 break;
b0afdda1
TT
1463 case 'U':
1464 fs_uuid = optarg;
1465 break;
818180cd
TT
1466 case 'V':
1467 /* Print version number and exit */
1e6e4c5e
TT
1468 show_version_only++;
1469 break;
3839e657
TT
1470 default:
1471 usage();
1472 }
3839e657 1473 }
55f4cbd9 1474 if ((optind == argc) && !show_version_only)
3839e657 1475 usage();
55f4cbd9 1476 device_name = argv[optind++];
a418d3ad 1477
1e6e4c5e 1478 if (!quiet || show_version_only)
efc6f628 1479 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
3879857e
TT
1480 E2FSPROGS_DATE);
1481
1e6e4c5e 1482 if (show_version_only) {
efc6f628 1483 fprintf(stderr, _("\tUsing %s\n"),
1e6e4c5e
TT
1484 error_message(EXT2_ET_BASE));
1485 exit(0);
1486 }
1487
77dc4eb0
TT
1488 /*
1489 * If there's no blocksize specified and there is a journal
1490 * device, use it to figure out the blocksize
1491 */
c5290fae 1492 if (blocksize <= 0 && journal_device) {
77dc4eb0 1493 ext2_filsys jfs;
a7ccdff8 1494 io_manager io_ptr;
77dc4eb0 1495
a7ccdff8 1496#ifdef CONFIG_TESTIO_DEBUG
f38cf3cb
TT
1497 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1498 io_ptr = test_io_manager;
1499 test_io_backing_manager = unix_io_manager;
1500 } else
a7ccdff8 1501#endif
f38cf3cb 1502 io_ptr = unix_io_manager;
77dc4eb0
TT
1503 retval = ext2fs_open(journal_device,
1504 EXT2_FLAG_JOURNAL_DEV_OK, 0,
a7ccdff8 1505 0, io_ptr, &jfs);
77dc4eb0
TT
1506 if (retval) {
1507 com_err(program_name, retval,
1508 _("while trying to open journal device %s\n"),
1509 journal_device);
1510 exit(1);
1511 }
54434927 1512 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
c5290fae 1513 com_err(program_name, 0,
ddc32a04 1514 _("Journal dev blocksize (%d) smaller than "
c5290fae
TT
1515 "minimum blocksize %d\n"), jfs->blocksize,
1516 -blocksize);
1517 exit(1);
1518 }
77dc4eb0 1519 blocksize = jfs->blocksize;
f8df04bc 1520 printf(_("Using journal device's blocksize: %d\n"), blocksize);
9b9a780f 1521 fs_param.s_log_block_size =
77dc4eb0
TT
1522 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1523 ext2fs_close(jfs);
1524 }
dc2ec525 1525
55f4cbd9 1526 if (optind < argc) {
493024ea
TT
1527 fs_blocks_count = parse_num_blocks2(argv[optind++],
1528 fs_param.s_log_block_size);
1529 if (!fs_blocks_count) {
792cce30 1530 com_err(program_name, 0,
9d92a201 1531 _("invalid blocks '%s' on device '%s'"),
792cce30 1532 argv[optind - 1], device_name);
55f4cbd9
TT
1533 exit(1);
1534 }
1535 }
1536 if (optind < argc)
1537 usage();
1538
74becf3c 1539 if (!force)
8ddaa66b 1540 check_plausibility(device_name);
63985320 1541 check_mount(device_name, force, _("filesystem"));
a418d3ad 1542
9b27e9cc 1543 /* Determine the size of the device (if possible) */
493024ea
TT
1544 if (noaction && fs_blocks_count) {
1545 dev_size = fs_blocks_count;
50787ea2 1546 retval = 0;
493024ea 1547 } else
02d6f47e
JS
1548 retval = ext2fs_get_device_size2(device_name,
1549 EXT2_BLOCK_SIZE(&fs_param),
1550 &dev_size);
efc6f628 1551
a789d840
TT
1552 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1553 com_err(program_name, retval,
d9c56d3c 1554 _("while trying to determine filesystem size"));
a789d840
TT
1555 exit(1);
1556 }
493024ea 1557 if (!fs_blocks_count) {
a789d840
TT
1558 if (retval == EXT2_ET_UNIMPLEMENTED) {
1559 com_err(program_name, 0,
d9c56d3c 1560 _("Couldn't determine device size; you "
a789d840 1561 "must specify\nthe size of the "
d9c56d3c 1562 "filesystem\n"));
a418d3ad 1563 exit(1);
26ab5315
TT
1564 } else {
1565 if (dev_size == 0) {
1566 com_err(program_name, 0,
1567 _("Device size reported to be zero. "
1568 "Invalid partition specified, or\n\t"
1569 "partition table wasn't reread "
1570 "after running fdisk, due to\n\t"
1571 "a modified partition being busy "
1572 "and in use. You may need to reboot\n\t"
1573 "to re-read your partition table.\n"
1574 ));
1575 exit(1);
1576 }
493024ea
TT
1577 fs_blocks_count = dev_size;
1578 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1579 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
4efbac6f 1580 EXT2_BLOCK_SIZE(&fs_param))-1));
4efbac6f 1581 }
493024ea 1582 } else if (!force && (fs_blocks_count > dev_size)) {
a789d840 1583 com_err(program_name, 0,
c5290fae 1584 _("Filesystem larger than apparent device size."));
a789d840 1585 proceed_question();
a418d3ad 1586 }
3839e657 1587
4c2b28ab
TT
1588 if (!fs_type)
1589 profile_get_string(profile, "devices", device_name,
1590 "fs_type", 0, &fs_type);
1591 if (!usage_types)
1592 profile_get_string(profile, "devices", device_name,
1593 "usage_types", 0, &usage_types);
1594
9b27e9cc
TT
1595 /*
1596 * We have the file system (or device) size, so we can now
1597 * determine the appropriate file system types so the fs can
1598 * be appropriately configured.
1599 */
1600 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1601 fs_blocks_count ? fs_blocks_count : dev_size,
1602 argv[0]);
1603 if (!fs_types) {
1604 fprintf(stderr, _("Failed to parse fs types list\n"));
1605 exit(1);
1606 }
1607
1608 /* Figure out what features should be enabled */
1609
1610 tmp = NULL;
1611 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1612 tmp = get_string_from_profile(fs_types, "base_features",
1613 "sparse_super,filetype,resize_inode,dir_index");
1614 edit_feature(tmp, &fs_param.s_feature_compat);
1615 free(tmp);
1616
6a426c97
ES
1617 /* And which mount options as well */
1618 tmp = get_string_from_profile(fs_types, "default_mntopts",
1619 "acl,user_xattr");
1620 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1621 if (tmp)
1622 free(tmp);
1623
9b27e9cc
TT
1624 for (cpp = fs_types; *cpp; cpp++) {
1625 tmp = NULL;
1626 profile_get_string(profile, "fs_types", *cpp,
1627 "features", "", &tmp);
1628 if (tmp && *tmp)
1629 edit_feature(tmp, &fs_param.s_feature_compat);
1630 if (tmp)
1631 free(tmp);
1632 }
1633 tmp = get_string_from_profile(fs_types, "default_features",
1634 "");
1635 }
1636 edit_feature(fs_features ? fs_features : tmp,
1637 &fs_param.s_feature_compat);
1638 if (tmp)
1639 free(tmp);
1640
493024ea
TT
1641 /*
1642 * We now need to do a sanity check of fs_blocks_count for
1643 * 32-bit vs 64-bit block number support.
1644 */
1645 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) {
1646 fs_blocks_count /= 4; /* Try using a 4k blocksize */
1647 blocksize = 4096;
1648 fs_param.s_log_block_size = 2;
1649 }
1650 if ((fs_blocks_count > MAX_32_NUM) &&
1651 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1652 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1653 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1654 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1655 }
1656 if ((fs_blocks_count > MAX_32_NUM) &&
1657 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1658 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1659 "too big to be expressed\n\t"
1660 "in 32 bits using a blocksize of %d.\n"),
1661 program_name, fs_blocks_count, device_name,
1662 EXT2_BLOCK_SIZE(&fs_param));
1663 exit(1);
1664 }
1665
1666 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1667
b4d5105b
TT
1668 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1669 fs_types[0] = strdup("journal");
1670 fs_types[1] = 0;
1671 }
1672
1673 if (verbose) {
1674 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1675 print_str_list(fs_types);
1676 }
1677
efc6f628 1678 if (r_opt == EXT2_GOOD_OLD_REV &&
b290d2dc 1679 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
92fb8545 1680 fs_param.s_feature_ro_compat)) {
9dc6ad1e
TT
1681 fprintf(stderr, _("Filesystem features not supported "
1682 "with revision 0 filesystems\n"));
1683 exit(1);
1684 }
1685
b290d2dc
TT
1686 if (s_opt > 0) {
1687 if (r_opt == EXT2_GOOD_OLD_REV) {
1688 fprintf(stderr, _("Sparse superblocks not supported "
1689 "with revision 0 filesystems\n"));
1690 exit(1);
1691 }
9dc6ad1e
TT
1692 fs_param.s_feature_ro_compat |=
1693 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
b290d2dc 1694 } else if (s_opt == 0)
9dc6ad1e
TT
1695 fs_param.s_feature_ro_compat &=
1696 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1697
b290d2dc
TT
1698 if (journal_size != 0) {
1699 if (r_opt == EXT2_GOOD_OLD_REV) {
1700 fprintf(stderr, _("Journals not supported "
1701 "with revision 0 filesystems\n"));
1702 exit(1);
1703 }
efc6f628 1704 fs_param.s_feature_compat |=
9dc6ad1e 1705 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
b290d2dc 1706 }
9dc6ad1e 1707
d3859af3
AK
1708 /* Get reserved_ratio from profile if not specified on cmd line. */
1709 if (reserved_ratio < 0.0) {
1710 reserved_ratio = get_double_from_profile(
1711 fs_types, "reserved_ratio", 5.0);
1712 if (reserved_ratio > 50 || reserved_ratio < 0) {
1713 com_err(program_name, 0,
1714 _("invalid reserved blocks percent - %lf"),
1715 reserved_ratio);
1716 exit(1);
1717 }
1718 }
1719
efc6f628 1720 if (fs_param.s_feature_incompat &
9dc6ad1e 1721 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
9dc6ad1e
TT
1722 reserved_ratio = 0;
1723 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1724 fs_param.s_feature_compat = 0;
1725 fs_param.s_feature_ro_compat = 0;
1726 }
d94cc2ea
TT
1727
1728 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1729 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1730 fprintf(stderr, _("The resize_inode and meta_bg features "
1731 "are not compatible.\n"
1732 "They can not be both enabled "
1733 "simultaneously.\n"));
1734 exit(1);
1735 }
1736
c046ac7f
TT
1737 /* Set first meta blockgroup via an environment variable */
1738 /* (this is mostly for debugging purposes) */
9b9a780f 1739 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
c046ac7f 1740 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
9b9a780f 1741 fs_param.s_first_meta_bg = atoi(tmp);
c046ac7f 1742
bb1158b9
TT
1743 /* Get the hardware sector sizes, if available */
1744 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
93d5c387
TT
1745 if (retval) {
1746 com_err(program_name, retval,
1747 _("while trying to determine hardware sector size"));
1748 exit(1);
1749 }
bb1158b9
TT
1750 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1751 if (retval) {
1752 com_err(program_name, retval,
1753 _("while trying to determine physical sector size"));
1754 exit(1);
1755 }
1cca86f5 1756
54434927 1757 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
92eb5a33
TT
1758 lsector_size = atoi(tmp);
1759 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
bb1158b9 1760 psector_size = atoi(tmp);
efc6f628 1761
92eb5a33
TT
1762 /* Older kernels may not have physical/logical distinction */
1763 if (!psector_size)
1764 psector_size = lsector_size;
1765
9dc6ad1e 1766 if (blocksize <= 0) {
3d43836f 1767 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
9dc6ad1e
TT
1768
1769 if (use_bsize == -1) {
1770 use_bsize = sys_page_size;
1771 if ((linux_version_code < (2*65536 + 6*256)) &&
1772 (use_bsize > 4096))
1773 use_bsize = 4096;
1774 }
2b21a0d9
TT
1775 if (lsector_size && use_bsize < lsector_size)
1776 use_bsize = lsector_size;
9dc6ad1e
TT
1777 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1778 use_bsize = -blocksize;
1779 blocksize = use_bsize;
4efbac6f
VAH
1780 ext2fs_blocks_count_set(&fs_param,
1781 ext2fs_blocks_count(&fs_param) /
1782 (blocksize / 1024));
bb1158b9 1783 } else {
f89f54af 1784 if (blocksize < lsector_size) { /* Impossible */
bb1158b9
TT
1785 com_err(program_name, EINVAL,
1786 _("while setting blocksize; too small "
1787 "for device\n"));
1788 exit(1);
f89f54af
TT
1789 } else if ((blocksize < psector_size) &&
1790 (psector_size <= sys_page_size)) { /* Suboptimal */
bb1158b9 1791 fprintf(stderr, _("Warning: specified blocksize %d is "
f89f54af
TT
1792 "less than device physical sectorsize %d\n"),
1793 blocksize, psector_size);
bb1158b9 1794 }
9dc6ad1e
TT
1795 }
1796
c6ed60cd
TT
1797 fs_param.s_log_block_size =
1798 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1799 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2d34a25f
TT
1800 if (!cluster_size)
1801 cluster_size = get_int_from_profile(fs_types,
1802 "cluster_size",
b12a0bc3 1803 blocksize*16);
2d34a25f
TT
1804 fs_param.s_log_cluster_size =
1805 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
c6ed60cd
TT
1806 } else
1807 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1808
9dc6ad1e 1809 if (inode_ratio == 0) {
3d43836f
TT
1810 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1811 8192);
9dc6ad1e
TT
1812 if (inode_ratio < blocksize)
1813 inode_ratio = blocksize;
c6ed60cd
TT
1814 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1815 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
9dc6ad1e
TT
1816 }
1817
9ed8e5fe 1818#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1599b470 1819 retval = get_device_geometry(device_name, &fs_param, psector_size);
9ed8e5fe
ES
1820 if (retval < 0) {
1821 fprintf(stderr,
13b0b123 1822 _("warning: Unable to get device geometry for %s\n"),
9ed8e5fe
ES
1823 device_name);
1824 } else if (retval) {
1825 printf(_("%s alignment is offset by %lu bytes.\n"),
1826 device_name, retval);
1827 printf(_("This may result in very poor performance, "
1828 "(re)-partitioning suggested.\n"));
9ed8e5fe
ES
1829 }
1830#endif
1831
9b9a780f 1832 blocksize = EXT2_BLOCK_SIZE(&fs_param);
a4396e9d 1833
45792c12
YZ
1834 /* This check should happen beyond the last assignment to blocksize */
1835 if (blocksize > sys_page_size) {
1836 if (!force) {
1837 com_err(program_name, 0,
1838 _("%d-byte blocks too big for system (max %d)"),
1839 blocksize, sys_page_size);
1840 proceed_question();
1841 }
1842 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1843 "(max %d), forced to continue\n"),
1844 blocksize, sys_page_size);
1845 }
1846
210fd2c7
TT
1847 lazy_itable_init = 0;
1848 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1849 lazy_itable_init = 1;
1850
efc6f628 1851 lazy_itable_init = get_bool_from_profile(fs_types,
210fd2c7
TT
1852 "lazy_itable_init",
1853 lazy_itable_init);
7fe5ff3c 1854 discard = get_bool_from_profile(fs_types, "discard" , discard);
6c54689f
AD
1855 journal_flags |= get_bool_from_profile(fs_types,
1856 "lazy_journal_init", 0) ?
1857 EXT2_MKJOURNAL_LAZYINIT : 0;
efc6f628 1858
2d36358d
TT
1859 /* Get options from profile */
1860 for (cpp = fs_types; *cpp; cpp++) {
1861 tmp = NULL;
1862 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1863 if (tmp && *tmp)
1864 parse_extended_opts(&fs_param, tmp);
45e338f5 1865 free(tmp);
2d36358d
TT
1866 }
1867
c6a44136 1868 if (extended_opts)
9b9a780f 1869 parse_extended_opts(&fs_param, extended_opts);
d323f8fb
TT
1870
1871 /* Since sparse_super is the default, we would only have a problem
1872 * here if it was explicitly disabled.
1873 */
9b9a780f
TT
1874 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1875 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
d323f8fb
TT
1876 com_err(program_name, 0,
1877 _("reserved online resize blocks not supported "
1878 "on non-sparse filesystem"));
1879 exit(1);
1880 }
1881
9b9a780f
TT
1882 if (fs_param.s_blocks_per_group) {
1883 if (fs_param.s_blocks_per_group < 256 ||
1884 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
521e3685 1885 com_err(program_name, 0,
d9c56d3c 1886 _("blocks per group count out of range"));
521e3685
TT
1887 exit(1);
1888 }
1889 }
1890
3d43836f
TT
1891 if (inode_size == 0)
1892 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
9ba40002
TT
1893 if (!flex_bg_size && (fs_param.s_feature_incompat &
1894 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1895 flex_bg_size = get_int_from_profile(fs_types,
1896 "flex_bg_size", 16);
1897 if (flex_bg_size) {
1898 if (!(fs_param.s_feature_incompat &
1899 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1900 com_err(program_name, 0,
1901 _("Flex_bg feature not enabled, so "
1902 "flex_bg size may not be specified"));
1903 exit(1);
1904 }
1905 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1906 }
067911ae
AD
1907
1908 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
932a489c 1909 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
9b9a780f 1910 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
932a489c
AD
1911 inode_size & (inode_size - 1)) {
1912 com_err(program_name, 0,
bb145b01 1913 _("invalid inode size %d (min %d/max %d)"),
932a489c 1914 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
c5290fae 1915 blocksize);
932a489c
AD
1916 exit(1);
1917 }
9b9a780f 1918 fs_param.s_inode_size = inode_size;
932a489c
AD
1919 }
1920
f3358643
ES
1921 /* Make sure number of inodes specified will fit in 32 bits */
1922 if (num_inodes == 0) {
de8f3a76 1923 unsigned long long n;
4efbac6f 1924 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
493024ea 1925 if (n > MAX_32_NUM) {
02d6f47e
JS
1926 if (fs_param.s_feature_incompat &
1927 EXT4_FEATURE_INCOMPAT_64BIT)
493024ea 1928 num_inodes = MAX_32_NUM;
02d6f47e
JS
1929 else {
1930 com_err(program_name, 0,
1931 _("too many inodes (%llu), raise"
1932 "inode ratio?"), n);
1933 exit(1);
1934 }
f3358643 1935 }
493024ea 1936 } else if (num_inodes > MAX_32_NUM) {
f3358643
ES
1937 com_err(program_name, 0,
1938 _("too many inodes (%llu), specify < 2^32 inodes"),
de8f3a76 1939 num_inodes);
f3358643
ES
1940 exit(1);
1941 }
3839e657
TT
1942 /*
1943 * Calculate number of inodes based on the inode ratio
1944 */
efc6f628 1945 fs_param.s_inodes_count = num_inodes ? num_inodes :
4efbac6f 1946 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
3839e657 1947
dcf7b091
AD
1948 if ((((long long)fs_param.s_inodes_count) *
1949 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
4efbac6f 1950 ((ext2fs_blocks_count(&fs_param)) *
dcf7b091
AD
1951 EXT2_BLOCK_SIZE(&fs_param))) {
1952 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1953 "(%u) too big for a\n\t"
4efbac6f 1954 "filesystem with %llu blocks, "
dcf7b091
AD
1955 "specify higher inode_ratio (-i)\n\t"
1956 "or lower inode count (-N).\n"),
1957 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
efc6f628 1958 fs_param.s_inodes_count,
4efbac6f 1959 (unsigned long long) ext2fs_blocks_count(&fs_param));
dcf7b091
AD
1960 exit(1);
1961 }
1962
3839e657
TT
1963 /*
1964 * Calculate number of blocks to reserve
1965 */
4efbac6f
VAH
1966 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
1967 ext2fs_blocks_count(&fs_param) / 100.0);
4c2b28ab
TT
1968
1969 free(fs_type);
1970 free(usage_types);
3839e657 1971}
d323f8fb 1972
b626b39a
AK
1973static int should_do_undo(const char *name)
1974{
1975 errcode_t retval;
1976 io_channel channel;
1977 __u16 s_magic;
1978 struct ext2_super_block super;
1979 io_manager manager = unix_io_manager;
1980 int csum_flag, force_undo;
1981
1982 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1983 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1984 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1985 if (!force_undo && (!csum_flag || !lazy_itable_init))
1986 return 0;
1987
1988 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1989 if (retval) {
1990 /*
1991 * We don't handle error cases instead we
1992 * declare that the file system doesn't exist
1993 * and let the rest of mke2fs take care of
1994 * error
1995 */
1996 retval = 0;
1997 goto open_err_out;
1998 }
1999
2000 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
24a117ab 2001 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
b626b39a
AK
2002 if (retval) {
2003 retval = 0;
2004 goto err_out;
2005 }
2006
2007#if defined(WORDS_BIGENDIAN)
2008 s_magic = ext2fs_swab16(super.s_magic);
2009#else
2010 s_magic = super.s_magic;
2011#endif
2012
2013 if (s_magic == EXT2_SUPER_MAGIC)
2014 retval = 1;
2015
2016err_out:
2017 io_channel_close(channel);
2018
2019open_err_out:
2020
2021 return retval;
2022}
2023
2024static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2025{
8a1cf3c2 2026 errcode_t retval = ENOMEM;
30295f16 2027 char *tdb_dir = NULL, *tdb_file = NULL;
b626b39a 2028 char *device_name, *tmp_name;
30295f16 2029 int free_tdb_dir = 0;
b626b39a
AK
2030
2031 /*
2032 * Configuration via a conf file would be
2033 * nice
2034 */
2035 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
30295f16 2036 if (!tdb_dir) {
b626b39a
AK
2037 profile_get_string(profile, "defaults",
2038 "undo_dir", 0, "/var/lib/e2fsprogs",
2039 &tdb_dir);
30295f16
ES
2040 free_tdb_dir = 1;
2041 }
b626b39a
AK
2042
2043 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2044 access(tdb_dir, W_OK))
2045 return 0;
2046
2047 tmp_name = strdup(name);
8a1cf3c2
TT
2048 if (!tmp_name)
2049 goto errout;
b626b39a 2050 device_name = basename(tmp_name);
f203bbdb 2051 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
8a1cf3c2
TT
2052 if (!tdb_file) {
2053 free(tmp_name);
2054 goto errout;
2055 }
b626b39a 2056 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
8a1cf3c2 2057 free(tmp_name);
b626b39a
AK
2058
2059 if (!access(tdb_file, F_OK)) {
2060 if (unlink(tdb_file) < 0) {
2061 retval = errno;
8a1cf3c2 2062 goto errout;
b626b39a
AK
2063 }
2064 }
2065
2066 set_undo_io_backing_manager(*io_ptr);
2067 *io_ptr = undo_io_manager;
8a1cf3c2
TT
2068 retval = set_undo_io_backup_file(tdb_file);
2069 if (retval)
2070 goto errout;
b626b39a
AK
2071 printf(_("Overwriting existing filesystem; this can be undone "
2072 "using the command:\n"
2073 " e2undo %s %s\n\n"), tdb_file, name);
79e62409 2074
30295f16
ES
2075 if (free_tdb_dir)
2076 free(tdb_dir);
f203bbdb 2077 free(tdb_file);
8a1cf3c2
TT
2078 return 0;
2079
2080errout:
30295f16
ES
2081 if (free_tdb_dir)
2082 free(tdb_dir);
8a1cf3c2
TT
2083 free(tdb_file);
2084 com_err(program_name, retval,
2085 _("while trying to setup undo file\n"));
b626b39a
AK
2086 return retval;
2087}
2088
7d9e3165
LC
2089static int mke2fs_discard_device(ext2_filsys fs)
2090{
2091 struct ext2fs_numeric_progress_struct progress;
2092 blk64_t blocks = ext2fs_blocks_count(fs->super);
2093 blk64_t count = DISCARD_STEP_MB;
d2bfdc7f 2094 blk64_t cur;
7d9e3165
LC
2095 int retval = 0;
2096
d2bfdc7f
LC
2097 /*
2098 * Let's try if discard really works on the device, so
2099 * we do not print numeric progress resulting in failure
2100 * afterwards.
2101 */
2102 retval = io_channel_discard(fs->io, 0, fs->blocksize);
aa07cb79
TT
2103 if (retval)
2104 return retval;
d2bfdc7f 2105 cur = fs->blocksize;
aa07cb79 2106
7d9e3165
LC
2107 count *= (1024 * 1024);
2108 count /= fs->blocksize;
2109
2110 ext2fs_numeric_progress_init(fs, &progress,
2111 _("Discarding device blocks: "),
2112 blocks);
2113 while (cur < blocks) {
2114 ext2fs_numeric_progress_update(fs, &progress, cur);
2115
2116 if (cur + count > blocks)
2117 count = blocks - cur;
2118
aa07cb79 2119 retval = io_channel_discard(fs->io, cur, count);
7d9e3165
LC
2120 if (retval)
2121 break;
2122 cur += count;
2123 }
2124
2125 if (retval) {
2126 ext2fs_numeric_progress_close(fs, &progress,
2127 _("failed - "));
2128 if (!quiet)
2129 printf("%s\n",error_message(retval));
2130 } else
2131 ext2fs_numeric_progress_close(fs, &progress,
2132 _("done \n"));
2133
2134 return retval;
2135}
2136
96367ad3 2137static void fix_cluster_bg_counts(ext2_filsys fs)
c6ed60cd
TT
2138{
2139 blk64_t cluster, num_clusters, tot_free;
2140 int grp_free, num_free, group, num;
2141
2142 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2143 tot_free = num_free = num = group = grp_free = 0;
2144 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2145 cluster < num_clusters; cluster++) {
2146 if (!ext2fs_test_block_bitmap2(fs->block_map,
2147 EXT2FS_C2B(fs, cluster))) {
2148 grp_free++;
2149 tot_free++;
2150 }
2151 num++;
2152 if ((num == fs->super->s_clusters_per_group) ||
2153 (cluster == num_clusters-1)) {
2154 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2155 ext2fs_group_desc_csum_set(fs, group);
2156 num = 0;
2157 grp_free = 0;
2158 group++;
2159 }
2160 }
fe75afbf 2161 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
c6ed60cd
TT
2162}
2163
1f5d7a89
AK
2164static int create_quota_inodes(ext2_filsys fs)
2165{
2166 quota_ctx_t qctx;
2167
2168 init_quota_context(&qctx, fs, -1);
2169 compute_quota(qctx, -1);
2170 write_quota_inode(qctx, USRQUOTA);
2171 write_quota_inode(qctx, GRPQUOTA);
2172 release_quota_context(&qctx);
2173
2174 return;
2175}
2176
3839e657
TT
2177int main (int argc, char *argv[])
2178{
2179 errcode_t retval = 0;
2180 ext2_filsys fs;
2181 badblocks_list bb_list = 0;
d4e0b1c6 2182 unsigned int journal_blocks;
faa2dcda 2183 unsigned int i, max_mnt_count, checkinterval;
d5f57d95 2184 int val, hash_alg;
463e7327
VAH
2185 int flags;
2186 int old_bitmaps;
a7ccdff8 2187 io_manager io_ptr;
b626b39a 2188 char tdb_string[40];
d5f57d95 2189 char *hash_alg_str;
6fcd6f84 2190 int itable_zeroed = 0;
d9c56d3c
TT
2191
2192#ifdef ENABLE_NLS
2193 setlocale(LC_MESSAGES, "");
14308a53 2194 setlocale(LC_CTYPE, "");
d9c56d3c
TT
2195 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2196 textdomain(NLS_CAT_NAME);
2197#endif
3839e657
TT
2198 PRS(argc, argv);
2199
a7ccdff8 2200#ifdef CONFIG_TESTIO_DEBUG
f38cf3cb
TT
2201 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2202 io_ptr = test_io_manager;
2203 test_io_backing_manager = unix_io_manager;
2204 } else
a7ccdff8 2205#endif
f38cf3cb 2206 io_ptr = unix_io_manager;
a7ccdff8 2207
b626b39a
AK
2208 if (should_do_undo(device_name)) {
2209 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2210 if (retval)
2211 exit(1);
2212 }
2213
3839e657
TT
2214 /*
2215 * Initialize the superblock....
2216 */
463e7327
VAH
2217 flags = EXT2_FLAG_EXCLUSIVE;
2218 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2219 &old_bitmaps);
2220 if (!old_bitmaps)
2221 flags |= EXT2_FLAG_64BITS;
95fd65bb
VAH
2222 /*
2223 * By default, we print how many inode tables or block groups
2224 * or whatever we've written so far. The quiet flag disables
2225 * this, along with a lot of other output.
2226 */
2227 if (!quiet)
2228 flags |= EXT2_FLAG_PRINT_PROGRESS;
463e7327 2229 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
3839e657 2230 if (retval) {
d9c56d3c 2231 com_err(device_name, retval, _("while setting up superblock"));
3839e657
TT
2232 exit(1);
2233 }
95fd65bb 2234
5827d241 2235 /* Can't undo discard ... */
8185ab9f 2236 if (!noaction && discard && (io_ptr != undo_io_manager)) {
7d9e3165 2237 retval = mke2fs_discard_device(fs);
d866599a 2238 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
6fcd6f84
ES
2239 if (verbose)
2240 printf(_("Discard succeeded and will return 0s "
2241 " - skipping inode table wipe\n"));
2242 lazy_itable_init = 1;
2243 itable_zeroed = 1;
2244 }
2245 }
5827d241 2246
b626b39a
AK
2247 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2248 32768 : fs->blocksize * 8);
2249 io_channel_set_options(fs->io, tdb_string);
3839e657 2250
6cb27404
TT
2251 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2252 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2253
b7c5b403
TT
2254 if ((fs_param.s_feature_incompat &
2255 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2256 (fs_param.s_feature_ro_compat &
2257 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2258 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2259 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2260 fs->super->s_kbytes_written = 1;
2261
e41784eb
TT
2262 /*
2263 * Wipe out the old on-disk superblock
2264 */
04a96857
TT
2265 if (!noaction)
2266 zap_sector(fs, 2, 6);
e41784eb 2267
1e3472c5 2268 /*
b0afdda1 2269 * Parse or generate a UUID for the filesystem
1e3472c5 2270 */
b0afdda1
TT
2271 if (fs_uuid) {
2272 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2273 com_err(device_name, 0, "could not parse UUID: %s\n",
2274 fs_uuid);
2275 exit(1);
2276 }
2277 } else
2278 uuid_generate(fs->super->s_uuid);
1e3472c5 2279
843049c4
TT
2280 /*
2281 * Initialize the directory index variables
2282 */
d5f57d95
TT
2283 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2284 "half_md4");
2285 hash_alg = e2p_string2hash(hash_alg_str);
5a2db046 2286 free(hash_alg_str);
d5f57d95
TT
2287 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2288 EXT2_HASH_HALF_MD4;
843049c4
TT
2289 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2290
44c09c04 2291 /*
3daf5926
ES
2292 * Periodic checks can be enabled/disabled via config file.
2293 * Note we override the kernel include file's idea of what the default
2294 * check interval (never) should be. It's a good idea to check at
2295 * least *occasionally*, specially since servers will never rarely get
2296 * to reboot, since Linux is so robust these days. :-)
2297 *
2298 * 180 days (six months) seems like a good value.
44c09c04 2299 */
3daf5926
ES
2300#ifdef EXT2_DFL_CHECKINTERVAL
2301#undef EXT2_DFL_CHECKINTERVAL
2302#endif
2303#define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2304
2305 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2306 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2307 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2308 /*
2309 * Add "jitter" to the superblock's check interval so that we
2310 * don't check all the filesystems at the same time. We use a
2311 * kludgy hack of using the UUID to derive a random jitter value
2312 */
2313 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2314 val += fs->super->s_uuid[i];
2315 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2316 }
44c09c04 2317
1e3472c5
TT
2318 /*
2319 * Override the creator OS, if applicable
2320 */
2321 if (creator_os && !set_os(fs->super, creator_os)) {
d9c56d3c 2322 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1e3472c5
TT
2323 exit(1);
2324 }
2325
4ea0a110
TT
2326 /*
2327 * For the Hurd, we will turn off filetype since it doesn't
2328 * support it.
2329 */
2330 if (fs->super->s_creator_os == EXT2_OS_HURD)
2331 fs->super->s_feature_incompat &=
2332 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2333
1e3472c5
TT
2334 /*
2335 * Set the volume label...
2336 */
2337 if (volume_label) {
ef9abe5f
TT
2338 memset(fs->super->s_volume_name, 0,
2339 sizeof(fs->super->s_volume_name));
2340 strncpy(fs->super->s_volume_name, volume_label,
2341 sizeof(fs->super->s_volume_name));
1e3472c5
TT
2342 }
2343
2344 /*
2345 * Set the last mount directory
2346 */
2347 if (mount_dir) {
ef9abe5f
TT
2348 memset(fs->super->s_last_mounted, 0,
2349 sizeof(fs->super->s_last_mounted));
2350 strncpy(fs->super->s_last_mounted, mount_dir,
2351 sizeof(fs->super->s_last_mounted));
1e3472c5 2352 }
efc6f628 2353
50787ea2 2354 if (!quiet || noaction)
3839e657
TT
2355 show_stats(fs);
2356
50787ea2
TT
2357 if (noaction)
2358 exit(0);
2359
16ed5b3a
TT
2360 if (fs->super->s_feature_incompat &
2361 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2362 create_journal_dev(fs);
568101f7 2363 exit(ext2fs_close(fs) ? 1 : 0);
16ed5b3a
TT
2364 }
2365
3839e657
TT
2366 if (bad_blocks_filename)
2367 read_bb_file(fs, &bb_list, bad_blocks_filename);
2368 if (cflag)
2369 test_disk(fs, &bb_list);
2370
2371 handle_bad_blocks(fs, bb_list);
0c17cb25 2372 fs->stride = fs_stride = fs->super->s_raid_stride;
95fd65bb
VAH
2373 if (!quiet)
2374 printf(_("Allocating group tables: "));
19c78dc0
TT
2375 retval = ext2fs_allocate_tables(fs);
2376 if (retval) {
2377 com_err(program_name, retval,
d9c56d3c 2378 _("while trying to allocate filesystem tables"));
19c78dc0
TT
2379 exit(1);
2380 }
95fd65bb
VAH
2381 if (!quiet)
2382 printf(_("done \n"));
c6ed60cd
TT
2383
2384 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2385 if (retval) {
2386 com_err(program_name, retval,
2387 _("\n\twhile converting subcluster bitmap"));
2388 exit(1);
2389 }
2390
f3db3566
TT
2391 if (super_only) {
2392 fs->super->s_state |= EXT2_ERROR_FS;
2393 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2394 } else {
59f27247 2395 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
02d6f47e
JS
2396 blk64_t rsv = 65536 / fs->blocksize;
2397 blk64_t blocks = ext2fs_blocks_count(fs->super);
2398 blk64_t start;
2399 blk64_t ret_blk;
1400bbb6
TT
2400
2401#ifdef ZAP_BOOTBLOCK
04a96857 2402 zap_sector(fs, 0, 2);
1400bbb6 2403#endif
59f27247 2404
1400bbb6
TT
2405 /*
2406 * Wipe out any old MD RAID (or other) metadata at the end
2407 * of the device. This will also verify that the device is
59f27247 2408 * as large as we think. Be careful with very small devices.
1400bbb6 2409 */
59f27247
AD
2410 start = (blocks & ~(rsv - 1));
2411 if (start > rsv)
2412 start -= rsv;
2413 if (start > 0)
02d6f47e 2414 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
b626b39a 2415 &ret_blk, NULL);
59f27247 2416
1400bbb6
TT
2417 if (retval) {
2418 com_err(program_name, retval,
02d6f47e 2419 _("while zeroing block %llu at end of filesystem"),
1400bbb6 2420 ret_blk);
1400bbb6 2421 }
6fcd6f84 2422 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
f3db3566
TT
2423 create_root_dir(fs);
2424 create_lost_and_found(fs);
2425 reserve_inodes(fs);
2426 create_bad_block_inode(fs, bb_list);
efc6f628 2427 if (fs->super->s_feature_compat &
ea774315
TT
2428 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2429 retval = ext2fs_create_resize_inode(fs);
2430 if (retval) {
2431 com_err("ext2fs_create_resize_inode", retval,
d323f8fb 2432 _("while reserving blocks for online resize"));
ea774315
TT
2433 exit(1);
2434 }
d323f8fb 2435 }
f3db3566 2436 }
8ddaa66b 2437
16ed5b3a
TT
2438 if (journal_device) {
2439 ext2_filsys jfs;
efc6f628 2440
8ddaa66b 2441 if (!force)
efc6f628 2442 check_plausibility(journal_device);
63985320 2443 check_mount(journal_device, force, _("journal"));
8ddaa66b 2444
16ed5b3a
TT
2445 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2446 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2447 fs->blocksize, unix_io_manager, &jfs);
2448 if (retval) {
2449 com_err(program_name, retval,
2450 _("while trying to open journal device %s\n"),
2451 journal_device);
2452 exit(1);
2453 }
93345d15 2454 if (!quiet) {
efc6f628 2455 printf(_("Adding journal to device %s: "),
8ddaa66b 2456 journal_device);
93345d15
TT
2457 fflush(stdout);
2458 }
16ed5b3a
TT
2459 retval = ext2fs_add_journal_device(fs, jfs);
2460 if(retval) {
efc6f628
TT
2461 com_err (program_name, retval,
2462 _("\n\twhile trying to add journal to device %s"),
8ddaa66b
TT
2463 journal_device);
2464 exit(1);
2465 }
2466 if (!quiet)
2467 printf(_("done\n"));
16ed5b3a 2468 ext2fs_close(jfs);
2d15576d 2469 free(journal_device);
9dc6ad1e 2470 } else if ((journal_size) ||
efc6f628 2471 (fs_param.s_feature_compat &
9dc6ad1e 2472 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2537b6d0 2473 journal_blocks = figure_journal_size(journal_size, fs);
93345d15 2474
a620badd
TT
2475 if (super_only) {
2476 printf(_("Skipping journal creation in super-only mode\n"));
2477 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2478 goto no_journal;
2479 }
2480
93345d15
TT
2481 if (!journal_blocks) {
2482 fs->super->s_feature_compat &=
2483 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2484 goto no_journal;
2485 }
2486 if (!quiet) {
d4e0b1c6 2487 printf(_("Creating journal (%u blocks): "),
16ad3334 2488 journal_blocks);
93345d15
TT
2489 fflush(stdout);
2490 }
63985320
TT
2491 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2492 journal_flags);
85ef4ae8
TT
2493 if (retval) {
2494 com_err (program_name, retval,
1d08d9bf 2495 _("\n\twhile trying to create journal"));
85ef4ae8
TT
2496 exit(1);
2497 }
2498 if (!quiet)
2499 printf(_("done\n"));
2500 }
93345d15
TT
2501no_journal:
2502
c6ed60cd
TT
2503 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2504 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2505 fix_cluster_bg_counts(fs);
1f5d7a89
AK
2506 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2507 EXT4_FEATURE_RO_COMPAT_QUOTA))
2508 create_quota_inodes(fs);
2509
3839e657 2510 if (!quiet)
d9c56d3c
TT
2511 printf(_("Writing superblocks and "
2512 "filesystem accounting information: "));
faa2dcda
LC
2513 checkinterval = fs->super->s_checkinterval;
2514 max_mnt_count = fs->super->s_max_mnt_count;
2515 retval = ext2fs_close(fs);
5d45d803 2516 if (retval) {
6693837e
TT
2517 fprintf(stderr,
2518 _("\nWarning, had trouble writing out superblocks."));
faa2dcda 2519 } else if (!quiet) {
2537b6d0 2520 printf(_("done\n\n"));
1cca86f5 2521 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
faa2dcda 2522 print_check_message(max_mnt_count, checkinterval);
93345d15 2523 }
faa2dcda 2524
a6d8302b
TT
2525 remove_error_table(&et_ext2_error_table);
2526 remove_error_table(&et_prof_error_table);
3d43836f 2527 profile_release(profile);
5a2db046
TT
2528 for (i=0; fs_types[i]; i++)
2529 free(fs_types[i]);
2530 free(fs_types);
faa2dcda 2531 return retval;
3839e657 2532}