blob: 1c98c50a6e259c34c761ab6e6b48c28fcdbe1654 [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'o93d5c382003-05-21 17:28:29 -0400150 int blocksize, int sector_size,
151 int *inode_ratio)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000152{
153 int megs;
154 int ratio = 0;
155 struct mke2fs_defaults *p;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400156 int use_bsize = 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000157
Andreas Dilger932a4892002-05-16 03:20:07 -0600158 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000159 if (inode_ratio)
160 ratio = *inode_ratio;
161 if (!fs_type)
162 fs_type = default_str;
163 for (p = settings; p->type; p++) {
164 if ((strcmp(p->type, fs_type) != 0) &&
165 (strcmp(p->type, default_str) != 0))
166 continue;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400167 if ((p->size != 0) && (megs > p->size))
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000168 continue;
169 if (ratio == 0)
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600170 *inode_ratio = p->inode_ratio < blocksize ?
171 blocksize : p->inode_ratio;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400172 use_bsize = p->blocksize;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000173 }
Theodore Ts'o11393002003-07-12 01:58:12 -0400174 if (sector_size && use_bsize < sector_size)
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400175 use_bsize = sector_size;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400176 if (blocksize <= 0) {
177 if (use_bsize == DEF_MAX_BLOCKSIZE)
178 use_bsize = sys_page_size;
179 if ((blocksize < 0) && (use_bsize < (-blocksize)))
180 use_bsize = -blocksize;
181 blocksize = use_bsize;
182 super->s_blocks_count /= blocksize / 1024;
183 }
184 super->s_log_frag_size = super->s_log_block_size =
185 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000186}
187
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400188
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000189/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190 * Helper function for read_bb_file and test_disk
191 */
192static void invalid_block(ext2_filsys fs, blk_t blk)
193{
Theodore Ts'o66938372002-03-08 00:14:46 -0500194 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 return;
196}
197
198/*
199 * Reads the bad blocks list from a file
200 */
201static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
202 const char *bad_blocks_file)
203{
204 FILE *f;
205 errcode_t retval;
206
207 f = fopen(bad_blocks_file, "r");
208 if (!f) {
209 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000210 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 exit(1);
212 }
213 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
214 fclose (f);
215 if (retval) {
216 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000217 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 exit(1);
219 }
220}
221
222/*
223 * Runs the badblocks program to test the disk
224 */
225static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
226{
227 FILE *f;
228 errcode_t retval;
229 char buf[1024];
230
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500231 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
232 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
233 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000234 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000235 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236 f = popen(buf, "r");
237 if (!f) {
238 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000239 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 exit(1);
241 }
242 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000243 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 if (retval) {
245 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000246 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 exit(1);
248 }
249}
250
251static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
252{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000253 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 int must_be_good;
255 blk_t blk;
256 badblocks_iterate bb_iter;
257 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000258 blk_t group_block;
259 int group;
260 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261
262 if (!bb_list)
263 return;
264
265 /*
266 * The primary superblock and group descriptors *must* be
267 * good; if not, abort.
268 */
269 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
270 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000271 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000272 fprintf(stderr, _("Block %d in primary "
273 "superblock/group descriptor area bad.\n"), i);
274 fprintf(stderr, _("Blocks %d through %d must be good "
275 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000277 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278 exit(1);
279 }
280 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000281
282 /*
283 * See if any of the bad blocks are showing up in the backup
284 * superblocks and/or group descriptors. If so, issue a
285 * warning and adjust the block counts appropriately.
286 */
287 group_block = fs->super->s_first_data_block +
288 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289
290 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000291 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000292 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000293 if (ext2fs_badblocks_list_test(bb_list,
294 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000295 if (!group_bad)
296 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000297_("Warning: the backup superblock/group descriptors at block %d contain\n"
298" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000299 group_block);
300 group_bad++;
301 group = ext2fs_group_of_blk(fs, group_block+j);
302 fs->group_desc[group].bg_free_blocks_count++;
303 fs->super->s_free_blocks_count++;
304 }
305 }
306 group_block += fs->super->s_blocks_per_group;
307 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308
309 /*
310 * Mark all the bad blocks as used...
311 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000312 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000314 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000315 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000316 exit(1);
317 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000318 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000319 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000320 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321}
322
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000323/*
324 * These functions implement a generalized progress meter.
325 */
326struct progress_struct {
327 char format[20];
328 char backup[80];
329 __u32 max;
330};
331
332static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500333 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000334{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000335 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000336
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000337 memset(progress, 0, sizeof(struct progress_struct));
338 if (quiet)
339 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000340
341 /*
342 * Figure out how many digits we need
343 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000344 i = int_log10(max);
345 sprintf(progress->format, "%%%dd/%%%dld", i, i);
346 memset(progress->backup, '\b', sizeof(progress->backup)-1);
347 progress->backup[sizeof(progress->backup)-1] = 0;
348 if ((2*i)+1 < sizeof(progress->backup))
349 progress->backup[(2*i)+1] = 0;
350 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000351
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000352 fputs(label, stdout);
353 fflush(stdout);
354}
355
356static void progress_update(struct progress_struct *progress, __u32 val)
357{
358 if (progress->format[0] == 0)
359 return;
360 printf(progress->format, val, progress->max);
361 fputs(progress->backup, stdout);
362}
363
364static void progress_close(struct progress_struct *progress)
365{
366 if (progress->format[0] == 0)
367 return;
368 fputs(_("done \n"), stdout);
369}
370
371
372/*
373 * Helper function which zeros out _num_ blocks starting at _blk_. In
374 * case of an error, the details of the error is returned via _ret_blk_
375 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
376 * success, and an error code on an error.
377 *
378 * As a special case, if the first argument is NULL, then it will
379 * attempt to free the static zeroizing buffer. (This is to keep
380 * programs that check for memory leaks happy.)
381 */
382static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
383 struct progress_struct *progress,
384 blk_t *ret_blk, int *ret_count)
385{
386 int j, count, next_update, next_update_incr;
387 static char *buf;
388 errcode_t retval;
389
390 /* If fs is null, clean up the static buffer and return */
391 if (!fs) {
392 if (buf) {
393 free(buf);
394 buf = 0;
395 }
396 return 0;
397 }
398 /* Allocate the zeroizing buffer if necessary */
399 if (!buf) {
400 buf = malloc(fs->blocksize * STRIDE_LENGTH);
401 if (!buf) {
402 com_err("malloc", ENOMEM,
403 _("while allocating zeroizing buffer"));
404 exit(1);
405 }
406 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
407 }
408 /* OK, do the write loop */
409 next_update = 0;
410 next_update_incr = num / 100;
411 if (next_update_incr < 1)
412 next_update_incr = 1;
413 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400414 count = num - j;
415 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000416 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000417 retval = io_channel_write_blk(fs->io, blk, count, buf);
418 if (retval) {
419 if (ret_count)
420 *ret_count = count;
421 if (ret_blk)
422 *ret_blk = blk;
423 return retval;
424 }
425 if (progress && j > next_update) {
426 next_update += num / 100;
427 progress_update(progress, blk);
428 }
429 }
430 return 0;
431}
432
433static void write_inode_tables(ext2_filsys fs)
434{
435 errcode_t retval;
436 blk_t blk;
437 int i, num;
438 struct progress_struct progress;
439
440 if (quiet)
441 memset(&progress, 0, sizeof(progress));
442 else
443 progress_init(&progress, _("Writing inode tables: "),
444 fs->group_desc_count);
445
Theodore Ts'o3839e651997-04-26 13:21:57 +0000446 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000447 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000449 blk = fs->group_desc[i].bg_inode_table;
450 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000451
452 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
453 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500454 fprintf(stderr, _("\nCould not write %d blocks "
455 "in inode table starting at %d: %s\n"),
456 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000457 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000458 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000459 if (sync_kludge) {
460 if (sync_kludge == 1)
461 sync();
462 else if ((i % sync_kludge) == 0)
463 sync();
464 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000466 zero_blocks(0, 0, 0, 0, 0, 0);
467 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468}
469
470static void create_root_dir(ext2_filsys fs)
471{
472 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000473 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000474
475 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
476 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000477 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 exit(1);
479 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000480 if (geteuid()) {
481 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
482 if (retval) {
483 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000484 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000485 exit(1);
486 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000487 inode.i_uid = getuid();
488 if (inode.i_uid)
489 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000490 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
491 if (retval) {
492 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000493 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000494 exit(1);
495 }
496 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497}
498
499static void create_lost_and_found(ext2_filsys fs)
500{
501 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000502 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503 const char *name = "lost+found";
504 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000505 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506
Theodore Ts'o6a525062001-12-24 09:40:00 -0500507 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
509 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000510 com_err("ext2fs_mkdir", retval,
511 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 exit(1);
513 }
514
515 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
516 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000517 com_err("ext2_lookup", retval,
518 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 exit(1);
520 }
521
522 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000523 if ((lpf_size += fs->blocksize) >= 16*1024)
524 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525 retval = ext2fs_expand_dir(fs, ino);
526 if (retval) {
527 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000528 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529 exit(1);
530 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500531 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532}
533
534static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
535{
536 errcode_t retval;
537
Theodore Ts'of3db3561997-04-26 13:34:30 +0000538 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 fs->group_desc[0].bg_free_inodes_count--;
540 fs->super->s_free_inodes_count--;
541 retval = ext2fs_update_bb_inode(fs, bb_list);
542 if (retval) {
543 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000544 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 exit(1);
546 }
547
548}
549
550static void reserve_inodes(ext2_filsys fs)
551{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000552 ext2_ino_t i;
553 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000554
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000555 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000556 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557 group = ext2fs_group_of_ino(fs, i);
558 fs->group_desc[group].bg_free_inodes_count--;
559 fs->super->s_free_inodes_count--;
560 }
561 ext2fs_mark_ib_dirty(fs);
562}
563
Theodore Ts'o756df352002-03-07 20:52:12 -0500564#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500565#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500566#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500567
Theodore Ts'o04a96852001-08-30 21:55:26 -0400568static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000569{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400570 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000571 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500572 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000573
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400574 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600575 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500576 printf(_("Out of memory erasing sectors %d-%d\n"),
577 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600578 exit(1);
579 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500580
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500581 if (sect == 0) {
582 /* Check for a BSD disklabel, and don't erase it if so */
583 retval = io_channel_read_blk(fs->io, 0, -512, buf);
584 if (retval)
585 fprintf(stderr,
586 _("Warning: could not read block 0: %s\n"),
587 error_message(retval));
588 else {
589 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
590 if ((*magic == BSD_DISKMAGIC) ||
591 (*magic == BSD_MAGICDISK))
592 return;
593 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500594 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500595
Theodore Ts'o70988102002-07-14 08:00:00 -0400596 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000597 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400598 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000599 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400600 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000601 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500602 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
603 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000604}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000605
606static void create_journal_dev(ext2_filsys fs)
607{
608 struct progress_struct progress;
609 errcode_t retval;
610 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000611 blk_t blk;
612 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000613
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000614 retval = ext2fs_create_journal_superblock(fs,
615 fs->super->s_blocks_count, 0, &buf);
616 if (retval) {
617 com_err("create_journal_dev", retval,
618 _("while initializing journal superblock"));
619 exit(1);
620 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000621 if (quiet)
622 memset(&progress, 0, sizeof(progress));
623 else
624 progress_init(&progress, _("Zeroing journal device: "),
625 fs->super->s_blocks_count);
626
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000627 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
628 &progress, &blk, &count);
629 if (retval) {
630 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000631 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000632 blk, count);
633 exit(1);
634 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000635 zero_blocks(0, 0, 0, 0, 0, 0);
636
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000637 retval = io_channel_write_blk(fs->io,
638 fs->super->s_first_data_block+1,
639 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000640 if (retval) {
641 com_err("create_journal_dev", retval,
642 _("while writing journal superblock"));
643 exit(1);
644 }
645 progress_close(&progress);
646}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000647
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648static void show_stats(ext2_filsys fs)
649{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000650 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000651 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000652 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000653 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654
655 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'o66938372002-03-08 00:14:46 -0500656 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000658
659 memset(buf, 0, sizeof(buf));
660 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000661 printf(_("Filesystem label=%s\n"), buf);
662 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000663 switch (fs->super->s_creator_os) {
664 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000665 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000666 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000667 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000668 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000669 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000670 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000671 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000672 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000673 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000674 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000675 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000676 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677 s->s_r_blocks_count,
678 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000679 printf(_("First data block=%u\n"), s->s_first_data_block);
680 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000681 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000682 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000683 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000684 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000686 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000687
688 if (fs->group_desc_count == 1) {
689 printf("\n");
690 return;
691 }
692
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000693 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694 group_block = s->s_first_data_block;
695 col_left = 0;
696 for (i = 1; i < fs->group_desc_count; i++) {
697 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000698 if (!ext2fs_bg_has_super(fs, i))
699 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000700 if (i != 1)
701 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000702 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000703 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000704 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000705 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000706 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000707 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000708 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709 }
710 printf("\n\n");
711}
712
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000713/*
714 * Set the S_CREATOR_OS field. Return true if OS is known,
715 * otherwise, 0.
716 */
717static int set_os(struct ext2_super_block *sb, char *os)
718{
719 if (isdigit (*os))
720 sb->s_creator_os = atoi (os);
721 else if (strcasecmp(os, "linux") == 0)
722 sb->s_creator_os = EXT2_OS_LINUX;
723 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
724 sb->s_creator_os = EXT2_OS_HURD;
725 else if (strcasecmp(os, "masix") == 0)
726 sb->s_creator_os = EXT2_OS_MASIX;
727 else
728 return 0;
729 return 1;
730}
731
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000732#define PATH_SET "PATH=/sbin"
733
Theodore Ts'od163b091997-10-03 17:42:28 +0000734static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000735{
736 char *buf, *token, *next, *p, *arg;
737 int len;
738 int raid_usage = 0;
739
740 len = strlen(opts);
741 buf = malloc(len+1);
742 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000743 fprintf(stderr, _("Couldn't allocate memory to parse "
744 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000745 exit(1);
746 }
747 strcpy(buf, opts);
748 for (token = buf; token && *token; token = next) {
749 p = strchr(token, ',');
750 next = 0;
751 if (p) {
752 *p = 0;
753 next = p+1;
754 }
755 arg = strchr(token, '=');
756 if (arg) {
757 *arg = 0;
758 arg++;
759 }
760 if (strcmp(token, "stride") == 0) {
761 if (!arg) {
762 raid_usage++;
763 continue;
764 }
765 fs_stride = strtoul(arg, &p, 0);
766 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000767 fprintf(stderr,
768 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000769 raid_usage++;
770 continue;
771 }
772 } else
773 raid_usage++;
774 }
775 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000776 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000777 "Raid options are separated by commas, "
778 "and may take an argument which\n"
779 "\tis set off by an equals ('=') sign.\n\n"
780 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000781 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000782 exit(1);
783 }
784}
785
Theodore Ts'o896938d1999-10-23 01:04:50 +0000786static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400787 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
788 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000789 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400790 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
791 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000792 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
793};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000794
795
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796static void PRS(int argc, char *argv[])
797{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400798 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000799 int size;
800 char * tmp;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000801 int blocksize = 0;
802 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600803 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000804 int reserved_ratio = 5;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400805 int sector_size = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000806 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000807 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000808 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000809 char * raid_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500810 const char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400811 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000812 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500813#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000814 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000815#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400816 long sysval;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000817
Theodore Ts'o3839e651997-04-26 13:21:57 +0000818 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000819 if (oldpath) {
820 char *newpath;
821
822 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
823 strcpy (newpath, PATH_SET);
824 strcat (newpath, ":");
825 strcat (newpath, oldpath);
826 putenv (newpath);
827 } else
828 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000829
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000830 tmp = getenv("MKE2FS_SYNC");
831 if (tmp)
832 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400833
834 /* Determine the system page size if possible */
835#ifdef HAVE_SYSCONF
836#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
837#define _SC_PAGESIZE _SC_PAGE_SIZE
838#endif
839#ifdef _SC_PAGESIZE
840 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600841 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400842 sys_page_size = sysval;
843#endif /* _SC_PAGESIZE */
844#endif /* HAVE_SYSCONF */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000845
Theodore Ts'o3839e651997-04-26 13:21:57 +0000846 setbuf(stdout, NULL);
847 setbuf(stderr, NULL);
848 initialize_ext2_error_table();
849 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000850 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400851 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
852 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500853#if 0
Theodore Ts'o843049c2002-09-22 15:37:40 -0400854 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500855#endif
Theodore Ts'o843049c2002-09-22 15:37:40 -0400856
Theodore Ts'o756df352002-03-07 20:52:12 -0500857#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000858 if (uname(&ut)) {
859 perror("uname");
860 exit(1);
861 }
862 if ((ut.release[0] == '1') ||
863 (ut.release[0] == '2' && ut.release[1] == '.' &&
864 ut.release[2] < '2' && ut.release[3] == '.')) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000865 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400866 param.s_feature_incompat = 0;
867 param.s_feature_compat = 0;
868 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000869 }
870#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700871
872 if (argc && *argv) {
873 program_name = get_progname(*argv);
874
875 /* If called as mkfs.ext3, create a journal inode */
876 if (!strcmp(program_name, "mkfs.ext3"))
877 journal_size = -1;
878 }
879
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000880 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000881 "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 +0000882 switch (c) {
883 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400884 blocksize = strtol(optarg, &tmp, 0);
885 b = (blocksize > 0) ? blocksize : -blocksize;
886 if (b < EXT2_MIN_BLOCK_SIZE ||
887 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000888 com_err(program_name, 0,
889 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000890 exit(1);
891 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600892 if (blocksize > 4096)
893 fprintf(stderr, _("Warning: blocksize %d not "
894 "usable on most systems.\n"),
895 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400896 if (blocksize > 0)
897 param.s_log_block_size =
898 int_log2(blocksize >>
899 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000900 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000901 case 'c': /* Check for bad blocks */
902 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500903 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000904 break;
905 case 'f':
906 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600907 if (size < EXT2_MIN_BLOCK_SIZE ||
908 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000909 com_err(program_name, 0,
910 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000911 optarg);
912 exit(1);
913 }
914 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000915 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500916 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000917 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918 break;
919 case 'g':
920 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000921 if (*tmp) {
922 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000923 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000924 exit(1);
925 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000926 if ((param.s_blocks_per_group % 8) != 0) {
927 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000928 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000929 exit(1);
930 }
931 break;
932 case 'i':
933 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600934 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
935 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000936 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000937 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -0600938 _("bad inode ratio %s (min %d/max %d"),
939 optarg, EXT2_MIN_BLOCK_SIZE,
940 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000941 exit(1);
942 }
943 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000944 case 'J':
945 parse_journal_opts(optarg);
946 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000947 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000948 param.s_feature_compat |=
949 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000950 if (!journal_size)
951 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000952 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000953 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000954 bad_blocks_filename = malloc(strlen(optarg)+1);
955 if (!bad_blocks_filename) {
956 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000957 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000958 exit(1);
959 }
960 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000961 break;
962 case 'm':
963 reserved_ratio = strtoul(optarg, &tmp, 0);
964 if (reserved_ratio > 50 || *tmp) {
965 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000966 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000967 optarg);
968 exit(1);
969 }
970 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000971 case 'n':
972 noaction++;
973 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000974 case 'o':
975 creator_os = optarg;
976 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000977 case 'r':
978 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000979 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400980 param.s_feature_incompat = 0;
981 param.s_feature_compat = 0;
982 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000983 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000984 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000985 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400986 if (atoi(optarg))
987 param.s_feature_ro_compat |=
988 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
989 else
990 param.s_feature_ro_compat &=
991 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000992 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000993#ifdef EXT2_DYNAMIC_REV
994 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -0600995 inode_size = strtoul(optarg, &tmp, 0);
996 if (*tmp) {
997 com_err(program_name, 0,
998 _("bad inode size - %s"), optarg);
999 exit(1);
1000 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001001 break;
1002#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001003 case 'N':
1004 num_inodes = atoi(optarg);
1005 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001006 case 'v':
1007 verbose = 1;
1008 break;
1009 case 'q':
1010 quiet = 1;
1011 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001012 case 'F':
1013 force = 1;
1014 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001015 case 'L':
1016 volume_label = optarg;
1017 break;
1018 case 'M':
1019 mount_dir = optarg;
1020 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001021 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001022 if (!strcmp(optarg, "none") || default_features) {
1023 param.s_feature_compat = 0;
1024 param.s_feature_incompat = 0;
1025 param.s_feature_ro_compat = 0;
1026 default_features = 0;
1027 }
1028 if (!strcmp(optarg, "none"))
1029 break;
1030 if (e2p_edit_feature(optarg,
1031 &param.s_feature_compat,
1032 ok_features)) {
1033 fprintf(stderr,
1034 _("Invalid filesystem option set: %s\n"), optarg);
1035 exit(1);
1036 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001037 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001038 case 'R':
1039 raid_opts = optarg;
1040 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001041 case 'S':
1042 super_only = 1;
1043 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001044 case 'T':
1045 fs_type = optarg;
1046 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001047 case 'V':
1048 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001049 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +00001050 error_message(EXT2_ET_BASE));
1051 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001052 default:
1053 usage();
1054 }
1055 if (optind == argc)
1056 usage();
1057 device_name = argv[optind];
1058 optind++;
1059 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001060 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1061
1062 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001063 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001064 argv[optind - 1]);
1065 exit(1);
1066 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001067 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001068 }
1069 if (optind < argc)
1070 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001071
Theodore Ts'o38798572003-04-16 15:29:39 -04001072 if (!quiet)
1073 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1074 E2FSPROGS_DATE);
1075
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001076 if (raid_opts)
1077 parse_raid_opts(raid_opts);
1078
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001079 /*
1080 * If there's no blocksize specified and there is a journal
1081 * device, use it to figure out the blocksize
1082 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001083 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001084 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001085 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001086
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001087#ifdef CONFIG_TESTIO_DEBUG
1088 io_ptr = test_io_manager;
1089 test_io_backing_manager = unix_io_manager;
1090#else
1091 io_ptr = unix_io_manager;
1092#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001093 retval = ext2fs_open(journal_device,
1094 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001095 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001096 if (retval) {
1097 com_err(program_name, retval,
1098 _("while trying to open journal device %s\n"),
1099 journal_device);
1100 exit(1);
1101 }
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001102 if ((blocksize < 0) && (jfs->blocksize < -blocksize)) {
1103 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001104 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001105 "minimum blocksize %d\n"), jfs->blocksize,
1106 -blocksize);
1107 exit(1);
1108 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001109 blocksize = jfs->blocksize;
1110 param.s_log_block_size =
1111 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1112 ext2fs_close(jfs);
1113 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001114
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001115 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001116 if (!force) {
1117 com_err(program_name, 0,
1118 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001119 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001120 proceed_question();
1121 }
1122 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1123 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001124 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001125 }
Theodore Ts'ocf7d5f12003-07-25 17:39:51 -04001126 if ((blocksize > 4096) &&
1127 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1128 fprintf(stderr, "\nWarning: some 2.4 kernels do not support "
1129 "blocksizes greater than 4096 \n\tusing ext3."
1130 " Use -b 4096 if this is an issue for you.\n\n");
Andreas Dilger932a4892002-05-16 03:20:07 -06001131
Andreas Dilgerfa724582001-08-04 01:18:34 -06001132 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1133 if (!fs_type)
1134 fs_type = "journal";
1135 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001136 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1137 param.s_feature_compat = 0;
1138 param.s_feature_ro_compat = 0;
1139 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001140 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1141 (param.s_feature_compat || param.s_feature_ro_compat ||
1142 param.s_feature_incompat))
1143 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1144
Theodore Ts'o74becf31997-04-26 14:37:06 +00001145 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001146 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001147 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001148
Theodore Ts'o3839e651997-04-26 13:21:57 +00001149 param.s_log_frag_size = param.s_log_block_size;
1150
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001151 if (noaction && param.s_blocks_count) {
1152 dev_size = param.s_blocks_count;
1153 retval = 0;
1154 } else
1155 retval = ext2fs_get_device_size(device_name,
1156 EXT2_BLOCK_SIZE(&param),
1157 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001158 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1159 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001160 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001161 exit(1);
1162 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001163 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001164 if (retval == EXT2_ET_UNIMPLEMENTED) {
1165 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001166 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001167 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001168 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001169 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001170 } else {
1171 if (dev_size == 0) {
1172 com_err(program_name, 0,
1173 _("Device size reported to be zero. "
1174 "Invalid partition specified, or\n\t"
1175 "partition table wasn't reread "
1176 "after running fdisk, due to\n\t"
1177 "a modified partition being busy "
1178 "and in use. You may need to reboot\n\t"
1179 "to re-read your partition table.\n"
1180 ));
1181 exit(1);
1182 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001183 param.s_blocks_count = dev_size;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001184 if (sys_page_size > EXT2_BLOCK_SIZE(&param))
1185 param.s_blocks_count &= ~((sys_page_size /
1186 EXT2_BLOCK_SIZE(&param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001187 }
1188
Theodore Ts'oa789d841998-03-30 01:20:55 +00001189 } else if (!force && (param.s_blocks_count > dev_size)) {
1190 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001191 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001192 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001193 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001194
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001195 /*
1196 * If the user asked for HAS_JOURNAL, then make sure a journal
1197 * gets created.
1198 */
1199 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1200 !journal_size)
1201 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001202
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001203 /* Set first meta blockgroup via an environment variable */
1204 /* (this is mostly for debugging purposes) */
1205 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1206 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1207 param.s_first_meta_bg = atoi(tmp);
1208
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001209 /* Get the hardware sector size, if available */
1210 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1211 if (retval) {
1212 com_err(program_name, retval,
1213 _("while trying to determine hardware sector size"));
1214 exit(1);
1215 }
1216
1217 set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001218 blocksize = EXT2_BLOCK_SIZE(&param);
1219
Theodore Ts'o521e3681997-04-29 17:48:10 +00001220 if (param.s_blocks_per_group) {
1221 if (param.s_blocks_per_group < 256 ||
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001222 param.s_blocks_per_group > 8 * blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001223 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001224 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001225 exit(1);
1226 }
1227 }
1228
Andreas Dilger932a4892002-05-16 03:20:07 -06001229 if (inode_size) {
1230 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1231 inode_size > EXT2_BLOCK_SIZE(&param) ||
1232 inode_size & (inode_size - 1)) {
1233 com_err(program_name, 0,
1234 _("bad inode size %d (min %d/max %d)"),
1235 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001236 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001237 exit(1);
1238 }
1239 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1240 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1241 "on most systems\n"),
1242 inode_size);
1243 param.s_inode_size = inode_size;
1244 }
1245
Theodore Ts'o3839e651997-04-26 13:21:57 +00001246 /*
1247 * Calculate number of inodes based on the inode ratio
1248 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001249 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001250 ((__u64) param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001251 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001252
1253 /*
1254 * Calculate number of blocks to reserve
1255 */
1256 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001257
Theodore Ts'o3839e651997-04-26 13:21:57 +00001258}
1259
1260int main (int argc, char *argv[])
1261{
1262 errcode_t retval = 0;
1263 ext2_filsys fs;
1264 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001265 int journal_blocks;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001266 int i, val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001267 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001268
1269#ifdef ENABLE_NLS
1270 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001271 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001272 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1273 textdomain(NLS_CAT_NAME);
1274#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001275 PRS(argc, argv);
1276
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001277#ifdef CONFIG_TESTIO_DEBUG
1278 io_ptr = test_io_manager;
1279 test_io_backing_manager = unix_io_manager;
1280#else
1281 io_ptr = unix_io_manager;
1282#endif
1283
Theodore Ts'o3839e651997-04-26 13:21:57 +00001284 /*
1285 * Initialize the superblock....
1286 */
1287 retval = ext2fs_initialize(device_name, 0, &param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001288 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001289 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001290 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001291 exit(1);
1292 }
1293
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001294 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001295 * Wipe out the old on-disk superblock
1296 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001297 if (!noaction)
1298 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001299
1300 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001301 * Generate a UUID for it...
1302 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001303 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001304
1305 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001306 * Initialize the directory index variables
1307 */
1308 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1309 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1310
1311 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001312 * Add "jitter" to the superblock's check interval so that we
1313 * don't check all the filesystems at the same time. We use a
1314 * kludgy hack of using the UUID to derive a random jitter value.
1315 */
1316 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1317 val += fs->super->s_uuid[i];
1318 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1319
1320 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001321 * Override the creator OS, if applicable
1322 */
1323 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001324 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001325 exit(1);
1326 }
1327
1328 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001329 * For the Hurd, we will turn off filetype since it doesn't
1330 * support it.
1331 */
1332 if (fs->super->s_creator_os == EXT2_OS_HURD)
1333 fs->super->s_feature_incompat &=
1334 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1335
1336 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001337 * Set the volume label...
1338 */
1339 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001340 memset(fs->super->s_volume_name, 0,
1341 sizeof(fs->super->s_volume_name));
1342 strncpy(fs->super->s_volume_name, volume_label,
1343 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001344 }
1345
1346 /*
1347 * Set the last mount directory
1348 */
1349 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001350 memset(fs->super->s_last_mounted, 0,
1351 sizeof(fs->super->s_last_mounted));
1352 strncpy(fs->super->s_last_mounted, mount_dir,
1353 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001354 }
1355
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001356 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001357 show_stats(fs);
1358
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001359 if (noaction)
1360 exit(0);
1361
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001362 if (fs->super->s_feature_incompat &
1363 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1364 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001365 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001366 }
1367
Theodore Ts'o3839e651997-04-26 13:21:57 +00001368 if (bad_blocks_filename)
1369 read_bb_file(fs, &bb_list, bad_blocks_filename);
1370 if (cflag)
1371 test_disk(fs, &bb_list);
1372
1373 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001374 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001375 retval = ext2fs_allocate_tables(fs);
1376 if (retval) {
1377 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001378 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001379 exit(1);
1380 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001381 if (super_only) {
1382 fs->super->s_state |= EXT2_ERROR_FS;
1383 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1384 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001385 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001386 int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001387 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001388 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001389 blk_t ret_blk;
1390
1391#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001392 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001393#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001394
1395 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001396 * Wipe out any old MD RAID (or other) metadata at the end
1397 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001398 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001399 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001400 start = (blocks & ~(rsv - 1));
1401 if (start > rsv)
1402 start -= rsv;
1403 if (start > 0)
1404 retval = zero_blocks(fs, start, blocks - start,
1405 NULL, &ret_blk, NULL);
1406
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001407 if (retval) {
1408 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001409 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001410 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001411 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001412 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001413 create_root_dir(fs);
1414 create_lost_and_found(fs);
1415 reserve_inodes(fs);
1416 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001417 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001418
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001419 if (journal_device) {
1420 ext2_filsys jfs;
1421
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001422 if (!force)
1423 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001424 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001425
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001426 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1427 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1428 fs->blocksize, unix_io_manager, &jfs);
1429 if (retval) {
1430 com_err(program_name, retval,
1431 _("while trying to open journal device %s\n"),
1432 journal_device);
1433 exit(1);
1434 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001435 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001436 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001437 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001438 fflush(stdout);
1439 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001440 retval = ext2fs_add_journal_device(fs, jfs);
1441 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001442 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001443 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001444 journal_device);
1445 exit(1);
1446 }
1447 if (!quiet)
1448 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001449 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001450 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001451 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001452 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001453
1454 if (!journal_blocks) {
1455 fs->super->s_feature_compat &=
1456 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1457 goto no_journal;
1458 }
1459 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001460 printf(_("Creating journal (%d blocks): "),
1461 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001462 fflush(stdout);
1463 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001464 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1465 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001466 if (retval) {
1467 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001468 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001469 exit(1);
1470 }
1471 if (!quiet)
1472 printf(_("done\n"));
1473 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001474no_journal:
1475
Theodore Ts'o3839e651997-04-26 13:21:57 +00001476 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001477 printf(_("Writing superblocks and "
1478 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001479 retval = ext2fs_flush(fs);
1480 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001481 fprintf(stderr,
1482 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001483 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001484 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001485 printf(_("done\n\n"));
Theodore Ts'o66cf2f62001-06-14 06:42:44 +00001486 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001487 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001488 val = ext2fs_close(fs);
1489 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001490}