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