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