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