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