Whamcloud - gitweb
mke2fs: Dumb down filesystems for GNU Hurd
[tools/e2fsprogs.git] / misc / mke2fs.c
1 /*
2  * mke2fs.c - Make a ext2fs filesystem.
3  * 
4  * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5  *      2003, 2004, 2005 by Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
10  * %End-Header%
11  */
12
13 /* Usage: mke2fs [options] device
14  * 
15  * The device may be a block device or a image of one, but this isn't
16  * enforced (but it's not much fun on a character device :-). 
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <ctype.h>
23 #include <time.h>
24 #ifdef __linux__
25 #include <sys/utsname.h>
26 #endif
27 #ifdef HAVE_GETOPT_H
28 #include <getopt.h>
29 #else
30 extern char *optarg;
31 extern int optind;
32 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #ifdef HAVE_MNTENT_H
43 #include <mntent.h>
44 #endif
45 #include <sys/ioctl.h>
46 #include <sys/types.h>
47 #include <libgen.h>
48 #include <limits.h>
49
50 #include "ext2fs/ext2_fs.h"
51 #include "et/com_err.h"
52 #include "uuid/uuid.h"
53 #include "e2p/e2p.h"
54 #include "ext2fs/ext2fs.h"
55 #include "util.h"
56 #include "profile.h"
57 #include "prof_err.h"
58 #include "../version.h"
59 #include "nls-enable.h"
60
61 #define STRIDE_LENGTH 8
62
63 #ifndef __sparc__
64 #define ZAP_BOOTBLOCK
65 #endif
66
67 extern int isatty(int);
68 extern FILE *fpopen(const char *cmd, const char *mode);
69
70 const char * program_name = "mke2fs";
71 const char * device_name /* = NULL */;
72
73 /* Command line options */
74 int     cflag;
75 int     verbose;
76 int     quiet;
77 int     super_only;
78 int     force;
79 int     noaction;
80 int     journal_size;
81 int     journal_flags;
82 int     lazy_itable_init;
83 char    *bad_blocks_filename;
84 __u32   fs_stride;
85
86 struct ext2_super_block fs_param;
87 char *creator_os;
88 char *volume_label;
89 char *mount_dir;
90 char *journal_device;
91 int sync_kludge;        /* Set using the MKE2FS_SYNC env. option */
92 char **fs_types;
93
94 profile_t       profile;
95
96 int sys_page_size = 4096;
97 int linux_version_code = 0;
98
99 static void usage(void)
100 {
101         fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
102         "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
103         "[-J journal-options]\n"
104         "\t[-G meta group size] [-N number-of-inodes]\n"
105         "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
106         "\t[-g blocks-per-group] [-L volume-label] "
107         "[-M last-mounted-directory]\n\t[-O feature[,...]] "
108         "[-r fs-revision] [-E extended-option[,...]]\n"
109         "\t[-T fs-type] [-jnqvFSV] device [blocks-count]\n"),
110                 program_name);
111         exit(1);
112 }
113
114 static int int_log2(int arg)
115 {
116         int     l = 0;
117
118         arg >>= 1;
119         while (arg) {
120                 l++;
121                 arg >>= 1;
122         }
123         return l;
124 }
125
126 static int int_log10(unsigned int arg)
127 {
128         int     l;
129
130         for (l=0; arg ; l++)
131                 arg = arg / 10;
132         return l;
133 }
134
135 static int parse_version_number(const char *s)
136 {
137         int     major, minor, rev;
138         char    *endptr;
139         const char *cp = s;
140
141         if (!s)
142                 return 0;
143         major = strtol(cp, &endptr, 10);
144         if (cp == endptr || *endptr != '.')
145                 return 0;
146         cp = endptr + 1;
147         minor = strtol(cp, &endptr, 10);
148         if (cp == endptr || *endptr != '.')
149                 return 0;
150         cp = endptr + 1;
151         rev = strtol(cp, &endptr, 10);
152         if (cp == endptr)
153                 return 0;
154         return ((((major * 256) + minor) * 256) + rev);
155 }
156
157 /*
158  * Helper function for read_bb_file and test_disk
159  */
160 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
161 {
162         fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
163         return;
164 }
165
166 /*
167  * Reads the bad blocks list from a file
168  */
169 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
170                          const char *bad_blocks_file)
171 {
172         FILE            *f;
173         errcode_t       retval;
174
175         f = fopen(bad_blocks_file, "r");
176         if (!f) {
177                 com_err("read_bad_blocks_file", errno,
178                         _("while trying to open %s"), bad_blocks_file);
179                 exit(1);
180         }
181         retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
182         fclose (f);
183         if (retval) {
184                 com_err("ext2fs_read_bb_FILE", retval,
185                         _("while reading in list of bad blocks from file"));
186                 exit(1);
187         }
188 }
189
190 /*
191  * Runs the badblocks program to test the disk
192  */
193 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
194 {
195         FILE            *f;
196         errcode_t       retval;
197         char            buf[1024];
198
199         sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
200                 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
201                 fs->device_name, fs->super->s_blocks_count-1);
202         if (verbose)
203                 printf(_("Running command: %s\n"), buf);
204         f = popen(buf, "r");
205         if (!f) {
206                 com_err("popen", errno,
207                         _("while trying to run '%s'"), buf);
208                 exit(1);
209         }
210         retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
211         pclose(f);
212         if (retval) {
213                 com_err("ext2fs_read_bb_FILE", retval,
214                         _("while processing list of bad blocks from program"));
215                 exit(1);
216         }
217 }
218
219 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
220 {
221         dgrp_t                  i;
222         blk_t                   j;
223         unsigned                must_be_good;
224         blk_t                   blk;
225         badblocks_iterate       bb_iter;
226         errcode_t               retval;
227         blk_t                   group_block;
228         int                     group;
229         int                     group_bad;
230
231         if (!bb_list)
232                 return;
233         
234         /*
235          * The primary superblock and group descriptors *must* be
236          * good; if not, abort.
237          */
238         must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
239         for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
240                 if (ext2fs_badblocks_list_test(bb_list, i)) {
241                         fprintf(stderr, _("Block %d in primary "
242                                 "superblock/group descriptor area bad.\n"), i);
243                         fprintf(stderr, _("Blocks %u through %u must be good "
244                                 "in order to build a filesystem.\n"),
245                                 fs->super->s_first_data_block, must_be_good);
246                         fputs(_("Aborting....\n"), stderr);
247                         exit(1);
248                 }
249         }
250
251         /*
252          * See if any of the bad blocks are showing up in the backup
253          * superblocks and/or group descriptors.  If so, issue a
254          * warning and adjust the block counts appropriately.
255          */
256         group_block = fs->super->s_first_data_block +
257                 fs->super->s_blocks_per_group;
258         
259         for (i = 1; i < fs->group_desc_count; i++) {
260                 group_bad = 0;
261                 for (j=0; j < fs->desc_blocks+1; j++) {
262                         if (ext2fs_badblocks_list_test(bb_list,
263                                                        group_block + j)) {
264                                 if (!group_bad) 
265                                         fprintf(stderr,
266 _("Warning: the backup superblock/group descriptors at block %u contain\n"
267 "       bad blocks.\n\n"),
268                                                 group_block);
269                                 group_bad++;
270                                 group = ext2fs_group_of_blk(fs, group_block+j);
271                                 fs->group_desc[group].bg_free_blocks_count++;
272                                 ext2fs_group_desc_csum_set(fs, group);
273                                 fs->super->s_free_blocks_count++;
274                         }
275                 }
276                 group_block += fs->super->s_blocks_per_group;
277         }
278         
279         /*
280          * Mark all the bad blocks as used...
281          */
282         retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
283         if (retval) {
284                 com_err("ext2fs_badblocks_list_iterate_begin", retval,
285                         _("while marking bad blocks as used"));
286                 exit(1);
287         }
288         while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) 
289                 ext2fs_mark_block_bitmap(fs->block_map, blk);
290         ext2fs_badblocks_list_iterate_end(bb_iter);
291 }
292
293 /*
294  * These functions implement a generalized progress meter.
295  */
296 struct progress_struct {
297         char            format[20];
298         char            backup[80];
299         __u32           max;
300         int             skip_progress;
301 };
302
303 static void progress_init(struct progress_struct *progress,
304                           const char *label,__u32 max)
305 {
306         int     i;
307
308         memset(progress, 0, sizeof(struct progress_struct));
309         if (quiet)
310                 return;
311
312         /*
313          * Figure out how many digits we need
314          */
315         i = int_log10(max);
316         sprintf(progress->format, "%%%dd/%%%dld", i, i);
317         memset(progress->backup, '\b', sizeof(progress->backup)-1);
318         progress->backup[sizeof(progress->backup)-1] = 0;
319         if ((2*i)+1 < (int) sizeof(progress->backup))
320                 progress->backup[(2*i)+1] = 0;
321         progress->max = max;
322
323         progress->skip_progress = 0;
324         if (getenv("MKE2FS_SKIP_PROGRESS"))
325                 progress->skip_progress++;
326
327         fputs(label, stdout);
328         fflush(stdout);
329 }
330
331 static void progress_update(struct progress_struct *progress, __u32 val)
332 {
333         if ((progress->format[0] == 0) || progress->skip_progress)
334                 return;
335         printf(progress->format, val, progress->max);
336         fputs(progress->backup, stdout);
337 }
338
339 static void progress_close(struct progress_struct *progress)
340 {
341         if (progress->format[0] == 0)
342                 return;
343         fputs(_("done                            \n"), stdout);
344 }
345
346 static void write_inode_tables(ext2_filsys fs, int lazy_flag)
347 {
348         errcode_t       retval;
349         blk_t           blk;
350         dgrp_t          i;
351         int             num, ipb;
352         struct progress_struct progress;
353
354         if (quiet)
355                 memset(&progress, 0, sizeof(progress));
356         else
357                 progress_init(&progress, _("Writing inode tables: "),
358                               fs->group_desc_count);
359
360         for (i = 0; i < fs->group_desc_count; i++) {
361                 progress_update(&progress, i);
362                 
363                 blk = fs->group_desc[i].bg_inode_table;
364                 num = fs->inode_blocks_per_group;
365
366                 if (lazy_flag) {
367                         ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
368                         num = ((((fs->super->s_inodes_per_group -
369                                   fs->group_desc[i].bg_itable_unused) *
370                                  EXT2_INODE_SIZE(fs->super)) +
371                                 EXT2_BLOCK_SIZE(fs->super) - 1) /
372                                EXT2_BLOCK_SIZE(fs->super));
373                 } else {
374                         /* The kernel doesn't need to zero the itable blocks */
375                         fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
376                         ext2fs_group_desc_csum_set(fs, i);
377                 }
378                 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
379                 if (retval) {
380                         fprintf(stderr, _("\nCould not write %d "
381                                   "blocks in inode table starting at %u: %s\n"),
382                                 num, blk, error_message(retval));
383                         exit(1);
384                 }
385                 if (sync_kludge) {
386                         if (sync_kludge == 1)
387                                 sync();
388                         else if ((i % sync_kludge) == 0)
389                                 sync();
390                 }
391         }
392         ext2fs_zero_blocks(0, 0, 0, 0, 0);
393         progress_close(&progress);
394 }
395
396 static void create_root_dir(ext2_filsys fs)
397 {
398         errcode_t               retval;
399         struct ext2_inode       inode;
400         __u32                   uid, gid;
401
402         retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
403         if (retval) {
404                 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
405                 exit(1);
406         }
407         if (geteuid()) {
408                 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
409                 if (retval) {
410                         com_err("ext2fs_read_inode", retval,
411                                 _("while reading root inode"));
412                         exit(1);
413                 }
414                 uid = getuid();
415                 inode.i_uid = uid;
416                 ext2fs_set_i_uid_high(inode, uid >> 16);
417                 if (uid) {
418                         gid = getgid();
419                         inode.i_gid = gid;
420                         ext2fs_set_i_gid_high(inode, gid >> 16);
421                 }
422                 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
423                 if (retval) {
424                         com_err("ext2fs_write_inode", retval,
425                                 _("while setting root inode ownership"));
426                         exit(1);
427                 }
428         }
429 }
430
431 static void create_lost_and_found(ext2_filsys fs)
432 {
433         unsigned int            lpf_size = 0;
434         errcode_t               retval;
435         ext2_ino_t              ino;
436         const char              *name = "lost+found";
437         int                     i;
438
439         fs->umask = 077;
440         retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
441         if (retval) {
442                 com_err("ext2fs_mkdir", retval,
443                         _("while creating /lost+found"));
444                 exit(1);
445         }
446
447         retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
448         if (retval) {
449                 com_err("ext2_lookup", retval,
450                         _("while looking up /lost+found"));
451                 exit(1);
452         }
453         
454         for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
455                 /* Ensure that lost+found is at least 2 blocks, so we always
456                  * test large empty blocks for big-block filesystems.  */
457                 if ((lpf_size += fs->blocksize) >= 16*1024 &&
458                     lpf_size >= 2 * fs->blocksize)
459                         break;
460                 retval = ext2fs_expand_dir(fs, ino);
461                 if (retval) {
462                         com_err("ext2fs_expand_dir", retval,
463                                 _("while expanding /lost+found"));
464                         exit(1);
465                 }
466         }
467 }
468
469 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
470 {
471         errcode_t       retval;
472         
473         ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
474         ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
475         retval = ext2fs_update_bb_inode(fs, bb_list);
476         if (retval) {
477                 com_err("ext2fs_update_bb_inode", retval,
478                         _("while setting bad block inode"));
479                 exit(1);
480         }
481
482 }
483
484 static void reserve_inodes(ext2_filsys fs)
485 {
486         ext2_ino_t      i;
487
488         for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
489                 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
490         ext2fs_mark_ib_dirty(fs);
491 }
492
493 #define BSD_DISKMAGIC   (0x82564557UL)  /* The disk magic number */
494 #define BSD_MAGICDISK   (0x57455682UL)  /* The disk magic number reversed */
495 #define BSD_LABEL_OFFSET        64
496
497 static void zap_sector(ext2_filsys fs, int sect, int nsect)
498 {
499         char *buf;
500         int retval;
501         unsigned int *magic;
502
503         buf = malloc(512*nsect);
504         if (!buf) {
505                 printf(_("Out of memory erasing sectors %d-%d\n"),
506                        sect, sect + nsect - 1);
507                 exit(1);
508         }
509
510         if (sect == 0) {
511                 /* Check for a BSD disklabel, and don't erase it if so */
512                 retval = io_channel_read_blk(fs->io, 0, -512, buf);
513                 if (retval)
514                         fprintf(stderr,
515                                 _("Warning: could not read block 0: %s\n"),
516                                 error_message(retval));
517                 else {
518                         magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
519                         if ((*magic == BSD_DISKMAGIC) ||
520                             (*magic == BSD_MAGICDISK))
521                                 return;
522                 }
523         }
524
525         memset(buf, 0, 512*nsect);
526         io_channel_set_blksize(fs->io, 512);
527         retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
528         io_channel_set_blksize(fs->io, fs->blocksize);
529         free(buf);
530         if (retval)
531                 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
532                         sect, error_message(retval));
533 }
534
535 static void create_journal_dev(ext2_filsys fs)
536 {
537         struct progress_struct progress;
538         errcode_t               retval;
539         char                    *buf;
540         blk_t                   blk, err_blk;
541         int                     c, count, err_count;
542
543         retval = ext2fs_create_journal_superblock(fs,
544                                   fs->super->s_blocks_count, 0, &buf);
545         if (retval) {
546                 com_err("create_journal_dev", retval,
547                         _("while initializing journal superblock"));
548                 exit(1);
549         }
550         if (quiet)
551                 memset(&progress, 0, sizeof(progress));
552         else
553                 progress_init(&progress, _("Zeroing journal device: "),
554                               fs->super->s_blocks_count);
555
556         blk = 0;
557         count = fs->super->s_blocks_count;
558         while (count > 0) {
559                 if (count > 1024)
560                         c = 1024;
561                 else
562                         c = count;
563                 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
564                 if (retval) {
565                         com_err("create_journal_dev", retval,
566                                 _("while zeroing journal device "
567                                   "(block %u, count %d)"),
568                                 err_blk, err_count);
569                         exit(1);
570                 }
571                 blk += c;
572                 count -= c;
573                 progress_update(&progress, blk);
574         }
575         ext2fs_zero_blocks(0, 0, 0, 0, 0);
576
577         retval = io_channel_write_blk(fs->io,
578                                       fs->super->s_first_data_block+1,
579                                       1, buf);
580         if (retval) {
581                 com_err("create_journal_dev", retval,
582                         _("while writing journal superblock"));
583                 exit(1);
584         }
585         progress_close(&progress);
586 }
587
588 static void show_stats(ext2_filsys fs)
589 {
590         struct ext2_super_block *s = fs->super;
591         char                    buf[80];
592         char                    *os;
593         blk_t                   group_block;
594         dgrp_t                  i;
595         int                     need, col_left;
596         
597         if (fs_param.s_blocks_count != s->s_blocks_count)
598                 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
599                        fs_param.s_blocks_count - s->s_blocks_count);
600
601         memset(buf, 0, sizeof(buf));
602         strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
603         printf(_("Filesystem label=%s\n"), buf);
604         fputs(_("OS type: "), stdout);
605         os = e2p_os2string(fs->super->s_creator_os);
606         fputs(os, stdout);
607         free(os);
608         printf("\n");
609         printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
610                 s->s_log_block_size);
611         printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
612                 s->s_log_frag_size);
613         printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
614                s->s_blocks_count);
615         printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
616                 s->s_r_blocks_count,
617                100.0 * s->s_r_blocks_count / s->s_blocks_count);
618         printf(_("First data block=%u\n"), s->s_first_data_block);
619         if (s->s_reserved_gdt_blocks)
620                 printf(_("Maximum filesystem blocks=%lu\n"),
621                        (s->s_reserved_gdt_blocks + fs->desc_blocks) *
622                        EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
623         if (fs->group_desc_count > 1)
624                 printf(_("%u block groups\n"), fs->group_desc_count);
625         else
626                 printf(_("%u block group\n"), fs->group_desc_count);
627         printf(_("%u blocks per group, %u fragments per group\n"),
628                s->s_blocks_per_group, s->s_frags_per_group);
629         printf(_("%u inodes per group\n"), s->s_inodes_per_group);
630
631         if (fs->group_desc_count == 1) {
632                 printf("\n");
633                 return;
634         }
635
636         printf(_("Superblock backups stored on blocks: "));
637         group_block = s->s_first_data_block;
638         col_left = 0;
639         for (i = 1; i < fs->group_desc_count; i++) {
640                 group_block += s->s_blocks_per_group;
641                 if (!ext2fs_bg_has_super(fs, i))
642                         continue;
643                 if (i != 1)
644                         printf(", ");
645                 need = int_log10(group_block) + 2;
646                 if (need > col_left) {
647                         printf("\n\t");
648                         col_left = 72;
649                 }
650                 col_left -= need;
651                 printf("%u", group_block);
652         }
653         printf("\n\n");
654 }
655
656 /*
657  * Set the S_CREATOR_OS field.  Return true if OS is known,
658  * otherwise, 0.
659  */
660 static int set_os(struct ext2_super_block *sb, char *os)
661 {
662         if (isdigit (*os))
663                 sb->s_creator_os = atoi (os);
664         else if (strcasecmp(os, "linux") == 0)
665                 sb->s_creator_os = EXT2_OS_LINUX;
666         else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
667                 sb->s_creator_os = EXT2_OS_HURD;
668         else if (strcasecmp(os, "freebsd") == 0)
669                 sb->s_creator_os = EXT2_OS_FREEBSD;
670         else if (strcasecmp(os, "lites") == 0)
671                 sb->s_creator_os = EXT2_OS_LITES;
672         else
673                 return 0;
674         return 1;
675 }
676
677 #define PATH_SET "PATH=/sbin"
678
679 static void parse_extended_opts(struct ext2_super_block *param, 
680                                 const char *opts)
681 {
682         char    *buf, *token, *next, *p, *arg, *badopt = 0;
683         int     len;
684         int     r_usage = 0;
685
686         len = strlen(opts);
687         buf = malloc(len+1);
688         if (!buf) {
689                 fprintf(stderr,
690                         _("Couldn't allocate memory to parse options!\n"));
691                 exit(1);
692         }
693         strcpy(buf, opts);
694         for (token = buf; token && *token; token = next) {
695                 p = strchr(token, ',');
696                 next = 0;
697                 if (p) {
698                         *p = 0;
699                         next = p+1;
700                 }
701                 arg = strchr(token, '=');
702                 if (arg) {
703                         *arg = 0;
704                         arg++;
705                 }
706                 if (strcmp(token, "stride") == 0) {
707                         if (!arg) {
708                                 r_usage++;
709                                 badopt = token;
710                                 continue;
711                         }
712                         param->s_raid_stride = strtoul(arg, &p, 0);
713                         if (*p || (param->s_raid_stride == 0)) {
714                                 fprintf(stderr,
715                                         _("Invalid stride parameter: %s\n"),
716                                         arg);
717                                 r_usage++;
718                                 continue;
719                         }
720                 } else if (strcmp(token, "stripe-width") == 0 ||
721                            strcmp(token, "stripe_width") == 0) {
722                         if (!arg) {
723                                 r_usage++;
724                                 badopt = token;
725                                 continue;
726                         }
727                         param->s_raid_stripe_width = strtoul(arg, &p, 0);
728                         if (*p || (param->s_raid_stripe_width == 0)) {
729                                 fprintf(stderr,
730                                         _("Invalid stripe-width parameter: %s\n"),
731                                         arg);
732                                 r_usage++;
733                                 continue;
734                         }
735                 } else if (!strcmp(token, "resize")) {
736                         unsigned long resize, bpg, rsv_groups;
737                         unsigned long group_desc_count, desc_blocks;
738                         unsigned int gdpb, blocksize;
739                         int rsv_gdb;
740
741                         if (!arg) {
742                                 r_usage++;
743                                 badopt = token;
744                                 continue;
745                         }
746
747                         resize = parse_num_blocks(arg, 
748                                                   param->s_log_block_size);
749
750                         if (resize == 0) {
751                                 fprintf(stderr, 
752                                         _("Invalid resize parameter: %s\n"),
753                                         arg);
754                                 r_usage++;
755                                 continue;
756                         }
757                         if (resize <= param->s_blocks_count) {
758                                 fprintf(stderr, 
759                                         _("The resize maximum must be greater "
760                                           "than the filesystem size.\n"));
761                                 r_usage++;
762                                 continue;
763                         }
764
765                         blocksize = EXT2_BLOCK_SIZE(param);
766                         bpg = param->s_blocks_per_group;
767                         if (!bpg)
768                                 bpg = blocksize * 8;
769                         gdpb = EXT2_DESC_PER_BLOCK(param);
770                         group_desc_count = 
771                                 ext2fs_div_ceil(param->s_blocks_count, bpg);
772                         desc_blocks = (group_desc_count +
773                                        gdpb - 1) / gdpb;
774                         rsv_groups = ext2fs_div_ceil(resize, bpg);
775                         rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) - 
776                                 desc_blocks;
777                         if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
778                                 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
779
780                         if (rsv_gdb > 0) {
781                                 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
782                                         fprintf(stderr, 
783         _("On-line resizing not supported with revision 0 filesystems\n"));
784                                         free(buf);
785                                         exit(1);
786                                 }
787                                 param->s_feature_compat |=
788                                         EXT2_FEATURE_COMPAT_RESIZE_INODE;
789
790                                 param->s_reserved_gdt_blocks = rsv_gdb;
791                         }
792                 } else if (!strcmp(token, "test_fs")) {
793                         param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
794                 } else if (!strcmp(token, "lazy_itable_init")) {
795                         if (arg)
796                                 lazy_itable_init = strtoul(arg, &p, 0);
797                         else
798                                 lazy_itable_init = 1;
799                 } else {
800                         r_usage++;
801                         badopt = token;
802                 }
803         }
804         if (r_usage) {
805                 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
806                         "Extended options are separated by commas, "
807                         "and may take an argument which\n"
808                         "\tis set off by an equals ('=') sign.\n\n"
809                         "Valid extended options are:\n"
810                         "\tstride=<RAID per-disk data chunk in blocks>\n"
811                         "\tstripe-width=<RAID stride * data disks in blocks>\n"
812                         "\tresize=<resize maximum size in blocks>\n"
813                         "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
814                         "\ttest_fs\n\n"),
815                         badopt ? badopt : "");
816                 free(buf);
817                 exit(1);
818         }
819         if (param->s_raid_stride &&
820             (param->s_raid_stripe_width % param->s_raid_stride) != 0)
821                 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
822                                   "multiple of stride %u.\n\n"),
823                         param->s_raid_stripe_width, param->s_raid_stride);
824
825         free(buf);
826 }       
827
828 static __u32 ok_features[3] = {
829         /* Compat */
830         EXT3_FEATURE_COMPAT_HAS_JOURNAL |
831                 EXT2_FEATURE_COMPAT_RESIZE_INODE |
832                 EXT2_FEATURE_COMPAT_DIR_INDEX |
833                 EXT2_FEATURE_COMPAT_EXT_ATTR,
834         /* Incompat */
835         EXT2_FEATURE_INCOMPAT_FILETYPE|
836                 EXT3_FEATURE_INCOMPAT_EXTENTS|
837                 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
838                 EXT2_FEATURE_INCOMPAT_META_BG|
839                 EXT4_FEATURE_INCOMPAT_FLEX_BG,
840         /* R/O compat */
841         EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
842                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
843                 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
844 };
845
846
847 static void syntax_err_report(const char *filename, long err, int line_num)
848 {
849         fprintf(stderr, 
850                 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
851                 filename, line_num, error_message(err));
852         exit(1);
853 }
854
855 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
856
857 static void edit_feature(const char *str, __u32 *compat_array) 
858 {
859         if (!str)
860                 return;
861
862         if (e2p_edit_feature(str, compat_array, ok_features)) {
863                 fprintf(stderr, _("Invalid filesystem option set: %s\n"), 
864                         str);
865                 exit(1);
866         }
867 }
868
869 struct str_list {
870         char **list;
871         int num;
872         int max;
873 };
874
875 static errcode_t init_list(struct str_list *sl)
876 {
877         sl->num = 0;
878         sl->max = 0;
879         sl->list = malloc((sl->max+1) * sizeof(char *));
880         if (!sl->list)
881                 return ENOMEM;
882         sl->list[0] = 0;
883         return 0;
884 }
885
886 static errcode_t push_string(struct str_list *sl, const char *str)
887 {
888         char **new_list;
889
890         if (sl->num >= sl->max) {
891                 sl->max += 2;
892                 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
893                 if (!new_list)
894                         return ENOMEM;
895                 sl->list = new_list;
896         }
897         sl->list[sl->num] = malloc(strlen(str)+1);
898         if (sl->list[sl->num] == 0)
899                 return ENOMEM;
900         strcpy(sl->list[sl->num], str);
901         sl->num++;
902         sl->list[sl->num] = 0;
903         return 0;
904 }
905
906 static void print_str_list(char **list)
907 {
908         char **cpp;
909
910         for (cpp = list; *cpp; cpp++) {
911                 printf("'%s'", *cpp);
912                 if (cpp[1])
913                         fputs(", ", stdout);
914         }
915         fputc('\n', stdout);
916 }
917
918 static char **parse_fs_type(const char *fs_type,
919                             const char *usage_types,
920                             struct ext2_super_block *fs_param,
921                             char *progname)
922 {
923         const char      *ext_type = 0;
924         char            *parse_str;
925         char            *profile_type = 0;
926         char            *cp, *t;
927         const char      *size_type;
928         struct str_list list;
929         unsigned long   meg;
930         int             is_hurd = 0;
931
932         if (init_list(&list))
933                 return 0;
934
935         if (creator_os && (!strcasecmp(creator_os, "GNU") ||
936                            !strcasecmp(creator_os, "hurd")))
937                 is_hurd = 1;
938
939         if (fs_type)
940                 ext_type = fs_type;
941         else if (is_hurd)
942                 ext_type = "ext2";
943         else if (progname) {
944                 ext_type = strrchr(progname, '/');
945                 if (ext_type)
946                         ext_type++;
947                 else
948                         ext_type = progname;
949
950                 if (!strncmp(ext_type, "mkfs.", 5)) {
951                         ext_type += 5;
952                         if (ext_type[0] == 0)
953                                 ext_type = 0;
954                 } else
955                         ext_type = 0;
956         }
957
958         if (!ext_type) {
959                 profile_get_string(profile, "defaults", "fs_type", 0,
960                                    "ext2", &profile_type);
961                 ext_type = profile_type;
962                 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
963                         ext_type = "ext3";
964         }
965
966         meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
967         if (fs_param->s_blocks_count < 3 * meg)
968                 size_type = "floppy";
969         else if (fs_param->s_blocks_count < 512 * meg)
970                 size_type = "small";
971         else
972                 size_type = "default";
973
974         if (!usage_types)
975                 usage_types = size_type;
976
977         parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
978         if (!parse_str) {
979                 free(list.list);
980                 return 0;
981         }
982         if (usage_types)
983                 strcpy(parse_str, usage_types);
984         else
985                 *parse_str = '\0';
986
987         if (ext_type)
988                 push_string(&list, ext_type);
989         cp = parse_str;
990         while (1) {
991                 t = strchr(cp, ',');
992                 if (t)
993                         *t = '\0';
994
995                 if (*cp)
996                         push_string(&list, cp);
997                 if (t)
998                         cp = t+1;
999                 else {
1000                         cp = "";
1001                         break;
1002                 }
1003         }
1004         free(parse_str);
1005         if (profile_type)
1006                 free(profile_type);
1007         if (is_hurd)
1008                 push_string(&list, "hurd");
1009         return (list.list);
1010 }
1011
1012 static char *get_string_from_profile(char **fs_types, const char *opt,
1013                                      const char *def_val)
1014 {
1015         char *ret = 0;
1016         int i;
1017
1018         for (i=0; fs_types[i]; i++);
1019         for (i-=1; i >=0 ; i--) {
1020                 profile_get_string(profile, "fs_types", fs_types[i],
1021                                    opt, 0, &ret);
1022                 if (ret)
1023                         return ret;
1024         }
1025         profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1026         return (ret);
1027 }
1028
1029 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1030 {
1031         int ret;
1032         char **cpp;
1033
1034         profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1035         for (cpp = fs_types; *cpp; cpp++)
1036                 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1037         return ret;
1038 }
1039
1040 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1041 {
1042         int ret;
1043         char **cpp;
1044
1045         profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1046         for (cpp = fs_types; *cpp; cpp++)
1047                 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1048         return ret;
1049 }
1050
1051 extern const char *mke2fs_default_profile;
1052 static const char *default_files[] = { "<default>", 0 };
1053
1054 static void PRS(int argc, char *argv[])
1055 {
1056         int             b, c;
1057         int             size;
1058         char            *tmp, **cpp;
1059         int             blocksize = 0;
1060         int             inode_ratio = 0;
1061         int             inode_size = 0;
1062         unsigned long   flex_bg_size = 0;
1063         double          reserved_ratio = 5.0;
1064         int             sector_size = 0;
1065         int             show_version_only = 0;
1066         unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1067         errcode_t       retval;
1068         char *          oldpath = getenv("PATH");
1069         char *          extended_opts = 0;
1070         const char *    fs_type = 0;
1071         const char *    usage_types = 0;
1072         blk_t           dev_size;
1073 #ifdef __linux__
1074         struct          utsname ut;
1075 #endif
1076         long            sysval;
1077         int             s_opt = -1, r_opt = -1;
1078         char            *fs_features = 0;
1079         int             use_bsize;
1080         char            *newpath;
1081         int             pathlen = sizeof(PATH_SET) + 1;
1082
1083         if (oldpath)
1084                 pathlen += strlen(oldpath);
1085         newpath = malloc(pathlen);
1086         strcpy(newpath, PATH_SET);
1087
1088         /* Update our PATH to include /sbin  */
1089         if (oldpath) {
1090                 strcat (newpath, ":");
1091                 strcat (newpath, oldpath);
1092         }
1093         putenv (newpath);
1094
1095         tmp = getenv("MKE2FS_SYNC");
1096         if (tmp)
1097                 sync_kludge = atoi(tmp);
1098
1099         /* Determine the system page size if possible */
1100 #ifdef HAVE_SYSCONF
1101 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1102 #define _SC_PAGESIZE _SC_PAGE_SIZE
1103 #endif
1104 #ifdef _SC_PAGESIZE
1105         sysval = sysconf(_SC_PAGESIZE);
1106         if (sysval > 0)
1107                 sys_page_size = sysval;
1108 #endif /* _SC_PAGESIZE */
1109 #endif /* HAVE_SYSCONF */
1110
1111         if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1112                 config_fn[0] = tmp;
1113         profile_set_syntax_err_cb(syntax_err_report);
1114         retval = profile_init(config_fn, &profile);
1115         if (retval == ENOENT) {
1116                 profile_init(default_files, &profile);
1117                 profile_set_default(profile, mke2fs_default_profile);
1118         }
1119         
1120         setbuf(stdout, NULL);
1121         setbuf(stderr, NULL);
1122         add_error_table(&et_ext2_error_table);
1123         add_error_table(&et_prof_error_table);
1124         memset(&fs_param, 0, sizeof(struct ext2_super_block));
1125         fs_param.s_rev_level = 1;  /* Create revision 1 filesystems now */
1126
1127 #ifdef __linux__
1128         if (uname(&ut)) {
1129                 perror("uname");
1130                 exit(1);
1131         }
1132         linux_version_code = parse_version_number(ut.release);
1133         if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1134                 fs_param.s_rev_level = 0;
1135 #endif
1136
1137         if (argc && *argv) {
1138                 program_name = get_progname(*argv);
1139
1140                 /* If called as mkfs.ext3, create a journal inode */
1141                 if (!strcmp(program_name, "mkfs.ext3"))
1142                         journal_size = -1;
1143         }
1144
1145         while ((c = getopt (argc, argv,
1146                     "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
1147                 switch (c) {
1148                 case 'b':
1149                         blocksize = strtol(optarg, &tmp, 0);
1150                         b = (blocksize > 0) ? blocksize : -blocksize;
1151                         if (b < EXT2_MIN_BLOCK_SIZE ||
1152                             b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1153                                 com_err(program_name, 0,
1154                                         _("invalid block size - %s"), optarg);
1155                                 exit(1);
1156                         }
1157                         if (blocksize > 4096)
1158                                 fprintf(stderr, _("Warning: blocksize %d not "
1159                                                   "usable on most systems.\n"),
1160                                         blocksize);
1161                         if (blocksize > 0) 
1162                                 fs_param.s_log_block_size =
1163                                         int_log2(blocksize >>
1164                                                  EXT2_MIN_BLOCK_LOG_SIZE);
1165                         break;
1166                 case 'c':       /* Check for bad blocks */
1167                         cflag++;
1168                         break;
1169                 case 'f':
1170                         size = strtoul(optarg, &tmp, 0);
1171                         if (size < EXT2_MIN_BLOCK_SIZE ||
1172                             size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1173                                 com_err(program_name, 0,
1174                                         _("invalid fragment size - %s"),
1175                                         optarg);
1176                                 exit(1);
1177                         }
1178                         fs_param.s_log_frag_size =
1179                                 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1180                         fprintf(stderr, _("Warning: fragments not supported.  "
1181                                "Ignoring -f option\n"));
1182                         break;
1183                 case 'g':
1184                         fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1185                         if (*tmp) {
1186                                 com_err(program_name, 0,
1187                                         _("Illegal number for blocks per group"));
1188                                 exit(1);
1189                         }
1190                         if ((fs_param.s_blocks_per_group % 8) != 0) {
1191                                 com_err(program_name, 0,
1192                                 _("blocks per group must be multiple of 8"));
1193                                 exit(1);
1194                         }
1195                         break;
1196                 case 'G':
1197                         flex_bg_size = strtoul(optarg, &tmp, 0);
1198                         if (*tmp) {
1199                                 com_err(program_name, 0,
1200                                         _("Illegal number for flex_bg size"));
1201                                 exit(1);
1202                         }
1203                         if (flex_bg_size < 2 ||
1204                             (flex_bg_size & (flex_bg_size-1)) != 0) {
1205                                 com_err(program_name, 0,
1206                                         _("flex_bg size must be a power of 2"));
1207                                 exit(1);
1208                         }
1209                         break;
1210                 case 'i':
1211                         inode_ratio = strtoul(optarg, &tmp, 0);
1212                         if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1213                             inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1214                             *tmp) {
1215                                 com_err(program_name, 0,
1216                                         _("invalid inode ratio %s (min %d/max %d)"),
1217                                         optarg, EXT2_MIN_BLOCK_SIZE,
1218                                         EXT2_MAX_BLOCK_SIZE);
1219                                 exit(1);
1220                         }
1221                         break;
1222                 case 'J':
1223                         parse_journal_opts(optarg);
1224                         break;
1225                 case 'j':
1226                         if (!journal_size)
1227                                 journal_size = -1;
1228                         break;
1229                 case 'l':
1230                         bad_blocks_filename = malloc(strlen(optarg)+1);
1231                         if (!bad_blocks_filename) {
1232                                 com_err(program_name, ENOMEM,
1233                                         _("in malloc for bad_blocks_filename"));
1234                                 exit(1);
1235                         }
1236                         strcpy(bad_blocks_filename, optarg);
1237                         break;
1238                 case 'm':
1239                         reserved_ratio = strtod(optarg, &tmp);
1240                         if (reserved_ratio > 50 || *tmp) {
1241                                 com_err(program_name, 0,
1242                                         _("invalid reserved blocks percent - %s"),
1243                                         optarg);
1244                                 exit(1);
1245                         }
1246                         break;
1247                 case 'n':
1248                         noaction++;
1249                         break;
1250                 case 'o':
1251                         creator_os = optarg;
1252                         break;
1253                 case 'q':
1254                         quiet = 1;
1255                         break;
1256                 case 'r':
1257                         r_opt = strtoul(optarg, &tmp, 0);
1258                         if (*tmp) {
1259                                 com_err(program_name, 0,
1260                                         _("bad revision level - %s"), optarg);
1261                                 exit(1);
1262                         }
1263                         fs_param.s_rev_level = r_opt;
1264                         break;
1265                 case 's':       /* deprecated */
1266                         s_opt = atoi(optarg);
1267                         break;
1268                 case 'I':
1269                         inode_size = strtoul(optarg, &tmp, 0);
1270                         if (*tmp) {
1271                                 com_err(program_name, 0,
1272                                         _("invalid inode size - %s"), optarg);
1273                                 exit(1);
1274                         }
1275                         break;
1276                 case 'v':
1277                         verbose = 1;
1278                         break;
1279                 case 'F':
1280                         force++;
1281                         break;
1282                 case 'L':
1283                         volume_label = optarg;
1284                         break;
1285                 case 'M':
1286                         mount_dir = optarg;
1287                         break;
1288                 case 'N':
1289                         num_inodes = strtoul(optarg, &tmp, 0);
1290                         if (*tmp) {
1291                                 com_err(program_name, 0,
1292                                         _("bad num inodes - %s"), optarg);
1293                                         exit(1);
1294                         }
1295                         break;
1296                 case 'O':
1297                         fs_features = optarg;
1298                         break;
1299                 case 'E':
1300                 case 'R':
1301                         extended_opts = optarg;
1302                         break;
1303                 case 'S':
1304                         super_only = 1;
1305                         break;
1306                 case 't':
1307                         fs_type = optarg;
1308                         break;
1309                 case 'T':
1310                         usage_types = optarg;
1311                         break;
1312                 case 'V':
1313                         /* Print version number and exit */
1314                         show_version_only++;
1315                         break;
1316                 default:
1317                         usage();
1318                 }
1319         }
1320         if ((optind == argc) && !show_version_only)
1321                 usage();
1322         device_name = argv[optind++];
1323
1324         if (!quiet || show_version_only)
1325                 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION, 
1326                          E2FSPROGS_DATE);
1327
1328         if (show_version_only) {
1329                 fprintf(stderr, _("\tUsing %s\n"), 
1330                         error_message(EXT2_ET_BASE));
1331                 exit(0);
1332         }
1333
1334         /*
1335          * If there's no blocksize specified and there is a journal
1336          * device, use it to figure out the blocksize
1337          */
1338         if (blocksize <= 0 && journal_device) {
1339                 ext2_filsys     jfs;
1340                 io_manager      io_ptr;
1341
1342 #ifdef CONFIG_TESTIO_DEBUG
1343                 io_ptr = test_io_manager;
1344                 test_io_backing_manager = unix_io_manager;
1345 #else
1346                 io_ptr = unix_io_manager;
1347 #endif
1348                 retval = ext2fs_open(journal_device,
1349                                      EXT2_FLAG_JOURNAL_DEV_OK, 0,
1350                                      0, io_ptr, &jfs);
1351                 if (retval) {
1352                         com_err(program_name, retval,
1353                                 _("while trying to open journal device %s\n"),
1354                                 journal_device);
1355                         exit(1);
1356                 }
1357                 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1358                         com_err(program_name, 0,
1359                                 _("Journal dev blocksize (%d) smaller than "
1360                                   "minimum blocksize %d\n"), jfs->blocksize,
1361                                 -blocksize);
1362                         exit(1);
1363                 }
1364                 blocksize = jfs->blocksize;
1365                 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1366                 fs_param.s_log_block_size =
1367                         int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1368                 ext2fs_close(jfs);
1369         }
1370
1371         if (blocksize > sys_page_size) {
1372                 if (!force) {
1373                         com_err(program_name, 0,
1374                                 _("%d-byte blocks too big for system (max %d)"),
1375                                 blocksize, sys_page_size);
1376                         proceed_question();
1377                 }
1378                 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1379                                   "(max %d), forced to continue\n"),
1380                         blocksize, sys_page_size);
1381         }
1382         if (optind < argc) {
1383                 fs_param.s_blocks_count = parse_num_blocks(argv[optind++], 
1384                                 fs_param.s_log_block_size);
1385                 if (!fs_param.s_blocks_count) {
1386                         com_err(program_name, 0, _("invalid blocks count - %s"),
1387                                 argv[optind - 1]);
1388                         exit(1);
1389                 }
1390         }
1391         if (optind < argc)
1392                 usage();
1393
1394         if (!force)
1395                 check_plausibility(device_name);
1396         check_mount(device_name, force, _("filesystem"));
1397
1398         fs_param.s_log_frag_size = fs_param.s_log_block_size;
1399
1400         if (noaction && fs_param.s_blocks_count) {
1401                 dev_size = fs_param.s_blocks_count;
1402                 retval = 0;
1403         } else {
1404         retry:
1405                 retval = ext2fs_get_device_size(device_name,
1406                                                 EXT2_BLOCK_SIZE(&fs_param),
1407                                                 &dev_size);
1408                 if ((retval == EFBIG) &&
1409                     (blocksize == 0) && 
1410                     (fs_param.s_log_block_size == 0)) {
1411                         fs_param.s_log_block_size = 2;
1412                         blocksize = 4096;
1413                         goto retry;
1414                 }
1415         }
1416                         
1417         if (retval == EFBIG) {
1418                 fprintf(stderr, _("%s: Size of device %s too big "
1419                                   "to be expressed in 32 bits\n\t"
1420                                   "using a blocksize of %d.\n"),
1421                         program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1422                 exit(1);
1423         }
1424         if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1425                 com_err(program_name, retval,
1426                         _("while trying to determine filesystem size"));
1427                 exit(1);
1428         }
1429         if (!fs_param.s_blocks_count) {
1430                 if (retval == EXT2_ET_UNIMPLEMENTED) {
1431                         com_err(program_name, 0,
1432                                 _("Couldn't determine device size; you "
1433                                 "must specify\nthe size of the "
1434                                 "filesystem\n"));
1435                         exit(1);
1436                 } else {
1437                         if (dev_size == 0) {
1438                                 com_err(program_name, 0,
1439                                 _("Device size reported to be zero.  "
1440                                   "Invalid partition specified, or\n\t"
1441                                   "partition table wasn't reread "
1442                                   "after running fdisk, due to\n\t"
1443                                   "a modified partition being busy "
1444                                   "and in use.  You may need to reboot\n\t"
1445                                   "to re-read your partition table.\n"
1446                                   ));
1447                                 exit(1);
1448                         }
1449                         fs_param.s_blocks_count = dev_size;
1450                         if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1451                                 fs_param.s_blocks_count &= ~((sys_page_size /
1452                                            EXT2_BLOCK_SIZE(&fs_param))-1);
1453                 }
1454                 
1455         } else if (!force && (fs_param.s_blocks_count > dev_size)) {
1456                 com_err(program_name, 0,
1457                         _("Filesystem larger than apparent device size."));
1458                 proceed_question();
1459         }
1460
1461         fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1462         if (!fs_types) {
1463                 fprintf(stderr, _("Failed to parse fs types list\n"));
1464                 exit(1);
1465         }
1466
1467         /* Figure out what features should be enabled */
1468
1469         tmp = NULL;
1470         if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1471                 tmp = get_string_from_profile(fs_types, "base_features",
1472                       "sparse_super,filetype,resize_inode,dir_index");
1473                 edit_feature(tmp, &fs_param.s_feature_compat);
1474                 free(tmp);
1475
1476                 for (cpp = fs_types; *cpp; cpp++) {
1477                         tmp = NULL;
1478                         profile_get_string(profile, "fs_types", *cpp,
1479                                            "features", "", &tmp);
1480                         if (tmp && *tmp)
1481                                 edit_feature(tmp, &fs_param.s_feature_compat);
1482                         if (tmp)
1483                                 free(tmp);
1484                 }
1485                 tmp = get_string_from_profile(fs_types, "default_features",
1486                                               "");
1487         }
1488         edit_feature(fs_features ? fs_features : tmp,
1489                      &fs_param.s_feature_compat);
1490         if (tmp)
1491                 free(tmp);
1492
1493         if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1494                 fs_types[0] = strdup("journal");
1495                 fs_types[1] = 0;
1496         }
1497
1498         if (verbose) {
1499                 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1500                 print_str_list(fs_types);
1501         }
1502
1503         if (r_opt == EXT2_GOOD_OLD_REV && 
1504             (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1505              fs_param.s_feature_incompat)) {
1506                 fprintf(stderr, _("Filesystem features not supported "
1507                                   "with revision 0 filesystems\n"));
1508                 exit(1);
1509         }
1510
1511         if (s_opt > 0) {
1512                 if (r_opt == EXT2_GOOD_OLD_REV) {
1513                         fprintf(stderr, _("Sparse superblocks not supported "
1514                                   "with revision 0 filesystems\n"));
1515                         exit(1);
1516                 }
1517                 fs_param.s_feature_ro_compat |=
1518                         EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1519         } else if (s_opt == 0)
1520                 fs_param.s_feature_ro_compat &=
1521                         ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1522
1523         if (journal_size != 0) {
1524                 if (r_opt == EXT2_GOOD_OLD_REV) {
1525                         fprintf(stderr, _("Journals not supported "
1526                                   "with revision 0 filesystems\n"));
1527                         exit(1);
1528                 }
1529                 fs_param.s_feature_compat |= 
1530                         EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1531         }
1532
1533         if (fs_param.s_feature_incompat & 
1534             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1535                 reserved_ratio = 0;
1536                 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1537                 fs_param.s_feature_compat = 0;
1538                 fs_param.s_feature_ro_compat = 0;
1539         }
1540
1541         if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1542             (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1543                 fprintf(stderr, _("The resize_inode and meta_bg features "
1544                                   "are not compatible.\n"
1545                                   "They can not be both enabled "
1546                                   "simultaneously.\n"));
1547                 exit(1);
1548         }
1549
1550         /* Set first meta blockgroup via an environment variable */
1551         /* (this is mostly for debugging purposes) */
1552         if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1553             ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1554                 fs_param.s_first_meta_bg = atoi(tmp);
1555
1556         /* Get the hardware sector size, if available */
1557         retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1558         if (retval) {
1559                 com_err(program_name, retval,
1560                         _("while trying to determine hardware sector size"));
1561                 exit(1);
1562         }
1563
1564         if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1565                 sector_size = atoi(tmp);
1566         
1567         if (blocksize <= 0) {
1568                 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1569
1570                 if (use_bsize == -1) {
1571                         use_bsize = sys_page_size;
1572                         if ((linux_version_code < (2*65536 + 6*256)) &&
1573                             (use_bsize > 4096))
1574                                 use_bsize = 4096;
1575                 }
1576                 if (sector_size && use_bsize < sector_size)
1577                         use_bsize = sector_size;
1578                 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1579                         use_bsize = -blocksize;
1580                 blocksize = use_bsize;
1581                 fs_param.s_blocks_count /= blocksize / 1024;
1582         }
1583
1584         if (inode_ratio == 0) {
1585                 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1586                                                    8192);
1587                 if (inode_ratio < blocksize)
1588                         inode_ratio = blocksize;
1589         }
1590
1591         fs_param.s_log_frag_size = fs_param.s_log_block_size =
1592                 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1593
1594         blocksize = EXT2_BLOCK_SIZE(&fs_param);
1595
1596         lazy_itable_init = get_bool_from_profile(fs_types, 
1597                                                  "lazy_itable_init", 0);
1598         
1599         /* Get options from profile */
1600         for (cpp = fs_types; *cpp; cpp++) {
1601                 tmp = NULL;
1602                 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1603                         if (tmp && *tmp)
1604                                 parse_extended_opts(&fs_param, tmp);
1605                         if (tmp)
1606                                 free(tmp);
1607         }
1608
1609         if (extended_opts)
1610                 parse_extended_opts(&fs_param, extended_opts);
1611
1612         /* Since sparse_super is the default, we would only have a problem
1613          * here if it was explicitly disabled.
1614          */
1615         if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1616             !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1617                 com_err(program_name, 0,
1618                         _("reserved online resize blocks not supported "
1619                           "on non-sparse filesystem"));
1620                 exit(1);
1621         }
1622
1623         if (fs_param.s_blocks_per_group) {
1624                 if (fs_param.s_blocks_per_group < 256 ||
1625                     fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1626                         com_err(program_name, 0,
1627                                 _("blocks per group count out of range"));
1628                         exit(1);
1629                 }
1630         }
1631
1632         if (inode_size == 0)
1633                 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1634         if (!flex_bg_size && (fs_param.s_feature_incompat &
1635                               EXT4_FEATURE_INCOMPAT_FLEX_BG))
1636                 flex_bg_size = get_int_from_profile(fs_types,
1637                                                     "flex_bg_size", 16);
1638         if (flex_bg_size) {
1639                 if (!(fs_param.s_feature_incompat &
1640                       EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1641                         com_err(program_name, 0,
1642                                 _("Flex_bg feature not enabled, so "
1643                                   "flex_bg size may not be specified"));
1644                         exit(1);
1645                 }
1646                 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1647         }
1648
1649         if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1650                 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1651                     inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1652                     inode_size & (inode_size - 1)) {
1653                         com_err(program_name, 0,
1654                                 _("invalid inode size %d (min %d/max %d)"),
1655                                 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1656                                 blocksize);
1657                         exit(1);
1658                 }
1659                 fs_param.s_inode_size = inode_size;
1660         }
1661
1662         /* Make sure number of inodes specified will fit in 32 bits */
1663         if (num_inodes == 0) {
1664                 unsigned long long n;
1665                 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
1666                 if (n > ~0U) {
1667                         com_err(program_name, 0,
1668                             _("too many inodes (%llu), raise inode ratio?"), n);
1669                         exit(1);
1670                 }
1671         } else if (num_inodes > ~0U) {
1672                 com_err(program_name, 0,
1673                         _("too many inodes (%llu), specify < 2^32 inodes"),
1674                           num_inodes);
1675                 exit(1);
1676         }
1677         /*
1678          * Calculate number of inodes based on the inode ratio
1679          */
1680         fs_param.s_inodes_count = num_inodes ? num_inodes : 
1681                 ((__u64) fs_param.s_blocks_count * blocksize)
1682                         / inode_ratio;
1683
1684         if ((((long long)fs_param.s_inodes_count) *
1685              (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1686             (((long long)fs_param.s_blocks_count) * 
1687              EXT2_BLOCK_SIZE(&fs_param))) {
1688                 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1689                                           "(%u) too big for a\n\t"
1690                                           "filesystem with %lu blocks, "
1691                                           "specify higher inode_ratio (-i)\n\t"
1692                                           "or lower inode count (-N).\n"),
1693                         inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1694                         fs_param.s_inodes_count, 
1695                         (unsigned long) fs_param.s_blocks_count);
1696                 exit(1);
1697         }
1698
1699         /*
1700          * Calculate number of blocks to reserve
1701          */
1702         fs_param.s_r_blocks_count = (unsigned int) (reserved_ratio *
1703                                         fs_param.s_blocks_count / 100.0);
1704 }
1705
1706 static int should_do_undo(const char *name)
1707 {
1708         errcode_t retval;
1709         io_channel channel;
1710         __u16   s_magic;
1711         struct ext2_super_block super;
1712         io_manager manager = unix_io_manager;
1713         int csum_flag, force_undo;
1714
1715         csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1716                                                EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1717         force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1718         if (!force_undo && (!csum_flag || !lazy_itable_init))
1719                 return 0;
1720
1721         retval = manager->open(name, IO_FLAG_EXCLUSIVE,  &channel);
1722         if (retval) {
1723                 /*
1724                  * We don't handle error cases instead we
1725                  * declare that the file system doesn't exist
1726                  * and let the rest of mke2fs take care of
1727                  * error
1728                  */
1729                 retval = 0;
1730                 goto open_err_out;
1731         }
1732
1733         io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1734         retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
1735         if (retval) {
1736                 retval = 0;
1737                 goto err_out;
1738         }
1739
1740 #if defined(WORDS_BIGENDIAN)
1741         s_magic = ext2fs_swab16(super.s_magic);
1742 #else
1743         s_magic = super.s_magic;
1744 #endif
1745
1746         if (s_magic == EXT2_SUPER_MAGIC)
1747                 retval = 1;
1748
1749 err_out:
1750         io_channel_close(channel);
1751
1752 open_err_out:
1753
1754         return retval;
1755 }
1756
1757 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1758 {
1759         errcode_t retval = 0;
1760         char *tdb_dir, tdb_file[PATH_MAX];
1761         char *device_name, *tmp_name;
1762
1763         /*
1764          * Configuration via a conf file would be
1765          * nice
1766          */
1767         tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1768         if (!tdb_dir)
1769                 profile_get_string(profile, "defaults",
1770                                    "undo_dir", 0, "/var/lib/e2fsprogs",
1771                                    &tdb_dir);
1772
1773         if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1774             access(tdb_dir, W_OK))
1775                 return 0;
1776
1777         tmp_name = strdup(name);
1778         device_name = basename(tmp_name);
1779         sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1780
1781         if (!access(tdb_file, F_OK)) {
1782                 if (unlink(tdb_file) < 0) {
1783                         retval = errno;
1784                         com_err(program_name, retval,
1785                                 _("while trying to delete %s"),
1786                                 tdb_file);
1787                         return retval;
1788                 }
1789         }
1790
1791         set_undo_io_backing_manager(*io_ptr);
1792         *io_ptr = undo_io_manager;
1793         set_undo_io_backup_file(tdb_file);
1794         printf(_("Overwriting existing filesystem; this can be undone "
1795                  "using the command:\n"
1796                  "    e2undo %s %s\n\n"), tdb_file, name);
1797
1798         free(tmp_name);
1799         return retval;
1800 }
1801
1802 int main (int argc, char *argv[])
1803 {
1804         errcode_t       retval = 0;
1805         ext2_filsys     fs;
1806         badblocks_list  bb_list = 0;
1807         unsigned int    journal_blocks;
1808         unsigned int    i;
1809         int             val;
1810         io_manager      io_ptr;
1811         char            tdb_string[40];
1812
1813 #ifdef ENABLE_NLS
1814         setlocale(LC_MESSAGES, "");
1815         setlocale(LC_CTYPE, "");
1816         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1817         textdomain(NLS_CAT_NAME);
1818 #endif
1819         PRS(argc, argv);
1820
1821 #ifdef CONFIG_TESTIO_DEBUG
1822         io_ptr = test_io_manager;
1823         test_io_backing_manager = unix_io_manager;
1824 #else
1825         io_ptr = unix_io_manager;
1826 #endif
1827
1828         if (should_do_undo(device_name)) {
1829                 retval = mke2fs_setup_tdb(device_name, &io_ptr);
1830                 if (retval)
1831                         exit(1);
1832         }
1833
1834         /*
1835          * Initialize the superblock....
1836          */
1837         retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
1838                                    io_ptr, &fs);
1839         if (retval) {
1840                 com_err(device_name, retval, _("while setting up superblock"));
1841                 exit(1);
1842         }
1843         sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1844                 32768 : fs->blocksize * 8);
1845         io_channel_set_options(fs->io, tdb_string);
1846
1847         if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1848                 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1849
1850         /*
1851          * Wipe out the old on-disk superblock
1852          */
1853         if (!noaction)
1854                 zap_sector(fs, 2, 6);
1855
1856         /*
1857          * Generate a UUID for it...
1858          */
1859         uuid_generate(fs->super->s_uuid);
1860
1861         /*
1862          * Initialize the directory index variables
1863          */
1864         fs->super->s_def_hash_version = EXT2_HASH_TEA;
1865         uuid_generate((unsigned char *) fs->super->s_hash_seed);
1866
1867         /*
1868          * Add "jitter" to the superblock's check interval so that we
1869          * don't check all the filesystems at the same time.  We use a
1870          * kludgy hack of using the UUID to derive a random jitter value.
1871          */
1872         for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1873                 val += fs->super->s_uuid[i];
1874         fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1875
1876         /*
1877          * Override the creator OS, if applicable
1878          */
1879         if (creator_os && !set_os(fs->super, creator_os)) {
1880                 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1881                 exit(1);
1882         }
1883
1884         /*
1885          * For the Hurd, we will turn off filetype since it doesn't
1886          * support it.
1887          */
1888         if (fs->super->s_creator_os == EXT2_OS_HURD)
1889                 fs->super->s_feature_incompat &=
1890                         ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1891
1892         /*
1893          * Set the volume label...
1894          */
1895         if (volume_label) {
1896                 memset(fs->super->s_volume_name, 0,
1897                        sizeof(fs->super->s_volume_name));
1898                 strncpy(fs->super->s_volume_name, volume_label,
1899                         sizeof(fs->super->s_volume_name));
1900         }
1901
1902         /*
1903          * Set the last mount directory
1904          */
1905         if (mount_dir) {
1906                 memset(fs->super->s_last_mounted, 0,
1907                        sizeof(fs->super->s_last_mounted));
1908                 strncpy(fs->super->s_last_mounted, mount_dir,
1909                         sizeof(fs->super->s_last_mounted));
1910         }
1911         
1912         if (!quiet || noaction)
1913                 show_stats(fs);
1914
1915         if (noaction)
1916                 exit(0);
1917
1918         if (fs->super->s_feature_incompat &
1919             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1920                 create_journal_dev(fs);
1921                 exit(ext2fs_close(fs) ? 1 : 0);
1922         }
1923
1924         if (bad_blocks_filename)
1925                 read_bb_file(fs, &bb_list, bad_blocks_filename);
1926         if (cflag)
1927                 test_disk(fs, &bb_list);
1928
1929         handle_bad_blocks(fs, bb_list);
1930         fs->stride = fs_stride = fs->super->s_raid_stride;
1931         retval = ext2fs_allocate_tables(fs);
1932         if (retval) {
1933                 com_err(program_name, retval,
1934                         _("while trying to allocate filesystem tables"));
1935                 exit(1);
1936         }
1937         if (super_only) {
1938                 fs->super->s_state |= EXT2_ERROR_FS;
1939                 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1940         } else {
1941                 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1942                 unsigned int rsv = 65536 / fs->blocksize;
1943                 unsigned long blocks = fs->super->s_blocks_count;
1944                 unsigned long start;
1945                 blk_t ret_blk;
1946
1947 #ifdef ZAP_BOOTBLOCK
1948                 zap_sector(fs, 0, 2);
1949 #endif
1950
1951                 /*
1952                  * Wipe out any old MD RAID (or other) metadata at the end
1953                  * of the device.  This will also verify that the device is
1954                  * as large as we think.  Be careful with very small devices.
1955                  */
1956                 start = (blocks & ~(rsv - 1));
1957                 if (start > rsv)
1958                         start -= rsv;
1959                 if (start > 0)
1960                         retval = ext2fs_zero_blocks(fs, start, blocks - start,
1961                                                     &ret_blk, NULL);
1962
1963                 if (retval) {
1964                         com_err(program_name, retval,
1965                                 _("while zeroing block %u at end of filesystem"),
1966                                 ret_blk);
1967                 }
1968                 write_inode_tables(fs, lazy_itable_init);
1969                 create_root_dir(fs);
1970                 create_lost_and_found(fs);
1971                 reserve_inodes(fs);
1972                 create_bad_block_inode(fs, bb_list);
1973                 if (fs->super->s_feature_compat & 
1974                     EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1975                         retval = ext2fs_create_resize_inode(fs);
1976                         if (retval) {
1977                                 com_err("ext2fs_create_resize_inode", retval,
1978                                 _("while reserving blocks for online resize"));
1979                                 exit(1);
1980                         }
1981                 }
1982         }
1983
1984         if (journal_device) {
1985                 ext2_filsys     jfs;
1986                 
1987                 if (!force)
1988                         check_plausibility(journal_device); 
1989                 check_mount(journal_device, force, _("journal"));
1990
1991                 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1992                                      EXT2_FLAG_JOURNAL_DEV_OK, 0,
1993                                      fs->blocksize, unix_io_manager, &jfs);
1994                 if (retval) {
1995                         com_err(program_name, retval,
1996                                 _("while trying to open journal device %s\n"),
1997                                 journal_device);
1998                         exit(1);
1999                 }
2000                 if (!quiet) {
2001                         printf(_("Adding journal to device %s: "), 
2002                                journal_device);
2003                         fflush(stdout);
2004                 }
2005                 retval = ext2fs_add_journal_device(fs, jfs);
2006                 if(retval) {
2007                         com_err (program_name, retval, 
2008                                  _("\n\twhile trying to add journal to device %s"), 
2009                                  journal_device);
2010                         exit(1);
2011                 }
2012                 if (!quiet)
2013                         printf(_("done\n"));
2014                 ext2fs_close(jfs);
2015                 free(journal_device);
2016         } else if ((journal_size) ||
2017                    (fs_param.s_feature_compat & 
2018                     EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2019                 journal_blocks = figure_journal_size(journal_size, fs);
2020
2021                 if (!journal_blocks) {
2022                         fs->super->s_feature_compat &=
2023                                 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2024                         goto no_journal;
2025                 }
2026                 if (!quiet) {
2027                         printf(_("Creating journal (%u blocks): "),
2028                                journal_blocks);
2029                         fflush(stdout);
2030                 }
2031                 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2032                                                   journal_flags);
2033                 if (retval) {
2034                         com_err (program_name, retval,
2035                                  _("\n\twhile trying to create journal"));
2036                         exit(1);
2037                 }
2038                 if (!quiet)
2039                         printf(_("done\n"));
2040         }
2041 no_journal:
2042
2043         if (!quiet)
2044                 printf(_("Writing superblocks and "
2045                        "filesystem accounting information: "));
2046         retval = ext2fs_flush(fs);
2047         if (retval) {
2048                 fprintf(stderr,
2049                         _("\nWarning, had trouble writing out superblocks."));
2050         }
2051         if (!quiet) {
2052                 printf(_("done\n\n"));
2053                 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2054                         print_check_message(fs);
2055         }
2056         val = ext2fs_close(fs);
2057         remove_error_table(&et_ext2_error_table);
2058         remove_error_table(&et_prof_error_table);
2059         profile_release(profile);
2060         return (retval || val) ? 1 : 0;
2061 }