blob: 6750867cf39402310a8f802a8796d7bc3c28229d [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
550 buf = malloc(512*nsect);
551 if (!buf) {
552 printf(_("Out of memory erasing sectors %d-%d\n"),
553 sect, sect + nsect - 1);
554 exit(1);
555 }
556
557 if (sect == 0) {
558 /* Check for a BSD disklabel, and don't erase it if so */
559 retval = io_channel_read_blk(fs->io, 0, -512, buf);
560 if (retval)
561 bb_error_msg("Warning: could not read block 0");
562 else {
563 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
564 if ((*magic == BSD_DISKMAGIC) ||
565 (*magic == BSD_MAGICDISK))
566 return;
567 }
568 }
569
570 memset(buf, 0, 512*nsect);
571 io_channel_set_blksize(fs->io, 512);
572 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
573 io_channel_set_blksize(fs->io, fs->blocksize);
574 free(buf);
575 if (retval)
576 bb_error_msg("Warning: could not erase sector %d", sect);
577}
578
579static void create_journal_dev(ext2_filsys fs)
580{
581 struct progress_struct progress;
582 errcode_t retval;
583 char *buf;
584 blk_t blk;
585 int count;
586
587 retval = ext2fs_create_journal_superblock(fs,
588 fs->super->s_blocks_count, 0, &buf);
589 if (retval) {
590 bb_error_msg_and_die("Could not init journal superblock");
591 }
592 if (quiet)
593 memset(&progress, 0, sizeof(progress));
594 else
595 progress_init(&progress, _("Zeroing journal device: "),
596 fs->super->s_blocks_count);
597
598 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
599 &progress, &blk, &count);
600 if (retval) {
601 bb_error_msg_and_die("Could not zero journal device (block %u, count %d)",
602 blk, count);
603 }
604 zero_blocks(0, 0, 0, 0, 0, 0);
605
606 retval = io_channel_write_blk(fs->io,
607 fs->super->s_first_data_block+1,
608 1, buf);
609 if (retval) {
610 bb_error_msg_and_die("Could not write journal superblock");
611 }
612 progress_close(&progress);
613}
614
615static void show_stats(ext2_filsys fs)
616{
617 struct ext2_super_block *s = fs->super;
618 char buf[80];
619 char *os;
620 blk_t group_block;
621 dgrp_t i;
622 int need, col_left;
623
624 if (param.s_blocks_count != s->s_blocks_count)
625 bb_error_msg("warning: %d blocks unused\n",
626 param.s_blocks_count - s->s_blocks_count);
627
628 memset(buf, 0, sizeof(buf));
629 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
630 printf("Filesystem label=%s\n", buf);
631 fputs(_("OS type: "), stdout);
632 os = e2p_os2string(fs->super->s_creator_os);
633 fputs(os, stdout);
634 free(os);
635 printf("\n");
636 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
637 s->s_log_block_size);
638 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
639 s->s_log_frag_size);
640 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
641 s->s_blocks_count);
642 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
643 s->s_r_blocks_count,
644 100.0 * s->s_r_blocks_count / s->s_blocks_count);
645 printf(_("First data block=%u\n"), s->s_first_data_block);
646 if (s->s_reserved_gdt_blocks)
647 printf(_("Maximum filesystem blocks=%lu\n"),
648 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
649 (fs->blocksize / sizeof(struct ext2_group_desc)) *
650 s->s_blocks_per_group);
651 if (fs->group_desc_count > 1)
652 printf(_("%u block groups\n"), fs->group_desc_count);
653 else
654 printf(_("%u block group\n"), fs->group_desc_count);
655 printf(_("%u blocks per group, %u fragments per group\n"),
656 s->s_blocks_per_group, s->s_frags_per_group);
657 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
658
659 if (fs->group_desc_count == 1) {
660 printf("\n");
661 return;
662 }
663
664 printf(_("Superblock backups stored on blocks: "));
665 group_block = s->s_first_data_block;
666 col_left = 0;
667 for (i = 1; i < fs->group_desc_count; i++) {
668 group_block += s->s_blocks_per_group;
669 if (!ext2fs_bg_has_super(fs, i))
670 continue;
671 if (i != 1)
672 printf(", ");
673 need = int_log10(group_block) + 2;
674 if (need > col_left) {
675 printf("\n\t");
676 col_left = 72;
677 }
678 col_left -= need;
679 printf("%u", group_block);
680 }
681 printf("\n\n");
682}
683
684/*
685 * Set the S_CREATOR_OS field. Return true if OS is known,
686 * otherwise, 0.
687 */
688static int set_os(struct ext2_super_block *sb, char *os)
689{
690 if (isdigit (*os))
691 sb->s_creator_os = atoi (os);
692 else if (strcasecmp(os, "linux") == 0)
693 sb->s_creator_os = EXT2_OS_LINUX;
694 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
695 sb->s_creator_os = EXT2_OS_HURD;
696 else if (strcasecmp(os, "masix") == 0)
697 sb->s_creator_os = EXT2_OS_MASIX;
698 else if (strcasecmp(os, "freebsd") == 0)
699 sb->s_creator_os = EXT2_OS_FREEBSD;
700 else if (strcasecmp(os, "lites") == 0)
701 sb->s_creator_os = EXT2_OS_LITES;
702 else
703 return 0;
704
705 return 1;
706}
707
708#define PATH_SET "PATH=/sbin"
709
710static void parse_extended_opts(struct ext2_super_block *sb_param,
711 const char *opts)
712{
713 char *buf, *token, *next, *p, *arg;
714 int len;
715 int r_usage = 0;
716
717 len = strlen(opts);
718 buf = xmalloc(len+1);
719 strcpy(buf, opts);
720 for (token = buf; token && *token; token = next) {
721 p = strchr(token, ',');
722 next = 0;
723 if (p) {
724 *p = 0;
725 next = p+1;
726 }
727 arg = strchr(token, '=');
728 if (arg) {
729 *arg = 0;
730 arg++;
731 }
732 if (strcmp(token, "stride") == 0) {
733 if (!arg) {
734 r_usage++;
735 continue;
736 }
737 fs_stride = strtoul(arg, &p, 0);
738 if (*p || (fs_stride == 0)) {
739 bb_error_msg("Invalid stride parameter");
740 r_usage++;
741 continue;
742 }
743 } else if (!strcmp(token, "resize")) {
744 unsigned long resize, bpg, rsv_groups;
745 unsigned long group_desc_count, desc_blocks;
746 unsigned int gdpb, blocksize;
747 int rsv_gdb;
748
749 if (!arg) {
750 r_usage++;
751 continue;
752 }
753
754 resize = parse_num_blocks(arg,
755 sb_param->s_log_block_size);
756
757 if (resize == 0) {
758 bb_error_msg("Invalid resize parameter: %s", arg);
759 r_usage++;
760 continue;
761 }
762 if (resize <= sb_param->s_blocks_count) {
763 bb_error_msg("The resize maximum must be greater than the filesystem size");
764 r_usage++;
765 continue;
766 }
767
768 blocksize = EXT2_BLOCK_SIZE(sb_param);
769 bpg = sb_param->s_blocks_per_group;
770 if (!bpg)
771 bpg = blocksize * 8;
772 gdpb = blocksize / sizeof(struct ext2_group_desc);
773 group_desc_count = (sb_param->s_blocks_count +
774 bpg - 1) / bpg;
775 desc_blocks = (group_desc_count +
776 gdpb - 1) / gdpb;
777 rsv_groups = (resize + bpg - 1) / bpg;
778 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
779 desc_blocks;
780 if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param))
781 rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param);
782
783 if (rsv_gdb > 0) {
784 sb_param->s_feature_compat |=
785 EXT2_FEATURE_COMPAT_RESIZE_INODE;
786
787 sb_param->s_reserved_gdt_blocks = rsv_gdb;
788 }
789 } else
790 r_usage++;
791 }
792 if (r_usage) {
793 bb_error_msg_and_die(
794 "\nBad options specified.\n\n"
795 "Options are separated by commas, "
796 "and may take an argument which\n"
797 "\tis set off by an equals ('=') sign.\n\n"
798 "Valid raid options are:\n"
799 "\tstride=<stride length in blocks>\n"
800 "\tresize=<resize maximum size in blocks>\n");
801 }
802}
803
804static __u32 ok_features[3] = {
805 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
806 EXT2_FEATURE_COMPAT_RESIZE_INODE |
807 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
808 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
809 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
810 EXT2_FEATURE_INCOMPAT_META_BG,
811 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
812};
813
814
815static void PRS(int argc, char *argv[])
816{
817 int b, c;
818 int size;
819 char * tmp;
820 int blocksize = 0;
821 int inode_ratio = 0;
822 int inode_size = 0;
823 int reserved_ratio = 5;
824 int sector_size = 0;
825 int show_version_only = 0;
826 ext2_ino_t num_inodes = 0;
827 errcode_t retval;
828 char * oldpath = getenv("PATH");
829 char * extended_opts = 0;
830 const char * fs_type = 0;
831 blk_t dev_size;
832#ifdef __linux__
833 struct utsname ut;
834#endif
835 long sysval;
836
837 /* Update our PATH to include /sbin */
838 if (oldpath) {
839 char *newpath;
840
841 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
842 strcpy (newpath, PATH_SET);
843 strcat (newpath, ":");
844 strcat (newpath, oldpath);
845 putenv (newpath);
846 } else
847 putenv (PATH_SET);
848
849 tmp = getenv("MKE2FS_SYNC");
850 if (tmp)
851 sync_kludge = atoi(tmp);
852
853 /* Determine the system page size if possible */
854#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
855#define _SC_PAGESIZE _SC_PAGE_SIZE
856#endif
857#ifdef _SC_PAGESIZE
858 sysval = sysconf(_SC_PAGESIZE);
859 if (sysval > 0)
860 sys_page_size = sysval;
861#endif /* _SC_PAGESIZE */
862
863 setbuf(stdout, NULL);
864 setbuf(stderr, NULL);
865 memset(&param, 0, sizeof(struct ext2_super_block));
866 param.s_rev_level = 1; /* Create revision 1 filesystems now */
867 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
868 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
869#if 0
870 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
871#endif
872
873#ifdef __linux__
874 if (uname(&ut)) {
875 perror("uname");
876 exit(1);
877 }
878 linux_version_code = parse_version_number(ut.release);
879 if (linux_version_code && linux_version_code < (2*65536 + 2*256)) {
880 param.s_rev_level = 0;
881 param.s_feature_incompat = 0;
882 param.s_feature_compat = 0;
883 param.s_feature_ro_compat = 0;
884 }
885#endif
886
887 if (argc && *argv) {
888 program_name = basename(*argv);
889
890 /* If called as mkfs.ext3, create a journal inode */
891 if (!strcmp(*argv + strlen(*argv) - 9, "mkfs.ext3"))
892 journal_size = -1;
893 }
894
895 while ((c = getopt (argc, argv,
896 "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
897 switch (c) {
898 case 'b':
899 blocksize = strtol(optarg, &tmp, 0);
900 b = (blocksize > 0) ? blocksize : -blocksize;
901 if (b < EXT2_MIN_BLOCK_SIZE ||
902 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
903 bb_error_msg_and_die("bad block size - %s", optarg);
904 }
905 if (blocksize > 4096)
906 bb_error_msg(
907 "Warning: blocksize %d not usable on most systems",
908 blocksize);
909 if (blocksize > 0)
910 param.s_log_block_size =
911 int_log2(blocksize >>
912 EXT2_MIN_BLOCK_LOG_SIZE);
913 break;
914 case 'c': /* Check for bad blocks */
915 case 't': /* deprecated */
916 cflag++;
917 break;
918 case 'f':
919 size = strtoul(optarg, &tmp, 0);
920 if (size < EXT2_MIN_BLOCK_SIZE ||
921 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
922 bb_error_msg_and_die("bad fragment size - %s", optarg);
923 }
924 param.s_log_frag_size =
925 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
926 bb_error_msg(
927 "Warning: fragments not supported. "
928 "Ignoring -f option");
929 break;
930 case 'g':
931 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
932 if (*tmp) {
933 bb_error_msg_and_die("Illegal number for blocks per group");
934 }
935 if ((param.s_blocks_per_group % 8) != 0) {
936 bb_error_msg_and_die("blocks per group must be multiple of 8");
937 }
938 break;
939 case 'i':
940 inode_ratio = strtoul(optarg, &tmp, 0);
941 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
942 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
943 *tmp) {
944 bb_error_msg_and_die("bad inode ratio %s (min %d/max %d",
945 optarg, EXT2_MIN_BLOCK_SIZE,
946 EXT2_MAX_BLOCK_SIZE);
947 }
948 break;
949 case 'J':
950 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
951 break;
952 case 'j':
953 param.s_feature_compat |=
954 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
955 if (!journal_size)
956 journal_size = -1;
957 break;
958 case 'l':
959 bad_blocks_filename = xmalloc(strlen(optarg)+1);
960 strcpy(bad_blocks_filename, optarg);
961 break;
962 case 'm':
963 reserved_ratio = strtoul(optarg, &tmp, 0);
964 if (reserved_ratio > 50 || *tmp) {
965 bb_error_msg_and_die("bad reserved blocks percent - %s", optarg);
966 }
967 break;
968 case 'n':
969 noaction++;
970 break;
971 case 'o':
972 creator_os = optarg;
973 break;
974 case 'r':
975 param.s_rev_level = atoi(optarg);
976 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
977 param.s_feature_incompat = 0;
978 param.s_feature_compat = 0;
979 param.s_feature_ro_compat = 0;
980 }
981 break;
982 case 's': /* deprecated */
983 if (atoi(optarg))
984 param.s_feature_ro_compat |=
985 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
986 else
987 param.s_feature_ro_compat &=
988 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
989 break;
990#ifdef EXT2_DYNAMIC_REV
991 case 'I':
992 inode_size = strtoul(optarg, &tmp, 0);
993 if (*tmp) {
994 bb_error_msg_and_die("bad inode size - %s", optarg);
995 }
996 break;
997#endif
998 case 'N':
999 num_inodes = atoi(optarg);
1000 break;
1001 case 'v':
1002 verbose = 1;
1003 break;
1004 case 'q':
1005 quiet = 1;
1006 break;
1007 case 'F':
1008 force = 1;
1009 break;
1010 case 'L':
1011 volume_label = optarg;
1012 break;
1013 case 'M':
1014 mount_dir = optarg;
1015 break;
1016 case 'O':
1017 if (!strcmp(optarg, "none")) {
1018 param.s_feature_compat = 0;
1019 param.s_feature_incompat = 0;
1020 param.s_feature_ro_compat = 0;
1021 break;
1022 }
1023 if (e2p_edit_feature(optarg,
1024 &param.s_feature_compat,
1025 ok_features)) {
1026 bb_error_msg_and_die("Invalid filesystem option set: %s", optarg);
1027 }
1028 break;
1029 case 'E':
1030 case 'R':
1031 extended_opts = optarg;
1032 break;
1033 case 'S':
1034 super_only = 1;
1035 break;
1036 case 'T':
1037 fs_type = optarg;
1038 break;
1039 case 'V':
1040 /* Print version number and exit */
1041 show_version_only++;
1042 break;
1043 default:
1044 bb_show_usage();
1045 }
1046 }
1047 if ((optind == argc) && !show_version_only)
1048 bb_show_usage();
1049 device_name = argv[optind++];
1050
1051 if (!quiet || show_version_only)
1052 bb_error_msg("mke2fs %s (%s)", E2FSPROGS_VERSION,
1053 E2FSPROGS_DATE);
1054
1055 if (show_version_only) {
1056 exit(0);
1057 }
1058
1059 /*
1060 * If there's no blocksize specified and there is a journal
1061 * device, use it to figure out the blocksize
1062 */
1063 if (blocksize <= 0 && journal_device) {
1064 ext2_filsys jfs;
1065 io_manager io_ptr;
1066
1067#ifdef CONFIG_TESTIO_DEBUG
1068 io_ptr = test_io_manager;
1069 test_io_backing_manager = unix_io_manager;
1070#else
1071 io_ptr = unix_io_manager;
1072#endif
1073 retval = ext2fs_open(journal_device,
1074 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1075 0, io_ptr, &jfs);
1076 if (retval) {
1077 bb_error_msg_and_die("Could not open journal device %s", journal_device);
1078 }
1079 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1080 bb_error_msg_and_die(
1081 "Journal dev blocksize (%d) smaller than "
1082 "minimum blocksize %d\n", jfs->blocksize,
1083 -blocksize);
1084 }
1085 blocksize = jfs->blocksize;
1086 param.s_log_block_size =
1087 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1088 ext2fs_close(jfs);
1089 }
1090
1091 if (blocksize > sys_page_size) {
1092 if (!force) {
1093 bb_error_msg("%d-byte blocks too big for system (max %d)",
1094 blocksize, sys_page_size);
1095 proceed_question();
1096 }
1097 bb_error_msg(
1098 "Warning: %d-byte blocks too big for system "
1099 "(max %d), forced to continue",
1100 blocksize, sys_page_size);
1101 }
1102 if ((blocksize > 4096) &&
1103 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1104 bb_error_msg(
1105 "\nWarning: some 2.4 kernels do not support "
1106 "blocksizes greater than 4096 \n\tusing ext3."
1107 " Use -b 4096 if this is an issue for you\n");
1108
1109 if (optind < argc) {
1110 param.s_blocks_count = parse_num_blocks(argv[optind++],
1111 param.s_log_block_size);
1112 if (!param.s_blocks_count) {
1113 bb_error_msg_and_die("bad blocks count - %s", argv[optind - 1]);
1114 }
1115 }
1116 if (optind < argc)
1117 bb_show_usage();
1118
1119 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1120 if (!fs_type)
1121 fs_type = "journal";
1122 reserved_ratio = 0;
1123 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1124 param.s_feature_compat = 0;
1125 param.s_feature_ro_compat = 0;
1126 }
1127 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1128 (param.s_feature_compat || param.s_feature_ro_compat ||
1129 param.s_feature_incompat))
1130 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1131
1132 if (!force)
1133 check_plausibility(device_name);
1134 check_mount(device_name, force, _("filesystem"));
1135
1136 param.s_log_frag_size = param.s_log_block_size;
1137
1138 if (noaction && param.s_blocks_count) {
1139 dev_size = param.s_blocks_count;
1140 retval = 0;
1141 } else {
1142 retry:
1143 retval = ext2fs_get_device_size(device_name,
1144 EXT2_BLOCK_SIZE(&param),
1145 &dev_size);
1146 if ((retval == EFBIG) &&
1147 (blocksize == 0) &&
1148 (param.s_log_block_size == 0)) {
1149 param.s_log_block_size = 2;
1150 blocksize = 4096;
1151 goto retry;
1152 }
1153 }
1154
1155 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1156 bb_error_msg_and_die("Could not determine filesystem size");
1157 }
1158 if (!param.s_blocks_count) {
1159 if (retval == EXT2_ET_UNIMPLEMENTED) {
1160 bb_error_msg_and_die(
1161 "Couldn't determine device size; you "
1162 "must specify\nthe size of the "
1163 "filesystem");
1164 } else {
1165 if (dev_size == 0) {
1166 bb_error_msg_and_die(
1167 "Device size reported to be zero. "
1168 "Invalid partition specified, or\n\t"
1169 "partition table wasn't reread "
1170 "after running fdisk, due to\n\t"
1171 "a modified partition being busy "
1172 "and in use. You may need to reboot\n\t"
1173 "to re-read your partition table.\n"
1174 );
1175 }
1176 param.s_blocks_count = dev_size;
1177 if (sys_page_size > EXT2_BLOCK_SIZE(&param))
1178 param.s_blocks_count &= ~((sys_page_size /
1179 EXT2_BLOCK_SIZE(&param))-1);
1180 }
1181
1182 } else if (!force && (param.s_blocks_count > dev_size)) {
1183 bb_error_msg("Filesystem larger than apparent device size");
1184 proceed_question();
1185 }
1186
1187 /*
1188 * If the user asked for HAS_JOURNAL, then make sure a journal
1189 * gets created.
1190 */
1191 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1192 !journal_size)
1193 journal_size = -1;
1194
1195 /* Set first meta blockgroup via an environment variable */
1196 /* (this is mostly for debugging purposes) */
1197 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1198 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1199 param.s_first_meta_bg = atoi(tmp);
1200
1201 /* Get the hardware sector size, if available */
1202 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1203 if (retval) {
1204 bb_error_msg_and_die("Could not determine hardware sector size");
1205 }
1206
1207 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1208 sector_size = atoi(tmp);
1209
1210 set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
1211 blocksize = EXT2_BLOCK_SIZE(&param);
1212
1213 if (extended_opts)
1214 parse_extended_opts(&param, extended_opts);
1215
1216 /* Since sparse_super is the default, we would only have a problem
1217 * here if it was explicitly disabled.
1218 */
1219 if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1220 !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1221 bb_error_msg_and_die("reserved online resize blocks not supported "
1222 "on non-sparse filesystem");
1223 }
1224
1225 if (param.s_blocks_per_group) {
1226 if (param.s_blocks_per_group < 256 ||
1227 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1228 bb_error_msg_and_die("blocks per group count out of range");
1229 }
1230 }
1231
1232 if (inode_size) {
1233 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1234 inode_size > EXT2_BLOCK_SIZE(&param) ||
1235 inode_size & (inode_size - 1)) {
1236 bb_error_msg_and_die("bad inode size %d (min %d/max %d)",
1237 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1238 blocksize);
1239 }
1240 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1241 bb_error_msg(
1242 "Warning: %d-byte inodes not usable on most systems",
1243 inode_size);
1244 param.s_inode_size = inode_size;
1245 }
1246
1247 /*
1248 * Calculate number of inodes based on the inode ratio
1249 */
1250 param.s_inodes_count = num_inodes ? num_inodes :
1251 ((__u64) param.s_blocks_count * blocksize)
1252 / inode_ratio;
1253
1254 /*
1255 * Calculate number of blocks to reserve
1256 */
1257 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1258}
1259
1260int mke2fs_main (int argc, char *argv[])
1261{
1262 errcode_t retval = 0;
1263 ext2_filsys fs;
1264 badblocks_list bb_list = 0;
1265 int journal_blocks;
1266 unsigned int i;
1267 int val;
1268 io_manager io_ptr;
1269
1270#ifdef ENABLE_NLS
1271 setlocale(LC_MESSAGES, "");
1272 setlocale(LC_CTYPE, "");
1273 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1274 textdomain(NLS_CAT_NAME);
1275#endif
1276 PRS(argc, argv);
1277
1278#ifdef CONFIG_TESTIO_DEBUG
1279 io_ptr = test_io_manager;
1280 test_io_backing_manager = unix_io_manager;
1281#else
1282 io_ptr = unix_io_manager;
1283#endif
1284
1285 /*
1286 * Initialize the superblock....
1287 */
1288 retval = ext2fs_initialize(device_name, 0, &param,
1289 io_ptr, &fs);
1290 if (retval) {
1291 bb_error_msg_and_die("Could not set up superblock");
1292 }
1293
1294 /*
1295 * Wipe out the old on-disk superblock
1296 */
1297 if (!noaction)
1298 zap_sector(fs, 2, 6);
1299
1300 /*
1301 * Generate a UUID for it...
1302 */
1303 uuid_generate(fs->super->s_uuid);
1304
1305 /*
1306 * Initialize the directory index variables
1307 */
1308 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1309 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1310
1311 /*
1312 * Add "jitter" to the superblock's check interval so that we
1313 * don't check all the filesystems at the same time. We use a
1314 * kludgy hack of using the UUID to derive a random jitter value.
1315 */
1316 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1317 val += fs->super->s_uuid[i];
1318 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1319
1320 /*
1321 * Override the creator OS, if applicable
1322 */
1323 if (creator_os && !set_os(fs->super, creator_os)) {
1324 bb_error_msg_and_die("unknown os - %s", creator_os);
1325 }
1326
1327 /*
1328 * For the Hurd, we will turn off filetype since it doesn't
1329 * support it.
1330 */
1331 if (fs->super->s_creator_os == EXT2_OS_HURD)
1332 fs->super->s_feature_incompat &=
1333 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1334
1335 /*
1336 * Set the volume label...
1337 */
1338 if (volume_label) {
1339 memset(fs->super->s_volume_name, 0,
1340 sizeof(fs->super->s_volume_name));
1341 strncpy(fs->super->s_volume_name, volume_label,
1342 sizeof(fs->super->s_volume_name));
1343 }
1344
1345 /*
1346 * Set the last mount directory
1347 */
1348 if (mount_dir) {
1349 memset(fs->super->s_last_mounted, 0,
1350 sizeof(fs->super->s_last_mounted));
1351 strncpy(fs->super->s_last_mounted, mount_dir,
1352 sizeof(fs->super->s_last_mounted));
1353 }
1354
1355 if (!quiet || noaction)
1356 show_stats(fs);
1357
1358 if (noaction)
1359 exit(0);
1360
1361 if (fs->super->s_feature_incompat &
1362 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1363 create_journal_dev(fs);
1364 exit(ext2fs_close(fs) ? 1 : 0);
1365 }
1366
1367 if (bad_blocks_filename)
1368 read_bb_file(fs, &bb_list, bad_blocks_filename);
1369 if (cflag)
1370 test_disk(fs, &bb_list);
1371
1372 handle_bad_blocks(fs, bb_list);
1373 fs->stride = fs_stride;
1374 retval = ext2fs_allocate_tables(fs);
1375 if (retval) {
1376 bb_error_msg_and_die("Could not allocate filesystem tables");
1377 }
1378 if (super_only) {
1379 fs->super->s_state |= EXT2_ERROR_FS;
1380 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1381 } else {
1382 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1383 unsigned int rsv = 65536 / fs->blocksize;
1384 unsigned long blocks = fs->super->s_blocks_count;
1385 unsigned long start;
1386 blk_t ret_blk;
1387
1388#ifdef ZAP_BOOTBLOCK
1389 zap_sector(fs, 0, 2);
1390#endif
1391
1392 /*
1393 * Wipe out any old MD RAID (or other) metadata at the end
1394 * of the device. This will also verify that the device is
1395 * as large as we think. Be careful with very small devices.
1396 */
1397 start = (blocks & ~(rsv - 1));
1398 if (start > rsv)
1399 start -= rsv;
1400 if (start > 0)
1401 retval = zero_blocks(fs, start, blocks - start,
1402 NULL, &ret_blk, NULL);
1403
1404 if (retval) {
1405 bb_error_msg("Could not zero block %u at end of filesystem", ret_blk);
1406 }
1407 write_inode_tables(fs);
1408 create_root_dir(fs);
1409 create_lost_and_found(fs);
1410 reserve_inodes(fs);
1411 create_bad_block_inode(fs, bb_list);
1412 if (fs->super->s_feature_compat &
1413 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1414 retval = ext2fs_create_resize_inode(fs);
1415 if (retval) {
1416 bb_error_msg_and_die("Could not reserve blocks for online resize");
1417 }
1418 }
1419 }
1420
1421 if (journal_device) {
1422 ext2_filsys jfs;
1423
1424 if (!force)
1425 check_plausibility(journal_device);
1426 check_mount(journal_device, force, _("journal"));
1427
1428 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1429 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1430 fs->blocksize, unix_io_manager, &jfs);
1431 if (retval) {
1432 bb_error_msg_and_die("Could not open journal device %s", journal_device);
1433 }
1434 if (!quiet) {
1435 printf("Adding journal to device %s: ", journal_device);
1436 fflush(stdout);
1437 }
1438 retval = ext2fs_add_journal_device(fs, jfs);
1439 if(retval) {
1440 bb_error_msg_and_die("Could not add journal to device %s", journal_device);
1441 }
1442 if (!quiet)
1443 printf("done\n");
1444 ext2fs_close(jfs);
1445 free(journal_device);
1446 } else if (journal_size) {
1447 journal_blocks = figure_journal_size(journal_size, fs);
1448
1449 if (!journal_blocks) {
1450 fs->super->s_feature_compat &=
1451 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1452 goto no_journal;
1453 }
1454 if (!quiet) {
1455 printf("Creating journal (%d blocks): ",
1456 journal_blocks);
1457 fflush(stdout);
1458 }
1459 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1460 journal_flags);
1461 if (retval) {
1462 bb_error_msg_and_die("Could not create journal");
1463 }
1464 if (!quiet)
1465 printf("done\n");
1466 }
1467no_journal:
1468
1469 if (!quiet)
1470 printf("Writing superblocks and "
1471 "filesystem accounting information: ");
1472 retval = ext2fs_flush(fs);
1473 if (retval) {
1474 bb_error_msg("\nWarning, had trouble writing out superblocks");
1475 }
1476 if (!quiet) {
1477 printf("done\n\n");
1478 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1479 print_check_message(fs);
1480 }
1481 val = ext2fs_close(fs);
1482 return (retval || val) ? 1 : 0;
1483}