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