]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/mke2fs.c
Fixed the journal handling so that an offer is made to clear the
[thirdparty/e2fsprogs.git] / misc / mke2fs.c
CommitLineData
3839e657
TT
1/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
19c78dc0
TT
4 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
3839e657
TT
10 */
11
12/* Usage: mke2fs [options] device
13 *
14 * The device may be a block device or a image of one, but this isn't
15 * enforced (but it's not much fun on a character device :-).
16 */
17
a418d3ad 18#include <stdio.h>
3839e657
TT
19#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
3839e657 22#include <time.h>
2740156b
TT
23#ifdef linux
24#include <sys/utsname.h>
25#endif
a418d3ad 26#ifdef HAVE_GETOPT_H
3839e657 27#include <getopt.h>
373b8337
TT
28#else
29extern char *optarg;
30extern int optind;
a418d3ad
TT
31#endif
32#ifdef HAVE_UNISTD_H
3839e657 33#include <unistd.h>
a418d3ad
TT
34#endif
35#ifdef HAVE_STDLIB_H
3839e657 36#include <stdlib.h>
a418d3ad
TT
37#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_MNTENT_H
3839e657 42#include <mntent.h>
a418d3ad 43#endif
3839e657 44#include <sys/ioctl.h>
f3db3566
TT
45#include <sys/types.h>
46
54c637d4 47#include "ext2fs/ext2_fs.h"
3839e657 48#include "et/com_err.h"
1e3472c5 49#include "uuid/uuid.h"
896938d5 50#include "e2p/e2p.h"
3839e657 51#include "ext2fs/ext2fs.h"
63985320 52#include "util.h"
3839e657 53#include "../version.h"
d9c56d3c 54#include "nls-enable.h"
3839e657
TT
55
56#define STRIDE_LENGTH 8
57
6733c2fd 58#ifndef __sparc__
1e3472c5
TT
59#define ZAP_BOOTBLOCK
60#endif
61
3839e657 62extern int isatty(int);
f3db3566 63extern FILE *fpopen(const char *cmd, const char *mode);
3839e657
TT
64
65const char * program_name = "mke2fs";
d48755e9 66const char * device_name /* = NULL */;
3839e657
TT
67
68/* Command line options */
16ed5b3a
TT
69int cflag;
70int verbose;
71int quiet;
72int super_only;
73int force;
74int noaction;
75int journal_size;
76int journal_flags;
77char *bad_blocks_filename;
78__u32 fs_stride;
3839e657
TT
79
80struct ext2_super_block param;
16ed5b3a
TT
81char *creator_os;
82char *volume_label;
83char *mount_dir;
84char *journal_device;
85int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
3839e657 86
63985320 87static void usage(void)
3839e657 88{
d9c56d3c 89 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
dc2ec525 90 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
5515e6b4
TT
91 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
92 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
18160d26 93 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
b10fd5e8 94 "[-r fs-revision] [-R raid_opts] [-qvSV] device [blocks-count]\n"),
3839e657
TT
95 program_name);
96 exit(1);
97}
98
6733c2fd 99static int int_log2(int arg)
3839e657
TT
100{
101 int l = 0;
102
103 arg >>= 1;
104 while (arg) {
105 l++;
106 arg >>= 1;
107 }
108 return l;
109}
110
6733c2fd 111static int int_log10(unsigned int arg)
1dde43f0
TT
112{
113 int l;
114
115 for (l=0; arg ; l++)
116 arg = arg / 10;
117 return l;
118}
119
50787ea2
TT
120/*
121 * This function sets the default parameters for a filesystem
122 *
2740156b
TT
123 * The type is specified by the user. The size is the maximum size
124 * (in megabytes) for which a set of parameters applies, with a size
125 * of zero meaning that it is the default parameter for the type.
126 * Note that order is important in the table below.
50787ea2
TT
127 */
128static char default_str[] = "default";
129struct mke2fs_defaults {
4a600566
TT
130 const char *type;
131 int size;
132 int blocksize;
133 int inode_ratio;
50787ea2
TT
134} settings[] = {
135 { default_str, 0, 4096, 8192 },
136 { default_str, 512, 1024, 4096 },
137 { default_str, 3, 1024, 8192 },
dc2ec525 138 { "journal", 0, 4096, 8192 },
50787ea2 139 { "news", 0, 4096, 4096 },
44c09c04
TT
140 { "largefile", 0, 4096, 1024 * 1024 },
141 { "largefile4", 0, 4096, 4096 * 1024 },
50787ea2
TT
142 { 0, 0, 0, 0},
143};
144
4ea7bd04
TT
145static void set_fs_defaults(const char *fs_type,
146 struct ext2_super_block *super,
50787ea2
TT
147 int blocksize, int *inode_ratio)
148{
149 int megs;
150 int ratio = 0;
151 struct mke2fs_defaults *p;
152
9094f284 153 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
50787ea2
TT
154 1024);
155 if (inode_ratio)
156 ratio = *inode_ratio;
157 if (!fs_type)
158 fs_type = default_str;
159 for (p = settings; p->type; p++) {
160 if ((strcmp(p->type, fs_type) != 0) &&
161 (strcmp(p->type, default_str) != 0))
162 continue;
163 if ((p->size != 0) &&
164 (megs > p->size))
165 continue;
166 if (ratio == 0)
167 *inode_ratio = p->inode_ratio;
168 if (blocksize == 0) {
9094f284 169 super->s_log_frag_size = super->s_log_block_size =
6733c2fd 170 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
50787ea2
TT
171 }
172 }
173 if (blocksize == 0)
9094f284 174 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
50787ea2
TT
175}
176
3839e657
TT
177/*
178 * Helper function for read_bb_file and test_disk
179 */
180static void invalid_block(ext2_filsys fs, blk_t blk)
181{
d9c56d3c 182 printf(_("Bad block %u out of range; ignored.\n"), blk);
3839e657
TT
183 return;
184}
185
186/*
187 * Reads the bad blocks list from a file
188 */
189static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
190 const char *bad_blocks_file)
191{
192 FILE *f;
193 errcode_t retval;
194
195 f = fopen(bad_blocks_file, "r");
196 if (!f) {
197 com_err("read_bad_blocks_file", errno,
d9c56d3c 198 _("while trying to open %s"), bad_blocks_file);
3839e657
TT
199 exit(1);
200 }
201 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
202 fclose (f);
203 if (retval) {
204 com_err("ext2fs_read_bb_FILE", retval,
d9c56d3c 205 _("while reading in list of bad blocks from file"));
3839e657
TT
206 exit(1);
207 }
208}
209
210/*
211 * Runs the badblocks program to test the disk
212 */
213static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
214{
215 FILE *f;
216 errcode_t retval;
217 char buf[1024];
218
3ed57c27
TT
219 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
220 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
221 fs->device_name, fs->super->s_blocks_count);
3839e657 222 if (verbose)
d9c56d3c 223 printf(_("Running command: %s\n"), buf);
3839e657
TT
224 f = popen(buf, "r");
225 if (!f) {
226 com_err("popen", errno,
d9c56d3c 227 _("while trying run '%s'"), buf);
3839e657
TT
228 exit(1);
229 }
230 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
f3db3566 231 pclose(f);
3839e657
TT
232 if (retval) {
233 com_err("ext2fs_read_bb_FILE", retval,
d9c56d3c 234 _("while processing list of bad blocks from program"));
3839e657
TT
235 exit(1);
236 }
237}
238
239static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
240{
f3db3566 241 int i, j;
3839e657
TT
242 int must_be_good;
243 blk_t blk;
244 badblocks_iterate bb_iter;
245 errcode_t retval;
f3db3566
TT
246 blk_t group_block;
247 int group;
248 int group_bad;
3839e657
TT
249
250 if (!bb_list)
251 return;
252
253 /*
254 * The primary superblock and group descriptors *must* be
255 * good; if not, abort.
256 */
257 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
258 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
cbbf031b 259 if (ext2fs_badblocks_list_test(bb_list, i)) {
d9c56d3c
TT
260 fprintf(stderr, _("Block %d in primary "
261 "superblock/group descriptor area bad.\n"), i);
262 fprintf(stderr, _("Blocks %d through %d must be good "
263 "in order to build a filesystem.\n"),
3839e657 264 fs->super->s_first_data_block, must_be_good);
d9c56d3c 265 fprintf(stderr, _("Aborting....\n"));
3839e657
TT
266 exit(1);
267 }
268 }
f3db3566
TT
269
270 /*
271 * See if any of the bad blocks are showing up in the backup
272 * superblocks and/or group descriptors. If so, issue a
273 * warning and adjust the block counts appropriately.
274 */
275 group_block = fs->super->s_first_data_block +
276 fs->super->s_blocks_per_group;
f3db3566
TT
277
278 for (i = 1; i < fs->group_desc_count; i++) {
92bcc595 279 group_bad = 0;
f3db3566 280 for (j=0; j < fs->desc_blocks+1; j++) {
cbbf031b
TT
281 if (ext2fs_badblocks_list_test(bb_list,
282 group_block + j)) {
f3db3566
TT
283 if (!group_bad)
284 fprintf(stderr,
d9c56d3c
TT
285_("Warning: the backup superblock/group descriptors at block %d contain\n"
286" bad blocks.\n\n"),
f3db3566
TT
287 group_block);
288 group_bad++;
289 group = ext2fs_group_of_blk(fs, group_block+j);
290 fs->group_desc[group].bg_free_blocks_count++;
291 fs->super->s_free_blocks_count++;
292 }
293 }
294 group_block += fs->super->s_blocks_per_group;
295 }
3839e657
TT
296
297 /*
298 * Mark all the bad blocks as used...
299 */
cbbf031b 300 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
3839e657 301 if (retval) {
cbbf031b 302 com_err("ext2fs_badblocks_list_iterate_begin", retval,
d9c56d3c 303 _("while marking bad blocks as used"));
3839e657
TT
304 exit(1);
305 }
cbbf031b 306 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
f3db3566 307 ext2fs_mark_block_bitmap(fs->block_map, blk);
cbbf031b 308 ext2fs_badblocks_list_iterate_end(bb_iter);
3839e657
TT
309}
310
16ed5b3a
TT
311/*
312 * These functions implement a generalized progress meter.
313 */
314struct progress_struct {
315 char format[20];
316 char backup[80];
317 __u32 max;
318};
7d5633cf 319
16ed5b3a 320static void progress_init(struct progress_struct *progress,
4ea7bd04 321 const char *label,__u32 max)
16ed5b3a
TT
322{
323 int i;
3839e657 324
16ed5b3a
TT
325 memset(progress, 0, sizeof(struct progress_struct));
326 if (quiet)
327 return;
1dde43f0
TT
328
329 /*
330 * Figure out how many digits we need
331 */
16ed5b3a
TT
332 i = int_log10(max);
333 sprintf(progress->format, "%%%dd/%%%dld", i, i);
334 memset(progress->backup, '\b', sizeof(progress->backup)-1);
335 progress->backup[sizeof(progress->backup)-1] = 0;
336 if ((2*i)+1 < sizeof(progress->backup))
337 progress->backup[(2*i)+1] = 0;
338 progress->max = max;
339
340 fputs(label, stdout);
341 fflush(stdout);
342}
343
344static void progress_update(struct progress_struct *progress, __u32 val)
345{
346 if (progress->format[0] == 0)
347 return;
348 printf(progress->format, val, progress->max);
349 fputs(progress->backup, stdout);
350}
351
352static void progress_close(struct progress_struct *progress)
353{
354 if (progress->format[0] == 0)
355 return;
356 fputs(_("done \n"), stdout);
357}
358
359
360/*
361 * Helper function which zeros out _num_ blocks starting at _blk_. In
362 * case of an error, the details of the error is returned via _ret_blk_
363 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
364 * success, and an error code on an error.
365 *
366 * As a special case, if the first argument is NULL, then it will
367 * attempt to free the static zeroizing buffer. (This is to keep
368 * programs that check for memory leaks happy.)
369 */
370static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
371 struct progress_struct *progress,
372 blk_t *ret_blk, int *ret_count)
373{
374 int j, count, next_update, next_update_incr;
375 static char *buf;
376 errcode_t retval;
377
378 /* If fs is null, clean up the static buffer and return */
379 if (!fs) {
380 if (buf) {
381 free(buf);
382 buf = 0;
383 }
384 return 0;
385 }
386 /* Allocate the zeroizing buffer if necessary */
387 if (!buf) {
388 buf = malloc(fs->blocksize * STRIDE_LENGTH);
389 if (!buf) {
390 com_err("malloc", ENOMEM,
391 _("while allocating zeroizing buffer"));
392 exit(1);
393 }
394 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
395 }
396 /* OK, do the write loop */
397 next_update = 0;
398 next_update_incr = num / 100;
399 if (next_update_incr < 1)
400 next_update_incr = 1;
401 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
402 if (num-j > STRIDE_LENGTH)
403 count = STRIDE_LENGTH;
404 else
405 count = num - j;
406 retval = io_channel_write_blk(fs->io, blk, count, buf);
407 if (retval) {
408 if (ret_count)
409 *ret_count = count;
410 if (ret_blk)
411 *ret_blk = blk;
412 return retval;
413 }
414 if (progress && j > next_update) {
415 next_update += num / 100;
416 progress_update(progress, blk);
417 }
418 }
419 return 0;
420}
421
422static void write_inode_tables(ext2_filsys fs)
423{
424 errcode_t retval;
425 blk_t blk;
426 int i, num;
427 struct progress_struct progress;
428
429 if (quiet)
430 memset(&progress, 0, sizeof(progress));
431 else
432 progress_init(&progress, _("Writing inode tables: "),
433 fs->group_desc_count);
1dde43f0 434
3839e657 435 for (i = 0; i < fs->group_desc_count; i++) {
16ed5b3a 436 progress_update(&progress, i);
3839e657 437
19c78dc0
TT
438 blk = fs->group_desc[i].bg_inode_table;
439 num = fs->inode_blocks_per_group;
16ed5b3a
TT
440
441 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
442 if (retval) {
443 printf(_("\nCould not write %d blocks "
444 "in inode table starting at %d: %s\n"),
445 num, blk, error_message(retval));
446 exit(1);
19c78dc0 447 }
7d5633cf
TT
448 if (sync_kludge) {
449 if (sync_kludge == 1)
450 sync();
451 else if ((i % sync_kludge) == 0)
452 sync();
453 }
3839e657 454 }
16ed5b3a
TT
455 zero_blocks(0, 0, 0, 0, 0, 0);
456 progress_close(&progress);
3839e657
TT
457}
458
459static void create_root_dir(ext2_filsys fs)
460{
461 errcode_t retval;
f3db3566 462 struct ext2_inode inode;
3839e657
TT
463
464 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
465 if (retval) {
d9c56d3c 466 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
3839e657
TT
467 exit(1);
468 }
f3db3566
TT
469 if (geteuid()) {
470 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
471 if (retval) {
472 com_err("ext2fs_read_inode", retval,
d9c56d3c 473 _("while reading root inode"));
f3db3566
TT
474 exit(1);
475 }
19c78dc0
TT
476 inode.i_uid = getuid();
477 if (inode.i_uid)
478 inode.i_gid = getgid();
f3db3566
TT
479 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
480 if (retval) {
481 com_err("ext2fs_write_inode", retval,
d9c56d3c 482 _("while setting root inode ownership"));
f3db3566
TT
483 exit(1);
484 }
485 }
3839e657
TT
486}
487
488static void create_lost_and_found(ext2_filsys fs)
489{
490 errcode_t retval;
dfcdc32f 491 ext2_ino_t ino;
3839e657
TT
492 const char *name = "lost+found";
493 int i;
50787ea2 494 int lpf_size = 0;
3839e657 495
6a525069 496 fs->umask = 077;
3839e657
TT
497 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
498 if (retval) {
d9c56d3c
TT
499 com_err("ext2fs_mkdir", retval,
500 _("while creating /lost+found"));
3839e657
TT
501 exit(1);
502 }
503
504 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
505 if (retval) {
d9c56d3c
TT
506 com_err("ext2_lookup", retval,
507 _("while looking up /lost+found"));
3839e657
TT
508 exit(1);
509 }
510
511 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
50787ea2
TT
512 if ((lpf_size += fs->blocksize) >= 16*1024)
513 break;
3839e657
TT
514 retval = ext2fs_expand_dir(fs, ino);
515 if (retval) {
516 com_err("ext2fs_expand_dir", retval,
d9c56d3c 517 _("while expanding /lost+found"));
3839e657
TT
518 exit(1);
519 }
6a525069 520 }
3839e657
TT
521}
522
523static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
524{
525 errcode_t retval;
526
f3db3566 527 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
3839e657
TT
528 fs->group_desc[0].bg_free_inodes_count--;
529 fs->super->s_free_inodes_count--;
530 retval = ext2fs_update_bb_inode(fs, bb_list);
531 if (retval) {
532 com_err("ext2fs_update_bb_inode", retval,
d9c56d3c 533 _("while setting bad block inode"));
3839e657
TT
534 exit(1);
535 }
536
537}
538
539static void reserve_inodes(ext2_filsys fs)
540{
dfcdc32f
TT
541 ext2_ino_t i;
542 int group;
3839e657 543
7f88b043 544 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
f3db3566 545 ext2fs_mark_inode_bitmap(fs->inode_map, i);
3839e657
TT
546 group = ext2fs_group_of_ino(fs, i);
547 fs->group_desc[group].bg_free_inodes_count--;
548 fs->super->s_free_inodes_count--;
549 }
550 ext2fs_mark_ib_dirty(fs);
551}
552
04a96857 553static void zap_sector(ext2_filsys fs, int sect, int nsect)
f3db3566 554{
3f85f65c 555 char *buf;
f3db3566
TT
556 int retval;
557
3f85f65c 558 buf = malloc(512*nsect);
568101f7 559 if (!buf) {
4ea7bd04
TT
560 printf(_("Out of memory erasing sectors %d-%d\n"),
561 sect, sect + nsect - 1);
568101f7
AD
562 exit(1);
563 }
3f85f65c 564 memset(buf, 0, 512*nsect);
f3db3566 565
e41784eb 566 io_channel_set_blksize(fs->io, 512);
04a96857 567 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
e41784eb 568 io_channel_set_blksize(fs->io, fs->blocksize);
3f85f65c 569 free(buf);
f3db3566 570 if (retval)
e294cf2f 571 printf(_("Warning: could not erase sector %d: %s\n"), sect,
f3db3566
TT
572 error_message(retval));
573}
16ed5b3a
TT
574
575static void create_journal_dev(ext2_filsys fs)
576{
577 struct progress_struct progress;
578 errcode_t retval;
579 char *buf;
dc2ec525
TT
580 blk_t blk;
581 int count;
16ed5b3a 582
d6a27e00
TT
583 retval = ext2fs_create_journal_superblock(fs,
584 fs->super->s_blocks_count, 0, &buf);
585 if (retval) {
586 com_err("create_journal_dev", retval,
587 _("while initializing journal superblock"));
588 exit(1);
589 }
16ed5b3a
TT
590 if (quiet)
591 memset(&progress, 0, sizeof(progress));
592 else
593 progress_init(&progress, _("Zeroing journal device: "),
594 fs->super->s_blocks_count);
595
16ed5b3a
TT
596 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
597 &progress, &blk, &count);
598 if (retval) {
599 com_err("create_journal_dev", retval,
14bbcbc3 600 _("while zeroing journal device (block %u, count %d)"),
16ed5b3a
TT
601 blk, count);
602 exit(1);
603 }
dc2ec525
TT
604 zero_blocks(0, 0, 0, 0, 0, 0);
605
dc2ec525
TT
606 retval = io_channel_write_blk(fs->io,
607 fs->super->s_first_data_block+1,
608 1, buf);
16ed5b3a
TT
609 if (retval) {
610 com_err("create_journal_dev", retval,
611 _("while writing journal superblock"));
612 exit(1);
613 }
614 progress_close(&progress);
615}
f3db3566 616
3839e657
TT
617static void show_stats(ext2_filsys fs)
618{
ef9abe5f 619 struct ext2_super_block *s = fs->super;
1e3472c5 620 char buf[80];
3839e657 621 blk_t group_block;
1dde43f0 622 int i, need, col_left;
3839e657
TT
623
624 if (param.s_blocks_count != s->s_blocks_count)
d9c56d3c 625 printf(_("warning: %d blocks unused.\n\n"),
3839e657 626 param.s_blocks_count - s->s_blocks_count);
50787ea2
TT
627
628 memset(buf, 0, sizeof(buf));
629 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
d9c56d3c
TT
630 printf(_("Filesystem label=%s\n"), buf);
631 printf(_("OS type: "));
1e3472c5
TT
632 switch (fs->super->s_creator_os) {
633 case EXT2_OS_LINUX: printf ("Linux"); break;
ad6783df 634 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
1e3472c5 635 case EXT2_OS_MASIX: printf ("Masix"); break;
d9c56d3c 636 default: printf (_("(unknown os)"));
1e3472c5 637 }
50787ea2 638 printf("\n");
d9c56d3c 639 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
50787ea2 640 s->s_log_block_size);
d9c56d3c 641 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
50787ea2 642 s->s_log_frag_size);
d9c56d3c 643 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
3839e657 644 s->s_blocks_count);
d9c56d3c 645 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
3839e657
TT
646 s->s_r_blocks_count,
647 100.0 * s->s_r_blocks_count / s->s_blocks_count);
d9c56d3c
TT
648 printf(_("First data block=%u\n"), s->s_first_data_block);
649 if (fs->group_desc_count > 1)
c8c071a0 650 printf(_("%u block groups\n"), fs->group_desc_count);
d9c56d3c 651 else
c8c071a0 652 printf(_("%u block group\n"), fs->group_desc_count);
d9c56d3c 653 printf(_("%u blocks per group, %u fragments per group\n"),
3839e657 654 s->s_blocks_per_group, s->s_frags_per_group);
d9c56d3c 655 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
3839e657
TT
656
657 if (fs->group_desc_count == 1) {
658 printf("\n");
659 return;
660 }
661
d9c56d3c 662 printf(_("Superblock backups stored on blocks: "));
3839e657
TT
663 group_block = s->s_first_data_block;
664 col_left = 0;
665 for (i = 1; i < fs->group_desc_count; i++) {
666 group_block += s->s_blocks_per_group;
521e3685
TT
667 if (!ext2fs_bg_has_super(fs, i))
668 continue;
7671433a
TT
669 if (i != 1)
670 printf(", ");
6733c2fd 671 need = int_log10(group_block) + 2;
1dde43f0 672 if (need > col_left) {
3839e657 673 printf("\n\t");
1dde43f0 674 col_left = 72;
3839e657 675 }
1dde43f0 676 col_left -= need;
a418d3ad 677 printf("%u", group_block);
3839e657
TT
678 }
679 printf("\n\n");
680}
681
1e3472c5
TT
682/*
683 * Set the S_CREATOR_OS field. Return true if OS is known,
684 * otherwise, 0.
685 */
686static int set_os(struct ext2_super_block *sb, char *os)
687{
688 if (isdigit (*os))
689 sb->s_creator_os = atoi (os);
690 else if (strcasecmp(os, "linux") == 0)
691 sb->s_creator_os = EXT2_OS_LINUX;
692 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
693 sb->s_creator_os = EXT2_OS_HURD;
694 else if (strcasecmp(os, "masix") == 0)
695 sb->s_creator_os = EXT2_OS_MASIX;
696 else
697 return 0;
698 return 1;
699}
700
a418d3ad
TT
701#define PATH_SET "PATH=/sbin"
702
d163b094 703static void parse_raid_opts(const char *opts)
a29f4d30
TT
704{
705 char *buf, *token, *next, *p, *arg;
706 int len;
707 int raid_usage = 0;
708
709 len = strlen(opts);
710 buf = malloc(len+1);
711 if (!buf) {
d9c56d3c
TT
712 fprintf(stderr, _("Couldn't allocate memory to parse "
713 "raid options!\n"));
a29f4d30
TT
714 exit(1);
715 }
716 strcpy(buf, opts);
717 for (token = buf; token && *token; token = next) {
718 p = strchr(token, ',');
719 next = 0;
720 if (p) {
721 *p = 0;
722 next = p+1;
723 }
724 arg = strchr(token, '=');
725 if (arg) {
726 *arg = 0;
727 arg++;
728 }
729 if (strcmp(token, "stride") == 0) {
730 if (!arg) {
731 raid_usage++;
732 continue;
733 }
734 fs_stride = strtoul(arg, &p, 0);
735 if (*p || (fs_stride == 0)) {
d9c56d3c
TT
736 fprintf(stderr,
737 _("Invalid stride parameter.\n"));
a29f4d30
TT
738 raid_usage++;
739 continue;
740 }
741 } else
742 raid_usage++;
743 }
744 if (raid_usage) {
d9c56d3c 745 fprintf(stderr, _("\nBad raid options specified.\n\n"
a29f4d30
TT
746 "Raid options are separated by commas, "
747 "and may take an argument which\n"
748 "\tis set off by an equals ('=') sign.\n\n"
749 "Valid raid options are:\n"
d9c56d3c 750 "\tstride=<stride length in blocks>\n\n"));
a29f4d30
TT
751 exit(1);
752 }
753}
754
896938d5 755static __u32 ok_features[3] = {
dc2ec525 756 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
16ed5b3a
TT
757 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
758 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
896938d5
TT
759 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
760};
a29f4d30
TT
761
762
3839e657
TT
763static void PRS(int argc, char *argv[])
764{
4a600566
TT
765 int c;
766 int size;
767 char * tmp;
80c22c90 768 blk_t group_blk_max = 8192;
4a600566
TT
769 int blocksize = 0;
770 int inode_ratio = 0;
771 int reserved_ratio = 5;
dfcdc32f 772 ext2_ino_t num_inodes = 0;
a418d3ad 773 errcode_t retval;
4a600566 774 char * oldpath = getenv("PATH");
4a600566 775 char * raid_opts = 0;
4ea7bd04 776 const char * fs_type = 0;
f3561ed5 777 int default_features = 1;
4a600566 778 blk_t dev_size;
2740156b 779#ifdef linux
4a600566 780 struct utsname ut;
2740156b 781#endif
14bbcbc3 782
3839e657 783 /* Update our PATH to include /sbin */
a418d3ad
TT
784 if (oldpath) {
785 char *newpath;
786
787 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
788 strcpy (newpath, PATH_SET);
789 strcat (newpath, ":");
790 strcat (newpath, oldpath);
791 putenv (newpath);
792 } else
793 putenv (PATH_SET);
3839e657 794
16ed5b3a
TT
795 tmp = getenv("MKE2FS_SYNC");
796 if (tmp)
797 sync_kludge = atoi(tmp);
798
3839e657
TT
799 setbuf(stdout, NULL);
800 setbuf(stderr, NULL);
801 initialize_ext2_error_table();
802 memset(&param, 0, sizeof(struct ext2_super_block));
50787ea2 803 param.s_rev_level = 1; /* Create revision 1 filesystems now */
f3561ed5
TT
804 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
805 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
806
14bbcbc3
TT
807#ifdef linux
808 if (uname(&ut)) {
809 perror("uname");
810 exit(1);
811 }
812 if ((ut.release[0] == '1') ||
813 (ut.release[0] == '2' && ut.release[1] == '.' &&
814 ut.release[2] < '2' && ut.release[3] == '.')) {
14bbcbc3 815 param.s_rev_level = 0;
f3561ed5
TT
816 param.s_feature_incompat = 0;
817 param.s_feature_compat = 0;
818 param.s_feature_ro_compat = 0;
14bbcbc3
TT
819 }
820#endif
9d8e6346 821 fprintf (stderr, "mke2fs %s (%s)\n",
04a96857 822 E2FSPROGS_VERSION, E2FSPROGS_DATE);
0072f8de
AD
823
824 if (argc && *argv) {
825 program_name = get_progname(*argv);
826
827 /* If called as mkfs.ext3, create a journal inode */
828 if (!strcmp(program_name, "mkfs.ext3"))
829 journal_size = -1;
830 }
831
1e3472c5 832 while ((c = getopt (argc, argv,
dc2ec525 833 "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF)
3839e657
TT
834 switch (c) {
835 case 'b':
50787ea2
TT
836 blocksize = strtoul(optarg, &tmp, 0);
837 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
d9c56d3c
TT
838 com_err(program_name, 0,
839 _("bad block size - %s"), optarg);
3839e657
TT
840 exit(1);
841 }
842 param.s_log_block_size =
6733c2fd 843 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
80c22c90 844 group_blk_max = blocksize * 8;
3839e657 845 break;
b10fd5e8
TT
846 case 'c': /* Check for bad blocks */
847 case 't': /* deprecated */
3ed57c27 848 cflag++;
3839e657
TT
849 break;
850 case 'f':
851 size = strtoul(optarg, &tmp, 0);
852 if (size < 1024 || size > 4096 || *tmp) {
d9c56d3c
TT
853 com_err(program_name, 0,
854 _("bad fragment size - %s"),
3839e657
TT
855 optarg);
856 exit(1);
857 }
858 param.s_log_frag_size =
6733c2fd 859 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
d9c56d3c
TT
860 printf(_("Warning: fragments not supported. "
861 "Ignoring -f option\n"));
3839e657
TT
862 break;
863 case 'g':
864 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
f3db3566
TT
865 if (*tmp) {
866 com_err(program_name, 0,
d9c56d3c 867 _("Illegal number for blocks per group"));
f3db3566
TT
868 exit(1);
869 }
f3db3566
TT
870 if ((param.s_blocks_per_group % 8) != 0) {
871 com_err(program_name, 0,
d9c56d3c 872 _("blocks per group must be multiple of 8"));
3839e657
TT
873 exit(1);
874 }
875 break;
876 case 'i':
877 inode_ratio = strtoul(optarg, &tmp, 0);
44c09c04 878 if (inode_ratio < 1024 || inode_ratio > 4096 * 1024 ||
3839e657 879 *tmp) {
d9c56d3c
TT
880 com_err(program_name, 0,
881 _("bad inode ratio - %s"), optarg);
3839e657
TT
882 exit(1);
883 }
884 break;
dc2ec525
TT
885 case 'J':
886 parse_journal_opts(optarg);
887 break;
85ef4ae8 888 case 'j':
14bbcbc3
TT
889 param.s_feature_compat |=
890 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
dc2ec525
TT
891 if (!journal_size)
892 journal_size = -1;
8ddaa66b 893 break;
3839e657 894 case 'l':
f3db3566
TT
895 bad_blocks_filename = malloc(strlen(optarg)+1);
896 if (!bad_blocks_filename) {
897 com_err(program_name, ENOMEM,
d9c56d3c 898 _("in malloc for bad_blocks_filename"));
f3db3566
TT
899 exit(1);
900 }
901 strcpy(bad_blocks_filename, optarg);
3839e657
TT
902 break;
903 case 'm':
904 reserved_ratio = strtoul(optarg, &tmp, 0);
905 if (reserved_ratio > 50 || *tmp) {
906 com_err(program_name, 0,
d9c56d3c 907 _("bad reserved blocks percent - %s"),
3839e657
TT
908 optarg);
909 exit(1);
910 }
911 break;
50787ea2
TT
912 case 'n':
913 noaction++;
914 break;
1e3472c5
TT
915 case 'o':
916 creator_os = optarg;
917 break;
7f88b043
TT
918 case 'r':
919 param.s_rev_level = atoi(optarg);
14bbcbc3 920 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
f3561ed5
TT
921 param.s_feature_incompat = 0;
922 param.s_feature_compat = 0;
923 param.s_feature_ro_compat = 0;
14bbcbc3 924 }
7f88b043 925 break;
b10fd5e8 926 case 's': /* deprecated */
f3561ed5
TT
927 if (atoi(optarg))
928 param.s_feature_ro_compat |=
929 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
930 else
931 param.s_feature_ro_compat &=
932 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
521e3685 933 break;
7f88b043
TT
934#ifdef EXT2_DYNAMIC_REV
935 case 'I':
936 param.s_inode_size = atoi(optarg);
937 break;
938#endif
5515e6b4
TT
939 case 'N':
940 num_inodes = atoi(optarg);
941 break;
3839e657
TT
942 case 'v':
943 verbose = 1;
944 break;
945 case 'q':
946 quiet = 1;
947 break;
74becf3c
TT
948 case 'F':
949 force = 1;
950 break;
1e3472c5
TT
951 case 'L':
952 volume_label = optarg;
953 break;
954 case 'M':
955 mount_dir = optarg;
956 break;
896938d5 957 case 'O':
f3561ed5
TT
958 if (!strcmp(optarg, "none") || default_features) {
959 param.s_feature_compat = 0;
960 param.s_feature_incompat = 0;
961 param.s_feature_ro_compat = 0;
962 default_features = 0;
963 }
964 if (!strcmp(optarg, "none"))
965 break;
966 if (e2p_edit_feature(optarg,
967 &param.s_feature_compat,
968 ok_features)) {
969 fprintf(stderr,
970 _("Invalid filesystem option set: %s\n"), optarg);
971 exit(1);
972 }
896938d5 973 break;
a29f4d30
TT
974 case 'R':
975 raid_opts = optarg;
976 break;
f3db3566
TT
977 case 'S':
978 super_only = 1;
979 break;
50787ea2
TT
980 case 'T':
981 fs_type = optarg;
982 break;
818180cd
TT
983 case 'V':
984 /* Print version number and exit */
d9c56d3c 985 fprintf(stderr, _("\tUsing %s\n"),
818180cd
TT
986 error_message(EXT2_ET_BASE));
987 exit(0);
3839e657
TT
988 default:
989 usage();
990 }
991 if (optind == argc)
992 usage();
993 device_name = argv[optind];
994 optind++;
995 if (optind < argc) {
e1a0a3e3
TT
996 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
997
998 if ((*tmp) || (tmp2 > 0xfffffffful)) {
d9c56d3c 999 com_err(program_name, 0, _("bad blocks count - %s"),
3839e657
TT
1000 argv[optind - 1]);
1001 exit(1);
1002 }
e1a0a3e3 1003 param.s_blocks_count = tmp2;
3839e657
TT
1004 }
1005 if (optind < argc)
1006 usage();
a418d3ad 1007
a29f4d30
TT
1008 if (raid_opts)
1009 parse_raid_opts(raid_opts);
1010
77dc4eb0
TT
1011 /*
1012 * If there's no blocksize specified and there is a journal
1013 * device, use it to figure out the blocksize
1014 */
1015 if (blocksize == 0 && journal_device) {
1016 ext2_filsys jfs;
1017
1018 retval = ext2fs_open(journal_device,
1019 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1020 0, unix_io_manager, &jfs);
1021 if (retval) {
1022 com_err(program_name, retval,
1023 _("while trying to open journal device %s\n"),
1024 journal_device);
1025 exit(1);
1026 }
1027 blocksize = jfs->blocksize;
1028 param.s_log_block_size =
1029 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1030 ext2fs_close(jfs);
1031 }
dc2ec525 1032
fa72458a
AD
1033 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1034 if (!fs_type)
1035 fs_type = "journal";
1036 reserved_ratio = 0;
f3561ed5
TT
1037 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1038 param.s_feature_compat = 0;
1039 param.s_feature_ro_compat = 0;
1040 }
14bbcbc3
TT
1041 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1042 (param.s_feature_compat || param.s_feature_ro_compat ||
1043 param.s_feature_incompat))
1044 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1045
74becf3c 1046 if (!force)
8ddaa66b 1047 check_plausibility(device_name);
63985320 1048 check_mount(device_name, force, _("filesystem"));
a418d3ad 1049
3839e657
TT
1050 param.s_log_frag_size = param.s_log_block_size;
1051
50787ea2
TT
1052 if (noaction && param.s_blocks_count) {
1053 dev_size = param.s_blocks_count;
1054 retval = 0;
1055 } else
1056 retval = ext2fs_get_device_size(device_name,
1057 EXT2_BLOCK_SIZE(&param),
1058 &dev_size);
a789d840
TT
1059 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1060 com_err(program_name, retval,
d9c56d3c 1061 _("while trying to determine filesystem size"));
a789d840
TT
1062 exit(1);
1063 }
a418d3ad 1064 if (!param.s_blocks_count) {
a789d840
TT
1065 if (retval == EXT2_ET_UNIMPLEMENTED) {
1066 com_err(program_name, 0,
d9c56d3c 1067 _("Couldn't determine device size; you "
a789d840 1068 "must specify\nthe size of the "
d9c56d3c 1069 "filesystem\n"));
a418d3ad 1070 exit(1);
26ab5315
TT
1071 } else {
1072 if (dev_size == 0) {
1073 com_err(program_name, 0,
1074 _("Device size reported to be zero. "
1075 "Invalid partition specified, or\n\t"
1076 "partition table wasn't reread "
1077 "after running fdisk, due to\n\t"
1078 "a modified partition being busy "
1079 "and in use. You may need to reboot\n\t"
1080 "to re-read your partition table.\n"
1081 ));
1082 exit(1);
1083 }
a789d840 1084 param.s_blocks_count = dev_size;
26ab5315
TT
1085 }
1086
a789d840
TT
1087 } else if (!force && (param.s_blocks_count > dev_size)) {
1088 com_err(program_name, 0,
d9c56d3c 1089 _("Filesystem larger than apparent filesystem size."));
a789d840 1090 proceed_question();
a418d3ad 1091 }
3839e657 1092
dc2ec525
TT
1093 /*
1094 * If the user asked for HAS_JOURNAL, then make sure a journal
1095 * gets created.
1096 */
1097 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1098 !journal_size)
1099 journal_size = -1;
77dc4eb0 1100
dc2ec525 1101 set_fs_defaults(fs_type, &param, blocksize, &inode_ratio);
50787ea2 1102
521e3685
TT
1103 if (param.s_blocks_per_group) {
1104 if (param.s_blocks_per_group < 256 ||
80c22c90 1105 param.s_blocks_per_group > group_blk_max || *tmp) {
521e3685 1106 com_err(program_name, 0,
d9c56d3c 1107 _("blocks per group count out of range"));
521e3685
TT
1108 exit(1);
1109 }
1110 }
1111
3839e657
TT
1112 /*
1113 * Calculate number of inodes based on the inode ratio
1114 */
5515e6b4 1115 param.s_inodes_count = num_inodes ? num_inodes :
e6597048 1116 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
f3db3566 1117 / inode_ratio;
3839e657
TT
1118
1119 /*
1120 * Calculate number of blocks to reserve
1121 */
1122 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
521e3685 1123
3839e657
TT
1124}
1125
1126int main (int argc, char *argv[])
1127{
1128 errcode_t retval = 0;
1129 ext2_filsys fs;
1130 badblocks_list bb_list = 0;
85ef4ae8 1131 int journal_blocks;
44c09c04 1132 int i, val;
d9c56d3c
TT
1133
1134#ifdef ENABLE_NLS
1135 setlocale(LC_MESSAGES, "");
14308a53 1136 setlocale(LC_CTYPE, "");
d9c56d3c
TT
1137 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1138 textdomain(NLS_CAT_NAME);
1139#endif
3839e657
TT
1140 PRS(argc, argv);
1141
3839e657
TT
1142 /*
1143 * Initialize the superblock....
1144 */
1145 retval = ext2fs_initialize(device_name, 0, &param,
1146 unix_io_manager, &fs);
1147 if (retval) {
d9c56d3c 1148 com_err(device_name, retval, _("while setting up superblock"));
3839e657
TT
1149 exit(1);
1150 }
1151
e41784eb
TT
1152 /*
1153 * Wipe out the old on-disk superblock
1154 */
04a96857
TT
1155 if (!noaction)
1156 zap_sector(fs, 2, 6);
e41784eb 1157
1e3472c5
TT
1158 /*
1159 * Generate a UUID for it...
1160 */
ef9abe5f 1161 uuid_generate(fs->super->s_uuid);
1e3472c5 1162
44c09c04
TT
1163 /*
1164 * Add "jitter" to the superblock's check interval so that we
1165 * don't check all the filesystems at the same time. We use a
1166 * kludgy hack of using the UUID to derive a random jitter value.
1167 */
1168 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1169 val += fs->super->s_uuid[i];
1170 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1171
1e3472c5
TT
1172 /*
1173 * Override the creator OS, if applicable
1174 */
1175 if (creator_os && !set_os(fs->super, creator_os)) {
d9c56d3c 1176 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1e3472c5
TT
1177 exit(1);
1178 }
1179
4ea0a110
TT
1180 /*
1181 * For the Hurd, we will turn off filetype since it doesn't
1182 * support it.
1183 */
1184 if (fs->super->s_creator_os == EXT2_OS_HURD)
1185 fs->super->s_feature_incompat &=
1186 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1187
1e3472c5
TT
1188 /*
1189 * Set the volume label...
1190 */
1191 if (volume_label) {
ef9abe5f
TT
1192 memset(fs->super->s_volume_name, 0,
1193 sizeof(fs->super->s_volume_name));
1194 strncpy(fs->super->s_volume_name, volume_label,
1195 sizeof(fs->super->s_volume_name));
1e3472c5
TT
1196 }
1197
1198 /*
1199 * Set the last mount directory
1200 */
1201 if (mount_dir) {
ef9abe5f
TT
1202 memset(fs->super->s_last_mounted, 0,
1203 sizeof(fs->super->s_last_mounted));
1204 strncpy(fs->super->s_last_mounted, mount_dir,
1205 sizeof(fs->super->s_last_mounted));
1e3472c5
TT
1206 }
1207
50787ea2 1208 if (!quiet || noaction)
3839e657
TT
1209 show_stats(fs);
1210
50787ea2
TT
1211 if (noaction)
1212 exit(0);
1213
16ed5b3a
TT
1214 if (fs->super->s_feature_incompat &
1215 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1216 create_journal_dev(fs);
568101f7 1217 exit(ext2fs_close(fs) ? 1 : 0);
16ed5b3a
TT
1218 }
1219
3839e657
TT
1220 if (bad_blocks_filename)
1221 read_bb_file(fs, &bb_list, bad_blocks_filename);
1222 if (cflag)
1223 test_disk(fs, &bb_list);
1224
1225 handle_bad_blocks(fs, bb_list);
a29f4d30 1226 fs->stride = fs_stride;
19c78dc0
TT
1227 retval = ext2fs_allocate_tables(fs);
1228 if (retval) {
1229 com_err(program_name, retval,
d9c56d3c 1230 _("while trying to allocate filesystem tables"));
19c78dc0
TT
1231 exit(1);
1232 }
f3db3566
TT
1233 if (super_only) {
1234 fs->super->s_state |= EXT2_ERROR_FS;
1235 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1236 } else {
59f27247 1237 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
04a96857 1238 int rsv = 65536 / fs->blocksize;
1400bbb6 1239 unsigned long blocks = fs->super->s_blocks_count;
59f27247 1240 unsigned long start;
1400bbb6
TT
1241 blk_t ret_blk;
1242
1243#ifdef ZAP_BOOTBLOCK
04a96857 1244 zap_sector(fs, 0, 2);
1400bbb6 1245#endif
59f27247 1246
1400bbb6
TT
1247 /*
1248 * Wipe out any old MD RAID (or other) metadata at the end
1249 * of the device. This will also verify that the device is
59f27247 1250 * as large as we think. Be careful with very small devices.
1400bbb6 1251 */
59f27247
AD
1252 start = (blocks & ~(rsv - 1));
1253 if (start > rsv)
1254 start -= rsv;
1255 if (start > 0)
1256 retval = zero_blocks(fs, start, blocks - start,
1257 NULL, &ret_blk, NULL);
1258
1400bbb6
TT
1259 if (retval) {
1260 com_err(program_name, retval,
1261 _("zeroing block %u at end of filesystem"),
1262 ret_blk);
1263 exit(1);
1264 }
19c78dc0 1265 write_inode_tables(fs);
f3db3566
TT
1266 create_root_dir(fs);
1267 create_lost_and_found(fs);
1268 reserve_inodes(fs);
1269 create_bad_block_inode(fs, bb_list);
f3db3566 1270 }
8ddaa66b 1271
16ed5b3a
TT
1272 if (journal_device) {
1273 ext2_filsys jfs;
1274
8ddaa66b
TT
1275 if (!force)
1276 check_plausibility(journal_device);
63985320 1277 check_mount(journal_device, force, _("journal"));
8ddaa66b 1278
16ed5b3a
TT
1279 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1280 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1281 fs->blocksize, unix_io_manager, &jfs);
1282 if (retval) {
1283 com_err(program_name, retval,
1284 _("while trying to open journal device %s\n"),
1285 journal_device);
1286 exit(1);
1287 }
93345d15 1288 if (!quiet) {
77dc4eb0 1289 printf(_("Adding journal to device %s: "),
8ddaa66b 1290 journal_device);
93345d15
TT
1291 fflush(stdout);
1292 }
16ed5b3a
TT
1293 retval = ext2fs_add_journal_device(fs, jfs);
1294 if(retval) {
8ddaa66b 1295 com_err (program_name, retval,
1d08d9bf 1296 _("\n\twhile trying to add journal to device %s"),
8ddaa66b
TT
1297 journal_device);
1298 exit(1);
1299 }
1300 if (!quiet)
1301 printf(_("done\n"));
16ed5b3a 1302 ext2fs_close(jfs);
2d15576d 1303 free(journal_device);
8ddaa66b 1304 } else if (journal_size) {
2537b6d0 1305 journal_blocks = figure_journal_size(journal_size, fs);
93345d15
TT
1306
1307 if (!journal_blocks) {
1308 fs->super->s_feature_compat &=
1309 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1310 goto no_journal;
1311 }
1312 if (!quiet) {
16ad3334
TT
1313 printf(_("Creating journal (%d blocks): "),
1314 journal_blocks);
93345d15
TT
1315 fflush(stdout);
1316 }
63985320
TT
1317 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1318 journal_flags);
85ef4ae8
TT
1319 if (retval) {
1320 com_err (program_name, retval,
1d08d9bf 1321 _("\n\twhile trying to create journal"));
85ef4ae8
TT
1322 exit(1);
1323 }
1324 if (!quiet)
1325 printf(_("done\n"));
1326 }
93345d15
TT
1327no_journal:
1328
3839e657 1329 if (!quiet)
d9c56d3c
TT
1330 printf(_("Writing superblocks and "
1331 "filesystem accounting information: "));
5d45d803
TT
1332 retval = ext2fs_flush(fs);
1333 if (retval) {
d9c56d3c 1334 printf(_("\nWarning, had trouble writing out superblocks."));
5d45d803 1335 }
93345d15 1336 if (!quiet) {
2537b6d0 1337 printf(_("done\n\n"));
66cf2f60 1338 print_check_message(fs);
93345d15 1339 }
568101f7
AD
1340 val = ext2fs_close(fs);
1341 return (retval || val) ? 1 : 0;
3839e657 1342}