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