blob: f865d435c4ccfcf679afd7a71487e63069453e28 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12/* Usage: mke2fs [options] device
13 *
14 * The device may be a block device or a image of one, but this isn't
15 * enforced (but it's not much fun on a character device :-).
16 */
17
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000018#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <time.h>
Theodore Ts'o756df352002-03-07 20:52:12 -050023#ifdef __linux__
Theodore Ts'o27401561999-09-14 20:11:19 +000024#include <sys/utsname.h>
25#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000028#else
29extern char *optarg;
30extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#endif
32#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
35#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000037#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000045#include <sys/types.h>
46
Theodore Ts'o54c637d2001-05-14 11:45:38 +000047#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000048#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000049#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000050#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000051#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000052#include "util.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000053#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000054#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
56#define STRIDE_LENGTH 8
57
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000058#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000059#define ZAP_BOOTBLOCK
60#endif
61
Theodore Ts'o3839e651997-04-26 13:21:57 +000062extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000063extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000064
65const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000066const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000067
68/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000069int cflag;
70int verbose;
71int quiet;
72int super_only;
73int force;
74int noaction;
75int journal_size;
76int journal_flags;
77char *bad_blocks_filename;
78__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079
80struct ext2_super_block param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000081char *creator_os;
82char *volume_label;
83char *mount_dir;
84char *journal_device;
85int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
Theodore Ts'o31e29a12002-05-17 10:53:07 -040087int sys_page_size = 4096;
88
Theodore Ts'o63985322001-01-03 17:02:13 +000089static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000090{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000091 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000092 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000093 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
94 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000095 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +000096 "[-r fs-revision] [-R raid_opts] [-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 program_name);
98 exit(1);
99}
100
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000101static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102{
103 int l = 0;
104
105 arg >>= 1;
106 while (arg) {
107 l++;
108 arg >>= 1;
109 }
110 return l;
111}
112
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000113static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000114{
115 int l;
116
117 for (l=0; arg ; l++)
118 arg = arg / 10;
119 return l;
120}
121
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000123 * This function sets the default parameters for a filesystem
124 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000125 * The type is specified by the user. The size is the maximum size
126 * (in megabytes) for which a set of parameters applies, with a size
127 * of zero meaning that it is the default parameter for the type.
128 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000129 */
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400130#define DEF_MAX_BLOCKSIZE -1
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000131static char default_str[] = "default";
132struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000133 const char *type;
134 int size;
135 int blocksize;
136 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000137} settings[] = {
138 { default_str, 0, 4096, 8192 },
139 { default_str, 512, 1024, 4096 },
140 { default_str, 3, 1024, 8192 },
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000141 { "journal", 0, 4096, 8192 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000142 { "news", 0, 4096, 4096 },
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400143 { "largefile", 0, DEF_MAX_BLOCKSIZE, 1024 * 1024 },
144 { "largefile4", 0, DEF_MAX_BLOCKSIZE, 4096 * 1024 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000145 { 0, 0, 0, 0},
146};
147
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500148static void set_fs_defaults(const char *fs_type,
149 struct ext2_super_block *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000150 int blocksize, int *inode_ratio)
151{
152 int megs;
153 int ratio = 0;
154 struct mke2fs_defaults *p;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400155 int use_bsize = 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000156
Andreas Dilger932a4892002-05-16 03:20:07 -0600157 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000158 if (inode_ratio)
159 ratio = *inode_ratio;
160 if (!fs_type)
161 fs_type = default_str;
162 for (p = settings; p->type; p++) {
163 if ((strcmp(p->type, fs_type) != 0) &&
164 (strcmp(p->type, default_str) != 0))
165 continue;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400166 if ((p->size != 0) && (megs > p->size))
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000167 continue;
168 if (ratio == 0)
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600169 *inode_ratio = p->inode_ratio < blocksize ?
170 blocksize : p->inode_ratio;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400171 use_bsize = p->blocksize;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000172 }
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400173 if (blocksize <= 0) {
174 if (use_bsize == DEF_MAX_BLOCKSIZE)
175 use_bsize = sys_page_size;
176 if ((blocksize < 0) && (use_bsize < (-blocksize)))
177 use_bsize = -blocksize;
178 blocksize = use_bsize;
179 super->s_blocks_count /= blocksize / 1024;
180 }
181 super->s_log_frag_size = super->s_log_block_size =
182 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000183}
184
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400185
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000186/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187 * Helper function for read_bb_file and test_disk
188 */
189static void invalid_block(ext2_filsys fs, blk_t blk)
190{
Theodore Ts'o66938372002-03-08 00:14:46 -0500191 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192 return;
193}
194
195/*
196 * Reads the bad blocks list from a file
197 */
198static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
199 const char *bad_blocks_file)
200{
201 FILE *f;
202 errcode_t retval;
203
204 f = fopen(bad_blocks_file, "r");
205 if (!f) {
206 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000207 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 exit(1);
209 }
210 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
211 fclose (f);
212 if (retval) {
213 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000214 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000215 exit(1);
216 }
217}
218
219/*
220 * Runs the badblocks program to test the disk
221 */
222static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
223{
224 FILE *f;
225 errcode_t retval;
226 char buf[1024];
227
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500228 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
229 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
230 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000231 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000232 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 f = popen(buf, "r");
234 if (!f) {
235 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000236 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 exit(1);
238 }
239 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000240 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 if (retval) {
242 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000243 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 exit(1);
245 }
246}
247
248static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
249{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000250 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251 int must_be_good;
252 blk_t blk;
253 badblocks_iterate bb_iter;
254 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000255 blk_t group_block;
256 int group;
257 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000258
259 if (!bb_list)
260 return;
261
262 /*
263 * The primary superblock and group descriptors *must* be
264 * good; if not, abort.
265 */
266 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
267 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000268 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000269 fprintf(stderr, _("Block %d in primary "
270 "superblock/group descriptor area bad.\n"), i);
271 fprintf(stderr, _("Blocks %d through %d must be good "
272 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000274 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000275 exit(1);
276 }
277 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000278
279 /*
280 * See if any of the bad blocks are showing up in the backup
281 * superblocks and/or group descriptors. If so, issue a
282 * warning and adjust the block counts appropriately.
283 */
284 group_block = fs->super->s_first_data_block +
285 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000286
287 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000288 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000290 if (ext2fs_badblocks_list_test(bb_list,
291 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000292 if (!group_bad)
293 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000294_("Warning: the backup superblock/group descriptors at block %d contain\n"
295" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000296 group_block);
297 group_bad++;
298 group = ext2fs_group_of_blk(fs, group_block+j);
299 fs->group_desc[group].bg_free_blocks_count++;
300 fs->super->s_free_blocks_count++;
301 }
302 }
303 group_block += fs->super->s_blocks_per_group;
304 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305
306 /*
307 * Mark all the bad blocks as used...
308 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000309 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000311 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000312 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 exit(1);
314 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000315 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000316 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000317 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000318}
319
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000320/*
321 * These functions implement a generalized progress meter.
322 */
323struct progress_struct {
324 char format[20];
325 char backup[80];
326 __u32 max;
327};
328
329static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500330 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000331{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000332 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000333
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000334 memset(progress, 0, sizeof(struct progress_struct));
335 if (quiet)
336 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000337
338 /*
339 * Figure out how many digits we need
340 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000341 i = int_log10(max);
342 sprintf(progress->format, "%%%dd/%%%dld", i, i);
343 memset(progress->backup, '\b', sizeof(progress->backup)-1);
344 progress->backup[sizeof(progress->backup)-1] = 0;
345 if ((2*i)+1 < sizeof(progress->backup))
346 progress->backup[(2*i)+1] = 0;
347 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000348
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000349 fputs(label, stdout);
350 fflush(stdout);
351}
352
353static void progress_update(struct progress_struct *progress, __u32 val)
354{
355 if (progress->format[0] == 0)
356 return;
357 printf(progress->format, val, progress->max);
358 fputs(progress->backup, stdout);
359}
360
361static void progress_close(struct progress_struct *progress)
362{
363 if (progress->format[0] == 0)
364 return;
365 fputs(_("done \n"), stdout);
366}
367
368
369/*
370 * Helper function which zeros out _num_ blocks starting at _blk_. In
371 * case of an error, the details of the error is returned via _ret_blk_
372 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
373 * success, and an error code on an error.
374 *
375 * As a special case, if the first argument is NULL, then it will
376 * attempt to free the static zeroizing buffer. (This is to keep
377 * programs that check for memory leaks happy.)
378 */
379static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
380 struct progress_struct *progress,
381 blk_t *ret_blk, int *ret_count)
382{
383 int j, count, next_update, next_update_incr;
384 static char *buf;
385 errcode_t retval;
386
387 /* If fs is null, clean up the static buffer and return */
388 if (!fs) {
389 if (buf) {
390 free(buf);
391 buf = 0;
392 }
393 return 0;
394 }
395 /* Allocate the zeroizing buffer if necessary */
396 if (!buf) {
397 buf = malloc(fs->blocksize * STRIDE_LENGTH);
398 if (!buf) {
399 com_err("malloc", ENOMEM,
400 _("while allocating zeroizing buffer"));
401 exit(1);
402 }
403 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
404 }
405 /* OK, do the write loop */
406 next_update = 0;
407 next_update_incr = num / 100;
408 if (next_update_incr < 1)
409 next_update_incr = 1;
410 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400411 count = num - j;
412 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000413 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000414 retval = io_channel_write_blk(fs->io, blk, count, buf);
415 if (retval) {
416 if (ret_count)
417 *ret_count = count;
418 if (ret_blk)
419 *ret_blk = blk;
420 return retval;
421 }
422 if (progress && j > next_update) {
423 next_update += num / 100;
424 progress_update(progress, blk);
425 }
426 }
427 return 0;
428}
429
430static void write_inode_tables(ext2_filsys fs)
431{
432 errcode_t retval;
433 blk_t blk;
434 int i, num;
435 struct progress_struct progress;
436
437 if (quiet)
438 memset(&progress, 0, sizeof(progress));
439 else
440 progress_init(&progress, _("Writing inode tables: "),
441 fs->group_desc_count);
442
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000444 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000446 blk = fs->group_desc[i].bg_inode_table;
447 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000448
449 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
450 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500451 fprintf(stderr, _("\nCould not write %d blocks "
452 "in inode table starting at %d: %s\n"),
453 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000454 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000455 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000456 if (sync_kludge) {
457 if (sync_kludge == 1)
458 sync();
459 else if ((i % sync_kludge) == 0)
460 sync();
461 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000463 zero_blocks(0, 0, 0, 0, 0, 0);
464 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465}
466
467static void create_root_dir(ext2_filsys fs)
468{
469 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000470 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471
472 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
473 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000474 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 exit(1);
476 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000477 if (geteuid()) {
478 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
479 if (retval) {
480 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000481 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000482 exit(1);
483 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000484 inode.i_uid = getuid();
485 if (inode.i_uid)
486 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000487 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
488 if (retval) {
489 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000490 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000491 exit(1);
492 }
493 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494}
495
496static void create_lost_and_found(ext2_filsys fs)
497{
498 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000499 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000500 const char *name = "lost+found";
501 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000502 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503
Theodore Ts'o6a525062001-12-24 09:40:00 -0500504 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
506 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000507 com_err("ext2fs_mkdir", retval,
508 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 exit(1);
510 }
511
512 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
513 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000514 com_err("ext2_lookup", retval,
515 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516 exit(1);
517 }
518
519 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000520 if ((lpf_size += fs->blocksize) >= 16*1024)
521 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 retval = ext2fs_expand_dir(fs, ino);
523 if (retval) {
524 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000525 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 exit(1);
527 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500528 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529}
530
531static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
532{
533 errcode_t retval;
534
Theodore Ts'of3db3561997-04-26 13:34:30 +0000535 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 fs->group_desc[0].bg_free_inodes_count--;
537 fs->super->s_free_inodes_count--;
538 retval = ext2fs_update_bb_inode(fs, bb_list);
539 if (retval) {
540 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000541 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542 exit(1);
543 }
544
545}
546
547static void reserve_inodes(ext2_filsys fs)
548{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000549 ext2_ino_t i;
550 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000552 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000553 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000554 group = ext2fs_group_of_ino(fs, i);
555 fs->group_desc[group].bg_free_inodes_count--;
556 fs->super->s_free_inodes_count--;
557 }
558 ext2fs_mark_ib_dirty(fs);
559}
560
Theodore Ts'o756df352002-03-07 20:52:12 -0500561#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500562#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500563#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500564
Theodore Ts'o04a96852001-08-30 21:55:26 -0400565static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000566{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400567 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000568 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500569 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000570
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400571 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600572 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500573 printf(_("Out of memory erasing sectors %d-%d\n"),
574 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600575 exit(1);
576 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500577
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500578 if (sect == 0) {
579 /* Check for a BSD disklabel, and don't erase it if so */
580 retval = io_channel_read_blk(fs->io, 0, -512, buf);
581 if (retval)
582 fprintf(stderr,
583 _("Warning: could not read block 0: %s\n"),
584 error_message(retval));
585 else {
586 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
587 if ((*magic == BSD_DISKMAGIC) ||
588 (*magic == BSD_MAGICDISK))
589 return;
590 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500591 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500592
Theodore Ts'o70988102002-07-14 08:00:00 -0400593 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000594 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400595 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000596 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400597 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000598 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500599 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
600 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000601}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000602
603static void create_journal_dev(ext2_filsys fs)
604{
605 struct progress_struct progress;
606 errcode_t retval;
607 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000608 blk_t blk;
609 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000610
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000611 retval = ext2fs_create_journal_superblock(fs,
612 fs->super->s_blocks_count, 0, &buf);
613 if (retval) {
614 com_err("create_journal_dev", retval,
615 _("while initializing journal superblock"));
616 exit(1);
617 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000618 if (quiet)
619 memset(&progress, 0, sizeof(progress));
620 else
621 progress_init(&progress, _("Zeroing journal device: "),
622 fs->super->s_blocks_count);
623
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000624 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
625 &progress, &blk, &count);
626 if (retval) {
627 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000628 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000629 blk, count);
630 exit(1);
631 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000632 zero_blocks(0, 0, 0, 0, 0, 0);
633
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000634 retval = io_channel_write_blk(fs->io,
635 fs->super->s_first_data_block+1,
636 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000637 if (retval) {
638 com_err("create_journal_dev", retval,
639 _("while writing journal superblock"));
640 exit(1);
641 }
642 progress_close(&progress);
643}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000644
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645static void show_stats(ext2_filsys fs)
646{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000647 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000648 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000650 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651
652 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'o66938372002-03-08 00:14:46 -0500653 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000655
656 memset(buf, 0, sizeof(buf));
657 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000658 printf(_("Filesystem label=%s\n"), buf);
659 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000660 switch (fs->super->s_creator_os) {
661 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000662 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000663 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000664 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000665 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000666 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000667 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000668 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000669 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000670 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000671 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000672 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000673 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674 s->s_r_blocks_count,
675 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000676 printf(_("First data block=%u\n"), s->s_first_data_block);
677 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000678 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000679 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000680 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000681 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000682 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000683 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684
685 if (fs->group_desc_count == 1) {
686 printf("\n");
687 return;
688 }
689
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000690 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691 group_block = s->s_first_data_block;
692 col_left = 0;
693 for (i = 1; i < fs->group_desc_count; i++) {
694 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000695 if (!ext2fs_bg_has_super(fs, i))
696 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000697 if (i != 1)
698 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000699 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000700 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000701 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000702 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000703 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000704 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000705 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000706 }
707 printf("\n\n");
708}
709
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000710/*
711 * Set the S_CREATOR_OS field. Return true if OS is known,
712 * otherwise, 0.
713 */
714static int set_os(struct ext2_super_block *sb, char *os)
715{
716 if (isdigit (*os))
717 sb->s_creator_os = atoi (os);
718 else if (strcasecmp(os, "linux") == 0)
719 sb->s_creator_os = EXT2_OS_LINUX;
720 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
721 sb->s_creator_os = EXT2_OS_HURD;
722 else if (strcasecmp(os, "masix") == 0)
723 sb->s_creator_os = EXT2_OS_MASIX;
724 else
725 return 0;
726 return 1;
727}
728
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000729#define PATH_SET "PATH=/sbin"
730
Theodore Ts'od163b091997-10-03 17:42:28 +0000731static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000732{
733 char *buf, *token, *next, *p, *arg;
734 int len;
735 int raid_usage = 0;
736
737 len = strlen(opts);
738 buf = malloc(len+1);
739 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000740 fprintf(stderr, _("Couldn't allocate memory to parse "
741 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000742 exit(1);
743 }
744 strcpy(buf, opts);
745 for (token = buf; token && *token; token = next) {
746 p = strchr(token, ',');
747 next = 0;
748 if (p) {
749 *p = 0;
750 next = p+1;
751 }
752 arg = strchr(token, '=');
753 if (arg) {
754 *arg = 0;
755 arg++;
756 }
757 if (strcmp(token, "stride") == 0) {
758 if (!arg) {
759 raid_usage++;
760 continue;
761 }
762 fs_stride = strtoul(arg, &p, 0);
763 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000764 fprintf(stderr,
765 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000766 raid_usage++;
767 continue;
768 }
769 } else
770 raid_usage++;
771 }
772 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000773 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000774 "Raid options are separated by commas, "
775 "and may take an argument which\n"
776 "\tis set off by an equals ('=') sign.\n\n"
777 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000778 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000779 exit(1);
780 }
781}
782
Theodore Ts'o896938d1999-10-23 01:04:50 +0000783static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400784 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
785 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000786 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400787 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
788 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000789 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
790};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000791
792
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793static void PRS(int argc, char *argv[])
794{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400795 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000796 int size;
797 char * tmp;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000798 int blocksize = 0;
799 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600800 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000801 int reserved_ratio = 5;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000802 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000803 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000804 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000805 char * raid_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500806 const char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400807 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000808 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500809#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000810 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000811#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400812 long sysval;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000813
Theodore Ts'o3839e651997-04-26 13:21:57 +0000814 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000815 if (oldpath) {
816 char *newpath;
817
818 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
819 strcpy (newpath, PATH_SET);
820 strcat (newpath, ":");
821 strcat (newpath, oldpath);
822 putenv (newpath);
823 } else
824 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000825
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000826 tmp = getenv("MKE2FS_SYNC");
827 if (tmp)
828 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400829
830 /* Determine the system page size if possible */
831#ifdef HAVE_SYSCONF
832#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
833#define _SC_PAGESIZE _SC_PAGE_SIZE
834#endif
835#ifdef _SC_PAGESIZE
836 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600837 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400838 sys_page_size = sysval;
839#endif /* _SC_PAGESIZE */
840#endif /* HAVE_SYSCONF */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000841
Theodore Ts'o3839e651997-04-26 13:21:57 +0000842 setbuf(stdout, NULL);
843 setbuf(stderr, NULL);
844 initialize_ext2_error_table();
845 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000846 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400847 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
848 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500849#if 0
Theodore Ts'o843049c2002-09-22 15:37:40 -0400850 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500851#endif
Theodore Ts'o843049c2002-09-22 15:37:40 -0400852
Theodore Ts'o756df352002-03-07 20:52:12 -0500853#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000854 if (uname(&ut)) {
855 perror("uname");
856 exit(1);
857 }
858 if ((ut.release[0] == '1') ||
859 (ut.release[0] == '2' && ut.release[1] == '.' &&
860 ut.release[2] < '2' && ut.release[3] == '.')) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000861 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400862 param.s_feature_incompat = 0;
863 param.s_feature_compat = 0;
864 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000865 }
866#endif
Andreas Dilger9d8e6342001-08-31 10:24:46 -0600867 fprintf (stderr, "mke2fs %s (%s)\n",
Theodore Ts'o04a96852001-08-30 21:55:26 -0400868 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700869
870 if (argc && *argv) {
871 program_name = get_progname(*argv);
872
873 /* If called as mkfs.ext3, create a journal inode */
874 if (!strcmp(program_name, "mkfs.ext3"))
875 journal_size = -1;
876 }
877
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000878 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000879 "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 +0000880 switch (c) {
881 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400882 blocksize = strtol(optarg, &tmp, 0);
883 b = (blocksize > 0) ? blocksize : -blocksize;
884 if (b < EXT2_MIN_BLOCK_SIZE ||
885 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000886 com_err(program_name, 0,
887 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000888 exit(1);
889 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600890 if (blocksize > 4096)
891 fprintf(stderr, _("Warning: blocksize %d not "
892 "usable on most systems.\n"),
893 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400894 if (blocksize > 0)
895 param.s_log_block_size =
896 int_log2(blocksize >>
897 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000898 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000899 case 'c': /* Check for bad blocks */
900 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500901 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000902 break;
903 case 'f':
904 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600905 if (size < EXT2_MIN_BLOCK_SIZE ||
906 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000907 com_err(program_name, 0,
908 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000909 optarg);
910 exit(1);
911 }
912 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000913 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500914 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000915 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000916 break;
917 case 'g':
918 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000919 if (*tmp) {
920 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000921 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000922 exit(1);
923 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000924 if ((param.s_blocks_per_group % 8) != 0) {
925 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000926 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927 exit(1);
928 }
929 break;
930 case 'i':
931 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600932 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
933 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000934 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000935 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -0600936 _("bad inode ratio %s (min %d/max %d"),
937 optarg, EXT2_MIN_BLOCK_SIZE,
938 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000939 exit(1);
940 }
941 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000942 case 'J':
943 parse_journal_opts(optarg);
944 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000945 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000946 param.s_feature_compat |=
947 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000948 if (!journal_size)
949 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000950 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000951 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000952 bad_blocks_filename = malloc(strlen(optarg)+1);
953 if (!bad_blocks_filename) {
954 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000955 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000956 exit(1);
957 }
958 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000959 break;
960 case 'm':
961 reserved_ratio = strtoul(optarg, &tmp, 0);
962 if (reserved_ratio > 50 || *tmp) {
963 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000964 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000965 optarg);
966 exit(1);
967 }
968 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000969 case 'n':
970 noaction++;
971 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000972 case 'o':
973 creator_os = optarg;
974 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000975 case 'r':
976 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000977 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400978 param.s_feature_incompat = 0;
979 param.s_feature_compat = 0;
980 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000981 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000982 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000983 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400984 if (atoi(optarg))
985 param.s_feature_ro_compat |=
986 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
987 else
988 param.s_feature_ro_compat &=
989 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000990 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000991#ifdef EXT2_DYNAMIC_REV
992 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -0600993 inode_size = strtoul(optarg, &tmp, 0);
994 if (*tmp) {
995 com_err(program_name, 0,
996 _("bad inode size - %s"), optarg);
997 exit(1);
998 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000999 break;
1000#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001001 case 'N':
1002 num_inodes = atoi(optarg);
1003 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001004 case 'v':
1005 verbose = 1;
1006 break;
1007 case 'q':
1008 quiet = 1;
1009 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001010 case 'F':
1011 force = 1;
1012 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001013 case 'L':
1014 volume_label = optarg;
1015 break;
1016 case 'M':
1017 mount_dir = optarg;
1018 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001019 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001020 if (!strcmp(optarg, "none") || default_features) {
1021 param.s_feature_compat = 0;
1022 param.s_feature_incompat = 0;
1023 param.s_feature_ro_compat = 0;
1024 default_features = 0;
1025 }
1026 if (!strcmp(optarg, "none"))
1027 break;
1028 if (e2p_edit_feature(optarg,
1029 &param.s_feature_compat,
1030 ok_features)) {
1031 fprintf(stderr,
1032 _("Invalid filesystem option set: %s\n"), optarg);
1033 exit(1);
1034 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001035 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001036 case 'R':
1037 raid_opts = optarg;
1038 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001039 case 'S':
1040 super_only = 1;
1041 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001042 case 'T':
1043 fs_type = optarg;
1044 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001045 case 'V':
1046 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001047 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +00001048 error_message(EXT2_ET_BASE));
1049 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001050 default:
1051 usage();
1052 }
1053 if (optind == argc)
1054 usage();
1055 device_name = argv[optind];
1056 optind++;
1057 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001058 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1059
1060 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001061 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001062 argv[optind - 1]);
1063 exit(1);
1064 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001065 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001066 }
1067 if (optind < argc)
1068 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001069
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001070 if (raid_opts)
1071 parse_raid_opts(raid_opts);
1072
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001073 /*
1074 * If there's no blocksize specified and there is a journal
1075 * device, use it to figure out the blocksize
1076 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001077 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001078 ext2_filsys jfs;
1079
1080 retval = ext2fs_open(journal_device,
1081 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1082 0, unix_io_manager, &jfs);
1083 if (retval) {
1084 com_err(program_name, retval,
1085 _("while trying to open journal device %s\n"),
1086 journal_device);
1087 exit(1);
1088 }
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001089 if ((blocksize < 0) && (jfs->blocksize < -blocksize)) {
1090 com_err(program_name, 0,
1091 _("Journal dev blocksize (%d) smaller than"
1092 "minimum blocksize %d\n"), jfs->blocksize,
1093 -blocksize);
1094 exit(1);
1095 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001096 blocksize = jfs->blocksize;
1097 param.s_log_block_size =
1098 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1099 ext2fs_close(jfs);
1100 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001101
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001102 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001103 if (!force) {
1104 com_err(program_name, 0,
1105 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001106 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001107 proceed_question();
1108 }
1109 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1110 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001111 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001112 }
1113
Andreas Dilgerfa724582001-08-04 01:18:34 -06001114 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1115 if (!fs_type)
1116 fs_type = "journal";
1117 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001118 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1119 param.s_feature_compat = 0;
1120 param.s_feature_ro_compat = 0;
1121 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001122 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1123 (param.s_feature_compat || param.s_feature_ro_compat ||
1124 param.s_feature_incompat))
1125 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1126
Theodore Ts'o74becf31997-04-26 14:37:06 +00001127 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001128 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001129 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001130
Theodore Ts'o3839e651997-04-26 13:21:57 +00001131 param.s_log_frag_size = param.s_log_block_size;
1132
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001133 if (noaction && param.s_blocks_count) {
1134 dev_size = param.s_blocks_count;
1135 retval = 0;
1136 } else
1137 retval = ext2fs_get_device_size(device_name,
1138 EXT2_BLOCK_SIZE(&param),
1139 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001140 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1141 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001142 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001143 exit(1);
1144 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001145 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001146 if (retval == EXT2_ET_UNIMPLEMENTED) {
1147 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001148 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001149 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001150 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001151 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001152 } else {
1153 if (dev_size == 0) {
1154 com_err(program_name, 0,
1155 _("Device size reported to be zero. "
1156 "Invalid partition specified, or\n\t"
1157 "partition table wasn't reread "
1158 "after running fdisk, due to\n\t"
1159 "a modified partition being busy "
1160 "and in use. You may need to reboot\n\t"
1161 "to re-read your partition table.\n"
1162 ));
1163 exit(1);
1164 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001165 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001166 }
1167
Theodore Ts'oa789d841998-03-30 01:20:55 +00001168 } else if (!force && (param.s_blocks_count > dev_size)) {
1169 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001170 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001171 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001172 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001173
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001174 /*
1175 * If the user asked for HAS_JOURNAL, then make sure a journal
1176 * gets created.
1177 */
1178 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1179 !journal_size)
1180 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001181
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001182 /* Set first meta blockgroup via an environment variable */
1183 /* (this is mostly for debugging purposes) */
1184 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1185 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1186 param.s_first_meta_bg = atoi(tmp);
1187
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001188 set_fs_defaults(fs_type, &param, blocksize, &inode_ratio);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001189 blocksize = EXT2_BLOCK_SIZE(&param);
1190
Theodore Ts'o521e3681997-04-29 17:48:10 +00001191 if (param.s_blocks_per_group) {
1192 if (param.s_blocks_per_group < 256 ||
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001193 param.s_blocks_per_group > 8 * blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001194 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001195 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001196 exit(1);
1197 }
1198 }
1199
Andreas Dilger932a4892002-05-16 03:20:07 -06001200 if (inode_size) {
1201 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1202 inode_size > EXT2_BLOCK_SIZE(&param) ||
1203 inode_size & (inode_size - 1)) {
1204 com_err(program_name, 0,
1205 _("bad inode size %d (min %d/max %d)"),
1206 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001207 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001208 exit(1);
1209 }
1210 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1211 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1212 "on most systems\n"),
1213 inode_size);
1214 param.s_inode_size = inode_size;
1215 }
1216
Theodore Ts'o3839e651997-04-26 13:21:57 +00001217 /*
1218 * Calculate number of inodes based on the inode ratio
1219 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001220 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001221 ((__u64) param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001222 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001223
1224 /*
1225 * Calculate number of blocks to reserve
1226 */
1227 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001228
Theodore Ts'o3839e651997-04-26 13:21:57 +00001229}
1230
1231int main (int argc, char *argv[])
1232{
1233 errcode_t retval = 0;
1234 ext2_filsys fs;
1235 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001236 int journal_blocks;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001237 int i, val;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001238
1239#ifdef ENABLE_NLS
1240 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001241 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001242 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1243 textdomain(NLS_CAT_NAME);
1244#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001245 PRS(argc, argv);
1246
Theodore Ts'o3839e651997-04-26 13:21:57 +00001247 /*
1248 * Initialize the superblock....
1249 */
1250 retval = ext2fs_initialize(device_name, 0, &param,
1251 unix_io_manager, &fs);
1252 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001253 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001254 exit(1);
1255 }
1256
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001257 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001258 * Wipe out the old on-disk superblock
1259 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001260 if (!noaction)
1261 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001262
1263 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001264 * Generate a UUID for it...
1265 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001266 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001267
1268 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001269 * Initialize the directory index variables
1270 */
1271 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1272 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1273
1274 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001275 * Add "jitter" to the superblock's check interval so that we
1276 * don't check all the filesystems at the same time. We use a
1277 * kludgy hack of using the UUID to derive a random jitter value.
1278 */
1279 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1280 val += fs->super->s_uuid[i];
1281 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1282
1283 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001284 * Override the creator OS, if applicable
1285 */
1286 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001287 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001288 exit(1);
1289 }
1290
1291 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001292 * For the Hurd, we will turn off filetype since it doesn't
1293 * support it.
1294 */
1295 if (fs->super->s_creator_os == EXT2_OS_HURD)
1296 fs->super->s_feature_incompat &=
1297 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1298
1299 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001300 * Set the volume label...
1301 */
1302 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001303 memset(fs->super->s_volume_name, 0,
1304 sizeof(fs->super->s_volume_name));
1305 strncpy(fs->super->s_volume_name, volume_label,
1306 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001307 }
1308
1309 /*
1310 * Set the last mount directory
1311 */
1312 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001313 memset(fs->super->s_last_mounted, 0,
1314 sizeof(fs->super->s_last_mounted));
1315 strncpy(fs->super->s_last_mounted, mount_dir,
1316 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001317 }
1318
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001319 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001320 show_stats(fs);
1321
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001322 if (noaction)
1323 exit(0);
1324
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001325 if (fs->super->s_feature_incompat &
1326 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1327 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001328 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001329 }
1330
Theodore Ts'o3839e651997-04-26 13:21:57 +00001331 if (bad_blocks_filename)
1332 read_bb_file(fs, &bb_list, bad_blocks_filename);
1333 if (cflag)
1334 test_disk(fs, &bb_list);
1335
1336 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001337 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001338 retval = ext2fs_allocate_tables(fs);
1339 if (retval) {
1340 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001341 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001342 exit(1);
1343 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001344 if (super_only) {
1345 fs->super->s_state |= EXT2_ERROR_FS;
1346 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1347 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001348 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001349 int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001350 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001351 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001352 blk_t ret_blk;
1353
1354#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001355 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001356#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001357
1358 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001359 * Wipe out any old MD RAID (or other) metadata at the end
1360 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001361 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001362 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001363 start = (blocks & ~(rsv - 1));
1364 if (start > rsv)
1365 start -= rsv;
1366 if (start > 0)
1367 retval = zero_blocks(fs, start, blocks - start,
1368 NULL, &ret_blk, NULL);
1369
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001370 if (retval) {
1371 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001372 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001373 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001374 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001375 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001376 create_root_dir(fs);
1377 create_lost_and_found(fs);
1378 reserve_inodes(fs);
1379 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001380 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001381
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001382 if (journal_device) {
1383 ext2_filsys jfs;
1384
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001385 if (!force)
1386 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001387 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001388
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001389 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1390 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1391 fs->blocksize, unix_io_manager, &jfs);
1392 if (retval) {
1393 com_err(program_name, retval,
1394 _("while trying to open journal device %s\n"),
1395 journal_device);
1396 exit(1);
1397 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001398 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001399 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001400 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001401 fflush(stdout);
1402 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001403 retval = ext2fs_add_journal_device(fs, jfs);
1404 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001405 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001406 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001407 journal_device);
1408 exit(1);
1409 }
1410 if (!quiet)
1411 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001412 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001413 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001414 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001415 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001416
1417 if (!journal_blocks) {
1418 fs->super->s_feature_compat &=
1419 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1420 goto no_journal;
1421 }
1422 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001423 printf(_("Creating journal (%d blocks): "),
1424 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001425 fflush(stdout);
1426 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001427 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1428 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001429 if (retval) {
1430 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001431 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001432 exit(1);
1433 }
1434 if (!quiet)
1435 printf(_("done\n"));
1436 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001437no_journal:
1438
Theodore Ts'o3839e651997-04-26 13:21:57 +00001439 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001440 printf(_("Writing superblocks and "
1441 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001442 retval = ext2fs_flush(fs);
1443 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001444 fprintf(stderr,
1445 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001446 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001447 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001448 printf(_("done\n\n"));
Theodore Ts'o66cf2f62001-06-14 06:42:44 +00001449 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001450 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001451 val = ext2fs_close(fs);
1452 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001453}