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