Whamcloud - gitweb
ChangeLog, e2image.c, mke2fs.c, mklost+found.c, tune2fs.c, util.c, uuidgen.c:
[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 <linux/ext2_fs.h>
48
49 #include "et/com_err.h"
50 #include "uuid/uuid.h"
51 #include "e2p/e2p.h"
52 #include "ext2fs/ext2fs.h"
53 #include "util.h"
54 #include "../version.h"
55 #include "nls-enable.h"
56
57 #define STRIDE_LENGTH 8
58
59 #ifndef __sparc__
60 #define ZAP_BOOTBLOCK
61 #endif
62
63 extern int isatty(int);
64 extern FILE *fpopen(const char *cmd, const char *mode);
65
66 const char * program_name = "mke2fs";
67 const char * device_name /* = NULL */;
68
69 /* Command line options */
70 int     cflag /* = 0 */ ;
71 int     verbose /* = 0 */ ;
72 int     quiet /* = 0 */ ;
73 int     super_only /* = 0 */ ;
74 int     force /* = 0 */ ;
75 int     noaction /* = 0 */ ;
76 int     journal_size /* = 0 */ ;
77 int     journal_flags /* = 0 */ ;
78 char    *bad_blocks_filename /* = 0 */ ;
79 __u32   fs_stride /* = 0 */ ;
80
81 struct ext2_super_block param;
82 char *creator_os /* = NULL */ ;
83 char *volume_label /* = NULL */ ;
84 char *mount_dir /* = NULL */ ;
85 char *journal_device /* = NULL */ ;
86
87 static void usage(void)
88 {
89         fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
90         "[-f fragment-size]\n\t[-i bytes-per-inode] [-j journal-options]"
91         " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
92         "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
93         "[-M last-mounted-directory] [-O feature[,...]]\n\t"
94         "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
95         "[-qvSV] device [blocks-count]\n"),
96                 program_name);
97         exit(1);
98 }
99
100 static int int_log2(int arg)
101 {
102         int     l = 0;
103
104         arg >>= 1;
105         while (arg) {
106                 l++;
107                 arg >>= 1;
108         }
109         return l;
110 }
111
112 static int int_log10(unsigned int arg)
113 {
114         int     l;
115
116         for (l=0; arg ; l++)
117                 arg = arg / 10;
118         return l;
119 }
120
121 /*
122  * This function sets the default parameters for a filesystem
123  *
124  * The type is specified by the user.  The size is the maximum size
125  * (in megabytes) for which a set of parameters applies, with a size
126  * of zero meaning that it is the default parameter for the type.
127  * Note that order is important in the table below.
128  */
129 static char default_str[] = "default";
130 struct mke2fs_defaults {
131         const char      *type;
132         int             size;
133         int             blocksize;
134         int             inode_ratio;
135 } settings[] = {
136         { default_str, 0, 4096, 8192 },
137         { default_str, 512, 1024, 4096 },
138         { default_str, 3, 1024, 8192 },
139         { "news", 0, 4096, 4096 },
140         { 0, 0, 0, 0},
141 };
142
143 static void set_fs_defaults(char *fs_type, struct ext2_super_block *super,
144                             int blocksize, int *inode_ratio)
145 {
146         int     megs;
147         int     ratio = 0;
148         struct mke2fs_defaults *p;
149
150         megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
151                 1024);
152         if (inode_ratio)
153                 ratio = *inode_ratio;
154         if (!fs_type)
155                 fs_type = default_str;
156         for (p = settings; p->type; p++) {
157                 if ((strcmp(p->type, fs_type) != 0) &&
158                     (strcmp(p->type, default_str) != 0))
159                         continue;
160                 if ((p->size != 0) &&
161                     (megs > p->size))
162                         continue;
163                 if (ratio == 0)
164                         *inode_ratio = p->inode_ratio;
165                 if (blocksize == 0) {
166                         super->s_log_frag_size = super->s_log_block_size =
167                                 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
168                 }
169         }
170         if (blocksize == 0)
171                 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
172 }
173
174 /*
175  * Helper function for read_bb_file and test_disk
176  */
177 static void invalid_block(ext2_filsys fs, blk_t blk)
178 {
179         printf(_("Bad block %u out of range; ignored.\n"), blk);
180         return;
181 }
182
183 /*
184  * Reads the bad blocks list from a file
185  */
186 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
187                          const char *bad_blocks_file)
188 {
189         FILE            *f;
190         errcode_t       retval;
191
192         f = fopen(bad_blocks_file, "r");
193         if (!f) {
194                 com_err("read_bad_blocks_file", errno,
195                         _("while trying to open %s"), bad_blocks_file);
196                 exit(1);
197         }
198         retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
199         fclose (f);
200         if (retval) {
201                 com_err("ext2fs_read_bb_FILE", retval,
202                         _("while reading in list of bad blocks from file"));
203                 exit(1);
204         }
205 }
206
207 /*
208  * Runs the badblocks program to test the disk
209  */
210 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
211 {
212         FILE            *f;
213         errcode_t       retval;
214         char            buf[1024];
215
216         sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
217                 quiet ? "" : "-s ", fs->device_name,
218                 fs->super->s_blocks_count);
219         if (verbose)
220                 printf(_("Running command: %s\n"), buf);
221         f = popen(buf, "r");
222         if (!f) {
223                 com_err("popen", errno,
224                         _("while trying run '%s'"), buf);
225                 exit(1);
226         }
227         retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
228         pclose(f);
229         if (retval) {
230                 com_err("ext2fs_read_bb_FILE", retval,
231                         _("while processing list of bad blocks from program"));
232                 exit(1);
233         }
234 }
235
236 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
237 {
238         int                     i, j;
239         int                     must_be_good;
240         blk_t                   blk;
241         badblocks_iterate       bb_iter;
242         errcode_t               retval;
243         blk_t                   group_block;
244         int                     group;
245         int                     group_bad;
246
247         if (!bb_list)
248                 return;
249         
250         /*
251          * The primary superblock and group descriptors *must* be
252          * good; if not, abort.
253          */
254         must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
255         for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
256                 if (badblocks_list_test(bb_list, i)) {
257                         fprintf(stderr, _("Block %d in primary "
258                                 "superblock/group descriptor area bad.\n"), i);
259                         fprintf(stderr, _("Blocks %d through %d must be good "
260                                 "in order to build a filesystem.\n"),
261                                 fs->super->s_first_data_block, must_be_good);
262                         fprintf(stderr, _("Aborting....\n"));
263                         exit(1);
264                 }
265         }
266
267         /*
268          * See if any of the bad blocks are showing up in the backup
269          * superblocks and/or group descriptors.  If so, issue a
270          * warning and adjust the block counts appropriately.
271          */
272         group_block = fs->super->s_first_data_block +
273                 fs->super->s_blocks_per_group;
274         
275         for (i = 1; i < fs->group_desc_count; i++) {
276                 group_bad = 0;
277                 for (j=0; j < fs->desc_blocks+1; j++) {
278                         if (badblocks_list_test(bb_list, group_block +
279                                                 j)) {
280                                 if (!group_bad) 
281                                         fprintf(stderr,
282 _("Warning: the backup superblock/group descriptors at block %d contain\n"
283 "       bad blocks.\n\n"),
284                                                 group_block);
285                                 group_bad++;
286                                 group = ext2fs_group_of_blk(fs, group_block+j);
287                                 fs->group_desc[group].bg_free_blocks_count++;
288                                 fs->super->s_free_blocks_count++;
289                         }
290                 }
291                 group_block += fs->super->s_blocks_per_group;
292         }
293         
294         /*
295          * Mark all the bad blocks as used...
296          */
297         retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
298         if (retval) {
299                 com_err("badblocks_list_iterate_begin", retval,
300                         _("while marking bad blocks as used"));
301                 exit(1);
302         }
303         while (badblocks_list_iterate(bb_iter, &blk)) 
304                 ext2fs_mark_block_bitmap(fs->block_map, blk);
305         badblocks_list_iterate_end(bb_iter);
306 }
307
308 static void write_inode_tables(ext2_filsys fs)
309 {
310         errcode_t       retval;
311         blk_t           blk;
312         int             i, j, num, count;
313         char            *buf;
314         char            format[20], backup[80];
315         int             sync_kludge = 0;
316         char            *mke2fs_sync;
317
318         mke2fs_sync = getenv("MKE2FS_SYNC");
319         if (mke2fs_sync)
320                 sync_kludge = atoi(mke2fs_sync);
321
322         buf = malloc(fs->blocksize * STRIDE_LENGTH);
323         if (!buf) {
324                 com_err("malloc", ENOMEM,
325                         _("while allocating zeroizing buffer"));
326                 exit(1);
327         }
328         memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
329
330         /*
331          * Figure out how many digits we need
332          */
333         i = int_log10(fs->group_desc_count);
334         sprintf(format, "%%%dd/%%%dld", i, i);
335         memset(backup, '\b', sizeof(backup)-1);
336         backup[sizeof(backup)-1] = 0;
337         if ((2*i)+1 < sizeof(backup))
338                 backup[(2*i)+1] = 0;
339
340         if (!quiet)
341                 printf(_("Writing inode tables: "));
342         for (i = 0; i < fs->group_desc_count; i++) {
343                 if (!quiet)
344                         printf(format, i, fs->group_desc_count);
345                 
346                 blk = fs->group_desc[i].bg_inode_table;
347                 num = fs->inode_blocks_per_group;
348                 
349                 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
350                         if (num-j > STRIDE_LENGTH)
351                                 count = STRIDE_LENGTH;
352                         else
353                                 count = num - j;
354                         retval = io_channel_write_blk(fs->io, blk, count, buf);
355                         if (retval)
356                                 printf(_("Warning: could not write %d blocks "
357                                        "in inode table starting at %d: %s\n"),
358                                        count, blk, error_message(retval));
359                 }
360                 if (!quiet) 
361                         fputs(backup, stdout);
362                 if (sync_kludge) {
363                         if (sync_kludge == 1)
364                                 sync();
365                         else if ((i % sync_kludge) == 0)
366                                 sync();
367                 }
368         }
369         free(buf);
370         if (!quiet)
371                 fputs(_("done                            \n"), stdout);
372 }
373
374 static void create_root_dir(ext2_filsys fs)
375 {
376         errcode_t       retval;
377         struct ext2_inode       inode;
378
379         retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
380         if (retval) {
381                 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
382                 exit(1);
383         }
384         if (geteuid()) {
385                 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
386                 if (retval) {
387                         com_err("ext2fs_read_inode", retval,
388                                 _("while reading root inode"));
389                         exit(1);
390                 }
391                 inode.i_uid = getuid();
392                 if (inode.i_uid)
393                         inode.i_gid = getgid();
394                 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
395                 if (retval) {
396                         com_err("ext2fs_write_inode", retval,
397                                 _("while setting root inode ownership"));
398                         exit(1);
399                 }
400         }
401 }
402
403 static void create_lost_and_found(ext2_filsys fs)
404 {
405         errcode_t               retval;
406         ext2_ino_t              ino;
407         const char              *name = "lost+found";
408         int                     i;
409         int                     lpf_size = 0;
410
411         retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
412         if (retval) {
413                 com_err("ext2fs_mkdir", retval,
414                         _("while creating /lost+found"));
415                 exit(1);
416         }
417
418         retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
419         if (retval) {
420                 com_err("ext2_lookup", retval,
421                         _("while looking up /lost+found"));
422                 exit(1);
423         }
424         
425         for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
426                 if ((lpf_size += fs->blocksize) >= 16*1024)
427                         break;
428                 retval = ext2fs_expand_dir(fs, ino);
429                 if (retval) {
430                         com_err("ext2fs_expand_dir", retval,
431                                 _("while expanding /lost+found"));
432                         exit(1);
433                 }
434         }               
435 }
436
437 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
438 {
439         errcode_t       retval;
440         
441         ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
442         fs->group_desc[0].bg_free_inodes_count--;
443         fs->super->s_free_inodes_count--;
444         retval = ext2fs_update_bb_inode(fs, bb_list);
445         if (retval) {
446                 com_err("ext2fs_update_bb_inode", retval,
447                         _("while setting bad block inode"));
448                 exit(1);
449         }
450
451 }
452
453 static void reserve_inodes(ext2_filsys fs)
454 {
455         ext2_ino_t      i;
456         int             group;
457
458         for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
459                 ext2fs_mark_inode_bitmap(fs->inode_map, i);
460                 group = ext2fs_group_of_ino(fs, i);
461                 fs->group_desc[group].bg_free_inodes_count--;
462                 fs->super->s_free_inodes_count--;
463         }
464         ext2fs_mark_ib_dirty(fs);
465 }
466
467 static void zap_sector(ext2_filsys fs, int sect)
468 {
469         char buf[512];
470         int retval;
471
472         memset(buf, 0, 512);
473         
474         io_channel_set_blksize(fs->io, 512);
475         retval = io_channel_write_blk(fs->io, sect, -512, buf);
476         io_channel_set_blksize(fs->io, fs->blocksize);
477         if (retval)
478                 printf(_("Warning: could not erase sector %d: %s\n"), sect,
479                        error_message(retval));
480 }
481         
482
483 static void show_stats(ext2_filsys fs)
484 {
485         struct ext2_super_block *s = fs->super;
486         char                    buf[80];
487         blk_t                   group_block;
488         int                     i, need, col_left;
489         
490         if (param.s_blocks_count != s->s_blocks_count)
491                 printf(_("warning: %d blocks unused.\n\n"),
492                        param.s_blocks_count - s->s_blocks_count);
493
494         memset(buf, 0, sizeof(buf));
495         strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
496         printf(_("Filesystem label=%s\n"), buf);
497         printf(_("OS type: "));
498         switch (fs->super->s_creator_os) {
499             case EXT2_OS_LINUX: printf ("Linux"); break;
500             case EXT2_OS_HURD:  printf ("GNU/Hurd");   break;
501             case EXT2_OS_MASIX: printf ("Masix"); break;
502             default:            printf (_("(unknown os)"));
503         }
504         printf("\n");
505         printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
506                 s->s_log_block_size);
507         printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
508                 s->s_log_frag_size);
509         printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
510                s->s_blocks_count);
511         printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
512                 s->s_r_blocks_count,
513                100.0 * s->s_r_blocks_count / s->s_blocks_count);
514         printf(_("First data block=%u\n"), s->s_first_data_block);
515         if (fs->group_desc_count > 1)
516                 printf(_("%u block groups\n"), fs->group_desc_count);
517         else
518                 printf(_("%u block group\n"), fs->group_desc_count);
519         printf(_("%u blocks per group, %u fragments per group\n"),
520                s->s_blocks_per_group, s->s_frags_per_group);
521         printf(_("%u inodes per group\n"), s->s_inodes_per_group);
522
523         if (fs->group_desc_count == 1) {
524                 printf("\n");
525                 return;
526         }
527         
528         printf(_("Superblock backups stored on blocks: "));
529         group_block = s->s_first_data_block;
530         col_left = 0;
531         for (i = 1; i < fs->group_desc_count; i++) {
532                 group_block += s->s_blocks_per_group;
533                 if (!ext2fs_bg_has_super(fs, i))
534                         continue;
535                 if (i != 1)
536                         printf(", ");
537                 need = int_log10(group_block) + 2;
538                 if (need > col_left) {
539                         printf("\n\t");
540                         col_left = 72;
541                 }
542                 col_left -= need;
543                 printf("%u", group_block);
544         }
545         printf("\n\n");
546 }
547
548 /*
549  * Set the S_CREATOR_OS field.  Return true if OS is known,
550  * otherwise, 0.
551  */
552 static int set_os(struct ext2_super_block *sb, char *os)
553 {
554         if (isdigit (*os))
555                 sb->s_creator_os = atoi (os);
556         else if (strcasecmp(os, "linux") == 0)
557                 sb->s_creator_os = EXT2_OS_LINUX;
558         else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
559                 sb->s_creator_os = EXT2_OS_HURD;
560         else if (strcasecmp(os, "masix") == 0)
561                 sb->s_creator_os = EXT2_OS_MASIX;
562         else
563                 return 0;
564         return 1;
565 }
566
567 #define PATH_SET "PATH=/sbin"
568
569 static void parse_raid_opts(const char *opts)
570 {
571         char    *buf, *token, *next, *p, *arg;
572         int     len;
573         int     raid_usage = 0;
574
575         len = strlen(opts);
576         buf = malloc(len+1);
577         if (!buf) {
578                 fprintf(stderr, _("Couldn't allocate memory to parse "
579                         "raid options!\n"));
580                 exit(1);
581         }
582         strcpy(buf, opts);
583         for (token = buf; token && *token; token = next) {
584                 p = strchr(token, ',');
585                 next = 0;
586                 if (p) {
587                         *p = 0;
588                         next = p+1;
589                 } 
590                 arg = strchr(token, '=');
591                 if (arg) {
592                         *arg = 0;
593                         arg++;
594                 }
595                 if (strcmp(token, "stride") == 0) {
596                         if (!arg) {
597                                 raid_usage++;
598                                 continue;
599                         }
600                         fs_stride = strtoul(arg, &p, 0);
601                         if (*p || (fs_stride == 0)) {
602                                 fprintf(stderr,
603                                         _("Invalid stride parameter.\n"));
604                                 raid_usage++;
605                                 continue;
606                         }
607                 } else
608                         raid_usage++;
609         }
610         if (raid_usage) {
611                 fprintf(stderr, _("\nBad raid options specified.\n\n"
612                         "Raid options are separated by commas, "
613                         "and may take an argument which\n"
614                         "\tis set off by an equals ('=') sign.\n\n"
615                         "Valid raid options are:\n"
616                         "\tstride=<stride length in blocks>\n\n"));
617                 exit(1);
618         }
619 }       
620
621 static __u32 ok_features[3] = {
622         0,                                      /* Compat */
623         EXT2_FEATURE_INCOMPAT_FILETYPE,         /* Incompat */
624         EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER     /* R/O compat */
625 };
626
627
628 static void PRS(int argc, char *argv[])
629 {
630         int             c;
631         int             size;
632         char *          tmp;
633         blk_t           group_blk_max = 8192;
634         int             blocksize = 0;
635         int             inode_ratio = 0;
636         int             reserved_ratio = 5;
637         ext2_ino_t      num_inodes = 0;
638         errcode_t       retval;
639         int             sparse_option = 1;
640         char *          oldpath = getenv("PATH");
641         struct ext2_super_block *param_ext2 = &param;
642         char *          raid_opts = 0;
643         char *          journal_opts = 0;
644         char *          fs_type = 0;
645         const char *    feature_set = "filetype";
646         blk_t           dev_size;
647 #ifdef linux
648         struct          utsname ut;
649
650         if (uname(&ut)) {
651                 perror("uname");
652                 exit(1);
653         }
654         if ((ut.release[0] == '1') ||
655             (ut.release[0] == '2' && ut.release[1] == '.' &&
656              ut.release[2] < '2' && ut.release[3] == '.'))
657                 feature_set = NULL;
658 #endif
659         /* Update our PATH to include /sbin  */
660         if (oldpath) {
661                 char *newpath;
662                 
663                 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
664                 strcpy (newpath, PATH_SET);
665                 strcat (newpath, ":");
666                 strcat (newpath, oldpath);
667                 putenv (newpath);
668         } else
669                 putenv (PATH_SET);
670
671         setbuf(stdout, NULL);
672         setbuf(stderr, NULL);
673         initialize_ext2_error_table();
674         memset(&param, 0, sizeof(struct ext2_super_block));
675         param.s_rev_level = 1;  /* Create revision 1 filesystems now */
676         
677         fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
678                  E2FSPROGS_VERSION, E2FSPROGS_DATE,
679                  EXT2FS_VERSION, EXT2FS_DATE);
680         if (argc && *argv)
681                 program_name = *argv;
682         while ((c = getopt (argc, argv,
683                     "b:cf:g:i:j:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF)
684                 switch (c) {
685                 case 'b':
686                         blocksize = strtoul(optarg, &tmp, 0);
687                         if (blocksize < 1024 || blocksize > 4096 || *tmp) {
688                                 com_err(program_name, 0,
689                                         _("bad block size - %s"), optarg);
690                                 exit(1);
691                         }
692                         param.s_log_block_size =
693                                 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
694                         group_blk_max = blocksize * 8;
695                         break;
696                 case 'c':
697                 case 't':       /* Check for bad blocks */
698                         cflag = 1;
699                         break;
700                 case 'f':
701                         size = strtoul(optarg, &tmp, 0);
702                         if (size < 1024 || size > 4096 || *tmp) {
703                                 com_err(program_name, 0,
704                                         _("bad fragment size - %s"),
705                                         optarg);
706                                 exit(1);
707                         }
708                         param.s_log_frag_size =
709                                 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
710                         printf(_("Warning: fragments not supported.  "
711                                "Ignoring -f option\n"));
712                         break;
713                 case 'g':
714                         param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
715                         if (*tmp) {
716                                 com_err(program_name, 0,
717                                         _("Illegal number for blocks per group"));
718                                 exit(1);
719                         }
720                         if ((param.s_blocks_per_group % 8) != 0) {
721                                 com_err(program_name, 0,
722                                 _("blocks per group must be multiple of 8"));
723                                 exit(1);
724                         }
725                         break;
726                 case 'i':
727                         inode_ratio = strtoul(optarg, &tmp, 0);
728                         if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
729                             *tmp) {
730                                 com_err(program_name, 0,
731                                         _("bad inode ratio - %s"), optarg);
732                                 exit(1);
733                         }
734                         break;
735                 case 'j':
736                         journal_opts = optarg;
737                         break;
738                 case 'l':
739                         bad_blocks_filename = malloc(strlen(optarg)+1);
740                         if (!bad_blocks_filename) {
741                                 com_err(program_name, ENOMEM,
742                                         _("in malloc for bad_blocks_filename"));
743                                 exit(1);
744                         }
745                         strcpy(bad_blocks_filename, optarg);
746                         break;
747                 case 'm':
748                         reserved_ratio = strtoul(optarg, &tmp, 0);
749                         if (reserved_ratio > 50 || *tmp) {
750                                 com_err(program_name, 0,
751                                         _("bad reserved blocks percent - %s"),
752                                         optarg);
753                                 exit(1);
754                         }
755                         break;
756                 case 'n':
757                         noaction++;
758                         break;
759                 case 'o':
760                         creator_os = optarg;
761                         break;
762                 case 'r':
763                         param.s_rev_level = atoi(optarg);
764                         break;
765                 case 's':
766                         sparse_option = atoi(optarg);
767                         break;
768 #ifdef EXT2_DYNAMIC_REV
769                 case 'I':
770                         param.s_inode_size = atoi(optarg);
771                         break;
772 #endif
773                 case 'N':
774                         num_inodes = atoi(optarg);
775                         break;
776                 case 'v':
777                         verbose = 1;
778                         break;
779                 case 'q':
780                         quiet = 1;
781                         break;
782                 case 'F':
783                         force = 1;
784                         break;
785                 case 'L':
786                         volume_label = optarg;
787                         break;
788                 case 'M':
789                         mount_dir = optarg;
790                         break;
791                 case 'O':
792                         feature_set = optarg;
793                         break;
794                 case 'R':
795                         raid_opts = optarg;
796                         break;
797                 case 'S':
798                         super_only = 1;
799                         break;
800                 case 'T':
801                         fs_type = optarg;
802                         break;
803                 case 'V':
804                         /* Print version number and exit */
805                         fprintf(stderr, _("\tUsing %s\n"),
806                                 error_message(EXT2_ET_BASE));
807                         exit(0);
808                 default:
809                         usage();
810                 }
811         if (optind == argc)
812                 usage();
813         device_name = argv[optind];
814         optind++;
815         if (optind < argc) {
816                 unsigned long tmp2  = strtoul(argv[optind++], &tmp, 0);
817
818                 if ((*tmp) || (tmp2 > 0xfffffffful)) {
819                         com_err(program_name, 0, _("bad blocks count - %s"),
820                                 argv[optind - 1]);
821                         exit(1);
822                 }
823                 param.s_blocks_count = tmp2;
824         }
825         if (optind < argc)
826                 usage();
827
828         if (raid_opts)
829                 parse_raid_opts(raid_opts);
830
831         if (journal_opts)
832                 parse_journal_opts(journal_opts);
833         
834         if (!force)
835                 check_plausibility(device_name);
836         check_mount(device_name, force, _("filesystem"));
837
838         param.s_log_frag_size = param.s_log_block_size;
839
840         if (noaction && param.s_blocks_count) {
841                 dev_size = param.s_blocks_count;
842                 retval = 0;
843         } else
844                 retval = ext2fs_get_device_size(device_name,
845                                                 EXT2_BLOCK_SIZE(&param),
846                                                 &dev_size);
847         if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
848                 com_err(program_name, retval,
849                         _("while trying to determine filesystem size"));
850                 exit(1);
851         }
852         if (!param.s_blocks_count) {
853                 if (retval == EXT2_ET_UNIMPLEMENTED) {
854                         com_err(program_name, 0,
855                                 _("Couldn't determine device size; you "
856                                 "must specify\nthe size of the "
857                                 "filesystem\n"));
858                         exit(1);
859                 } else {
860                         if (dev_size == 0) {
861                                 com_err(program_name, 0,
862                                 _("Device size reported to be zero.  "
863                                   "Invalid partition specified, or\n\t"
864                                   "partition table wasn't reread "
865                                   "after running fdisk, due to\n\t"
866                                   "a modified partition being busy "
867                                   "and in use.  You may need to reboot\n\t"
868                                   "to re-read your partition table.\n"
869                                   ));
870                                 exit(1);
871                         }
872                         param.s_blocks_count = dev_size;
873                 }
874                 
875         } else if (!force && (param.s_blocks_count > dev_size)) {
876                 com_err(program_name, 0,
877                         _("Filesystem larger than apparent filesystem size."));
878                 proceed_question();
879         }
880
881         set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
882
883         if (param.s_blocks_per_group) {
884                 if (param.s_blocks_per_group < 256 ||
885                     param.s_blocks_per_group > group_blk_max || *tmp) {
886                         com_err(program_name, 0,
887                                 _("blocks per group count out of range"));
888                         exit(1);
889                 }
890         }
891
892         /*
893          * Calculate number of inodes based on the inode ratio
894          */
895         param.s_inodes_count = num_inodes ? num_inodes : 
896                 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
897                         / inode_ratio;
898
899         /*
900          * Calculate number of blocks to reserve
901          */
902         param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
903
904         /* Turn off features not supported by the earlier filesystem version */
905         if (param.s_rev_level == 0) {
906                 sparse_option = 0;
907                 feature_set = NULL;
908         }
909         if (sparse_option)
910                 param_ext2->s_feature_ro_compat |=
911                         EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
912
913         if (feature_set && !strncasecmp(feature_set, "none", 4))
914                 feature_set = NULL;
915         if (feature_set && e2p_edit_feature(feature_set,
916                                             &param_ext2->s_feature_compat,
917                                             ok_features)) {
918                 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
919                         feature_set);
920                 exit(1);
921         }
922 }
923                                         
924 int main (int argc, char *argv[])
925 {
926         errcode_t       retval = 0;
927         ext2_filsys     fs;
928         badblocks_list  bb_list = 0;
929         int             journal_blocks;
930
931 #ifdef ENABLE_NLS
932         setlocale(LC_MESSAGES, "");
933         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
934         textdomain(NLS_CAT_NAME);
935 #endif
936         PRS(argc, argv);
937
938         /*
939          * Initialize the superblock....
940          */
941         retval = ext2fs_initialize(device_name, 0, &param,
942                                    unix_io_manager, &fs);
943         if (retval) {
944                 com_err(device_name, retval, _("while setting up superblock"));
945                 exit(1);
946         }
947
948         /*
949          * Wipe out the old on-disk superblock
950          */
951         zap_sector(fs, 2);
952
953         /*
954          * Generate a UUID for it...
955          */
956         uuid_generate(fs->super->s_uuid);
957
958         /*
959          * Override the creator OS, if applicable
960          */
961         if (creator_os && !set_os(fs->super, creator_os)) {
962                 com_err (program_name, 0, _("unknown os - %s"), creator_os);
963                 exit(1);
964         }
965
966         /*
967          * For the Hurd, we will turn off filetype since it doesn't
968          * support it.
969          */
970         if (fs->super->s_creator_os == EXT2_OS_HURD)
971                 fs->super->s_feature_incompat &=
972                         ~EXT2_FEATURE_INCOMPAT_FILETYPE;
973
974         /*
975          * Set the volume label...
976          */
977         if (volume_label) {
978                 memset(fs->super->s_volume_name, 0,
979                        sizeof(fs->super->s_volume_name));
980                 strncpy(fs->super->s_volume_name, volume_label,
981                         sizeof(fs->super->s_volume_name));
982         }
983
984         /*
985          * Set the last mount directory
986          */
987         if (mount_dir) {
988                 memset(fs->super->s_last_mounted, 0,
989                        sizeof(fs->super->s_last_mounted));
990                 strncpy(fs->super->s_last_mounted, mount_dir,
991                         sizeof(fs->super->s_last_mounted));
992         }
993         
994         if (!quiet || noaction)
995                 show_stats(fs);
996
997         if (noaction)
998                 exit(0);
999
1000         if (bad_blocks_filename)
1001                 read_bb_file(fs, &bb_list, bad_blocks_filename);
1002         if (cflag)
1003                 test_disk(fs, &bb_list);
1004
1005         handle_bad_blocks(fs, bb_list);
1006         fs->stride = fs_stride;
1007         retval = ext2fs_allocate_tables(fs);
1008         if (retval) {
1009                 com_err(program_name, retval,
1010                         _("while trying to allocate filesystem tables"));
1011                 exit(1);
1012         }
1013         if (super_only) {
1014                 fs->super->s_state |= EXT2_ERROR_FS;
1015                 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1016         } else {
1017                 write_inode_tables(fs);
1018                 create_root_dir(fs);
1019                 create_lost_and_found(fs);
1020                 reserve_inodes(fs);
1021                 create_bad_block_inode(fs, bb_list);
1022 #ifdef ZAP_BOOTBLOCK
1023                 zap_sector(fs, 0);
1024 #endif
1025         }
1026
1027         journal_blocks = journal_size * 1024 / (fs->blocksize   / 1024);
1028         if (journal_device) { 
1029                 if (!force)
1030                         check_plausibility(journal_device); 
1031                 check_mount(journal_device, force, _("journal"));
1032
1033                 if (!quiet)
1034                         printf(_("Creating journal on device %s: "), 
1035                                journal_device);
1036                 retval = ext2fs_add_journal_device(fs, journal_device,
1037                                                    journal_blocks,
1038                                                    journal_flags);
1039                 if(retval) { 
1040                         com_err (program_name, retval, 
1041                                  _("while trying to create journal on device %s"), 
1042                                  journal_device);
1043                         exit(1);
1044                 }
1045                 if (!quiet)
1046                         printf(_("done\n"));
1047         } else if (journal_size) {
1048                 if (!quiet)
1049                         printf(_("Creating journal (%d blocks): "),
1050                                journal_blocks);
1051                 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1052                                                   journal_flags);
1053                 if (retval) {
1054                         com_err (program_name, retval,
1055                                  _("while trying to create journal"));
1056                         exit(1);
1057                 }
1058                 if (!quiet)
1059                         printf(_("done\n"));
1060         }
1061         
1062         if (!quiet)
1063                 printf(_("Writing superblocks and "
1064                        "filesystem accounting information: "));
1065         retval = ext2fs_flush(fs);
1066         if (retval) {
1067                 printf(_("\nWarning, had trouble writing out superblocks."));
1068         }
1069         if (!quiet)
1070                 printf(_("done\n"));
1071         ext2fs_close(fs);
1072         return 0;
1073 }