]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/mke2fs.c
Basic flexible block group support
[thirdparty/e2fsprogs.git] / misc / mke2fs.c
CommitLineData
3839e657
TT
1/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
55f4cbd9
TT
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
19c78dc0
TT
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
3839e657
TT
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
a418d3ad 19#include <stdio.h>
3839e657
TT
20#include <string.h>
21#include <fcntl.h>
22#include <ctype.h>
3839e657 23#include <time.h>
756df353 24#ifdef __linux__
2740156b
TT
25#include <sys/utsname.h>
26#endif
a418d3ad 27#ifdef HAVE_GETOPT_H
3839e657 28#include <getopt.h>
373b8337
TT
29#else
30extern char *optarg;
31extern int optind;
a418d3ad
TT
32#endif
33#ifdef HAVE_UNISTD_H
3839e657 34#include <unistd.h>
a418d3ad
TT
35#endif
36#ifdef HAVE_STDLIB_H
3839e657 37#include <stdlib.h>
a418d3ad
TT
38#endif
39#ifdef HAVE_ERRNO_H
40#include <errno.h>
41#endif
42#ifdef HAVE_MNTENT_H
3839e657 43#include <mntent.h>
a418d3ad 44#endif
3839e657 45#include <sys/ioctl.h>
f3db3566
TT
46#include <sys/types.h>
47
54c637d4 48#include "ext2fs/ext2_fs.h"
3839e657 49#include "et/com_err.h"
1e3472c5 50#include "uuid/uuid.h"
896938d5 51#include "e2p/e2p.h"
3839e657 52#include "ext2fs/ext2fs.h"
63985320 53#include "util.h"
9dc6ad1e 54#include "profile.h"
a6d8302b 55#include "prof_err.h"
3839e657 56#include "../version.h"
d9c56d3c 57#include "nls-enable.h"
3839e657
TT
58
59#define STRIDE_LENGTH 8
60
6733c2fd 61#ifndef __sparc__
1e3472c5
TT
62#define ZAP_BOOTBLOCK
63#endif
64
3839e657 65extern int isatty(int);
f3db3566 66extern FILE *fpopen(const char *cmd, const char *mode);
3839e657
TT
67
68const char * program_name = "mke2fs";
d48755e9 69const char * device_name /* = NULL */;
3839e657
TT
70
71/* Command line options */
16ed5b3a
TT
72int cflag;
73int verbose;
74int quiet;
75int super_only;
76int force;
77int noaction;
78int journal_size;
79int journal_flags;
a4396e9d 80int lazy_itable_init;
16ed5b3a
TT
81char *bad_blocks_filename;
82__u32 fs_stride;
3839e657 83
9b9a780f 84struct ext2_super_block fs_param;
16ed5b3a
TT
85char *creator_os;
86char *volume_label;
87char *mount_dir;
88char *journal_device;
89int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
3839e657 90
9dc6ad1e
TT
91profile_t profile;
92
31e29a12 93int sys_page_size = 4096;
d99225ec 94int linux_version_code = 0;
31e29a12 95
63985320 96static void usage(void)
3839e657 97{
bdd80f28 98 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
067911ae 99 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
bdd80f28 100 "[-J journal-options]\n"
067911ae
AD
101 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
102 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
103 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
bdd80f28
TT
104 "[-r fs-revision] [-E extended-option[,...]]\n"
105 "\t[-T fs-type] [-jnqvFSV] device [blocks-count]\n"),
3839e657
TT
106 program_name);
107 exit(1);
108}
109
6733c2fd 110static int int_log2(int arg)
3839e657
TT
111{
112 int l = 0;
113
114 arg >>= 1;
115 while (arg) {
116 l++;
117 arg >>= 1;
118 }
119 return l;
120}
121
6733c2fd 122static int int_log10(unsigned int arg)
1dde43f0
TT
123{
124 int l;
125
126 for (l=0; arg ; l++)
127 arg = arg / 10;
128 return l;
129}
130
d99225ec
TT
131static int parse_version_number(const char *s)
132{
133 int major, minor, rev;
134 char *endptr;
135 const char *cp = s;
136
137 if (!s)
138 return 0;
139 major = strtol(cp, &endptr, 10);
140 if (cp == endptr || *endptr != '.')
141 return 0;
142 cp = endptr + 1;
143 minor = strtol(cp, &endptr, 10);
144 if (cp == endptr || *endptr != '.')
145 return 0;
146 cp = endptr + 1;
147 rev = strtol(cp, &endptr, 10);
148 if (cp == endptr)
149 return 0;
150 return ((((major * 256) + minor) * 256) + rev);
151}
152
3839e657
TT
153/*
154 * Helper function for read_bb_file and test_disk
155 */
54434927 156static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
3839e657 157{
6693837e 158 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
3839e657
TT
159 return;
160}
161
162/*
163 * Reads the bad blocks list from a file
164 */
165static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
166 const char *bad_blocks_file)
167{
168 FILE *f;
169 errcode_t retval;
170
171 f = fopen(bad_blocks_file, "r");
172 if (!f) {
173 com_err("read_bad_blocks_file", errno,
d9c56d3c 174 _("while trying to open %s"), bad_blocks_file);
3839e657
TT
175 exit(1);
176 }
177 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
178 fclose (f);
179 if (retval) {
180 com_err("ext2fs_read_bb_FILE", retval,
d9c56d3c 181 _("while reading in list of bad blocks from file"));
3839e657
TT
182 exit(1);
183 }
184}
185
186/*
187 * Runs the badblocks program to test the disk
188 */
189static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
190{
191 FILE *f;
192 errcode_t retval;
193 char buf[1024];
194
d0ff90d5 195 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
3ed57c27 196 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
8ade4792 197 fs->device_name, fs->super->s_blocks_count-1);
3839e657 198 if (verbose)
d9c56d3c 199 printf(_("Running command: %s\n"), buf);
3839e657
TT
200 f = popen(buf, "r");
201 if (!f) {
202 com_err("popen", errno,
f37ab68a 203 _("while trying to run '%s'"), buf);
3839e657
TT
204 exit(1);
205 }
206 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
f3db3566 207 pclose(f);
3839e657
TT
208 if (retval) {
209 com_err("ext2fs_read_bb_FILE", retval,
d9c56d3c 210 _("while processing list of bad blocks from program"));
3839e657
TT
211 exit(1);
212 }
213}
214
215static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
216{
54434927
TT
217 dgrp_t i;
218 blk_t j;
219 unsigned must_be_good;
3839e657
TT
220 blk_t blk;
221 badblocks_iterate bb_iter;
222 errcode_t retval;
f3db3566
TT
223 blk_t group_block;
224 int group;
225 int group_bad;
3839e657
TT
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++) {
cbbf031b 236 if (ext2fs_badblocks_list_test(bb_list, i)) {
d9c56d3c
TT
237 fprintf(stderr, _("Block %d in primary "
238 "superblock/group descriptor area bad.\n"), i);
d0ff90d5 239 fprintf(stderr, _("Blocks %u through %u must be good "
d9c56d3c 240 "in order to build a filesystem.\n"),
3839e657 241 fs->super->s_first_data_block, must_be_good);
54434927 242 fputs(_("Aborting....\n"), stderr);
3839e657
TT
243 exit(1);
244 }
245 }
f3db3566
TT
246
247 /*
248 * See if any of the bad blocks are showing up in the backup
249 * superblocks and/or group descriptors. If so, issue a
250 * warning and adjust the block counts appropriately.
251 */
252 group_block = fs->super->s_first_data_block +
253 fs->super->s_blocks_per_group;
f3db3566
TT
254
255 for (i = 1; i < fs->group_desc_count; i++) {
92bcc595 256 group_bad = 0;
f3db3566 257 for (j=0; j < fs->desc_blocks+1; j++) {
cbbf031b
TT
258 if (ext2fs_badblocks_list_test(bb_list,
259 group_block + j)) {
f3db3566
TT
260 if (!group_bad)
261 fprintf(stderr,
8deb80a5 262_("Warning: the backup superblock/group descriptors at block %u contain\n"
d9c56d3c 263" bad blocks.\n\n"),
f3db3566
TT
264 group_block);
265 group_bad++;
266 group = ext2fs_group_of_blk(fs, group_block+j);
267 fs->group_desc[group].bg_free_blocks_count++;
5711ed29 268 ext2fs_group_desc_csum_set(fs, group);
f3db3566
TT
269 fs->super->s_free_blocks_count++;
270 }
271 }
272 group_block += fs->super->s_blocks_per_group;
273 }
3839e657
TT
274
275 /*
276 * Mark all the bad blocks as used...
277 */
cbbf031b 278 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
3839e657 279 if (retval) {
cbbf031b 280 com_err("ext2fs_badblocks_list_iterate_begin", retval,
d9c56d3c 281 _("while marking bad blocks as used"));
3839e657
TT
282 exit(1);
283 }
cbbf031b 284 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
f3db3566 285 ext2fs_mark_block_bitmap(fs->block_map, blk);
cbbf031b 286 ext2fs_badblocks_list_iterate_end(bb_iter);
3839e657
TT
287}
288
16ed5b3a
TT
289/*
290 * These functions implement a generalized progress meter.
291 */
292struct progress_struct {
293 char format[20];
294 char backup[80];
295 __u32 max;
1cca86f5 296 int skip_progress;
16ed5b3a 297};
7d5633cf 298
16ed5b3a 299static void progress_init(struct progress_struct *progress,
4ea7bd04 300 const char *label,__u32 max)
16ed5b3a
TT
301{
302 int i;
3839e657 303
16ed5b3a
TT
304 memset(progress, 0, sizeof(struct progress_struct));
305 if (quiet)
306 return;
1dde43f0
TT
307
308 /*
309 * Figure out how many digits we need
310 */
16ed5b3a
TT
311 i = int_log10(max);
312 sprintf(progress->format, "%%%dd/%%%dld", i, i);
313 memset(progress->backup, '\b', sizeof(progress->backup)-1);
314 progress->backup[sizeof(progress->backup)-1] = 0;
54434927 315 if ((2*i)+1 < (int) sizeof(progress->backup))
16ed5b3a
TT
316 progress->backup[(2*i)+1] = 0;
317 progress->max = max;
318
1cca86f5
TT
319 progress->skip_progress = 0;
320 if (getenv("MKE2FS_SKIP_PROGRESS"))
321 progress->skip_progress++;
322
16ed5b3a
TT
323 fputs(label, stdout);
324 fflush(stdout);
325}
326
327static void progress_update(struct progress_struct *progress, __u32 val)
328{
1cca86f5 329 if ((progress->format[0] == 0) || progress->skip_progress)
16ed5b3a
TT
330 return;
331 printf(progress->format, val, progress->max);
332 fputs(progress->backup, stdout);
333}
334
335static void progress_close(struct progress_struct *progress)
336{
337 if (progress->format[0] == 0)
338 return;
339 fputs(_("done \n"), stdout);
340}
341
342
343/*
344 * Helper function which zeros out _num_ blocks starting at _blk_. In
345 * case of an error, the details of the error is returned via _ret_blk_
346 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
347 * success, and an error code on an error.
348 *
349 * As a special case, if the first argument is NULL, then it will
350 * attempt to free the static zeroizing buffer. (This is to keep
351 * programs that check for memory leaks happy.)
352 */
353static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
354 struct progress_struct *progress,
355 blk_t *ret_blk, int *ret_count)
356{
357 int j, count, next_update, next_update_incr;
358 static char *buf;
359 errcode_t retval;
360
361 /* If fs is null, clean up the static buffer and return */
362 if (!fs) {
363 if (buf) {
364 free(buf);
365 buf = 0;
366 }
367 return 0;
368 }
369 /* Allocate the zeroizing buffer if necessary */
370 if (!buf) {
371 buf = malloc(fs->blocksize * STRIDE_LENGTH);
372 if (!buf) {
373 com_err("malloc", ENOMEM,
374 _("while allocating zeroizing buffer"));
375 exit(1);
376 }
377 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
378 }
379 /* OK, do the write loop */
380 next_update = 0;
381 next_update_incr = num / 100;
382 if (next_update_incr < 1)
383 next_update_incr = 1;
384 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
f044b4d8
TT
385 count = num - j;
386 if (count > STRIDE_LENGTH)
16ed5b3a 387 count = STRIDE_LENGTH;
16ed5b3a
TT
388 retval = io_channel_write_blk(fs->io, blk, count, buf);
389 if (retval) {
390 if (ret_count)
391 *ret_count = count;
392 if (ret_blk)
393 *ret_blk = blk;
394 return retval;
395 }
396 if (progress && j > next_update) {
397 next_update += num / 100;
398 progress_update(progress, blk);
399 }
400 }
401 return 0;
402}
403
a4396e9d 404static void write_inode_tables(ext2_filsys fs, int lazy_flag)
16ed5b3a
TT
405{
406 errcode_t retval;
407 blk_t blk;
54434927 408 dgrp_t i;
0f2794c0 409 int num, ipb;
16ed5b3a
TT
410 struct progress_struct progress;
411
412 if (quiet)
413 memset(&progress, 0, sizeof(progress));
414 else
415 progress_init(&progress, _("Writing inode tables: "),
416 fs->group_desc_count);
1dde43f0 417
3839e657 418 for (i = 0; i < fs->group_desc_count; i++) {
16ed5b3a 419 progress_update(&progress, i);
3839e657 420
19c78dc0
TT
421 blk = fs->group_desc[i].bg_inode_table;
422 num = fs->inode_blocks_per_group;
16ed5b3a 423
0f2794c0
TT
424 if (lazy_flag) {
425 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
426 printf("bg %i, num %d, ipb %d, unused %d ",
427 i, num, ipb, fs->group_desc[i].bg_itable_unused);
428 num = ((((fs->super->s_inodes_per_group -
429 fs->group_desc[i].bg_itable_unused) *
430 EXT2_INODE_SIZE(fs->super)) +
431 EXT2_BLOCK_SIZE(fs->super) - 1) /
432 EXT2_BLOCK_SIZE(fs->super));
433 printf("new num %d\n", num);
434 } else {
d2d22a29
JS
435 /* The kernel doesn't need to zero the itable blocks */
436 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
5711ed29 437 ext2fs_group_desc_csum_set(fs, i);
19c78dc0 438 }
0f2794c0
TT
439 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
440 if (retval) {
441 fprintf(stderr, _("\nCould not write %d "
442 "blocks in inode table starting at %u: %s\n"),
443 num, blk, error_message(retval));
444 exit(1);
445 }
7d5633cf
TT
446 if (sync_kludge) {
447 if (sync_kludge == 1)
448 sync();
449 else if ((i % sync_kludge) == 0)
450 sync();
451 }
3839e657 452 }
16ed5b3a
TT
453 zero_blocks(0, 0, 0, 0, 0, 0);
454 progress_close(&progress);
3839e657
TT
455}
456
457static void create_root_dir(ext2_filsys fs)
458{
5113a6e3 459 errcode_t retval;
f3db3566 460 struct ext2_inode inode;
5113a6e3 461 __u32 uid, gid;
3839e657
TT
462
463 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
464 if (retval) {
d9c56d3c 465 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
3839e657
TT
466 exit(1);
467 }
f3db3566
TT
468 if (geteuid()) {
469 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
470 if (retval) {
471 com_err("ext2fs_read_inode", retval,
d9c56d3c 472 _("while reading root inode"));
f3db3566
TT
473 exit(1);
474 }
5113a6e3
ES
475 uid = getuid();
476 inode.i_uid = uid;
15343922 477 ext2fs_set_i_uid_high(inode, uid >> 16);
5113a6e3
ES
478 if (uid) {
479 gid = getgid();
480 inode.i_gid = gid;
15343922 481 ext2fs_set_i_gid_high(inode, gid >> 16);
5113a6e3 482 }
e27b4563 483 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
f3db3566
TT
484 if (retval) {
485 com_err("ext2fs_write_inode", retval,
d9c56d3c 486 _("while setting root inode ownership"));
f3db3566
TT
487 exit(1);
488 }
489 }
3839e657
TT
490}
491
492static void create_lost_and_found(ext2_filsys fs)
493{
2d328bb7 494 unsigned int lpf_size = 0;
3839e657 495 errcode_t retval;
dfcdc32f 496 ext2_ino_t ino;
3839e657
TT
497 const char *name = "lost+found";
498 int i;
499
6a525069 500 fs->umask = 077;
3839e657
TT
501 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
502 if (retval) {
d9c56d3c
TT
503 com_err("ext2fs_mkdir", retval,
504 _("while creating /lost+found"));
3839e657
TT
505 exit(1);
506 }
507
508 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
509 if (retval) {
d9c56d3c
TT
510 com_err("ext2_lookup", retval,
511 _("while looking up /lost+found"));
3839e657
TT
512 exit(1);
513 }
514
515 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
8c7c6eb1
AD
516 /* Ensure that lost+found is at least 2 blocks, so we always
517 * test large empty blocks for big-block filesystems. */
518 if ((lpf_size += fs->blocksize) >= 16*1024 &&
519 lpf_size >= 2 * fs->blocksize)
50787ea2 520 break;
3839e657
TT
521 retval = ext2fs_expand_dir(fs, ino);
522 if (retval) {
523 com_err("ext2fs_expand_dir", retval,
d9c56d3c 524 _("while expanding /lost+found"));
3839e657
TT
525 exit(1);
526 }
6a525069 527 }
3839e657
TT
528}
529
530static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
531{
532 errcode_t retval;
533
f3db3566 534 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
5711ed29 535 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
3839e657
TT
536 retval = ext2fs_update_bb_inode(fs, bb_list);
537 if (retval) {
538 com_err("ext2fs_update_bb_inode", retval,
d9c56d3c 539 _("while setting bad block inode"));
3839e657
TT
540 exit(1);
541 }
542
543}
544
545static void reserve_inodes(ext2_filsys fs)
546{
dfcdc32f
TT
547 ext2_ino_t i;
548 int group;
3839e657 549
5711ed29
TT
550 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
551 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
3839e657
TT
552 ext2fs_mark_ib_dirty(fs);
553}
554
756df353 555#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
7ef3bb83 556#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
756df353 557#define BSD_LABEL_OFFSET 64
756df353 558
04a96857 559static void zap_sector(ext2_filsys fs, int sect, int nsect)
f3db3566 560{
3f85f65c 561 char *buf;
f3db3566 562 int retval;
756df353 563 unsigned int *magic;
f3db3566 564
3f85f65c 565 buf = malloc(512*nsect);
568101f7 566 if (!buf) {
4ea7bd04
TT
567 printf(_("Out of memory erasing sectors %d-%d\n"),
568 sect, sect + nsect - 1);
568101f7
AD
569 exit(1);
570 }
756df353 571
7ef3bb83
TT
572 if (sect == 0) {
573 /* Check for a BSD disklabel, and don't erase it if so */
574 retval = io_channel_read_blk(fs->io, 0, -512, buf);
575 if (retval)
576 fprintf(stderr,
577 _("Warning: could not read block 0: %s\n"),
578 error_message(retval));
579 else {
580 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
581 if ((*magic == BSD_DISKMAGIC) ||
582 (*magic == BSD_MAGICDISK))
583 return;
584 }
756df353 585 }
756df353 586
7098810d 587 memset(buf, 0, 512*nsect);
e41784eb 588 io_channel_set_blksize(fs->io, 512);
04a96857 589 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
e41784eb 590 io_channel_set_blksize(fs->io, fs->blocksize);
3f85f65c 591 free(buf);
f3db3566 592 if (retval)
6693837e
TT
593 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
594 sect, error_message(retval));
f3db3566 595}
16ed5b3a
TT
596
597static void create_journal_dev(ext2_filsys fs)
598{
599 struct progress_struct progress;
600 errcode_t retval;
601 char *buf;
dc2ec525
TT
602 blk_t blk;
603 int count;
16ed5b3a 604
d6a27e00
TT
605 retval = ext2fs_create_journal_superblock(fs,
606 fs->super->s_blocks_count, 0, &buf);
607 if (retval) {
608 com_err("create_journal_dev", retval,
609 _("while initializing journal superblock"));
610 exit(1);
611 }
16ed5b3a
TT
612 if (quiet)
613 memset(&progress, 0, sizeof(progress));
614 else
615 progress_init(&progress, _("Zeroing journal device: "),
616 fs->super->s_blocks_count);
617
16ed5b3a
TT
618 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
619 &progress, &blk, &count);
620 if (retval) {
621 com_err("create_journal_dev", retval,
14bbcbc3 622 _("while zeroing journal device (block %u, count %d)"),
16ed5b3a
TT
623 blk, count);
624 exit(1);
625 }
dc2ec525
TT
626 zero_blocks(0, 0, 0, 0, 0, 0);
627
dc2ec525
TT
628 retval = io_channel_write_blk(fs->io,
629 fs->super->s_first_data_block+1,
630 1, buf);
16ed5b3a
TT
631 if (retval) {
632 com_err("create_journal_dev", retval,
633 _("while writing journal superblock"));
634 exit(1);
635 }
636 progress_close(&progress);
637}
f3db3566 638
3839e657
TT
639static void show_stats(ext2_filsys fs)
640{
ef9abe5f 641 struct ext2_super_block *s = fs->super;
1e3472c5 642 char buf[80];
63253946 643 char *os;
3839e657 644 blk_t group_block;
54434927
TT
645 dgrp_t i;
646 int need, col_left;
3839e657 647
9b9a780f 648 if (fs_param.s_blocks_count != s->s_blocks_count)
8deb80a5 649 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
9b9a780f 650 fs_param.s_blocks_count - s->s_blocks_count);
50787ea2
TT
651
652 memset(buf, 0, sizeof(buf));
653 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
d9c56d3c 654 printf(_("Filesystem label=%s\n"), buf);
54434927 655 fputs(_("OS type: "), stdout);
63253946
TT
656 os = e2p_os2string(fs->super->s_creator_os);
657 fputs(os, stdout);
658 free(os);
50787ea2 659 printf("\n");
d9c56d3c 660 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
50787ea2 661 s->s_log_block_size);
d9c56d3c 662 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
50787ea2 663 s->s_log_frag_size);
d9c56d3c 664 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
3839e657 665 s->s_blocks_count);
d9c56d3c 666 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
3839e657
TT
667 s->s_r_blocks_count,
668 100.0 * s->s_r_blocks_count / s->s_blocks_count);
d9c56d3c 669 printf(_("First data block=%u\n"), s->s_first_data_block);
d323f8fb
TT
670 if (s->s_reserved_gdt_blocks)
671 printf(_("Maximum filesystem blocks=%lu\n"),
672 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
f2de1d38 673 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
d9c56d3c 674 if (fs->group_desc_count > 1)
c8c071a0 675 printf(_("%u block groups\n"), fs->group_desc_count);
d9c56d3c 676 else
c8c071a0 677 printf(_("%u block group\n"), fs->group_desc_count);
d9c56d3c 678 printf(_("%u blocks per group, %u fragments per group\n"),
3839e657 679 s->s_blocks_per_group, s->s_frags_per_group);
d9c56d3c 680 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
3839e657
TT
681
682 if (fs->group_desc_count == 1) {
683 printf("\n");
684 return;
685 }
d323f8fb 686
d9c56d3c 687 printf(_("Superblock backups stored on blocks: "));
3839e657
TT
688 group_block = s->s_first_data_block;
689 col_left = 0;
690 for (i = 1; i < fs->group_desc_count; i++) {
691 group_block += s->s_blocks_per_group;
521e3685
TT
692 if (!ext2fs_bg_has_super(fs, i))
693 continue;
7671433a
TT
694 if (i != 1)
695 printf(", ");
6733c2fd 696 need = int_log10(group_block) + 2;
1dde43f0 697 if (need > col_left) {
3839e657 698 printf("\n\t");
1dde43f0 699 col_left = 72;
3839e657 700 }
1dde43f0 701 col_left -= need;
a418d3ad 702 printf("%u", group_block);
3839e657
TT
703 }
704 printf("\n\n");
705}
706
1e3472c5
TT
707/*
708 * Set the S_CREATOR_OS field. Return true if OS is known,
709 * otherwise, 0.
710 */
711static int set_os(struct ext2_super_block *sb, char *os)
712{
713 if (isdigit (*os))
714 sb->s_creator_os = atoi (os);
715 else if (strcasecmp(os, "linux") == 0)
716 sb->s_creator_os = EXT2_OS_LINUX;
717 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
718 sb->s_creator_os = EXT2_OS_HURD;
ea1e8f47
TT
719 else if (strcasecmp(os, "freebsd") == 0)
720 sb->s_creator_os = EXT2_OS_FREEBSD;
721 else if (strcasecmp(os, "lites") == 0)
722 sb->s_creator_os = EXT2_OS_LITES;
1e3472c5
TT
723 else
724 return 0;
725 return 1;
726}
727
a418d3ad
TT
728#define PATH_SET "PATH=/sbin"
729
c6a44136
TT
730static void parse_extended_opts(struct ext2_super_block *param,
731 const char *opts)
a29f4d30 732{
2d328bb7 733 char *buf, *token, *next, *p, *arg, *badopt = 0;
a29f4d30 734 int len;
d323f8fb 735 int r_usage = 0;
a29f4d30
TT
736
737 len = strlen(opts);
738 buf = malloc(len+1);
739 if (!buf) {
d323f8fb
TT
740 fprintf(stderr,
741 _("Couldn't allocate memory to parse options!\n"));
a29f4d30
TT
742 exit(1);
743 }
744 strcpy(buf, opts);
745 for (token = buf; token && *token; token = next) {
746 p = strchr(token, ',');
747 next = 0;
748 if (p) {
749 *p = 0;
750 next = p+1;
d323f8fb 751 }
a29f4d30
TT
752 arg = strchr(token, '=');
753 if (arg) {
754 *arg = 0;
755 arg++;
756 }
757 if (strcmp(token, "stride") == 0) {
758 if (!arg) {
d323f8fb 759 r_usage++;
0c17cb25 760 badopt = token;
a29f4d30
TT
761 continue;
762 }
0c17cb25
TT
763 param->s_raid_stride = strtoul(arg, &p, 0);
764 if (*p || (param->s_raid_stride == 0)) {
d9c56d3c 765 fprintf(stderr,
f37ab68a
TT
766 _("Invalid stride parameter: %s\n"),
767 arg);
d323f8fb
TT
768 r_usage++;
769 continue;
770 }
0c17cb25
TT
771 } else if (strcmp(token, "stripe-width") == 0 ||
772 strcmp(token, "stripe_width") == 0) {
773 if (!arg) {
774 r_usage++;
775 badopt = token;
776 continue;
777 }
778 param->s_raid_stripe_width = strtoul(arg, &p, 0);
779 if (*p || (param->s_raid_stripe_width == 0)) {
780 fprintf(stderr,
781 _("Invalid stripe-width parameter: %s\n"),
782 arg);
783 r_usage++;
784 continue;
785 }
d323f8fb 786 } else if (!strcmp(token, "resize")) {
c6a44136
TT
787 unsigned long resize, bpg, rsv_groups;
788 unsigned long group_desc_count, desc_blocks;
789 unsigned int gdpb, blocksize;
790 int rsv_gdb;
d323f8fb
TT
791
792 if (!arg) {
793 r_usage++;
0c17cb25 794 badopt = token;
a29f4d30
TT
795 continue;
796 }
d323f8fb 797
55f4cbd9
TT
798 resize = parse_num_blocks(arg,
799 param->s_log_block_size);
d323f8fb
TT
800
801 if (resize == 0) {
55f4cbd9
TT
802 fprintf(stderr,
803 _("Invalid resize parameter: %s\n"),
804 arg);
d323f8fb
TT
805 r_usage++;
806 continue;
807 }
c6a44136
TT
808 if (resize <= param->s_blocks_count) {
809 fprintf(stderr,
f37ab68a
TT
810 _("The resize maximum must be greater "
811 "than the filesystem size.\n"));
c6a44136
TT
812 r_usage++;
813 continue;
814 }
d323f8fb 815
c6a44136
TT
816 blocksize = EXT2_BLOCK_SIZE(param);
817 bpg = param->s_blocks_per_group;
818 if (!bpg)
819 bpg = blocksize * 8;
f2de1d38 820 gdpb = EXT2_DESC_PER_BLOCK(param);
69022e02
TT
821 group_desc_count =
822 ext2fs_div_ceil(param->s_blocks_count, bpg);
c6a44136
TT
823 desc_blocks = (group_desc_count +
824 gdpb - 1) / gdpb;
69022e02
TT
825 rsv_groups = ext2fs_div_ceil(resize, bpg);
826 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
c6a44136 827 desc_blocks;
9b9a780f 828 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
c6a44136
TT
829 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
830
831 if (rsv_gdb > 0) {
b290d2dc
TT
832 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
833 fprintf(stderr,
834 _("On-line resizing not supported with revision 0 filesystems\n"));
21400381 835 free(buf);
b290d2dc
TT
836 exit(1);
837 }
c6a44136
TT
838 param->s_feature_compat |=
839 EXT2_FEATURE_COMPAT_RESIZE_INODE;
840
841 param->s_reserved_gdt_blocks = rsv_gdb;
842 }
6cb27404
TT
843 } else if (!strcmp(token, "test_fs")) {
844 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
a4396e9d
TT
845 } else if (!strcmp(token, "lazy_itable_init")) {
846 if (!arg) {
847 r_usage++;
848 badopt = token;
849 continue;
850 }
851 lazy_itable_init = strtoul(arg, &p, 0);
0c17cb25 852 } else {
d323f8fb 853 r_usage++;
0c17cb25
TT
854 badopt = token;
855 }
a29f4d30 856 }
d323f8fb 857 if (r_usage) {
0c17cb25 858 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
bb145b01 859 "Extended options are separated by commas, "
a29f4d30
TT
860 "and may take an argument which\n"
861 "\tis set off by an equals ('=') sign.\n\n"
bb145b01 862 "Valid extended options are:\n"
0c17cb25
TT
863 "\tstride=<RAID per-disk data chunk in blocks>\n"
864 "\tstripe-width=<RAID stride * data disks in blocks>\n"
a4396e9d
TT
865 "\tresize=<resize maximum size in blocks>\n"
866 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
867 "\ttest_fs\n\n"),
2d328bb7 868 badopt ? badopt : "");
21400381 869 free(buf);
a29f4d30
TT
870 exit(1);
871 }
0c17cb25
TT
872 if (param->s_raid_stride &&
873 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
874 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
875 "multiple of stride %u.\n\n"),
876 param->s_raid_stripe_width, param->s_raid_stride);
877
21400381 878 free(buf);
a29f4d30
TT
879}
880
896938d5 881static __u32 ok_features[3] = {
558df544 882 /* Compat */
843049c4 883 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
d323f8fb 884 EXT2_FEATURE_COMPAT_RESIZE_INODE |
f5fa2007 885 EXT2_FEATURE_COMPAT_DIR_INDEX |
558df544
TT
886 EXT2_FEATURE_COMPAT_EXT_ATTR,
887 /* Incompat */
888 EXT2_FEATURE_INCOMPAT_FILETYPE|
bf6b848e 889 EXT3_FEATURE_INCOMPAT_EXTENTS|
c046ac7f 890 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
c2d4300b
JS
891 EXT2_FEATURE_INCOMPAT_META_BG|
892 EXT4_FEATURE_INCOMPAT_FLEX_BG,
558df544
TT
893 /* R/O compat */
894 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
d2d22a29
JS
895 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
896 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
896938d5 897};
a29f4d30
TT
898
899
9dc6ad1e
TT
900static void syntax_err_report(const char *filename, long err, int line_num)
901{
902 fprintf(stderr,
903 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
904 filename, line_num, error_message(err));
905 exit(1);
906}
907
abcfdfda 908static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
9dc6ad1e
TT
909
910static void edit_feature(const char *str, __u32 *compat_array)
911{
912 if (!str)
913 return;
914
915 if (e2p_edit_feature(str, compat_array, ok_features)) {
916 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
917 str);
918 exit(1);
919 }
920}
921
3d43836f
TT
922struct str_list {
923 char **list;
924 int num;
925 int max;
926};
927
928static errcode_t init_list(struct str_list *sl)
929{
930 sl->num = 0;
931 sl->max = 0;
932 sl->list = malloc((sl->max+1) * sizeof(char *));
933 if (!sl->list)
934 return ENOMEM;
935 sl->list[0] = 0;
936 return 0;
937}
938
939static errcode_t push_string(struct str_list *sl, const char *str)
940{
941 char **new_list;
942
943 if (sl->num >= sl->max) {
944 sl->max += 2;
945 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
946 if (!new_list)
947 return ENOMEM;
948 sl->list = new_list;
949 }
950 sl->list[sl->num] = malloc(strlen(str)+1);
951 if (sl->list[sl->num] == 0)
952 return ENOMEM;
953 strcpy(sl->list[sl->num], str);
954 sl->num++;
955 sl->list[sl->num] = 0;
956 return 0;
957}
958
959static void print_str_list(char **list)
960{
961 char **cpp;
962
963 for (cpp = list; *cpp; cpp++) {
964 printf("'%s'", *cpp);
965 if (cpp[1])
966 fputs(", ", stdout);
967 }
968 fputc('\n', stdout);
969}
970
971static char **parse_fs_type(const char *fs_type,
972 const char *usage_types,
973 struct ext2_super_block *fs_param,
974 char *progname)
975{
976 const char *ext_type = 0;
977 char *parse_str;
978 char *profile_type = 0;
979 char *cp, *t;
980 const char *size_type;
981 struct str_list list;
982 int state = 0;
983 unsigned long meg;
984
985 if (init_list(&list))
986 return 0;
987
988 if (fs_type)
989 ext_type = fs_type;
990 else if (progname) {
991 ext_type = strrchr(progname, '/');
992 if (ext_type)
993 ext_type++;
994 else
995 ext_type = progname;
996
997 if (!strncmp(ext_type, "mkfs.", 5)) {
998 ext_type += 5;
999 if (ext_type[0] == 0)
1000 ext_type = 0;
1001 } else
1002 ext_type = 0;
1003 }
1004
1005 if (!ext_type) {
1006 profile_get_string(profile, "defaults", "fs_type", 0,
1007 "ext2", &profile_type);
1008 ext_type = profile_type;
1009 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1010 ext_type = "ext3";
1011 }
1012
1013 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1014 if (fs_param->s_blocks_count < 3 * meg)
1015 size_type = "floppy";
1016 else if (fs_param->s_blocks_count < 512 * meg)
1017 size_type = "small";
1018 else
1019 size_type = "default";
1020
1021 if (!usage_types)
1022 usage_types = size_type;
1023
1024 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1025 if (!parse_str) {
1026 free(list.list);
1027 return 0;
1028 }
1029 if (usage_types)
1030 strcpy(parse_str, usage_types);
1031 else
1032 *parse_str = '\0';
1033
1034 if (ext_type)
1035 push_string(&list, ext_type);
1036 cp = parse_str;
1037 while (1) {
1038 t = strchr(cp, ',');
1039 if (t)
1040 *t = '\0';
1041
1042 if (*cp)
1043 push_string(&list, cp);
1044 if (t)
1045 cp = t+1;
1046 else {
1047 cp = "";
1048 break;
1049 }
1050 }
1051 free(parse_str);
1052 if (profile_type)
1053 free(profile_type);
1054 return (list.list);
1055}
1056
1057static char *get_string_from_profile(char **fs_types, const char *opt,
1058 const char *def_val)
1059{
1060 char *ret = 0;
1061 char **cpp;
1062 int i;
1063
1064 for (i=0; fs_types[i]; i++);
1065 for (i-=1; i >=0 ; i--) {
1066 profile_get_string(profile, "fs_types", fs_types[i],
1067 opt, 0, &ret);
1068 if (ret)
1069 return ret;
1070 }
1071 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1072 return (ret);
1073}
1074
1075static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1076{
1077 int ret;
1078 char **cpp;
1079
1080 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1081 for (cpp = fs_types; *cpp; cpp++)
1082 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1083 return ret;
1084}
1085
a4396e9d
TT
1086static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1087{
1088 int ret;
1089 char **cpp;
1090
1091 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1092 for (cpp = fs_types; *cpp; cpp++)
1093 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1094 return ret;
1095}
3d43836f 1096
d48bc604
TT
1097extern const char *mke2fs_default_profile;
1098static const char *default_files[] = { "<default>", 0 };
1099
3839e657
TT
1100static void PRS(int argc, char *argv[])
1101{
c5290fae 1102 int b, c;
4a600566 1103 int size;
9dc6ad1e 1104 char *tmp, *tmp2;
4a600566
TT
1105 int blocksize = 0;
1106 int inode_ratio = 0;
932a489c 1107 int inode_size = 0;
ce911145 1108 double reserved_ratio = 5.0;
93d5c387 1109 int sector_size = 0;
1e6e4c5e 1110 int show_version_only = 0;
de8f3a76 1111 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
a418d3ad 1112 errcode_t retval;
4a600566 1113 char * oldpath = getenv("PATH");
c6a44136 1114 char * extended_opts = 0;
4ea7bd04 1115 const char * fs_type = 0;
3d43836f
TT
1116 const char * usage_types = 0;
1117 char **fs_types;
4a600566 1118 blk_t dev_size;
756df353 1119#ifdef __linux__
4a600566 1120 struct utsname ut;
2740156b 1121#endif
31e29a12 1122 long sysval;
9dc6ad1e
TT
1123 int s_opt = -1, r_opt = -1;
1124 char *fs_features = 0;
1125 int use_bsize;
642935c0
TT
1126 char *newpath;
1127 int pathlen = sizeof(PATH_SET) + 1;
1128
1129 if (oldpath)
1130 pathlen += strlen(oldpath);
1131 newpath = malloc(pathlen);
1132 strcpy(newpath, PATH_SET);
14bbcbc3 1133
3839e657 1134 /* Update our PATH to include /sbin */
a418d3ad 1135 if (oldpath) {
a418d3ad
TT
1136 strcat (newpath, ":");
1137 strcat (newpath, oldpath);
642935c0
TT
1138 }
1139 putenv (newpath);
3839e657 1140
16ed5b3a
TT
1141 tmp = getenv("MKE2FS_SYNC");
1142 if (tmp)
1143 sync_kludge = atoi(tmp);
31e29a12
TT
1144
1145 /* Determine the system page size if possible */
1146#ifdef HAVE_SYSCONF
1147#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1148#define _SC_PAGESIZE _SC_PAGE_SIZE
1149#endif
1150#ifdef _SC_PAGESIZE
1151 sysval = sysconf(_SC_PAGESIZE);
aab6fe73 1152 if (sysval > 0)
31e29a12
TT
1153 sys_page_size = sysval;
1154#endif /* _SC_PAGESIZE */
1155#endif /* HAVE_SYSCONF */
9dc6ad1e
TT
1156
1157 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1158 config_fn[0] = tmp;
1159 profile_set_syntax_err_cb(syntax_err_report);
d48bc604
TT
1160 retval = profile_init(config_fn, &profile);
1161 if (retval == ENOENT) {
1162 profile_init(default_files, &profile);
1163 profile_set_default(profile, mke2fs_default_profile);
1164 }
16ed5b3a 1165
3839e657
TT
1166 setbuf(stdout, NULL);
1167 setbuf(stderr, NULL);
a6d8302b
TT
1168 add_error_table(&et_ext2_error_table);
1169 add_error_table(&et_prof_error_table);
9b9a780f
TT
1170 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1171 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
843049c4 1172
756df353 1173#ifdef __linux__
14bbcbc3
TT
1174 if (uname(&ut)) {
1175 perror("uname");
1176 exit(1);
1177 }
d99225ec 1178 linux_version_code = parse_version_number(ut.release);
9dc6ad1e 1179 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
9b9a780f 1180 fs_param.s_rev_level = 0;
14bbcbc3 1181#endif
0072f8de
AD
1182
1183 if (argc && *argv) {
1184 program_name = get_progname(*argv);
1185
1186 /* If called as mkfs.ext3, create a journal inode */
1187 if (!strcmp(program_name, "mkfs.ext3"))
1188 journal_size = -1;
1189 }
1190
1e3472c5 1191 while ((c = getopt (argc, argv,
3d43836f 1192 "b:cf:g:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
3839e657
TT
1193 switch (c) {
1194 case 'b':
c5290fae
TT
1195 blocksize = strtol(optarg, &tmp, 0);
1196 b = (blocksize > 0) ? blocksize : -blocksize;
1197 if (b < EXT2_MIN_BLOCK_SIZE ||
1198 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
d9c56d3c 1199 com_err(program_name, 0,
f37ab68a 1200 _("invalid block size - %s"), optarg);
3839e657
TT
1201 exit(1);
1202 }
932a489c
AD
1203 if (blocksize > 4096)
1204 fprintf(stderr, _("Warning: blocksize %d not "
1205 "usable on most systems.\n"),
1206 blocksize);
c5290fae 1207 if (blocksize > 0)
9b9a780f 1208 fs_param.s_log_block_size =
c5290fae
TT
1209 int_log2(blocksize >>
1210 EXT2_MIN_BLOCK_LOG_SIZE);
3839e657 1211 break;
b10fd5e8 1212 case 'c': /* Check for bad blocks */
3ed57c27 1213 cflag++;
3839e657
TT
1214 break;
1215 case 'f':
1216 size = strtoul(optarg, &tmp, 0);
932a489c
AD
1217 if (size < EXT2_MIN_BLOCK_SIZE ||
1218 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
d9c56d3c 1219 com_err(program_name, 0,
bb145b01 1220 _("invalid fragment size - %s"),
3839e657
TT
1221 optarg);
1222 exit(1);
1223 }
9b9a780f 1224 fs_param.s_log_frag_size =
6733c2fd 1225 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
6693837e 1226 fprintf(stderr, _("Warning: fragments not supported. "
d9c56d3c 1227 "Ignoring -f option\n"));
3839e657
TT
1228 break;
1229 case 'g':
9b9a780f 1230 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
f3db3566
TT
1231 if (*tmp) {
1232 com_err(program_name, 0,
d9c56d3c 1233 _("Illegal number for blocks per group"));
f3db3566
TT
1234 exit(1);
1235 }
9b9a780f 1236 if ((fs_param.s_blocks_per_group % 8) != 0) {
f3db3566 1237 com_err(program_name, 0,
d9c56d3c 1238 _("blocks per group must be multiple of 8"));
3839e657
TT
1239 exit(1);
1240 }
1241 break;
1242 case 'i':
1243 inode_ratio = strtoul(optarg, &tmp, 0);
932a489c
AD
1244 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1245 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
3839e657 1246 *tmp) {
d9c56d3c 1247 com_err(program_name, 0,
bb145b01 1248 _("invalid inode ratio %s (min %d/max %d)"),
932a489c
AD
1249 optarg, EXT2_MIN_BLOCK_SIZE,
1250 EXT2_MAX_BLOCK_SIZE);
3839e657
TT
1251 exit(1);
1252 }
1253 break;
dc2ec525
TT
1254 case 'J':
1255 parse_journal_opts(optarg);
1256 break;
85ef4ae8 1257 case 'j':
dc2ec525
TT
1258 if (!journal_size)
1259 journal_size = -1;
8ddaa66b 1260 break;
3839e657 1261 case 'l':
f3db3566
TT
1262 bad_blocks_filename = malloc(strlen(optarg)+1);
1263 if (!bad_blocks_filename) {
1264 com_err(program_name, ENOMEM,
d9c56d3c 1265 _("in malloc for bad_blocks_filename"));
f3db3566
TT
1266 exit(1);
1267 }
1268 strcpy(bad_blocks_filename, optarg);
3839e657
TT
1269 break;
1270 case 'm':
ce911145 1271 reserved_ratio = strtod(optarg, &tmp);
3839e657
TT
1272 if (reserved_ratio > 50 || *tmp) {
1273 com_err(program_name, 0,
bb145b01 1274 _("invalid reserved blocks percent - %s"),
3839e657
TT
1275 optarg);
1276 exit(1);
1277 }
1278 break;
50787ea2
TT
1279 case 'n':
1280 noaction++;
1281 break;
1e3472c5
TT
1282 case 'o':
1283 creator_os = optarg;
1284 break;
2524785d
AD
1285 case 'q':
1286 quiet = 1;
1287 break;
7f88b043 1288 case 'r':
9dc6ad1e 1289 r_opt = strtoul(optarg, &tmp, 0);
2524785d
AD
1290 if (*tmp) {
1291 com_err(program_name, 0,
1292 _("bad revision level - %s"), optarg);
1293 exit(1);
1294 }
9dc6ad1e 1295 fs_param.s_rev_level = r_opt;
7f88b043 1296 break;
b10fd5e8 1297 case 's': /* deprecated */
9dc6ad1e 1298 s_opt = atoi(optarg);
521e3685 1299 break;
7f88b043 1300 case 'I':
932a489c
AD
1301 inode_size = strtoul(optarg, &tmp, 0);
1302 if (*tmp) {
1303 com_err(program_name, 0,
bb145b01 1304 _("invalid inode size - %s"), optarg);
932a489c
AD
1305 exit(1);
1306 }
7f88b043 1307 break;
3839e657
TT
1308 case 'v':
1309 verbose = 1;
1310 break;
74becf3c 1311 case 'F':
c16e610c 1312 force++;
74becf3c 1313 break;
1e3472c5
TT
1314 case 'L':
1315 volume_label = optarg;
1316 break;
1317 case 'M':
1318 mount_dir = optarg;
1319 break;
2524785d
AD
1320 case 'N':
1321 num_inodes = strtoul(optarg, &tmp, 0);
1322 if (*tmp) {
1323 com_err(program_name, 0,
1324 _("bad num inodes - %s"), optarg);
1325 exit(1);
1326 }
1327 break;
896938d5 1328 case 'O':
9dc6ad1e 1329 fs_features = optarg;
896938d5 1330 break;
c6a44136 1331 case 'E':
a29f4d30 1332 case 'R':
c6a44136 1333 extended_opts = optarg;
a29f4d30 1334 break;
f3db3566
TT
1335 case 'S':
1336 super_only = 1;
1337 break;
3d43836f 1338 case 't':
50787ea2
TT
1339 fs_type = optarg;
1340 break;
3d43836f
TT
1341 case 'T':
1342 usage_types = optarg;
1343 break;
818180cd
TT
1344 case 'V':
1345 /* Print version number and exit */
1e6e4c5e
TT
1346 show_version_only++;
1347 break;
3839e657
TT
1348 default:
1349 usage();
1350 }
3839e657 1351 }
55f4cbd9 1352 if ((optind == argc) && !show_version_only)
3839e657 1353 usage();
55f4cbd9 1354 device_name = argv[optind++];
a418d3ad 1355
1e6e4c5e 1356 if (!quiet || show_version_only)
3879857e
TT
1357 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1358 E2FSPROGS_DATE);
1359
1e6e4c5e
TT
1360 if (show_version_only) {
1361 fprintf(stderr, _("\tUsing %s\n"),
1362 error_message(EXT2_ET_BASE));
1363 exit(0);
1364 }
1365
77dc4eb0
TT
1366 /*
1367 * If there's no blocksize specified and there is a journal
1368 * device, use it to figure out the blocksize
1369 */
c5290fae 1370 if (blocksize <= 0 && journal_device) {
77dc4eb0 1371 ext2_filsys jfs;
a7ccdff8 1372 io_manager io_ptr;
77dc4eb0 1373
a7ccdff8
TT
1374#ifdef CONFIG_TESTIO_DEBUG
1375 io_ptr = test_io_manager;
1376 test_io_backing_manager = unix_io_manager;
1377#else
1378 io_ptr = unix_io_manager;
1379#endif
77dc4eb0
TT
1380 retval = ext2fs_open(journal_device,
1381 EXT2_FLAG_JOURNAL_DEV_OK, 0,
a7ccdff8 1382 0, io_ptr, &jfs);
77dc4eb0
TT
1383 if (retval) {
1384 com_err(program_name, retval,
1385 _("while trying to open journal device %s\n"),
1386 journal_device);
1387 exit(1);
1388 }
54434927 1389 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
c5290fae 1390 com_err(program_name, 0,
ddc32a04 1391 _("Journal dev blocksize (%d) smaller than "
c5290fae
TT
1392 "minimum blocksize %d\n"), jfs->blocksize,
1393 -blocksize);
1394 exit(1);
1395 }
77dc4eb0 1396 blocksize = jfs->blocksize;
9b9a780f 1397 fs_param.s_log_block_size =
77dc4eb0
TT
1398 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1399 ext2fs_close(jfs);
1400 }
dc2ec525 1401
31e29a12 1402 if (blocksize > sys_page_size) {
932a489c
AD
1403 if (!force) {
1404 com_err(program_name, 0,
1405 _("%d-byte blocks too big for system (max %d)"),
31e29a12 1406 blocksize, sys_page_size);
932a489c
AD
1407 proceed_question();
1408 }
1409 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1410 "(max %d), forced to continue\n"),
31e29a12 1411 blocksize, sys_page_size);
932a489c 1412 }
55f4cbd9 1413 if (optind < argc) {
9b9a780f
TT
1414 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1415 fs_param.s_log_block_size);
1416 if (!fs_param.s_blocks_count) {
f37ab68a 1417 com_err(program_name, 0, _("invalid blocks count - %s"),
55f4cbd9
TT
1418 argv[optind - 1]);
1419 exit(1);
1420 }
1421 }
1422 if (optind < argc)
1423 usage();
1424
74becf3c 1425 if (!force)
8ddaa66b 1426 check_plausibility(device_name);
63985320 1427 check_mount(device_name, force, _("filesystem"));
a418d3ad 1428
9b9a780f 1429 fs_param.s_log_frag_size = fs_param.s_log_block_size;
3839e657 1430
9b9a780f
TT
1431 if (noaction && fs_param.s_blocks_count) {
1432 dev_size = fs_param.s_blocks_count;
50787ea2 1433 retval = 0;
20953129
TT
1434 } else {
1435 retry:
50787ea2 1436 retval = ext2fs_get_device_size(device_name,
9b9a780f 1437 EXT2_BLOCK_SIZE(&fs_param),
50787ea2 1438 &dev_size);
20953129
TT
1439 if ((retval == EFBIG) &&
1440 (blocksize == 0) &&
9b9a780f
TT
1441 (fs_param.s_log_block_size == 0)) {
1442 fs_param.s_log_block_size = 2;
20953129
TT
1443 blocksize = 4096;
1444 goto retry;
1445 }
1446 }
1447
a789d840
TT
1448 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1449 com_err(program_name, retval,
d9c56d3c 1450 _("while trying to determine filesystem size"));
a789d840
TT
1451 exit(1);
1452 }
9b9a780f 1453 if (!fs_param.s_blocks_count) {
a789d840
TT
1454 if (retval == EXT2_ET_UNIMPLEMENTED) {
1455 com_err(program_name, 0,
d9c56d3c 1456 _("Couldn't determine device size; you "
a789d840 1457 "must specify\nthe size of the "
d9c56d3c 1458 "filesystem\n"));
a418d3ad 1459 exit(1);
26ab5315
TT
1460 } else {
1461 if (dev_size == 0) {
1462 com_err(program_name, 0,
1463 _("Device size reported to be zero. "
1464 "Invalid partition specified, or\n\t"
1465 "partition table wasn't reread "
1466 "after running fdisk, due to\n\t"
1467 "a modified partition being busy "
1468 "and in use. You may need to reboot\n\t"
1469 "to re-read your partition table.\n"
1470 ));
1471 exit(1);
1472 }
9b9a780f
TT
1473 fs_param.s_blocks_count = dev_size;
1474 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1475 fs_param.s_blocks_count &= ~((sys_page_size /
1476 EXT2_BLOCK_SIZE(&fs_param))-1);
26ab5315
TT
1477 }
1478
9b9a780f 1479 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
a789d840 1480 com_err(program_name, 0,
c5290fae 1481 _("Filesystem larger than apparent device size."));
a789d840 1482 proceed_question();
a418d3ad 1483 }
3839e657 1484
3d43836f
TT
1485 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1486 if (!fs_types) {
1487 fprintf(stderr, _("Failed to parse fs types list\n"));
1488 exit(1);
1489 }
1490 if (verbose) {
1491 fputs("Fs_types for mke2fs.conf resolution: ", stdout);
1492 print_str_list(fs_types);
1493 }
1494
9dc6ad1e 1495 if (!fs_type) {
d1b4b85c 1496 int megs = (__u64)fs_param.s_blocks_count *
9dc6ad1e
TT
1497 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1498
e066150a
BB
1499 if (fs_param.s_feature_incompat &
1500 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1501 fs_type = "journal";
1502 else if (megs <= 3)
9dc6ad1e
TT
1503 fs_type = "floppy";
1504 else if (megs <= 512)
1505 fs_type = "small";
1506 else
1507 fs_type = "default";
1508 }
1509
1510 /* Figure out what features should be enabled */
1511
3d43836f 1512 tmp = NULL;
b290d2dc 1513 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
3d43836f
TT
1514 char **cpp;
1515
1516 tmp = get_string_from_profile(fs_types, "base_features",
1517 "sparse_super,filetype,resize_inode,dir_index");
1518 edit_feature(tmp, &fs_param.s_feature_compat);
b290d2dc 1519 free(tmp);
b290d2dc 1520
3d43836f
TT
1521 for (cpp = fs_types; *cpp; cpp++) {
1522 tmp = NULL;
1523 profile_get_string(profile, "fs_types", *cpp,
1524 "features", "", &tmp);
1525 if (tmp && *tmp)
1526 edit_feature(tmp, &fs_param.s_feature_compat);
1527 if (tmp)
1528 free(tmp);
1529 }
1530 tmp = get_string_from_profile(fs_types, "default_features",
1531 "");
b290d2dc 1532 }
3d43836f 1533 edit_feature(fs_features ? fs_features : tmp,
b290d2dc
TT
1534 &fs_param.s_feature_compat);
1535 if (tmp)
1536 free(tmp);
b290d2dc
TT
1537
1538 if (r_opt == EXT2_GOOD_OLD_REV &&
1539 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1540 fs_param.s_feature_incompat)) {
9dc6ad1e
TT
1541 fprintf(stderr, _("Filesystem features not supported "
1542 "with revision 0 filesystems\n"));
1543 exit(1);
1544 }
1545
b290d2dc
TT
1546 if (s_opt > 0) {
1547 if (r_opt == EXT2_GOOD_OLD_REV) {
1548 fprintf(stderr, _("Sparse superblocks not supported "
1549 "with revision 0 filesystems\n"));
1550 exit(1);
1551 }
9dc6ad1e
TT
1552 fs_param.s_feature_ro_compat |=
1553 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
b290d2dc 1554 } else if (s_opt == 0)
9dc6ad1e
TT
1555 fs_param.s_feature_ro_compat &=
1556 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1557
b290d2dc
TT
1558 if (journal_size != 0) {
1559 if (r_opt == EXT2_GOOD_OLD_REV) {
1560 fprintf(stderr, _("Journals not supported "
1561 "with revision 0 filesystems\n"));
1562 exit(1);
1563 }
9dc6ad1e
TT
1564 fs_param.s_feature_compat |=
1565 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
b290d2dc 1566 }
9dc6ad1e
TT
1567
1568 if (fs_param.s_feature_incompat &
1569 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
9dc6ad1e
TT
1570 reserved_ratio = 0;
1571 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1572 fs_param.s_feature_compat = 0;
1573 fs_param.s_feature_ro_compat = 0;
1574 }
1575
c046ac7f
TT
1576 /* Set first meta blockgroup via an environment variable */
1577 /* (this is mostly for debugging purposes) */
9b9a780f 1578 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
c046ac7f 1579 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
9b9a780f 1580 fs_param.s_first_meta_bg = atoi(tmp);
c046ac7f 1581
93d5c387
TT
1582 /* Get the hardware sector size, if available */
1583 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1584 if (retval) {
1585 com_err(program_name, retval,
1586 _("while trying to determine hardware sector size"));
1587 exit(1);
1588 }
1cca86f5 1589
54434927 1590 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1cca86f5 1591 sector_size = atoi(tmp);
93d5c387 1592
9dc6ad1e 1593 if (blocksize <= 0) {
3d43836f 1594 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
9dc6ad1e
TT
1595
1596 if (use_bsize == -1) {
1597 use_bsize = sys_page_size;
1598 if ((linux_version_code < (2*65536 + 6*256)) &&
1599 (use_bsize > 4096))
1600 use_bsize = 4096;
1601 }
1602 if (sector_size && use_bsize < sector_size)
1603 use_bsize = sector_size;
1604 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1605 use_bsize = -blocksize;
1606 blocksize = use_bsize;
1607 fs_param.s_blocks_count /= blocksize / 1024;
1608 }
1609
1610 if (inode_ratio == 0) {
3d43836f
TT
1611 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1612 8192);
9dc6ad1e
TT
1613 if (inode_ratio < blocksize)
1614 inode_ratio = blocksize;
1615 }
1616
1617 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1618 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1619
9b9a780f 1620 blocksize = EXT2_BLOCK_SIZE(&fs_param);
a4396e9d
TT
1621
1622 lazy_itable_init = get_bool_from_profile(fs_types,
1623 "lazy_itable_init", 0);
c5290fae 1624
c6a44136 1625 if (extended_opts)
9b9a780f 1626 parse_extended_opts(&fs_param, extended_opts);
d323f8fb
TT
1627
1628 /* Since sparse_super is the default, we would only have a problem
1629 * here if it was explicitly disabled.
1630 */
9b9a780f
TT
1631 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1632 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
d323f8fb
TT
1633 com_err(program_name, 0,
1634 _("reserved online resize blocks not supported "
1635 "on non-sparse filesystem"));
1636 exit(1);
1637 }
1638
9b9a780f
TT
1639 if (fs_param.s_blocks_per_group) {
1640 if (fs_param.s_blocks_per_group < 256 ||
1641 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
521e3685 1642 com_err(program_name, 0,
d9c56d3c 1643 _("blocks per group count out of range"));
521e3685
TT
1644 exit(1);
1645 }
1646 }
1647
3d43836f
TT
1648 if (inode_size == 0)
1649 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
067911ae
AD
1650
1651 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
932a489c 1652 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
9b9a780f 1653 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
932a489c
AD
1654 inode_size & (inode_size - 1)) {
1655 com_err(program_name, 0,
bb145b01 1656 _("invalid inode size %d (min %d/max %d)"),
932a489c 1657 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
c5290fae 1658 blocksize);
932a489c
AD
1659 exit(1);
1660 }
9b9a780f 1661 fs_param.s_inode_size = inode_size;
932a489c
AD
1662 }
1663
f3358643
ES
1664 /* Make sure number of inodes specified will fit in 32 bits */
1665 if (num_inodes == 0) {
de8f3a76
AD
1666 unsigned long long n;
1667 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
f3358643
ES
1668 if (n > ~0U) {
1669 com_err(program_name, 0,
1670 _("too many inodes (%llu), raise inode ratio?"), n);
1671 exit(1);
1672 }
1673 } else if (num_inodes > ~0U) {
1674 com_err(program_name, 0,
1675 _("too many inodes (%llu), specify < 2^32 inodes"),
de8f3a76 1676 num_inodes);
f3358643
ES
1677 exit(1);
1678 }
3839e657
TT
1679 /*
1680 * Calculate number of inodes based on the inode ratio
1681 */
9b9a780f
TT
1682 fs_param.s_inodes_count = num_inodes ? num_inodes :
1683 ((__u64) fs_param.s_blocks_count * blocksize)
f3db3566 1684 / inode_ratio;
3839e657 1685
dcf7b091
AD
1686 if ((((long long)fs_param.s_inodes_count) *
1687 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1688 (((long long)fs_param.s_blocks_count) *
1689 EXT2_BLOCK_SIZE(&fs_param))) {
1690 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1691 "(%u) too big for a\n\t"
1692 "filesystem with %lu blocks, "
1693 "specify higher inode_ratio (-i)\n\t"
1694 "or lower inode count (-N).\n"),
1695 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
de8f3a76
AD
1696 fs_param.s_inodes_count,
1697 (unsigned long) fs_param.s_blocks_count);
dcf7b091
AD
1698 exit(1);
1699 }
1700
3839e657
TT
1701 /*
1702 * Calculate number of blocks to reserve
1703 */
a8862d9e
TT
1704 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1705 fs_param.s_blocks_count);
3839e657 1706}
d323f8fb 1707
3839e657
TT
1708int main (int argc, char *argv[])
1709{
1710 errcode_t retval = 0;
1711 ext2_filsys fs;
1712 badblocks_list bb_list = 0;
d4e0b1c6 1713 unsigned int journal_blocks;
54434927
TT
1714 unsigned int i;
1715 int val;
a7ccdff8 1716 io_manager io_ptr;
d9c56d3c
TT
1717
1718#ifdef ENABLE_NLS
1719 setlocale(LC_MESSAGES, "");
14308a53 1720 setlocale(LC_CTYPE, "");
d9c56d3c
TT
1721 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1722 textdomain(NLS_CAT_NAME);
1723#endif
3839e657
TT
1724 PRS(argc, argv);
1725
a7ccdff8
TT
1726#ifdef CONFIG_TESTIO_DEBUG
1727 io_ptr = test_io_manager;
1728 test_io_backing_manager = unix_io_manager;
1729#else
1730 io_ptr = unix_io_manager;
1731#endif
1732
3839e657
TT
1733 /*
1734 * Initialize the superblock....
1735 */
616059bf 1736 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
a7ccdff8 1737 io_ptr, &fs);
3839e657 1738 if (retval) {
d9c56d3c 1739 com_err(device_name, retval, _("while setting up superblock"));
3839e657
TT
1740 exit(1);
1741 }
1742
6cb27404
TT
1743 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1744 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1745
e41784eb
TT
1746 /*
1747 * Wipe out the old on-disk superblock
1748 */
04a96857
TT
1749 if (!noaction)
1750 zap_sector(fs, 2, 6);
e41784eb 1751
1e3472c5
TT
1752 /*
1753 * Generate a UUID for it...
1754 */
ef9abe5f 1755 uuid_generate(fs->super->s_uuid);
1e3472c5 1756
843049c4
TT
1757 /*
1758 * Initialize the directory index variables
1759 */
1760 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1761 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1762
44c09c04
TT
1763 /*
1764 * Add "jitter" to the superblock's check interval so that we
1765 * don't check all the filesystems at the same time. We use a
1766 * kludgy hack of using the UUID to derive a random jitter value.
1767 */
1768 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1769 val += fs->super->s_uuid[i];
1770 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1771
1e3472c5
TT
1772 /*
1773 * Override the creator OS, if applicable
1774 */
1775 if (creator_os && !set_os(fs->super, creator_os)) {
d9c56d3c 1776 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1e3472c5
TT
1777 exit(1);
1778 }
1779
4ea0a110
TT
1780 /*
1781 * For the Hurd, we will turn off filetype since it doesn't
1782 * support it.
1783 */
1784 if (fs->super->s_creator_os == EXT2_OS_HURD)
1785 fs->super->s_feature_incompat &=
1786 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1787
1e3472c5
TT
1788 /*
1789 * Set the volume label...
1790 */
1791 if (volume_label) {
ef9abe5f
TT
1792 memset(fs->super->s_volume_name, 0,
1793 sizeof(fs->super->s_volume_name));
1794 strncpy(fs->super->s_volume_name, volume_label,
1795 sizeof(fs->super->s_volume_name));
1e3472c5
TT
1796 }
1797
1798 /*
1799 * Set the last mount directory
1800 */
1801 if (mount_dir) {
ef9abe5f
TT
1802 memset(fs->super->s_last_mounted, 0,
1803 sizeof(fs->super->s_last_mounted));
1804 strncpy(fs->super->s_last_mounted, mount_dir,
1805 sizeof(fs->super->s_last_mounted));
1e3472c5
TT
1806 }
1807
50787ea2 1808 if (!quiet || noaction)
3839e657
TT
1809 show_stats(fs);
1810
50787ea2
TT
1811 if (noaction)
1812 exit(0);
1813
16ed5b3a
TT
1814 if (fs->super->s_feature_incompat &
1815 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1816 create_journal_dev(fs);
568101f7 1817 exit(ext2fs_close(fs) ? 1 : 0);
16ed5b3a
TT
1818 }
1819
3839e657
TT
1820 if (bad_blocks_filename)
1821 read_bb_file(fs, &bb_list, bad_blocks_filename);
1822 if (cflag)
1823 test_disk(fs, &bb_list);
1824
1825 handle_bad_blocks(fs, bb_list);
0c17cb25 1826 fs->stride = fs_stride = fs->super->s_raid_stride;
19c78dc0
TT
1827 retval = ext2fs_allocate_tables(fs);
1828 if (retval) {
1829 com_err(program_name, retval,
d9c56d3c 1830 _("while trying to allocate filesystem tables"));
19c78dc0
TT
1831 exit(1);
1832 }
f3db3566
TT
1833 if (super_only) {
1834 fs->super->s_state |= EXT2_ERROR_FS;
1835 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1836 } else {
59f27247 1837 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
54434927 1838 unsigned int rsv = 65536 / fs->blocksize;
1400bbb6 1839 unsigned long blocks = fs->super->s_blocks_count;
59f27247 1840 unsigned long start;
1400bbb6
TT
1841 blk_t ret_blk;
1842
1843#ifdef ZAP_BOOTBLOCK
04a96857 1844 zap_sector(fs, 0, 2);
1400bbb6 1845#endif
59f27247 1846
1400bbb6
TT
1847 /*
1848 * Wipe out any old MD RAID (or other) metadata at the end
1849 * of the device. This will also verify that the device is
59f27247 1850 * as large as we think. Be careful with very small devices.
1400bbb6 1851 */
59f27247
AD
1852 start = (blocks & ~(rsv - 1));
1853 if (start > rsv)
1854 start -= rsv;
1855 if (start > 0)
1856 retval = zero_blocks(fs, start, blocks - start,
1857 NULL, &ret_blk, NULL);
1858
1400bbb6
TT
1859 if (retval) {
1860 com_err(program_name, retval,
f044b4d8 1861 _("while zeroing block %u at end of filesystem"),
1400bbb6 1862 ret_blk);
1400bbb6 1863 }
a4396e9d 1864 write_inode_tables(fs, lazy_itable_init);
f3db3566
TT
1865 create_root_dir(fs);
1866 create_lost_and_found(fs);
1867 reserve_inodes(fs);
1868 create_bad_block_inode(fs, bb_list);
ea774315
TT
1869 if (fs->super->s_feature_compat &
1870 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1871 retval = ext2fs_create_resize_inode(fs);
1872 if (retval) {
1873 com_err("ext2fs_create_resize_inode", retval,
d323f8fb 1874 _("while reserving blocks for online resize"));
ea774315
TT
1875 exit(1);
1876 }
d323f8fb 1877 }
f3db3566 1878 }
8ddaa66b 1879
16ed5b3a
TT
1880 if (journal_device) {
1881 ext2_filsys jfs;
1882
8ddaa66b
TT
1883 if (!force)
1884 check_plausibility(journal_device);
63985320 1885 check_mount(journal_device, force, _("journal"));
8ddaa66b 1886
16ed5b3a
TT
1887 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1888 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1889 fs->blocksize, unix_io_manager, &jfs);
1890 if (retval) {
1891 com_err(program_name, retval,
1892 _("while trying to open journal device %s\n"),
1893 journal_device);
1894 exit(1);
1895 }
93345d15 1896 if (!quiet) {
77dc4eb0 1897 printf(_("Adding journal to device %s: "),
8ddaa66b 1898 journal_device);
93345d15
TT
1899 fflush(stdout);
1900 }
16ed5b3a
TT
1901 retval = ext2fs_add_journal_device(fs, jfs);
1902 if(retval) {
8ddaa66b 1903 com_err (program_name, retval,
1d08d9bf 1904 _("\n\twhile trying to add journal to device %s"),
8ddaa66b
TT
1905 journal_device);
1906 exit(1);
1907 }
1908 if (!quiet)
1909 printf(_("done\n"));
16ed5b3a 1910 ext2fs_close(jfs);
2d15576d 1911 free(journal_device);
9dc6ad1e
TT
1912 } else if ((journal_size) ||
1913 (fs_param.s_feature_compat &
1914 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2537b6d0 1915 journal_blocks = figure_journal_size(journal_size, fs);
93345d15
TT
1916
1917 if (!journal_blocks) {
1918 fs->super->s_feature_compat &=
1919 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1920 goto no_journal;
1921 }
1922 if (!quiet) {
d4e0b1c6 1923 printf(_("Creating journal (%u blocks): "),
16ad3334 1924 journal_blocks);
93345d15
TT
1925 fflush(stdout);
1926 }
63985320
TT
1927 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1928 journal_flags);
85ef4ae8
TT
1929 if (retval) {
1930 com_err (program_name, retval,
1d08d9bf 1931 _("\n\twhile trying to create journal"));
85ef4ae8
TT
1932 exit(1);
1933 }
1934 if (!quiet)
1935 printf(_("done\n"));
1936 }
93345d15
TT
1937no_journal:
1938
3839e657 1939 if (!quiet)
d9c56d3c
TT
1940 printf(_("Writing superblocks and "
1941 "filesystem accounting information: "));
5d45d803
TT
1942 retval = ext2fs_flush(fs);
1943 if (retval) {
6693837e
TT
1944 fprintf(stderr,
1945 _("\nWarning, had trouble writing out superblocks."));
5d45d803 1946 }
93345d15 1947 if (!quiet) {
2537b6d0 1948 printf(_("done\n\n"));
1cca86f5
TT
1949 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1950 print_check_message(fs);
93345d15 1951 }
568101f7 1952 val = ext2fs_close(fs);
a6d8302b
TT
1953 remove_error_table(&et_ext2_error_table);
1954 remove_error_table(&et_prof_error_table);
3d43836f 1955 profile_release(profile);
568101f7 1956 return (retval || val) ? 1 : 0;
3839e657 1957}