blob: 6fd554005860124141d6ce7fdad3db51958cbf21 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05004 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 */
12
13/* Usage: mke2fs [options] device
14 *
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
17 */
18
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000019#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <string.h>
21#include <fcntl.h>
22#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <time.h>
Theodore Ts'o756df352002-03-07 20:52:12 -050024#ifdef __linux__
Theodore Ts'o27401561999-09-14 20:11:19 +000025#include <sys/utsname.h>
26#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000027#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000029#else
30extern char *optarg;
31extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000032#endif
33#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000034#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000035#endif
36#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000037#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000038#endif
39#ifdef HAVE_ERRNO_H
40#include <errno.h>
41#endif
42#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000043#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000044#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000045#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000046#include <sys/types.h>
47
Theodore Ts'o54c637d2001-05-14 11:45:38 +000048#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000049#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000050#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000051#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000052#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000053#include "util.h"
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050054#include "profile.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000056#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000057
58#define STRIDE_LENGTH 8
59
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000060#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000061#define ZAP_BOOTBLOCK
62#endif
63
Theodore Ts'o3839e651997-04-26 13:21:57 +000064extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000065extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000066
67const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000068const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000069
70/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000071int cflag;
72int verbose;
73int quiet;
74int super_only;
75int force;
76int noaction;
77int journal_size;
78int journal_flags;
79char *bad_blocks_filename;
80__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050082struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000083char *creator_os;
84char *volume_label;
85char *mount_dir;
86char *journal_device;
87int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000088
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050089profile_t profile;
90
Theodore Ts'o31e29a12002-05-17 10:53:07 -040091int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040092int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040093
Theodore Ts'o63985322001-01-03 17:02:13 +000094static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000095{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000096 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000097 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000098 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
99 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +0000100 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500101 "[-r fs-revision] [-R options] [-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102 program_name);
103 exit(1);
104}
105
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000106static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107{
108 int l = 0;
109
110 arg >>= 1;
111 while (arg) {
112 l++;
113 arg >>= 1;
114 }
115 return l;
116}
117
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000118static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000119{
120 int l;
121
122 for (l=0; arg ; l++)
123 arg = arg / 10;
124 return l;
125}
126
Theodore Ts'od99225e2004-09-25 07:40:12 -0400127static int parse_version_number(const char *s)
128{
129 int major, minor, rev;
130 char *endptr;
131 const char *cp = s;
132
133 if (!s)
134 return 0;
135 major = strtol(cp, &endptr, 10);
136 if (cp == endptr || *endptr != '.')
137 return 0;
138 cp = endptr + 1;
139 minor = strtol(cp, &endptr, 10);
140 if (cp == endptr || *endptr != '.')
141 return 0;
142 cp = endptr + 1;
143 rev = strtol(cp, &endptr, 10);
144 if (cp == endptr)
145 return 0;
146 return ((((major * 256) + minor) * 256) + rev);
147}
148
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000149/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 * Helper function for read_bb_file and test_disk
151 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500152static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000153{
Theodore Ts'o66938372002-03-08 00:14:46 -0500154 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155 return;
156}
157
158/*
159 * Reads the bad blocks list from a file
160 */
161static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
162 const char *bad_blocks_file)
163{
164 FILE *f;
165 errcode_t retval;
166
167 f = fopen(bad_blocks_file, "r");
168 if (!f) {
169 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000170 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000171 exit(1);
172 }
173 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
174 fclose (f);
175 if (retval) {
176 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000177 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 exit(1);
179 }
180}
181
182/*
183 * Runs the badblocks program to test the disk
184 */
185static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
186{
187 FILE *f;
188 errcode_t retval;
189 char buf[1024];
190
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500191 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
192 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
193 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000195 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 f = popen(buf, "r");
197 if (!f) {
198 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400199 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 exit(1);
201 }
202 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000203 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 if (retval) {
205 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000206 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 exit(1);
208 }
209}
210
211static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
212{
Theodore Ts'o54434922003-12-07 01:28:50 -0500213 dgrp_t i;
214 blk_t j;
215 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000216 blk_t blk;
217 badblocks_iterate bb_iter;
218 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000219 blk_t group_block;
220 int group;
221 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222
223 if (!bb_list)
224 return;
225
226 /*
227 * The primary superblock and group descriptors *must* be
228 * good; if not, abort.
229 */
230 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
231 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000232 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000233 fprintf(stderr, _("Block %d in primary "
234 "superblock/group descriptor area bad.\n"), i);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500235 fprintf(stderr, _("Blocks %u through %d must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000236 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500238 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 exit(1);
240 }
241 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000242
243 /*
244 * See if any of the bad blocks are showing up in the backup
245 * superblocks and/or group descriptors. If so, issue a
246 * warning and adjust the block counts appropriately.
247 */
248 group_block = fs->super->s_first_data_block +
249 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000250
251 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000252 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000253 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000254 if (ext2fs_badblocks_list_test(bb_list,
255 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000256 if (!group_bad)
257 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500258_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000259" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000260 group_block);
261 group_bad++;
262 group = ext2fs_group_of_blk(fs, group_block+j);
263 fs->group_desc[group].bg_free_blocks_count++;
264 fs->super->s_free_blocks_count++;
265 }
266 }
267 group_block += fs->super->s_blocks_per_group;
268 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269
270 /*
271 * Mark all the bad blocks as used...
272 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000273 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000275 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000276 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 exit(1);
278 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000279 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000280 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000281 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282}
283
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000284/*
285 * These functions implement a generalized progress meter.
286 */
287struct progress_struct {
288 char format[20];
289 char backup[80];
290 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400291 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000292};
293
294static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500295 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000297 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000298
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000299 memset(progress, 0, sizeof(struct progress_struct));
300 if (quiet)
301 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000302
303 /*
304 * Figure out how many digits we need
305 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000306 i = int_log10(max);
307 sprintf(progress->format, "%%%dd/%%%dld", i, i);
308 memset(progress->backup, '\b', sizeof(progress->backup)-1);
309 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500310 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000311 progress->backup[(2*i)+1] = 0;
312 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000313
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400314 progress->skip_progress = 0;
315 if (getenv("MKE2FS_SKIP_PROGRESS"))
316 progress->skip_progress++;
317
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000318 fputs(label, stdout);
319 fflush(stdout);
320}
321
322static void progress_update(struct progress_struct *progress, __u32 val)
323{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400324 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000325 return;
326 printf(progress->format, val, progress->max);
327 fputs(progress->backup, stdout);
328}
329
330static void progress_close(struct progress_struct *progress)
331{
332 if (progress->format[0] == 0)
333 return;
334 fputs(_("done \n"), stdout);
335}
336
337
338/*
339 * Helper function which zeros out _num_ blocks starting at _blk_. In
340 * case of an error, the details of the error is returned via _ret_blk_
341 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
342 * success, and an error code on an error.
343 *
344 * As a special case, if the first argument is NULL, then it will
345 * attempt to free the static zeroizing buffer. (This is to keep
346 * programs that check for memory leaks happy.)
347 */
348static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
349 struct progress_struct *progress,
350 blk_t *ret_blk, int *ret_count)
351{
352 int j, count, next_update, next_update_incr;
353 static char *buf;
354 errcode_t retval;
355
356 /* If fs is null, clean up the static buffer and return */
357 if (!fs) {
358 if (buf) {
359 free(buf);
360 buf = 0;
361 }
362 return 0;
363 }
364 /* Allocate the zeroizing buffer if necessary */
365 if (!buf) {
366 buf = malloc(fs->blocksize * STRIDE_LENGTH);
367 if (!buf) {
368 com_err("malloc", ENOMEM,
369 _("while allocating zeroizing buffer"));
370 exit(1);
371 }
372 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
373 }
374 /* OK, do the write loop */
375 next_update = 0;
376 next_update_incr = num / 100;
377 if (next_update_incr < 1)
378 next_update_incr = 1;
379 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400380 count = num - j;
381 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000382 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000383 retval = io_channel_write_blk(fs->io, blk, count, buf);
384 if (retval) {
385 if (ret_count)
386 *ret_count = count;
387 if (ret_blk)
388 *ret_blk = blk;
389 return retval;
390 }
391 if (progress && j > next_update) {
392 next_update += num / 100;
393 progress_update(progress, blk);
394 }
395 }
396 return 0;
397}
398
399static void write_inode_tables(ext2_filsys fs)
400{
401 errcode_t retval;
402 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500403 dgrp_t i;
404 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000405 struct progress_struct progress;
406
407 if (quiet)
408 memset(&progress, 0, sizeof(progress));
409 else
410 progress_init(&progress, _("Writing inode tables: "),
411 fs->group_desc_count);
412
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000414 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000416 blk = fs->group_desc[i].bg_inode_table;
417 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000418
419 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
420 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -0500421 fprintf(stderr, _("\nCould not write %d blocks "
Takashi Sato8deb80a2006-03-18 21:43:46 -0500422 "in inode table starting at %u: %s\n"),
Theodore Ts'o66938372002-03-08 00:14:46 -0500423 num, blk, error_message(retval));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000424 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000425 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000426 if (sync_kludge) {
427 if (sync_kludge == 1)
428 sync();
429 else if ((i % sync_kludge) == 0)
430 sync();
431 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000433 zero_blocks(0, 0, 0, 0, 0, 0);
434 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435}
436
437static void create_root_dir(ext2_filsys fs)
438{
439 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000440 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441
442 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
443 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000444 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445 exit(1);
446 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000447 if (geteuid()) {
448 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
449 if (retval) {
450 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000451 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000452 exit(1);
453 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000454 inode.i_uid = getuid();
455 if (inode.i_uid)
456 inode.i_gid = getgid();
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500457 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000458 if (retval) {
459 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000460 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000461 exit(1);
462 }
463 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464}
465
466static void create_lost_and_found(ext2_filsys fs)
467{
468 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000469 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000470 const char *name = "lost+found";
471 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000472 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473
Theodore Ts'o6a525062001-12-24 09:40:00 -0500474 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
476 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000477 com_err("ext2fs_mkdir", retval,
478 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479 exit(1);
480 }
481
482 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
483 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000484 com_err("ext2_lookup", retval,
485 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000486 exit(1);
487 }
488
489 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000490 if ((lpf_size += fs->blocksize) >= 16*1024)
491 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492 retval = ext2fs_expand_dir(fs, ino);
493 if (retval) {
494 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000495 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 exit(1);
497 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500498 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499}
500
501static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
502{
503 errcode_t retval;
504
Theodore Ts'of3db3561997-04-26 13:34:30 +0000505 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506 fs->group_desc[0].bg_free_inodes_count--;
507 fs->super->s_free_inodes_count--;
508 retval = ext2fs_update_bb_inode(fs, bb_list);
509 if (retval) {
510 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000511 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 exit(1);
513 }
514
515}
516
517static void reserve_inodes(ext2_filsys fs)
518{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000519 ext2_ino_t i;
520 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000522 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000523 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 group = ext2fs_group_of_ino(fs, i);
525 fs->group_desc[group].bg_free_inodes_count--;
526 fs->super->s_free_inodes_count--;
527 }
528 ext2fs_mark_ib_dirty(fs);
529}
530
Theodore Ts'o756df352002-03-07 20:52:12 -0500531#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500532#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500533#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500534
Theodore Ts'o04a96852001-08-30 21:55:26 -0400535static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000536{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400537 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000538 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500539 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000540
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400541 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600542 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500543 printf(_("Out of memory erasing sectors %d-%d\n"),
544 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600545 exit(1);
546 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500547
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500548 if (sect == 0) {
549 /* Check for a BSD disklabel, and don't erase it if so */
550 retval = io_channel_read_blk(fs->io, 0, -512, buf);
551 if (retval)
552 fprintf(stderr,
553 _("Warning: could not read block 0: %s\n"),
554 error_message(retval));
555 else {
556 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
557 if ((*magic == BSD_DISKMAGIC) ||
558 (*magic == BSD_MAGICDISK))
559 return;
560 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500561 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500562
Theodore Ts'o70988102002-07-14 08:00:00 -0400563 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000564 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400565 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000566 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400567 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000568 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500569 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
570 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000571}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000572
573static void create_journal_dev(ext2_filsys fs)
574{
575 struct progress_struct progress;
576 errcode_t retval;
577 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000578 blk_t blk;
579 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000580
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000581 retval = ext2fs_create_journal_superblock(fs,
582 fs->super->s_blocks_count, 0, &buf);
583 if (retval) {
584 com_err("create_journal_dev", retval,
585 _("while initializing journal superblock"));
586 exit(1);
587 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000588 if (quiet)
589 memset(&progress, 0, sizeof(progress));
590 else
591 progress_init(&progress, _("Zeroing journal device: "),
592 fs->super->s_blocks_count);
593
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000594 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
595 &progress, &blk, &count);
596 if (retval) {
597 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000598 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000599 blk, count);
600 exit(1);
601 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000602 zero_blocks(0, 0, 0, 0, 0, 0);
603
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000604 retval = io_channel_write_blk(fs->io,
605 fs->super->s_first_data_block+1,
606 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000607 if (retval) {
608 com_err("create_journal_dev", retval,
609 _("while writing journal superblock"));
610 exit(1);
611 }
612 progress_close(&progress);
613}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000614
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615static void show_stats(ext2_filsys fs)
616{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000617 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000618 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500619 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000620 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500621 dgrp_t i;
622 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500624 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500625 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500626 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000627
628 memset(buf, 0, sizeof(buf));
629 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000630 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500631 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500632 os = e2p_os2string(fs->super->s_creator_os);
633 fputs(os, stdout);
634 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000635 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000636 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000637 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000638 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000639 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000640 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000641 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000642 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643 s->s_r_blocks_count,
644 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000645 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500646 if (s->s_reserved_gdt_blocks)
647 printf(_("Maximum filesystem blocks=%lu\n"),
648 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
649 (fs->blocksize / sizeof(struct ext2_group_desc)) *
650 s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000651 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000652 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000653 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000654 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000655 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000657 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658
659 if (fs->group_desc_count == 1) {
660 printf("\n");
661 return;
662 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500663
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000664 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000665 group_block = s->s_first_data_block;
666 col_left = 0;
667 for (i = 1; i < fs->group_desc_count; i++) {
668 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000669 if (!ext2fs_bg_has_super(fs, i))
670 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000671 if (i != 1)
672 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000673 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000674 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000675 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000676 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000678 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000679 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680 }
681 printf("\n\n");
682}
683
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000684/*
685 * Set the S_CREATOR_OS field. Return true if OS is known,
686 * otherwise, 0.
687 */
688static int set_os(struct ext2_super_block *sb, char *os)
689{
690 if (isdigit (*os))
691 sb->s_creator_os = atoi (os);
692 else if (strcasecmp(os, "linux") == 0)
693 sb->s_creator_os = EXT2_OS_LINUX;
694 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
695 sb->s_creator_os = EXT2_OS_HURD;
696 else if (strcasecmp(os, "masix") == 0)
697 sb->s_creator_os = EXT2_OS_MASIX;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500698 else if (strcasecmp(os, "freebsd") == 0)
699 sb->s_creator_os = EXT2_OS_FREEBSD;
700 else if (strcasecmp(os, "lites") == 0)
701 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000702 else
703 return 0;
704 return 1;
705}
706
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000707#define PATH_SET "PATH=/sbin"
708
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500709static void parse_extended_opts(struct ext2_super_block *param,
710 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000711{
712 char *buf, *token, *next, *p, *arg;
713 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500714 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000715
716 len = strlen(opts);
717 buf = malloc(len+1);
718 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500719 fprintf(stderr,
720 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000721 exit(1);
722 }
723 strcpy(buf, opts);
724 for (token = buf; token && *token; token = next) {
725 p = strchr(token, ',');
726 next = 0;
727 if (p) {
728 *p = 0;
729 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500730 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000731 arg = strchr(token, '=');
732 if (arg) {
733 *arg = 0;
734 arg++;
735 }
736 if (strcmp(token, "stride") == 0) {
737 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500738 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000739 continue;
740 }
741 fs_stride = strtoul(arg, &p, 0);
742 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000743 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400744 _("Invalid stride parameter: %s\n"),
745 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500746 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000747 continue;
748 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500749 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500750 unsigned long resize, bpg, rsv_groups;
751 unsigned long group_desc_count, desc_blocks;
752 unsigned int gdpb, blocksize;
753 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500754
755 if (!arg) {
756 r_usage++;
757 continue;
758 }
759
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500760 resize = parse_num_blocks(arg,
761 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500762
763 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500764 fprintf(stderr,
765 _("Invalid resize parameter: %s\n"),
766 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500767 r_usage++;
768 continue;
769 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500770 if (resize <= param->s_blocks_count) {
771 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400772 _("The resize maximum must be greater "
773 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500774 r_usage++;
775 continue;
776 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500777
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500778 blocksize = EXT2_BLOCK_SIZE(param);
779 bpg = param->s_blocks_per_group;
780 if (!bpg)
781 bpg = blocksize * 8;
782 gdpb = blocksize / sizeof(struct ext2_group_desc);
783 group_desc_count = (param->s_blocks_count +
784 bpg - 1) / bpg;
785 desc_blocks = (group_desc_count +
786 gdpb - 1) / gdpb;
787 rsv_groups = (resize + bpg - 1) / bpg;
788 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
789 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500790 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500791 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
792
793 if (rsv_gdb > 0) {
794 param->s_feature_compat |=
795 EXT2_FEATURE_COMPAT_RESIZE_INODE;
796
797 param->s_reserved_gdt_blocks = rsv_gdb;
798 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000799 } else
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500800 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000801 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500802 if (r_usage) {
803 fprintf(stderr, _("\nBad options specified.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400804 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000805 "and may take an argument which\n"
806 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400807 "Valid extended options are:\n"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500808 "\tstride=<stride length in blocks>\n"
809 "\tresize=<resize maximum size in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000810 exit(1);
811 }
812}
813
Theodore Ts'o896938d1999-10-23 01:04:50 +0000814static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400815 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500816 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'o843049c2002-09-22 15:37:40 -0400817 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000818 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400819 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
820 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000821 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
822};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000823
824
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500825static void syntax_err_report(const char *filename, long err, int line_num)
826{
827 fprintf(stderr,
828 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
829 filename, line_num, error_message(err));
830 exit(1);
831}
832
833static const char *config_fn[] = { "/etc/mke2fs.conf", 0 };
834
835static void edit_feature(const char *str, __u32 *compat_array)
836{
837 if (!str)
838 return;
839
840 if (e2p_edit_feature(str, compat_array, ok_features)) {
841 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
842 str);
843 exit(1);
844 }
845}
846
Theodore Ts'o3839e651997-04-26 13:21:57 +0000847static void PRS(int argc, char *argv[])
848{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400849 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000850 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500851 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000852 int blocksize = 0;
853 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600854 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -0500855 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400856 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500857 int show_version_only = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000858 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000859 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000860 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500861 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500862 const char * fs_type = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000863 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500864#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000865 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000866#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400867 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500868 int s_opt = -1, r_opt = -1;
869 char *fs_features = 0;
870 int use_bsize;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000871
Theodore Ts'o3839e651997-04-26 13:21:57 +0000872 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000873 if (oldpath) {
874 char *newpath;
875
876 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
877 strcpy (newpath, PATH_SET);
878 strcat (newpath, ":");
879 strcat (newpath, oldpath);
880 putenv (newpath);
881 } else
882 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000883
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000884 tmp = getenv("MKE2FS_SYNC");
885 if (tmp)
886 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400887
888 /* Determine the system page size if possible */
889#ifdef HAVE_SYSCONF
890#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
891#define _SC_PAGESIZE _SC_PAGE_SIZE
892#endif
893#ifdef _SC_PAGESIZE
894 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600895 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400896 sys_page_size = sysval;
897#endif /* _SC_PAGESIZE */
898#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500899
900 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
901 config_fn[0] = tmp;
902 profile_set_syntax_err_cb(syntax_err_report);
903 profile_init(config_fn, &profile);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000904
Theodore Ts'o3839e651997-04-26 13:21:57 +0000905 setbuf(stdout, NULL);
906 setbuf(stderr, NULL);
907 initialize_ext2_error_table();
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500908 memset(&fs_param, 0, sizeof(struct ext2_super_block));
909 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400910
Theodore Ts'o756df352002-03-07 20:52:12 -0500911#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000912 if (uname(&ut)) {
913 perror("uname");
914 exit(1);
915 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400916 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500917 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500918 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000919#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700920
921 if (argc && *argv) {
922 program_name = get_progname(*argv);
923
924 /* If called as mkfs.ext3, create a journal inode */
925 if (!strcmp(program_name, "mkfs.ext3"))
926 journal_size = -1;
927 }
928
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000929 while ((c = getopt (argc, argv,
Andreas Dilger25247852005-07-06 12:58:15 -0500930 "b:cf:g:i:jl:m:no:qr:s:tvE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000931 switch (c) {
932 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400933 blocksize = strtol(optarg, &tmp, 0);
934 b = (blocksize > 0) ? blocksize : -blocksize;
935 if (b < EXT2_MIN_BLOCK_SIZE ||
936 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000937 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400938 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000939 exit(1);
940 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600941 if (blocksize > 4096)
942 fprintf(stderr, _("Warning: blocksize %d not "
943 "usable on most systems.\n"),
944 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400945 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500946 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400947 int_log2(blocksize >>
948 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000949 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000950 case 'c': /* Check for bad blocks */
951 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500952 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000953 break;
954 case 'f':
955 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600956 if (size < EXT2_MIN_BLOCK_SIZE ||
957 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000958 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -0400959 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000960 optarg);
961 exit(1);
962 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500963 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000964 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -0500965 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000966 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000967 break;
968 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500969 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000970 if (*tmp) {
971 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000972 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000973 exit(1);
974 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500975 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000976 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000977 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000978 exit(1);
979 }
980 break;
981 case 'i':
982 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600983 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
984 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000985 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000986 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -0400987 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -0600988 optarg, EXT2_MIN_BLOCK_SIZE,
989 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000990 exit(1);
991 }
992 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000993 case 'J':
994 parse_journal_opts(optarg);
995 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000996 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000997 if (!journal_size)
998 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000999 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001000 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001001 bad_blocks_filename = malloc(strlen(optarg)+1);
1002 if (!bad_blocks_filename) {
1003 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001004 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001005 exit(1);
1006 }
1007 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001008 break;
1009 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001010 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001011 if (reserved_ratio > 50 || *tmp) {
1012 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001013 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001014 optarg);
1015 exit(1);
1016 }
1017 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001018 case 'n':
1019 noaction++;
1020 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001021 case 'o':
1022 creator_os = optarg;
1023 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001024 case 'q':
1025 quiet = 1;
1026 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001027 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001028 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001029 if (*tmp) {
1030 com_err(program_name, 0,
1031 _("bad revision level - %s"), optarg);
1032 exit(1);
1033 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001034 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001035 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001036 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001037 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001038 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001039#ifdef EXT2_DYNAMIC_REV
1040 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001041 inode_size = strtoul(optarg, &tmp, 0);
1042 if (*tmp) {
1043 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001044 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001045 exit(1);
1046 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001047 break;
1048#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001049 case 'v':
1050 verbose = 1;
1051 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001052 case 'F':
1053 force = 1;
1054 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001055 case 'L':
1056 volume_label = optarg;
1057 break;
1058 case 'M':
1059 mount_dir = optarg;
1060 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001061 case 'N':
1062 num_inodes = strtoul(optarg, &tmp, 0);
1063 if (*tmp) {
1064 com_err(program_name, 0,
1065 _("bad num inodes - %s"), optarg);
1066 exit(1);
1067 }
1068 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001069 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001070 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001071 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001072 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001073 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001074 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001075 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001076 case 'S':
1077 super_only = 1;
1078 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001079 case 'T':
1080 fs_type = optarg;
1081 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001082 case 'V':
1083 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001084 show_version_only++;
1085 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001086 default:
1087 usage();
1088 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001089 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001090 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001091 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001092 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001093
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001094 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001095 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1096 E2FSPROGS_DATE);
1097
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001098 if (show_version_only) {
1099 fprintf(stderr, _("\tUsing %s\n"),
1100 error_message(EXT2_ET_BASE));
1101 exit(0);
1102 }
1103
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001104 /*
1105 * If there's no blocksize specified and there is a journal
1106 * device, use it to figure out the blocksize
1107 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001108 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001109 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001110 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001111
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001112#ifdef CONFIG_TESTIO_DEBUG
1113 io_ptr = test_io_manager;
1114 test_io_backing_manager = unix_io_manager;
1115#else
1116 io_ptr = unix_io_manager;
1117#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001118 retval = ext2fs_open(journal_device,
1119 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001120 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001121 if (retval) {
1122 com_err(program_name, retval,
1123 _("while trying to open journal device %s\n"),
1124 journal_device);
1125 exit(1);
1126 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001127 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001128 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001129 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001130 "minimum blocksize %d\n"), jfs->blocksize,
1131 -blocksize);
1132 exit(1);
1133 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001134 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001135 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001136 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1137 ext2fs_close(jfs);
1138 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001139
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001140 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001141 if (!force) {
1142 com_err(program_name, 0,
1143 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001144 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001145 proceed_question();
1146 }
1147 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1148 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001149 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001150 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001151 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001152 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1153 fs_param.s_log_block_size);
1154 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001155 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001156 argv[optind - 1]);
1157 exit(1);
1158 }
1159 }
1160 if (optind < argc)
1161 usage();
1162
Theodore Ts'o74becf31997-04-26 14:37:06 +00001163 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001164 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001165 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001166
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001167 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001168
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001169 if (noaction && fs_param.s_blocks_count) {
1170 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001171 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001172 } else {
1173 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001174 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001175 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001176 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001177 if ((retval == EFBIG) &&
1178 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001179 (fs_param.s_log_block_size == 0)) {
1180 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001181 blocksize = 4096;
1182 goto retry;
1183 }
1184 }
1185
Theodore Ts'oa789d841998-03-30 01:20:55 +00001186 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1187 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001188 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001189 exit(1);
1190 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001191 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001192 if (retval == EXT2_ET_UNIMPLEMENTED) {
1193 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001194 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001195 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001196 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001197 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001198 } else {
1199 if (dev_size == 0) {
1200 com_err(program_name, 0,
1201 _("Device size reported to be zero. "
1202 "Invalid partition specified, or\n\t"
1203 "partition table wasn't reread "
1204 "after running fdisk, due to\n\t"
1205 "a modified partition being busy "
1206 "and in use. You may need to reboot\n\t"
1207 "to re-read your partition table.\n"
1208 ));
1209 exit(1);
1210 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001211 fs_param.s_blocks_count = dev_size;
1212 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1213 fs_param.s_blocks_count &= ~((sys_page_size /
1214 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001215 }
1216
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001217 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001218 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001219 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001220 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001221 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001222
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001223 if (!fs_type) {
1224 int megs = fs_param.s_blocks_count *
1225 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1226
1227 if (megs <= 3)
1228 fs_type = "floppy";
1229 else if (megs <= 512)
1230 fs_type = "small";
1231 else
1232 fs_type = "default";
1233 }
1234
1235 /* Figure out what features should be enabled */
1236
1237 if (r_opt == EXT2_GOOD_OLD_REV && fs_features) {
1238 fprintf(stderr, _("Filesystem features not supported "
1239 "with revision 0 filesystems\n"));
1240 exit(1);
1241 }
1242
1243 profile_get_string(profile, "defaults", "base_features", 0,
1244 "filetype,sparse_super", &tmp);
1245 profile_get_string(profile, "fs_types", fs_type, "base_features",
1246 tmp, &tmp2);
1247 edit_feature(tmp2, &fs_param.s_feature_compat);
1248 free(tmp);
1249 free(tmp2);
1250
1251 profile_get_string(profile, "defaults", "default_features", 0,
1252 "", &tmp);
1253 profile_get_string(profile, "fs_types", fs_type,
1254 "default_features", tmp, &tmp2);
1255 edit_feature(fs_features ? fs_features : tmp2,
1256 &fs_param.s_feature_compat);
1257 free(tmp);
1258 free(tmp2);
1259
1260 if (s_opt > 0)
1261 fs_param.s_feature_ro_compat |=
1262 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1263 else if (s_opt == 0)
1264 fs_param.s_feature_ro_compat &=
1265 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1266
1267 if (journal_size != 0)
1268 fs_param.s_feature_compat |=
1269 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1270
1271 if (fs_param.s_feature_incompat &
1272 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1273 if (!fs_type)
1274 fs_type = "journal";
1275 reserved_ratio = 0;
1276 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1277 fs_param.s_feature_compat = 0;
1278 fs_param.s_feature_ro_compat = 0;
1279 }
1280
1281 if (fs_param.s_rev_level == EXT2_GOOD_OLD_REV) {
1282 fs_param.s_feature_incompat = 0;
1283 fs_param.s_feature_compat = 0;
1284 fs_param.s_feature_ro_compat = 0;
1285 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001286
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001287 /* Set first meta blockgroup via an environment variable */
1288 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001289 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001290 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001291 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001292
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001293 /* Get the hardware sector size, if available */
1294 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1295 if (retval) {
1296 com_err(program_name, retval,
1297 _("while trying to determine hardware sector size"));
1298 exit(1);
1299 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001300
Theodore Ts'o54434922003-12-07 01:28:50 -05001301 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001302 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001303
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001304 if (blocksize <= 0) {
1305 profile_get_integer(profile, "defaults", "blocksize", 0,
1306 1024, &use_bsize);
1307 profile_get_integer(profile, "fs_types", fs_type,
1308 "blocksize", use_bsize, &use_bsize);
1309
1310 if (use_bsize == -1) {
1311 use_bsize = sys_page_size;
1312 if ((linux_version_code < (2*65536 + 6*256)) &&
1313 (use_bsize > 4096))
1314 use_bsize = 4096;
1315 }
1316 if (sector_size && use_bsize < sector_size)
1317 use_bsize = sector_size;
1318 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1319 use_bsize = -blocksize;
1320 blocksize = use_bsize;
1321 fs_param.s_blocks_count /= blocksize / 1024;
1322 }
1323
1324 if (inode_ratio == 0) {
1325 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1326 8192, &inode_ratio);
1327 profile_get_integer(profile, "fs_types", fs_type,
1328 "inode_ratio", inode_ratio,
1329 &inode_ratio);
1330
1331 if (inode_ratio < blocksize)
1332 inode_ratio = blocksize;
1333 }
1334
1335 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1336 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1337
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001338 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001339
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001340 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001341 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001342
1343 /* Since sparse_super is the default, we would only have a problem
1344 * here if it was explicitly disabled.
1345 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001346 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1347 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001348 com_err(program_name, 0,
1349 _("reserved online resize blocks not supported "
1350 "on non-sparse filesystem"));
1351 exit(1);
1352 }
1353
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001354 if (fs_param.s_blocks_per_group) {
1355 if (fs_param.s_blocks_per_group < 256 ||
1356 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001357 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001358 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001359 exit(1);
1360 }
1361 }
1362
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001363 if (!force && fs_param.s_blocks_count >= (1 << 31)) {
Theodore Ts'o675b79c2005-06-30 20:00:41 -04001364 com_err(program_name, 0,
1365 _("Filesystem too large. No more than 2**31-1 blocks\n"
1366 "\t (8TB using a blocksize of 4k) are currently supported."));
1367 exit(1);
1368 }
1369
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001370 if ((blocksize > 4096) &&
1371 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1372 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1373 "blocksizes greater than 4096\n\tusing ext3. "
1374 "Use -b 4096 if this is an issue for you.\n\n"));
1375
Andreas Dilger932a4892002-05-16 03:20:07 -06001376 if (inode_size) {
1377 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001378 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001379 inode_size & (inode_size - 1)) {
1380 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001381 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001382 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001383 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001384 exit(1);
1385 }
1386 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1387 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1388 "on most systems\n"),
1389 inode_size);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001390 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001391 }
1392
Theodore Ts'o3839e651997-04-26 13:21:57 +00001393 /*
1394 * Calculate number of inodes based on the inode ratio
1395 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001396 fs_param.s_inodes_count = num_inodes ? num_inodes :
1397 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001398 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001399
1400 /*
1401 * Calculate number of blocks to reserve
1402 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001403 fs_param.s_r_blocks_count = (fs_param.s_blocks_count * reserved_ratio)
1404 / 100;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001405}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001406
Theodore Ts'o3839e651997-04-26 13:21:57 +00001407int main (int argc, char *argv[])
1408{
1409 errcode_t retval = 0;
1410 ext2_filsys fs;
1411 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001412 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001413 unsigned int i;
1414 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001415 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001416
1417#ifdef ENABLE_NLS
1418 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001419 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001420 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1421 textdomain(NLS_CAT_NAME);
1422#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001423 PRS(argc, argv);
1424
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001425#ifdef CONFIG_TESTIO_DEBUG
1426 io_ptr = test_io_manager;
1427 test_io_backing_manager = unix_io_manager;
1428#else
1429 io_ptr = unix_io_manager;
1430#endif
1431
Theodore Ts'o3839e651997-04-26 13:21:57 +00001432 /*
1433 * Initialize the superblock....
1434 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001435 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001436 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001437 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001438 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001439 exit(1);
1440 }
1441
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001442 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001443 * Wipe out the old on-disk superblock
1444 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001445 if (!noaction)
1446 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001447
1448 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001449 * Generate a UUID for it...
1450 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001451 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001452
1453 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001454 * Initialize the directory index variables
1455 */
1456 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1457 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1458
1459 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001460 * Add "jitter" to the superblock's check interval so that we
1461 * don't check all the filesystems at the same time. We use a
1462 * kludgy hack of using the UUID to derive a random jitter value.
1463 */
1464 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1465 val += fs->super->s_uuid[i];
1466 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1467
1468 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001469 * Override the creator OS, if applicable
1470 */
1471 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001472 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001473 exit(1);
1474 }
1475
1476 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001477 * For the Hurd, we will turn off filetype since it doesn't
1478 * support it.
1479 */
1480 if (fs->super->s_creator_os == EXT2_OS_HURD)
1481 fs->super->s_feature_incompat &=
1482 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1483
1484 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001485 * Set the volume label...
1486 */
1487 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001488 memset(fs->super->s_volume_name, 0,
1489 sizeof(fs->super->s_volume_name));
1490 strncpy(fs->super->s_volume_name, volume_label,
1491 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001492 }
1493
1494 /*
1495 * Set the last mount directory
1496 */
1497 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001498 memset(fs->super->s_last_mounted, 0,
1499 sizeof(fs->super->s_last_mounted));
1500 strncpy(fs->super->s_last_mounted, mount_dir,
1501 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001502 }
1503
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001504 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001505 show_stats(fs);
1506
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001507 if (noaction)
1508 exit(0);
1509
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001510 if (fs->super->s_feature_incompat &
1511 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1512 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001513 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001514 }
1515
Theodore Ts'o3839e651997-04-26 13:21:57 +00001516 if (bad_blocks_filename)
1517 read_bb_file(fs, &bb_list, bad_blocks_filename);
1518 if (cflag)
1519 test_disk(fs, &bb_list);
1520
1521 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001522 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001523 retval = ext2fs_allocate_tables(fs);
1524 if (retval) {
1525 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001526 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001527 exit(1);
1528 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001529 if (super_only) {
1530 fs->super->s_state |= EXT2_ERROR_FS;
1531 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1532 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001533 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001534 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001535 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001536 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001537 blk_t ret_blk;
1538
1539#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001540 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001541#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001542
1543 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001544 * Wipe out any old MD RAID (or other) metadata at the end
1545 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001546 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001547 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001548 start = (blocks & ~(rsv - 1));
1549 if (start > rsv)
1550 start -= rsv;
1551 if (start > 0)
1552 retval = zero_blocks(fs, start, blocks - start,
1553 NULL, &ret_blk, NULL);
1554
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001555 if (retval) {
1556 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001557 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001558 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001559 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001560 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001561 create_root_dir(fs);
1562 create_lost_and_found(fs);
1563 reserve_inodes(fs);
1564 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001565 if (fs->super->s_feature_compat &
1566 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1567 retval = ext2fs_create_resize_inode(fs);
1568 if (retval) {
1569 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001570 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001571 exit(1);
1572 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001573 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001574 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001575
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001576 if (journal_device) {
1577 ext2_filsys jfs;
1578
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001579 if (!force)
1580 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001581 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001582
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001583 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1584 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1585 fs->blocksize, unix_io_manager, &jfs);
1586 if (retval) {
1587 com_err(program_name, retval,
1588 _("while trying to open journal device %s\n"),
1589 journal_device);
1590 exit(1);
1591 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001592 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001593 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001594 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001595 fflush(stdout);
1596 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001597 retval = ext2fs_add_journal_device(fs, jfs);
1598 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001599 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001600 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001601 journal_device);
1602 exit(1);
1603 }
1604 if (!quiet)
1605 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001606 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001607 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001608 } else if ((journal_size) ||
1609 (fs_param.s_feature_compat &
1610 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001611 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001612
1613 if (!journal_blocks) {
1614 fs->super->s_feature_compat &=
1615 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1616 goto no_journal;
1617 }
1618 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001619 printf(_("Creating journal (%d blocks): "),
1620 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001621 fflush(stdout);
1622 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001623 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1624 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001625 if (retval) {
1626 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001627 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001628 exit(1);
1629 }
1630 if (!quiet)
1631 printf(_("done\n"));
1632 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001633no_journal:
1634
Theodore Ts'o3839e651997-04-26 13:21:57 +00001635 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001636 printf(_("Writing superblocks and "
1637 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001638 retval = ext2fs_flush(fs);
1639 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001640 fprintf(stderr,
1641 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001642 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001643 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001644 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001645 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1646 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001647 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001648 val = ext2fs_close(fs);
1649 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001650}