blob: 3b4401516296377ea08a61651b87158373f786be [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12/* Usage: mke2fs [options] device
13 *
14 * The device may be a block device or a image of one, but this isn't
15 * enforced (but it's not much fun on a character device :-).
16 */
17
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000018#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <time.h>
Theodore Ts'o756df352002-03-07 20:52:12 -050023#ifdef __linux__
Theodore Ts'o27401561999-09-14 20:11:19 +000024#include <sys/utsname.h>
25#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000028#else
29extern char *optarg;
30extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#endif
32#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
35#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000037#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000045#include <sys/types.h>
46
Theodore Ts'o54c637d2001-05-14 11:45:38 +000047#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000048#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000049#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000050#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000051#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000052#include "util.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000053#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000054#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
56#define STRIDE_LENGTH 8
57
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000058#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000059#define ZAP_BOOTBLOCK
60#endif
61
Theodore Ts'o3839e651997-04-26 13:21:57 +000062extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000063extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000064
65const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000066const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000067
68/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000069int cflag;
70int verbose;
71int quiet;
72int super_only;
73int force;
74int noaction;
75int journal_size;
76int journal_flags;
77char *bad_blocks_filename;
78__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079
80struct ext2_super_block param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000081char *creator_os;
82char *volume_label;
83char *mount_dir;
84char *journal_device;
85int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
Theodore Ts'o31e29a12002-05-17 10:53:07 -040087int sys_page_size = 4096;
88
Theodore Ts'o63985322001-01-03 17:02:13 +000089static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000090{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000091 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000092 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000093 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
94 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000095 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +000096 "[-r fs-revision] [-R raid_opts] [-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 program_name);
98 exit(1);
99}
100
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000101static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102{
103 int l = 0;
104
105 arg >>= 1;
106 while (arg) {
107 l++;
108 arg >>= 1;
109 }
110 return l;
111}
112
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000113static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000114{
115 int l;
116
117 for (l=0; arg ; l++)
118 arg = arg / 10;
119 return l;
120}
121
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000123 * This function sets the default parameters for a filesystem
124 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000125 * The type is specified by the user. The size is the maximum size
126 * (in megabytes) for which a set of parameters applies, with a size
127 * of zero meaning that it is the default parameter for the type.
128 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000129 */
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400130#define DEF_MAX_BLOCKSIZE -1
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000131static char default_str[] = "default";
132struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000133 const char *type;
134 int size;
135 int blocksize;
136 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000137} settings[] = {
138 { default_str, 0, 4096, 8192 },
139 { default_str, 512, 1024, 4096 },
140 { default_str, 3, 1024, 8192 },
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000141 { "journal", 0, 4096, 8192 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000142 { "news", 0, 4096, 4096 },
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400143 { "largefile", 0, DEF_MAX_BLOCKSIZE, 1024 * 1024 },
144 { "largefile4", 0, DEF_MAX_BLOCKSIZE, 4096 * 1024 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000145 { 0, 0, 0, 0},
146};
147
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500148static void set_fs_defaults(const char *fs_type,
149 struct ext2_super_block *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000150 int blocksize, int *inode_ratio)
151{
152 int megs;
153 int ratio = 0;
154 struct mke2fs_defaults *p;
155
Andreas Dilger932a4892002-05-16 03:20:07 -0600156 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000157 if (inode_ratio)
158 ratio = *inode_ratio;
159 if (!fs_type)
160 fs_type = default_str;
161 for (p = settings; p->type; p++) {
162 if ((strcmp(p->type, fs_type) != 0) &&
163 (strcmp(p->type, default_str) != 0))
164 continue;
165 if ((p->size != 0) &&
166 (megs > p->size))
167 continue;
168 if (ratio == 0)
169 *inode_ratio = p->inode_ratio;
170 if (blocksize == 0) {
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400171 if (p->blocksize == DEF_MAX_BLOCKSIZE)
172 p->blocksize = sys_page_size;
Theodore Ts'o9094f281999-10-26 16:42:50 +0000173 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000174 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000175 }
176 }
177 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000178 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000179}
180
181/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182 * Helper function for read_bb_file and test_disk
183 */
184static void invalid_block(ext2_filsys fs, blk_t blk)
185{
Theodore Ts'o66938372002-03-08 00:14:46 -0500186 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187 return;
188}
189
190/*
191 * Reads the bad blocks list from a file
192 */
193static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
194 const char *bad_blocks_file)
195{
196 FILE *f;
197 errcode_t retval;
198
199 f = fopen(bad_blocks_file, "r");
200 if (!f) {
201 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000202 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203 exit(1);
204 }
205 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
206 fclose (f);
207 if (retval) {
208 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000209 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210 exit(1);
211 }
212}
213
214/*
215 * Runs the badblocks program to test the disk
216 */
217static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
218{
219 FILE *f;
220 errcode_t retval;
221 char buf[1024];
222
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500223 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
224 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
225 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000227 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 f = popen(buf, "r");
229 if (!f) {
230 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000231 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232 exit(1);
233 }
234 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000235 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236 if (retval) {
237 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000238 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 exit(1);
240 }
241}
242
243static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
244{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000245 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000246 int must_be_good;
247 blk_t blk;
248 badblocks_iterate bb_iter;
249 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000250 blk_t group_block;
251 int group;
252 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000253
254 if (!bb_list)
255 return;
256
257 /*
258 * The primary superblock and group descriptors *must* be
259 * good; if not, abort.
260 */
261 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
262 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000263 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000264 fprintf(stderr, _("Block %d in primary "
265 "superblock/group descriptor area bad.\n"), i);
266 fprintf(stderr, _("Blocks %d through %d must be good "
267 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000269 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 exit(1);
271 }
272 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000273
274 /*
275 * See if any of the bad blocks are showing up in the backup
276 * superblocks and/or group descriptors. If so, issue a
277 * warning and adjust the block counts appropriately.
278 */
279 group_block = fs->super->s_first_data_block +
280 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000281
282 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000283 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000284 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000285 if (ext2fs_badblocks_list_test(bb_list,
286 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000287 if (!group_bad)
288 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000289_("Warning: the backup superblock/group descriptors at block %d contain\n"
290" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000291 group_block);
292 group_bad++;
293 group = ext2fs_group_of_blk(fs, group_block+j);
294 fs->group_desc[group].bg_free_blocks_count++;
295 fs->super->s_free_blocks_count++;
296 }
297 }
298 group_block += fs->super->s_blocks_per_group;
299 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300
301 /*
302 * Mark all the bad blocks as used...
303 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000304 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000306 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000307 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 exit(1);
309 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000310 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000311 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000312 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313}
314
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000315/*
316 * These functions implement a generalized progress meter.
317 */
318struct progress_struct {
319 char format[20];
320 char backup[80];
321 __u32 max;
322};
323
324static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500325 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000326{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000327 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000328
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000329 memset(progress, 0, sizeof(struct progress_struct));
330 if (quiet)
331 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000332
333 /*
334 * Figure out how many digits we need
335 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000336 i = int_log10(max);
337 sprintf(progress->format, "%%%dd/%%%dld", i, i);
338 memset(progress->backup, '\b', sizeof(progress->backup)-1);
339 progress->backup[sizeof(progress->backup)-1] = 0;
340 if ((2*i)+1 < sizeof(progress->backup))
341 progress->backup[(2*i)+1] = 0;
342 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000343
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000344 fputs(label, stdout);
345 fflush(stdout);
346}
347
348static void progress_update(struct progress_struct *progress, __u32 val)
349{
350 if (progress->format[0] == 0)
351 return;
352 printf(progress->format, val, progress->max);
353 fputs(progress->backup, stdout);
354}
355
356static void progress_close(struct progress_struct *progress)
357{
358 if (progress->format[0] == 0)
359 return;
360 fputs(_("done \n"), stdout);
361}
362
363
364/*
365 * Helper function which zeros out _num_ blocks starting at _blk_. In
366 * case of an error, the details of the error is returned via _ret_blk_
367 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
368 * success, and an error code on an error.
369 *
370 * As a special case, if the first argument is NULL, then it will
371 * attempt to free the static zeroizing buffer. (This is to keep
372 * programs that check for memory leaks happy.)
373 */
374static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
375 struct progress_struct *progress,
376 blk_t *ret_blk, int *ret_count)
377{
378 int j, count, next_update, next_update_incr;
379 static char *buf;
380 errcode_t retval;
381
382 /* If fs is null, clean up the static buffer and return */
383 if (!fs) {
384 if (buf) {
385 free(buf);
386 buf = 0;
387 }
388 return 0;
389 }
390 /* Allocate the zeroizing buffer if necessary */
391 if (!buf) {
392 buf = malloc(fs->blocksize * STRIDE_LENGTH);
393 if (!buf) {
394 com_err("malloc", ENOMEM,
395 _("while allocating zeroizing buffer"));
396 exit(1);
397 }
398 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
399 }
400 /* OK, do the write loop */
401 next_update = 0;
402 next_update_incr = num / 100;
403 if (next_update_incr < 1)
404 next_update_incr = 1;
405 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
406 if (num-j > STRIDE_LENGTH)
407 count = STRIDE_LENGTH;
408 else
409 count = num - j;
410 retval = io_channel_write_blk(fs->io, blk, count, buf);
411 if (retval) {
412 if (ret_count)
413 *ret_count = count;
414 if (ret_blk)
415 *ret_blk = blk;
416 return retval;
417 }
418 if (progress && j > next_update) {
419 next_update += num / 100;
420 progress_update(progress, blk);
421 }
422 }
423 return 0;
424}
425
426static void write_inode_tables(ext2_filsys fs)
427{
428 errcode_t retval;
429 blk_t blk;
430 int i, num;
431 struct progress_struct progress;
432
433 if (quiet)
434 memset(&progress, 0, sizeof(progress));
435 else
436 progress_init(&progress, _("Writing inode tables: "),
437 fs->group_desc_count);
438
Theodore Ts'o3839e651997-04-26 13:21:57 +0000439 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000440 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000442 blk = fs->group_desc[i].bg_inode_table;
443 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000444
445 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
446 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500447 fprintf(stderr, _("\nCould not write %d blocks "
448 "in inode table starting at %d: %s\n"),
449 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000450 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000451 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000452 if (sync_kludge) {
453 if (sync_kludge == 1)
454 sync();
455 else if ((i % sync_kludge) == 0)
456 sync();
457 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000459 zero_blocks(0, 0, 0, 0, 0, 0);
460 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461}
462
463static void create_root_dir(ext2_filsys fs)
464{
465 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000466 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467
468 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
469 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000470 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471 exit(1);
472 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000473 if (geteuid()) {
474 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
475 if (retval) {
476 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000477 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000478 exit(1);
479 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000480 inode.i_uid = getuid();
481 if (inode.i_uid)
482 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000483 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
484 if (retval) {
485 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000486 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000487 exit(1);
488 }
489 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490}
491
492static void create_lost_and_found(ext2_filsys fs)
493{
494 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000495 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 const char *name = "lost+found";
497 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000498 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499
Theodore Ts'o6a525062001-12-24 09:40:00 -0500500 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
502 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000503 com_err("ext2fs_mkdir", retval,
504 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 exit(1);
506 }
507
508 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
509 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000510 com_err("ext2_lookup", retval,
511 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 exit(1);
513 }
514
515 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000516 if ((lpf_size += fs->blocksize) >= 16*1024)
517 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 retval = ext2fs_expand_dir(fs, ino);
519 if (retval) {
520 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000521 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 exit(1);
523 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500524 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525}
526
527static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
528{
529 errcode_t retval;
530
Theodore Ts'of3db3561997-04-26 13:34:30 +0000531 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 fs->group_desc[0].bg_free_inodes_count--;
533 fs->super->s_free_inodes_count--;
534 retval = ext2fs_update_bb_inode(fs, bb_list);
535 if (retval) {
536 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000537 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538 exit(1);
539 }
540
541}
542
543static void reserve_inodes(ext2_filsys fs)
544{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000545 ext2_ino_t i;
546 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000548 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000549 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000550 group = ext2fs_group_of_ino(fs, i);
551 fs->group_desc[group].bg_free_inodes_count--;
552 fs->super->s_free_inodes_count--;
553 }
554 ext2fs_mark_ib_dirty(fs);
555}
556
Theodore Ts'o756df352002-03-07 20:52:12 -0500557#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500558#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500559#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500560
Theodore Ts'o04a96852001-08-30 21:55:26 -0400561static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000562{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400563 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000564 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500565 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000566
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400567 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600568 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500569 printf(_("Out of memory erasing sectors %d-%d\n"),
570 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600571 exit(1);
572 }
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400573 memset(buf, 0, 512*nsect);
Theodore Ts'o756df352002-03-07 20:52:12 -0500574
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500575 if (sect == 0) {
576 /* Check for a BSD disklabel, and don't erase it if so */
577 retval = io_channel_read_blk(fs->io, 0, -512, buf);
578 if (retval)
579 fprintf(stderr,
580 _("Warning: could not read block 0: %s\n"),
581 error_message(retval));
582 else {
583 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
584 if ((*magic == BSD_DISKMAGIC) ||
585 (*magic == BSD_MAGICDISK))
586 return;
587 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500588 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500589
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000590 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400591 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000592 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400593 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000594 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500595 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
596 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000597}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000598
599static void create_journal_dev(ext2_filsys fs)
600{
601 struct progress_struct progress;
602 errcode_t retval;
603 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000604 blk_t blk;
605 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000606
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000607 retval = ext2fs_create_journal_superblock(fs,
608 fs->super->s_blocks_count, 0, &buf);
609 if (retval) {
610 com_err("create_journal_dev", retval,
611 _("while initializing journal superblock"));
612 exit(1);
613 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000614 if (quiet)
615 memset(&progress, 0, sizeof(progress));
616 else
617 progress_init(&progress, _("Zeroing journal device: "),
618 fs->super->s_blocks_count);
619
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000620 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
621 &progress, &blk, &count);
622 if (retval) {
623 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000624 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000625 blk, count);
626 exit(1);
627 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000628 zero_blocks(0, 0, 0, 0, 0, 0);
629
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000630 retval = io_channel_write_blk(fs->io,
631 fs->super->s_first_data_block+1,
632 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000633 if (retval) {
634 com_err("create_journal_dev", retval,
635 _("while writing journal superblock"));
636 exit(1);
637 }
638 progress_close(&progress);
639}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000640
Theodore Ts'o3839e651997-04-26 13:21:57 +0000641static void show_stats(ext2_filsys fs)
642{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000643 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000644 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000646 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647
648 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'o66938372002-03-08 00:14:46 -0500649 fprintf(stderr, _("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000650 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000651
652 memset(buf, 0, sizeof(buf));
653 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000654 printf(_("Filesystem label=%s\n"), buf);
655 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000656 switch (fs->super->s_creator_os) {
657 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000658 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000659 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000660 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000661 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000662 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000663 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000664 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000665 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000666 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000667 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000669 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670 s->s_r_blocks_count,
671 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000672 printf(_("First data block=%u\n"), s->s_first_data_block);
673 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000674 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000675 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000676 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000677 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000678 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000679 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680
681 if (fs->group_desc_count == 1) {
682 printf("\n");
683 return;
684 }
685
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000686 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000687 group_block = s->s_first_data_block;
688 col_left = 0;
689 for (i = 1; i < fs->group_desc_count; i++) {
690 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000691 if (!ext2fs_bg_has_super(fs, i))
692 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000693 if (i != 1)
694 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000695 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000696 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000697 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000698 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000699 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000700 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000701 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000702 }
703 printf("\n\n");
704}
705
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000706/*
707 * Set the S_CREATOR_OS field. Return true if OS is known,
708 * otherwise, 0.
709 */
710static int set_os(struct ext2_super_block *sb, char *os)
711{
712 if (isdigit (*os))
713 sb->s_creator_os = atoi (os);
714 else if (strcasecmp(os, "linux") == 0)
715 sb->s_creator_os = EXT2_OS_LINUX;
716 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
717 sb->s_creator_os = EXT2_OS_HURD;
718 else if (strcasecmp(os, "masix") == 0)
719 sb->s_creator_os = EXT2_OS_MASIX;
720 else
721 return 0;
722 return 1;
723}
724
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000725#define PATH_SET "PATH=/sbin"
726
Theodore Ts'od163b091997-10-03 17:42:28 +0000727static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000728{
729 char *buf, *token, *next, *p, *arg;
730 int len;
731 int raid_usage = 0;
732
733 len = strlen(opts);
734 buf = malloc(len+1);
735 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000736 fprintf(stderr, _("Couldn't allocate memory to parse "
737 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000738 exit(1);
739 }
740 strcpy(buf, opts);
741 for (token = buf; token && *token; token = next) {
742 p = strchr(token, ',');
743 next = 0;
744 if (p) {
745 *p = 0;
746 next = p+1;
747 }
748 arg = strchr(token, '=');
749 if (arg) {
750 *arg = 0;
751 arg++;
752 }
753 if (strcmp(token, "stride") == 0) {
754 if (!arg) {
755 raid_usage++;
756 continue;
757 }
758 fs_stride = strtoul(arg, &p, 0);
759 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000760 fprintf(stderr,
761 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000762 raid_usage++;
763 continue;
764 }
765 } else
766 raid_usage++;
767 }
768 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000769 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000770 "Raid options are separated by commas, "
771 "and may take an argument which\n"
772 "\tis set off by an equals ('=') sign.\n\n"
773 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000774 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000775 exit(1);
776 }
777}
778
Theodore Ts'o896938d1999-10-23 01:04:50 +0000779static __u32 ok_features[3] = {
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000780 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000781 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
782 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000783 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
784};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000785
786
Theodore Ts'o3839e651997-04-26 13:21:57 +0000787static void PRS(int argc, char *argv[])
788{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000789 int c;
790 int size;
791 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000792 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000793 int blocksize = 0;
794 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600795 int inode_size = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000796 int reserved_ratio = 5;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000797 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000798 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000799 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000800 char * raid_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500801 const char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400802 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000803 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500804#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000805 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000806#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400807 long sysval;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000808
Theodore Ts'o3839e651997-04-26 13:21:57 +0000809 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000810 if (oldpath) {
811 char *newpath;
812
813 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
814 strcpy (newpath, PATH_SET);
815 strcat (newpath, ":");
816 strcat (newpath, oldpath);
817 putenv (newpath);
818 } else
819 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000820
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000821 tmp = getenv("MKE2FS_SYNC");
822 if (tmp)
823 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400824
825 /* Determine the system page size if possible */
826#ifdef HAVE_SYSCONF
827#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
828#define _SC_PAGESIZE _SC_PAGE_SIZE
829#endif
830#ifdef _SC_PAGESIZE
831 sysval = sysconf(_SC_PAGESIZE);
832 if (sysconf > 0)
833 sys_page_size = sysval;
834#endif /* _SC_PAGESIZE */
835#endif /* HAVE_SYSCONF */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000836
Theodore Ts'o3839e651997-04-26 13:21:57 +0000837 setbuf(stdout, NULL);
838 setbuf(stderr, NULL);
839 initialize_ext2_error_table();
840 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000841 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400842 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
843 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
844
Theodore Ts'o756df352002-03-07 20:52:12 -0500845#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000846 if (uname(&ut)) {
847 perror("uname");
848 exit(1);
849 }
850 if ((ut.release[0] == '1') ||
851 (ut.release[0] == '2' && ut.release[1] == '.' &&
852 ut.release[2] < '2' && ut.release[3] == '.')) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000853 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400854 param.s_feature_incompat = 0;
855 param.s_feature_compat = 0;
856 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000857 }
858#endif
Andreas Dilger9d8e6342001-08-31 10:24:46 -0600859 fprintf (stderr, "mke2fs %s (%s)\n",
Theodore Ts'o04a96852001-08-30 21:55:26 -0400860 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700861
862 if (argc && *argv) {
863 program_name = get_progname(*argv);
864
865 /* If called as mkfs.ext3, create a journal inode */
866 if (!strcmp(program_name, "mkfs.ext3"))
867 journal_size = -1;
868 }
869
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000870 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000871 "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 +0000872 switch (c) {
873 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000874 blocksize = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600875 if (blocksize < EXT2_MIN_BLOCK_SIZE ||
876 blocksize > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000877 com_err(program_name, 0,
878 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000879 exit(1);
880 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600881 if (blocksize > 4096)
882 fprintf(stderr, _("Warning: blocksize %d not "
883 "usable on most systems.\n"),
884 blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000885 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000886 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000887 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000888 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000889 case 'c': /* Check for bad blocks */
890 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500891 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000892 break;
893 case 'f':
894 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600895 if (size < EXT2_MIN_BLOCK_SIZE ||
896 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000897 com_err(program_name, 0,
898 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000899 optarg);
900 exit(1);
901 }
902 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000903 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500904 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000905 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000906 break;
907 case 'g':
908 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000909 if (*tmp) {
910 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000911 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000912 exit(1);
913 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000914 if ((param.s_blocks_per_group % 8) != 0) {
915 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000916 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000917 exit(1);
918 }
919 break;
920 case 'i':
921 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600922 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
923 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000924 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000925 com_err(program_name, 0,
Andreas Dilger932a4892002-05-16 03:20:07 -0600926 _("bad inode ratio %s (min %d/max %d"),
927 optarg, EXT2_MIN_BLOCK_SIZE,
928 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000929 exit(1);
930 }
931 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000932 case 'J':
933 parse_journal_opts(optarg);
934 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000935 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000936 param.s_feature_compat |=
937 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000938 if (!journal_size)
939 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000940 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000941 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000942 bad_blocks_filename = malloc(strlen(optarg)+1);
943 if (!bad_blocks_filename) {
944 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000945 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000946 exit(1);
947 }
948 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000949 break;
950 case 'm':
951 reserved_ratio = strtoul(optarg, &tmp, 0);
952 if (reserved_ratio > 50 || *tmp) {
953 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000954 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000955 optarg);
956 exit(1);
957 }
958 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000959 case 'n':
960 noaction++;
961 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000962 case 'o':
963 creator_os = optarg;
964 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000965 case 'r':
966 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000967 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400968 param.s_feature_incompat = 0;
969 param.s_feature_compat = 0;
970 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000971 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000972 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000973 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400974 if (atoi(optarg))
975 param.s_feature_ro_compat |=
976 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
977 else
978 param.s_feature_ro_compat &=
979 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000980 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000981#ifdef EXT2_DYNAMIC_REV
982 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -0600983 inode_size = strtoul(optarg, &tmp, 0);
984 if (*tmp) {
985 com_err(program_name, 0,
986 _("bad inode size - %s"), optarg);
987 exit(1);
988 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000989 break;
990#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000991 case 'N':
992 num_inodes = atoi(optarg);
993 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000994 case 'v':
995 verbose = 1;
996 break;
997 case 'q':
998 quiet = 1;
999 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001000 case 'F':
1001 force = 1;
1002 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001003 case 'L':
1004 volume_label = optarg;
1005 break;
1006 case 'M':
1007 mount_dir = optarg;
1008 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001009 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001010 if (!strcmp(optarg, "none") || default_features) {
1011 param.s_feature_compat = 0;
1012 param.s_feature_incompat = 0;
1013 param.s_feature_ro_compat = 0;
1014 default_features = 0;
1015 }
1016 if (!strcmp(optarg, "none"))
1017 break;
1018 if (e2p_edit_feature(optarg,
1019 &param.s_feature_compat,
1020 ok_features)) {
1021 fprintf(stderr,
1022 _("Invalid filesystem option set: %s\n"), optarg);
1023 exit(1);
1024 }
Theodore Ts'o896938d1999-10-23 01:04:50 +00001025 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001026 case 'R':
1027 raid_opts = optarg;
1028 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001029 case 'S':
1030 super_only = 1;
1031 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001032 case 'T':
1033 fs_type = optarg;
1034 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001035 case 'V':
1036 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001037 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +00001038 error_message(EXT2_ET_BASE));
1039 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001040 default:
1041 usage();
1042 }
1043 if (optind == argc)
1044 usage();
1045 device_name = argv[optind];
1046 optind++;
1047 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001048 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1049
1050 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001051 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001052 argv[optind - 1]);
1053 exit(1);
1054 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001055 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001056 }
1057 if (optind < argc)
1058 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001059
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001060 if (raid_opts)
1061 parse_raid_opts(raid_opts);
1062
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001063 /*
1064 * If there's no blocksize specified and there is a journal
1065 * device, use it to figure out the blocksize
1066 */
1067 if (blocksize == 0 && journal_device) {
1068 ext2_filsys jfs;
1069
1070 retval = ext2fs_open(journal_device,
1071 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1072 0, unix_io_manager, &jfs);
1073 if (retval) {
1074 com_err(program_name, retval,
1075 _("while trying to open journal device %s\n"),
1076 journal_device);
1077 exit(1);
1078 }
1079 blocksize = jfs->blocksize;
1080 param.s_log_block_size =
1081 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1082 ext2fs_close(jfs);
1083 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001084
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001085 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001086 if (!force) {
1087 com_err(program_name, 0,
1088 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001089 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001090 proceed_question();
1091 }
1092 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1093 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001094 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001095 }
1096
Andreas Dilgerfa724582001-08-04 01:18:34 -06001097 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1098 if (!fs_type)
1099 fs_type = "journal";
1100 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001101 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1102 param.s_feature_compat = 0;
1103 param.s_feature_ro_compat = 0;
1104 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001105 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1106 (param.s_feature_compat || param.s_feature_ro_compat ||
1107 param.s_feature_incompat))
1108 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1109
Theodore Ts'o74becf31997-04-26 14:37:06 +00001110 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001111 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001112 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001113
Theodore Ts'o3839e651997-04-26 13:21:57 +00001114 param.s_log_frag_size = param.s_log_block_size;
1115
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001116 if (noaction && param.s_blocks_count) {
1117 dev_size = param.s_blocks_count;
1118 retval = 0;
1119 } else
1120 retval = ext2fs_get_device_size(device_name,
1121 EXT2_BLOCK_SIZE(&param),
1122 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001123 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1124 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001125 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001126 exit(1);
1127 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001128 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001129 if (retval == EXT2_ET_UNIMPLEMENTED) {
1130 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001131 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001132 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001133 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001134 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001135 } else {
1136 if (dev_size == 0) {
1137 com_err(program_name, 0,
1138 _("Device size reported to be zero. "
1139 "Invalid partition specified, or\n\t"
1140 "partition table wasn't reread "
1141 "after running fdisk, due to\n\t"
1142 "a modified partition being busy "
1143 "and in use. You may need to reboot\n\t"
1144 "to re-read your partition table.\n"
1145 ));
1146 exit(1);
1147 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001148 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001149 }
1150
Theodore Ts'oa789d841998-03-30 01:20:55 +00001151 } else if (!force && (param.s_blocks_count > dev_size)) {
1152 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001153 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001154 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001155 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001156
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001157 /*
1158 * If the user asked for HAS_JOURNAL, then make sure a journal
1159 * gets created.
1160 */
1161 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1162 !journal_size)
1163 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001164
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001165 set_fs_defaults(fs_type, &param, blocksize, &inode_ratio);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001166
Theodore Ts'o521e3681997-04-29 17:48:10 +00001167 if (param.s_blocks_per_group) {
1168 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001169 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001170 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001171 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001172 exit(1);
1173 }
1174 }
1175
Andreas Dilger932a4892002-05-16 03:20:07 -06001176 if (inode_size) {
1177 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1178 inode_size > EXT2_BLOCK_SIZE(&param) ||
1179 inode_size & (inode_size - 1)) {
1180 com_err(program_name, 0,
1181 _("bad inode size %d (min %d/max %d)"),
1182 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1183 EXT2_BLOCK_SIZE(&param));
1184 exit(1);
1185 }
1186 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1187 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1188 "on most systems\n"),
1189 inode_size);
1190 param.s_inode_size = inode_size;
1191 }
1192
Theodore Ts'o3839e651997-04-26 13:21:57 +00001193 /*
1194 * Calculate number of inodes based on the inode ratio
1195 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001196 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +00001197 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +00001198 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001199
1200 /*
1201 * Calculate number of blocks to reserve
1202 */
1203 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001204
Theodore Ts'o3839e651997-04-26 13:21:57 +00001205}
1206
1207int main (int argc, char *argv[])
1208{
1209 errcode_t retval = 0;
1210 ext2_filsys fs;
1211 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001212 int journal_blocks;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001213 int i, val;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001214
1215#ifdef ENABLE_NLS
1216 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001217 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001218 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1219 textdomain(NLS_CAT_NAME);
1220#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001221 PRS(argc, argv);
1222
Theodore Ts'o3839e651997-04-26 13:21:57 +00001223 /*
1224 * Initialize the superblock....
1225 */
1226 retval = ext2fs_initialize(device_name, 0, &param,
1227 unix_io_manager, &fs);
1228 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001229 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001230 exit(1);
1231 }
1232
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001233 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001234 * Wipe out the old on-disk superblock
1235 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001236 if (!noaction)
1237 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001238
1239 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001240 * Generate a UUID for it...
1241 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001242 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001243
1244 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001245 * Add "jitter" to the superblock's check interval so that we
1246 * don't check all the filesystems at the same time. We use a
1247 * kludgy hack of using the UUID to derive a random jitter value.
1248 */
1249 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1250 val += fs->super->s_uuid[i];
1251 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1252
1253 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001254 * Override the creator OS, if applicable
1255 */
1256 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001257 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001258 exit(1);
1259 }
1260
1261 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001262 * For the Hurd, we will turn off filetype since it doesn't
1263 * support it.
1264 */
1265 if (fs->super->s_creator_os == EXT2_OS_HURD)
1266 fs->super->s_feature_incompat &=
1267 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1268
1269 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001270 * Set the volume label...
1271 */
1272 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001273 memset(fs->super->s_volume_name, 0,
1274 sizeof(fs->super->s_volume_name));
1275 strncpy(fs->super->s_volume_name, volume_label,
1276 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001277 }
1278
1279 /*
1280 * Set the last mount directory
1281 */
1282 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001283 memset(fs->super->s_last_mounted, 0,
1284 sizeof(fs->super->s_last_mounted));
1285 strncpy(fs->super->s_last_mounted, mount_dir,
1286 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001287 }
1288
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001289 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001290 show_stats(fs);
1291
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001292 if (noaction)
1293 exit(0);
1294
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001295 if (fs->super->s_feature_incompat &
1296 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1297 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001298 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001299 }
1300
Theodore Ts'o3839e651997-04-26 13:21:57 +00001301 if (bad_blocks_filename)
1302 read_bb_file(fs, &bb_list, bad_blocks_filename);
1303 if (cflag)
1304 test_disk(fs, &bb_list);
1305
1306 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001307 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001308 retval = ext2fs_allocate_tables(fs);
1309 if (retval) {
1310 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001311 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001312 exit(1);
1313 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001314 if (super_only) {
1315 fs->super->s_state |= EXT2_ERROR_FS;
1316 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1317 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001318 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001319 int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001320 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001321 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001322 blk_t ret_blk;
1323
1324#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001325 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001326#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001327
1328 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001329 * Wipe out any old MD RAID (or other) metadata at the end
1330 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001331 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001332 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001333 start = (blocks & ~(rsv - 1));
1334 if (start > rsv)
1335 start -= rsv;
1336 if (start > 0)
1337 retval = zero_blocks(fs, start, blocks - start,
1338 NULL, &ret_blk, NULL);
1339
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001340 if (retval) {
1341 com_err(program_name, retval,
1342 _("zeroing block %u at end of filesystem"),
1343 ret_blk);
1344 exit(1);
1345 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001346 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001347 create_root_dir(fs);
1348 create_lost_and_found(fs);
1349 reserve_inodes(fs);
1350 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001351 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001352
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001353 if (journal_device) {
1354 ext2_filsys jfs;
1355
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001356 if (!force)
1357 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001358 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001359
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001360 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1361 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1362 fs->blocksize, unix_io_manager, &jfs);
1363 if (retval) {
1364 com_err(program_name, retval,
1365 _("while trying to open journal device %s\n"),
1366 journal_device);
1367 exit(1);
1368 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001369 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001370 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001371 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001372 fflush(stdout);
1373 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001374 retval = ext2fs_add_journal_device(fs, jfs);
1375 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001376 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001377 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001378 journal_device);
1379 exit(1);
1380 }
1381 if (!quiet)
1382 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001383 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001384 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001385 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001386 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001387
1388 if (!journal_blocks) {
1389 fs->super->s_feature_compat &=
1390 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1391 goto no_journal;
1392 }
1393 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001394 printf(_("Creating journal (%d blocks): "),
1395 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001396 fflush(stdout);
1397 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001398 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1399 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001400 if (retval) {
1401 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001402 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001403 exit(1);
1404 }
1405 if (!quiet)
1406 printf(_("done\n"));
1407 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001408no_journal:
1409
Theodore Ts'o3839e651997-04-26 13:21:57 +00001410 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001411 printf(_("Writing superblocks and "
1412 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001413 retval = ext2fs_flush(fs);
1414 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001415 fprintf(stderr,
1416 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001417 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001418 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001419 printf(_("done\n\n"));
Theodore Ts'o66cf2f62001-06-14 06:42:44 +00001420 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001421 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001422 val = ext2fs_close(fs);
1423 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001424}