blob: 2365fbb17b35743c4319eaeff590564320e58101 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05004 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 */
12
13/* Usage: mke2fs [options] device
14 *
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
17 */
18
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000019#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <string.h>
21#include <fcntl.h>
22#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <time.h>
Theodore Ts'o756df352002-03-07 20:52:12 -050024#ifdef __linux__
Theodore Ts'o27401561999-09-14 20:11:19 +000025#include <sys/utsname.h>
26#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000027#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000029#else
30extern char *optarg;
31extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000032#endif
33#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000034#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000035#endif
36#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000037#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000038#endif
39#ifdef HAVE_ERRNO_H
40#include <errno.h>
41#endif
42#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000043#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000044#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000045#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000046#include <sys/types.h>
47
Theodore Ts'o54c637d2001-05-14 11:45:38 +000048#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000049#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000050#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000051#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000052#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000053#include "util.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000054#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000055#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
57#define STRIDE_LENGTH 8
58
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000059#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000060#define ZAP_BOOTBLOCK
61#endif
62
Theodore Ts'o3839e651997-04-26 13:21:57 +000063extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000064extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000065
66const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000067const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000068
69/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000070int cflag;
71int verbose;
72int quiet;
73int super_only;
74int force;
75int noaction;
76int journal_size;
77int journal_flags;
78char *bad_blocks_filename;
79__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000080
81struct ext2_super_block param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000082char *creator_os;
83char *volume_label;
84char *mount_dir;
85char *journal_device;
86int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000087
Theodore Ts'o31e29a12002-05-17 10:53:07 -040088int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040089int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040090
Theodore Ts'o63985322001-01-03 17:02:13 +000091static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000092{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000093 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000094 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000095 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
96 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000097 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
Theodore Ts'od323f8f2004-12-15 14:39:16 -050098 "[-r fs-revision] [-R options] [-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000099 program_name);
100 exit(1);
101}
102
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000103static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104{
105 int l = 0;
106
107 arg >>= 1;
108 while (arg) {
109 l++;
110 arg >>= 1;
111 }
112 return l;
113}
114
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000115static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000116{
117 int l;
118
119 for (l=0; arg ; l++)
120 arg = arg / 10;
121 return l;
122}
123
Theodore Ts'od99225e2004-09-25 07:40:12 -0400124static int parse_version_number(const char *s)
125{
126 int major, minor, rev;
127 char *endptr;
128 const char *cp = s;
129
130 if (!s)
131 return 0;
132 major = strtol(cp, &endptr, 10);
133 if (cp == endptr || *endptr != '.')
134 return 0;
135 cp = endptr + 1;
136 minor = strtol(cp, &endptr, 10);
137 if (cp == endptr || *endptr != '.')
138 return 0;
139 cp = endptr + 1;
140 rev = strtol(cp, &endptr, 10);
141 if (cp == endptr)
142 return 0;
143 return ((((major * 256) + minor) * 256) + rev);
144}
145
146
147
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000149 * This function sets the default parameters for a filesystem
150 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000151 * The type is specified by the user. The size is the maximum size
152 * (in megabytes) for which a set of parameters applies, with a size
153 * of zero meaning that it is the default parameter for the type.
154 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000155 */
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400156#define DEF_MAX_BLOCKSIZE -1
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000157static char default_str[] = "default";
158struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000159 const char *type;
160 int size;
161 int blocksize;
162 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000163} settings[] = {
164 { default_str, 0, 4096, 8192 },
165 { default_str, 512, 1024, 4096 },
166 { default_str, 3, 1024, 8192 },
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000167 { "journal", 0, 4096, 8192 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000168 { "news", 0, 4096, 4096 },
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400169 { "largefile", 0, DEF_MAX_BLOCKSIZE, 1024 * 1024 },
170 { "largefile4", 0, DEF_MAX_BLOCKSIZE, 4096 * 1024 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000171 { 0, 0, 0, 0},
172};
173
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500174static void set_fs_defaults(const char *fs_type,
175 struct ext2_super_block *super,
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400176 int blocksize, int sector_size,
177 int *inode_ratio)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000178{
179 int megs;
180 int ratio = 0;
181 struct mke2fs_defaults *p;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400182 int use_bsize = 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000183
Andreas Dilger932a4892002-05-16 03:20:07 -0600184 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000185 if (inode_ratio)
186 ratio = *inode_ratio;
187 if (!fs_type)
188 fs_type = default_str;
189 for (p = settings; p->type; p++) {
190 if ((strcmp(p->type, fs_type) != 0) &&
191 (strcmp(p->type, default_str) != 0))
192 continue;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400193 if ((p->size != 0) && (megs > p->size))
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000194 continue;
195 if (ratio == 0)
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600196 *inode_ratio = p->inode_ratio < blocksize ?
197 blocksize : p->inode_ratio;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400198 use_bsize = p->blocksize;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000199 }
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400200 if (blocksize <= 0) {
Theodore Ts'od99225e2004-09-25 07:40:12 -0400201 if (use_bsize == DEF_MAX_BLOCKSIZE) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400202 use_bsize = sys_page_size;
Theodore Ts'od99225e2004-09-25 07:40:12 -0400203 if ((linux_version_code < (2*65536 + 6*256)) &&
204 (use_bsize > 4096))
205 use_bsize = 4096;
206 }
Theodore Ts'o79120f82003-08-31 20:56:57 -0400207 if (sector_size && use_bsize < sector_size)
208 use_bsize = sector_size;
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400209 if ((blocksize < 0) && (use_bsize < (-blocksize)))
210 use_bsize = -blocksize;
211 blocksize = use_bsize;
212 super->s_blocks_count /= blocksize / 1024;
213 }
214 super->s_log_frag_size = super->s_log_block_size =
215 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000216}
217
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400218
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000219/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 * Helper function for read_bb_file and test_disk
221 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500222static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223{
Theodore Ts'o66938372002-03-08 00:14:46 -0500224 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225 return;
226}
227
228/*
229 * Reads the bad blocks list from a file
230 */
231static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
232 const char *bad_blocks_file)
233{
234 FILE *f;
235 errcode_t retval;
236
237 f = fopen(bad_blocks_file, "r");
238 if (!f) {
239 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000240 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 exit(1);
242 }
243 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
244 fclose (f);
245 if (retval) {
246 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000247 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 exit(1);
249 }
250}
251
252/*
253 * Runs the badblocks program to test the disk
254 */
255static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
256{
257 FILE *f;
258 errcode_t retval;
259 char buf[1024];
260
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500261 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
262 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
263 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000265 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 f = popen(buf, "r");
267 if (!f) {
268 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000269 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 exit(1);
271 }
272 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000273 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274 if (retval) {
275 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000276 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 exit(1);
278 }
279}
280
281static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
282{
Theodore Ts'o54434922003-12-07 01:28:50 -0500283 dgrp_t i;
284 blk_t j;
285 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 blk_t blk;
287 badblocks_iterate bb_iter;
288 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289 blk_t group_block;
290 int group;
291 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292
293 if (!bb_list)
294 return;
295
296 /*
297 * The primary superblock and group descriptors *must* be
298 * good; if not, abort.
299 */
300 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
301 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000302 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000303 fprintf(stderr, _("Block %d in primary "
304 "superblock/group descriptor area bad.\n"), i);
305 fprintf(stderr, _("Blocks %d through %d must be good "
306 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500308 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309 exit(1);
310 }
311 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000312
313 /*
314 * See if any of the bad blocks are showing up in the backup
315 * superblocks and/or group descriptors. If so, issue a
316 * warning and adjust the block counts appropriately.
317 */
318 group_block = fs->super->s_first_data_block +
319 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000320
321 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000322 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000323 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000324 if (ext2fs_badblocks_list_test(bb_list,
325 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000326 if (!group_bad)
327 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000328_("Warning: the backup superblock/group descriptors at block %d contain\n"
329" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000330 group_block);
331 group_bad++;
332 group = ext2fs_group_of_blk(fs, group_block+j);
333 fs->group_desc[group].bg_free_blocks_count++;
334 fs->super->s_free_blocks_count++;
335 }
336 }
337 group_block += fs->super->s_blocks_per_group;
338 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000339
340 /*
341 * Mark all the bad blocks as used...
342 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000343 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000345 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000346 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347 exit(1);
348 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000349 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000350 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000351 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352}
353
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000354/*
355 * These functions implement a generalized progress meter.
356 */
357struct progress_struct {
358 char format[20];
359 char backup[80];
360 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400361 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000362};
363
364static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500365 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000367 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000368
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000369 memset(progress, 0, sizeof(struct progress_struct));
370 if (quiet)
371 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000372
373 /*
374 * Figure out how many digits we need
375 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000376 i = int_log10(max);
377 sprintf(progress->format, "%%%dd/%%%dld", i, i);
378 memset(progress->backup, '\b', sizeof(progress->backup)-1);
379 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500380 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000381 progress->backup[(2*i)+1] = 0;
382 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000383
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400384 progress->skip_progress = 0;
385 if (getenv("MKE2FS_SKIP_PROGRESS"))
386 progress->skip_progress++;
387
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000388 fputs(label, stdout);
389 fflush(stdout);
390}
391
392static void progress_update(struct progress_struct *progress, __u32 val)
393{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400394 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000395 return;
396 printf(progress->format, val, progress->max);
397 fputs(progress->backup, stdout);
398}
399
400static void progress_close(struct progress_struct *progress)
401{
402 if (progress->format[0] == 0)
403 return;
404 fputs(_("done \n"), stdout);
405}
406
407
408/*
409 * Helper function which zeros out _num_ blocks starting at _blk_. In
410 * case of an error, the details of the error is returned via _ret_blk_
411 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
412 * success, and an error code on an error.
413 *
414 * As a special case, if the first argument is NULL, then it will
415 * attempt to free the static zeroizing buffer. (This is to keep
416 * programs that check for memory leaks happy.)
417 */
418static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
419 struct progress_struct *progress,
420 blk_t *ret_blk, int *ret_count)
421{
422 int j, count, next_update, next_update_incr;
423 static char *buf;
424 errcode_t retval;
425
426 /* If fs is null, clean up the static buffer and return */
427 if (!fs) {
428 if (buf) {
429 free(buf);
430 buf = 0;
431 }
432 return 0;
433 }
434 /* Allocate the zeroizing buffer if necessary */
435 if (!buf) {
436 buf = malloc(fs->blocksize * STRIDE_LENGTH);
437 if (!buf) {
438 com_err("malloc", ENOMEM,
439 _("while allocating zeroizing buffer"));
440 exit(1);
441 }
442 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
443 }
444 /* OK, do the write loop */
445 next_update = 0;
446 next_update_incr = num / 100;
447 if (next_update_incr < 1)
448 next_update_incr = 1;
449 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400450 count = num - j;
451 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000452 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000453 retval = io_channel_write_blk(fs->io, blk, count, buf);
454 if (retval) {
455 if (ret_count)
456 *ret_count = count;
457 if (ret_blk)
458 *ret_blk = blk;
459 return retval;
460 }
461 if (progress && j > next_update) {
462 next_update += num / 100;
463 progress_update(progress, blk);
464 }
465 }
466 return 0;
467}
468
469static void write_inode_tables(ext2_filsys fs)
470{
471 errcode_t retval;
472 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500473 dgrp_t i;
474 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000475 struct progress_struct progress;
476
477 if (quiet)
478 memset(&progress, 0, sizeof(progress));
479 else
480 progress_init(&progress, _("Writing inode tables: "),
481 fs->group_desc_count);
482
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000484 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000486 blk = fs->group_desc[i].bg_inode_table;
487 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000488
489 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
490 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500491 fprintf(stderr, _("\nCould not write %d blocks "
492 "in inode table starting at %d: %s\n"),
493 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000494 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000495 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000496 if (sync_kludge) {
497 if (sync_kludge == 1)
498 sync();
499 else if ((i % sync_kludge) == 0)
500 sync();
501 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000503 zero_blocks(0, 0, 0, 0, 0, 0);
504 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505}
506
507static void create_root_dir(ext2_filsys fs)
508{
509 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000510 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511
512 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
513 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000514 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 exit(1);
516 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000517 if (geteuid()) {
518 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
519 if (retval) {
520 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000521 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000522 exit(1);
523 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000524 inode.i_uid = getuid();
525 if (inode.i_uid)
526 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000527 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
528 if (retval) {
529 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000530 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000531 exit(1);
532 }
533 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534}
535
536static void create_lost_and_found(ext2_filsys fs)
537{
538 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000539 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540 const char *name = "lost+found";
541 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000542 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543
Theodore Ts'o6a525062001-12-24 09:40:00 -0500544 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
546 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000547 com_err("ext2fs_mkdir", retval,
548 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549 exit(1);
550 }
551
552 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
553 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000554 com_err("ext2_lookup", retval,
555 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000556 exit(1);
557 }
558
559 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000560 if ((lpf_size += fs->blocksize) >= 16*1024)
561 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562 retval = ext2fs_expand_dir(fs, ino);
563 if (retval) {
564 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000565 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566 exit(1);
567 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500568 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000569}
570
571static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
572{
573 errcode_t retval;
574
Theodore Ts'of3db3561997-04-26 13:34:30 +0000575 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 fs->group_desc[0].bg_free_inodes_count--;
577 fs->super->s_free_inodes_count--;
578 retval = ext2fs_update_bb_inode(fs, bb_list);
579 if (retval) {
580 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000581 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582 exit(1);
583 }
584
585}
586
587static void reserve_inodes(ext2_filsys fs)
588{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000589 ext2_ino_t i;
590 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000591
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000592 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000593 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594 group = ext2fs_group_of_ino(fs, i);
595 fs->group_desc[group].bg_free_inodes_count--;
596 fs->super->s_free_inodes_count--;
597 }
598 ext2fs_mark_ib_dirty(fs);
599}
600
Theodore Ts'o756df352002-03-07 20:52:12 -0500601#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500602#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500603#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500604
Theodore Ts'o04a96852001-08-30 21:55:26 -0400605static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000606{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400607 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000608 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500609 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000610
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400611 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600612 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500613 printf(_("Out of memory erasing sectors %d-%d\n"),
614 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600615 exit(1);
616 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500617
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500618 if (sect == 0) {
619 /* Check for a BSD disklabel, and don't erase it if so */
620 retval = io_channel_read_blk(fs->io, 0, -512, buf);
621 if (retval)
622 fprintf(stderr,
623 _("Warning: could not read block 0: %s\n"),
624 error_message(retval));
625 else {
626 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
627 if ((*magic == BSD_DISKMAGIC) ||
628 (*magic == BSD_MAGICDISK))
629 return;
630 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500631 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500632
Theodore Ts'o70988102002-07-14 08:00:00 -0400633 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000634 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400635 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000636 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400637 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000638 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500639 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
640 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000641}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000642
643static void create_journal_dev(ext2_filsys fs)
644{
645 struct progress_struct progress;
646 errcode_t retval;
647 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000648 blk_t blk;
649 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000650
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000651 retval = ext2fs_create_journal_superblock(fs,
652 fs->super->s_blocks_count, 0, &buf);
653 if (retval) {
654 com_err("create_journal_dev", retval,
655 _("while initializing journal superblock"));
656 exit(1);
657 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000658 if (quiet)
659 memset(&progress, 0, sizeof(progress));
660 else
661 progress_init(&progress, _("Zeroing journal device: "),
662 fs->super->s_blocks_count);
663
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000664 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
665 &progress, &blk, &count);
666 if (retval) {
667 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000668 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000669 blk, count);
670 exit(1);
671 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000672 zero_blocks(0, 0, 0, 0, 0, 0);
673
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000674 retval = io_channel_write_blk(fs->io,
675 fs->super->s_first_data_block+1,
676 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000677 if (retval) {
678 com_err("create_journal_dev", retval,
679 _("while writing journal superblock"));
680 exit(1);
681 }
682 progress_close(&progress);
683}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000684
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685static void show_stats(ext2_filsys fs)
686{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000687 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000688 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000689 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500690 dgrp_t i;
691 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000692
693 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'o66938372002-03-08 00:14:46 -0500694 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000696
697 memset(buf, 0, sizeof(buf));
698 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000699 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500700 fputs(_("OS type: "), stdout);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000701 switch (fs->super->s_creator_os) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500702 case EXT2_OS_LINUX: fputs("Linux", stdout); break;
703 case EXT2_OS_HURD: fputs("GNU/Hurd", stdout); break;
704 case EXT2_OS_MASIX: fputs ("Masix", stdout); break;
705 default: fputs(_("(unknown os)"), stdout);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000706 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000707 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000708 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000709 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000710 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000711 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000712 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000713 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000714 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000715 s->s_r_blocks_count,
716 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000717 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500718 if (s->s_reserved_gdt_blocks)
719 printf(_("Maximum filesystem blocks=%lu\n"),
720 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
721 (fs->blocksize / sizeof(struct ext2_group_desc)) *
722 s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000723 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000724 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000725 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000726 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000727 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000729 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730
731 if (fs->group_desc_count == 1) {
732 printf("\n");
733 return;
734 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500735
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000736 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737 group_block = s->s_first_data_block;
738 col_left = 0;
739 for (i = 1; i < fs->group_desc_count; i++) {
740 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000741 if (!ext2fs_bg_has_super(fs, i))
742 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000743 if (i != 1)
744 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000745 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000746 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000747 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000748 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000750 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000751 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752 }
753 printf("\n\n");
754}
755
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000756/*
757 * Set the S_CREATOR_OS field. Return true if OS is known,
758 * otherwise, 0.
759 */
760static int set_os(struct ext2_super_block *sb, char *os)
761{
762 if (isdigit (*os))
763 sb->s_creator_os = atoi (os);
764 else if (strcasecmp(os, "linux") == 0)
765 sb->s_creator_os = EXT2_OS_LINUX;
766 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
767 sb->s_creator_os = EXT2_OS_HURD;
768 else if (strcasecmp(os, "masix") == 0)
769 sb->s_creator_os = EXT2_OS_MASIX;
770 else
771 return 0;
772 return 1;
773}
774
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000775#define PATH_SET "PATH=/sbin"
776
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500777static void parse_r_opts(struct ext2_super_block *param, const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000778{
779 char *buf, *token, *next, *p, *arg;
780 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500781 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000782
783 len = strlen(opts);
784 buf = malloc(len+1);
785 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500786 fprintf(stderr,
787 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000788 exit(1);
789 }
790 strcpy(buf, opts);
791 for (token = buf; token && *token; token = next) {
792 p = strchr(token, ',');
793 next = 0;
794 if (p) {
795 *p = 0;
796 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500797 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000798 arg = strchr(token, '=');
799 if (arg) {
800 *arg = 0;
801 arg++;
802 }
803 if (strcmp(token, "stride") == 0) {
804 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500805 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000806 continue;
807 }
808 fs_stride = strtoul(arg, &p, 0);
809 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000810 fprintf(stderr,
811 _("Invalid stride parameter.\n"));
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500812 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000813 continue;
814 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500815 } else if (!strcmp(token, "resize")) {
816 unsigned long resize = 1;
817 int tmp;
818
819 if (!arg) {
820 r_usage++;
821 continue;
822 }
823
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500824 resize = parse_num_blocks(arg,
825 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500826
827 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500828 fprintf(stderr,
829 _("Invalid resize parameter: %s\n"),
830 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500831 r_usage++;
832 continue;
833 }
834 param->s_feature_compat |=
835 EXT2_FEATURE_COMPAT_RESIZE_INODE;
836 tmp = param->s_blocks_per_group;
837 if (tmp > EXT2_MAX_BLOCKS_PER_GROUP(param))
838 tmp = EXT2_MAX_BLOCKS_PER_GROUP(param);
839 resize = (resize + tmp - 1) / tmp;
840 tmp = (1 << param->s_log_block_size) /
841 sizeof(struct ext2_group_desc);
842 resize = (resize + tmp - 1) / tmp;
843 /* XXX param->s_res_gdt_blocks = resize - existing
844 cur_groups = (resize - sb->s_first_data_block +
845 EXT2_BLOCKS_PER_GROUP(super) - 1) /bpg;
846 cur_gdb = (cur_groups + gdpb - 1) / gdpb;
847 */
848
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000849 } else
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500850 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000851 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500852 if (r_usage) {
853 fprintf(stderr, _("\nBad options specified.\n\n"
854 "Options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000855 "and may take an argument which\n"
856 "\tis set off by an equals ('=') sign.\n\n"
857 "Valid raid options are:\n"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500858 "\tstride=<stride length in blocks>\n"
859 "\tresize=<resize maximum size in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000860 exit(1);
861 }
862}
863
Theodore Ts'o896938d1999-10-23 01:04:50 +0000864static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400865 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500866 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'o843049c2002-09-22 15:37:40 -0400867 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000868 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400869 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
870 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000871 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
872};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000873
874
Theodore Ts'o3839e651997-04-26 13:21:57 +0000875static void PRS(int argc, char *argv[])
876{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400877 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000878 int size;
879 char * tmp;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000880 int blocksize = 0;
881 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600882 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000883 int reserved_ratio = 5;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400884 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500885 int show_version_only = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000886 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000887 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000888 char * oldpath = getenv("PATH");
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500889 char * r_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500890 const char * fs_type = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000891 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500892#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000893 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000894#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400895 long sysval;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000896
Theodore Ts'o3839e651997-04-26 13:21:57 +0000897 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000898 if (oldpath) {
899 char *newpath;
900
901 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
902 strcpy (newpath, PATH_SET);
903 strcat (newpath, ":");
904 strcat (newpath, oldpath);
905 putenv (newpath);
906 } else
907 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000908
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000909 tmp = getenv("MKE2FS_SYNC");
910 if (tmp)
911 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400912
913 /* Determine the system page size if possible */
914#ifdef HAVE_SYSCONF
915#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
916#define _SC_PAGESIZE _SC_PAGE_SIZE
917#endif
918#ifdef _SC_PAGESIZE
919 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600920 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400921 sys_page_size = sysval;
922#endif /* _SC_PAGESIZE */
923#endif /* HAVE_SYSCONF */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000924
Theodore Ts'o3839e651997-04-26 13:21:57 +0000925 setbuf(stdout, NULL);
926 setbuf(stderr, NULL);
927 initialize_ext2_error_table();
928 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000929 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400930 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
931 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500932#if 0
Theodore Ts'o843049c2002-09-22 15:37:40 -0400933 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500934#endif
Theodore Ts'o843049c2002-09-22 15:37:40 -0400935
Theodore Ts'o756df352002-03-07 20:52:12 -0500936#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000937 if (uname(&ut)) {
938 perror("uname");
939 exit(1);
940 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400941 linux_version_code = parse_version_number(ut.release);
942 if (linux_version_code && linux_version_code < (2*65536 + 2*256)) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000943 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400944 param.s_feature_incompat = 0;
945 param.s_feature_compat = 0;
946 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000947 }
948#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700949
950 if (argc && *argv) {
951 program_name = get_progname(*argv);
952
953 /* If called as mkfs.ext3, create a journal inode */
954 if (!strcmp(program_name, "mkfs.ext3"))
955 journal_size = -1;
956 }
957
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000958 while ((c = getopt (argc, argv,
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500959 "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 +0000960 switch (c) {
961 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400962 blocksize = strtol(optarg, &tmp, 0);
963 b = (blocksize > 0) ? blocksize : -blocksize;
964 if (b < EXT2_MIN_BLOCK_SIZE ||
965 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000966 com_err(program_name, 0,
967 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000968 exit(1);
969 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600970 if (blocksize > 4096)
971 fprintf(stderr, _("Warning: blocksize %d not "
972 "usable on most systems.\n"),
973 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400974 if (blocksize > 0)
975 param.s_log_block_size =
976 int_log2(blocksize >>
977 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000978 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000979 case 'c': /* Check for bad blocks */
980 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500981 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000982 break;
983 case 'f':
984 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600985 if (size < EXT2_MIN_BLOCK_SIZE ||
986 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000987 com_err(program_name, 0,
988 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000989 optarg);
990 exit(1);
991 }
992 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000993 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500994 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000995 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000996 break;
997 case 'g':
998 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000999 if (*tmp) {
1000 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001001 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001002 exit(1);
1003 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001004 if ((param.s_blocks_per_group % 8) != 0) {
1005 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001006 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001007 exit(1);
1008 }
1009 break;
1010 case 'i':
1011 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001012 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1013 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001014 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001015 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -06001016 _("bad inode ratio %s (min %d/max %d"),
1017 optarg, EXT2_MIN_BLOCK_SIZE,
1018 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001019 exit(1);
1020 }
1021 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001022 case 'J':
1023 parse_journal_opts(optarg);
1024 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001025 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001026 param.s_feature_compat |=
1027 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001028 if (!journal_size)
1029 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001030 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001031 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001032 bad_blocks_filename = malloc(strlen(optarg)+1);
1033 if (!bad_blocks_filename) {
1034 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001035 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001036 exit(1);
1037 }
1038 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001039 break;
1040 case 'm':
1041 reserved_ratio = strtoul(optarg, &tmp, 0);
1042 if (reserved_ratio > 50 || *tmp) {
1043 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001044 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001045 optarg);
1046 exit(1);
1047 }
1048 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001049 case 'n':
1050 noaction++;
1051 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001052 case 'o':
1053 creator_os = optarg;
1054 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001055 case 'r':
1056 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001057 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001058 param.s_feature_incompat = 0;
1059 param.s_feature_compat = 0;
1060 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001061 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001062 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001063 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001064 if (atoi(optarg))
1065 param.s_feature_ro_compat |=
1066 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1067 else
1068 param.s_feature_ro_compat &=
1069 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001070 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001071#ifdef EXT2_DYNAMIC_REV
1072 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001073 inode_size = strtoul(optarg, &tmp, 0);
1074 if (*tmp) {
1075 com_err(program_name, 0,
1076 _("bad inode size - %s"), optarg);
1077 exit(1);
1078 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001079 break;
1080#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001081 case 'N':
1082 num_inodes = atoi(optarg);
1083 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001084 case 'v':
1085 verbose = 1;
1086 break;
1087 case 'q':
1088 quiet = 1;
1089 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001090 case 'F':
1091 force = 1;
1092 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001093 case 'L':
1094 volume_label = optarg;
1095 break;
1096 case 'M':
1097 mount_dir = optarg;
1098 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001099 case 'O':
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001100 if (!strcmp(optarg, "none")) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001101 param.s_feature_compat = 0;
1102 param.s_feature_incompat = 0;
1103 param.s_feature_ro_compat = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001104 break;
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001105 }
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001106 if (e2p_edit_feature(optarg,
1107 &param.s_feature_compat,
1108 ok_features)) {
1109 fprintf(stderr,
1110 _("Invalid filesystem option set: %s\n"), optarg);
1111 exit(1);
1112 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001113 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001114 case 'R':
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001115 r_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001116 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001117 case 'S':
1118 super_only = 1;
1119 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001120 case 'T':
1121 fs_type = optarg;
1122 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001123 case 'V':
1124 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001125 show_version_only++;
1126 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001127 default:
1128 usage();
1129 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001130 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001131 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001132 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001133 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001134
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001135 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001136 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1137 E2FSPROGS_DATE);
1138
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001139 if (show_version_only) {
1140 fprintf(stderr, _("\tUsing %s\n"),
1141 error_message(EXT2_ET_BASE));
1142 exit(0);
1143 }
1144
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001145 /*
1146 * If there's no blocksize specified and there is a journal
1147 * device, use it to figure out the blocksize
1148 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001149 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001150 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001151 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001152
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001153#ifdef CONFIG_TESTIO_DEBUG
1154 io_ptr = test_io_manager;
1155 test_io_backing_manager = unix_io_manager;
1156#else
1157 io_ptr = unix_io_manager;
1158#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001159 retval = ext2fs_open(journal_device,
1160 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001161 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001162 if (retval) {
1163 com_err(program_name, retval,
1164 _("while trying to open journal device %s\n"),
1165 journal_device);
1166 exit(1);
1167 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001168 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001169 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001170 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001171 "minimum blocksize %d\n"), jfs->blocksize,
1172 -blocksize);
1173 exit(1);
1174 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001175 blocksize = jfs->blocksize;
1176 param.s_log_block_size =
1177 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1178 ext2fs_close(jfs);
1179 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001180
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001181 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001182 if (!force) {
1183 com_err(program_name, 0,
1184 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001185 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001186 proceed_question();
1187 }
1188 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1189 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001190 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001191 }
Theodore Ts'ocf7d5f12003-07-25 17:39:51 -04001192 if ((blocksize > 4096) &&
1193 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1194 fprintf(stderr, "\nWarning: some 2.4 kernels do not support "
1195 "blocksizes greater than 4096 \n\tusing ext3."
1196 " Use -b 4096 if this is an issue for you.\n\n");
Andreas Dilger932a4892002-05-16 03:20:07 -06001197
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001198 if (optind < argc) {
1199 param.s_blocks_count = parse_num_blocks(argv[optind++],
1200 param.s_log_block_size);
1201 if (!param.s_blocks_count) {
1202 com_err(program_name, 0, _("bad blocks count - %s"),
1203 argv[optind - 1]);
1204 exit(1);
1205 }
1206 }
1207 if (optind < argc)
1208 usage();
1209
Andreas Dilgerfa724582001-08-04 01:18:34 -06001210 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1211 if (!fs_type)
1212 fs_type = "journal";
1213 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001214 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1215 param.s_feature_compat = 0;
1216 param.s_feature_ro_compat = 0;
1217 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001218 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1219 (param.s_feature_compat || param.s_feature_ro_compat ||
1220 param.s_feature_incompat))
1221 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1222
Theodore Ts'o74becf31997-04-26 14:37:06 +00001223 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001224 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001225 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001226
Theodore Ts'o3839e651997-04-26 13:21:57 +00001227 param.s_log_frag_size = param.s_log_block_size;
1228
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001229 if (noaction && param.s_blocks_count) {
1230 dev_size = param.s_blocks_count;
1231 retval = 0;
1232 } else
1233 retval = ext2fs_get_device_size(device_name,
1234 EXT2_BLOCK_SIZE(&param),
1235 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001236 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1237 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001238 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001239 exit(1);
1240 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001241 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001242 if (retval == EXT2_ET_UNIMPLEMENTED) {
1243 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001244 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001245 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001246 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001247 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001248 } else {
1249 if (dev_size == 0) {
1250 com_err(program_name, 0,
1251 _("Device size reported to be zero. "
1252 "Invalid partition specified, or\n\t"
1253 "partition table wasn't reread "
1254 "after running fdisk, due to\n\t"
1255 "a modified partition being busy "
1256 "and in use. You may need to reboot\n\t"
1257 "to re-read your partition table.\n"
1258 ));
1259 exit(1);
1260 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001261 param.s_blocks_count = dev_size;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001262 if (sys_page_size > EXT2_BLOCK_SIZE(&param))
1263 param.s_blocks_count &= ~((sys_page_size /
1264 EXT2_BLOCK_SIZE(&param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001265 }
1266
Theodore Ts'oa789d841998-03-30 01:20:55 +00001267 } else if (!force && (param.s_blocks_count > dev_size)) {
1268 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001269 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001270 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001271 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001272
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001273 /*
1274 * If the user asked for HAS_JOURNAL, then make sure a journal
1275 * gets created.
1276 */
1277 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1278 !journal_size)
1279 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001280
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001281 /* Set first meta blockgroup via an environment variable */
1282 /* (this is mostly for debugging purposes) */
1283 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1284 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1285 param.s_first_meta_bg = atoi(tmp);
1286
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001287 /* Get the hardware sector size, if available */
1288 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1289 if (retval) {
1290 com_err(program_name, retval,
1291 _("while trying to determine hardware sector size"));
1292 exit(1);
1293 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001294
Theodore Ts'o54434922003-12-07 01:28:50 -05001295 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001296 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001297
1298 set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001299 blocksize = EXT2_BLOCK_SIZE(&param);
1300
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001301 if (r_opts)
1302 parse_r_opts(&param, r_opts);
1303
1304 /* Since sparse_super is the default, we would only have a problem
1305 * here if it was explicitly disabled.
1306 */
1307 if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1308 !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1309 com_err(program_name, 0,
1310 _("reserved online resize blocks not supported "
1311 "on non-sparse filesystem"));
1312 exit(1);
1313 }
1314
Theodore Ts'o521e3681997-04-29 17:48:10 +00001315 if (param.s_blocks_per_group) {
1316 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o54434922003-12-07 01:28:50 -05001317 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001318 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001319 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001320 exit(1);
1321 }
1322 }
1323
Andreas Dilger932a4892002-05-16 03:20:07 -06001324 if (inode_size) {
1325 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1326 inode_size > EXT2_BLOCK_SIZE(&param) ||
1327 inode_size & (inode_size - 1)) {
1328 com_err(program_name, 0,
1329 _("bad inode size %d (min %d/max %d)"),
1330 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001331 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001332 exit(1);
1333 }
1334 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1335 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1336 "on most systems\n"),
1337 inode_size);
1338 param.s_inode_size = inode_size;
1339 }
1340
Theodore Ts'o3839e651997-04-26 13:21:57 +00001341 /*
1342 * Calculate number of inodes based on the inode ratio
1343 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001344 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001345 ((__u64) param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001346 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001347
1348 /*
1349 * Calculate number of blocks to reserve
1350 */
1351 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1352}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001353
Theodore Ts'o3839e651997-04-26 13:21:57 +00001354int main (int argc, char *argv[])
1355{
1356 errcode_t retval = 0;
1357 ext2_filsys fs;
1358 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001359 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001360 unsigned int i;
1361 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001362 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001363
1364#ifdef ENABLE_NLS
1365 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001366 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001367 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1368 textdomain(NLS_CAT_NAME);
1369#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001370 PRS(argc, argv);
1371
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001372#ifdef CONFIG_TESTIO_DEBUG
1373 io_ptr = test_io_manager;
1374 test_io_backing_manager = unix_io_manager;
1375#else
1376 io_ptr = unix_io_manager;
1377#endif
1378
Theodore Ts'o3839e651997-04-26 13:21:57 +00001379 /*
1380 * Initialize the superblock....
1381 */
1382 retval = ext2fs_initialize(device_name, 0, &param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001383 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001384 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001385 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001386 exit(1);
1387 }
1388
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001389 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001390 * Wipe out the old on-disk superblock
1391 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001392 if (!noaction)
1393 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001394
1395 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001396 * Generate a UUID for it...
1397 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001398 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001399
1400 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001401 * Initialize the directory index variables
1402 */
1403 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1404 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1405
1406 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001407 * Add "jitter" to the superblock's check interval so that we
1408 * don't check all the filesystems at the same time. We use a
1409 * kludgy hack of using the UUID to derive a random jitter value.
1410 */
1411 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1412 val += fs->super->s_uuid[i];
1413 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1414
1415 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001416 * Override the creator OS, if applicable
1417 */
1418 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001419 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001420 exit(1);
1421 }
1422
1423 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001424 * For the Hurd, we will turn off filetype since it doesn't
1425 * support it.
1426 */
1427 if (fs->super->s_creator_os == EXT2_OS_HURD)
1428 fs->super->s_feature_incompat &=
1429 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1430
1431 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001432 * Set the volume label...
1433 */
1434 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001435 memset(fs->super->s_volume_name, 0,
1436 sizeof(fs->super->s_volume_name));
1437 strncpy(fs->super->s_volume_name, volume_label,
1438 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001439 }
1440
1441 /*
1442 * Set the last mount directory
1443 */
1444 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001445 memset(fs->super->s_last_mounted, 0,
1446 sizeof(fs->super->s_last_mounted));
1447 strncpy(fs->super->s_last_mounted, mount_dir,
1448 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001449 }
1450
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001451 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001452 show_stats(fs);
1453
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001454 if (noaction)
1455 exit(0);
1456
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001457 if (fs->super->s_feature_incompat &
1458 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1459 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001460 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001461 }
1462
Theodore Ts'o3839e651997-04-26 13:21:57 +00001463 if (bad_blocks_filename)
1464 read_bb_file(fs, &bb_list, bad_blocks_filename);
1465 if (cflag)
1466 test_disk(fs, &bb_list);
1467
1468 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001469 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001470 retval = ext2fs_allocate_tables(fs);
1471 if (retval) {
1472 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001473 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001474 exit(1);
1475 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001476 if (super_only) {
1477 fs->super->s_state |= EXT2_ERROR_FS;
1478 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1479 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001480 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001481 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001482 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001483 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001484 blk_t ret_blk;
1485
1486#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001487 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001488#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001489
1490 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001491 * Wipe out any old MD RAID (or other) metadata at the end
1492 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001493 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001494 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001495 start = (blocks & ~(rsv - 1));
1496 if (start > rsv)
1497 start -= rsv;
1498 if (start > 0)
1499 retval = zero_blocks(fs, start, blocks - start,
1500 NULL, &ret_blk, NULL);
1501
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001502 if (retval) {
1503 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001504 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001505 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001506 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001507 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001508 create_root_dir(fs);
1509 create_lost_and_found(fs);
1510 reserve_inodes(fs);
1511 create_bad_block_inode(fs, bb_list);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001512 retval = ext2fs_create_resize_inode(fs);
1513 if (retval) {
1514 com_err("ext2fs_create_resize_inode", retval,
1515 _("while reserving blocks for online resize"));
1516 exit(1);
1517 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001518 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001519
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001520 if (journal_device) {
1521 ext2_filsys jfs;
1522
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001523 if (!force)
1524 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001525 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001526
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001527 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1528 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1529 fs->blocksize, unix_io_manager, &jfs);
1530 if (retval) {
1531 com_err(program_name, retval,
1532 _("while trying to open journal device %s\n"),
1533 journal_device);
1534 exit(1);
1535 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001536 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001537 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001538 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001539 fflush(stdout);
1540 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001541 retval = ext2fs_add_journal_device(fs, jfs);
1542 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001543 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001544 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001545 journal_device);
1546 exit(1);
1547 }
1548 if (!quiet)
1549 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001550 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001551 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001552 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001553 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001554
1555 if (!journal_blocks) {
1556 fs->super->s_feature_compat &=
1557 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1558 goto no_journal;
1559 }
1560 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001561 printf(_("Creating journal (%d blocks): "),
1562 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001563 fflush(stdout);
1564 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001565 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1566 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001567 if (retval) {
1568 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001569 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001570 exit(1);
1571 }
1572 if (!quiet)
1573 printf(_("done\n"));
1574 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001575no_journal:
1576
Theodore Ts'o3839e651997-04-26 13:21:57 +00001577 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001578 printf(_("Writing superblocks and "
1579 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001580 retval = ext2fs_flush(fs);
1581 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001582 fprintf(stderr,
1583 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001584 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001585 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001586 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001587 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1588 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001589 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001590 val = ext2fs_close(fs);
1591 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001592}