2 * mke2fs.c - Make a ext2fs filesystem.
4 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
12 /* Usage: mke2fs [options] device
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 :-).
24 #include <sys/utsname.h>
44 #include <sys/ioctl.h>
45 #include <sys/types.h>
47 #include "ext2fs/ext2_fs.h"
48 #include "et/com_err.h"
49 #include "uuid/uuid.h"
51 #include "ext2fs/ext2fs.h"
53 #include "../version.h"
54 #include "nls-enable.h"
56 #define STRIDE_LENGTH 8
62 extern int isatty(int);
63 extern FILE *fpopen(const char *cmd, const char *mode);
65 const char * program_name = "mke2fs";
66 const char * device_name /* = NULL */;
68 /* Command line options */
77 char *bad_blocks_filename;
80 struct ext2_super_block param;
85 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
87 int sys_page_size = 4096;
89 static void usage(void)
91 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
92 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
93 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
94 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
95 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
96 "[-r fs-revision] [-R raid_opts] [-qvSV] device [blocks-count]\n"),
101 static int int_log2(int arg)
113 static int int_log10(unsigned int arg)
123 * This function sets the default parameters for a filesystem
125 * The type is specified by the user. The size is the maximum size
126 * (in megabytes) for which a set of parameters applies, with a size
127 * of zero meaning that it is the default parameter for the type.
128 * Note that order is important in the table below.
130 #define DEF_MAX_BLOCKSIZE -1
131 static char default_str[] = "default";
132 struct mke2fs_defaults {
138 { default_str, 0, 4096, 8192 },
139 { default_str, 512, 1024, 4096 },
140 { default_str, 3, 1024, 8192 },
141 { "journal", 0, 4096, 8192 },
142 { "news", 0, 4096, 4096 },
143 { "largefile", 0, DEF_MAX_BLOCKSIZE, 1024 * 1024 },
144 { "largefile4", 0, DEF_MAX_BLOCKSIZE, 4096 * 1024 },
148 static void set_fs_defaults(const char *fs_type,
149 struct ext2_super_block *super,
150 int blocksize, int *inode_ratio)
154 struct mke2fs_defaults *p;
156 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
158 ratio = *inode_ratio;
160 fs_type = default_str;
161 for (p = settings; p->type; p++) {
162 if ((strcmp(p->type, fs_type) != 0) &&
163 (strcmp(p->type, default_str) != 0))
165 if ((p->size != 0) &&
169 *inode_ratio = p->inode_ratio;
170 if (blocksize == 0) {
171 if (p->blocksize == DEF_MAX_BLOCKSIZE)
172 p->blocksize = sys_page_size;
173 super->s_log_frag_size = super->s_log_block_size =
174 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
178 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
182 * Helper function for read_bb_file and test_disk
184 static void invalid_block(ext2_filsys fs, blk_t blk)
186 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
191 * Reads the bad blocks list from a file
193 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
194 const char *bad_blocks_file)
199 f = fopen(bad_blocks_file, "r");
201 com_err("read_bad_blocks_file", errno,
202 _("while trying to open %s"), bad_blocks_file);
205 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
208 com_err("ext2fs_read_bb_FILE", retval,
209 _("while reading in list of bad blocks from file"));
215 * Runs the badblocks program to test the disk
217 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
223 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
224 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
225 fs->device_name, fs->super->s_blocks_count);
227 printf(_("Running command: %s\n"), buf);
230 com_err("popen", errno,
231 _("while trying run '%s'"), buf);
234 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
237 com_err("ext2fs_read_bb_FILE", retval,
238 _("while processing list of bad blocks from program"));
243 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
248 badblocks_iterate bb_iter;
258 * The primary superblock and group descriptors *must* be
259 * good; if not, abort.
261 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
262 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
263 if (ext2fs_badblocks_list_test(bb_list, i)) {
264 fprintf(stderr, _("Block %d in primary "
265 "superblock/group descriptor area bad.\n"), i);
266 fprintf(stderr, _("Blocks %d through %d must be good "
267 "in order to build a filesystem.\n"),
268 fs->super->s_first_data_block, must_be_good);
269 fprintf(stderr, _("Aborting....\n"));
275 * See if any of the bad blocks are showing up in the backup
276 * superblocks and/or group descriptors. If so, issue a
277 * warning and adjust the block counts appropriately.
279 group_block = fs->super->s_first_data_block +
280 fs->super->s_blocks_per_group;
282 for (i = 1; i < fs->group_desc_count; i++) {
284 for (j=0; j < fs->desc_blocks+1; j++) {
285 if (ext2fs_badblocks_list_test(bb_list,
289 _("Warning: the backup superblock/group descriptors at block %d contain\n"
293 group = ext2fs_group_of_blk(fs, group_block+j);
294 fs->group_desc[group].bg_free_blocks_count++;
295 fs->super->s_free_blocks_count++;
298 group_block += fs->super->s_blocks_per_group;
302 * Mark all the bad blocks as used...
304 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
306 com_err("ext2fs_badblocks_list_iterate_begin", retval,
307 _("while marking bad blocks as used"));
310 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
311 ext2fs_mark_block_bitmap(fs->block_map, blk);
312 ext2fs_badblocks_list_iterate_end(bb_iter);
316 * These functions implement a generalized progress meter.
318 struct progress_struct {
324 static void progress_init(struct progress_struct *progress,
325 const char *label,__u32 max)
329 memset(progress, 0, sizeof(struct progress_struct));
334 * Figure out how many digits we need
337 sprintf(progress->format, "%%%dd/%%%dld", i, i);
338 memset(progress->backup, '\b', sizeof(progress->backup)-1);
339 progress->backup[sizeof(progress->backup)-1] = 0;
340 if ((2*i)+1 < sizeof(progress->backup))
341 progress->backup[(2*i)+1] = 0;
344 fputs(label, stdout);
348 static void progress_update(struct progress_struct *progress, __u32 val)
350 if (progress->format[0] == 0)
352 printf(progress->format, val, progress->max);
353 fputs(progress->backup, stdout);
356 static void progress_close(struct progress_struct *progress)
358 if (progress->format[0] == 0)
360 fputs(_("done \n"), stdout);
365 * Helper function which zeros out _num_ blocks starting at _blk_. In
366 * case of an error, the details of the error is returned via _ret_blk_
367 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
368 * success, and an error code on an error.
370 * As a special case, if the first argument is NULL, then it will
371 * attempt to free the static zeroizing buffer. (This is to keep
372 * programs that check for memory leaks happy.)
374 static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
375 struct progress_struct *progress,
376 blk_t *ret_blk, int *ret_count)
378 int j, count, next_update, next_update_incr;
382 /* If fs is null, clean up the static buffer and return */
390 /* Allocate the zeroizing buffer if necessary */
392 buf = malloc(fs->blocksize * STRIDE_LENGTH);
394 com_err("malloc", ENOMEM,
395 _("while allocating zeroizing buffer"));
398 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
400 /* OK, do the write loop */
402 next_update_incr = num / 100;
403 if (next_update_incr < 1)
404 next_update_incr = 1;
405 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
406 if (num-j > STRIDE_LENGTH)
407 count = STRIDE_LENGTH;
410 retval = io_channel_write_blk(fs->io, blk, count, buf);
418 if (progress && j > next_update) {
419 next_update += num / 100;
420 progress_update(progress, blk);
426 static void write_inode_tables(ext2_filsys fs)
431 struct progress_struct progress;
434 memset(&progress, 0, sizeof(progress));
436 progress_init(&progress, _("Writing inode tables: "),
437 fs->group_desc_count);
439 for (i = 0; i < fs->group_desc_count; i++) {
440 progress_update(&progress, i);
442 blk = fs->group_desc[i].bg_inode_table;
443 num = fs->inode_blocks_per_group;
445 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
447 fprintf(stderr, _("\nCould not write %d blocks "
448 "in inode table starting at %d: %s\n"),
449 num, blk, error_message(retval));
453 if (sync_kludge == 1)
455 else if ((i % sync_kludge) == 0)
459 zero_blocks(0, 0, 0, 0, 0, 0);
460 progress_close(&progress);
463 static void create_root_dir(ext2_filsys fs)
466 struct ext2_inode inode;
468 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
470 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
474 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
476 com_err("ext2fs_read_inode", retval,
477 _("while reading root inode"));
480 inode.i_uid = getuid();
482 inode.i_gid = getgid();
483 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
485 com_err("ext2fs_write_inode", retval,
486 _("while setting root inode ownership"));
492 static void create_lost_and_found(ext2_filsys fs)
496 const char *name = "lost+found";
501 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
503 com_err("ext2fs_mkdir", retval,
504 _("while creating /lost+found"));
508 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
510 com_err("ext2_lookup", retval,
511 _("while looking up /lost+found"));
515 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
516 if ((lpf_size += fs->blocksize) >= 16*1024)
518 retval = ext2fs_expand_dir(fs, ino);
520 com_err("ext2fs_expand_dir", retval,
521 _("while expanding /lost+found"));
527 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
531 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
532 fs->group_desc[0].bg_free_inodes_count--;
533 fs->super->s_free_inodes_count--;
534 retval = ext2fs_update_bb_inode(fs, bb_list);
536 com_err("ext2fs_update_bb_inode", retval,
537 _("while setting bad block inode"));
543 static void reserve_inodes(ext2_filsys fs)
548 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
549 ext2fs_mark_inode_bitmap(fs->inode_map, i);
550 group = ext2fs_group_of_ino(fs, i);
551 fs->group_desc[group].bg_free_inodes_count--;
552 fs->super->s_free_inodes_count--;
554 ext2fs_mark_ib_dirty(fs);
557 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
558 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
559 #define BSD_LABEL_OFFSET 64
561 static void zap_sector(ext2_filsys fs, int sect, int nsect)
567 buf = malloc(512*nsect);
569 printf(_("Out of memory erasing sectors %d-%d\n"),
570 sect, sect + nsect - 1);
573 memset(buf, 0, 512*nsect);
576 /* Check for a BSD disklabel, and don't erase it if so */
577 retval = io_channel_read_blk(fs->io, 0, -512, buf);
580 _("Warning: could not read block 0: %s\n"),
581 error_message(retval));
583 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
584 if ((*magic == BSD_DISKMAGIC) ||
585 (*magic == BSD_MAGICDISK))
590 io_channel_set_blksize(fs->io, 512);
591 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
592 io_channel_set_blksize(fs->io, fs->blocksize);
595 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
596 sect, error_message(retval));
599 static void create_journal_dev(ext2_filsys fs)
601 struct progress_struct progress;
607 retval = ext2fs_create_journal_superblock(fs,
608 fs->super->s_blocks_count, 0, &buf);
610 com_err("create_journal_dev", retval,
611 _("while initializing journal superblock"));
615 memset(&progress, 0, sizeof(progress));
617 progress_init(&progress, _("Zeroing journal device: "),
618 fs->super->s_blocks_count);
620 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
621 &progress, &blk, &count);
623 com_err("create_journal_dev", retval,
624 _("while zeroing journal device (block %u, count %d)"),
628 zero_blocks(0, 0, 0, 0, 0, 0);
630 retval = io_channel_write_blk(fs->io,
631 fs->super->s_first_data_block+1,
634 com_err("create_journal_dev", retval,
635 _("while writing journal superblock"));
638 progress_close(&progress);
641 static void show_stats(ext2_filsys fs)
643 struct ext2_super_block *s = fs->super;
646 int i, need, col_left;
648 if (param.s_blocks_count != s->s_blocks_count)
649 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
650 param.s_blocks_count - s->s_blocks_count);
652 memset(buf, 0, sizeof(buf));
653 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
654 printf(_("Filesystem label=%s\n"), buf);
655 printf(_("OS type: "));
656 switch (fs->super->s_creator_os) {
657 case EXT2_OS_LINUX: printf ("Linux"); break;
658 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
659 case EXT2_OS_MASIX: printf ("Masix"); break;
660 default: printf (_("(unknown os)"));
663 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
664 s->s_log_block_size);
665 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
667 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
669 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
671 100.0 * s->s_r_blocks_count / s->s_blocks_count);
672 printf(_("First data block=%u\n"), s->s_first_data_block);
673 if (fs->group_desc_count > 1)
674 printf(_("%u block groups\n"), fs->group_desc_count);
676 printf(_("%u block group\n"), fs->group_desc_count);
677 printf(_("%u blocks per group, %u fragments per group\n"),
678 s->s_blocks_per_group, s->s_frags_per_group);
679 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
681 if (fs->group_desc_count == 1) {
686 printf(_("Superblock backups stored on blocks: "));
687 group_block = s->s_first_data_block;
689 for (i = 1; i < fs->group_desc_count; i++) {
690 group_block += s->s_blocks_per_group;
691 if (!ext2fs_bg_has_super(fs, i))
695 need = int_log10(group_block) + 2;
696 if (need > col_left) {
701 printf("%u", group_block);
707 * Set the S_CREATOR_OS field. Return true if OS is known,
710 static int set_os(struct ext2_super_block *sb, char *os)
713 sb->s_creator_os = atoi (os);
714 else if (strcasecmp(os, "linux") == 0)
715 sb->s_creator_os = EXT2_OS_LINUX;
716 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
717 sb->s_creator_os = EXT2_OS_HURD;
718 else if (strcasecmp(os, "masix") == 0)
719 sb->s_creator_os = EXT2_OS_MASIX;
725 #define PATH_SET "PATH=/sbin"
727 static void parse_raid_opts(const char *opts)
729 char *buf, *token, *next, *p, *arg;
736 fprintf(stderr, _("Couldn't allocate memory to parse "
741 for (token = buf; token && *token; token = next) {
742 p = strchr(token, ',');
748 arg = strchr(token, '=');
753 if (strcmp(token, "stride") == 0) {
758 fs_stride = strtoul(arg, &p, 0);
759 if (*p || (fs_stride == 0)) {
761 _("Invalid stride parameter.\n"));
769 fprintf(stderr, _("\nBad raid options specified.\n\n"
770 "Raid options are separated by commas, "
771 "and may take an argument which\n"
772 "\tis set off by an equals ('=') sign.\n\n"
773 "Valid raid options are:\n"
774 "\tstride=<stride length in blocks>\n\n"));
779 static __u32 ok_features[3] = {
780 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
781 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
782 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
783 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
787 static void PRS(int argc, char *argv[])
792 blk_t group_blk_max = 8192;
796 int reserved_ratio = 5;
797 ext2_ino_t num_inodes = 0;
799 char * oldpath = getenv("PATH");
800 char * raid_opts = 0;
801 const char * fs_type = 0;
802 int default_features = 1;
809 /* Update our PATH to include /sbin */
813 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
814 strcpy (newpath, PATH_SET);
815 strcat (newpath, ":");
816 strcat (newpath, oldpath);
821 tmp = getenv("MKE2FS_SYNC");
823 sync_kludge = atoi(tmp);
825 /* Determine the system page size if possible */
827 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
828 #define _SC_PAGESIZE _SC_PAGE_SIZE
831 sysval = sysconf(_SC_PAGESIZE);
833 sys_page_size = sysval;
834 #endif /* _SC_PAGESIZE */
835 #endif /* HAVE_SYSCONF */
837 setbuf(stdout, NULL);
838 setbuf(stderr, NULL);
839 initialize_ext2_error_table();
840 memset(¶m, 0, sizeof(struct ext2_super_block));
841 param.s_rev_level = 1; /* Create revision 1 filesystems now */
842 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
843 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
850 if ((ut.release[0] == '1') ||
851 (ut.release[0] == '2' && ut.release[1] == '.' &&
852 ut.release[2] < '2' && ut.release[3] == '.')) {
853 param.s_rev_level = 0;
854 param.s_feature_incompat = 0;
855 param.s_feature_compat = 0;
856 param.s_feature_ro_compat = 0;
859 fprintf (stderr, "mke2fs %s (%s)\n",
860 E2FSPROGS_VERSION, E2FSPROGS_DATE);
863 program_name = get_progname(*argv);
865 /* If called as mkfs.ext3, create a journal inode */
866 if (!strcmp(program_name, "mkfs.ext3"))
870 while ((c = getopt (argc, argv,
871 "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF)
874 blocksize = strtoul(optarg, &tmp, 0);
875 if (blocksize < EXT2_MIN_BLOCK_SIZE ||
876 blocksize > EXT2_MAX_BLOCK_SIZE || *tmp) {
877 com_err(program_name, 0,
878 _("bad block size - %s"), optarg);
881 if (blocksize > 4096)
882 fprintf(stderr, _("Warning: blocksize %d not "
883 "usable on most systems.\n"),
885 param.s_log_block_size =
886 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
887 group_blk_max = blocksize * 8;
889 case 'c': /* Check for bad blocks */
890 case 't': /* deprecated */
894 size = strtoul(optarg, &tmp, 0);
895 if (size < EXT2_MIN_BLOCK_SIZE ||
896 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
897 com_err(program_name, 0,
898 _("bad fragment size - %s"),
902 param.s_log_frag_size =
903 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
904 fprintf(stderr, _("Warning: fragments not supported. "
905 "Ignoring -f option\n"));
908 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
910 com_err(program_name, 0,
911 _("Illegal number for blocks per group"));
914 if ((param.s_blocks_per_group % 8) != 0) {
915 com_err(program_name, 0,
916 _("blocks per group must be multiple of 8"));
921 inode_ratio = strtoul(optarg, &tmp, 0);
922 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
923 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
925 com_err(program_name, 0,
926 _("bad inode ratio %s (min %d/max %d"),
927 optarg, EXT2_MIN_BLOCK_SIZE,
928 EXT2_MAX_BLOCK_SIZE);
933 parse_journal_opts(optarg);
936 param.s_feature_compat |=
937 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
942 bad_blocks_filename = malloc(strlen(optarg)+1);
943 if (!bad_blocks_filename) {
944 com_err(program_name, ENOMEM,
945 _("in malloc for bad_blocks_filename"));
948 strcpy(bad_blocks_filename, optarg);
951 reserved_ratio = strtoul(optarg, &tmp, 0);
952 if (reserved_ratio > 50 || *tmp) {
953 com_err(program_name, 0,
954 _("bad reserved blocks percent - %s"),
966 param.s_rev_level = atoi(optarg);
967 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
968 param.s_feature_incompat = 0;
969 param.s_feature_compat = 0;
970 param.s_feature_ro_compat = 0;
973 case 's': /* deprecated */
975 param.s_feature_ro_compat |=
976 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
978 param.s_feature_ro_compat &=
979 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
981 #ifdef EXT2_DYNAMIC_REV
983 inode_size = strtoul(optarg, &tmp, 0);
985 com_err(program_name, 0,
986 _("bad inode size - %s"), optarg);
992 num_inodes = atoi(optarg);
1004 volume_label = optarg;
1010 if (!strcmp(optarg, "none") || default_features) {
1011 param.s_feature_compat = 0;
1012 param.s_feature_incompat = 0;
1013 param.s_feature_ro_compat = 0;
1014 default_features = 0;
1016 if (!strcmp(optarg, "none"))
1018 if (e2p_edit_feature(optarg,
1019 ¶m.s_feature_compat,
1022 _("Invalid filesystem option set: %s\n"), optarg);
1036 /* Print version number and exit */
1037 fprintf(stderr, _("\tUsing %s\n"),
1038 error_message(EXT2_ET_BASE));
1045 device_name = argv[optind];
1047 if (optind < argc) {
1048 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1050 if ((*tmp) || (tmp2 > 0xfffffffful)) {
1051 com_err(program_name, 0, _("bad blocks count - %s"),
1055 param.s_blocks_count = tmp2;
1061 parse_raid_opts(raid_opts);
1064 * If there's no blocksize specified and there is a journal
1065 * device, use it to figure out the blocksize
1067 if (blocksize == 0 && journal_device) {
1070 retval = ext2fs_open(journal_device,
1071 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1072 0, unix_io_manager, &jfs);
1074 com_err(program_name, retval,
1075 _("while trying to open journal device %s\n"),
1079 blocksize = jfs->blocksize;
1080 param.s_log_block_size =
1081 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1085 if (blocksize > sys_page_size) {
1087 com_err(program_name, 0,
1088 _("%d-byte blocks too big for system (max %d)"),
1089 blocksize, sys_page_size);
1092 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1093 "(max %d), forced to continue\n"),
1094 blocksize, sys_page_size);
1097 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1099 fs_type = "journal";
1101 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1102 param.s_feature_compat = 0;
1103 param.s_feature_ro_compat = 0;
1105 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1106 (param.s_feature_compat || param.s_feature_ro_compat ||
1107 param.s_feature_incompat))
1108 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1111 check_plausibility(device_name);
1112 check_mount(device_name, force, _("filesystem"));
1114 param.s_log_frag_size = param.s_log_block_size;
1116 if (noaction && param.s_blocks_count) {
1117 dev_size = param.s_blocks_count;
1120 retval = ext2fs_get_device_size(device_name,
1121 EXT2_BLOCK_SIZE(¶m),
1123 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1124 com_err(program_name, retval,
1125 _("while trying to determine filesystem size"));
1128 if (!param.s_blocks_count) {
1129 if (retval == EXT2_ET_UNIMPLEMENTED) {
1130 com_err(program_name, 0,
1131 _("Couldn't determine device size; you "
1132 "must specify\nthe size of the "
1136 if (dev_size == 0) {
1137 com_err(program_name, 0,
1138 _("Device size reported to be zero. "
1139 "Invalid partition specified, or\n\t"
1140 "partition table wasn't reread "
1141 "after running fdisk, due to\n\t"
1142 "a modified partition being busy "
1143 "and in use. You may need to reboot\n\t"
1144 "to re-read your partition table.\n"
1148 param.s_blocks_count = dev_size;
1151 } else if (!force && (param.s_blocks_count > dev_size)) {
1152 com_err(program_name, 0,
1153 _("Filesystem larger than apparent filesystem size."));
1158 * If the user asked for HAS_JOURNAL, then make sure a journal
1161 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1165 set_fs_defaults(fs_type, ¶m, blocksize, &inode_ratio);
1167 if (param.s_blocks_per_group) {
1168 if (param.s_blocks_per_group < 256 ||
1169 param.s_blocks_per_group > group_blk_max || *tmp) {
1170 com_err(program_name, 0,
1171 _("blocks per group count out of range"));
1177 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1178 inode_size > EXT2_BLOCK_SIZE(¶m) ||
1179 inode_size & (inode_size - 1)) {
1180 com_err(program_name, 0,
1181 _("bad inode size %d (min %d/max %d)"),
1182 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1183 EXT2_BLOCK_SIZE(¶m));
1186 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1187 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1188 "on most systems\n"),
1190 param.s_inode_size = inode_size;
1194 * Calculate number of inodes based on the inode ratio
1196 param.s_inodes_count = num_inodes ? num_inodes :
1197 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m))
1201 * Calculate number of blocks to reserve
1203 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1207 int main (int argc, char *argv[])
1209 errcode_t retval = 0;
1211 badblocks_list bb_list = 0;
1216 setlocale(LC_MESSAGES, "");
1217 setlocale(LC_CTYPE, "");
1218 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1219 textdomain(NLS_CAT_NAME);
1224 * Initialize the superblock....
1226 retval = ext2fs_initialize(device_name, 0, ¶m,
1227 unix_io_manager, &fs);
1229 com_err(device_name, retval, _("while setting up superblock"));
1234 * Wipe out the old on-disk superblock
1237 zap_sector(fs, 2, 6);
1240 * Generate a UUID for it...
1242 uuid_generate(fs->super->s_uuid);
1245 * Add "jitter" to the superblock's check interval so that we
1246 * don't check all the filesystems at the same time. We use a
1247 * kludgy hack of using the UUID to derive a random jitter value.
1249 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1250 val += fs->super->s_uuid[i];
1251 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1254 * Override the creator OS, if applicable
1256 if (creator_os && !set_os(fs->super, creator_os)) {
1257 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1262 * For the Hurd, we will turn off filetype since it doesn't
1265 if (fs->super->s_creator_os == EXT2_OS_HURD)
1266 fs->super->s_feature_incompat &=
1267 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1270 * Set the volume label...
1273 memset(fs->super->s_volume_name, 0,
1274 sizeof(fs->super->s_volume_name));
1275 strncpy(fs->super->s_volume_name, volume_label,
1276 sizeof(fs->super->s_volume_name));
1280 * Set the last mount directory
1283 memset(fs->super->s_last_mounted, 0,
1284 sizeof(fs->super->s_last_mounted));
1285 strncpy(fs->super->s_last_mounted, mount_dir,
1286 sizeof(fs->super->s_last_mounted));
1289 if (!quiet || noaction)
1295 if (fs->super->s_feature_incompat &
1296 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1297 create_journal_dev(fs);
1298 exit(ext2fs_close(fs) ? 1 : 0);
1301 if (bad_blocks_filename)
1302 read_bb_file(fs, &bb_list, bad_blocks_filename);
1304 test_disk(fs, &bb_list);
1306 handle_bad_blocks(fs, bb_list);
1307 fs->stride = fs_stride;
1308 retval = ext2fs_allocate_tables(fs);
1310 com_err(program_name, retval,
1311 _("while trying to allocate filesystem tables"));
1315 fs->super->s_state |= EXT2_ERROR_FS;
1316 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1318 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1319 int rsv = 65536 / fs->blocksize;
1320 unsigned long blocks = fs->super->s_blocks_count;
1321 unsigned long start;
1324 #ifdef ZAP_BOOTBLOCK
1325 zap_sector(fs, 0, 2);
1329 * Wipe out any old MD RAID (or other) metadata at the end
1330 * of the device. This will also verify that the device is
1331 * as large as we think. Be careful with very small devices.
1333 start = (blocks & ~(rsv - 1));
1337 retval = zero_blocks(fs, start, blocks - start,
1338 NULL, &ret_blk, NULL);
1341 com_err(program_name, retval,
1342 _("zeroing block %u at end of filesystem"),
1346 write_inode_tables(fs);
1347 create_root_dir(fs);
1348 create_lost_and_found(fs);
1350 create_bad_block_inode(fs, bb_list);
1353 if (journal_device) {
1357 check_plausibility(journal_device);
1358 check_mount(journal_device, force, _("journal"));
1360 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1361 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1362 fs->blocksize, unix_io_manager, &jfs);
1364 com_err(program_name, retval,
1365 _("while trying to open journal device %s\n"),
1370 printf(_("Adding journal to device %s: "),
1374 retval = ext2fs_add_journal_device(fs, jfs);
1376 com_err (program_name, retval,
1377 _("\n\twhile trying to add journal to device %s"),
1382 printf(_("done\n"));
1384 free(journal_device);
1385 } else if (journal_size) {
1386 journal_blocks = figure_journal_size(journal_size, fs);
1388 if (!journal_blocks) {
1389 fs->super->s_feature_compat &=
1390 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1394 printf(_("Creating journal (%d blocks): "),
1398 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1401 com_err (program_name, retval,
1402 _("\n\twhile trying to create journal"));
1406 printf(_("done\n"));
1411 printf(_("Writing superblocks and "
1412 "filesystem accounting information: "));
1413 retval = ext2fs_flush(fs);
1416 _("\nWarning, had trouble writing out superblocks."));
1419 printf(_("done\n\n"));
1420 print_check_message(fs);
1422 val = ext2fs_close(fs);
1423 return (retval || val) ? 1 : 0;