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