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