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