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