blob: 8bf5c18cce652e40eb88d934dc4c9a96561f70ef [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'od323f8f2004-12-15 14:39:16 -050097 "[-r fs-revision] [-R options] [-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);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500717 if (s->s_reserved_gdt_blocks)
718 printf(_("Maximum filesystem blocks=%lu\n"),
719 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
720 (fs->blocksize / sizeof(struct ext2_group_desc)) *
721 s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000722 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000723 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000724 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000725 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000726 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000727 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000728 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000729
730 if (fs->group_desc_count == 1) {
731 printf("\n");
732 return;
733 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500734
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000735 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000736 group_block = s->s_first_data_block;
737 col_left = 0;
738 for (i = 1; i < fs->group_desc_count; i++) {
739 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000740 if (!ext2fs_bg_has_super(fs, i))
741 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000742 if (i != 1)
743 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000744 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000745 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000746 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000747 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000748 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000749 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000750 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000751 }
752 printf("\n\n");
753}
754
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000755/*
756 * Set the S_CREATOR_OS field. Return true if OS is known,
757 * otherwise, 0.
758 */
759static int set_os(struct ext2_super_block *sb, char *os)
760{
761 if (isdigit (*os))
762 sb->s_creator_os = atoi (os);
763 else if (strcasecmp(os, "linux") == 0)
764 sb->s_creator_os = EXT2_OS_LINUX;
765 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
766 sb->s_creator_os = EXT2_OS_HURD;
767 else if (strcasecmp(os, "masix") == 0)
768 sb->s_creator_os = EXT2_OS_MASIX;
769 else
770 return 0;
771 return 1;
772}
773
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000774#define PATH_SET "PATH=/sbin"
775
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500776static void parse_r_opts(struct ext2_super_block *param, const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000777{
778 char *buf, *token, *next, *p, *arg;
779 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500780 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000781
782 len = strlen(opts);
783 buf = malloc(len+1);
784 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500785 fprintf(stderr,
786 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000787 exit(1);
788 }
789 strcpy(buf, opts);
790 for (token = buf; token && *token; token = next) {
791 p = strchr(token, ',');
792 next = 0;
793 if (p) {
794 *p = 0;
795 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500796 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000797 arg = strchr(token, '=');
798 if (arg) {
799 *arg = 0;
800 arg++;
801 }
802 if (strcmp(token, "stride") == 0) {
803 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500804 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000805 continue;
806 }
807 fs_stride = strtoul(arg, &p, 0);
808 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000809 fprintf(stderr,
810 _("Invalid stride parameter.\n"));
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500811 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000812 continue;
813 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500814 } else if (!strcmp(token, "resize")) {
815 unsigned long resize = 1;
816 int tmp;
817
818 if (!arg) {
819 r_usage++;
820 continue;
821 }
822
823 p = &arg[strlen(arg) - 1];
824
825 switch(*p++) {
826 case 'T':
827 case 't': resize <<= 10; /* no break */
828 case 'G':
829 case 'g': resize <<= 10; /* no break */
830 case 'M':
831 case 'm': resize <<= 10; /* no break */
832 case 'K':
833 case 'k': resize >>= param->s_log_block_size -10; *p = 0; break;
834 case 'b': resize >>= param->s_log_block_size - 9; *p = 0; break;
835 case '0': break;
836 case '1': break;
837 case '2': break;
838 case '3': break;
839 case '4': break;
840 case '5': break;
841 case '6': break;
842 case '7': break;
843 case '8': break;
844 case '9': break;
845 default: r_usage++; continue;
846 }
847
848 resize *= strtoul(arg, NULL, 0);
849
850 if (resize == 0) {
851 fprintf(stderr,
852 _("Invalid resize parameter.\n"));
853 r_usage++;
854 continue;
855 }
856 param->s_feature_compat |=
857 EXT2_FEATURE_COMPAT_RESIZE_INODE;
858 tmp = param->s_blocks_per_group;
859 if (tmp > EXT2_MAX_BLOCKS_PER_GROUP(param))
860 tmp = EXT2_MAX_BLOCKS_PER_GROUP(param);
861 resize = (resize + tmp - 1) / tmp;
862 tmp = (1 << param->s_log_block_size) /
863 sizeof(struct ext2_group_desc);
864 resize = (resize + tmp - 1) / tmp;
865 /* XXX param->s_res_gdt_blocks = resize - existing
866 cur_groups = (resize - sb->s_first_data_block +
867 EXT2_BLOCKS_PER_GROUP(super) - 1) /bpg;
868 cur_gdb = (cur_groups + gdpb - 1) / gdpb;
869 */
870
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000871 } else
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500872 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000873 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500874 if (r_usage) {
875 fprintf(stderr, _("\nBad options specified.\n\n"
876 "Options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000877 "and may take an argument which\n"
878 "\tis set off by an equals ('=') sign.\n\n"
879 "Valid raid options are:\n"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500880 "\tstride=<stride length in blocks>\n"
881 "\tresize=<resize maximum size in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000882 exit(1);
883 }
884}
885
Theodore Ts'o896938d1999-10-23 01:04:50 +0000886static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400887 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500888 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'o843049c2002-09-22 15:37:40 -0400889 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000890 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400891 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
892 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000893 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
894};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000895
896
Theodore Ts'o3839e651997-04-26 13:21:57 +0000897static void PRS(int argc, char *argv[])
898{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400899 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000900 int size;
901 char * tmp;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000902 int blocksize = 0;
903 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600904 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000905 int reserved_ratio = 5;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400906 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500907 int show_version_only = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000908 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000909 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000910 char * oldpath = getenv("PATH");
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500911 char * r_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500912 const char * fs_type = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000913 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500914#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000915 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000916#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400917 long sysval;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000918
Theodore Ts'o3839e651997-04-26 13:21:57 +0000919 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000920 if (oldpath) {
921 char *newpath;
922
923 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
924 strcpy (newpath, PATH_SET);
925 strcat (newpath, ":");
926 strcat (newpath, oldpath);
927 putenv (newpath);
928 } else
929 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000930
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000931 tmp = getenv("MKE2FS_SYNC");
932 if (tmp)
933 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400934
935 /* Determine the system page size if possible */
936#ifdef HAVE_SYSCONF
937#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
938#define _SC_PAGESIZE _SC_PAGE_SIZE
939#endif
940#ifdef _SC_PAGESIZE
941 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600942 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400943 sys_page_size = sysval;
944#endif /* _SC_PAGESIZE */
945#endif /* HAVE_SYSCONF */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000946
Theodore Ts'o3839e651997-04-26 13:21:57 +0000947 setbuf(stdout, NULL);
948 setbuf(stderr, NULL);
949 initialize_ext2_error_table();
950 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000951 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400952 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
953 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500954#if 0
Theodore Ts'o843049c2002-09-22 15:37:40 -0400955 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
Theodore Ts'o54779c62002-11-13 07:00:16 -0500956#endif
Theodore Ts'o843049c2002-09-22 15:37:40 -0400957
Theodore Ts'o756df352002-03-07 20:52:12 -0500958#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000959 if (uname(&ut)) {
960 perror("uname");
961 exit(1);
962 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400963 linux_version_code = parse_version_number(ut.release);
964 if (linux_version_code && linux_version_code < (2*65536 + 2*256)) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000965 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400966 param.s_feature_incompat = 0;
967 param.s_feature_compat = 0;
968 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000969 }
970#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700971
972 if (argc && *argv) {
973 program_name = get_progname(*argv);
974
975 /* If called as mkfs.ext3, create a journal inode */
976 if (!strcmp(program_name, "mkfs.ext3"))
977 journal_size = -1;
978 }
979
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000980 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000981 "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 +0000982 switch (c) {
983 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400984 blocksize = strtol(optarg, &tmp, 0);
985 b = (blocksize > 0) ? blocksize : -blocksize;
986 if (b < EXT2_MIN_BLOCK_SIZE ||
987 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000988 com_err(program_name, 0,
989 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000990 exit(1);
991 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600992 if (blocksize > 4096)
993 fprintf(stderr, _("Warning: blocksize %d not "
994 "usable on most systems.\n"),
995 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400996 if (blocksize > 0)
997 param.s_log_block_size =
998 int_log2(blocksize >>
999 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001000 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001001 case 'c': /* Check for bad blocks */
1002 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001003 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001004 break;
1005 case 'f':
1006 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001007 if (size < EXT2_MIN_BLOCK_SIZE ||
1008 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001009 com_err(program_name, 0,
1010 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001011 optarg);
1012 exit(1);
1013 }
1014 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001015 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001016 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001017 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001018 break;
1019 case 'g':
1020 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001021 if (*tmp) {
1022 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001023 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001024 exit(1);
1025 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001026 if ((param.s_blocks_per_group % 8) != 0) {
1027 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001028 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001029 exit(1);
1030 }
1031 break;
1032 case 'i':
1033 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001034 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1035 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001036 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001037 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -06001038 _("bad inode ratio %s (min %d/max %d"),
1039 optarg, EXT2_MIN_BLOCK_SIZE,
1040 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041 exit(1);
1042 }
1043 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001044 case 'J':
1045 parse_journal_opts(optarg);
1046 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001047 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001048 param.s_feature_compat |=
1049 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001050 if (!journal_size)
1051 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001052 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001053 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001054 bad_blocks_filename = malloc(strlen(optarg)+1);
1055 if (!bad_blocks_filename) {
1056 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001057 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001058 exit(1);
1059 }
1060 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001061 break;
1062 case 'm':
1063 reserved_ratio = strtoul(optarg, &tmp, 0);
1064 if (reserved_ratio > 50 || *tmp) {
1065 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001066 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001067 optarg);
1068 exit(1);
1069 }
1070 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001071 case 'n':
1072 noaction++;
1073 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001074 case 'o':
1075 creator_os = optarg;
1076 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001077 case 'r':
1078 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001079 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001080 param.s_feature_incompat = 0;
1081 param.s_feature_compat = 0;
1082 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001083 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001084 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001085 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001086 if (atoi(optarg))
1087 param.s_feature_ro_compat |=
1088 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1089 else
1090 param.s_feature_ro_compat &=
1091 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001092 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001093#ifdef EXT2_DYNAMIC_REV
1094 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001095 inode_size = strtoul(optarg, &tmp, 0);
1096 if (*tmp) {
1097 com_err(program_name, 0,
1098 _("bad inode size - %s"), optarg);
1099 exit(1);
1100 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001101 break;
1102#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001103 case 'N':
1104 num_inodes = atoi(optarg);
1105 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001106 case 'v':
1107 verbose = 1;
1108 break;
1109 case 'q':
1110 quiet = 1;
1111 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001112 case 'F':
1113 force = 1;
1114 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001115 case 'L':
1116 volume_label = optarg;
1117 break;
1118 case 'M':
1119 mount_dir = optarg;
1120 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001121 case 'O':
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001122 if (!strcmp(optarg, "none")) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001123 param.s_feature_compat = 0;
1124 param.s_feature_incompat = 0;
1125 param.s_feature_ro_compat = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001126 break;
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001127 }
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001128 if (e2p_edit_feature(optarg,
1129 &param.s_feature_compat,
1130 ok_features)) {
1131 fprintf(stderr,
1132 _("Invalid filesystem option set: %s\n"), optarg);
1133 exit(1);
1134 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001135 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001136 case 'R':
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001137 r_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001138 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001139 case 'S':
1140 super_only = 1;
1141 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001142 case 'T':
1143 fs_type = optarg;
1144 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001145 case 'V':
1146 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001147 show_version_only++;
1148 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001149 default:
1150 usage();
1151 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001152 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001153 usage();
1154 device_name = argv[optind];
1155 optind++;
1156 if (optind < argc) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001157 unsigned long long tmp2 = strtoull(argv[optind++], &tmp, 0);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001158
1159 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001160 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001161 argv[optind - 1]);
1162 exit(1);
1163 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001164 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001165 }
1166 if (optind < argc)
1167 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001168
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001169 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001170 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1171 E2FSPROGS_DATE);
1172
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001173 if (show_version_only) {
1174 fprintf(stderr, _("\tUsing %s\n"),
1175 error_message(EXT2_ET_BASE));
1176 exit(0);
1177 }
1178
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001179 /*
1180 * If there's no blocksize specified and there is a journal
1181 * device, use it to figure out the blocksize
1182 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001183 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001184 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001185 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001186
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001187#ifdef CONFIG_TESTIO_DEBUG
1188 io_ptr = test_io_manager;
1189 test_io_backing_manager = unix_io_manager;
1190#else
1191 io_ptr = unix_io_manager;
1192#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001193 retval = ext2fs_open(journal_device,
1194 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001195 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001196 if (retval) {
1197 com_err(program_name, retval,
1198 _("while trying to open journal device %s\n"),
1199 journal_device);
1200 exit(1);
1201 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001202 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001203 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001204 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001205 "minimum blocksize %d\n"), jfs->blocksize,
1206 -blocksize);
1207 exit(1);
1208 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001209 blocksize = jfs->blocksize;
1210 param.s_log_block_size =
1211 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1212 ext2fs_close(jfs);
1213 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001214
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001215 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001216 if (!force) {
1217 com_err(program_name, 0,
1218 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001219 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001220 proceed_question();
1221 }
1222 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1223 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001224 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001225 }
Theodore Ts'ocf7d5f12003-07-25 17:39:51 -04001226 if ((blocksize > 4096) &&
1227 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1228 fprintf(stderr, "\nWarning: some 2.4 kernels do not support "
1229 "blocksizes greater than 4096 \n\tusing ext3."
1230 " Use -b 4096 if this is an issue for you.\n\n");
Andreas Dilger932a4892002-05-16 03:20:07 -06001231
Andreas Dilgerfa724582001-08-04 01:18:34 -06001232 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1233 if (!fs_type)
1234 fs_type = "journal";
1235 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001236 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1237 param.s_feature_compat = 0;
1238 param.s_feature_ro_compat = 0;
1239 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001240 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1241 (param.s_feature_compat || param.s_feature_ro_compat ||
1242 param.s_feature_incompat))
1243 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1244
Theodore Ts'o74becf31997-04-26 14:37:06 +00001245 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001246 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001247 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001248
Theodore Ts'o3839e651997-04-26 13:21:57 +00001249 param.s_log_frag_size = param.s_log_block_size;
1250
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001251 if (noaction && param.s_blocks_count) {
1252 dev_size = param.s_blocks_count;
1253 retval = 0;
1254 } else
1255 retval = ext2fs_get_device_size(device_name,
1256 EXT2_BLOCK_SIZE(&param),
1257 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001258 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1259 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001260 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001261 exit(1);
1262 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001263 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001264 if (retval == EXT2_ET_UNIMPLEMENTED) {
1265 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001266 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001267 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001268 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001269 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001270 } else {
1271 if (dev_size == 0) {
1272 com_err(program_name, 0,
1273 _("Device size reported to be zero. "
1274 "Invalid partition specified, or\n\t"
1275 "partition table wasn't reread "
1276 "after running fdisk, due to\n\t"
1277 "a modified partition being busy "
1278 "and in use. You may need to reboot\n\t"
1279 "to re-read your partition table.\n"
1280 ));
1281 exit(1);
1282 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001283 param.s_blocks_count = dev_size;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001284 if (sys_page_size > EXT2_BLOCK_SIZE(&param))
1285 param.s_blocks_count &= ~((sys_page_size /
1286 EXT2_BLOCK_SIZE(&param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001287 }
1288
Theodore Ts'oa789d841998-03-30 01:20:55 +00001289 } else if (!force && (param.s_blocks_count > dev_size)) {
1290 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001291 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001292 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001293 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001294
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001295 /*
1296 * If the user asked for HAS_JOURNAL, then make sure a journal
1297 * gets created.
1298 */
1299 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1300 !journal_size)
1301 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001302
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001303 /* Set first meta blockgroup via an environment variable */
1304 /* (this is mostly for debugging purposes) */
1305 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1306 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1307 param.s_first_meta_bg = atoi(tmp);
1308
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001309 /* Get the hardware sector size, if available */
1310 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1311 if (retval) {
1312 com_err(program_name, retval,
1313 _("while trying to determine hardware sector size"));
1314 exit(1);
1315 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001316
Theodore Ts'o54434922003-12-07 01:28:50 -05001317 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001318 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001319
1320 set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001321 blocksize = EXT2_BLOCK_SIZE(&param);
1322
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001323 if (r_opts)
1324 parse_r_opts(&param, r_opts);
1325
1326 /* Since sparse_super is the default, we would only have a problem
1327 * here if it was explicitly disabled.
1328 */
1329 if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1330 !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1331 com_err(program_name, 0,
1332 _("reserved online resize blocks not supported "
1333 "on non-sparse filesystem"));
1334 exit(1);
1335 }
1336
Theodore Ts'o521e3681997-04-29 17:48:10 +00001337 if (param.s_blocks_per_group) {
1338 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o54434922003-12-07 01:28:50 -05001339 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001340 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001341 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001342 exit(1);
1343 }
1344 }
1345
Andreas Dilger932a4892002-05-16 03:20:07 -06001346 if (inode_size) {
1347 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1348 inode_size > EXT2_BLOCK_SIZE(&param) ||
1349 inode_size & (inode_size - 1)) {
1350 com_err(program_name, 0,
1351 _("bad inode size %d (min %d/max %d)"),
1352 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001353 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001354 exit(1);
1355 }
1356 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1357 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1358 "on most systems\n"),
1359 inode_size);
1360 param.s_inode_size = inode_size;
1361 }
1362
Theodore Ts'o3839e651997-04-26 13:21:57 +00001363 /*
1364 * Calculate number of inodes based on the inode ratio
1365 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001366 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001367 ((__u64) param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001368 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001369
1370 /*
1371 * Calculate number of blocks to reserve
1372 */
1373 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1374}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001375
Theodore Ts'o3839e651997-04-26 13:21:57 +00001376int main (int argc, char *argv[])
1377{
1378 errcode_t retval = 0;
1379 ext2_filsys fs;
1380 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001381 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001382 unsigned int i;
1383 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001384 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001385
1386#ifdef ENABLE_NLS
1387 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001388 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001389 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1390 textdomain(NLS_CAT_NAME);
1391#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001392 PRS(argc, argv);
1393
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001394#ifdef CONFIG_TESTIO_DEBUG
1395 io_ptr = test_io_manager;
1396 test_io_backing_manager = unix_io_manager;
1397#else
1398 io_ptr = unix_io_manager;
1399#endif
1400
Theodore Ts'o3839e651997-04-26 13:21:57 +00001401 /*
1402 * Initialize the superblock....
1403 */
1404 retval = ext2fs_initialize(device_name, 0, &param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001405 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001406 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001407 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001408 exit(1);
1409 }
1410
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001411 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001412 * Wipe out the old on-disk superblock
1413 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001414 if (!noaction)
1415 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001416
1417 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001418 * Generate a UUID for it...
1419 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001420 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001421
1422 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001423 * Initialize the directory index variables
1424 */
1425 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1426 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1427
1428 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001429 * Add "jitter" to the superblock's check interval so that we
1430 * don't check all the filesystems at the same time. We use a
1431 * kludgy hack of using the UUID to derive a random jitter value.
1432 */
1433 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1434 val += fs->super->s_uuid[i];
1435 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1436
1437 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001438 * Override the creator OS, if applicable
1439 */
1440 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001441 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001442 exit(1);
1443 }
1444
1445 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001446 * For the Hurd, we will turn off filetype since it doesn't
1447 * support it.
1448 */
1449 if (fs->super->s_creator_os == EXT2_OS_HURD)
1450 fs->super->s_feature_incompat &=
1451 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1452
1453 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001454 * Set the volume label...
1455 */
1456 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001457 memset(fs->super->s_volume_name, 0,
1458 sizeof(fs->super->s_volume_name));
1459 strncpy(fs->super->s_volume_name, volume_label,
1460 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001461 }
1462
1463 /*
1464 * Set the last mount directory
1465 */
1466 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001467 memset(fs->super->s_last_mounted, 0,
1468 sizeof(fs->super->s_last_mounted));
1469 strncpy(fs->super->s_last_mounted, mount_dir,
1470 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001471 }
1472
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001473 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001474 show_stats(fs);
1475
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001476 if (noaction)
1477 exit(0);
1478
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001479 if (fs->super->s_feature_incompat &
1480 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1481 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001482 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001483 }
1484
Theodore Ts'o3839e651997-04-26 13:21:57 +00001485 if (bad_blocks_filename)
1486 read_bb_file(fs, &bb_list, bad_blocks_filename);
1487 if (cflag)
1488 test_disk(fs, &bb_list);
1489
1490 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001491 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001492 retval = ext2fs_allocate_tables(fs);
1493 if (retval) {
1494 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001495 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001496 exit(1);
1497 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001498 if (super_only) {
1499 fs->super->s_state |= EXT2_ERROR_FS;
1500 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1501 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001502 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001503 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001504 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001505 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001506 blk_t ret_blk;
1507
1508#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001509 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001510#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001511
1512 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001513 * Wipe out any old MD RAID (or other) metadata at the end
1514 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001515 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001516 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001517 start = (blocks & ~(rsv - 1));
1518 if (start > rsv)
1519 start -= rsv;
1520 if (start > 0)
1521 retval = zero_blocks(fs, start, blocks - start,
1522 NULL, &ret_blk, NULL);
1523
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001524 if (retval) {
1525 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001526 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001527 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001528 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001529 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001530 create_root_dir(fs);
1531 create_lost_and_found(fs);
1532 reserve_inodes(fs);
1533 create_bad_block_inode(fs, bb_list);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001534 retval = ext2fs_create_resize_inode(fs);
1535 if (retval) {
1536 com_err("ext2fs_create_resize_inode", retval,
1537 _("while reserving blocks for online resize"));
1538 exit(1);
1539 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001540 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001541
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001542 if (journal_device) {
1543 ext2_filsys jfs;
1544
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001545 if (!force)
1546 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001547 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001548
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001549 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1550 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1551 fs->blocksize, unix_io_manager, &jfs);
1552 if (retval) {
1553 com_err(program_name, retval,
1554 _("while trying to open journal device %s\n"),
1555 journal_device);
1556 exit(1);
1557 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001558 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001559 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001560 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001561 fflush(stdout);
1562 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001563 retval = ext2fs_add_journal_device(fs, jfs);
1564 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001565 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001566 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001567 journal_device);
1568 exit(1);
1569 }
1570 if (!quiet)
1571 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001572 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001573 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001574 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001575 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001576
1577 if (!journal_blocks) {
1578 fs->super->s_feature_compat &=
1579 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1580 goto no_journal;
1581 }
1582 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001583 printf(_("Creating journal (%d blocks): "),
1584 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001585 fflush(stdout);
1586 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001587 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1588 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001589 if (retval) {
1590 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001591 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001592 exit(1);
1593 }
1594 if (!quiet)
1595 printf(_("done\n"));
1596 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001597no_journal:
1598
Theodore Ts'o3839e651997-04-26 13:21:57 +00001599 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001600 printf(_("Writing superblocks and "
1601 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001602 retval = ext2fs_flush(fs);
1603 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001604 fprintf(stderr,
1605 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001606 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001607 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001608 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001609 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1610 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001611 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001612 val = ext2fs_close(fs);
1613 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001614}