blob: 9e90c53084cf8896f3995b5bd10eeb89d17e236e [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'o27401561999-09-14 20:11:19 +000023#ifdef linux
24#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'of3db3561997-04-26 13:34:30 +000047#include <linux/ext2_fs.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000048
49#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'od48755e2000-12-09 14:36:04 +000070int cflag /* = 0 */ ;
71int verbose /* = 0 */ ;
72int quiet /* = 0 */ ;
73int super_only /* = 0 */ ;
74int force /* = 0 */ ;
75int noaction /* = 0 */ ;
76int journal_size /* = 0 */ ;
77int journal_flags /* = 0 */ ;
78char *bad_blocks_filename /* = 0 */ ;
79__u32 fs_stride /* = 0 */ ;
Theodore Ts'o3839e651997-04-26 13:21:57 +000080
81struct ext2_super_block param;
Theodore Ts'od48755e2000-12-09 14:36:04 +000082char *creator_os /* = NULL */ ;
83char *volume_label /* = NULL */ ;
84char *mount_dir /* = NULL */ ;
85char *journal_device /* = NULL */ ;
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
Theodore Ts'o63985322001-01-03 17:02:13 +000087static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000088{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000089 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'o16ad3332000-12-31 03:21:56 +000090 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000091 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
92 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000093 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
94 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000095 "[-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 program_name);
97 exit(1);
98}
99
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000100static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101{
102 int l = 0;
103
104 arg >>= 1;
105 while (arg) {
106 l++;
107 arg >>= 1;
108 }
109 return l;
110}
111
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000112static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000113{
114 int l;
115
116 for (l=0; arg ; l++)
117 arg = arg / 10;
118 return l;
119}
120
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000122 * This function sets the default parameters for a filesystem
123 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000124 * The type is specified by the user. The size is the maximum size
125 * (in megabytes) for which a set of parameters applies, with a size
126 * of zero meaning that it is the default parameter for the type.
127 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000128 */
129static char default_str[] = "default";
130struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000131 const char *type;
132 int size;
133 int blocksize;
134 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000135} settings[] = {
136 { default_str, 0, 4096, 8192 },
137 { default_str, 512, 1024, 4096 },
138 { default_str, 3, 1024, 8192 },
139 { "news", 0, 4096, 4096 },
140 { 0, 0, 0, 0},
141};
142
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000143static void set_fs_defaults(char *fs_type, struct ext2_super_block *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000144 int blocksize, int *inode_ratio)
145{
146 int megs;
147 int ratio = 0;
148 struct mke2fs_defaults *p;
149
Theodore Ts'o9094f281999-10-26 16:42:50 +0000150 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000151 1024);
152 if (inode_ratio)
153 ratio = *inode_ratio;
154 if (!fs_type)
155 fs_type = default_str;
156 for (p = settings; p->type; p++) {
157 if ((strcmp(p->type, fs_type) != 0) &&
158 (strcmp(p->type, default_str) != 0))
159 continue;
160 if ((p->size != 0) &&
161 (megs > p->size))
162 continue;
163 if (ratio == 0)
164 *inode_ratio = p->inode_ratio;
165 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000166 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000167 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000168 }
169 }
170 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000171 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000172}
173
174/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 * Helper function for read_bb_file and test_disk
176 */
177static void invalid_block(ext2_filsys fs, blk_t blk)
178{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000179 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 return;
181}
182
183/*
184 * Reads the bad blocks list from a file
185 */
186static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
187 const char *bad_blocks_file)
188{
189 FILE *f;
190 errcode_t retval;
191
192 f = fopen(bad_blocks_file, "r");
193 if (!f) {
194 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000195 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 exit(1);
197 }
198 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
199 fclose (f);
200 if (retval) {
201 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000202 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203 exit(1);
204 }
205}
206
207/*
208 * Runs the badblocks program to test the disk
209 */
210static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
211{
212 FILE *f;
213 errcode_t retval;
214 char buf[1024];
215
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000216 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
217 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 fs->super->s_blocks_count);
219 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000220 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000221 f = popen(buf, "r");
222 if (!f) {
223 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000224 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225 exit(1);
226 }
227 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000228 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229 if (retval) {
230 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000231 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232 exit(1);
233 }
234}
235
236static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
237{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000238 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 int must_be_good;
240 blk_t blk;
241 badblocks_iterate bb_iter;
242 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000243 blk_t group_block;
244 int group;
245 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000246
247 if (!bb_list)
248 return;
249
250 /*
251 * The primary superblock and group descriptors *must* be
252 * good; if not, abort.
253 */
254 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
255 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
256 if (badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000257 fprintf(stderr, _("Block %d in primary "
258 "superblock/group descriptor area bad.\n"), i);
259 fprintf(stderr, _("Blocks %d through %d must be good "
260 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000262 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263 exit(1);
264 }
265 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000266
267 /*
268 * See if any of the bad blocks are showing up in the backup
269 * superblocks and/or group descriptors. If so, issue a
270 * warning and adjust the block counts appropriately.
271 */
272 group_block = fs->super->s_first_data_block +
273 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000274
275 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000276 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000277 for (j=0; j < fs->desc_blocks+1; j++) {
278 if (badblocks_list_test(bb_list, group_block +
279 j)) {
280 if (!group_bad)
281 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000282_("Warning: the backup superblock/group descriptors at block %d contain\n"
283" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000284 group_block);
285 group_bad++;
286 group = ext2fs_group_of_blk(fs, group_block+j);
287 fs->group_desc[group].bg_free_blocks_count++;
288 fs->super->s_free_blocks_count++;
289 }
290 }
291 group_block += fs->super->s_blocks_per_group;
292 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000293
294 /*
295 * Mark all the bad blocks as used...
296 */
297 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
298 if (retval) {
299 com_err("badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000300 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 exit(1);
302 }
303 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000304 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 badblocks_list_iterate_end(bb_iter);
306}
307
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000308static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309{
310 errcode_t retval;
311 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000312 int i, j, num, count;
313 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000314 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000315 int sync_kludge = 0;
316 char *mke2fs_sync;
317
318 mke2fs_sync = getenv("MKE2FS_SYNC");
319 if (mke2fs_sync)
320 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321
322 buf = malloc(fs->blocksize * STRIDE_LENGTH);
323 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000324 com_err("malloc", ENOMEM,
325 _("while allocating zeroizing buffer"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000326 exit(1);
327 }
328 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000329
330 /*
331 * Figure out how many digits we need
332 */
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000333 i = int_log10(fs->group_desc_count);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000334 sprintf(format, "%%%dd/%%%dld", i, i);
335 memset(backup, '\b', sizeof(backup)-1);
336 backup[sizeof(backup)-1] = 0;
337 if ((2*i)+1 < sizeof(backup))
338 backup[(2*i)+1] = 0;
339
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000341 printf(_("Writing inode tables: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342 for (i = 0; i < fs->group_desc_count; i++) {
343 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000344 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000346 blk = fs->group_desc[i].bg_inode_table;
347 num = fs->inode_blocks_per_group;
348
349 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
350 if (num-j > STRIDE_LENGTH)
351 count = STRIDE_LENGTH;
352 else
353 count = num - j;
354 retval = io_channel_write_blk(fs->io, blk, count, buf);
355 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000356 printf(_("Warning: could not write %d blocks "
357 "in inode table starting at %d: %s\n"),
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000358 count, blk, error_message(retval));
359 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000361 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000362 if (sync_kludge) {
363 if (sync_kludge == 1)
364 sync();
365 else if ((i % sync_kludge) == 0)
366 sync();
367 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000368 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000369 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000371 fputs(_("done \n"), stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000372}
373
374static void create_root_dir(ext2_filsys fs)
375{
376 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000377 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378
379 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
380 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000381 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382 exit(1);
383 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000384 if (geteuid()) {
385 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
386 if (retval) {
387 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000388 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000389 exit(1);
390 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000391 inode.i_uid = getuid();
392 if (inode.i_uid)
393 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000394 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
395 if (retval) {
396 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000397 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000398 exit(1);
399 }
400 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000401}
402
403static void create_lost_and_found(ext2_filsys fs)
404{
405 errcode_t retval;
406 ino_t ino;
407 const char *name = "lost+found";
408 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000409 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410
411 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
412 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000413 com_err("ext2fs_mkdir", retval,
414 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415 exit(1);
416 }
417
418 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
419 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000420 com_err("ext2_lookup", retval,
421 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422 exit(1);
423 }
424
425 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000426 if ((lpf_size += fs->blocksize) >= 16*1024)
427 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428 retval = ext2fs_expand_dir(fs, ino);
429 if (retval) {
430 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000431 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 exit(1);
433 }
434 }
435}
436
437static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
438{
439 errcode_t retval;
440
Theodore Ts'of3db3561997-04-26 13:34:30 +0000441 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 fs->group_desc[0].bg_free_inodes_count--;
443 fs->super->s_free_inodes_count--;
444 retval = ext2fs_update_bb_inode(fs, bb_list);
445 if (retval) {
446 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000447 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 exit(1);
449 }
450
451}
452
453static void reserve_inodes(ext2_filsys fs)
454{
455 ino_t i;
456 int group;
457
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000458 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000459 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 group = ext2fs_group_of_ino(fs, i);
461 fs->group_desc[group].bg_free_inodes_count--;
462 fs->super->s_free_inodes_count--;
463 }
464 ext2fs_mark_ib_dirty(fs);
465}
466
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000467static void zap_sector(ext2_filsys fs, int sect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000468{
469 char buf[512];
470 int retval;
471
472 memset(buf, 0, 512);
473
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000474 io_channel_set_blksize(fs->io, 512);
475 retval = io_channel_write_blk(fs->io, sect, -512, buf);
476 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000477 if (retval)
Theodore Ts'oe294cf22000-10-24 18:41:44 +0000478 printf(_("Warning: could not erase sector %d: %s\n"), sect,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000479 error_message(retval));
480}
481
482
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483static void show_stats(ext2_filsys fs)
484{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000485 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000486 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000488 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000489
490 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000491 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000493
494 memset(buf, 0, sizeof(buf));
495 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000496 printf(_("Filesystem label=%s\n"), buf);
497 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000498 switch (fs->super->s_creator_os) {
499 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000500 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000501 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000502 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000503 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000504 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000505 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000506 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000507 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000508 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000509 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000511 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 s->s_r_blocks_count,
513 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000514 printf(_("First data block=%u\n"), s->s_first_data_block);
515 if (fs->group_desc_count > 1)
516 printf(_("%lu block groups\n"), fs->group_desc_count);
517 else
518 printf(_("%lu block group\n"), fs->group_desc_count);
519 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000520 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000521 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522
523 if (fs->group_desc_count == 1) {
524 printf("\n");
525 return;
526 }
527
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000528 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529 group_block = s->s_first_data_block;
530 col_left = 0;
531 for (i = 1; i < fs->group_desc_count; i++) {
532 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000533 if (!ext2fs_bg_has_super(fs, i))
534 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000535 if (i != 1)
536 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000537 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000538 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000540 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000542 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000543 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 }
545 printf("\n\n");
546}
547
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000548/*
549 * Set the S_CREATOR_OS field. Return true if OS is known,
550 * otherwise, 0.
551 */
552static int set_os(struct ext2_super_block *sb, char *os)
553{
554 if (isdigit (*os))
555 sb->s_creator_os = atoi (os);
556 else if (strcasecmp(os, "linux") == 0)
557 sb->s_creator_os = EXT2_OS_LINUX;
558 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
559 sb->s_creator_os = EXT2_OS_HURD;
560 else if (strcasecmp(os, "masix") == 0)
561 sb->s_creator_os = EXT2_OS_MASIX;
562 else
563 return 0;
564 return 1;
565}
566
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000567#define PATH_SET "PATH=/sbin"
568
Theodore Ts'od163b091997-10-03 17:42:28 +0000569static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000570{
571 char *buf, *token, *next, *p, *arg;
572 int len;
573 int raid_usage = 0;
574
575 len = strlen(opts);
576 buf = malloc(len+1);
577 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000578 fprintf(stderr, _("Couldn't allocate memory to parse "
579 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000580 exit(1);
581 }
582 strcpy(buf, opts);
583 for (token = buf; token && *token; token = next) {
584 p = strchr(token, ',');
585 next = 0;
586 if (p) {
587 *p = 0;
588 next = p+1;
589 }
590 arg = strchr(token, '=');
591 if (arg) {
592 *arg = 0;
593 arg++;
594 }
595 if (strcmp(token, "stride") == 0) {
596 if (!arg) {
597 raid_usage++;
598 continue;
599 }
600 fs_stride = strtoul(arg, &p, 0);
601 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000602 fprintf(stderr,
603 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000604 raid_usage++;
605 continue;
606 }
607 } else
608 raid_usage++;
609 }
610 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000611 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000612 "Raid options are separated by commas, "
613 "and may take an argument which\n"
614 "\tis set off by an equals ('=') sign.\n\n"
615 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000616 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000617 exit(1);
618 }
619}
620
Theodore Ts'o896938d1999-10-23 01:04:50 +0000621static __u32 ok_features[3] = {
622 0, /* Compat */
623 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
624 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
625};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000626
627
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628static void PRS(int argc, char *argv[])
629{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000630 int c;
631 int size;
632 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000633 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000634 int blocksize = 0;
635 int inode_ratio = 0;
636 int reserved_ratio = 5;
637 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000638 errcode_t retval;
Theodore Ts'of6f65832000-10-25 03:01:37 +0000639 int sparse_option = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000640 char * oldpath = getenv("PATH");
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000641 struct ext2_super_block *param_ext2 = &param;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000642 char * raid_opts = 0;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000643 char * journal_opts = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000644 char * fs_type = 0;
Theodore Ts'of6f65832000-10-25 03:01:37 +0000645 const char * feature_set = "filetype";
Theodore Ts'o4a600561999-10-26 14:35:51 +0000646 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000647#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000648 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000649
650 if (uname(&ut)) {
651 perror("uname");
652 exit(1);
653 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000654 if ((ut.release[0] == '1') ||
655 (ut.release[0] == '2' && ut.release[1] == '.' &&
656 ut.release[2] < '2' && ut.release[3] == '.'))
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000657 feature_set = NULL;
Theodore Ts'o27401561999-09-14 20:11:19 +0000658#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000659 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000660 if (oldpath) {
661 char *newpath;
662
663 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
664 strcpy (newpath, PATH_SET);
665 strcat (newpath, ":");
666 strcat (newpath, oldpath);
667 putenv (newpath);
668 } else
669 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670
671 setbuf(stdout, NULL);
672 setbuf(stderr, NULL);
673 initialize_ext2_error_table();
674 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000675 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000676
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000677 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000678 E2FSPROGS_VERSION, E2FSPROGS_DATE,
679 EXT2FS_VERSION, EXT2FS_DATE);
680 if (argc && *argv)
681 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000682 while ((c = getopt (argc, argv,
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000683 "b:cf:g:i:j:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684 switch (c) {
685 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000686 blocksize = strtoul(optarg, &tmp, 0);
687 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000688 com_err(program_name, 0,
689 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690 exit(1);
691 }
692 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000693 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000694 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 break;
696 case 'c':
697 case 't': /* Check for bad blocks */
698 cflag = 1;
699 break;
700 case 'f':
701 size = strtoul(optarg, &tmp, 0);
702 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000703 com_err(program_name, 0,
704 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000705 optarg);
706 exit(1);
707 }
708 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000709 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000710 printf(_("Warning: fragments not supported. "
711 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712 break;
713 case 'g':
714 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000715 if (*tmp) {
716 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000717 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000718 exit(1);
719 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000720 if ((param.s_blocks_per_group % 8) != 0) {
721 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000722 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723 exit(1);
724 }
725 break;
726 case 'i':
727 inode_ratio = strtoul(optarg, &tmp, 0);
728 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
729 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000730 com_err(program_name, 0,
731 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000732 exit(1);
733 }
734 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000735 case 'j':
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000736 journal_opts = optarg;
737 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000738 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000739 bad_blocks_filename = malloc(strlen(optarg)+1);
740 if (!bad_blocks_filename) {
741 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000742 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000743 exit(1);
744 }
745 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000746 break;
747 case 'm':
748 reserved_ratio = strtoul(optarg, &tmp, 0);
749 if (reserved_ratio > 50 || *tmp) {
750 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000751 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752 optarg);
753 exit(1);
754 }
755 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000756 case 'n':
757 noaction++;
758 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000759 case 'o':
760 creator_os = optarg;
761 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000762 case 'r':
763 param.s_rev_level = atoi(optarg);
764 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000765 case 's':
766 sparse_option = atoi(optarg);
767 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000768#ifdef EXT2_DYNAMIC_REV
769 case 'I':
770 param.s_inode_size = atoi(optarg);
771 break;
772#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000773 case 'N':
774 num_inodes = atoi(optarg);
775 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000776 case 'v':
777 verbose = 1;
778 break;
779 case 'q':
780 quiet = 1;
781 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000782 case 'F':
783 force = 1;
784 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000785 case 'L':
786 volume_label = optarg;
787 break;
788 case 'M':
789 mount_dir = optarg;
790 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000791 case 'O':
792 feature_set = optarg;
793 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000794 case 'R':
795 raid_opts = optarg;
796 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000797 case 'S':
798 super_only = 1;
799 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000800 case 'T':
801 fs_type = optarg;
802 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000803 case 'V':
804 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000805 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000806 error_message(EXT2_ET_BASE));
807 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000808 default:
809 usage();
810 }
811 if (optind == argc)
812 usage();
813 device_name = argv[optind];
814 optind++;
815 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000816 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
817
818 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000819 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000820 argv[optind - 1]);
821 exit(1);
822 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000823 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000824 }
825 if (optind < argc)
826 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000827
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000828 if (raid_opts)
829 parse_raid_opts(raid_opts);
830
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000831 if (journal_opts)
832 parse_journal_opts(journal_opts);
833
Theodore Ts'o74becf31997-04-26 14:37:06 +0000834 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000835 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +0000836 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000837
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838 param.s_log_frag_size = param.s_log_block_size;
839
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000840 if (noaction && param.s_blocks_count) {
841 dev_size = param.s_blocks_count;
842 retval = 0;
843 } else
844 retval = ext2fs_get_device_size(device_name,
845 EXT2_BLOCK_SIZE(&param),
846 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000847 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
848 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000849 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000850 exit(1);
851 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000852 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000853 if (retval == EXT2_ET_UNIMPLEMENTED) {
854 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000855 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +0000856 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000857 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000858 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +0000859 } else {
860 if (dev_size == 0) {
861 com_err(program_name, 0,
862 _("Device size reported to be zero. "
863 "Invalid partition specified, or\n\t"
864 "partition table wasn't reread "
865 "after running fdisk, due to\n\t"
866 "a modified partition being busy "
867 "and in use. You may need to reboot\n\t"
868 "to re-read your partition table.\n"
869 ));
870 exit(1);
871 }
Theodore Ts'oa789d841998-03-30 01:20:55 +0000872 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +0000873 }
874
Theodore Ts'oa789d841998-03-30 01:20:55 +0000875 } else if (!force && (param.s_blocks_count > dev_size)) {
876 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000877 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000878 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000879 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000880
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000881 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
882
Theodore Ts'o521e3681997-04-29 17:48:10 +0000883 if (param.s_blocks_per_group) {
884 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000885 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +0000886 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000887 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +0000888 exit(1);
889 }
890 }
891
Theodore Ts'o3839e651997-04-26 13:21:57 +0000892 /*
893 * Calculate number of inodes based on the inode ratio
894 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000895 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +0000896 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000897 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000898
899 /*
900 * Calculate number of blocks to reserve
901 */
902 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000903
Theodore Ts'of6f65832000-10-25 03:01:37 +0000904 /* Turn off features not supported by the earlier filesystem version */
905 if (param.s_rev_level == 0) {
906 sparse_option = 0;
907 feature_set = NULL;
908 }
Theodore Ts'o27401561999-09-14 20:11:19 +0000909 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +0000910 param_ext2->s_feature_ro_compat |=
911 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'of6f65832000-10-25 03:01:37 +0000912
Theodore Ts'o896938d1999-10-23 01:04:50 +0000913 if (feature_set && !strncasecmp(feature_set, "none", 4))
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000914 feature_set = NULL;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000915 if (feature_set && e2p_edit_feature(feature_set,
916 &param_ext2->s_feature_compat,
917 ok_features)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000918 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o896938d1999-10-23 01:04:50 +0000919 feature_set);
920 exit(1);
921 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000922}
923
924int main (int argc, char *argv[])
925{
926 errcode_t retval = 0;
927 ext2_filsys fs;
928 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000929 int journal_blocks;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000930
931#ifdef ENABLE_NLS
932 setlocale(LC_MESSAGES, "");
933 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
934 textdomain(NLS_CAT_NAME);
935#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000936 PRS(argc, argv);
937
Theodore Ts'o3839e651997-04-26 13:21:57 +0000938 /*
939 * Initialize the superblock....
940 */
941 retval = ext2fs_initialize(device_name, 0, &param,
942 unix_io_manager, &fs);
943 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000944 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000945 exit(1);
946 }
947
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000948 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000949 * Wipe out the old on-disk superblock
950 */
951 zap_sector(fs, 2);
952
953 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000954 * Generate a UUID for it...
955 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000956 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000957
958 /*
959 * Override the creator OS, if applicable
960 */
961 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000962 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000963 exit(1);
964 }
965
966 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000967 * For the Hurd, we will turn off filetype since it doesn't
968 * support it.
969 */
970 if (fs->super->s_creator_os == EXT2_OS_HURD)
971 fs->super->s_feature_incompat &=
972 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
973
974 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000975 * Set the volume label...
976 */
977 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000978 memset(fs->super->s_volume_name, 0,
979 sizeof(fs->super->s_volume_name));
980 strncpy(fs->super->s_volume_name, volume_label,
981 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000982 }
983
984 /*
985 * Set the last mount directory
986 */
987 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000988 memset(fs->super->s_last_mounted, 0,
989 sizeof(fs->super->s_last_mounted));
990 strncpy(fs->super->s_last_mounted, mount_dir,
991 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000992 }
993
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000994 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000995 show_stats(fs);
996
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000997 if (noaction)
998 exit(0);
999
Theodore Ts'o3839e651997-04-26 13:21:57 +00001000 if (bad_blocks_filename)
1001 read_bb_file(fs, &bb_list, bad_blocks_filename);
1002 if (cflag)
1003 test_disk(fs, &bb_list);
1004
1005 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001006 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001007 retval = ext2fs_allocate_tables(fs);
1008 if (retval) {
1009 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001010 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001011 exit(1);
1012 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001013 if (super_only) {
1014 fs->super->s_state |= EXT2_ERROR_FS;
1015 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1016 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001017 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001018 create_root_dir(fs);
1019 create_lost_and_found(fs);
1020 reserve_inodes(fs);
1021 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001022#ifdef ZAP_BOOTBLOCK
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001023 zap_sector(fs, 0);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001024#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001025 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001026
Theodore Ts'od48755e2000-12-09 14:36:04 +00001027 journal_blocks = journal_size * 1024 / (fs->blocksize / 1024);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001028 if (journal_device) {
1029 if (!force)
1030 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001031 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001032
1033 if (!quiet)
1034 printf(_("Creating journal on device %s: "),
1035 journal_device);
Theodore Ts'od48755e2000-12-09 14:36:04 +00001036 retval = ext2fs_add_journal_device(fs, journal_device,
1037 journal_blocks,
1038 journal_flags);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001039 if(retval) {
1040 com_err (program_name, retval,
1041 _("while trying to create journal on device %s"),
1042 journal_device);
1043 exit(1);
1044 }
1045 if (!quiet)
1046 printf(_("done\n"));
1047 } else if (journal_size) {
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001048 if (!quiet)
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001049 printf(_("Creating journal (%d blocks): "),
1050 journal_blocks);
Theodore Ts'o63985322001-01-03 17:02:13 +00001051 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1052 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001053 if (retval) {
1054 com_err (program_name, retval,
1055 _("while trying to create journal"));
1056 exit(1);
1057 }
1058 if (!quiet)
1059 printf(_("done\n"));
1060 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001061
1062 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001063 printf(_("Writing superblocks and "
1064 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001065 retval = ext2fs_flush(fs);
1066 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001067 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001068 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001069 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001070 printf(_("done\n"));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001071 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001072 return 0;
1073}