blob: b83aac1b43aeedbeeccbd5893b56ec8cad49f609 [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] "
Andreas Dilger067911a2006-07-15 22:08:20 -040097 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
98 "[-j] [-J journal-options]\n"
99 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
100 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
101 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
102 "[-r fs-revision] [-R options] [-qvSV]\n\tdevice [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 program_name);
104 exit(1);
105}
106
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000107static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108{
109 int l = 0;
110
111 arg >>= 1;
112 while (arg) {
113 l++;
114 arg >>= 1;
115 }
116 return l;
117}
118
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000119static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000120{
121 int l;
122
123 for (l=0; arg ; l++)
124 arg = arg / 10;
125 return l;
126}
127
Theodore Ts'od99225e2004-09-25 07:40:12 -0400128static int parse_version_number(const char *s)
129{
130 int major, minor, rev;
131 char *endptr;
132 const char *cp = s;
133
134 if (!s)
135 return 0;
136 major = strtol(cp, &endptr, 10);
137 if (cp == endptr || *endptr != '.')
138 return 0;
139 cp = endptr + 1;
140 minor = strtol(cp, &endptr, 10);
141 if (cp == endptr || *endptr != '.')
142 return 0;
143 cp = endptr + 1;
144 rev = strtol(cp, &endptr, 10);
145 if (cp == endptr)
146 return 0;
147 return ((((major * 256) + minor) * 256) + rev);
148}
149
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000150/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 * Helper function for read_bb_file and test_disk
152 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500153static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154{
Theodore Ts'o66938372002-03-08 00:14:46 -0500155 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 return;
157}
158
159/*
160 * Reads the bad blocks list from a file
161 */
162static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
163 const char *bad_blocks_file)
164{
165 FILE *f;
166 errcode_t retval;
167
168 f = fopen(bad_blocks_file, "r");
169 if (!f) {
170 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000171 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172 exit(1);
173 }
174 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
175 fclose (f);
176 if (retval) {
177 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000178 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 exit(1);
180 }
181}
182
183/*
184 * Runs the badblocks program to test the disk
185 */
186static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
187{
188 FILE *f;
189 errcode_t retval;
190 char buf[1024];
191
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400192 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500193 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
194 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000196 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197 f = popen(buf, "r");
198 if (!f) {
199 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400200 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201 exit(1);
202 }
203 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000204 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 if (retval) {
206 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000207 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 exit(1);
209 }
210}
211
212static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
213{
Theodore Ts'o54434922003-12-07 01:28:50 -0500214 dgrp_t i;
215 blk_t j;
216 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000217 blk_t blk;
218 badblocks_iterate bb_iter;
219 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000220 blk_t group_block;
221 int group;
222 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223
224 if (!bb_list)
225 return;
226
227 /*
228 * The primary superblock and group descriptors *must* be
229 * good; if not, abort.
230 */
231 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
232 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000233 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000234 fprintf(stderr, _("Block %d in primary "
235 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400236 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000237 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500239 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 exit(1);
241 }
242 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000243
244 /*
245 * See if any of the bad blocks are showing up in the backup
246 * superblocks and/or group descriptors. If so, issue a
247 * warning and adjust the block counts appropriately.
248 */
249 group_block = fs->super->s_first_data_block +
250 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000251
252 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000253 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000254 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000255 if (ext2fs_badblocks_list_test(bb_list,
256 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000257 if (!group_bad)
258 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500259_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000260" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000261 group_block);
262 group_bad++;
263 group = ext2fs_group_of_blk(fs, group_block+j);
264 fs->group_desc[group].bg_free_blocks_count++;
265 fs->super->s_free_blocks_count++;
266 }
267 }
268 group_block += fs->super->s_blocks_per_group;
269 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270
271 /*
272 * Mark all the bad blocks as used...
273 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000274 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000275 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000276 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000277 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278 exit(1);
279 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000280 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000281 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000282 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283}
284
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000285/*
286 * These functions implement a generalized progress meter.
287 */
288struct progress_struct {
289 char format[20];
290 char backup[80];
291 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400292 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000293};
294
295static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500296 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000298 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000299
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000300 memset(progress, 0, sizeof(struct progress_struct));
301 if (quiet)
302 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000303
304 /*
305 * Figure out how many digits we need
306 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000307 i = int_log10(max);
308 sprintf(progress->format, "%%%dd/%%%dld", i, i);
309 memset(progress->backup, '\b', sizeof(progress->backup)-1);
310 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500311 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000312 progress->backup[(2*i)+1] = 0;
313 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000314
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400315 progress->skip_progress = 0;
316 if (getenv("MKE2FS_SKIP_PROGRESS"))
317 progress->skip_progress++;
318
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000319 fputs(label, stdout);
320 fflush(stdout);
321}
322
323static void progress_update(struct progress_struct *progress, __u32 val)
324{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400325 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000326 return;
327 printf(progress->format, val, progress->max);
328 fputs(progress->backup, stdout);
329}
330
331static void progress_close(struct progress_struct *progress)
332{
333 if (progress->format[0] == 0)
334 return;
335 fputs(_("done \n"), stdout);
336}
337
338
339/*
340 * Helper function which zeros out _num_ blocks starting at _blk_. In
341 * case of an error, the details of the error is returned via _ret_blk_
342 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
343 * success, and an error code on an error.
344 *
345 * As a special case, if the first argument is NULL, then it will
346 * attempt to free the static zeroizing buffer. (This is to keep
347 * programs that check for memory leaks happy.)
348 */
349static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
350 struct progress_struct *progress,
351 blk_t *ret_blk, int *ret_count)
352{
353 int j, count, next_update, next_update_incr;
354 static char *buf;
355 errcode_t retval;
356
357 /* If fs is null, clean up the static buffer and return */
358 if (!fs) {
359 if (buf) {
360 free(buf);
361 buf = 0;
362 }
363 return 0;
364 }
365 /* Allocate the zeroizing buffer if necessary */
366 if (!buf) {
367 buf = malloc(fs->blocksize * STRIDE_LENGTH);
368 if (!buf) {
369 com_err("malloc", ENOMEM,
370 _("while allocating zeroizing buffer"));
371 exit(1);
372 }
373 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
374 }
375 /* OK, do the write loop */
376 next_update = 0;
377 next_update_incr = num / 100;
378 if (next_update_incr < 1)
379 next_update_incr = 1;
380 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400381 count = num - j;
382 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000383 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000384 retval = io_channel_write_blk(fs->io, blk, count, buf);
385 if (retval) {
386 if (ret_count)
387 *ret_count = count;
388 if (ret_blk)
389 *ret_blk = blk;
390 return retval;
391 }
392 if (progress && j > next_update) {
393 next_update += num / 100;
394 progress_update(progress, blk);
395 }
396 }
397 return 0;
398}
399
400static void write_inode_tables(ext2_filsys fs)
401{
402 errcode_t retval;
403 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500404 dgrp_t i;
405 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000406 struct progress_struct progress;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400407 int lazy_flag = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000408
409 if (quiet)
410 memset(&progress, 0, sizeof(progress));
411 else
412 progress_init(&progress, _("Writing inode tables: "),
413 fs->group_desc_count);
414
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400415 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
416 EXT2_FEATURE_COMPAT_LAZY_BG))
417 lazy_flag = 1;
418
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000420 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000422 blk = fs->group_desc[i].bg_inode_table;
423 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000424
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400425 if (!(lazy_flag &&
426 (fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT))) {
427 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
428 if (retval) {
429 fprintf(stderr, _("\nCould not write %d "
430 "blocks in inode table starting at %u: %s\n"),
431 num, blk, error_message(retval));
432 exit(1);
433 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000434 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000435 if (sync_kludge) {
436 if (sync_kludge == 1)
437 sync();
438 else if ((i % sync_kludge) == 0)
439 sync();
440 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000442 zero_blocks(0, 0, 0, 0, 0, 0);
443 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444}
445
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400446static void setup_lazy_bg(ext2_filsys fs)
447{
448 dgrp_t i;
449 int blks;
450 struct ext2_super_block *sb = fs->super;
451 struct ext2_group_desc *bg = fs->group_desc;
452
453 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
454 EXT2_FEATURE_COMPAT_LAZY_BG)) {
455 for (i = 0; i < fs->group_desc_count; i++, bg++) {
456 if ((i == 0) ||
457 (i == fs->group_desc_count-1))
458 continue;
459 if (bg->bg_free_inodes_count ==
460 sb->s_inodes_per_group) {
461 bg->bg_free_inodes_count = 0;
462 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
463 sb->s_free_inodes_count -=
464 sb->s_inodes_per_group;
465 }
466 blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
467 if (bg->bg_free_blocks_count == blks) {
468 bg->bg_free_blocks_count = 0;
469 bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
470 sb->s_free_blocks_count -= blks;
471 }
472 }
473 }
474}
475
476
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477static void create_root_dir(ext2_filsys fs)
478{
479 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000480 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481
482 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
483 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000484 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 exit(1);
486 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000487 if (geteuid()) {
488 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
489 if (retval) {
490 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000491 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000492 exit(1);
493 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000494 inode.i_uid = getuid();
495 if (inode.i_uid)
496 inode.i_gid = getgid();
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500497 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000498 if (retval) {
499 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000500 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000501 exit(1);
502 }
503 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504}
505
506static void create_lost_and_found(ext2_filsys fs)
507{
508 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000509 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 const char *name = "lost+found";
511 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000512 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513
Theodore Ts'o6a525062001-12-24 09:40:00 -0500514 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
516 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000517 com_err("ext2fs_mkdir", retval,
518 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 exit(1);
520 }
521
522 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
523 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000524 com_err("ext2_lookup", retval,
525 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 exit(1);
527 }
528
529 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000530 if ((lpf_size += fs->blocksize) >= 16*1024)
531 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 retval = ext2fs_expand_dir(fs, ino);
533 if (retval) {
534 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000535 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 exit(1);
537 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500538 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539}
540
541static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
542{
543 errcode_t retval;
544
Theodore Ts'of3db3561997-04-26 13:34:30 +0000545 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 fs->group_desc[0].bg_free_inodes_count--;
547 fs->super->s_free_inodes_count--;
548 retval = ext2fs_update_bb_inode(fs, bb_list);
549 if (retval) {
550 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000551 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 exit(1);
553 }
554
555}
556
557static void reserve_inodes(ext2_filsys fs)
558{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000559 ext2_ino_t i;
560 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000562 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000563 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 group = ext2fs_group_of_ino(fs, i);
565 fs->group_desc[group].bg_free_inodes_count--;
566 fs->super->s_free_inodes_count--;
567 }
568 ext2fs_mark_ib_dirty(fs);
569}
570
Theodore Ts'o756df352002-03-07 20:52:12 -0500571#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500572#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500573#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500574
Theodore Ts'o04a96852001-08-30 21:55:26 -0400575static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000576{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400577 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000578 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500579 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000580
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400581 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600582 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500583 printf(_("Out of memory erasing sectors %d-%d\n"),
584 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600585 exit(1);
586 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500587
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500588 if (sect == 0) {
589 /* Check for a BSD disklabel, and don't erase it if so */
590 retval = io_channel_read_blk(fs->io, 0, -512, buf);
591 if (retval)
592 fprintf(stderr,
593 _("Warning: could not read block 0: %s\n"),
594 error_message(retval));
595 else {
596 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
597 if ((*magic == BSD_DISKMAGIC) ||
598 (*magic == BSD_MAGICDISK))
599 return;
600 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500601 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500602
Theodore Ts'o70988102002-07-14 08:00:00 -0400603 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000604 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400605 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000606 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400607 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000608 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500609 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
610 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000611}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000612
613static void create_journal_dev(ext2_filsys fs)
614{
615 struct progress_struct progress;
616 errcode_t retval;
617 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000618 blk_t blk;
619 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000620
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000621 retval = ext2fs_create_journal_superblock(fs,
622 fs->super->s_blocks_count, 0, &buf);
623 if (retval) {
624 com_err("create_journal_dev", retval,
625 _("while initializing journal superblock"));
626 exit(1);
627 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000628 if (quiet)
629 memset(&progress, 0, sizeof(progress));
630 else
631 progress_init(&progress, _("Zeroing journal device: "),
632 fs->super->s_blocks_count);
633
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000634 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
635 &progress, &blk, &count);
636 if (retval) {
637 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000638 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000639 blk, count);
640 exit(1);
641 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000642 zero_blocks(0, 0, 0, 0, 0, 0);
643
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000644 retval = io_channel_write_blk(fs->io,
645 fs->super->s_first_data_block+1,
646 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000647 if (retval) {
648 com_err("create_journal_dev", retval,
649 _("while writing journal superblock"));
650 exit(1);
651 }
652 progress_close(&progress);
653}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000654
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655static void show_stats(ext2_filsys fs)
656{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000657 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000658 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500659 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500661 dgrp_t i;
662 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500664 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500665 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500666 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000667
668 memset(buf, 0, sizeof(buf));
669 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000670 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500671 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500672 os = e2p_os2string(fs->super->s_creator_os);
673 fputs(os, stdout);
674 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000675 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000676 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000677 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000678 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000679 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000680 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000681 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000682 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683 s->s_r_blocks_count,
684 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000685 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500686 if (s->s_reserved_gdt_blocks)
687 printf(_("Maximum filesystem blocks=%lu\n"),
688 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
689 (fs->blocksize / sizeof(struct ext2_group_desc)) *
690 s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000691 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000692 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000693 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000694 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000695 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000696 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000697 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698
699 if (fs->group_desc_count == 1) {
700 printf("\n");
701 return;
702 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500703
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000704 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000705 group_block = s->s_first_data_block;
706 col_left = 0;
707 for (i = 1; i < fs->group_desc_count; i++) {
708 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000709 if (!ext2fs_bg_has_super(fs, i))
710 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000711 if (i != 1)
712 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000713 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000714 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000715 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000716 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000718 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000719 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000720 }
721 printf("\n\n");
722}
723
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000724/*
725 * Set the S_CREATOR_OS field. Return true if OS is known,
726 * otherwise, 0.
727 */
728static int set_os(struct ext2_super_block *sb, char *os)
729{
730 if (isdigit (*os))
731 sb->s_creator_os = atoi (os);
732 else if (strcasecmp(os, "linux") == 0)
733 sb->s_creator_os = EXT2_OS_LINUX;
734 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
735 sb->s_creator_os = EXT2_OS_HURD;
736 else if (strcasecmp(os, "masix") == 0)
737 sb->s_creator_os = EXT2_OS_MASIX;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500738 else if (strcasecmp(os, "freebsd") == 0)
739 sb->s_creator_os = EXT2_OS_FREEBSD;
740 else if (strcasecmp(os, "lites") == 0)
741 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000742 else
743 return 0;
744 return 1;
745}
746
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000747#define PATH_SET "PATH=/sbin"
748
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500749static void parse_extended_opts(struct ext2_super_block *param,
750 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000751{
752 char *buf, *token, *next, *p, *arg;
753 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500754 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000755
756 len = strlen(opts);
757 buf = malloc(len+1);
758 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500759 fprintf(stderr,
760 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000761 exit(1);
762 }
763 strcpy(buf, opts);
764 for (token = buf; token && *token; token = next) {
765 p = strchr(token, ',');
766 next = 0;
767 if (p) {
768 *p = 0;
769 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500770 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000771 arg = strchr(token, '=');
772 if (arg) {
773 *arg = 0;
774 arg++;
775 }
776 if (strcmp(token, "stride") == 0) {
777 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500778 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000779 continue;
780 }
781 fs_stride = strtoul(arg, &p, 0);
782 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000783 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400784 _("Invalid stride parameter: %s\n"),
785 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500786 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000787 continue;
788 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500789 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500790 unsigned long resize, bpg, rsv_groups;
791 unsigned long group_desc_count, desc_blocks;
792 unsigned int gdpb, blocksize;
793 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500794
795 if (!arg) {
796 r_usage++;
797 continue;
798 }
799
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500800 resize = parse_num_blocks(arg,
801 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500802
803 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500804 fprintf(stderr,
805 _("Invalid resize parameter: %s\n"),
806 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500807 r_usage++;
808 continue;
809 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500810 if (resize <= param->s_blocks_count) {
811 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400812 _("The resize maximum must be greater "
813 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500814 r_usage++;
815 continue;
816 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500817
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500818 blocksize = EXT2_BLOCK_SIZE(param);
819 bpg = param->s_blocks_per_group;
820 if (!bpg)
821 bpg = blocksize * 8;
822 gdpb = blocksize / sizeof(struct ext2_group_desc);
Theodore Ts'o69022e02006-08-30 01:57:00 -0400823 group_desc_count =
824 ext2fs_div_ceil(param->s_blocks_count, bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500825 desc_blocks = (group_desc_count +
826 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400827 rsv_groups = ext2fs_div_ceil(resize, bpg);
828 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500829 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500830 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500831 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
832
833 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400834 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
835 fprintf(stderr,
836 _("On-line resizing not supported with revision 0 filesystems\n"));
837 exit(1);
838 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500839 param->s_feature_compat |=
840 EXT2_FEATURE_COMPAT_RESIZE_INODE;
841
842 param->s_reserved_gdt_blocks = rsv_gdb;
843 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000844 } else
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500845 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000846 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500847 if (r_usage) {
848 fprintf(stderr, _("\nBad options specified.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400849 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000850 "and may take an argument which\n"
851 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400852 "Valid extended options are:\n"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500853 "\tstride=<stride length in blocks>\n"
854 "\tresize=<resize maximum size in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000855 exit(1);
856 }
857}
858
Theodore Ts'o896938d1999-10-23 01:04:50 +0000859static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400860 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500861 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400862 EXT2_FEATURE_COMPAT_DIR_INDEX |
863 EXT2_FEATURE_COMPAT_LAZY_BG, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000864 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400865 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
866 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000867 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
868};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000869
870
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500871static void syntax_err_report(const char *filename, long err, int line_num)
872{
873 fprintf(stderr,
874 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
875 filename, line_num, error_message(err));
876 exit(1);
877}
878
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200879static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500880
881static void edit_feature(const char *str, __u32 *compat_array)
882{
883 if (!str)
884 return;
885
886 if (e2p_edit_feature(str, compat_array, ok_features)) {
887 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
888 str);
889 exit(1);
890 }
891}
892
Theodore Ts'o3839e651997-04-26 13:21:57 +0000893static void PRS(int argc, char *argv[])
894{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400895 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000896 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500897 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000898 int blocksize = 0;
899 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600900 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -0500901 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400902 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500903 int show_version_only = 0;
Eric Sandeenf3358642006-09-12 14:56:17 -0400904 __u64 num_inodes = 0; /* u64 to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000905 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000906 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500907 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500908 const char * fs_type = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000909 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500910#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000911 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000912#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400913 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500914 int s_opt = -1, r_opt = -1;
915 char *fs_features = 0;
916 int use_bsize;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000917
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000919 if (oldpath) {
920 char *newpath;
921
922 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
923 strcpy (newpath, PATH_SET);
924 strcat (newpath, ":");
925 strcat (newpath, oldpath);
926 putenv (newpath);
927 } else
928 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000929
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000930 tmp = getenv("MKE2FS_SYNC");
931 if (tmp)
932 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400933
934 /* Determine the system page size if possible */
935#ifdef HAVE_SYSCONF
936#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
937#define _SC_PAGESIZE _SC_PAGE_SIZE
938#endif
939#ifdef _SC_PAGESIZE
940 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600941 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400942 sys_page_size = sysval;
943#endif /* _SC_PAGESIZE */
944#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500945
946 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
947 config_fn[0] = tmp;
948 profile_set_syntax_err_cb(syntax_err_report);
949 profile_init(config_fn, &profile);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000950
Theodore Ts'o3839e651997-04-26 13:21:57 +0000951 setbuf(stdout, NULL);
952 setbuf(stderr, NULL);
953 initialize_ext2_error_table();
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500954 memset(&fs_param, 0, sizeof(struct ext2_super_block));
955 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400956
Theodore Ts'o756df352002-03-07 20:52:12 -0500957#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000958 if (uname(&ut)) {
959 perror("uname");
960 exit(1);
961 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400962 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500963 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500964 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000965#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700966
967 if (argc && *argv) {
968 program_name = get_progname(*argv);
969
970 /* If called as mkfs.ext3, create a journal inode */
971 if (!strcmp(program_name, "mkfs.ext3"))
972 journal_size = -1;
973 }
974
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000975 while ((c = getopt (argc, argv,
Andreas Dilger25247852005-07-06 12:58:15 -0500976 "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 +0000977 switch (c) {
978 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400979 blocksize = strtol(optarg, &tmp, 0);
980 b = (blocksize > 0) ? blocksize : -blocksize;
981 if (b < EXT2_MIN_BLOCK_SIZE ||
982 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000983 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400984 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000985 exit(1);
986 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600987 if (blocksize > 4096)
988 fprintf(stderr, _("Warning: blocksize %d not "
989 "usable on most systems.\n"),
990 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400991 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500992 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400993 int_log2(blocksize >>
994 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000995 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000996 case 'c': /* Check for bad blocks */
997 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500998 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000999 break;
1000 case 'f':
1001 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001002 if (size < EXT2_MIN_BLOCK_SIZE ||
1003 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001004 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001005 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001006 optarg);
1007 exit(1);
1008 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001009 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001010 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001011 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001012 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001013 break;
1014 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001015 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001016 if (*tmp) {
1017 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001018 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001019 exit(1);
1020 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001021 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001022 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001023 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001024 exit(1);
1025 }
1026 break;
1027 case 'i':
1028 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001029 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1030 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001031 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001032 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001033 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001034 optarg, EXT2_MIN_BLOCK_SIZE,
1035 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001036 exit(1);
1037 }
1038 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001039 case 'J':
1040 parse_journal_opts(optarg);
1041 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001042 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001043 if (!journal_size)
1044 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001045 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001046 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001047 bad_blocks_filename = malloc(strlen(optarg)+1);
1048 if (!bad_blocks_filename) {
1049 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001050 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001051 exit(1);
1052 }
1053 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001054 break;
1055 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001056 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001057 if (reserved_ratio > 50 || *tmp) {
1058 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001059 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001060 optarg);
1061 exit(1);
1062 }
1063 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001064 case 'n':
1065 noaction++;
1066 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001067 case 'o':
1068 creator_os = optarg;
1069 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001070 case 'q':
1071 quiet = 1;
1072 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001073 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001074 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001075 if (*tmp) {
1076 com_err(program_name, 0,
1077 _("bad revision level - %s"), optarg);
1078 exit(1);
1079 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001080 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001081 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001082 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001083 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001084 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001085 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001086 inode_size = strtoul(optarg, &tmp, 0);
1087 if (*tmp) {
1088 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001089 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001090 exit(1);
1091 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001092 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001093 case 'v':
1094 verbose = 1;
1095 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001096 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001097 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001098 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001099 case 'L':
1100 volume_label = optarg;
1101 break;
1102 case 'M':
1103 mount_dir = optarg;
1104 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001105 case 'N':
1106 num_inodes = strtoul(optarg, &tmp, 0);
1107 if (*tmp) {
1108 com_err(program_name, 0,
1109 _("bad num inodes - %s"), optarg);
1110 exit(1);
1111 }
1112 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001113 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001114 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001115 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001116 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001117 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001118 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001119 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001120 case 'S':
1121 super_only = 1;
1122 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001123 case 'T':
1124 fs_type = optarg;
1125 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001126 case 'V':
1127 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001128 show_version_only++;
1129 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001130 default:
1131 usage();
1132 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001133 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001134 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001135 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001136 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001137
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001138 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001139 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1140 E2FSPROGS_DATE);
1141
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001142 if (show_version_only) {
1143 fprintf(stderr, _("\tUsing %s\n"),
1144 error_message(EXT2_ET_BASE));
1145 exit(0);
1146 }
1147
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001148 /*
1149 * If there's no blocksize specified and there is a journal
1150 * device, use it to figure out the blocksize
1151 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001152 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001153 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001154 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001155
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001156#ifdef CONFIG_TESTIO_DEBUG
1157 io_ptr = test_io_manager;
1158 test_io_backing_manager = unix_io_manager;
1159#else
1160 io_ptr = unix_io_manager;
1161#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001162 retval = ext2fs_open(journal_device,
1163 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001164 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001165 if (retval) {
1166 com_err(program_name, retval,
1167 _("while trying to open journal device %s\n"),
1168 journal_device);
1169 exit(1);
1170 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001171 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001172 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001173 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001174 "minimum blocksize %d\n"), jfs->blocksize,
1175 -blocksize);
1176 exit(1);
1177 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001178 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001179 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001180 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1181 ext2fs_close(jfs);
1182 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001183
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001184 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001185 if (!force) {
1186 com_err(program_name, 0,
1187 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001188 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001189 proceed_question();
1190 }
1191 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1192 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001193 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001194 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001195 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001196 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1197 fs_param.s_log_block_size);
1198 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001199 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001200 argv[optind - 1]);
1201 exit(1);
1202 }
1203 }
1204 if (optind < argc)
1205 usage();
1206
Theodore Ts'o74becf31997-04-26 14:37:06 +00001207 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001208 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001209 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001210
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001211 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001212
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001213 if (noaction && fs_param.s_blocks_count) {
1214 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001215 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001216 } else {
1217 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001218 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001219 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001220 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001221 if ((retval == EFBIG) &&
1222 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001223 (fs_param.s_log_block_size == 0)) {
1224 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001225 blocksize = 4096;
1226 goto retry;
1227 }
1228 }
1229
Theodore Ts'oa789d841998-03-30 01:20:55 +00001230 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1231 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001232 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001233 exit(1);
1234 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001235 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001236 if (retval == EXT2_ET_UNIMPLEMENTED) {
1237 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001238 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001239 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001240 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001241 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001242 } else {
1243 if (dev_size == 0) {
1244 com_err(program_name, 0,
1245 _("Device size reported to be zero. "
1246 "Invalid partition specified, or\n\t"
1247 "partition table wasn't reread "
1248 "after running fdisk, due to\n\t"
1249 "a modified partition being busy "
1250 "and in use. You may need to reboot\n\t"
1251 "to re-read your partition table.\n"
1252 ));
1253 exit(1);
1254 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001255 fs_param.s_blocks_count = dev_size;
1256 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1257 fs_param.s_blocks_count &= ~((sys_page_size /
1258 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001259 }
1260
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001261 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001262 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001263 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001264 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001265 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001266
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001267 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001268 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001269 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1270
1271 if (megs <= 3)
1272 fs_type = "floppy";
1273 else if (megs <= 512)
1274 fs_type = "small";
1275 else
1276 fs_type = "default";
1277 }
1278
1279 /* Figure out what features should be enabled */
1280
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001281 tmp = tmp2 = NULL;
1282 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1283 profile_get_string(profile, "defaults", "base_features", 0,
1284 "filetype,sparse_super", &tmp);
1285 profile_get_string(profile, "fs_types", fs_type,
1286 "base_features", tmp, &tmp2);
1287 edit_feature(tmp2, &fs_param.s_feature_compat);
1288 free(tmp);
1289 free(tmp2);
1290
1291 tmp = tmp2 = NULL;
1292 profile_get_string(profile, "defaults", "default_features", 0,
1293 "", &tmp);
1294 profile_get_string(profile, "fs_types", fs_type,
1295 "default_features", tmp, &tmp2);
1296 }
1297 edit_feature(fs_features ? fs_features : tmp2,
1298 &fs_param.s_feature_compat);
1299 if (tmp)
1300 free(tmp);
1301 if (tmp2)
1302 free(tmp2);
1303
1304 if (r_opt == EXT2_GOOD_OLD_REV &&
1305 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1306 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001307 fprintf(stderr, _("Filesystem features not supported "
1308 "with revision 0 filesystems\n"));
1309 exit(1);
1310 }
1311
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001312 if (s_opt > 0) {
1313 if (r_opt == EXT2_GOOD_OLD_REV) {
1314 fprintf(stderr, _("Sparse superblocks not supported "
1315 "with revision 0 filesystems\n"));
1316 exit(1);
1317 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001318 fs_param.s_feature_ro_compat |=
1319 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001320 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001321 fs_param.s_feature_ro_compat &=
1322 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1323
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001324 if (journal_size != 0) {
1325 if (r_opt == EXT2_GOOD_OLD_REV) {
1326 fprintf(stderr, _("Journals not supported "
1327 "with revision 0 filesystems\n"));
1328 exit(1);
1329 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001330 fs_param.s_feature_compat |=
1331 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001332 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001333
1334 if (fs_param.s_feature_incompat &
1335 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1336 if (!fs_type)
1337 fs_type = "journal";
1338 reserved_ratio = 0;
1339 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1340 fs_param.s_feature_compat = 0;
1341 fs_param.s_feature_ro_compat = 0;
1342 }
1343
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001344 /* Set first meta blockgroup via an environment variable */
1345 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001346 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001347 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001348 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001349
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001350 /* Get the hardware sector size, if available */
1351 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1352 if (retval) {
1353 com_err(program_name, retval,
1354 _("while trying to determine hardware sector size"));
1355 exit(1);
1356 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001357
Theodore Ts'o54434922003-12-07 01:28:50 -05001358 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001359 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001360
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001361 if (blocksize <= 0) {
1362 profile_get_integer(profile, "defaults", "blocksize", 0,
1363 1024, &use_bsize);
1364 profile_get_integer(profile, "fs_types", fs_type,
1365 "blocksize", use_bsize, &use_bsize);
1366
1367 if (use_bsize == -1) {
1368 use_bsize = sys_page_size;
1369 if ((linux_version_code < (2*65536 + 6*256)) &&
1370 (use_bsize > 4096))
1371 use_bsize = 4096;
1372 }
1373 if (sector_size && use_bsize < sector_size)
1374 use_bsize = sector_size;
1375 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1376 use_bsize = -blocksize;
1377 blocksize = use_bsize;
1378 fs_param.s_blocks_count /= blocksize / 1024;
1379 }
1380
1381 if (inode_ratio == 0) {
1382 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1383 8192, &inode_ratio);
1384 profile_get_integer(profile, "fs_types", fs_type,
1385 "inode_ratio", inode_ratio,
1386 &inode_ratio);
1387
1388 if (inode_ratio < blocksize)
1389 inode_ratio = blocksize;
1390 }
1391
1392 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1393 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1394
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001395 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001396
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001397 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001398 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001399
1400 /* Since sparse_super is the default, we would only have a problem
1401 * here if it was explicitly disabled.
1402 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001403 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1404 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001405 com_err(program_name, 0,
1406 _("reserved online resize blocks not supported "
1407 "on non-sparse filesystem"));
1408 exit(1);
1409 }
1410
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001411 if (fs_param.s_blocks_per_group) {
1412 if (fs_param.s_blocks_per_group < 256 ||
1413 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001414 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001415 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001416 exit(1);
1417 }
1418 }
1419
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001420 if (!force && fs_param.s_blocks_count >= (1 << 31)) {
Theodore Ts'o675b79c2005-06-30 20:00:41 -04001421 com_err(program_name, 0,
1422 _("Filesystem too large. No more than 2**31-1 blocks\n"
1423 "\t (8TB using a blocksize of 4k) are currently supported."));
1424 exit(1);
1425 }
1426
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001427 if ((blocksize > 4096) &&
1428 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1429 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1430 "blocksizes greater than 4096\n\tusing ext3. "
1431 "Use -b 4096 if this is an issue for you.\n\n"));
1432
Andreas Dilger067911a2006-07-15 22:08:20 -04001433 if (inode_size == 0) {
1434 profile_get_integer(profile, "defaults", "inode_size", NULL,
1435 0, &inode_size);
1436 profile_get_integer(profile, "fs_types", fs_type,
1437 "inode_size", inode_size,
1438 &inode_size);
1439 }
1440
1441 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001442 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001443 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001444 inode_size & (inode_size - 1)) {
1445 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001446 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001447 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001448 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001449 exit(1);
1450 }
1451 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1452 fprintf(stderr, _("Warning: %d-byte inodes not usable "
Andreas Dilger067911a2006-07-15 22:08:20 -04001453 "on older systems\n"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001454 inode_size);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001455 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001456 }
1457
Eric Sandeenf3358642006-09-12 14:56:17 -04001458 /* Make sure number of inodes specified will fit in 32 bits */
1459 if (num_inodes == 0) {
1460 __u64 n;
1461 n = (__u64) fs_param.s_blocks_count * blocksize / inode_ratio;
1462 if (n > ~0U) {
1463 com_err(program_name, 0,
1464 _("too many inodes (%llu), raise inode ratio?"), n);
1465 exit(1);
1466 }
1467 } else if (num_inodes > ~0U) {
1468 com_err(program_name, 0,
1469 _("too many inodes (%llu), specify < 2^32 inodes"),
1470 (__u64)num_inodes);
1471 exit(1);
1472 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001473 /*
1474 * Calculate number of inodes based on the inode ratio
1475 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001476 fs_param.s_inodes_count = num_inodes ? num_inodes :
1477 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001478 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001479
1480 /*
1481 * Calculate number of blocks to reserve
1482 */
Theodore Ts'oa8862d92006-08-30 03:08:13 -04001483 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1484 fs_param.s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001485}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001486
Theodore Ts'o3839e651997-04-26 13:21:57 +00001487int main (int argc, char *argv[])
1488{
1489 errcode_t retval = 0;
1490 ext2_filsys fs;
1491 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001492 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001493 unsigned int i;
1494 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001495 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001496
1497#ifdef ENABLE_NLS
1498 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001499 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001500 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1501 textdomain(NLS_CAT_NAME);
1502#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001503 PRS(argc, argv);
1504
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001505#ifdef CONFIG_TESTIO_DEBUG
1506 io_ptr = test_io_manager;
1507 test_io_backing_manager = unix_io_manager;
1508#else
1509 io_ptr = unix_io_manager;
1510#endif
1511
Theodore Ts'o3839e651997-04-26 13:21:57 +00001512 /*
1513 * Initialize the superblock....
1514 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001515 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001516 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001517 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001518 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001519 exit(1);
1520 }
1521
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001522 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001523 * Wipe out the old on-disk superblock
1524 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001525 if (!noaction)
1526 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001527
1528 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001529 * Generate a UUID for it...
1530 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001531 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001532
1533 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001534 * Initialize the directory index variables
1535 */
1536 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1537 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1538
1539 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001540 * Add "jitter" to the superblock's check interval so that we
1541 * don't check all the filesystems at the same time. We use a
1542 * kludgy hack of using the UUID to derive a random jitter value.
1543 */
1544 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1545 val += fs->super->s_uuid[i];
1546 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1547
1548 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001549 * Override the creator OS, if applicable
1550 */
1551 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001552 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001553 exit(1);
1554 }
1555
1556 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001557 * For the Hurd, we will turn off filetype since it doesn't
1558 * support it.
1559 */
1560 if (fs->super->s_creator_os == EXT2_OS_HURD)
1561 fs->super->s_feature_incompat &=
1562 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1563
1564 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001565 * Set the volume label...
1566 */
1567 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001568 memset(fs->super->s_volume_name, 0,
1569 sizeof(fs->super->s_volume_name));
1570 strncpy(fs->super->s_volume_name, volume_label,
1571 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001572 }
1573
1574 /*
1575 * Set the last mount directory
1576 */
1577 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001578 memset(fs->super->s_last_mounted, 0,
1579 sizeof(fs->super->s_last_mounted));
1580 strncpy(fs->super->s_last_mounted, mount_dir,
1581 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001582 }
1583
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001584 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001585 show_stats(fs);
1586
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001587 if (noaction)
1588 exit(0);
1589
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001590 if (fs->super->s_feature_incompat &
1591 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1592 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001593 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001594 }
1595
Theodore Ts'o3839e651997-04-26 13:21:57 +00001596 if (bad_blocks_filename)
1597 read_bb_file(fs, &bb_list, bad_blocks_filename);
1598 if (cflag)
1599 test_disk(fs, &bb_list);
1600
1601 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001602 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001603 retval = ext2fs_allocate_tables(fs);
1604 if (retval) {
1605 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001606 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001607 exit(1);
1608 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001609 if (super_only) {
1610 fs->super->s_state |= EXT2_ERROR_FS;
1611 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1612 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001613 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001614 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001615 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001616 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001617 blk_t ret_blk;
1618
1619#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001620 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001621#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001622
1623 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001624 * Wipe out any old MD RAID (or other) metadata at the end
1625 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001626 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001627 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001628 start = (blocks & ~(rsv - 1));
1629 if (start > rsv)
1630 start -= rsv;
1631 if (start > 0)
1632 retval = zero_blocks(fs, start, blocks - start,
1633 NULL, &ret_blk, NULL);
1634
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001635 if (retval) {
1636 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001637 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001638 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001639 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -04001640 setup_lazy_bg(fs);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001641 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001642 create_root_dir(fs);
1643 create_lost_and_found(fs);
1644 reserve_inodes(fs);
1645 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001646 if (fs->super->s_feature_compat &
1647 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1648 retval = ext2fs_create_resize_inode(fs);
1649 if (retval) {
1650 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001651 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001652 exit(1);
1653 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001654 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001655 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001656
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001657 if (journal_device) {
1658 ext2_filsys jfs;
1659
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001660 if (!force)
1661 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001662 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001663
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001664 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1665 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1666 fs->blocksize, unix_io_manager, &jfs);
1667 if (retval) {
1668 com_err(program_name, retval,
1669 _("while trying to open journal device %s\n"),
1670 journal_device);
1671 exit(1);
1672 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001673 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001674 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001675 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001676 fflush(stdout);
1677 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001678 retval = ext2fs_add_journal_device(fs, jfs);
1679 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001680 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001681 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001682 journal_device);
1683 exit(1);
1684 }
1685 if (!quiet)
1686 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001687 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001688 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001689 } else if ((journal_size) ||
1690 (fs_param.s_feature_compat &
1691 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001692 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001693
1694 if (!journal_blocks) {
1695 fs->super->s_feature_compat &=
1696 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1697 goto no_journal;
1698 }
1699 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001700 printf(_("Creating journal (%d blocks): "),
1701 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001702 fflush(stdout);
1703 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001704 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1705 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001706 if (retval) {
1707 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001708 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001709 exit(1);
1710 }
1711 if (!quiet)
1712 printf(_("done\n"));
1713 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001714no_journal:
1715
Theodore Ts'o3839e651997-04-26 13:21:57 +00001716 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001717 printf(_("Writing superblocks and "
1718 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001719 retval = ext2fs_flush(fs);
1720 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001721 fprintf(stderr,
1722 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001723 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001724 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001725 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001726 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1727 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001728 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001729 val = ext2fs_close(fs);
1730 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001731}