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