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