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