blob: 89b4f6b404353e241fe420e19596be0fb1acb2c2 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * 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%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
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
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000018#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <time.h>
Theodore Ts'o756df352002-03-07 20:52:12 -050023#ifdef __linux__
Theodore Ts'o27401561999-09-14 20:11:19 +000024#include <sys/utsname.h>
25#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000028#else
29extern char *optarg;
30extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#endif
32#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
35#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000037#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000045#include <sys/types.h>
46
Andreas Dilger932a4892002-05-16 03:20:07 -060047#ifdef HAVE_ASM_PAGE_H
48#include <asm/page.h>
49#define SYS_MAX_BLOCKSIZE PAGE_SIZE
50#else
51#define SYS_MAX_BLOCKSIZE 4096
52#endif
53
Theodore Ts'o54c637d2001-05-14 11:45:38 +000054#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000056#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000057#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000058#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000059#include "util.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000060#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000061#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000062
63#define STRIDE_LENGTH 8
64
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000065#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000066#define ZAP_BOOTBLOCK
67#endif
68
Theodore Ts'o3839e651997-04-26 13:21:57 +000069extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000070extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000071
72const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000073const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000074
75/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000076int cflag;
77int verbose;
78int quiet;
79int super_only;
80int force;
81int noaction;
82int journal_size;
83int journal_flags;
84char *bad_blocks_filename;
85__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
87struct ext2_super_block param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000088char *creator_os;
89char *volume_label;
90char *mount_dir;
91char *journal_device;
92int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000093
Theodore Ts'o63985322001-01-03 17:02:13 +000094static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000095{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000096 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000097 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000098 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
99 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +0000100 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000101 "[-r fs-revision] [-R raid_opts] [-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102 program_name);
103 exit(1);
104}
105
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000106static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107{
108 int l = 0;
109
110 arg >>= 1;
111 while (arg) {
112 l++;
113 arg >>= 1;
114 }
115 return l;
116}
117
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000118static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000119{
120 int l;
121
122 for (l=0; arg ; l++)
123 arg = arg / 10;
124 return l;
125}
126
Theodore Ts'o3839e651997-04-26 13:21:57 +0000127/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000128 * This function sets the default parameters for a filesystem
129 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000130 * The type is specified by the user. The size is the maximum size
131 * (in megabytes) for which a set of parameters applies, with a size
132 * of zero meaning that it is the default parameter for the type.
133 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000134 */
135static char default_str[] = "default";
136struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000137 const char *type;
138 int size;
139 int blocksize;
140 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000141} settings[] = {
142 { default_str, 0, 4096, 8192 },
143 { default_str, 512, 1024, 4096 },
144 { default_str, 3, 1024, 8192 },
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000145 { "journal", 0, 4096, 8192 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000146 { "news", 0, 4096, 4096 },
Andreas Dilger932a4892002-05-16 03:20:07 -0600147 { "largefile", 0, SYS_MAX_BLOCKSIZE, 1024 * 1024 },
148 { "largefile4", 0, SYS_MAX_BLOCKSIZE, 4096 * 1024 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000149 { 0, 0, 0, 0},
150};
151
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500152static void set_fs_defaults(const char *fs_type,
153 struct ext2_super_block *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000154 int blocksize, int *inode_ratio)
155{
156 int megs;
157 int ratio = 0;
158 struct mke2fs_defaults *p;
159
Andreas Dilger932a4892002-05-16 03:20:07 -0600160 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000161 if (inode_ratio)
162 ratio = *inode_ratio;
163 if (!fs_type)
164 fs_type = default_str;
165 for (p = settings; p->type; p++) {
166 if ((strcmp(p->type, fs_type) != 0) &&
167 (strcmp(p->type, default_str) != 0))
168 continue;
169 if ((p->size != 0) &&
170 (megs > p->size))
171 continue;
172 if (ratio == 0)
173 *inode_ratio = p->inode_ratio;
174 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000175 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000176 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000177 }
178 }
179 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000180 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000181}
182
183/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184 * Helper function for read_bb_file and test_disk
185 */
186static void invalid_block(ext2_filsys fs, blk_t blk)
187{
Theodore Ts'o66938372002-03-08 00:14:46 -0500188 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189 return;
190}
191
192/*
193 * Reads the bad blocks list from a file
194 */
195static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
196 const char *bad_blocks_file)
197{
198 FILE *f;
199 errcode_t retval;
200
201 f = fopen(bad_blocks_file, "r");
202 if (!f) {
203 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000204 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 exit(1);
206 }
207 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
208 fclose (f);
209 if (retval) {
210 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000211 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000212 exit(1);
213 }
214}
215
216/*
217 * Runs the badblocks program to test the disk
218 */
219static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
220{
221 FILE *f;
222 errcode_t retval;
223 char buf[1024];
224
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500225 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
226 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
227 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000229 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230 f = popen(buf, "r");
231 if (!f) {
232 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000233 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000234 exit(1);
235 }
236 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000237 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 if (retval) {
239 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000240 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 exit(1);
242 }
243}
244
245static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
246{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000247 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 int must_be_good;
249 blk_t blk;
250 badblocks_iterate bb_iter;
251 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000252 blk_t group_block;
253 int group;
254 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000255
256 if (!bb_list)
257 return;
258
259 /*
260 * The primary superblock and group descriptors *must* be
261 * good; if not, abort.
262 */
263 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
264 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000265 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000266 fprintf(stderr, _("Block %d in primary "
267 "superblock/group descriptor area bad.\n"), i);
268 fprintf(stderr, _("Blocks %d through %d must be good "
269 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000271 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272 exit(1);
273 }
274 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000275
276 /*
277 * See if any of the bad blocks are showing up in the backup
278 * superblocks and/or group descriptors. If so, issue a
279 * warning and adjust the block counts appropriately.
280 */
281 group_block = fs->super->s_first_data_block +
282 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000283
284 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000285 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000286 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000287 if (ext2fs_badblocks_list_test(bb_list,
288 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289 if (!group_bad)
290 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000291_("Warning: the backup superblock/group descriptors at block %d contain\n"
292" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000293 group_block);
294 group_bad++;
295 group = ext2fs_group_of_blk(fs, group_block+j);
296 fs->group_desc[group].bg_free_blocks_count++;
297 fs->super->s_free_blocks_count++;
298 }
299 }
300 group_block += fs->super->s_blocks_per_group;
301 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000302
303 /*
304 * Mark all the bad blocks as used...
305 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000306 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000308 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000309 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310 exit(1);
311 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000312 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000313 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000314 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315}
316
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000317/*
318 * These functions implement a generalized progress meter.
319 */
320struct progress_struct {
321 char format[20];
322 char backup[80];
323 __u32 max;
324};
325
326static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500327 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000328{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000329 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000330
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000331 memset(progress, 0, sizeof(struct progress_struct));
332 if (quiet)
333 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000334
335 /*
336 * Figure out how many digits we need
337 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000338 i = int_log10(max);
339 sprintf(progress->format, "%%%dd/%%%dld", i, i);
340 memset(progress->backup, '\b', sizeof(progress->backup)-1);
341 progress->backup[sizeof(progress->backup)-1] = 0;
342 if ((2*i)+1 < sizeof(progress->backup))
343 progress->backup[(2*i)+1] = 0;
344 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000345
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000346 fputs(label, stdout);
347 fflush(stdout);
348}
349
350static void progress_update(struct progress_struct *progress, __u32 val)
351{
352 if (progress->format[0] == 0)
353 return;
354 printf(progress->format, val, progress->max);
355 fputs(progress->backup, stdout);
356}
357
358static void progress_close(struct progress_struct *progress)
359{
360 if (progress->format[0] == 0)
361 return;
362 fputs(_("done \n"), stdout);
363}
364
365
366/*
367 * Helper function which zeros out _num_ blocks starting at _blk_. In
368 * case of an error, the details of the error is returned via _ret_blk_
369 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
370 * success, and an error code on an error.
371 *
372 * As a special case, if the first argument is NULL, then it will
373 * attempt to free the static zeroizing buffer. (This is to keep
374 * programs that check for memory leaks happy.)
375 */
376static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
377 struct progress_struct *progress,
378 blk_t *ret_blk, int *ret_count)
379{
380 int j, count, next_update, next_update_incr;
381 static char *buf;
382 errcode_t retval;
383
384 /* If fs is null, clean up the static buffer and return */
385 if (!fs) {
386 if (buf) {
387 free(buf);
388 buf = 0;
389 }
390 return 0;
391 }
392 /* Allocate the zeroizing buffer if necessary */
393 if (!buf) {
394 buf = malloc(fs->blocksize * STRIDE_LENGTH);
395 if (!buf) {
396 com_err("malloc", ENOMEM,
397 _("while allocating zeroizing buffer"));
398 exit(1);
399 }
400 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
401 }
402 /* OK, do the write loop */
403 next_update = 0;
404 next_update_incr = num / 100;
405 if (next_update_incr < 1)
406 next_update_incr = 1;
407 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
408 if (num-j > STRIDE_LENGTH)
409 count = STRIDE_LENGTH;
410 else
411 count = num - j;
412 retval = io_channel_write_blk(fs->io, blk, count, buf);
413 if (retval) {
414 if (ret_count)
415 *ret_count = count;
416 if (ret_blk)
417 *ret_blk = blk;
418 return retval;
419 }
420 if (progress && j > next_update) {
421 next_update += num / 100;
422 progress_update(progress, blk);
423 }
424 }
425 return 0;
426}
427
428static void write_inode_tables(ext2_filsys fs)
429{
430 errcode_t retval;
431 blk_t blk;
432 int i, num;
433 struct progress_struct progress;
434
435 if (quiet)
436 memset(&progress, 0, sizeof(progress));
437 else
438 progress_init(&progress, _("Writing inode tables: "),
439 fs->group_desc_count);
440
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000442 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000444 blk = fs->group_desc[i].bg_inode_table;
445 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000446
447 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
448 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500449 fprintf(stderr, _("\nCould not write %d blocks "
450 "in inode table starting at %d: %s\n"),
451 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000452 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000453 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000454 if (sync_kludge) {
455 if (sync_kludge == 1)
456 sync();
457 else if ((i % sync_kludge) == 0)
458 sync();
459 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000461 zero_blocks(0, 0, 0, 0, 0, 0);
462 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463}
464
465static void create_root_dir(ext2_filsys fs)
466{
467 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000468 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469
470 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
471 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000472 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473 exit(1);
474 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000475 if (geteuid()) {
476 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
477 if (retval) {
478 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000479 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000480 exit(1);
481 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000482 inode.i_uid = getuid();
483 if (inode.i_uid)
484 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000485 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
486 if (retval) {
487 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000488 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000489 exit(1);
490 }
491 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492}
493
494static void create_lost_and_found(ext2_filsys fs)
495{
496 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000497 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 const char *name = "lost+found";
499 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000500 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501
Theodore Ts'o6a525062001-12-24 09:40:00 -0500502 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
504 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000505 com_err("ext2fs_mkdir", retval,
506 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507 exit(1);
508 }
509
510 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
511 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000512 com_err("ext2_lookup", retval,
513 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 exit(1);
515 }
516
517 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000518 if ((lpf_size += fs->blocksize) >= 16*1024)
519 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000520 retval = ext2fs_expand_dir(fs, ino);
521 if (retval) {
522 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000523 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 exit(1);
525 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500526 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527}
528
529static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
530{
531 errcode_t retval;
532
Theodore Ts'of3db3561997-04-26 13:34:30 +0000533 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 fs->group_desc[0].bg_free_inodes_count--;
535 fs->super->s_free_inodes_count--;
536 retval = ext2fs_update_bb_inode(fs, bb_list);
537 if (retval) {
538 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000539 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540 exit(1);
541 }
542
543}
544
545static void reserve_inodes(ext2_filsys fs)
546{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000547 ext2_ino_t i;
548 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000550 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000551 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 group = ext2fs_group_of_ino(fs, i);
553 fs->group_desc[group].bg_free_inodes_count--;
554 fs->super->s_free_inodes_count--;
555 }
556 ext2fs_mark_ib_dirty(fs);
557}
558
Theodore Ts'o756df352002-03-07 20:52:12 -0500559#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500560#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500561#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500562
Theodore Ts'o04a96852001-08-30 21:55:26 -0400563static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000564{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400565 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000566 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500567 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000568
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400569 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600570 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500571 printf(_("Out of memory erasing sectors %d-%d\n"),
572 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600573 exit(1);
574 }
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400575 memset(buf, 0, 512*nsect);
Theodore Ts'o756df352002-03-07 20:52:12 -0500576
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500577 if (sect == 0) {
578 /* Check for a BSD disklabel, and don't erase it if so */
579 retval = io_channel_read_blk(fs->io, 0, -512, buf);
580 if (retval)
581 fprintf(stderr,
582 _("Warning: could not read block 0: %s\n"),
583 error_message(retval));
584 else {
585 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
586 if ((*magic == BSD_DISKMAGIC) ||
587 (*magic == BSD_MAGICDISK))
588 return;
589 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500590 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500591
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000592 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400593 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000594 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400595 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000596 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500597 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
598 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000599}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000600
601static void create_journal_dev(ext2_filsys fs)
602{
603 struct progress_struct progress;
604 errcode_t retval;
605 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000606 blk_t blk;
607 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000608
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000609 retval = ext2fs_create_journal_superblock(fs,
610 fs->super->s_blocks_count, 0, &buf);
611 if (retval) {
612 com_err("create_journal_dev", retval,
613 _("while initializing journal superblock"));
614 exit(1);
615 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000616 if (quiet)
617 memset(&progress, 0, sizeof(progress));
618 else
619 progress_init(&progress, _("Zeroing journal device: "),
620 fs->super->s_blocks_count);
621
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000622 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
623 &progress, &blk, &count);
624 if (retval) {
625 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000626 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000627 blk, count);
628 exit(1);
629 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000630 zero_blocks(0, 0, 0, 0, 0, 0);
631
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000632 retval = io_channel_write_blk(fs->io,
633 fs->super->s_first_data_block+1,
634 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000635 if (retval) {
636 com_err("create_journal_dev", retval,
637 _("while writing journal superblock"));
638 exit(1);
639 }
640 progress_close(&progress);
641}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000642
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643static void show_stats(ext2_filsys fs)
644{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000645 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000646 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000648 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649
650 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'o66938372002-03-08 00:14:46 -0500651 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000652 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000653
654 memset(buf, 0, sizeof(buf));
655 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000656 printf(_("Filesystem label=%s\n"), buf);
657 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000658 switch (fs->super->s_creator_os) {
659 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000660 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000661 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000662 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000663 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000664 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000665 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000666 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000667 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000668 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000669 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000671 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000672 s->s_r_blocks_count,
673 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000674 printf(_("First data block=%u\n"), s->s_first_data_block);
675 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000676 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000677 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000678 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000679 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000681 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000682
683 if (fs->group_desc_count == 1) {
684 printf("\n");
685 return;
686 }
687
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000688 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000689 group_block = s->s_first_data_block;
690 col_left = 0;
691 for (i = 1; i < fs->group_desc_count; i++) {
692 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000693 if (!ext2fs_bg_has_super(fs, i))
694 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000695 if (i != 1)
696 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000697 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000698 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000699 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000700 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000701 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000702 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000703 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000704 }
705 printf("\n\n");
706}
707
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000708/*
709 * Set the S_CREATOR_OS field. Return true if OS is known,
710 * otherwise, 0.
711 */
712static int set_os(struct ext2_super_block *sb, char *os)
713{
714 if (isdigit (*os))
715 sb->s_creator_os = atoi (os);
716 else if (strcasecmp(os, "linux") == 0)
717 sb->s_creator_os = EXT2_OS_LINUX;
718 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
719 sb->s_creator_os = EXT2_OS_HURD;
720 else if (strcasecmp(os, "masix") == 0)
721 sb->s_creator_os = EXT2_OS_MASIX;
722 else
723 return 0;
724 return 1;
725}
726
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000727#define PATH_SET "PATH=/sbin"
728
Theodore Ts'od163b091997-10-03 17:42:28 +0000729static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000730{
731 char *buf, *token, *next, *p, *arg;
732 int len;
733 int raid_usage = 0;
734
735 len = strlen(opts);
736 buf = malloc(len+1);
737 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000738 fprintf(stderr, _("Couldn't allocate memory to parse "
739 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000740 exit(1);
741 }
742 strcpy(buf, opts);
743 for (token = buf; token && *token; token = next) {
744 p = strchr(token, ',');
745 next = 0;
746 if (p) {
747 *p = 0;
748 next = p+1;
749 }
750 arg = strchr(token, '=');
751 if (arg) {
752 *arg = 0;
753 arg++;
754 }
755 if (strcmp(token, "stride") == 0) {
756 if (!arg) {
757 raid_usage++;
758 continue;
759 }
760 fs_stride = strtoul(arg, &p, 0);
761 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000762 fprintf(stderr,
763 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000764 raid_usage++;
765 continue;
766 }
767 } else
768 raid_usage++;
769 }
770 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000771 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000772 "Raid options are separated by commas, "
773 "and may take an argument which\n"
774 "\tis set off by an equals ('=') sign.\n\n"
775 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000776 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000777 exit(1);
778 }
779}
780
Theodore Ts'o896938d1999-10-23 01:04:50 +0000781static __u32 ok_features[3] = {
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000782 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000783 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
784 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000785 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
786};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000787
788
Theodore Ts'o3839e651997-04-26 13:21:57 +0000789static void PRS(int argc, char *argv[])
790{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000791 int c;
792 int size;
793 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000794 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000795 int blocksize = 0;
796 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600797 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000798 int reserved_ratio = 5;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000799 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000800 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000801 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000802 char * raid_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500803 const char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400804 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000805 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500806#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000807 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000808#endif
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000809
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000811 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);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000821
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000822 tmp = getenv("MKE2FS_SYNC");
823 if (tmp)
824 sync_kludge = atoi(tmp);
825
Theodore Ts'o3839e651997-04-26 13:21:57 +0000826 setbuf(stdout, NULL);
827 setbuf(stderr, NULL);
828 initialize_ext2_error_table();
829 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000830 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400831 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
832 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
833
Theodore Ts'o756df352002-03-07 20:52:12 -0500834#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000835 if (uname(&ut)) {
836 perror("uname");
837 exit(1);
838 }
839 if ((ut.release[0] == '1') ||
840 (ut.release[0] == '2' && ut.release[1] == '.' &&
841 ut.release[2] < '2' && ut.release[3] == '.')) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000842 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400843 param.s_feature_incompat = 0;
844 param.s_feature_compat = 0;
845 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000846 }
847#endif
Andreas Dilger9d8e6342001-08-31 10:24:46 -0600848 fprintf (stderr, "mke2fs %s (%s)\n",
Theodore Ts'o04a96852001-08-30 21:55:26 -0400849 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700850
851 if (argc && *argv) {
852 program_name = get_progname(*argv);
853
854 /* If called as mkfs.ext3, create a journal inode */
855 if (!strcmp(program_name, "mkfs.ext3"))
856 journal_size = -1;
857 }
858
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000859 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000860 "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000861 switch (c) {
862 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000863 blocksize = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600864 if (blocksize < EXT2_MIN_BLOCK_SIZE ||
865 blocksize > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000866 com_err(program_name, 0,
867 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000868 exit(1);
869 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600870 if (blocksize > 4096)
871 fprintf(stderr, _("Warning: blocksize %d not "
872 "usable on most systems.\n"),
873 blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000874 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000875 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000876 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000877 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000878 case 'c': /* Check for bad blocks */
879 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500880 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000881 break;
882 case 'f':
883 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600884 if (size < EXT2_MIN_BLOCK_SIZE ||
885 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000886 com_err(program_name, 0,
887 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000888 optarg);
889 exit(1);
890 }
891 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000892 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500893 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000894 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000895 break;
896 case 'g':
897 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000898 if (*tmp) {
899 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000900 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000901 exit(1);
902 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000903 if ((param.s_blocks_per_group % 8) != 0) {
904 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000905 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000906 exit(1);
907 }
908 break;
909 case 'i':
910 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600911 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
912 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000913 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000914 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -0600915 _("bad inode ratio %s (min %d/max %d"),
916 optarg, EXT2_MIN_BLOCK_SIZE,
917 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918 exit(1);
919 }
920 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000921 case 'J':
922 parse_journal_opts(optarg);
923 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000924 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000925 param.s_feature_compat |=
926 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000927 if (!journal_size)
928 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000929 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000930 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000931 bad_blocks_filename = malloc(strlen(optarg)+1);
932 if (!bad_blocks_filename) {
933 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000934 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000935 exit(1);
936 }
937 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000938 break;
939 case 'm':
940 reserved_ratio = strtoul(optarg, &tmp, 0);
941 if (reserved_ratio > 50 || *tmp) {
942 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000943 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000944 optarg);
945 exit(1);
946 }
947 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000948 case 'n':
949 noaction++;
950 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000951 case 'o':
952 creator_os = optarg;
953 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000954 case 'r':
955 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000956 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400957 param.s_feature_incompat = 0;
958 param.s_feature_compat = 0;
959 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000960 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000961 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000962 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400963 if (atoi(optarg))
964 param.s_feature_ro_compat |=
965 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
966 else
967 param.s_feature_ro_compat &=
968 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000969 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000970#ifdef EXT2_DYNAMIC_REV
971 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -0600972 inode_size = strtoul(optarg, &tmp, 0);
973 if (*tmp) {
974 com_err(program_name, 0,
975 _("bad inode size - %s"), optarg);
976 exit(1);
977 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000978 break;
979#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000980 case 'N':
981 num_inodes = atoi(optarg);
982 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000983 case 'v':
984 verbose = 1;
985 break;
986 case 'q':
987 quiet = 1;
988 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000989 case 'F':
990 force = 1;
991 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000992 case 'L':
993 volume_label = optarg;
994 break;
995 case 'M':
996 mount_dir = optarg;
997 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000998 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400999 if (!strcmp(optarg, "none") || default_features) {
1000 param.s_feature_compat = 0;
1001 param.s_feature_incompat = 0;
1002 param.s_feature_ro_compat = 0;
1003 default_features = 0;
1004 }
1005 if (!strcmp(optarg, "none"))
1006 break;
1007 if (e2p_edit_feature(optarg,
1008 &param.s_feature_compat,
1009 ok_features)) {
1010 fprintf(stderr,
1011 _("Invalid filesystem option set: %s\n"), optarg);
1012 exit(1);
1013 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001014 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001015 case 'R':
1016 raid_opts = optarg;
1017 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001018 case 'S':
1019 super_only = 1;
1020 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001021 case 'T':
1022 fs_type = optarg;
1023 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001024 case 'V':
1025 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001026 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +00001027 error_message(EXT2_ET_BASE));
1028 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001029 default:
1030 usage();
1031 }
1032 if (optind == argc)
1033 usage();
1034 device_name = argv[optind];
1035 optind++;
1036 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001037 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1038
1039 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001040 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041 argv[optind - 1]);
1042 exit(1);
1043 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001044 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001045 }
1046 if (optind < argc)
1047 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001048
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001049 if (raid_opts)
1050 parse_raid_opts(raid_opts);
1051
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001052 /*
1053 * If there's no blocksize specified and there is a journal
1054 * device, use it to figure out the blocksize
1055 */
1056 if (blocksize == 0 && journal_device) {
1057 ext2_filsys jfs;
1058
1059 retval = ext2fs_open(journal_device,
1060 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1061 0, unix_io_manager, &jfs);
1062 if (retval) {
1063 com_err(program_name, retval,
1064 _("while trying to open journal device %s\n"),
1065 journal_device);
1066 exit(1);
1067 }
1068 blocksize = jfs->blocksize;
1069 param.s_log_block_size =
1070 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1071 ext2fs_close(jfs);
1072 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001073
Andreas Dilger932a4892002-05-16 03:20:07 -06001074 if (blocksize > SYS_MAX_BLOCKSIZE) {
1075 if (!force) {
1076 com_err(program_name, 0,
1077 _("%d-byte blocks too big for system (max %d)"),
1078 blocksize, SYS_MAX_BLOCKSIZE);
1079 proceed_question();
1080 }
1081 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1082 "(max %d), forced to continue\n"),
1083 blocksize, SYS_MAX_BLOCKSIZE);
1084 }
1085
Andreas Dilgerfa724582001-08-04 01:18:34 -06001086 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1087 if (!fs_type)
1088 fs_type = "journal";
1089 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001090 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1091 param.s_feature_compat = 0;
1092 param.s_feature_ro_compat = 0;
1093 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001094 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1095 (param.s_feature_compat || param.s_feature_ro_compat ||
1096 param.s_feature_incompat))
1097 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1098
Theodore Ts'o74becf31997-04-26 14:37:06 +00001099 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001100 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001101 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001102
Theodore Ts'o3839e651997-04-26 13:21:57 +00001103 param.s_log_frag_size = param.s_log_block_size;
1104
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001105 if (noaction && param.s_blocks_count) {
1106 dev_size = param.s_blocks_count;
1107 retval = 0;
1108 } else
1109 retval = ext2fs_get_device_size(device_name,
1110 EXT2_BLOCK_SIZE(&param),
1111 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001112 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1113 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001114 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001115 exit(1);
1116 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001117 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001118 if (retval == EXT2_ET_UNIMPLEMENTED) {
1119 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001120 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001121 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001122 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001123 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001124 } else {
1125 if (dev_size == 0) {
1126 com_err(program_name, 0,
1127 _("Device size reported to be zero. "
1128 "Invalid partition specified, or\n\t"
1129 "partition table wasn't reread "
1130 "after running fdisk, due to\n\t"
1131 "a modified partition being busy "
1132 "and in use. You may need to reboot\n\t"
1133 "to re-read your partition table.\n"
1134 ));
1135 exit(1);
1136 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001137 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001138 }
1139
Theodore Ts'oa789d841998-03-30 01:20:55 +00001140 } else if (!force && (param.s_blocks_count > dev_size)) {
1141 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001142 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001143 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001144 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001145
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001146 /*
1147 * If the user asked for HAS_JOURNAL, then make sure a journal
1148 * gets created.
1149 */
1150 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1151 !journal_size)
1152 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001153
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001154 set_fs_defaults(fs_type, &param, blocksize, &inode_ratio);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001155
Theodore Ts'o521e3681997-04-29 17:48:10 +00001156 if (param.s_blocks_per_group) {
1157 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001158 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001159 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001160 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001161 exit(1);
1162 }
1163 }
1164
Andreas Dilger932a4892002-05-16 03:20:07 -06001165 if (inode_size) {
1166 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1167 inode_size > EXT2_BLOCK_SIZE(&param) ||
1168 inode_size & (inode_size - 1)) {
1169 com_err(program_name, 0,
1170 _("bad inode size %d (min %d/max %d)"),
1171 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1172 EXT2_BLOCK_SIZE(&param));
1173 exit(1);
1174 }
1175 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1176 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1177 "on most systems\n"),
1178 inode_size);
1179 param.s_inode_size = inode_size;
1180 }
1181
Theodore Ts'o3839e651997-04-26 13:21:57 +00001182 /*
1183 * Calculate number of inodes based on the inode ratio
1184 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001185 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +00001186 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +00001187 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001188
1189 /*
1190 * Calculate number of blocks to reserve
1191 */
1192 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001193
Theodore Ts'o3839e651997-04-26 13:21:57 +00001194}
1195
1196int main (int argc, char *argv[])
1197{
1198 errcode_t retval = 0;
1199 ext2_filsys fs;
1200 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001201 int journal_blocks;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001202 int i, val;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001203
1204#ifdef ENABLE_NLS
1205 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001206 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001207 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1208 textdomain(NLS_CAT_NAME);
1209#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001210 PRS(argc, argv);
1211
Theodore Ts'o3839e651997-04-26 13:21:57 +00001212 /*
1213 * Initialize the superblock....
1214 */
1215 retval = ext2fs_initialize(device_name, 0, &param,
1216 unix_io_manager, &fs);
1217 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001218 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001219 exit(1);
1220 }
1221
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001222 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001223 * Wipe out the old on-disk superblock
1224 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001225 if (!noaction)
1226 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001227
1228 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001229 * Generate a UUID for it...
1230 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001231 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001232
1233 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001234 * Add "jitter" to the superblock's check interval so that we
1235 * don't check all the filesystems at the same time. We use a
1236 * kludgy hack of using the UUID to derive a random jitter value.
1237 */
1238 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1239 val += fs->super->s_uuid[i];
1240 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1241
1242 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001243 * Override the creator OS, if applicable
1244 */
1245 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001246 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001247 exit(1);
1248 }
1249
1250 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001251 * For the Hurd, we will turn off filetype since it doesn't
1252 * support it.
1253 */
1254 if (fs->super->s_creator_os == EXT2_OS_HURD)
1255 fs->super->s_feature_incompat &=
1256 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1257
1258 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001259 * Set the volume label...
1260 */
1261 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001262 memset(fs->super->s_volume_name, 0,
1263 sizeof(fs->super->s_volume_name));
1264 strncpy(fs->super->s_volume_name, volume_label,
1265 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001266 }
1267
1268 /*
1269 * Set the last mount directory
1270 */
1271 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001272 memset(fs->super->s_last_mounted, 0,
1273 sizeof(fs->super->s_last_mounted));
1274 strncpy(fs->super->s_last_mounted, mount_dir,
1275 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001276 }
1277
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001278 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001279 show_stats(fs);
1280
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001281 if (noaction)
1282 exit(0);
1283
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001284 if (fs->super->s_feature_incompat &
1285 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1286 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001287 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001288 }
1289
Theodore Ts'o3839e651997-04-26 13:21:57 +00001290 if (bad_blocks_filename)
1291 read_bb_file(fs, &bb_list, bad_blocks_filename);
1292 if (cflag)
1293 test_disk(fs, &bb_list);
1294
1295 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001296 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001297 retval = ext2fs_allocate_tables(fs);
1298 if (retval) {
1299 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001300 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001301 exit(1);
1302 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001303 if (super_only) {
1304 fs->super->s_state |= EXT2_ERROR_FS;
1305 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1306 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001307 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001308 int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001309 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001310 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001311 blk_t ret_blk;
1312
1313#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001314 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001315#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001316
1317 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001318 * Wipe out any old MD RAID (or other) metadata at the end
1319 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001320 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001321 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001322 start = (blocks & ~(rsv - 1));
1323 if (start > rsv)
1324 start -= rsv;
1325 if (start > 0)
1326 retval = zero_blocks(fs, start, blocks - start,
1327 NULL, &ret_blk, NULL);
1328
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001329 if (retval) {
1330 com_err(program_name, retval,
1331 _("zeroing block %u at end of filesystem"),
1332 ret_blk);
1333 exit(1);
1334 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001335 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001336 create_root_dir(fs);
1337 create_lost_and_found(fs);
1338 reserve_inodes(fs);
1339 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001340 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001341
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001342 if (journal_device) {
1343 ext2_filsys jfs;
1344
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001345 if (!force)
1346 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001347 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001348
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001349 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1350 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1351 fs->blocksize, unix_io_manager, &jfs);
1352 if (retval) {
1353 com_err(program_name, retval,
1354 _("while trying to open journal device %s\n"),
1355 journal_device);
1356 exit(1);
1357 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001358 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001359 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001360 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001361 fflush(stdout);
1362 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001363 retval = ext2fs_add_journal_device(fs, jfs);
1364 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001365 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001366 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001367 journal_device);
1368 exit(1);
1369 }
1370 if (!quiet)
1371 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001372 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001373 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001374 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001375 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001376
1377 if (!journal_blocks) {
1378 fs->super->s_feature_compat &=
1379 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1380 goto no_journal;
1381 }
1382 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001383 printf(_("Creating journal (%d blocks): "),
1384 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001385 fflush(stdout);
1386 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001387 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1388 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001389 if (retval) {
1390 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001391 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001392 exit(1);
1393 }
1394 if (!quiet)
1395 printf(_("done\n"));
1396 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001397no_journal:
1398
Theodore Ts'o3839e651997-04-26 13:21:57 +00001399 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001400 printf(_("Writing superblocks and "
1401 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001402 retval = ext2fs_flush(fs);
1403 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001404 fprintf(stderr,
1405 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001406 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001407 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001408 printf(_("done\n\n"));
Theodore Ts'o66cf2f62001-06-14 06:42:44 +00001409 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001410 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001411 val = ext2fs_close(fs);
1412 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001413}