blob: 96f7b71a93d9009a1bf6376c6bfb5fdb8492368e [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 */
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;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400330 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000331};
332
333static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500334 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000335{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000336 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000337
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000338 memset(progress, 0, sizeof(struct progress_struct));
339 if (quiet)
340 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000341
342 /*
343 * Figure out how many digits we need
344 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000345 i = int_log10(max);
346 sprintf(progress->format, "%%%dd/%%%dld", i, i);
347 memset(progress->backup, '\b', sizeof(progress->backup)-1);
348 progress->backup[sizeof(progress->backup)-1] = 0;
349 if ((2*i)+1 < sizeof(progress->backup))
350 progress->backup[(2*i)+1] = 0;
351 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000352
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400353 progress->skip_progress = 0;
354 if (getenv("MKE2FS_SKIP_PROGRESS"))
355 progress->skip_progress++;
356
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000357 fputs(label, stdout);
358 fflush(stdout);
359}
360
361static void progress_update(struct progress_struct *progress, __u32 val)
362{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400363 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000364 return;
365 printf(progress->format, val, progress->max);
366 fputs(progress->backup, stdout);
367}
368
369static void progress_close(struct progress_struct *progress)
370{
371 if (progress->format[0] == 0)
372 return;
373 fputs(_("done \n"), stdout);
374}
375
376
377/*
378 * Helper function which zeros out _num_ blocks starting at _blk_. In
379 * case of an error, the details of the error is returned via _ret_blk_
380 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
381 * success, and an error code on an error.
382 *
383 * As a special case, if the first argument is NULL, then it will
384 * attempt to free the static zeroizing buffer. (This is to keep
385 * programs that check for memory leaks happy.)
386 */
387static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
388 struct progress_struct *progress,
389 blk_t *ret_blk, int *ret_count)
390{
391 int j, count, next_update, next_update_incr;
392 static char *buf;
393 errcode_t retval;
394
395 /* If fs is null, clean up the static buffer and return */
396 if (!fs) {
397 if (buf) {
398 free(buf);
399 buf = 0;
400 }
401 return 0;
402 }
403 /* Allocate the zeroizing buffer if necessary */
404 if (!buf) {
405 buf = malloc(fs->blocksize * STRIDE_LENGTH);
406 if (!buf) {
407 com_err("malloc", ENOMEM,
408 _("while allocating zeroizing buffer"));
409 exit(1);
410 }
411 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
412 }
413 /* OK, do the write loop */
414 next_update = 0;
415 next_update_incr = num / 100;
416 if (next_update_incr < 1)
417 next_update_incr = 1;
418 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400419 count = num - j;
420 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000421 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000422 retval = io_channel_write_blk(fs->io, blk, count, buf);
423 if (retval) {
424 if (ret_count)
425 *ret_count = count;
426 if (ret_blk)
427 *ret_blk = blk;
428 return retval;
429 }
430 if (progress && j > next_update) {
431 next_update += num / 100;
432 progress_update(progress, blk);
433 }
434 }
435 return 0;
436}
437
438static void write_inode_tables(ext2_filsys fs)
439{
440 errcode_t retval;
441 blk_t blk;
442 int i, num;
443 struct progress_struct progress;
444
445 if (quiet)
446 memset(&progress, 0, sizeof(progress));
447 else
448 progress_init(&progress, _("Writing inode tables: "),
449 fs->group_desc_count);
450
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000452 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000454 blk = fs->group_desc[i].bg_inode_table;
455 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000456
457 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
458 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500459 fprintf(stderr, _("\nCould not write %d blocks "
460 "in inode table starting at %d: %s\n"),
461 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000462 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000463 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000464 if (sync_kludge) {
465 if (sync_kludge == 1)
466 sync();
467 else if ((i % sync_kludge) == 0)
468 sync();
469 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000470 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000471 zero_blocks(0, 0, 0, 0, 0, 0);
472 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473}
474
475static void create_root_dir(ext2_filsys fs)
476{
477 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000478 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479
480 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
481 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000482 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483 exit(1);
484 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000485 if (geteuid()) {
486 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
487 if (retval) {
488 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000489 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000490 exit(1);
491 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000492 inode.i_uid = getuid();
493 if (inode.i_uid)
494 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000495 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
496 if (retval) {
497 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000498 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000499 exit(1);
500 }
501 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502}
503
504static void create_lost_and_found(ext2_filsys fs)
505{
506 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000507 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 const char *name = "lost+found";
509 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000510 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511
Theodore Ts'o6a525062001-12-24 09:40:00 -0500512 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
514 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000515 com_err("ext2fs_mkdir", retval,
516 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 exit(1);
518 }
519
520 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
521 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000522 com_err("ext2_lookup", retval,
523 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 exit(1);
525 }
526
527 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000528 if ((lpf_size += fs->blocksize) >= 16*1024)
529 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 retval = ext2fs_expand_dir(fs, ino);
531 if (retval) {
532 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000533 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 exit(1);
535 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500536 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537}
538
539static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
540{
541 errcode_t retval;
542
Theodore Ts'of3db3561997-04-26 13:34:30 +0000543 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 fs->group_desc[0].bg_free_inodes_count--;
545 fs->super->s_free_inodes_count--;
546 retval = ext2fs_update_bb_inode(fs, bb_list);
547 if (retval) {
548 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000549 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000550 exit(1);
551 }
552
553}
554
555static void reserve_inodes(ext2_filsys fs)
556{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000557 ext2_ino_t i;
558 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000560 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000561 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562 group = ext2fs_group_of_ino(fs, i);
563 fs->group_desc[group].bg_free_inodes_count--;
564 fs->super->s_free_inodes_count--;
565 }
566 ext2fs_mark_ib_dirty(fs);
567}
568
Theodore Ts'o756df352002-03-07 20:52:12 -0500569#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500570#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500571#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500572
Theodore Ts'o04a96852001-08-30 21:55:26 -0400573static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000574{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400575 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000576 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500577 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000578
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400579 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600580 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500581 printf(_("Out of memory erasing sectors %d-%d\n"),
582 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600583 exit(1);
584 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500585
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500586 if (sect == 0) {
587 /* Check for a BSD disklabel, and don't erase it if so */
588 retval = io_channel_read_blk(fs->io, 0, -512, buf);
589 if (retval)
590 fprintf(stderr,
591 _("Warning: could not read block 0: %s\n"),
592 error_message(retval));
593 else {
594 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
595 if ((*magic == BSD_DISKMAGIC) ||
596 (*magic == BSD_MAGICDISK))
597 return;
598 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500599 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500600
Theodore Ts'o70988102002-07-14 08:00:00 -0400601 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000602 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400603 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000604 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400605 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000606 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500607 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
608 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000609}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000610
611static void create_journal_dev(ext2_filsys fs)
612{
613 struct progress_struct progress;
614 errcode_t retval;
615 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000616 blk_t blk;
617 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000618
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000619 retval = ext2fs_create_journal_superblock(fs,
620 fs->super->s_blocks_count, 0, &buf);
621 if (retval) {
622 com_err("create_journal_dev", retval,
623 _("while initializing journal superblock"));
624 exit(1);
625 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000626 if (quiet)
627 memset(&progress, 0, sizeof(progress));
628 else
629 progress_init(&progress, _("Zeroing journal device: "),
630 fs->super->s_blocks_count);
631
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000632 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
633 &progress, &blk, &count);
634 if (retval) {
635 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000636 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000637 blk, count);
638 exit(1);
639 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000640 zero_blocks(0, 0, 0, 0, 0, 0);
641
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000642 retval = io_channel_write_blk(fs->io,
643 fs->super->s_first_data_block+1,
644 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000645 if (retval) {
646 com_err("create_journal_dev", retval,
647 _("while writing journal superblock"));
648 exit(1);
649 }
650 progress_close(&progress);
651}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000652
Theodore Ts'o3839e651997-04-26 13:21:57 +0000653static void show_stats(ext2_filsys fs)
654{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000655 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000656 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000658 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000659
660 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'o66938372002-03-08 00:14:46 -0500661 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000662 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000663
664 memset(buf, 0, sizeof(buf));
665 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000666 printf(_("Filesystem label=%s\n"), buf);
667 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000668 switch (fs->super->s_creator_os) {
669 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000670 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000671 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000672 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000673 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000674 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000675 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000676 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000677 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000678 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000679 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000681 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000682 s->s_r_blocks_count,
683 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000684 printf(_("First data block=%u\n"), s->s_first_data_block);
685 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000686 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000687 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000688 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000689 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000691 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000692
693 if (fs->group_desc_count == 1) {
694 printf("\n");
695 return;
696 }
697
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000698 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000699 group_block = s->s_first_data_block;
700 col_left = 0;
701 for (i = 1; i < fs->group_desc_count; i++) {
702 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000703 if (!ext2fs_bg_has_super(fs, i))
704 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000705 if (i != 1)
706 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000707 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000708 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000710 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000711 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000712 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000713 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 }
715 printf("\n\n");
716}
717
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000718/*
719 * Set the S_CREATOR_OS field. Return true if OS is known,
720 * otherwise, 0.
721 */
722static int set_os(struct ext2_super_block *sb, char *os)
723{
724 if (isdigit (*os))
725 sb->s_creator_os = atoi (os);
726 else if (strcasecmp(os, "linux") == 0)
727 sb->s_creator_os = EXT2_OS_LINUX;
728 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
729 sb->s_creator_os = EXT2_OS_HURD;
730 else if (strcasecmp(os, "masix") == 0)
731 sb->s_creator_os = EXT2_OS_MASIX;
732 else
733 return 0;
734 return 1;
735}
736
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000737#define PATH_SET "PATH=/sbin"
738
Theodore Ts'od163b091997-10-03 17:42:28 +0000739static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000740{
741 char *buf, *token, *next, *p, *arg;
742 int len;
743 int raid_usage = 0;
744
745 len = strlen(opts);
746 buf = malloc(len+1);
747 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000748 fprintf(stderr, _("Couldn't allocate memory to parse "
749 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000750 exit(1);
751 }
752 strcpy(buf, opts);
753 for (token = buf; token && *token; token = next) {
754 p = strchr(token, ',');
755 next = 0;
756 if (p) {
757 *p = 0;
758 next = p+1;
759 }
760 arg = strchr(token, '=');
761 if (arg) {
762 *arg = 0;
763 arg++;
764 }
765 if (strcmp(token, "stride") == 0) {
766 if (!arg) {
767 raid_usage++;
768 continue;
769 }
770 fs_stride = strtoul(arg, &p, 0);
771 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000772 fprintf(stderr,
773 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000774 raid_usage++;
775 continue;
776 }
777 } else
778 raid_usage++;
779 }
780 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000781 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000782 "Raid options are separated by commas, "
783 "and may take an argument which\n"
784 "\tis set off by an equals ('=') sign.\n\n"
785 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000786 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000787 exit(1);
788 }
789}
790
Theodore Ts'o896938d1999-10-23 01:04:50 +0000791static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400792 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
793 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000794 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400795 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
796 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000797 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
798};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000799
800
Theodore Ts'o3839e651997-04-26 13:21:57 +0000801static void PRS(int argc, char *argv[])
802{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400803 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000804 int size;
805 char * tmp;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000806 int blocksize = 0;
807 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600808 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000809 int reserved_ratio = 5;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400810 int sector_size = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000811 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000812 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000813 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000814 char * raid_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500815 const char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400816 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000817 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500818#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000819 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000820#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400821 long sysval;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000822
Theodore Ts'o3839e651997-04-26 13:21:57 +0000823 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000824 if (oldpath) {
825 char *newpath;
826
827 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
828 strcpy (newpath, PATH_SET);
829 strcat (newpath, ":");
830 strcat (newpath, oldpath);
831 putenv (newpath);
832 } else
833 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000834
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000835 tmp = getenv("MKE2FS_SYNC");
836 if (tmp)
837 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400838
839 /* Determine the system page size if possible */
840#ifdef HAVE_SYSCONF
841#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
842#define _SC_PAGESIZE _SC_PAGE_SIZE
843#endif
844#ifdef _SC_PAGESIZE
845 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600846 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400847 sys_page_size = sysval;
848#endif /* _SC_PAGESIZE */
849#endif /* HAVE_SYSCONF */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000850
Theodore Ts'o3839e651997-04-26 13:21:57 +0000851 setbuf(stdout, NULL);
852 setbuf(stderr, NULL);
853 initialize_ext2_error_table();
854 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000855 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400856 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
857 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500858#if 0
Theodore Ts'o843049c2002-09-22 15:37:40 -0400859 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500860#endif
Theodore Ts'o843049c2002-09-22 15:37:40 -0400861
Theodore Ts'o756df352002-03-07 20:52:12 -0500862#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000863 if (uname(&ut)) {
864 perror("uname");
865 exit(1);
866 }
867 if ((ut.release[0] == '1') ||
868 (ut.release[0] == '2' && ut.release[1] == '.' &&
869 ut.release[2] < '2' && ut.release[3] == '.')) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000870 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400871 param.s_feature_incompat = 0;
872 param.s_feature_compat = 0;
873 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000874 }
875#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700876
877 if (argc && *argv) {
878 program_name = get_progname(*argv);
879
880 /* If called as mkfs.ext3, create a journal inode */
881 if (!strcmp(program_name, "mkfs.ext3"))
882 journal_size = -1;
883 }
884
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000885 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000886 "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 +0000887 switch (c) {
888 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400889 blocksize = strtol(optarg, &tmp, 0);
890 b = (blocksize > 0) ? blocksize : -blocksize;
891 if (b < EXT2_MIN_BLOCK_SIZE ||
892 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000893 com_err(program_name, 0,
894 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000895 exit(1);
896 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600897 if (blocksize > 4096)
898 fprintf(stderr, _("Warning: blocksize %d not "
899 "usable on most systems.\n"),
900 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400901 if (blocksize > 0)
902 param.s_log_block_size =
903 int_log2(blocksize >>
904 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000905 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000906 case 'c': /* Check for bad blocks */
907 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500908 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000909 break;
910 case 'f':
911 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600912 if (size < EXT2_MIN_BLOCK_SIZE ||
913 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000914 com_err(program_name, 0,
915 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000916 optarg);
917 exit(1);
918 }
919 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000920 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500921 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000922 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000923 break;
924 case 'g':
925 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000926 if (*tmp) {
927 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000928 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000929 exit(1);
930 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000931 if ((param.s_blocks_per_group % 8) != 0) {
932 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000933 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000934 exit(1);
935 }
936 break;
937 case 'i':
938 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600939 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
940 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000941 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000942 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -0600943 _("bad inode ratio %s (min %d/max %d"),
944 optarg, EXT2_MIN_BLOCK_SIZE,
945 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000946 exit(1);
947 }
948 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000949 case 'J':
950 parse_journal_opts(optarg);
951 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000952 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000953 param.s_feature_compat |=
954 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000955 if (!journal_size)
956 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000957 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000958 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000959 bad_blocks_filename = malloc(strlen(optarg)+1);
960 if (!bad_blocks_filename) {
961 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000962 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000963 exit(1);
964 }
965 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000966 break;
967 case 'm':
968 reserved_ratio = strtoul(optarg, &tmp, 0);
969 if (reserved_ratio > 50 || *tmp) {
970 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000971 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000972 optarg);
973 exit(1);
974 }
975 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000976 case 'n':
977 noaction++;
978 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000979 case 'o':
980 creator_os = optarg;
981 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000982 case 'r':
983 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000984 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400985 param.s_feature_incompat = 0;
986 param.s_feature_compat = 0;
987 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000988 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000989 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000990 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400991 if (atoi(optarg))
992 param.s_feature_ro_compat |=
993 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
994 else
995 param.s_feature_ro_compat &=
996 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000997 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000998#ifdef EXT2_DYNAMIC_REV
999 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001000 inode_size = strtoul(optarg, &tmp, 0);
1001 if (*tmp) {
1002 com_err(program_name, 0,
1003 _("bad inode size - %s"), optarg);
1004 exit(1);
1005 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001006 break;
1007#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001008 case 'N':
1009 num_inodes = atoi(optarg);
1010 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001011 case 'v':
1012 verbose = 1;
1013 break;
1014 case 'q':
1015 quiet = 1;
1016 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001017 case 'F':
1018 force = 1;
1019 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001020 case 'L':
1021 volume_label = optarg;
1022 break;
1023 case 'M':
1024 mount_dir = optarg;
1025 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001026 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001027 if (!strcmp(optarg, "none") || default_features) {
1028 param.s_feature_compat = 0;
1029 param.s_feature_incompat = 0;
1030 param.s_feature_ro_compat = 0;
1031 default_features = 0;
1032 }
1033 if (!strcmp(optarg, "none"))
1034 break;
1035 if (e2p_edit_feature(optarg,
1036 &param.s_feature_compat,
1037 ok_features)) {
1038 fprintf(stderr,
1039 _("Invalid filesystem option set: %s\n"), optarg);
1040 exit(1);
1041 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001042 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001043 case 'R':
1044 raid_opts = optarg;
1045 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001046 case 'S':
1047 super_only = 1;
1048 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001049 case 'T':
1050 fs_type = optarg;
1051 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001052 case 'V':
1053 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001054 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +00001055 error_message(EXT2_ET_BASE));
1056 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001057 default:
1058 usage();
1059 }
1060 if (optind == argc)
1061 usage();
1062 device_name = argv[optind];
1063 optind++;
1064 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001065 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1066
1067 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001068 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001069 argv[optind - 1]);
1070 exit(1);
1071 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001072 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001073 }
1074 if (optind < argc)
1075 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001076
Theodore Ts'o38798572003-04-16 15:29:39 -04001077 if (!quiet)
1078 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1079 E2FSPROGS_DATE);
1080
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001081 if (raid_opts)
1082 parse_raid_opts(raid_opts);
1083
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001084 /*
1085 * If there's no blocksize specified and there is a journal
1086 * device, use it to figure out the blocksize
1087 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001088 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001089 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001090 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001091
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001092#ifdef CONFIG_TESTIO_DEBUG
1093 io_ptr = test_io_manager;
1094 test_io_backing_manager = unix_io_manager;
1095#else
1096 io_ptr = unix_io_manager;
1097#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001098 retval = ext2fs_open(journal_device,
1099 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001100 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001101 if (retval) {
1102 com_err(program_name, retval,
1103 _("while trying to open journal device %s\n"),
1104 journal_device);
1105 exit(1);
1106 }
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001107 if ((blocksize < 0) && (jfs->blocksize < -blocksize)) {
1108 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001109 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001110 "minimum blocksize %d\n"), jfs->blocksize,
1111 -blocksize);
1112 exit(1);
1113 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001114 blocksize = jfs->blocksize;
1115 param.s_log_block_size =
1116 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1117 ext2fs_close(jfs);
1118 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001119
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001120 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001121 if (!force) {
1122 com_err(program_name, 0,
1123 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001124 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001125 proceed_question();
1126 }
1127 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1128 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001129 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001130 }
Theodore Ts'ocf7d5f12003-07-25 17:39:51 -04001131 if ((blocksize > 4096) &&
1132 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1133 fprintf(stderr, "\nWarning: some 2.4 kernels do not support "
1134 "blocksizes greater than 4096 \n\tusing ext3."
1135 " Use -b 4096 if this is an issue for you.\n\n");
Andreas Dilger932a4892002-05-16 03:20:07 -06001136
Andreas Dilgerfa724582001-08-04 01:18:34 -06001137 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1138 if (!fs_type)
1139 fs_type = "journal";
1140 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001141 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1142 param.s_feature_compat = 0;
1143 param.s_feature_ro_compat = 0;
1144 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001145 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1146 (param.s_feature_compat || param.s_feature_ro_compat ||
1147 param.s_feature_incompat))
1148 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1149
Theodore Ts'o74becf31997-04-26 14:37:06 +00001150 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001151 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001152 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001153
Theodore Ts'o3839e651997-04-26 13:21:57 +00001154 param.s_log_frag_size = param.s_log_block_size;
1155
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001156 if (noaction && param.s_blocks_count) {
1157 dev_size = param.s_blocks_count;
1158 retval = 0;
1159 } else
1160 retval = ext2fs_get_device_size(device_name,
1161 EXT2_BLOCK_SIZE(&param),
1162 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001163 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1164 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001165 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001166 exit(1);
1167 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001168 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001169 if (retval == EXT2_ET_UNIMPLEMENTED) {
1170 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001171 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001172 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001173 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001174 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001175 } else {
1176 if (dev_size == 0) {
1177 com_err(program_name, 0,
1178 _("Device size reported to be zero. "
1179 "Invalid partition specified, or\n\t"
1180 "partition table wasn't reread "
1181 "after running fdisk, due to\n\t"
1182 "a modified partition being busy "
1183 "and in use. You may need to reboot\n\t"
1184 "to re-read your partition table.\n"
1185 ));
1186 exit(1);
1187 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001188 param.s_blocks_count = dev_size;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001189 if (sys_page_size > EXT2_BLOCK_SIZE(&param))
1190 param.s_blocks_count &= ~((sys_page_size /
1191 EXT2_BLOCK_SIZE(&param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001192 }
1193
Theodore Ts'oa789d841998-03-30 01:20:55 +00001194 } else if (!force && (param.s_blocks_count > dev_size)) {
1195 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001196 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001197 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001198 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001199
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001200 /*
1201 * If the user asked for HAS_JOURNAL, then make sure a journal
1202 * gets created.
1203 */
1204 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1205 !journal_size)
1206 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001207
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001208 /* Set first meta blockgroup via an environment variable */
1209 /* (this is mostly for debugging purposes) */
1210 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1211 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1212 param.s_first_meta_bg = atoi(tmp);
1213
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001214 /* Get the hardware sector size, if available */
1215 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1216 if (retval) {
1217 com_err(program_name, retval,
1218 _("while trying to determine hardware sector size"));
1219 exit(1);
1220 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001221
1222 if (tmp = getenv("MKE2FS_DEVICE_SECTSIZE"))
1223 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001224
1225 set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001226 blocksize = EXT2_BLOCK_SIZE(&param);
1227
Theodore Ts'o521e3681997-04-29 17:48:10 +00001228 if (param.s_blocks_per_group) {
1229 if (param.s_blocks_per_group < 256 ||
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001230 param.s_blocks_per_group > 8 * blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001231 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001232 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001233 exit(1);
1234 }
1235 }
1236
Andreas Dilger932a4892002-05-16 03:20:07 -06001237 if (inode_size) {
1238 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1239 inode_size > EXT2_BLOCK_SIZE(&param) ||
1240 inode_size & (inode_size - 1)) {
1241 com_err(program_name, 0,
1242 _("bad inode size %d (min %d/max %d)"),
1243 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001244 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001245 exit(1);
1246 }
1247 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1248 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1249 "on most systems\n"),
1250 inode_size);
1251 param.s_inode_size = inode_size;
1252 }
1253
Theodore Ts'o3839e651997-04-26 13:21:57 +00001254 /*
1255 * Calculate number of inodes based on the inode ratio
1256 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001257 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001258 ((__u64) param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001259 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001260
1261 /*
1262 * Calculate number of blocks to reserve
1263 */
1264 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001265
Theodore Ts'o3839e651997-04-26 13:21:57 +00001266}
1267
1268int main (int argc, char *argv[])
1269{
1270 errcode_t retval = 0;
1271 ext2_filsys fs;
1272 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001273 int journal_blocks;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001274 int i, val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001275 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001276
1277#ifdef ENABLE_NLS
1278 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001279 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001280 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1281 textdomain(NLS_CAT_NAME);
1282#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001283 PRS(argc, argv);
1284
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001285#ifdef CONFIG_TESTIO_DEBUG
1286 io_ptr = test_io_manager;
1287 test_io_backing_manager = unix_io_manager;
1288#else
1289 io_ptr = unix_io_manager;
1290#endif
1291
Theodore Ts'o3839e651997-04-26 13:21:57 +00001292 /*
1293 * Initialize the superblock....
1294 */
1295 retval = ext2fs_initialize(device_name, 0, &param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001296 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001297 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001298 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001299 exit(1);
1300 }
1301
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001302 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001303 * Wipe out the old on-disk superblock
1304 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001305 if (!noaction)
1306 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001307
1308 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001309 * Generate a UUID for it...
1310 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001311 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001312
1313 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001314 * Initialize the directory index variables
1315 */
1316 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1317 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1318
1319 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001320 * Add "jitter" to the superblock's check interval so that we
1321 * don't check all the filesystems at the same time. We use a
1322 * kludgy hack of using the UUID to derive a random jitter value.
1323 */
1324 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1325 val += fs->super->s_uuid[i];
1326 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1327
1328 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001329 * Override the creator OS, if applicable
1330 */
1331 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001332 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001333 exit(1);
1334 }
1335
1336 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001337 * For the Hurd, we will turn off filetype since it doesn't
1338 * support it.
1339 */
1340 if (fs->super->s_creator_os == EXT2_OS_HURD)
1341 fs->super->s_feature_incompat &=
1342 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1343
1344 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001345 * Set the volume label...
1346 */
1347 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001348 memset(fs->super->s_volume_name, 0,
1349 sizeof(fs->super->s_volume_name));
1350 strncpy(fs->super->s_volume_name, volume_label,
1351 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001352 }
1353
1354 /*
1355 * Set the last mount directory
1356 */
1357 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001358 memset(fs->super->s_last_mounted, 0,
1359 sizeof(fs->super->s_last_mounted));
1360 strncpy(fs->super->s_last_mounted, mount_dir,
1361 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001362 }
1363
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001364 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001365 show_stats(fs);
1366
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001367 if (noaction)
1368 exit(0);
1369
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001370 if (fs->super->s_feature_incompat &
1371 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1372 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001373 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001374 }
1375
Theodore Ts'o3839e651997-04-26 13:21:57 +00001376 if (bad_blocks_filename)
1377 read_bb_file(fs, &bb_list, bad_blocks_filename);
1378 if (cflag)
1379 test_disk(fs, &bb_list);
1380
1381 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001382 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001383 retval = ext2fs_allocate_tables(fs);
1384 if (retval) {
1385 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001386 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001387 exit(1);
1388 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001389 if (super_only) {
1390 fs->super->s_state |= EXT2_ERROR_FS;
1391 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1392 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001393 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001394 int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001395 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001396 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001397 blk_t ret_blk;
1398
1399#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001400 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001401#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001402
1403 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001404 * Wipe out any old MD RAID (or other) metadata at the end
1405 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001406 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001407 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001408 start = (blocks & ~(rsv - 1));
1409 if (start > rsv)
1410 start -= rsv;
1411 if (start > 0)
1412 retval = zero_blocks(fs, start, blocks - start,
1413 NULL, &ret_blk, NULL);
1414
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001415 if (retval) {
1416 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001417 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001418 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001419 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001420 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001421 create_root_dir(fs);
1422 create_lost_and_found(fs);
1423 reserve_inodes(fs);
1424 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001425 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001426
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001427 if (journal_device) {
1428 ext2_filsys jfs;
1429
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001430 if (!force)
1431 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001432 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001433
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001434 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1435 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1436 fs->blocksize, unix_io_manager, &jfs);
1437 if (retval) {
1438 com_err(program_name, retval,
1439 _("while trying to open journal device %s\n"),
1440 journal_device);
1441 exit(1);
1442 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001443 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001444 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001445 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001446 fflush(stdout);
1447 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001448 retval = ext2fs_add_journal_device(fs, jfs);
1449 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001450 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001451 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001452 journal_device);
1453 exit(1);
1454 }
1455 if (!quiet)
1456 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001457 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001458 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001459 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001460 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001461
1462 if (!journal_blocks) {
1463 fs->super->s_feature_compat &=
1464 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1465 goto no_journal;
1466 }
1467 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001468 printf(_("Creating journal (%d blocks): "),
1469 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001470 fflush(stdout);
1471 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001472 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1473 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001474 if (retval) {
1475 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001476 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001477 exit(1);
1478 }
1479 if (!quiet)
1480 printf(_("done\n"));
1481 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001482no_journal:
1483
Theodore Ts'o3839e651997-04-26 13:21:57 +00001484 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001485 printf(_("Writing superblocks and "
1486 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001487 retval = ext2fs_flush(fs);
1488 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001489 fprintf(stderr,
1490 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001491 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001492 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001493 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001494 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1495 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001496 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001497 val = ext2fs_close(fs);
1498 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001499}