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