blob: fcc9eec7da082d92073a6625d4ffd7900fce2fe1 [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;
Theodore Ts'od99225e2004-09-25 07:40:12 -040088int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040089
Theodore Ts'o63985322001-01-03 17:02:13 +000090static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000091{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000092 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000093 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000094 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
95 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000096 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +000097 "[-r fs-revision] [-R raid_opts] [-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000098 program_name);
99 exit(1);
100}
101
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000102static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103{
104 int l = 0;
105
106 arg >>= 1;
107 while (arg) {
108 l++;
109 arg >>= 1;
110 }
111 return l;
112}
113
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000114static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000115{
116 int l;
117
118 for (l=0; arg ; l++)
119 arg = arg / 10;
120 return l;
121}
122
Theodore Ts'od99225e2004-09-25 07:40:12 -0400123static int parse_version_number(const char *s)
124{
125 int major, minor, rev;
126 char *endptr;
127 const char *cp = s;
128
129 if (!s)
130 return 0;
131 major = strtol(cp, &endptr, 10);
132 if (cp == endptr || *endptr != '.')
133 return 0;
134 cp = endptr + 1;
135 minor = strtol(cp, &endptr, 10);
136 if (cp == endptr || *endptr != '.')
137 return 0;
138 cp = endptr + 1;
139 rev = strtol(cp, &endptr, 10);
140 if (cp == endptr)
141 return 0;
142 return ((((major * 256) + minor) * 256) + rev);
143}
144
145
146
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000148 * This function sets the default parameters for a filesystem
149 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000150 * The type is specified by the user. The size is the maximum size
151 * (in megabytes) for which a set of parameters applies, with a size
152 * of zero meaning that it is the default parameter for the type.
153 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000154 */
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400155#define DEF_MAX_BLOCKSIZE -1
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000156static char default_str[] = "default";
157struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000158 const char *type;
159 int size;
160 int blocksize;
161 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000162} settings[] = {
163 { default_str, 0, 4096, 8192 },
164 { default_str, 512, 1024, 4096 },
165 { default_str, 3, 1024, 8192 },
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000166 { "journal", 0, 4096, 8192 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000167 { "news", 0, 4096, 4096 },
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400168 { "largefile", 0, DEF_MAX_BLOCKSIZE, 1024 * 1024 },
169 { "largefile4", 0, DEF_MAX_BLOCKSIZE, 4096 * 1024 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000170 { 0, 0, 0, 0},
171};
172
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500173static void set_fs_defaults(const char *fs_type,
174 struct ext2_super_block *super,
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400175 int blocksize, int sector_size,
176 int *inode_ratio)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000177{
178 int megs;
179 int ratio = 0;
180 struct mke2fs_defaults *p;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400181 int use_bsize = 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000182
Andreas Dilger932a4892002-05-16 03:20:07 -0600183 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000184 if (inode_ratio)
185 ratio = *inode_ratio;
186 if (!fs_type)
187 fs_type = default_str;
188 for (p = settings; p->type; p++) {
189 if ((strcmp(p->type, fs_type) != 0) &&
190 (strcmp(p->type, default_str) != 0))
191 continue;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400192 if ((p->size != 0) && (megs > p->size))
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000193 continue;
194 if (ratio == 0)
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600195 *inode_ratio = p->inode_ratio < blocksize ?
196 blocksize : p->inode_ratio;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400197 use_bsize = p->blocksize;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000198 }
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400199 if (blocksize <= 0) {
Theodore Ts'od99225e2004-09-25 07:40:12 -0400200 if (use_bsize == DEF_MAX_BLOCKSIZE) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400201 use_bsize = sys_page_size;
Theodore Ts'od99225e2004-09-25 07:40:12 -0400202 if ((linux_version_code < (2*65536 + 6*256)) &&
203 (use_bsize > 4096))
204 use_bsize = 4096;
205 }
Theodore Ts'o79120f82003-08-31 20:56:57 -0400206 if (sector_size && use_bsize < sector_size)
207 use_bsize = sector_size;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400208 if ((blocksize < 0) && (use_bsize < (-blocksize)))
209 use_bsize = -blocksize;
210 blocksize = use_bsize;
211 super->s_blocks_count /= blocksize / 1024;
212 }
213 super->s_log_frag_size = super->s_log_block_size =
214 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000215}
216
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400217
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000218/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000219 * Helper function for read_bb_file and test_disk
220 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500221static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222{
Theodore Ts'o66938372002-03-08 00:14:46 -0500223 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224 return;
225}
226
227/*
228 * Reads the bad blocks list from a file
229 */
230static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
231 const char *bad_blocks_file)
232{
233 FILE *f;
234 errcode_t retval;
235
236 f = fopen(bad_blocks_file, "r");
237 if (!f) {
238 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000239 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 exit(1);
241 }
242 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
243 fclose (f);
244 if (retval) {
245 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000246 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 exit(1);
248 }
249}
250
251/*
252 * Runs the badblocks program to test the disk
253 */
254static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
255{
256 FILE *f;
257 errcode_t retval;
258 char buf[1024];
259
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500260 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
261 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
262 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000264 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 f = popen(buf, "r");
266 if (!f) {
267 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000268 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269 exit(1);
270 }
271 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000272 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273 if (retval) {
274 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000275 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 exit(1);
277 }
278}
279
280static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
281{
Theodore Ts'o54434922003-12-07 01:28:50 -0500282 dgrp_t i;
283 blk_t j;
284 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285 blk_t blk;
286 badblocks_iterate bb_iter;
287 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000288 blk_t group_block;
289 int group;
290 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291
292 if (!bb_list)
293 return;
294
295 /*
296 * The primary superblock and group descriptors *must* be
297 * good; if not, abort.
298 */
299 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
300 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000301 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000302 fprintf(stderr, _("Block %d in primary "
303 "superblock/group descriptor area bad.\n"), i);
304 fprintf(stderr, _("Blocks %d through %d must be good "
305 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000306 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500307 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 exit(1);
309 }
310 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000311
312 /*
313 * See if any of the bad blocks are showing up in the backup
314 * superblocks and/or group descriptors. If so, issue a
315 * warning and adjust the block counts appropriately.
316 */
317 group_block = fs->super->s_first_data_block +
318 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000319
320 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000321 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000322 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000323 if (ext2fs_badblocks_list_test(bb_list,
324 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000325 if (!group_bad)
326 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000327_("Warning: the backup superblock/group descriptors at block %d contain\n"
328" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000329 group_block);
330 group_bad++;
331 group = ext2fs_group_of_blk(fs, group_block+j);
332 fs->group_desc[group].bg_free_blocks_count++;
333 fs->super->s_free_blocks_count++;
334 }
335 }
336 group_block += fs->super->s_blocks_per_group;
337 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000338
339 /*
340 * Mark all the bad blocks as used...
341 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000342 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000343 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000344 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000345 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346 exit(1);
347 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000348 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000349 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000350 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000351}
352
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000353/*
354 * These functions implement a generalized progress meter.
355 */
356struct progress_struct {
357 char format[20];
358 char backup[80];
359 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400360 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000361};
362
363static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500364 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000365{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000366 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000367
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000368 memset(progress, 0, sizeof(struct progress_struct));
369 if (quiet)
370 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000371
372 /*
373 * Figure out how many digits we need
374 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000375 i = int_log10(max);
376 sprintf(progress->format, "%%%dd/%%%dld", i, i);
377 memset(progress->backup, '\b', sizeof(progress->backup)-1);
378 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500379 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000380 progress->backup[(2*i)+1] = 0;
381 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000382
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400383 progress->skip_progress = 0;
384 if (getenv("MKE2FS_SKIP_PROGRESS"))
385 progress->skip_progress++;
386
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000387 fputs(label, stdout);
388 fflush(stdout);
389}
390
391static void progress_update(struct progress_struct *progress, __u32 val)
392{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400393 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000394 return;
395 printf(progress->format, val, progress->max);
396 fputs(progress->backup, stdout);
397}
398
399static void progress_close(struct progress_struct *progress)
400{
401 if (progress->format[0] == 0)
402 return;
403 fputs(_("done \n"), stdout);
404}
405
406
407/*
408 * Helper function which zeros out _num_ blocks starting at _blk_. In
409 * case of an error, the details of the error is returned via _ret_blk_
410 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
411 * success, and an error code on an error.
412 *
413 * As a special case, if the first argument is NULL, then it will
414 * attempt to free the static zeroizing buffer. (This is to keep
415 * programs that check for memory leaks happy.)
416 */
417static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
418 struct progress_struct *progress,
419 blk_t *ret_blk, int *ret_count)
420{
421 int j, count, next_update, next_update_incr;
422 static char *buf;
423 errcode_t retval;
424
425 /* If fs is null, clean up the static buffer and return */
426 if (!fs) {
427 if (buf) {
428 free(buf);
429 buf = 0;
430 }
431 return 0;
432 }
433 /* Allocate the zeroizing buffer if necessary */
434 if (!buf) {
435 buf = malloc(fs->blocksize * STRIDE_LENGTH);
436 if (!buf) {
437 com_err("malloc", ENOMEM,
438 _("while allocating zeroizing buffer"));
439 exit(1);
440 }
441 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
442 }
443 /* OK, do the write loop */
444 next_update = 0;
445 next_update_incr = num / 100;
446 if (next_update_incr < 1)
447 next_update_incr = 1;
448 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400449 count = num - j;
450 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000451 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000452 retval = io_channel_write_blk(fs->io, blk, count, buf);
453 if (retval) {
454 if (ret_count)
455 *ret_count = count;
456 if (ret_blk)
457 *ret_blk = blk;
458 return retval;
459 }
460 if (progress && j > next_update) {
461 next_update += num / 100;
462 progress_update(progress, blk);
463 }
464 }
465 return 0;
466}
467
468static void write_inode_tables(ext2_filsys fs)
469{
470 errcode_t retval;
471 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500472 dgrp_t i;
473 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000474 struct progress_struct progress;
475
476 if (quiet)
477 memset(&progress, 0, sizeof(progress));
478 else
479 progress_init(&progress, _("Writing inode tables: "),
480 fs->group_desc_count);
481
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000483 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000485 blk = fs->group_desc[i].bg_inode_table;
486 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000487
488 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
489 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500490 fprintf(stderr, _("\nCould not write %d blocks "
491 "in inode table starting at %d: %s\n"),
492 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000493 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000494 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000495 if (sync_kludge) {
496 if (sync_kludge == 1)
497 sync();
498 else if ((i % sync_kludge) == 0)
499 sync();
500 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000502 zero_blocks(0, 0, 0, 0, 0, 0);
503 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504}
505
506static void create_root_dir(ext2_filsys fs)
507{
508 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000509 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510
511 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
512 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000513 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 exit(1);
515 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000516 if (geteuid()) {
517 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
518 if (retval) {
519 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000520 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000521 exit(1);
522 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000523 inode.i_uid = getuid();
524 if (inode.i_uid)
525 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000526 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
527 if (retval) {
528 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000529 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000530 exit(1);
531 }
532 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533}
534
535static void create_lost_and_found(ext2_filsys fs)
536{
537 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000538 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 const char *name = "lost+found";
540 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000541 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542
Theodore Ts'o6a525062001-12-24 09:40:00 -0500543 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
545 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000546 com_err("ext2fs_mkdir", retval,
547 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548 exit(1);
549 }
550
551 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
552 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000553 com_err("ext2_lookup", retval,
554 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 exit(1);
556 }
557
558 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000559 if ((lpf_size += fs->blocksize) >= 16*1024)
560 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561 retval = ext2fs_expand_dir(fs, ino);
562 if (retval) {
563 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000564 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000565 exit(1);
566 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500567 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568}
569
570static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
571{
572 errcode_t retval;
573
Theodore Ts'of3db3561997-04-26 13:34:30 +0000574 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000575 fs->group_desc[0].bg_free_inodes_count--;
576 fs->super->s_free_inodes_count--;
577 retval = ext2fs_update_bb_inode(fs, bb_list);
578 if (retval) {
579 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000580 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000581 exit(1);
582 }
583
584}
585
586static void reserve_inodes(ext2_filsys fs)
587{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000588 ext2_ino_t i;
589 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000590
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000591 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000592 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000593 group = ext2fs_group_of_ino(fs, i);
594 fs->group_desc[group].bg_free_inodes_count--;
595 fs->super->s_free_inodes_count--;
596 }
597 ext2fs_mark_ib_dirty(fs);
598}
599
Theodore Ts'o756df352002-03-07 20:52:12 -0500600#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500601#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500602#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500603
Theodore Ts'o04a96852001-08-30 21:55:26 -0400604static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000605{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400606 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000607 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500608 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000609
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400610 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600611 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500612 printf(_("Out of memory erasing sectors %d-%d\n"),
613 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600614 exit(1);
615 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500616
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500617 if (sect == 0) {
618 /* Check for a BSD disklabel, and don't erase it if so */
619 retval = io_channel_read_blk(fs->io, 0, -512, buf);
620 if (retval)
621 fprintf(stderr,
622 _("Warning: could not read block 0: %s\n"),
623 error_message(retval));
624 else {
625 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
626 if ((*magic == BSD_DISKMAGIC) ||
627 (*magic == BSD_MAGICDISK))
628 return;
629 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500630 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500631
Theodore Ts'o70988102002-07-14 08:00:00 -0400632 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000633 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400634 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000635 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400636 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000637 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500638 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
639 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000640}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000641
642static void create_journal_dev(ext2_filsys fs)
643{
644 struct progress_struct progress;
645 errcode_t retval;
646 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000647 blk_t blk;
648 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000649
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000650 retval = ext2fs_create_journal_superblock(fs,
651 fs->super->s_blocks_count, 0, &buf);
652 if (retval) {
653 com_err("create_journal_dev", retval,
654 _("while initializing journal superblock"));
655 exit(1);
656 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000657 if (quiet)
658 memset(&progress, 0, sizeof(progress));
659 else
660 progress_init(&progress, _("Zeroing journal device: "),
661 fs->super->s_blocks_count);
662
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000663 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
664 &progress, &blk, &count);
665 if (retval) {
666 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000667 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000668 blk, count);
669 exit(1);
670 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000671 zero_blocks(0, 0, 0, 0, 0, 0);
672
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000673 retval = io_channel_write_blk(fs->io,
674 fs->super->s_first_data_block+1,
675 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000676 if (retval) {
677 com_err("create_journal_dev", retval,
678 _("while writing journal superblock"));
679 exit(1);
680 }
681 progress_close(&progress);
682}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000683
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684static void show_stats(ext2_filsys fs)
685{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000686 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000687 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500689 dgrp_t i;
690 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691
692 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'o66938372002-03-08 00:14:46 -0500693 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000695
696 memset(buf, 0, sizeof(buf));
697 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000698 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500699 fputs(_("OS type: "), stdout);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000700 switch (fs->super->s_creator_os) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500701 case EXT2_OS_LINUX: fputs("Linux", stdout); break;
702 case EXT2_OS_HURD: fputs("GNU/Hurd", stdout); break;
703 case EXT2_OS_MASIX: fputs ("Masix", stdout); break;
704 default: fputs(_("(unknown os)"), stdout);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000705 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000706 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000707 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000708 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000709 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000710 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000711 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000713 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 s->s_r_blocks_count,
715 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000716 printf(_("First data block=%u\n"), s->s_first_data_block);
717 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000718 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000719 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000720 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000721 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000722 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000723 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000724
725 if (fs->group_desc_count == 1) {
726 printf("\n");
727 return;
728 }
729
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000730 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000731 group_block = s->s_first_data_block;
732 col_left = 0;
733 for (i = 1; i < fs->group_desc_count; i++) {
734 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000735 if (!ext2fs_bg_has_super(fs, i))
736 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000737 if (i != 1)
738 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000739 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000740 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000741 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000742 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000743 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000744 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000745 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000746 }
747 printf("\n\n");
748}
749
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000750/*
751 * Set the S_CREATOR_OS field. Return true if OS is known,
752 * otherwise, 0.
753 */
754static int set_os(struct ext2_super_block *sb, char *os)
755{
756 if (isdigit (*os))
757 sb->s_creator_os = atoi (os);
758 else if (strcasecmp(os, "linux") == 0)
759 sb->s_creator_os = EXT2_OS_LINUX;
760 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
761 sb->s_creator_os = EXT2_OS_HURD;
762 else if (strcasecmp(os, "masix") == 0)
763 sb->s_creator_os = EXT2_OS_MASIX;
764 else
765 return 0;
766 return 1;
767}
768
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000769#define PATH_SET "PATH=/sbin"
770
Theodore Ts'od163b091997-10-03 17:42:28 +0000771static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000772{
773 char *buf, *token, *next, *p, *arg;
774 int len;
775 int raid_usage = 0;
776
777 len = strlen(opts);
778 buf = malloc(len+1);
779 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000780 fprintf(stderr, _("Couldn't allocate memory to parse "
781 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000782 exit(1);
783 }
784 strcpy(buf, opts);
785 for (token = buf; token && *token; token = next) {
786 p = strchr(token, ',');
787 next = 0;
788 if (p) {
789 *p = 0;
790 next = p+1;
791 }
792 arg = strchr(token, '=');
793 if (arg) {
794 *arg = 0;
795 arg++;
796 }
797 if (strcmp(token, "stride") == 0) {
798 if (!arg) {
799 raid_usage++;
800 continue;
801 }
802 fs_stride = strtoul(arg, &p, 0);
803 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000804 fprintf(stderr,
805 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000806 raid_usage++;
807 continue;
808 }
809 } else
810 raid_usage++;
811 }
812 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000813 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000814 "Raid options are separated by commas, "
815 "and may take an argument which\n"
816 "\tis set off by an equals ('=') sign.\n\n"
817 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000818 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000819 exit(1);
820 }
821}
822
Theodore Ts'o896938d1999-10-23 01:04:50 +0000823static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400824 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
825 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000826 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400827 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
828 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000829 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
830};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000831
832
Theodore Ts'o3839e651997-04-26 13:21:57 +0000833static void PRS(int argc, char *argv[])
834{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400835 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000836 int size;
837 char * tmp;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000838 int blocksize = 0;
839 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600840 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000841 int reserved_ratio = 5;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400842 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500843 int show_version_only = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000844 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000845 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000846 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000847 char * raid_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500848 const char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400849 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000850 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500851#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000852 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000853#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400854 long sysval;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000855
Theodore Ts'o3839e651997-04-26 13:21:57 +0000856 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000857 if (oldpath) {
858 char *newpath;
859
860 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
861 strcpy (newpath, PATH_SET);
862 strcat (newpath, ":");
863 strcat (newpath, oldpath);
864 putenv (newpath);
865 } else
866 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000867
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000868 tmp = getenv("MKE2FS_SYNC");
869 if (tmp)
870 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400871
872 /* Determine the system page size if possible */
873#ifdef HAVE_SYSCONF
874#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
875#define _SC_PAGESIZE _SC_PAGE_SIZE
876#endif
877#ifdef _SC_PAGESIZE
878 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600879 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400880 sys_page_size = sysval;
881#endif /* _SC_PAGESIZE */
882#endif /* HAVE_SYSCONF */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000883
Theodore Ts'o3839e651997-04-26 13:21:57 +0000884 setbuf(stdout, NULL);
885 setbuf(stderr, NULL);
886 initialize_ext2_error_table();
887 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000888 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400889 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
890 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500891#if 0
Theodore Ts'o843049c2002-09-22 15:37:40 -0400892 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500893#endif
Theodore Ts'o843049c2002-09-22 15:37:40 -0400894
Theodore Ts'o756df352002-03-07 20:52:12 -0500895#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000896 if (uname(&ut)) {
897 perror("uname");
898 exit(1);
899 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400900 linux_version_code = parse_version_number(ut.release);
901 if (linux_version_code && linux_version_code < (2*65536 + 2*256)) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000902 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400903 param.s_feature_incompat = 0;
904 param.s_feature_compat = 0;
905 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000906 }
907#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700908
909 if (argc && *argv) {
910 program_name = get_progname(*argv);
911
912 /* If called as mkfs.ext3, create a journal inode */
913 if (!strcmp(program_name, "mkfs.ext3"))
914 journal_size = -1;
915 }
916
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000917 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000918 "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 +0000919 switch (c) {
920 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400921 blocksize = strtol(optarg, &tmp, 0);
922 b = (blocksize > 0) ? blocksize : -blocksize;
923 if (b < EXT2_MIN_BLOCK_SIZE ||
924 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000925 com_err(program_name, 0,
926 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927 exit(1);
928 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600929 if (blocksize > 4096)
930 fprintf(stderr, _("Warning: blocksize %d not "
931 "usable on most systems.\n"),
932 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400933 if (blocksize > 0)
934 param.s_log_block_size =
935 int_log2(blocksize >>
936 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000937 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000938 case 'c': /* Check for bad blocks */
939 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500940 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000941 break;
942 case 'f':
943 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600944 if (size < EXT2_MIN_BLOCK_SIZE ||
945 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000946 com_err(program_name, 0,
947 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000948 optarg);
949 exit(1);
950 }
951 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000952 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500953 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000954 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000955 break;
956 case 'g':
957 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000958 if (*tmp) {
959 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000960 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000961 exit(1);
962 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000963 if ((param.s_blocks_per_group % 8) != 0) {
964 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000965 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000966 exit(1);
967 }
968 break;
969 case 'i':
970 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600971 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
972 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000973 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000974 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -0600975 _("bad inode ratio %s (min %d/max %d"),
976 optarg, EXT2_MIN_BLOCK_SIZE,
977 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000978 exit(1);
979 }
980 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000981 case 'J':
982 parse_journal_opts(optarg);
983 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000984 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000985 param.s_feature_compat |=
986 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000987 if (!journal_size)
988 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000989 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000990 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000991 bad_blocks_filename = malloc(strlen(optarg)+1);
992 if (!bad_blocks_filename) {
993 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000994 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000995 exit(1);
996 }
997 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000998 break;
999 case 'm':
1000 reserved_ratio = strtoul(optarg, &tmp, 0);
1001 if (reserved_ratio > 50 || *tmp) {
1002 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001003 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001004 optarg);
1005 exit(1);
1006 }
1007 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001008 case 'n':
1009 noaction++;
1010 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001011 case 'o':
1012 creator_os = optarg;
1013 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001014 case 'r':
1015 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001016 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001017 param.s_feature_incompat = 0;
1018 param.s_feature_compat = 0;
1019 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001020 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001021 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001022 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001023 if (atoi(optarg))
1024 param.s_feature_ro_compat |=
1025 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1026 else
1027 param.s_feature_ro_compat &=
1028 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001029 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001030#ifdef EXT2_DYNAMIC_REV
1031 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001032 inode_size = strtoul(optarg, &tmp, 0);
1033 if (*tmp) {
1034 com_err(program_name, 0,
1035 _("bad inode size - %s"), optarg);
1036 exit(1);
1037 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001038 break;
1039#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001040 case 'N':
1041 num_inodes = atoi(optarg);
1042 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001043 case 'v':
1044 verbose = 1;
1045 break;
1046 case 'q':
1047 quiet = 1;
1048 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001049 case 'F':
1050 force = 1;
1051 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001052 case 'L':
1053 volume_label = optarg;
1054 break;
1055 case 'M':
1056 mount_dir = optarg;
1057 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001058 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001059 if (!strcmp(optarg, "none") || default_features) {
1060 param.s_feature_compat = 0;
1061 param.s_feature_incompat = 0;
1062 param.s_feature_ro_compat = 0;
1063 default_features = 0;
1064 }
1065 if (!strcmp(optarg, "none"))
1066 break;
1067 if (e2p_edit_feature(optarg,
1068 &param.s_feature_compat,
1069 ok_features)) {
1070 fprintf(stderr,
1071 _("Invalid filesystem option set: %s\n"), optarg);
1072 exit(1);
1073 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001074 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001075 case 'R':
1076 raid_opts = optarg;
1077 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001078 case 'S':
1079 super_only = 1;
1080 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001081 case 'T':
1082 fs_type = optarg;
1083 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001084 case 'V':
1085 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001086 show_version_only++;
1087 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001088 default:
1089 usage();
1090 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001091 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001092 usage();
1093 device_name = argv[optind];
1094 optind++;
1095 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001096 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1097
1098 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001099 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001100 argv[optind - 1]);
1101 exit(1);
1102 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001103 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001104 }
1105 if (optind < argc)
1106 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001107
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001108 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001109 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1110 E2FSPROGS_DATE);
1111
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001112 if (show_version_only) {
1113 fprintf(stderr, _("\tUsing %s\n"),
1114 error_message(EXT2_ET_BASE));
1115 exit(0);
1116 }
1117
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001118 if (raid_opts)
1119 parse_raid_opts(raid_opts);
1120
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001121 /*
1122 * If there's no blocksize specified and there is a journal
1123 * device, use it to figure out the blocksize
1124 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001125 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001126 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001127 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001128
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001129#ifdef CONFIG_TESTIO_DEBUG
1130 io_ptr = test_io_manager;
1131 test_io_backing_manager = unix_io_manager;
1132#else
1133 io_ptr = unix_io_manager;
1134#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001135 retval = ext2fs_open(journal_device,
1136 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001137 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001138 if (retval) {
1139 com_err(program_name, retval,
1140 _("while trying to open journal device %s\n"),
1141 journal_device);
1142 exit(1);
1143 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001144 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001145 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001146 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001147 "minimum blocksize %d\n"), jfs->blocksize,
1148 -blocksize);
1149 exit(1);
1150 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001151 blocksize = jfs->blocksize;
1152 param.s_log_block_size =
1153 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1154 ext2fs_close(jfs);
1155 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001156
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001157 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001158 if (!force) {
1159 com_err(program_name, 0,
1160 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001161 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001162 proceed_question();
1163 }
1164 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1165 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001166 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001167 }
Theodore Ts'ocf7d5f12003-07-25 17:39:51 -04001168 if ((blocksize > 4096) &&
1169 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1170 fprintf(stderr, "\nWarning: some 2.4 kernels do not support "
1171 "blocksizes greater than 4096 \n\tusing ext3."
1172 " Use -b 4096 if this is an issue for you.\n\n");
Andreas Dilger932a4892002-05-16 03:20:07 -06001173
Andreas Dilgerfa724582001-08-04 01:18:34 -06001174 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1175 if (!fs_type)
1176 fs_type = "journal";
1177 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001178 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1179 param.s_feature_compat = 0;
1180 param.s_feature_ro_compat = 0;
1181 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001182 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1183 (param.s_feature_compat || param.s_feature_ro_compat ||
1184 param.s_feature_incompat))
1185 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1186
Theodore Ts'o74becf31997-04-26 14:37:06 +00001187 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001188 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001189 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001190
Theodore Ts'o3839e651997-04-26 13:21:57 +00001191 param.s_log_frag_size = param.s_log_block_size;
1192
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001193 if (noaction && param.s_blocks_count) {
1194 dev_size = param.s_blocks_count;
1195 retval = 0;
1196 } else
1197 retval = ext2fs_get_device_size(device_name,
1198 EXT2_BLOCK_SIZE(&param),
1199 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001200 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1201 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001202 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001203 exit(1);
1204 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001205 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001206 if (retval == EXT2_ET_UNIMPLEMENTED) {
1207 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001208 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001209 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001210 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001211 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001212 } else {
1213 if (dev_size == 0) {
1214 com_err(program_name, 0,
1215 _("Device size reported to be zero. "
1216 "Invalid partition specified, or\n\t"
1217 "partition table wasn't reread "
1218 "after running fdisk, due to\n\t"
1219 "a modified partition being busy "
1220 "and in use. You may need to reboot\n\t"
1221 "to re-read your partition table.\n"
1222 ));
1223 exit(1);
1224 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001225 param.s_blocks_count = dev_size;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001226 if (sys_page_size > EXT2_BLOCK_SIZE(&param))
1227 param.s_blocks_count &= ~((sys_page_size /
1228 EXT2_BLOCK_SIZE(&param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001229 }
1230
Theodore Ts'oa789d841998-03-30 01:20:55 +00001231 } else if (!force && (param.s_blocks_count > dev_size)) {
1232 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001233 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001234 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001235 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001236
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001237 /*
1238 * If the user asked for HAS_JOURNAL, then make sure a journal
1239 * gets created.
1240 */
1241 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1242 !journal_size)
1243 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001244
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001245 /* Set first meta blockgroup via an environment variable */
1246 /* (this is mostly for debugging purposes) */
1247 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1248 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1249 param.s_first_meta_bg = atoi(tmp);
1250
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001251 /* Get the hardware sector size, if available */
1252 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1253 if (retval) {
1254 com_err(program_name, retval,
1255 _("while trying to determine hardware sector size"));
1256 exit(1);
1257 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001258
Theodore Ts'o54434922003-12-07 01:28:50 -05001259 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001260 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001261
1262 set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001263 blocksize = EXT2_BLOCK_SIZE(&param);
1264
Theodore Ts'o521e3681997-04-29 17:48:10 +00001265 if (param.s_blocks_per_group) {
1266 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o54434922003-12-07 01:28:50 -05001267 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001268 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001269 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001270 exit(1);
1271 }
1272 }
1273
Andreas Dilger932a4892002-05-16 03:20:07 -06001274 if (inode_size) {
1275 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1276 inode_size > EXT2_BLOCK_SIZE(&param) ||
1277 inode_size & (inode_size - 1)) {
1278 com_err(program_name, 0,
1279 _("bad inode size %d (min %d/max %d)"),
1280 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001281 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001282 exit(1);
1283 }
1284 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1285 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1286 "on most systems\n"),
1287 inode_size);
1288 param.s_inode_size = inode_size;
1289 }
1290
Theodore Ts'o3839e651997-04-26 13:21:57 +00001291 /*
1292 * Calculate number of inodes based on the inode ratio
1293 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001294 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001295 ((__u64) param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001296 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001297
1298 /*
1299 * Calculate number of blocks to reserve
1300 */
1301 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001302
Theodore Ts'o3839e651997-04-26 13:21:57 +00001303}
1304
1305int main (int argc, char *argv[])
1306{
1307 errcode_t retval = 0;
1308 ext2_filsys fs;
1309 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001310 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001311 unsigned int i;
1312 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001313 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001314
1315#ifdef ENABLE_NLS
1316 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001317 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001318 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1319 textdomain(NLS_CAT_NAME);
1320#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001321 PRS(argc, argv);
1322
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001323#ifdef CONFIG_TESTIO_DEBUG
1324 io_ptr = test_io_manager;
1325 test_io_backing_manager = unix_io_manager;
1326#else
1327 io_ptr = unix_io_manager;
1328#endif
1329
Theodore Ts'o3839e651997-04-26 13:21:57 +00001330 /*
1331 * Initialize the superblock....
1332 */
1333 retval = ext2fs_initialize(device_name, 0, &param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001334 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001335 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001336 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001337 exit(1);
1338 }
1339
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001340 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001341 * Wipe out the old on-disk superblock
1342 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001343 if (!noaction)
1344 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001345
1346 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001347 * Generate a UUID for it...
1348 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001349 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001350
1351 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001352 * Initialize the directory index variables
1353 */
1354 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1355 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1356
1357 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001358 * Add "jitter" to the superblock's check interval so that we
1359 * don't check all the filesystems at the same time. We use a
1360 * kludgy hack of using the UUID to derive a random jitter value.
1361 */
1362 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1363 val += fs->super->s_uuid[i];
1364 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1365
1366 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001367 * Override the creator OS, if applicable
1368 */
1369 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001370 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001371 exit(1);
1372 }
1373
1374 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001375 * For the Hurd, we will turn off filetype since it doesn't
1376 * support it.
1377 */
1378 if (fs->super->s_creator_os == EXT2_OS_HURD)
1379 fs->super->s_feature_incompat &=
1380 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1381
1382 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001383 * Set the volume label...
1384 */
1385 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001386 memset(fs->super->s_volume_name, 0,
1387 sizeof(fs->super->s_volume_name));
1388 strncpy(fs->super->s_volume_name, volume_label,
1389 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001390 }
1391
1392 /*
1393 * Set the last mount directory
1394 */
1395 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001396 memset(fs->super->s_last_mounted, 0,
1397 sizeof(fs->super->s_last_mounted));
1398 strncpy(fs->super->s_last_mounted, mount_dir,
1399 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001400 }
1401
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001402 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001403 show_stats(fs);
1404
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001405 if (noaction)
1406 exit(0);
1407
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001408 if (fs->super->s_feature_incompat &
1409 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1410 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001411 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001412 }
1413
Theodore Ts'o3839e651997-04-26 13:21:57 +00001414 if (bad_blocks_filename)
1415 read_bb_file(fs, &bb_list, bad_blocks_filename);
1416 if (cflag)
1417 test_disk(fs, &bb_list);
1418
1419 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001420 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001421 retval = ext2fs_allocate_tables(fs);
1422 if (retval) {
1423 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001424 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001425 exit(1);
1426 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001427 if (super_only) {
1428 fs->super->s_state |= EXT2_ERROR_FS;
1429 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1430 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001431 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001432 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001433 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001434 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001435 blk_t ret_blk;
1436
1437#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001438 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001439#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001440
1441 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001442 * Wipe out any old MD RAID (or other) metadata at the end
1443 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001444 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001445 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001446 start = (blocks & ~(rsv - 1));
1447 if (start > rsv)
1448 start -= rsv;
1449 if (start > 0)
1450 retval = zero_blocks(fs, start, blocks - start,
1451 NULL, &ret_blk, NULL);
1452
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001453 if (retval) {
1454 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001455 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001456 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001457 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001458 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001459 create_root_dir(fs);
1460 create_lost_and_found(fs);
1461 reserve_inodes(fs);
1462 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001463 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001464
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001465 if (journal_device) {
1466 ext2_filsys jfs;
1467
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001468 if (!force)
1469 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001470 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001471
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001472 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1473 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1474 fs->blocksize, unix_io_manager, &jfs);
1475 if (retval) {
1476 com_err(program_name, retval,
1477 _("while trying to open journal device %s\n"),
1478 journal_device);
1479 exit(1);
1480 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001481 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001482 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001483 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001484 fflush(stdout);
1485 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001486 retval = ext2fs_add_journal_device(fs, jfs);
1487 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001488 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001489 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001490 journal_device);
1491 exit(1);
1492 }
1493 if (!quiet)
1494 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001495 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001496 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001497 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001498 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001499
1500 if (!journal_blocks) {
1501 fs->super->s_feature_compat &=
1502 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1503 goto no_journal;
1504 }
1505 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001506 printf(_("Creating journal (%d blocks): "),
1507 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001508 fflush(stdout);
1509 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001510 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1511 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001512 if (retval) {
1513 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001514 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001515 exit(1);
1516 }
1517 if (!quiet)
1518 printf(_("done\n"));
1519 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001520no_journal:
1521
Theodore Ts'o3839e651997-04-26 13:21:57 +00001522 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001523 printf(_("Writing superblocks and "
1524 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001525 retval = ext2fs_flush(fs);
1526 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001527 fprintf(stderr,
1528 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001529 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001530 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001531 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001532 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1533 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001534 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001535 val = ext2fs_close(fs);
1536 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001537}