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