blob: fd912c9233ce35cfdce0160b5c6fdeb97010cd29 [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'oc5290fa2003-04-11 22:10:50 -0400174 if (blocksize <= 0) {
175 if (use_bsize == DEF_MAX_BLOCKSIZE)
176 use_bsize = sys_page_size;
Theodore Ts'o79120f82003-08-31 20:56:57 -0400177 if (sector_size && use_bsize < sector_size)
178 use_bsize = sector_size;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400179 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 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500192static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193{
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'o54434922003-12-07 01:28:50 -0500253 dgrp_t i;
254 blk_t j;
255 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000256 blk_t blk;
257 badblocks_iterate bb_iter;
258 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000259 blk_t group_block;
260 int group;
261 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000262
263 if (!bb_list)
264 return;
265
266 /*
267 * The primary superblock and group descriptors *must* be
268 * good; if not, abort.
269 */
270 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
271 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000272 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000273 fprintf(stderr, _("Block %d in primary "
274 "superblock/group descriptor area bad.\n"), i);
275 fprintf(stderr, _("Blocks %d through %d must be good "
276 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500278 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 exit(1);
280 }
281 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000282
283 /*
284 * See if any of the bad blocks are showing up in the backup
285 * superblocks and/or group descriptors. If so, issue a
286 * warning and adjust the block counts appropriately.
287 */
288 group_block = fs->super->s_first_data_block +
289 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000290
291 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000292 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000293 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000294 if (ext2fs_badblocks_list_test(bb_list,
295 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000296 if (!group_bad)
297 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000298_("Warning: the backup superblock/group descriptors at block %d contain\n"
299" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000300 group_block);
301 group_bad++;
302 group = ext2fs_group_of_blk(fs, group_block+j);
303 fs->group_desc[group].bg_free_blocks_count++;
304 fs->super->s_free_blocks_count++;
305 }
306 }
307 group_block += fs->super->s_blocks_per_group;
308 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309
310 /*
311 * Mark all the bad blocks as used...
312 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000313 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000315 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000316 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317 exit(1);
318 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000319 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000320 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000321 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000322}
323
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000324/*
325 * These functions implement a generalized progress meter.
326 */
327struct progress_struct {
328 char format[20];
329 char backup[80];
330 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400331 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000332};
333
334static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500335 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000336{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000337 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000338
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000339 memset(progress, 0, sizeof(struct progress_struct));
340 if (quiet)
341 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000342
343 /*
344 * Figure out how many digits we need
345 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000346 i = int_log10(max);
347 sprintf(progress->format, "%%%dd/%%%dld", i, i);
348 memset(progress->backup, '\b', sizeof(progress->backup)-1);
349 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500350 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000351 progress->backup[(2*i)+1] = 0;
352 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000353
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400354 progress->skip_progress = 0;
355 if (getenv("MKE2FS_SKIP_PROGRESS"))
356 progress->skip_progress++;
357
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000358 fputs(label, stdout);
359 fflush(stdout);
360}
361
362static void progress_update(struct progress_struct *progress, __u32 val)
363{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400364 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000365 return;
366 printf(progress->format, val, progress->max);
367 fputs(progress->backup, stdout);
368}
369
370static void progress_close(struct progress_struct *progress)
371{
372 if (progress->format[0] == 0)
373 return;
374 fputs(_("done \n"), stdout);
375}
376
377
378/*
379 * Helper function which zeros out _num_ blocks starting at _blk_. In
380 * case of an error, the details of the error is returned via _ret_blk_
381 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
382 * success, and an error code on an error.
383 *
384 * As a special case, if the first argument is NULL, then it will
385 * attempt to free the static zeroizing buffer. (This is to keep
386 * programs that check for memory leaks happy.)
387 */
388static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
389 struct progress_struct *progress,
390 blk_t *ret_blk, int *ret_count)
391{
392 int j, count, next_update, next_update_incr;
393 static char *buf;
394 errcode_t retval;
395
396 /* If fs is null, clean up the static buffer and return */
397 if (!fs) {
398 if (buf) {
399 free(buf);
400 buf = 0;
401 }
402 return 0;
403 }
404 /* Allocate the zeroizing buffer if necessary */
405 if (!buf) {
406 buf = malloc(fs->blocksize * STRIDE_LENGTH);
407 if (!buf) {
408 com_err("malloc", ENOMEM,
409 _("while allocating zeroizing buffer"));
410 exit(1);
411 }
412 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
413 }
414 /* OK, do the write loop */
415 next_update = 0;
416 next_update_incr = num / 100;
417 if (next_update_incr < 1)
418 next_update_incr = 1;
419 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400420 count = num - j;
421 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000422 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000423 retval = io_channel_write_blk(fs->io, blk, count, buf);
424 if (retval) {
425 if (ret_count)
426 *ret_count = count;
427 if (ret_blk)
428 *ret_blk = blk;
429 return retval;
430 }
431 if (progress && j > next_update) {
432 next_update += num / 100;
433 progress_update(progress, blk);
434 }
435 }
436 return 0;
437}
438
439static void write_inode_tables(ext2_filsys fs)
440{
441 errcode_t retval;
442 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500443 dgrp_t i;
444 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000445 struct progress_struct progress;
446
447 if (quiet)
448 memset(&progress, 0, sizeof(progress));
449 else
450 progress_init(&progress, _("Writing inode tables: "),
451 fs->group_desc_count);
452
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000454 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000456 blk = fs->group_desc[i].bg_inode_table;
457 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000458
459 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
460 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500461 fprintf(stderr, _("\nCould not write %d blocks "
462 "in inode table starting at %d: %s\n"),
463 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000464 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000465 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000466 if (sync_kludge) {
467 if (sync_kludge == 1)
468 sync();
469 else if ((i % sync_kludge) == 0)
470 sync();
471 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000473 zero_blocks(0, 0, 0, 0, 0, 0);
474 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475}
476
477static void create_root_dir(ext2_filsys fs)
478{
479 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000480 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481
482 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
483 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000484 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 exit(1);
486 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000487 if (geteuid()) {
488 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
489 if (retval) {
490 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000491 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000492 exit(1);
493 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000494 inode.i_uid = getuid();
495 if (inode.i_uid)
496 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000497 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
498 if (retval) {
499 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000500 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000501 exit(1);
502 }
503 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504}
505
506static void create_lost_and_found(ext2_filsys fs)
507{
508 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000509 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 const char *name = "lost+found";
511 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000512 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513
Theodore Ts'o6a525062001-12-24 09:40:00 -0500514 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
516 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000517 com_err("ext2fs_mkdir", retval,
518 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 exit(1);
520 }
521
522 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
523 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000524 com_err("ext2_lookup", retval,
525 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 exit(1);
527 }
528
529 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000530 if ((lpf_size += fs->blocksize) >= 16*1024)
531 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 retval = ext2fs_expand_dir(fs, ino);
533 if (retval) {
534 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000535 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 exit(1);
537 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500538 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539}
540
541static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
542{
543 errcode_t retval;
544
Theodore Ts'of3db3561997-04-26 13:34:30 +0000545 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 fs->group_desc[0].bg_free_inodes_count--;
547 fs->super->s_free_inodes_count--;
548 retval = ext2fs_update_bb_inode(fs, bb_list);
549 if (retval) {
550 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000551 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 exit(1);
553 }
554
555}
556
557static void reserve_inodes(ext2_filsys fs)
558{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000559 ext2_ino_t i;
560 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000562 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000563 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 group = ext2fs_group_of_ino(fs, i);
565 fs->group_desc[group].bg_free_inodes_count--;
566 fs->super->s_free_inodes_count--;
567 }
568 ext2fs_mark_ib_dirty(fs);
569}
570
Theodore Ts'o756df352002-03-07 20:52:12 -0500571#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500572#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500573#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500574
Theodore Ts'o04a96852001-08-30 21:55:26 -0400575static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000576{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400577 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000578 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500579 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000580
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400581 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600582 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500583 printf(_("Out of memory erasing sectors %d-%d\n"),
584 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600585 exit(1);
586 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500587
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500588 if (sect == 0) {
589 /* Check for a BSD disklabel, and don't erase it if so */
590 retval = io_channel_read_blk(fs->io, 0, -512, buf);
591 if (retval)
592 fprintf(stderr,
593 _("Warning: could not read block 0: %s\n"),
594 error_message(retval));
595 else {
596 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
597 if ((*magic == BSD_DISKMAGIC) ||
598 (*magic == BSD_MAGICDISK))
599 return;
600 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500601 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500602
Theodore Ts'o70988102002-07-14 08:00:00 -0400603 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000604 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400605 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000606 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400607 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000608 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500609 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
610 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000611}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000612
613static void create_journal_dev(ext2_filsys fs)
614{
615 struct progress_struct progress;
616 errcode_t retval;
617 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000618 blk_t blk;
619 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000620
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000621 retval = ext2fs_create_journal_superblock(fs,
622 fs->super->s_blocks_count, 0, &buf);
623 if (retval) {
624 com_err("create_journal_dev", retval,
625 _("while initializing journal superblock"));
626 exit(1);
627 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000628 if (quiet)
629 memset(&progress, 0, sizeof(progress));
630 else
631 progress_init(&progress, _("Zeroing journal device: "),
632 fs->super->s_blocks_count);
633
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000634 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
635 &progress, &blk, &count);
636 if (retval) {
637 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000638 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000639 blk, count);
640 exit(1);
641 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000642 zero_blocks(0, 0, 0, 0, 0, 0);
643
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000644 retval = io_channel_write_blk(fs->io,
645 fs->super->s_first_data_block+1,
646 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000647 if (retval) {
648 com_err("create_journal_dev", retval,
649 _("while writing journal superblock"));
650 exit(1);
651 }
652 progress_close(&progress);
653}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000654
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655static void show_stats(ext2_filsys fs)
656{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000657 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000658 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000659 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500660 dgrp_t i;
661 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000662
663 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'o66938372002-03-08 00:14:46 -0500664 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000665 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000666
667 memset(buf, 0, sizeof(buf));
668 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000669 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500670 fputs(_("OS type: "), stdout);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000671 switch (fs->super->s_creator_os) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500672 case EXT2_OS_LINUX: fputs("Linux", stdout); break;
673 case EXT2_OS_HURD: fputs("GNU/Hurd", stdout); break;
674 case EXT2_OS_MASIX: fputs ("Masix", stdout); break;
675 default: fputs(_("(unknown os)"), stdout);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000676 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000677 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000678 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000679 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000680 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000681 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000682 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000684 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685 s->s_r_blocks_count,
686 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000687 printf(_("First data block=%u\n"), s->s_first_data_block);
688 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000689 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000690 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000691 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000692 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000693 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000694 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695
696 if (fs->group_desc_count == 1) {
697 printf("\n");
698 return;
699 }
700
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000701 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000702 group_block = s->s_first_data_block;
703 col_left = 0;
704 for (i = 1; i < fs->group_desc_count; i++) {
705 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000706 if (!ext2fs_bg_has_super(fs, i))
707 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000708 if (i != 1)
709 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000710 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000711 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000713 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000715 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000716 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717 }
718 printf("\n\n");
719}
720
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000721/*
722 * Set the S_CREATOR_OS field. Return true if OS is known,
723 * otherwise, 0.
724 */
725static int set_os(struct ext2_super_block *sb, char *os)
726{
727 if (isdigit (*os))
728 sb->s_creator_os = atoi (os);
729 else if (strcasecmp(os, "linux") == 0)
730 sb->s_creator_os = EXT2_OS_LINUX;
731 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
732 sb->s_creator_os = EXT2_OS_HURD;
733 else if (strcasecmp(os, "masix") == 0)
734 sb->s_creator_os = EXT2_OS_MASIX;
735 else
736 return 0;
737 return 1;
738}
739
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000740#define PATH_SET "PATH=/sbin"
741
Theodore Ts'od163b091997-10-03 17:42:28 +0000742static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000743{
744 char *buf, *token, *next, *p, *arg;
745 int len;
746 int raid_usage = 0;
747
748 len = strlen(opts);
749 buf = malloc(len+1);
750 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000751 fprintf(stderr, _("Couldn't allocate memory to parse "
752 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000753 exit(1);
754 }
755 strcpy(buf, opts);
756 for (token = buf; token && *token; token = next) {
757 p = strchr(token, ',');
758 next = 0;
759 if (p) {
760 *p = 0;
761 next = p+1;
762 }
763 arg = strchr(token, '=');
764 if (arg) {
765 *arg = 0;
766 arg++;
767 }
768 if (strcmp(token, "stride") == 0) {
769 if (!arg) {
770 raid_usage++;
771 continue;
772 }
773 fs_stride = strtoul(arg, &p, 0);
774 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000775 fprintf(stderr,
776 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000777 raid_usage++;
778 continue;
779 }
780 } else
781 raid_usage++;
782 }
783 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000784 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000785 "Raid options are separated by commas, "
786 "and may take an argument which\n"
787 "\tis set off by an equals ('=') sign.\n\n"
788 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000789 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000790 exit(1);
791 }
792}
793
Theodore Ts'o896938d1999-10-23 01:04:50 +0000794static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400795 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
796 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000797 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400798 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
799 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000800 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
801};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000802
803
Theodore Ts'o3839e651997-04-26 13:21:57 +0000804static void PRS(int argc, char *argv[])
805{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400806 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000807 int size;
808 char * tmp;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000809 int blocksize = 0;
810 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600811 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000812 int reserved_ratio = 5;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400813 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500814 int show_version_only = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000815 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000816 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000817 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000818 char * raid_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500819 const char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400820 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000821 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500822#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000823 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000824#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400825 long sysval;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000826
Theodore Ts'o3839e651997-04-26 13:21:57 +0000827 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000828 if (oldpath) {
829 char *newpath;
830
831 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
832 strcpy (newpath, PATH_SET);
833 strcat (newpath, ":");
834 strcat (newpath, oldpath);
835 putenv (newpath);
836 } else
837 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000839 tmp = getenv("MKE2FS_SYNC");
840 if (tmp)
841 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400842
843 /* Determine the system page size if possible */
844#ifdef HAVE_SYSCONF
845#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
846#define _SC_PAGESIZE _SC_PAGE_SIZE
847#endif
848#ifdef _SC_PAGESIZE
849 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600850 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400851 sys_page_size = sysval;
852#endif /* _SC_PAGESIZE */
853#endif /* HAVE_SYSCONF */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000854
Theodore Ts'o3839e651997-04-26 13:21:57 +0000855 setbuf(stdout, NULL);
856 setbuf(stderr, NULL);
857 initialize_ext2_error_table();
858 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000859 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400860 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
861 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500862#if 0
Theodore Ts'o843049c2002-09-22 15:37:40 -0400863 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500864#endif
Theodore Ts'o843049c2002-09-22 15:37:40 -0400865
Theodore Ts'o756df352002-03-07 20:52:12 -0500866#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000867 if (uname(&ut)) {
868 perror("uname");
869 exit(1);
870 }
871 if ((ut.release[0] == '1') ||
872 (ut.release[0] == '2' && ut.release[1] == '.' &&
873 ut.release[2] < '2' && ut.release[3] == '.')) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000874 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400875 param.s_feature_incompat = 0;
876 param.s_feature_compat = 0;
877 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000878 }
879#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700880
881 if (argc && *argv) {
882 program_name = get_progname(*argv);
883
884 /* If called as mkfs.ext3, create a journal inode */
885 if (!strcmp(program_name, "mkfs.ext3"))
886 journal_size = -1;
887 }
888
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000889 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000890 "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 +0000891 switch (c) {
892 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400893 blocksize = strtol(optarg, &tmp, 0);
894 b = (blocksize > 0) ? blocksize : -blocksize;
895 if (b < EXT2_MIN_BLOCK_SIZE ||
896 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000897 com_err(program_name, 0,
898 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000899 exit(1);
900 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600901 if (blocksize > 4096)
902 fprintf(stderr, _("Warning: blocksize %d not "
903 "usable on most systems.\n"),
904 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400905 if (blocksize > 0)
906 param.s_log_block_size =
907 int_log2(blocksize >>
908 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000909 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000910 case 'c': /* Check for bad blocks */
911 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500912 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000913 break;
914 case 'f':
915 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600916 if (size < EXT2_MIN_BLOCK_SIZE ||
917 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000918 com_err(program_name, 0,
919 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000920 optarg);
921 exit(1);
922 }
923 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000924 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500925 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000926 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927 break;
928 case 'g':
929 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000930 if (*tmp) {
931 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000932 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000933 exit(1);
934 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000935 if ((param.s_blocks_per_group % 8) != 0) {
936 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000937 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000938 exit(1);
939 }
940 break;
941 case 'i':
942 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600943 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
944 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000945 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000946 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -0600947 _("bad inode ratio %s (min %d/max %d"),
948 optarg, EXT2_MIN_BLOCK_SIZE,
949 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000950 exit(1);
951 }
952 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000953 case 'J':
954 parse_journal_opts(optarg);
955 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000956 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000957 param.s_feature_compat |=
958 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000959 if (!journal_size)
960 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000961 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000962 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000963 bad_blocks_filename = malloc(strlen(optarg)+1);
964 if (!bad_blocks_filename) {
965 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000966 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000967 exit(1);
968 }
969 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000970 break;
971 case 'm':
972 reserved_ratio = strtoul(optarg, &tmp, 0);
973 if (reserved_ratio > 50 || *tmp) {
974 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000975 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000976 optarg);
977 exit(1);
978 }
979 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000980 case 'n':
981 noaction++;
982 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000983 case 'o':
984 creator_os = optarg;
985 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000986 case 'r':
987 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000988 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400989 param.s_feature_incompat = 0;
990 param.s_feature_compat = 0;
991 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000992 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000993 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000994 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400995 if (atoi(optarg))
996 param.s_feature_ro_compat |=
997 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
998 else
999 param.s_feature_ro_compat &=
1000 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001001 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001002#ifdef EXT2_DYNAMIC_REV
1003 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001004 inode_size = strtoul(optarg, &tmp, 0);
1005 if (*tmp) {
1006 com_err(program_name, 0,
1007 _("bad inode size - %s"), optarg);
1008 exit(1);
1009 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001010 break;
1011#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001012 case 'N':
1013 num_inodes = atoi(optarg);
1014 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001015 case 'v':
1016 verbose = 1;
1017 break;
1018 case 'q':
1019 quiet = 1;
1020 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001021 case 'F':
1022 force = 1;
1023 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001024 case 'L':
1025 volume_label = optarg;
1026 break;
1027 case 'M':
1028 mount_dir = optarg;
1029 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001030 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001031 if (!strcmp(optarg, "none") || default_features) {
1032 param.s_feature_compat = 0;
1033 param.s_feature_incompat = 0;
1034 param.s_feature_ro_compat = 0;
1035 default_features = 0;
1036 }
1037 if (!strcmp(optarg, "none"))
1038 break;
1039 if (e2p_edit_feature(optarg,
1040 &param.s_feature_compat,
1041 ok_features)) {
1042 fprintf(stderr,
1043 _("Invalid filesystem option set: %s\n"), optarg);
1044 exit(1);
1045 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001046 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001047 case 'R':
1048 raid_opts = optarg;
1049 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001050 case 'S':
1051 super_only = 1;
1052 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001053 case 'T':
1054 fs_type = optarg;
1055 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001056 case 'V':
1057 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001058 show_version_only++;
1059 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001060 default:
1061 usage();
1062 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001063 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001064 usage();
1065 device_name = argv[optind];
1066 optind++;
1067 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001068 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1069
1070 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001071 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001072 argv[optind - 1]);
1073 exit(1);
1074 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001075 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001076 }
1077 if (optind < argc)
1078 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001079
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001080 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001081 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1082 E2FSPROGS_DATE);
1083
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001084 if (show_version_only) {
1085 fprintf(stderr, _("\tUsing %s\n"),
1086 error_message(EXT2_ET_BASE));
1087 exit(0);
1088 }
1089
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001090 if (raid_opts)
1091 parse_raid_opts(raid_opts);
1092
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001093 /*
1094 * If there's no blocksize specified and there is a journal
1095 * device, use it to figure out the blocksize
1096 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001097 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001098 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001099 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001100
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001101#ifdef CONFIG_TESTIO_DEBUG
1102 io_ptr = test_io_manager;
1103 test_io_backing_manager = unix_io_manager;
1104#else
1105 io_ptr = unix_io_manager;
1106#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001107 retval = ext2fs_open(journal_device,
1108 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001109 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001110 if (retval) {
1111 com_err(program_name, retval,
1112 _("while trying to open journal device %s\n"),
1113 journal_device);
1114 exit(1);
1115 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001116 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001117 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001118 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001119 "minimum blocksize %d\n"), jfs->blocksize,
1120 -blocksize);
1121 exit(1);
1122 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001123 blocksize = jfs->blocksize;
1124 param.s_log_block_size =
1125 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1126 ext2fs_close(jfs);
1127 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001128
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001129 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001130 if (!force) {
1131 com_err(program_name, 0,
1132 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001133 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001134 proceed_question();
1135 }
1136 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1137 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001138 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001139 }
Theodore Ts'ocf7d5f12003-07-25 17:39:51 -04001140 if ((blocksize > 4096) &&
1141 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1142 fprintf(stderr, "\nWarning: some 2.4 kernels do not support "
1143 "blocksizes greater than 4096 \n\tusing ext3."
1144 " Use -b 4096 if this is an issue for you.\n\n");
Andreas Dilger932a4892002-05-16 03:20:07 -06001145
Andreas Dilgerfa724582001-08-04 01:18:34 -06001146 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1147 if (!fs_type)
1148 fs_type = "journal";
1149 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001150 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1151 param.s_feature_compat = 0;
1152 param.s_feature_ro_compat = 0;
1153 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001154 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1155 (param.s_feature_compat || param.s_feature_ro_compat ||
1156 param.s_feature_incompat))
1157 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1158
Theodore Ts'o74becf31997-04-26 14:37:06 +00001159 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001160 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001161 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001162
Theodore Ts'o3839e651997-04-26 13:21:57 +00001163 param.s_log_frag_size = param.s_log_block_size;
1164
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001165 if (noaction && param.s_blocks_count) {
1166 dev_size = param.s_blocks_count;
1167 retval = 0;
1168 } else
1169 retval = ext2fs_get_device_size(device_name,
1170 EXT2_BLOCK_SIZE(&param),
1171 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001172 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1173 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001174 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001175 exit(1);
1176 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001177 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001178 if (retval == EXT2_ET_UNIMPLEMENTED) {
1179 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001180 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001181 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001182 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001183 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001184 } else {
1185 if (dev_size == 0) {
1186 com_err(program_name, 0,
1187 _("Device size reported to be zero. "
1188 "Invalid partition specified, or\n\t"
1189 "partition table wasn't reread "
1190 "after running fdisk, due to\n\t"
1191 "a modified partition being busy "
1192 "and in use. You may need to reboot\n\t"
1193 "to re-read your partition table.\n"
1194 ));
1195 exit(1);
1196 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001197 param.s_blocks_count = dev_size;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001198 if (sys_page_size > EXT2_BLOCK_SIZE(&param))
1199 param.s_blocks_count &= ~((sys_page_size /
1200 EXT2_BLOCK_SIZE(&param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001201 }
1202
Theodore Ts'oa789d841998-03-30 01:20:55 +00001203 } else if (!force && (param.s_blocks_count > dev_size)) {
1204 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001205 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001206 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001207 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001208
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001209 /*
1210 * If the user asked for HAS_JOURNAL, then make sure a journal
1211 * gets created.
1212 */
1213 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1214 !journal_size)
1215 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001216
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001217 /* Set first meta blockgroup via an environment variable */
1218 /* (this is mostly for debugging purposes) */
1219 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1220 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1221 param.s_first_meta_bg = atoi(tmp);
1222
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001223 /* Get the hardware sector size, if available */
1224 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1225 if (retval) {
1226 com_err(program_name, retval,
1227 _("while trying to determine hardware sector size"));
1228 exit(1);
1229 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001230
Theodore Ts'o54434922003-12-07 01:28:50 -05001231 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001232 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001233
1234 set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001235 blocksize = EXT2_BLOCK_SIZE(&param);
1236
Theodore Ts'o521e3681997-04-29 17:48:10 +00001237 if (param.s_blocks_per_group) {
1238 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o54434922003-12-07 01:28:50 -05001239 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001240 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001241 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001242 exit(1);
1243 }
1244 }
1245
Andreas Dilger932a4892002-05-16 03:20:07 -06001246 if (inode_size) {
1247 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1248 inode_size > EXT2_BLOCK_SIZE(&param) ||
1249 inode_size & (inode_size - 1)) {
1250 com_err(program_name, 0,
1251 _("bad inode size %d (min %d/max %d)"),
1252 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001253 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001254 exit(1);
1255 }
1256 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1257 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1258 "on most systems\n"),
1259 inode_size);
1260 param.s_inode_size = inode_size;
1261 }
1262
Theodore Ts'o3839e651997-04-26 13:21:57 +00001263 /*
1264 * Calculate number of inodes based on the inode ratio
1265 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001266 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001267 ((__u64) param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001268 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001269
1270 /*
1271 * Calculate number of blocks to reserve
1272 */
1273 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001274
Theodore Ts'o3839e651997-04-26 13:21:57 +00001275}
1276
1277int main (int argc, char *argv[])
1278{
1279 errcode_t retval = 0;
1280 ext2_filsys fs;
1281 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001282 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001283 unsigned int i;
1284 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001285 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001286
1287#ifdef ENABLE_NLS
1288 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001289 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001290 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1291 textdomain(NLS_CAT_NAME);
1292#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001293 PRS(argc, argv);
1294
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001295#ifdef CONFIG_TESTIO_DEBUG
1296 io_ptr = test_io_manager;
1297 test_io_backing_manager = unix_io_manager;
1298#else
1299 io_ptr = unix_io_manager;
1300#endif
1301
Theodore Ts'o3839e651997-04-26 13:21:57 +00001302 /*
1303 * Initialize the superblock....
1304 */
1305 retval = ext2fs_initialize(device_name, 0, &param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001306 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001307 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001308 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001309 exit(1);
1310 }
1311
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001312 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001313 * Wipe out the old on-disk superblock
1314 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001315 if (!noaction)
1316 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001317
1318 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001319 * Generate a UUID for it...
1320 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001321 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001322
1323 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001324 * Initialize the directory index variables
1325 */
1326 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1327 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1328
1329 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001330 * Add "jitter" to the superblock's check interval so that we
1331 * don't check all the filesystems at the same time. We use a
1332 * kludgy hack of using the UUID to derive a random jitter value.
1333 */
1334 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1335 val += fs->super->s_uuid[i];
1336 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1337
1338 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001339 * Override the creator OS, if applicable
1340 */
1341 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001342 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001343 exit(1);
1344 }
1345
1346 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001347 * For the Hurd, we will turn off filetype since it doesn't
1348 * support it.
1349 */
1350 if (fs->super->s_creator_os == EXT2_OS_HURD)
1351 fs->super->s_feature_incompat &=
1352 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1353
1354 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001355 * Set the volume label...
1356 */
1357 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001358 memset(fs->super->s_volume_name, 0,
1359 sizeof(fs->super->s_volume_name));
1360 strncpy(fs->super->s_volume_name, volume_label,
1361 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001362 }
1363
1364 /*
1365 * Set the last mount directory
1366 */
1367 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001368 memset(fs->super->s_last_mounted, 0,
1369 sizeof(fs->super->s_last_mounted));
1370 strncpy(fs->super->s_last_mounted, mount_dir,
1371 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001372 }
1373
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001374 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001375 show_stats(fs);
1376
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001377 if (noaction)
1378 exit(0);
1379
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001380 if (fs->super->s_feature_incompat &
1381 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1382 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001383 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001384 }
1385
Theodore Ts'o3839e651997-04-26 13:21:57 +00001386 if (bad_blocks_filename)
1387 read_bb_file(fs, &bb_list, bad_blocks_filename);
1388 if (cflag)
1389 test_disk(fs, &bb_list);
1390
1391 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001392 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001393 retval = ext2fs_allocate_tables(fs);
1394 if (retval) {
1395 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001396 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001397 exit(1);
1398 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001399 if (super_only) {
1400 fs->super->s_state |= EXT2_ERROR_FS;
1401 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1402 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001403 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001404 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001405 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001406 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001407 blk_t ret_blk;
1408
1409#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001410 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001411#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001412
1413 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001414 * Wipe out any old MD RAID (or other) metadata at the end
1415 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001416 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001417 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001418 start = (blocks & ~(rsv - 1));
1419 if (start > rsv)
1420 start -= rsv;
1421 if (start > 0)
1422 retval = zero_blocks(fs, start, blocks - start,
1423 NULL, &ret_blk, NULL);
1424
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001425 if (retval) {
1426 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001427 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001428 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001429 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001430 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001431 create_root_dir(fs);
1432 create_lost_and_found(fs);
1433 reserve_inodes(fs);
1434 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001435 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001436
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001437 if (journal_device) {
1438 ext2_filsys jfs;
1439
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001440 if (!force)
1441 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001442 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001443
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001444 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1445 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1446 fs->blocksize, unix_io_manager, &jfs);
1447 if (retval) {
1448 com_err(program_name, retval,
1449 _("while trying to open journal device %s\n"),
1450 journal_device);
1451 exit(1);
1452 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001453 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001454 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001455 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001456 fflush(stdout);
1457 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001458 retval = ext2fs_add_journal_device(fs, jfs);
1459 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001460 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001461 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001462 journal_device);
1463 exit(1);
1464 }
1465 if (!quiet)
1466 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001467 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001468 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001469 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001470 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001471
1472 if (!journal_blocks) {
1473 fs->super->s_feature_compat &=
1474 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1475 goto no_journal;
1476 }
1477 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001478 printf(_("Creating journal (%d blocks): "),
1479 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001480 fflush(stdout);
1481 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001482 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1483 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001484 if (retval) {
1485 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001486 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001487 exit(1);
1488 }
1489 if (!quiet)
1490 printf(_("done\n"));
1491 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001492no_journal:
1493
Theodore Ts'o3839e651997-04-26 13:21:57 +00001494 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001495 printf(_("Writing superblocks and "
1496 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001497 retval = ext2fs_flush(fs);
1498 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001499 fprintf(stderr,
1500 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001501 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001502 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001503 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001504 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1505 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001506 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001507 val = ext2fs_close(fs);
1508 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001509}