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