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