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