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