blob: 562549615474cde67ec9ed86ff77d1faca25d2c0 [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
Theodore Ts'o54c637d2001-05-14 11:45:38 +000047#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000048#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000049#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000050#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000051#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000052#include "util.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000053#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000054#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
56#define STRIDE_LENGTH 8
57
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000058#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000059#define ZAP_BOOTBLOCK
60#endif
61
Theodore Ts'o3839e651997-04-26 13:21:57 +000062extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000063extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000064
65const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000066const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000067
68/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000069int cflag;
70int verbose;
71int quiet;
72int super_only;
73int force;
74int noaction;
75int journal_size;
76int journal_flags;
77char *bad_blocks_filename;
78__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079
80struct ext2_super_block param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000081char *creator_os;
82char *volume_label;
83char *mount_dir;
84char *journal_device;
85int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
Theodore Ts'o31e29a12002-05-17 10:53:07 -040087int sys_page_size = 4096;
88
Theodore Ts'o63985322001-01-03 17:02:13 +000089static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000090{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000091 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000092 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000093 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
94 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000095 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +000096 "[-r fs-revision] [-R raid_opts] [-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 program_name);
98 exit(1);
99}
100
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000101static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102{
103 int l = 0;
104
105 arg >>= 1;
106 while (arg) {
107 l++;
108 arg >>= 1;
109 }
110 return l;
111}
112
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000113static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000114{
115 int l;
116
117 for (l=0; arg ; l++)
118 arg = arg / 10;
119 return l;
120}
121
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000123 * This function sets the default parameters for a filesystem
124 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000125 * The type is specified by the user. The size is the maximum size
126 * (in megabytes) for which a set of parameters applies, with a size
127 * of zero meaning that it is the default parameter for the type.
128 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000129 */
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400130#define DEF_MAX_BLOCKSIZE -1
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000131static char default_str[] = "default";
132struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000133 const char *type;
134 int size;
135 int blocksize;
136 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000137} settings[] = {
138 { default_str, 0, 4096, 8192 },
139 { default_str, 512, 1024, 4096 },
140 { default_str, 3, 1024, 8192 },
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000141 { "journal", 0, 4096, 8192 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000142 { "news", 0, 4096, 4096 },
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400143 { "largefile", 0, DEF_MAX_BLOCKSIZE, 1024 * 1024 },
144 { "largefile4", 0, DEF_MAX_BLOCKSIZE, 4096 * 1024 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000145 { 0, 0, 0, 0},
146};
147
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500148static void set_fs_defaults(const char *fs_type,
149 struct ext2_super_block *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000150 int blocksize, int *inode_ratio)
151{
152 int megs;
153 int ratio = 0;
154 struct mke2fs_defaults *p;
155
Andreas Dilger932a4892002-05-16 03:20:07 -0600156 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000157 if (inode_ratio)
158 ratio = *inode_ratio;
159 if (!fs_type)
160 fs_type = default_str;
161 for (p = settings; p->type; p++) {
162 if ((strcmp(p->type, fs_type) != 0) &&
163 (strcmp(p->type, default_str) != 0))
164 continue;
165 if ((p->size != 0) &&
166 (megs > p->size))
167 continue;
168 if (ratio == 0)
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600169 *inode_ratio = p->inode_ratio < blocksize ?
170 blocksize : p->inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000171 if (blocksize == 0) {
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400172 if (p->blocksize == DEF_MAX_BLOCKSIZE)
173 p->blocksize = sys_page_size;
Theodore Ts'o9094f281999-10-26 16:42:50 +0000174 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000175 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000176 }
177 }
178 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000179 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000180}
181
182/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 * Helper function for read_bb_file and test_disk
184 */
185static void invalid_block(ext2_filsys fs, blk_t blk)
186{
Theodore Ts'o66938372002-03-08 00:14:46 -0500187 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000188 return;
189}
190
191/*
192 * Reads the bad blocks list from a file
193 */
194static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
195 const char *bad_blocks_file)
196{
197 FILE *f;
198 errcode_t retval;
199
200 f = fopen(bad_blocks_file, "r");
201 if (!f) {
202 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000203 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 exit(1);
205 }
206 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
207 fclose (f);
208 if (retval) {
209 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000210 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 exit(1);
212 }
213}
214
215/*
216 * Runs the badblocks program to test the disk
217 */
218static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
219{
220 FILE *f;
221 errcode_t retval;
222 char buf[1024];
223
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500224 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
225 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
226 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000227 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000228 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229 f = popen(buf, "r");
230 if (!f) {
231 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000232 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 exit(1);
234 }
235 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000236 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 if (retval) {
238 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000239 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 exit(1);
241 }
242}
243
244static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
245{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000246 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 int must_be_good;
248 blk_t blk;
249 badblocks_iterate bb_iter;
250 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000251 blk_t group_block;
252 int group;
253 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254
255 if (!bb_list)
256 return;
257
258 /*
259 * The primary superblock and group descriptors *must* be
260 * good; if not, abort.
261 */
262 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
263 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000264 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000265 fprintf(stderr, _("Block %d in primary "
266 "superblock/group descriptor area bad.\n"), i);
267 fprintf(stderr, _("Blocks %d through %d must be good "
268 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000270 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000271 exit(1);
272 }
273 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000274
275 /*
276 * See if any of the bad blocks are showing up in the backup
277 * superblocks and/or group descriptors. If so, issue a
278 * warning and adjust the block counts appropriately.
279 */
280 group_block = fs->super->s_first_data_block +
281 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000282
283 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000284 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000285 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000286 if (ext2fs_badblocks_list_test(bb_list,
287 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000288 if (!group_bad)
289 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000290_("Warning: the backup superblock/group descriptors at block %d contain\n"
291" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000292 group_block);
293 group_bad++;
294 group = ext2fs_group_of_blk(fs, group_block+j);
295 fs->group_desc[group].bg_free_blocks_count++;
296 fs->super->s_free_blocks_count++;
297 }
298 }
299 group_block += fs->super->s_blocks_per_group;
300 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301
302 /*
303 * Mark all the bad blocks as used...
304 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000305 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000306 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000307 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000308 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309 exit(1);
310 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000311 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000312 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000313 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314}
315
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000316/*
317 * These functions implement a generalized progress meter.
318 */
319struct progress_struct {
320 char format[20];
321 char backup[80];
322 __u32 max;
323};
324
325static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500326 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000327{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000328 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000329
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000330 memset(progress, 0, sizeof(struct progress_struct));
331 if (quiet)
332 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000333
334 /*
335 * Figure out how many digits we need
336 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000337 i = int_log10(max);
338 sprintf(progress->format, "%%%dd/%%%dld", i, i);
339 memset(progress->backup, '\b', sizeof(progress->backup)-1);
340 progress->backup[sizeof(progress->backup)-1] = 0;
341 if ((2*i)+1 < sizeof(progress->backup))
342 progress->backup[(2*i)+1] = 0;
343 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000344
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000345 fputs(label, stdout);
346 fflush(stdout);
347}
348
349static void progress_update(struct progress_struct *progress, __u32 val)
350{
351 if (progress->format[0] == 0)
352 return;
353 printf(progress->format, val, progress->max);
354 fputs(progress->backup, stdout);
355}
356
357static void progress_close(struct progress_struct *progress)
358{
359 if (progress->format[0] == 0)
360 return;
361 fputs(_("done \n"), stdout);
362}
363
364
365/*
366 * Helper function which zeros out _num_ blocks starting at _blk_. In
367 * case of an error, the details of the error is returned via _ret_blk_
368 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
369 * success, and an error code on an error.
370 *
371 * As a special case, if the first argument is NULL, then it will
372 * attempt to free the static zeroizing buffer. (This is to keep
373 * programs that check for memory leaks happy.)
374 */
375static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
376 struct progress_struct *progress,
377 blk_t *ret_blk, int *ret_count)
378{
379 int j, count, next_update, next_update_incr;
380 static char *buf;
381 errcode_t retval;
382
383 /* If fs is null, clean up the static buffer and return */
384 if (!fs) {
385 if (buf) {
386 free(buf);
387 buf = 0;
388 }
389 return 0;
390 }
391 /* Allocate the zeroizing buffer if necessary */
392 if (!buf) {
393 buf = malloc(fs->blocksize * STRIDE_LENGTH);
394 if (!buf) {
395 com_err("malloc", ENOMEM,
396 _("while allocating zeroizing buffer"));
397 exit(1);
398 }
399 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
400 }
401 /* OK, do the write loop */
402 next_update = 0;
403 next_update_incr = num / 100;
404 if (next_update_incr < 1)
405 next_update_incr = 1;
406 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
407 if (num-j > STRIDE_LENGTH)
408 count = STRIDE_LENGTH;
409 else
410 count = num - j;
411 retval = io_channel_write_blk(fs->io, blk, count, buf);
412 if (retval) {
413 if (ret_count)
414 *ret_count = count;
415 if (ret_blk)
416 *ret_blk = blk;
417 return retval;
418 }
419 if (progress && j > next_update) {
420 next_update += num / 100;
421 progress_update(progress, blk);
422 }
423 }
424 return 0;
425}
426
427static void write_inode_tables(ext2_filsys fs)
428{
429 errcode_t retval;
430 blk_t blk;
431 int i, num;
432 struct progress_struct progress;
433
434 if (quiet)
435 memset(&progress, 0, sizeof(progress));
436 else
437 progress_init(&progress, _("Writing inode tables: "),
438 fs->group_desc_count);
439
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000441 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000443 blk = fs->group_desc[i].bg_inode_table;
444 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000445
446 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
447 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500448 fprintf(stderr, _("\nCould not write %d blocks "
449 "in inode table starting at %d: %s\n"),
450 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000451 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000452 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000453 if (sync_kludge) {
454 if (sync_kludge == 1)
455 sync();
456 else if ((i % sync_kludge) == 0)
457 sync();
458 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000460 zero_blocks(0, 0, 0, 0, 0, 0);
461 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462}
463
464static void create_root_dir(ext2_filsys fs)
465{
466 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000467 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468
469 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
470 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000471 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472 exit(1);
473 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000474 if (geteuid()) {
475 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
476 if (retval) {
477 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000478 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000479 exit(1);
480 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000481 inode.i_uid = getuid();
482 if (inode.i_uid)
483 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000484 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
485 if (retval) {
486 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000487 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000488 exit(1);
489 }
490 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491}
492
493static void create_lost_and_found(ext2_filsys fs)
494{
495 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000496 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497 const char *name = "lost+found";
498 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000499 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000500
Theodore Ts'o6a525062001-12-24 09:40:00 -0500501 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
503 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000504 com_err("ext2fs_mkdir", retval,
505 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506 exit(1);
507 }
508
509 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
510 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000511 com_err("ext2_lookup", retval,
512 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 exit(1);
514 }
515
516 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000517 if ((lpf_size += fs->blocksize) >= 16*1024)
518 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 retval = ext2fs_expand_dir(fs, ino);
520 if (retval) {
521 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000522 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000523 exit(1);
524 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500525 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526}
527
528static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
529{
530 errcode_t retval;
531
Theodore Ts'of3db3561997-04-26 13:34:30 +0000532 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533 fs->group_desc[0].bg_free_inodes_count--;
534 fs->super->s_free_inodes_count--;
535 retval = ext2fs_update_bb_inode(fs, bb_list);
536 if (retval) {
537 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000538 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 exit(1);
540 }
541
542}
543
544static void reserve_inodes(ext2_filsys fs)
545{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000546 ext2_ino_t i;
547 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000549 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000550 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551 group = ext2fs_group_of_ino(fs, i);
552 fs->group_desc[group].bg_free_inodes_count--;
553 fs->super->s_free_inodes_count--;
554 }
555 ext2fs_mark_ib_dirty(fs);
556}
557
Theodore Ts'o756df352002-03-07 20:52:12 -0500558#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500559#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500560#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500561
Theodore Ts'o04a96852001-08-30 21:55:26 -0400562static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000563{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400564 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000565 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500566 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000567
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400568 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600569 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500570 printf(_("Out of memory erasing sectors %d-%d\n"),
571 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600572 exit(1);
573 }
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400574 memset(buf, 0, 512*nsect);
Theodore Ts'o756df352002-03-07 20:52:12 -0500575
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500576 if (sect == 0) {
577 /* Check for a BSD disklabel, and don't erase it if so */
578 retval = io_channel_read_blk(fs->io, 0, -512, buf);
579 if (retval)
580 fprintf(stderr,
581 _("Warning: could not read block 0: %s\n"),
582 error_message(retval));
583 else {
584 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
585 if ((*magic == BSD_DISKMAGIC) ||
586 (*magic == BSD_MAGICDISK))
587 return;
588 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500589 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500590
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000591 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400592 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000593 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400594 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000595 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500596 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
597 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000598}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000599
600static void create_journal_dev(ext2_filsys fs)
601{
602 struct progress_struct progress;
603 errcode_t retval;
604 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000605 blk_t blk;
606 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000607
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000608 retval = ext2fs_create_journal_superblock(fs,
609 fs->super->s_blocks_count, 0, &buf);
610 if (retval) {
611 com_err("create_journal_dev", retval,
612 _("while initializing journal superblock"));
613 exit(1);
614 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000615 if (quiet)
616 memset(&progress, 0, sizeof(progress));
617 else
618 progress_init(&progress, _("Zeroing journal device: "),
619 fs->super->s_blocks_count);
620
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000621 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
622 &progress, &blk, &count);
623 if (retval) {
624 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000625 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000626 blk, count);
627 exit(1);
628 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000629 zero_blocks(0, 0, 0, 0, 0, 0);
630
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000631 retval = io_channel_write_blk(fs->io,
632 fs->super->s_first_data_block+1,
633 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000634 if (retval) {
635 com_err("create_journal_dev", retval,
636 _("while writing journal superblock"));
637 exit(1);
638 }
639 progress_close(&progress);
640}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000641
Theodore Ts'o3839e651997-04-26 13:21:57 +0000642static void show_stats(ext2_filsys fs)
643{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000644 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000645 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000646 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000647 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648
649 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'o66938372002-03-08 00:14:46 -0500650 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000652
653 memset(buf, 0, sizeof(buf));
654 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000655 printf(_("Filesystem label=%s\n"), buf);
656 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000657 switch (fs->super->s_creator_os) {
658 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000659 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000660 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000661 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000662 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000663 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000664 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000665 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000666 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000667 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000668 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000669 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000670 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671 s->s_r_blocks_count,
672 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000673 printf(_("First data block=%u\n"), s->s_first_data_block);
674 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000675 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000676 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000677 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000678 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000679 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000680 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000681
682 if (fs->group_desc_count == 1) {
683 printf("\n");
684 return;
685 }
686
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000687 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688 group_block = s->s_first_data_block;
689 col_left = 0;
690 for (i = 1; i < fs->group_desc_count; i++) {
691 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000692 if (!ext2fs_bg_has_super(fs, i))
693 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000694 if (i != 1)
695 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000696 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000697 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000699 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000701 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000702 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000703 }
704 printf("\n\n");
705}
706
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000707/*
708 * Set the S_CREATOR_OS field. Return true if OS is known,
709 * otherwise, 0.
710 */
711static int set_os(struct ext2_super_block *sb, char *os)
712{
713 if (isdigit (*os))
714 sb->s_creator_os = atoi (os);
715 else if (strcasecmp(os, "linux") == 0)
716 sb->s_creator_os = EXT2_OS_LINUX;
717 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
718 sb->s_creator_os = EXT2_OS_HURD;
719 else if (strcasecmp(os, "masix") == 0)
720 sb->s_creator_os = EXT2_OS_MASIX;
721 else
722 return 0;
723 return 1;
724}
725
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000726#define PATH_SET "PATH=/sbin"
727
Theodore Ts'od163b091997-10-03 17:42:28 +0000728static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000729{
730 char *buf, *token, *next, *p, *arg;
731 int len;
732 int raid_usage = 0;
733
734 len = strlen(opts);
735 buf = malloc(len+1);
736 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000737 fprintf(stderr, _("Couldn't allocate memory to parse "
738 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000739 exit(1);
740 }
741 strcpy(buf, opts);
742 for (token = buf; token && *token; token = next) {
743 p = strchr(token, ',');
744 next = 0;
745 if (p) {
746 *p = 0;
747 next = p+1;
748 }
749 arg = strchr(token, '=');
750 if (arg) {
751 *arg = 0;
752 arg++;
753 }
754 if (strcmp(token, "stride") == 0) {
755 if (!arg) {
756 raid_usage++;
757 continue;
758 }
759 fs_stride = strtoul(arg, &p, 0);
760 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000761 fprintf(stderr,
762 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000763 raid_usage++;
764 continue;
765 }
766 } else
767 raid_usage++;
768 }
769 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000770 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000771 "Raid options are separated by commas, "
772 "and may take an argument which\n"
773 "\tis set off by an equals ('=') sign.\n\n"
774 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000775 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000776 exit(1);
777 }
778}
779
Theodore Ts'o896938d1999-10-23 01:04:50 +0000780static __u32 ok_features[3] = {
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000781 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000782 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
783 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000784 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
785};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000786
787
Theodore Ts'o3839e651997-04-26 13:21:57 +0000788static void PRS(int argc, char *argv[])
789{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000790 int c;
791 int size;
792 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000793 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000794 int blocksize = 0;
795 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600796 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000797 int reserved_ratio = 5;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000798 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000799 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000800 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000801 char * raid_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500802 const char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400803 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000804 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500805#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000806 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000807#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400808 long sysval;
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);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400825
826 /* Determine the system page size if possible */
827#ifdef HAVE_SYSCONF
828#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
829#define _SC_PAGESIZE _SC_PAGE_SIZE
830#endif
831#ifdef _SC_PAGESIZE
832 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600833 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400834 sys_page_size = sysval;
835#endif /* _SC_PAGESIZE */
836#endif /* HAVE_SYSCONF */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000837
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838 setbuf(stdout, NULL);
839 setbuf(stderr, NULL);
840 initialize_ext2_error_table();
841 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000842 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400843 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
844 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
845
Theodore Ts'o756df352002-03-07 20:52:12 -0500846#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000847 if (uname(&ut)) {
848 perror("uname");
849 exit(1);
850 }
851 if ((ut.release[0] == '1') ||
852 (ut.release[0] == '2' && ut.release[1] == '.' &&
853 ut.release[2] < '2' && ut.release[3] == '.')) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000854 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400855 param.s_feature_incompat = 0;
856 param.s_feature_compat = 0;
857 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000858 }
859#endif
Andreas Dilger9d8e6342001-08-31 10:24:46 -0600860 fprintf (stderr, "mke2fs %s (%s)\n",
Theodore Ts'o04a96852001-08-30 21:55:26 -0400861 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700862
863 if (argc && *argv) {
864 program_name = get_progname(*argv);
865
866 /* If called as mkfs.ext3, create a journal inode */
867 if (!strcmp(program_name, "mkfs.ext3"))
868 journal_size = -1;
869 }
870
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000871 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000872 "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 +0000873 switch (c) {
874 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000875 blocksize = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600876 if (blocksize < EXT2_MIN_BLOCK_SIZE ||
877 blocksize > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000878 com_err(program_name, 0,
879 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000880 exit(1);
881 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600882 if (blocksize > 4096)
883 fprintf(stderr, _("Warning: blocksize %d not "
884 "usable on most systems.\n"),
885 blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000886 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000887 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000888 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000889 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000890 case 'c': /* Check for bad blocks */
891 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500892 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000893 break;
894 case 'f':
895 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600896 if (size < EXT2_MIN_BLOCK_SIZE ||
897 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000898 com_err(program_name, 0,
899 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000900 optarg);
901 exit(1);
902 }
903 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000904 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500905 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000906 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000907 break;
908 case 'g':
909 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000910 if (*tmp) {
911 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000912 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000913 exit(1);
914 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000915 if ((param.s_blocks_per_group % 8) != 0) {
916 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000917 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918 exit(1);
919 }
920 break;
921 case 'i':
922 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600923 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
924 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000925 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000926 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -0600927 _("bad inode ratio %s (min %d/max %d"),
928 optarg, EXT2_MIN_BLOCK_SIZE,
929 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000930 exit(1);
931 }
932 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000933 case 'J':
934 parse_journal_opts(optarg);
935 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000936 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000937 param.s_feature_compat |=
938 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000939 if (!journal_size)
940 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000941 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000942 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000943 bad_blocks_filename = malloc(strlen(optarg)+1);
944 if (!bad_blocks_filename) {
945 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000946 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000947 exit(1);
948 }
949 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000950 break;
951 case 'm':
952 reserved_ratio = strtoul(optarg, &tmp, 0);
953 if (reserved_ratio > 50 || *tmp) {
954 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000955 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000956 optarg);
957 exit(1);
958 }
959 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000960 case 'n':
961 noaction++;
962 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000963 case 'o':
964 creator_os = optarg;
965 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000966 case 'r':
967 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000968 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400969 param.s_feature_incompat = 0;
970 param.s_feature_compat = 0;
971 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000972 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000973 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000974 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400975 if (atoi(optarg))
976 param.s_feature_ro_compat |=
977 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
978 else
979 param.s_feature_ro_compat &=
980 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000981 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000982#ifdef EXT2_DYNAMIC_REV
983 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -0600984 inode_size = strtoul(optarg, &tmp, 0);
985 if (*tmp) {
986 com_err(program_name, 0,
987 _("bad inode size - %s"), optarg);
988 exit(1);
989 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000990 break;
991#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000992 case 'N':
993 num_inodes = atoi(optarg);
994 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000995 case 'v':
996 verbose = 1;
997 break;
998 case 'q':
999 quiet = 1;
1000 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001001 case 'F':
1002 force = 1;
1003 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001004 case 'L':
1005 volume_label = optarg;
1006 break;
1007 case 'M':
1008 mount_dir = optarg;
1009 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001010 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001011 if (!strcmp(optarg, "none") || default_features) {
1012 param.s_feature_compat = 0;
1013 param.s_feature_incompat = 0;
1014 param.s_feature_ro_compat = 0;
1015 default_features = 0;
1016 }
1017 if (!strcmp(optarg, "none"))
1018 break;
1019 if (e2p_edit_feature(optarg,
1020 &param.s_feature_compat,
1021 ok_features)) {
1022 fprintf(stderr,
1023 _("Invalid filesystem option set: %s\n"), optarg);
1024 exit(1);
1025 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001026 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001027 case 'R':
1028 raid_opts = optarg;
1029 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001030 case 'S':
1031 super_only = 1;
1032 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001033 case 'T':
1034 fs_type = optarg;
1035 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001036 case 'V':
1037 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001038 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +00001039 error_message(EXT2_ET_BASE));
1040 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041 default:
1042 usage();
1043 }
1044 if (optind == argc)
1045 usage();
1046 device_name = argv[optind];
1047 optind++;
1048 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001049 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1050
1051 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001052 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001053 argv[optind - 1]);
1054 exit(1);
1055 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001056 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001057 }
1058 if (optind < argc)
1059 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001060
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001061 if (raid_opts)
1062 parse_raid_opts(raid_opts);
1063
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001064 /*
1065 * If there's no blocksize specified and there is a journal
1066 * device, use it to figure out the blocksize
1067 */
1068 if (blocksize == 0 && journal_device) {
1069 ext2_filsys jfs;
1070
1071 retval = ext2fs_open(journal_device,
1072 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1073 0, unix_io_manager, &jfs);
1074 if (retval) {
1075 com_err(program_name, retval,
1076 _("while trying to open journal device %s\n"),
1077 journal_device);
1078 exit(1);
1079 }
1080 blocksize = jfs->blocksize;
1081 param.s_log_block_size =
1082 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1083 ext2fs_close(jfs);
1084 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001085
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001086 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001087 if (!force) {
1088 com_err(program_name, 0,
1089 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001090 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001091 proceed_question();
1092 }
1093 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1094 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001095 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001096 }
1097
Andreas Dilgerfa724582001-08-04 01:18:34 -06001098 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1099 if (!fs_type)
1100 fs_type = "journal";
1101 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001102 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1103 param.s_feature_compat = 0;
1104 param.s_feature_ro_compat = 0;
1105 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001106 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1107 (param.s_feature_compat || param.s_feature_ro_compat ||
1108 param.s_feature_incompat))
1109 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1110
Theodore Ts'o74becf31997-04-26 14:37:06 +00001111 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001112 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001113 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001114
Theodore Ts'o3839e651997-04-26 13:21:57 +00001115 param.s_log_frag_size = param.s_log_block_size;
1116
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001117 if (noaction && param.s_blocks_count) {
1118 dev_size = param.s_blocks_count;
1119 retval = 0;
1120 } else
1121 retval = ext2fs_get_device_size(device_name,
1122 EXT2_BLOCK_SIZE(&param),
1123 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001124 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1125 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001126 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001127 exit(1);
1128 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001129 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001130 if (retval == EXT2_ET_UNIMPLEMENTED) {
1131 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001132 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001133 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001134 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001135 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001136 } else {
1137 if (dev_size == 0) {
1138 com_err(program_name, 0,
1139 _("Device size reported to be zero. "
1140 "Invalid partition specified, or\n\t"
1141 "partition table wasn't reread "
1142 "after running fdisk, due to\n\t"
1143 "a modified partition being busy "
1144 "and in use. You may need to reboot\n\t"
1145 "to re-read your partition table.\n"
1146 ));
1147 exit(1);
1148 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001149 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001150 }
1151
Theodore Ts'oa789d841998-03-30 01:20:55 +00001152 } else if (!force && (param.s_blocks_count > dev_size)) {
1153 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001154 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001155 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001156 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001157
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001158 /*
1159 * If the user asked for HAS_JOURNAL, then make sure a journal
1160 * gets created.
1161 */
1162 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1163 !journal_size)
1164 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001165
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001166 set_fs_defaults(fs_type, &param, blocksize, &inode_ratio);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001167
Theodore Ts'o521e3681997-04-29 17:48:10 +00001168 if (param.s_blocks_per_group) {
1169 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001170 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001171 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001172 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001173 exit(1);
1174 }
1175 }
1176
Andreas Dilger932a4892002-05-16 03:20:07 -06001177 if (inode_size) {
1178 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1179 inode_size > EXT2_BLOCK_SIZE(&param) ||
1180 inode_size & (inode_size - 1)) {
1181 com_err(program_name, 0,
1182 _("bad inode size %d (min %d/max %d)"),
1183 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1184 EXT2_BLOCK_SIZE(&param));
1185 exit(1);
1186 }
1187 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1188 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1189 "on most systems\n"),
1190 inode_size);
1191 param.s_inode_size = inode_size;
1192 }
1193
Theodore Ts'o3839e651997-04-26 13:21:57 +00001194 /*
1195 * Calculate number of inodes based on the inode ratio
1196 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001197 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +00001198 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +00001199 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001200
1201 /*
1202 * Calculate number of blocks to reserve
1203 */
1204 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001205
Theodore Ts'o3839e651997-04-26 13:21:57 +00001206}
1207
1208int main (int argc, char *argv[])
1209{
1210 errcode_t retval = 0;
1211 ext2_filsys fs;
1212 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001213 int journal_blocks;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001214 int i, val;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001215
1216#ifdef ENABLE_NLS
1217 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001218 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001219 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1220 textdomain(NLS_CAT_NAME);
1221#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001222 PRS(argc, argv);
1223
Theodore Ts'o3839e651997-04-26 13:21:57 +00001224 /*
1225 * Initialize the superblock....
1226 */
1227 retval = ext2fs_initialize(device_name, 0, &param,
1228 unix_io_manager, &fs);
1229 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001230 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001231 exit(1);
1232 }
1233
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001234 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001235 * Wipe out the old on-disk superblock
1236 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001237 if (!noaction)
1238 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001239
1240 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001241 * Generate a UUID for it...
1242 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001243 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001244
1245 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001246 * Add "jitter" to the superblock's check interval so that we
1247 * don't check all the filesystems at the same time. We use a
1248 * kludgy hack of using the UUID to derive a random jitter value.
1249 */
1250 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1251 val += fs->super->s_uuid[i];
1252 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1253
1254 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001255 * Override the creator OS, if applicable
1256 */
1257 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001258 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001259 exit(1);
1260 }
1261
1262 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001263 * For the Hurd, we will turn off filetype since it doesn't
1264 * support it.
1265 */
1266 if (fs->super->s_creator_os == EXT2_OS_HURD)
1267 fs->super->s_feature_incompat &=
1268 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1269
1270 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001271 * Set the volume label...
1272 */
1273 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001274 memset(fs->super->s_volume_name, 0,
1275 sizeof(fs->super->s_volume_name));
1276 strncpy(fs->super->s_volume_name, volume_label,
1277 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001278 }
1279
1280 /*
1281 * Set the last mount directory
1282 */
1283 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001284 memset(fs->super->s_last_mounted, 0,
1285 sizeof(fs->super->s_last_mounted));
1286 strncpy(fs->super->s_last_mounted, mount_dir,
1287 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001288 }
1289
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001290 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001291 show_stats(fs);
1292
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001293 if (noaction)
1294 exit(0);
1295
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001296 if (fs->super->s_feature_incompat &
1297 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1298 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001299 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001300 }
1301
Theodore Ts'o3839e651997-04-26 13:21:57 +00001302 if (bad_blocks_filename)
1303 read_bb_file(fs, &bb_list, bad_blocks_filename);
1304 if (cflag)
1305 test_disk(fs, &bb_list);
1306
1307 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001308 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001309 retval = ext2fs_allocate_tables(fs);
1310 if (retval) {
1311 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001312 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001313 exit(1);
1314 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001315 if (super_only) {
1316 fs->super->s_state |= EXT2_ERROR_FS;
1317 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1318 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001319 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001320 int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001321 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001322 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001323 blk_t ret_blk;
1324
1325#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001326 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001327#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001328
1329 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001330 * Wipe out any old MD RAID (or other) metadata at the end
1331 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001332 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001333 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001334 start = (blocks & ~(rsv - 1));
1335 if (start > rsv)
1336 start -= rsv;
1337 if (start > 0)
1338 retval = zero_blocks(fs, start, blocks - start,
1339 NULL, &ret_blk, NULL);
1340
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001341 if (retval) {
1342 com_err(program_name, retval,
1343 _("zeroing block %u at end of filesystem"),
1344 ret_blk);
1345 exit(1);
1346 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001347 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001348 create_root_dir(fs);
1349 create_lost_and_found(fs);
1350 reserve_inodes(fs);
1351 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001352 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001353
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001354 if (journal_device) {
1355 ext2_filsys jfs;
1356
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001357 if (!force)
1358 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001359 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001360
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001361 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1362 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1363 fs->blocksize, unix_io_manager, &jfs);
1364 if (retval) {
1365 com_err(program_name, retval,
1366 _("while trying to open journal device %s\n"),
1367 journal_device);
1368 exit(1);
1369 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001370 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001371 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001372 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001373 fflush(stdout);
1374 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001375 retval = ext2fs_add_journal_device(fs, jfs);
1376 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001377 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001378 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001379 journal_device);
1380 exit(1);
1381 }
1382 if (!quiet)
1383 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001384 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001385 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001386 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001387 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001388
1389 if (!journal_blocks) {
1390 fs->super->s_feature_compat &=
1391 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1392 goto no_journal;
1393 }
1394 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001395 printf(_("Creating journal (%d blocks): "),
1396 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001397 fflush(stdout);
1398 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001399 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1400 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001401 if (retval) {
1402 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001403 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001404 exit(1);
1405 }
1406 if (!quiet)
1407 printf(_("done\n"));
1408 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001409no_journal:
1410
Theodore Ts'o3839e651997-04-26 13:21:57 +00001411 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001412 printf(_("Writing superblocks and "
1413 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001414 retval = ext2fs_flush(fs);
1415 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001416 fprintf(stderr,
1417 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001418 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001419 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001420 printf(_("done\n\n"));
Theodore Ts'o66cf2f62001-06-14 06:42:44 +00001421 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001422 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001423 val = ext2fs_close(fs);
1424 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001425}