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