Whamcloud - gitweb
0cdcc059174fce01902d3809947c9cb66d2fc67a
[tools/e2fsprogs.git] / misc / mke2fs.c
1 /*
2  * mke2fs.c - Make a ext2fs filesystem.
3  * 
4  * Copyright (C) 1994 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 /* Usage: mke2fs [options] device
9  * 
10  * The device may be a block device or a image of one, but this isn't
11  * enforced (but it's not much fun on a character device :-). 
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <ctype.h>
18 #include <termios.h>
19 #include <time.h>
20 #ifdef HAVE_GETOPT_H
21 #include <getopt.h>
22 #endif
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #ifdef HAVE_MNTENT_H
33 #include <mntent.h>
34 #endif
35 #include <malloc.h>
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <stdio.h>
40
41 #ifdef HAVE_LINUX_FS_H
42 #include <linux/fs.h>
43 #endif
44 #include <linux/ext2_fs.h>
45 #ifdef HAVE_LINUX_MAJOR_H
46 #include <linux/major.h>
47 #endif
48
49 #include "et/com_err.h"
50 #include "uuid/uuid.h"
51 #include "ext2fs/ext2fs.h"
52 #include "../version.h"
53
54 #define STRIDE_LENGTH 8
55
56 #ifndef sparc
57 #define ZAP_BOOTBLOCK
58 #endif
59
60 extern int isatty(int);
61 extern FILE *fpopen(const char *cmd, const char *mode);
62
63 const char * program_name = "mke2fs";
64 const char * device_name = NULL;
65
66 /* Command line options */
67 int     cflag = 0;
68 int     verbose = 0;
69 int     quiet = 0;
70 int     super_only = 0;
71 int     force = 0;
72 char    *bad_blocks_filename = 0;
73
74 struct ext2_super_block param;
75 char *creator_os = NULL;
76 char *volume_label = NULL;
77 char *mount_dir = NULL;
78
79 static void usage(NOARGS)
80 {
81         fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
82         "[-f fragment-size]\n\t[-i bytes-per-inode] "
83         "[-m reserved-blocks-percentage] [-qvS]\n\t"
84         "[-o creator-os] [-g blocks-per-group] [-L volume-label]\n\t"
85         "[-M last-mounted-directory] device [blocks-count]\n",
86                 program_name);
87         exit(1);
88 }
89
90 static int log2(int arg)
91 {
92         int     l = 0;
93
94         arg >>= 1;
95         while (arg) {
96                 l++;
97                 arg >>= 1;
98         }
99         return l;
100 }
101
102 static void check_plausibility(NOARGS)
103 {
104 #ifdef HAVE_LINUX_MAJOR_H
105         int val;
106         struct stat s;
107         
108         val = stat(device_name, &s);
109         
110         if(val == -1) {
111                 perror("stat");
112                 exit(1);
113         }
114         if(!S_ISBLK(s.st_mode)) {
115                 printf("%s is not a block special device.\n", device_name);
116                 printf("Proceed anyway? (y,n) ");
117                 if (getchar() != 'y')
118                         exit(1);
119                 return;
120         }
121         if ((MAJOR(s.st_rdev) == HD_MAJOR && MINOR(s.st_rdev)%64 == 0) ||
122             (MAJOR(s.st_rdev) == SCSI_DISK_MAJOR &&
123              MINOR(s.st_rdev)%16 == 0)) {
124                 printf("%s is entire device, not just one partition!\n", 
125                        device_name);
126                 printf("Proceed anyway? (y,n) ");
127                 if (getchar() != 'y')
128                         exit(1);
129                 return;
130         }
131 #endif
132 }
133
134 static void check_mount(NOARGS)
135 {
136         errcode_t       retval;
137         int             mount_flags;
138
139         retval = ext2fs_check_if_mounted(device_name, &mount_flags);
140         if (retval) {
141                 com_err("ext2fs_check_if_mount", retval,
142                         "while determining whether %s is mounted.",
143                         device_name);
144                 return;
145         }
146         if (!(mount_flags & EXT2_MF_MOUNTED))
147                 return;
148         
149         fprintf(stderr, "%s is mounted; will not make a filesystem here!\n",
150                 device_name);
151         exit(1);
152 }
153
154 /*
155  * Helper function for read_bb_file and test_disk
156  */
157 static void invalid_block(ext2_filsys fs, blk_t blk)
158 {
159         printf("Bad block %u out of range; ignored.\n", blk);
160         return;
161 }
162
163 /*
164  * Reads the bad blocks list from a file
165  */
166 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
167                          const char *bad_blocks_file)
168 {
169         FILE            *f;
170         errcode_t       retval;
171
172         f = fopen(bad_blocks_file, "r");
173         if (!f) {
174                 com_err("read_bad_blocks_file", errno,
175                         "while trying to open %s", bad_blocks_file);
176                 exit(1);
177         }
178         retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
179         fclose (f);
180         if (retval) {
181                 com_err("ext2fs_read_bb_FILE", retval,
182                         "while reading in list of bad blocks from file");
183                 exit(1);
184         }
185 }
186
187 /*
188  * Runs the badblocks program to test the disk
189  */
190 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
191 {
192         FILE            *f;
193         errcode_t       retval;
194         char            buf[1024];
195
196         sprintf(buf, "badblocks %s%s %d", quiet ? "" : "-s ",
197                 fs->device_name,
198                 fs->super->s_blocks_count);
199         if (verbose)
200                 printf("Running command: %s\n", buf);
201         f = popen(buf, "r");
202         if (!f) {
203                 com_err("popen", errno,
204                         "while trying run '%s'", buf);
205                 exit(1);
206         }
207         retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
208         pclose(f);
209         if (retval) {
210                 com_err("ext2fs_read_bb_FILE", retval,
211                         "while processing list of bad blocks from program");
212                 exit(1);
213         }
214 }
215
216 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
217 {
218         int                     i, j;
219         int                     must_be_good;
220         blk_t                   blk;
221         badblocks_iterate       bb_iter;
222         errcode_t               retval;
223         blk_t                   group_block;
224         int                     group;
225         int                     group_bad;
226
227         if (!bb_list)
228                 return;
229         
230         /*
231          * The primary superblock and group descriptors *must* be
232          * good; if not, abort.
233          */
234         must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
235         for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
236                 if (badblocks_list_test(bb_list, i)) {
237                         fprintf(stderr, "Block %d in primary superblock/group "
238                                 "descriptor area bad.\n", i);
239                         fprintf(stderr, "Blocks %d through %d must be good "
240                                 "in order to build a filesystem.\n",
241                                 fs->super->s_first_data_block, must_be_good);
242                         fprintf(stderr, "Aborting....\n");
243                         exit(1);
244                 }
245         }
246
247         /*
248          * See if any of the bad blocks are showing up in the backup
249          * superblocks and/or group descriptors.  If so, issue a
250          * warning and adjust the block counts appropriately.
251          */
252         group_block = fs->super->s_first_data_block +
253                 fs->super->s_blocks_per_group;
254         group_bad = 0;
255         
256         for (i = 1; i < fs->group_desc_count; i++) {
257                 for (j=0; j < fs->desc_blocks+1; j++) {
258                         if (badblocks_list_test(bb_list, group_block +
259                                                 j)) {
260                                 if (!group_bad) 
261                                         fprintf(stderr,
262 "Warning: the backup superblock/group descriptors at block %d contain\n"
263 "       bad blocks.\n\n",
264                                                 group_block);
265                                 group_bad++;
266                                 group = ext2fs_group_of_blk(fs, group_block+j);
267                                 fs->group_desc[group].bg_free_blocks_count++;
268                                 fs->super->s_free_blocks_count++;
269                         }
270                 }
271                 group_block += fs->super->s_blocks_per_group;
272         }
273         
274         /*
275          * Mark all the bad blocks as used...
276          */
277         retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
278         if (retval) {
279                 com_err("badblocks_list_iterate_begin", retval,
280                         "while marking bad blocks as used");
281                 exit(1);
282         }
283         while (badblocks_list_iterate(bb_iter, &blk)) 
284                 ext2fs_mark_block_bitmap(fs->block_map, blk);
285         badblocks_list_iterate_end(bb_iter);
286 }
287
288 static void new_table_block(ext2_filsys fs, blk_t first_block,
289                             const char *name, int num, int initialize,
290                             const char *buf, blk_t *new_block)
291 {
292         errcode_t       retval;
293         blk_t           blk;
294         int             i;
295         int             count;
296         
297         retval = ext2fs_get_free_blocks(fs, first_block,
298                         first_block + fs->super->s_blocks_per_group,
299                                         num, fs->block_map, new_block);
300         if (retval) {
301                 printf("Could not allocate %d block(s) for %s: %s\n",
302                        num, name, error_message(retval));
303                 ext2fs_unmark_valid(fs);
304                 return;
305         }
306         if (initialize) {
307                 blk = *new_block;
308                 for (i=0; i < num; i += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
309                         if (num-i > STRIDE_LENGTH)
310                                 count = STRIDE_LENGTH;
311                         else
312                                 count = num - i;
313                         retval = io_channel_write_blk(fs->io, blk, count, buf);
314                         if (retval)
315                                 printf("Warning: could not write %d blocks "
316                                        "starting at %d for %s: %s\n",
317                                        count, blk, name,
318                                        error_message(retval));
319                 }
320         }
321         blk = *new_block;
322         for (i = 0; i < num; i++, blk++)
323                 ext2fs_mark_block_bitmap(fs->block_map, blk);
324 }       
325
326 static void alloc_tables(ext2_filsys fs)
327 {
328         blk_t   group_blk;
329         int     i;
330         char    *buf;
331
332         buf = malloc(fs->blocksize * STRIDE_LENGTH);
333         if (!buf) {
334                 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
335                 exit(1);
336         }
337         memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
338         
339         group_blk = fs->super->s_first_data_block;
340         if (!quiet)
341                 printf("Writing inode tables: ");
342         for (i = 0; i < fs->group_desc_count; i++) {
343                 if (!quiet)
344                         printf("%4d/%4ld", i, fs->group_desc_count);
345                 new_table_block(fs, group_blk, "block bitmap", 1, 0, buf,
346                                 &fs->group_desc[i].bg_block_bitmap);
347                 new_table_block(fs, group_blk, "inode bitmap", 1, 0, buf,
348                                 &fs->group_desc[i].bg_inode_bitmap);
349                 new_table_block(fs, group_blk, "inode table",
350                                 fs->inode_blocks_per_group,
351                                 !super_only, buf,
352                                 &fs->group_desc[i].bg_inode_table);
353                 
354                 group_blk += fs->super->s_blocks_per_group;
355                 if (!quiet) 
356                         printf("\b\b\b\b\b\b\b\b\b");
357         }
358         if (!quiet)
359                 printf("done     \n");
360 }
361
362 static void create_root_dir(ext2_filsys fs)
363 {
364         errcode_t       retval;
365         struct ext2_inode       inode;
366
367         retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
368         if (retval) {
369                 com_err("ext2fs_mkdir", retval, "while creating root dir");
370                 exit(1);
371         }
372         if (geteuid()) {
373                 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
374                 if (retval) {
375                         com_err("ext2fs_read_inode", retval,
376                                 "while reading root inode");
377                         exit(1);
378                 }
379                 inode.i_uid = geteuid();
380                 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
381                 if (retval) {
382                         com_err("ext2fs_write_inode", retval,
383                                 "while setting root inode ownership");
384                         exit(1);
385                 }
386         }
387 }
388
389 static void create_lost_and_found(ext2_filsys fs)
390 {
391         errcode_t               retval;
392         ino_t                   ino;
393         const char              *name = "lost+found";
394         int                     i;
395
396         retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
397         if (retval) {
398                 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
399                 exit(1);
400         }
401
402         retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
403         if (retval) {
404                 com_err("ext2_lookup", retval, "while looking up /lost+found");
405                 exit(1);
406         }
407         
408         for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
409                 retval = ext2fs_expand_dir(fs, ino);
410                 if (retval) {
411                         com_err("ext2fs_expand_dir", retval,
412                                 "while expanding /lost+found");
413                         exit(1);
414                 }
415         }               
416 }
417
418 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
419 {
420         errcode_t       retval;
421         
422         ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
423         fs->group_desc[0].bg_free_inodes_count--;
424         fs->super->s_free_inodes_count--;
425         retval = ext2fs_update_bb_inode(fs, bb_list);
426         if (retval) {
427                 com_err("ext2fs_update_bb_inode", retval,
428                         "while setting bad block inode");
429                 exit(1);
430         }
431
432 }
433
434 static void reserve_inodes(ext2_filsys fs)
435 {
436         ino_t   i;
437         int     group;
438
439         for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
440                 ext2fs_mark_inode_bitmap(fs->inode_map, i);
441                 group = ext2fs_group_of_ino(fs, i);
442                 fs->group_desc[group].bg_free_inodes_count--;
443                 fs->super->s_free_inodes_count--;
444         }
445         ext2fs_mark_ib_dirty(fs);
446 }
447
448 #ifdef ZAP_BOOTBLOCK
449 static void zap_bootblock(ext2_filsys fs)
450 {
451         char buf[512];
452         int retval;
453
454         memset(buf, 0, 512);
455         
456         retval = io_channel_write_blk(fs->io, 0, -512, buf);
457         if (retval)
458                 printf("Warning: could not erase block 0: %s\n", 
459                        error_message(retval));
460 }
461 #endif
462         
463
464 static void show_stats(ext2_filsys fs)
465 {
466         struct ext2fs_sb        *s = (struct ext2fs_sb *) fs->super;
467         char                    buf[80];
468         blk_t                   group_block;
469         int                     i, col_left;
470         
471         if (param.s_blocks_count != s->s_blocks_count)
472                 printf("warning: %d blocks unused.\n\n",
473                        param.s_blocks_count - s->s_blocks_count);
474         
475         switch (fs->super->s_creator_os) {
476             case EXT2_OS_LINUX: printf ("Linux"); break;
477             case EXT2_OS_HURD:  printf ("GNU/hurd");   break;
478             case EXT2_OS_MASIX: printf ("Masix"); break;
479             default:            printf ("(unknown os)");
480         }
481         printf (" ext2 filesystem format\n");
482         memset(buf, 0, sizeof(buf));
483         strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
484         printf("Filesystem label=%s\n", buf);
485         printf("%u inodes, %u blocks\n", s->s_inodes_count,
486                s->s_blocks_count);
487         printf("%u blocks (%2.2f%%) reserved for the super user\n",
488                 s->s_r_blocks_count,
489                100.0 * s->s_r_blocks_count / s->s_blocks_count);
490         printf("First data block=%u\n", s->s_first_data_block);
491         printf("Block size=%u (log=%u)\n", fs->blocksize,
492                 s->s_log_block_size);
493         printf("Fragment size=%u (log=%u)\n", fs->fragsize,
494                 s->s_log_frag_size);
495         printf("%lu block group%s\n", fs->group_desc_count,
496                 (fs->group_desc_count > 1) ? "s" : "");
497         printf("%u blocks per group, %u fragments per group\n",
498                s->s_blocks_per_group, s->s_frags_per_group);
499         printf("%u inodes per group\n", s->s_inodes_per_group);
500
501         if (fs->group_desc_count == 1) {
502                 printf("\n");
503                 return;
504         }
505         
506         printf("Superblock backups stored on blocks: ");
507         group_block = s->s_first_data_block;
508         col_left = 0;
509         for (i = 1; i < fs->group_desc_count; i++) {
510                 group_block += s->s_blocks_per_group;
511                 if (!col_left--) {
512                         printf("\n\t");
513                         col_left = 8;
514                 }
515                 printf("%u", group_block);
516                 if (i != fs->group_desc_count - 1)
517                         printf(", ");
518         }
519         printf("\n\n");
520 }
521
522 #ifndef HAVE_STRCASECMP
523 static int strcasecmp (char *s1, char *s2)
524 {
525         while (*s1 && *s2) {
526                 int ch1 = *s1++, ch2 = *s2++;
527                 if (isupper (ch1))
528                         ch1 = tolower (ch1);
529                 if (isupper (ch2))
530                         ch2 = tolower (ch2);
531                 if (ch1 != ch2)
532                         return ch1 - ch2;
533         }
534         return *s1 ? 1 : *s2 ? -1 : 0;
535 }
536 #endif
537
538 /*
539  * Set the S_CREATOR_OS field.  Return true if OS is known,
540  * otherwise, 0.
541  */
542 static int set_os(struct ext2_super_block *sb, char *os)
543 {
544         if (isdigit (*os))
545                 sb->s_creator_os = atoi (os);
546         else if (strcasecmp(os, "linux") == 0)
547                 sb->s_creator_os = EXT2_OS_LINUX;
548         else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
549                 sb->s_creator_os = EXT2_OS_HURD;
550         else if (strcasecmp(os, "masix") == 0)
551                 sb->s_creator_os = EXT2_OS_MASIX;
552         else
553                 return 0;
554         return 1;
555 }
556
557 #define PATH_SET "PATH=/sbin"
558
559 static void PRS(int argc, char *argv[])
560 {
561         char    c;
562         int     size;
563         char    * tmp;
564         int     inode_ratio = 4096;
565         int     reserved_ratio = 5;
566         errcode_t       retval;
567         char    *oldpath = getenv("PATH");
568
569         /* Update our PATH to include /sbin  */
570         if (oldpath) {
571                 char *newpath;
572                 
573                 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
574                 strcpy (newpath, PATH_SET);
575                 strcat (newpath, ":");
576                 strcat (newpath, oldpath);
577                 putenv (newpath);
578         } else
579                 putenv (PATH_SET);
580
581         setbuf(stdout, NULL);
582         setbuf(stderr, NULL);
583         initialize_ext2_error_table();
584         memset(&param, 0, sizeof(struct ext2_super_block));
585         
586         fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
587                  E2FSPROGS_VERSION, E2FSPROGS_DATE,
588                  EXT2FS_VERSION, EXT2FS_DATE);
589         if (argc && *argv)
590                 program_name = *argv;
591         while ((c = getopt (argc, argv,
592                             "b:cf:g:i:l:m:o:qr:tvI:SFL:M:")) != EOF)
593                 switch (c) {
594                 case 'b':
595                         size = strtoul(optarg, &tmp, 0);
596                         if (size < 1024 || size > 4096 || *tmp) {
597                                 com_err(program_name, 0, "bad block size - %s",
598                                         optarg);
599                                 exit(1);
600                         }
601                         param.s_log_block_size =
602                                 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
603                         break;
604                 case 'c':
605                 case 't':       /* Check for bad blocks */
606                         cflag = 1;
607                         break;
608                 case 'f':
609                         size = strtoul(optarg, &tmp, 0);
610                         if (size < 1024 || size > 4096 || *tmp) {
611                                 com_err(program_name, 0, "bad fragment size - %s",
612                                         optarg);
613                                 exit(1);
614                         }
615                         param.s_log_frag_size =
616                                 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
617                         printf("Warning: fragments not supported.  "
618                                "Ignoring -f option\n");
619                         break;
620                 case 'g':
621                         param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
622                         if (*tmp) {
623                                 com_err(program_name, 0,
624                                         "Illegal number for blocks per group");
625                                 exit(1);
626                         }
627                         if (param.s_blocks_per_group < 256 ||
628                             param.s_blocks_per_group > 8192 || *tmp) {
629                                 com_err(program_name, 0,
630                                         "blocks per group count out of range");
631                                 exit(1);
632                         }
633                         if ((param.s_blocks_per_group % 8) != 0) {
634                                 com_err(program_name, 0,
635                                 "blocks per group must be multiple of 8");
636                                 exit(1);
637                         }
638                         break;
639                 case 'i':
640                         inode_ratio = strtoul(optarg, &tmp, 0);
641                         if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
642                             *tmp) {
643                                 com_err(program_name, 0, "bad inode ratio - %s",
644                                         optarg);
645                                 exit(1);
646                         }
647                         break;
648                 case 'l':
649                         bad_blocks_filename = malloc(strlen(optarg)+1);
650                         if (!bad_blocks_filename) {
651                                 com_err(program_name, ENOMEM,
652                                         "in malloc for bad_blocks_filename");
653                                 exit(1);
654                         }
655                         strcpy(bad_blocks_filename, optarg);
656                         break;
657                 case 'm':
658                         reserved_ratio = strtoul(optarg, &tmp, 0);
659                         if (reserved_ratio > 50 || *tmp) {
660                                 com_err(program_name, 0,
661                                         "bad reserved blocks percent - %s",
662                                         optarg);
663                                 exit(1);
664                         }
665                         break;
666                 case 'o':
667                         creator_os = optarg;
668                         break;
669                 case 'r':
670                         param.s_rev_level = atoi(optarg);
671                         break;
672 #ifdef EXT2_DYNAMIC_REV
673                 case 'I':
674                         param.s_inode_size = atoi(optarg);
675                         break;
676 #endif
677                 case 'v':
678                         verbose = 1;
679                         break;
680                 case 'q':
681                         quiet = 1;
682                         break;
683                 case 'F':
684                         force = 1;
685                         break;
686                 case 'L':
687                         volume_label = optarg;
688                         break;
689                 case 'M':
690                         mount_dir = optarg;
691                         break;
692                 case 'S':
693                         super_only = 1;
694                         break;
695                 default:
696                         usage();
697                 }
698         if (optind == argc)
699                 usage();
700         device_name = argv[optind];
701         optind++;
702         if (optind < argc) {
703                 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
704                 if (*tmp) {
705                         com_err(program_name, 0, "bad blocks count - %s",
706                                 argv[optind - 1]);
707                         exit(1);
708                 }
709         }
710         if (optind < argc)
711                 usage();
712
713         if (!force)
714                 check_plausibility();
715         check_mount();
716
717         param.s_log_frag_size = param.s_log_block_size;
718
719         if (!param.s_blocks_count) {
720                 retval = ext2fs_get_device_size(device_name,
721                                                 EXT2_BLOCK_SIZE(&param),
722                                                 &param.s_blocks_count);
723                 if (retval) {
724                         com_err(program_name, retval,
725                                 "while trying to determine filesystem size");
726                         exit(1);
727                 }
728         }
729
730         /*
731          * Calculate number of inodes based on the inode ratio
732          */
733         param.s_inodes_count =
734                 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
735                         / inode_ratio;
736
737         /*
738          * Calculate number of blocks to reserve
739          */
740         param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
741 }
742                                         
743 int main (int argc, char *argv[])
744 {
745         errcode_t       retval = 0;
746         ext2_filsys     fs;
747         badblocks_list  bb_list = 0;
748         struct ext2fs_sb *s;
749         
750         PRS(argc, argv);
751
752         /*
753          * Initialize the superblock....
754          */
755         retval = ext2fs_initialize(device_name, 0, &param,
756                                    unix_io_manager, &fs);
757         if (retval) {
758                 com_err(device_name, retval, "while setting up superblock");
759                 exit(1);
760         }
761
762         /*
763          * Generate a UUID for it...
764          */
765         s = (struct ext2fs_sb *) fs->super;
766         uuid_generate(s->s_uuid);
767
768         /*
769          * Override the creator OS, if applicable
770          */
771         if (creator_os && !set_os(fs->super, creator_os)) {
772                 com_err (program_name, 0, "unknown os - %s", creator_os);
773                 exit(1);
774         }
775
776         /*
777          * Set the volume label...
778          */
779         if (volume_label) {
780                 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
781                 strncpy(s->s_volume_name, volume_label,
782                         sizeof(s->s_volume_name));
783         }
784
785         /*
786          * Set the last mount directory
787          */
788         if (mount_dir) {
789                 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
790                 strncpy(s->s_last_mounted, mount_dir,
791                         sizeof(s->s_last_mounted));
792         }
793         
794         if (!quiet)
795                 show_stats(fs);
796
797         if (bad_blocks_filename)
798                 read_bb_file(fs, &bb_list, bad_blocks_filename);
799         if (cflag)
800                 test_disk(fs, &bb_list);
801
802         handle_bad_blocks(fs, bb_list);
803         alloc_tables(fs);
804         if (super_only) {
805                 fs->super->s_state |= EXT2_ERROR_FS;
806                 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
807         } else {
808                 create_root_dir(fs);
809                 create_lost_and_found(fs);
810                 reserve_inodes(fs);
811                 create_bad_block_inode(fs, bb_list);
812 #ifdef ZAP_BOOTBLOCK
813                 zap_bootblock(fs);
814 #endif
815         }
816         
817         if (!quiet)
818                 printf("Writing superblocks and "
819                        "filesystem accounting information: ");
820         ext2fs_close(fs);
821         if (!quiet)
822                 printf("done\n");
823         return 0;
824 }