blob: 80a40ac70802c3ecafa2e096596a7ab9bf693ea7 [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 " : "",
Theodore Ts'o8ade4792006-11-08 00:41:50 -0500194 fs->device_name, fs->super->s_blocks_count-1);
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'o642935c2006-11-14 23:38:17 -0500917 char *newpath;
918 int pathlen = sizeof(PATH_SET) + 1;
919
920 if (oldpath)
921 pathlen += strlen(oldpath);
922 newpath = malloc(pathlen);
923 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000924
Theodore Ts'o3839e651997-04-26 13:21:57 +0000925 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000926 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000927 strcat (newpath, ":");
928 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -0500929 }
930 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000931
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000932 tmp = getenv("MKE2FS_SYNC");
933 if (tmp)
934 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400935
936 /* Determine the system page size if possible */
937#ifdef HAVE_SYSCONF
938#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
939#define _SC_PAGESIZE _SC_PAGE_SIZE
940#endif
941#ifdef _SC_PAGESIZE
942 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600943 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400944 sys_page_size = sysval;
945#endif /* _SC_PAGESIZE */
946#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500947
948 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
949 config_fn[0] = tmp;
950 profile_set_syntax_err_cb(syntax_err_report);
951 profile_init(config_fn, &profile);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000952
Theodore Ts'o3839e651997-04-26 13:21:57 +0000953 setbuf(stdout, NULL);
954 setbuf(stderr, NULL);
955 initialize_ext2_error_table();
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500956 memset(&fs_param, 0, sizeof(struct ext2_super_block));
957 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400958
Theodore Ts'o756df352002-03-07 20:52:12 -0500959#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000960 if (uname(&ut)) {
961 perror("uname");
962 exit(1);
963 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400964 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500965 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500966 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000967#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700968
969 if (argc && *argv) {
970 program_name = get_progname(*argv);
971
972 /* If called as mkfs.ext3, create a journal inode */
973 if (!strcmp(program_name, "mkfs.ext3"))
974 journal_size = -1;
975 }
976
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000977 while ((c = getopt (argc, argv,
Andreas Dilger25247852005-07-06 12:58:15 -0500978 "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 +0000979 switch (c) {
980 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400981 blocksize = strtol(optarg, &tmp, 0);
982 b = (blocksize > 0) ? blocksize : -blocksize;
983 if (b < EXT2_MIN_BLOCK_SIZE ||
984 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000985 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400986 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000987 exit(1);
988 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600989 if (blocksize > 4096)
990 fprintf(stderr, _("Warning: blocksize %d not "
991 "usable on most systems.\n"),
992 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400993 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500994 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400995 int_log2(blocksize >>
996 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000997 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000998 case 'c': /* Check for bad blocks */
999 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001000 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001001 break;
1002 case 'f':
1003 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001004 if (size < EXT2_MIN_BLOCK_SIZE ||
1005 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001006 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001007 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001008 optarg);
1009 exit(1);
1010 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001011 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001012 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001013 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001014 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001015 break;
1016 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001017 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001018 if (*tmp) {
1019 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001020 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001021 exit(1);
1022 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001023 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001024 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001025 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001026 exit(1);
1027 }
1028 break;
1029 case 'i':
1030 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001031 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1032 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001033 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001034 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001035 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001036 optarg, EXT2_MIN_BLOCK_SIZE,
1037 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001038 exit(1);
1039 }
1040 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001041 case 'J':
1042 parse_journal_opts(optarg);
1043 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001044 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001045 if (!journal_size)
1046 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001047 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001048 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001049 bad_blocks_filename = malloc(strlen(optarg)+1);
1050 if (!bad_blocks_filename) {
1051 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001052 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001053 exit(1);
1054 }
1055 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001056 break;
1057 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001058 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001059 if (reserved_ratio > 50 || *tmp) {
1060 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001061 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001062 optarg);
1063 exit(1);
1064 }
1065 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001066 case 'n':
1067 noaction++;
1068 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001069 case 'o':
1070 creator_os = optarg;
1071 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001072 case 'q':
1073 quiet = 1;
1074 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001075 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001076 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001077 if (*tmp) {
1078 com_err(program_name, 0,
1079 _("bad revision level - %s"), optarg);
1080 exit(1);
1081 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001082 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001083 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001084 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001085 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001086 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001087 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001088 inode_size = strtoul(optarg, &tmp, 0);
1089 if (*tmp) {
1090 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001091 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001092 exit(1);
1093 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001094 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001095 case 'v':
1096 verbose = 1;
1097 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001098 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001099 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001100 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001101 case 'L':
1102 volume_label = optarg;
1103 break;
1104 case 'M':
1105 mount_dir = optarg;
1106 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001107 case 'N':
1108 num_inodes = strtoul(optarg, &tmp, 0);
1109 if (*tmp) {
1110 com_err(program_name, 0,
1111 _("bad num inodes - %s"), optarg);
1112 exit(1);
1113 }
1114 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001115 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001116 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001117 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001118 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001119 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001120 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001121 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001122 case 'S':
1123 super_only = 1;
1124 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001125 case 'T':
1126 fs_type = optarg;
1127 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001128 case 'V':
1129 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001130 show_version_only++;
1131 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001132 default:
1133 usage();
1134 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001135 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001136 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001137 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001138 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001139
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001140 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001141 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1142 E2FSPROGS_DATE);
1143
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001144 if (show_version_only) {
1145 fprintf(stderr, _("\tUsing %s\n"),
1146 error_message(EXT2_ET_BASE));
1147 exit(0);
1148 }
1149
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001150 /*
1151 * If there's no blocksize specified and there is a journal
1152 * device, use it to figure out the blocksize
1153 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001154 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001155 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001156 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001157
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001158#ifdef CONFIG_TESTIO_DEBUG
1159 io_ptr = test_io_manager;
1160 test_io_backing_manager = unix_io_manager;
1161#else
1162 io_ptr = unix_io_manager;
1163#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001164 retval = ext2fs_open(journal_device,
1165 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001166 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001167 if (retval) {
1168 com_err(program_name, retval,
1169 _("while trying to open journal device %s\n"),
1170 journal_device);
1171 exit(1);
1172 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001173 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001174 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001175 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001176 "minimum blocksize %d\n"), jfs->blocksize,
1177 -blocksize);
1178 exit(1);
1179 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001180 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001181 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001182 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1183 ext2fs_close(jfs);
1184 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001185
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001186 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001187 if (!force) {
1188 com_err(program_name, 0,
1189 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001190 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001191 proceed_question();
1192 }
1193 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1194 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001195 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001196 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001197 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001198 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1199 fs_param.s_log_block_size);
1200 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001201 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001202 argv[optind - 1]);
1203 exit(1);
1204 }
1205 }
1206 if (optind < argc)
1207 usage();
1208
Theodore Ts'o74becf31997-04-26 14:37:06 +00001209 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001210 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001211 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001212
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001213 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001214
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001215 if (noaction && fs_param.s_blocks_count) {
1216 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001217 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001218 } else {
1219 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001220 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001221 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001222 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001223 if ((retval == EFBIG) &&
1224 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001225 (fs_param.s_log_block_size == 0)) {
1226 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001227 blocksize = 4096;
1228 goto retry;
1229 }
1230 }
1231
Theodore Ts'oa789d841998-03-30 01:20:55 +00001232 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1233 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001234 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001235 exit(1);
1236 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001237 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001238 if (retval == EXT2_ET_UNIMPLEMENTED) {
1239 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001240 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001241 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001242 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001243 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001244 } else {
1245 if (dev_size == 0) {
1246 com_err(program_name, 0,
1247 _("Device size reported to be zero. "
1248 "Invalid partition specified, or\n\t"
1249 "partition table wasn't reread "
1250 "after running fdisk, due to\n\t"
1251 "a modified partition being busy "
1252 "and in use. You may need to reboot\n\t"
1253 "to re-read your partition table.\n"
1254 ));
1255 exit(1);
1256 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001257 fs_param.s_blocks_count = dev_size;
1258 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1259 fs_param.s_blocks_count &= ~((sys_page_size /
1260 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001261 }
1262
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001263 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001264 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001265 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001266 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001267 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001268
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001269 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001270 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001271 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1272
1273 if (megs <= 3)
1274 fs_type = "floppy";
1275 else if (megs <= 512)
1276 fs_type = "small";
1277 else
1278 fs_type = "default";
1279 }
1280
1281 /* Figure out what features should be enabled */
1282
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001283 tmp = tmp2 = NULL;
1284 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1285 profile_get_string(profile, "defaults", "base_features", 0,
1286 "filetype,sparse_super", &tmp);
1287 profile_get_string(profile, "fs_types", fs_type,
1288 "base_features", tmp, &tmp2);
1289 edit_feature(tmp2, &fs_param.s_feature_compat);
1290 free(tmp);
1291 free(tmp2);
1292
1293 tmp = tmp2 = NULL;
1294 profile_get_string(profile, "defaults", "default_features", 0,
1295 "", &tmp);
1296 profile_get_string(profile, "fs_types", fs_type,
1297 "default_features", tmp, &tmp2);
1298 }
1299 edit_feature(fs_features ? fs_features : tmp2,
1300 &fs_param.s_feature_compat);
1301 if (tmp)
1302 free(tmp);
1303 if (tmp2)
1304 free(tmp2);
1305
1306 if (r_opt == EXT2_GOOD_OLD_REV &&
1307 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1308 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001309 fprintf(stderr, _("Filesystem features not supported "
1310 "with revision 0 filesystems\n"));
1311 exit(1);
1312 }
1313
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001314 if (s_opt > 0) {
1315 if (r_opt == EXT2_GOOD_OLD_REV) {
1316 fprintf(stderr, _("Sparse superblocks not supported "
1317 "with revision 0 filesystems\n"));
1318 exit(1);
1319 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001320 fs_param.s_feature_ro_compat |=
1321 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001322 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001323 fs_param.s_feature_ro_compat &=
1324 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1325
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001326 if (journal_size != 0) {
1327 if (r_opt == EXT2_GOOD_OLD_REV) {
1328 fprintf(stderr, _("Journals not supported "
1329 "with revision 0 filesystems\n"));
1330 exit(1);
1331 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001332 fs_param.s_feature_compat |=
1333 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001334 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001335
1336 if (fs_param.s_feature_incompat &
1337 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1338 if (!fs_type)
1339 fs_type = "journal";
1340 reserved_ratio = 0;
1341 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1342 fs_param.s_feature_compat = 0;
1343 fs_param.s_feature_ro_compat = 0;
1344 }
1345
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001346 /* Set first meta blockgroup via an environment variable */
1347 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001348 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001349 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001350 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001351
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001352 /* Get the hardware sector size, if available */
1353 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1354 if (retval) {
1355 com_err(program_name, retval,
1356 _("while trying to determine hardware sector size"));
1357 exit(1);
1358 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001359
Theodore Ts'o54434922003-12-07 01:28:50 -05001360 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001361 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001362
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001363 if (blocksize <= 0) {
1364 profile_get_integer(profile, "defaults", "blocksize", 0,
1365 1024, &use_bsize);
1366 profile_get_integer(profile, "fs_types", fs_type,
1367 "blocksize", use_bsize, &use_bsize);
1368
1369 if (use_bsize == -1) {
1370 use_bsize = sys_page_size;
1371 if ((linux_version_code < (2*65536 + 6*256)) &&
1372 (use_bsize > 4096))
1373 use_bsize = 4096;
1374 }
1375 if (sector_size && use_bsize < sector_size)
1376 use_bsize = sector_size;
1377 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1378 use_bsize = -blocksize;
1379 blocksize = use_bsize;
1380 fs_param.s_blocks_count /= blocksize / 1024;
1381 }
1382
1383 if (inode_ratio == 0) {
1384 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1385 8192, &inode_ratio);
1386 profile_get_integer(profile, "fs_types", fs_type,
1387 "inode_ratio", inode_ratio,
1388 &inode_ratio);
1389
1390 if (inode_ratio < blocksize)
1391 inode_ratio = blocksize;
1392 }
1393
1394 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1395 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1396
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001397 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001398
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001399 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001400 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001401
1402 /* Since sparse_super is the default, we would only have a problem
1403 * here if it was explicitly disabled.
1404 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001405 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1406 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001407 com_err(program_name, 0,
1408 _("reserved online resize blocks not supported "
1409 "on non-sparse filesystem"));
1410 exit(1);
1411 }
1412
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001413 if (fs_param.s_blocks_per_group) {
1414 if (fs_param.s_blocks_per_group < 256 ||
1415 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001416 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001417 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001418 exit(1);
1419 }
1420 }
1421
Theodore Ts'o642935c2006-11-14 23:38:17 -05001422 if (!force && fs_param.s_blocks_count >= ((unsigned) 1 << 31)) {
Theodore Ts'o675b79c2005-06-30 20:00:41 -04001423 com_err(program_name, 0,
1424 _("Filesystem too large. No more than 2**31-1 blocks\n"
1425 "\t (8TB using a blocksize of 4k) are currently supported."));
1426 exit(1);
1427 }
1428
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001429 if ((blocksize > 4096) &&
1430 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1431 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1432 "blocksizes greater than 4096\n\tusing ext3. "
1433 "Use -b 4096 if this is an issue for you.\n\n"));
1434
Andreas Dilger067911a2006-07-15 22:08:20 -04001435 if (inode_size == 0) {
1436 profile_get_integer(profile, "defaults", "inode_size", NULL,
1437 0, &inode_size);
1438 profile_get_integer(profile, "fs_types", fs_type,
1439 "inode_size", inode_size,
1440 &inode_size);
1441 }
1442
1443 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001444 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001445 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001446 inode_size & (inode_size - 1)) {
1447 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001448 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001449 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001450 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001451 exit(1);
1452 }
1453 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1454 fprintf(stderr, _("Warning: %d-byte inodes not usable "
Andreas Dilger067911a2006-07-15 22:08:20 -04001455 "on older systems\n"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001456 inode_size);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001457 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001458 }
1459
Eric Sandeenf3358642006-09-12 14:56:17 -04001460 /* Make sure number of inodes specified will fit in 32 bits */
1461 if (num_inodes == 0) {
1462 __u64 n;
1463 n = (__u64) fs_param.s_blocks_count * blocksize / inode_ratio;
1464 if (n > ~0U) {
1465 com_err(program_name, 0,
1466 _("too many inodes (%llu), raise inode ratio?"), n);
1467 exit(1);
1468 }
1469 } else if (num_inodes > ~0U) {
1470 com_err(program_name, 0,
1471 _("too many inodes (%llu), specify < 2^32 inodes"),
1472 (__u64)num_inodes);
1473 exit(1);
1474 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001475 /*
1476 * Calculate number of inodes based on the inode ratio
1477 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001478 fs_param.s_inodes_count = num_inodes ? num_inodes :
1479 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001480 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001481
1482 /*
1483 * Calculate number of blocks to reserve
1484 */
Theodore Ts'oa8862d92006-08-30 03:08:13 -04001485 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1486 fs_param.s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001487}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001488
Theodore Ts'o3839e651997-04-26 13:21:57 +00001489int main (int argc, char *argv[])
1490{
1491 errcode_t retval = 0;
1492 ext2_filsys fs;
1493 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001494 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001495 unsigned int i;
1496 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001497 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001498
1499#ifdef ENABLE_NLS
1500 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001501 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001502 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1503 textdomain(NLS_CAT_NAME);
1504#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001505 PRS(argc, argv);
1506
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001507#ifdef CONFIG_TESTIO_DEBUG
1508 io_ptr = test_io_manager;
1509 test_io_backing_manager = unix_io_manager;
1510#else
1511 io_ptr = unix_io_manager;
1512#endif
1513
Theodore Ts'o3839e651997-04-26 13:21:57 +00001514 /*
1515 * Initialize the superblock....
1516 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001517 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001518 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001519 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001520 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001521 exit(1);
1522 }
1523
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001524 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001525 * Wipe out the old on-disk superblock
1526 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001527 if (!noaction)
1528 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001529
1530 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001531 * Generate a UUID for it...
1532 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001533 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001534
1535 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001536 * Initialize the directory index variables
1537 */
1538 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1539 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1540
1541 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001542 * Add "jitter" to the superblock's check interval so that we
1543 * don't check all the filesystems at the same time. We use a
1544 * kludgy hack of using the UUID to derive a random jitter value.
1545 */
1546 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1547 val += fs->super->s_uuid[i];
1548 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1549
1550 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001551 * Override the creator OS, if applicable
1552 */
1553 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001554 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001555 exit(1);
1556 }
1557
1558 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001559 * For the Hurd, we will turn off filetype since it doesn't
1560 * support it.
1561 */
1562 if (fs->super->s_creator_os == EXT2_OS_HURD)
1563 fs->super->s_feature_incompat &=
1564 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1565
1566 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001567 * Set the volume label...
1568 */
1569 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001570 memset(fs->super->s_volume_name, 0,
1571 sizeof(fs->super->s_volume_name));
1572 strncpy(fs->super->s_volume_name, volume_label,
1573 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001574 }
1575
1576 /*
1577 * Set the last mount directory
1578 */
1579 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001580 memset(fs->super->s_last_mounted, 0,
1581 sizeof(fs->super->s_last_mounted));
1582 strncpy(fs->super->s_last_mounted, mount_dir,
1583 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001584 }
1585
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001586 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001587 show_stats(fs);
1588
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001589 if (noaction)
1590 exit(0);
1591
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001592 if (fs->super->s_feature_incompat &
1593 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1594 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001595 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001596 }
1597
Theodore Ts'o3839e651997-04-26 13:21:57 +00001598 if (bad_blocks_filename)
1599 read_bb_file(fs, &bb_list, bad_blocks_filename);
1600 if (cflag)
1601 test_disk(fs, &bb_list);
1602
1603 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001604 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001605 retval = ext2fs_allocate_tables(fs);
1606 if (retval) {
1607 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001608 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001609 exit(1);
1610 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001611 if (super_only) {
1612 fs->super->s_state |= EXT2_ERROR_FS;
1613 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1614 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001615 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001616 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001617 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001618 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001619 blk_t ret_blk;
1620
1621#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001622 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001623#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001624
1625 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001626 * Wipe out any old MD RAID (or other) metadata at the end
1627 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001628 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001629 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001630 start = (blocks & ~(rsv - 1));
1631 if (start > rsv)
1632 start -= rsv;
1633 if (start > 0)
1634 retval = zero_blocks(fs, start, blocks - start,
1635 NULL, &ret_blk, NULL);
1636
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001637 if (retval) {
1638 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001639 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001640 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001641 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -04001642 setup_lazy_bg(fs);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001643 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001644 create_root_dir(fs);
1645 create_lost_and_found(fs);
1646 reserve_inodes(fs);
1647 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001648 if (fs->super->s_feature_compat &
1649 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1650 retval = ext2fs_create_resize_inode(fs);
1651 if (retval) {
1652 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001653 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001654 exit(1);
1655 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001656 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001657 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001658
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001659 if (journal_device) {
1660 ext2_filsys jfs;
1661
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001662 if (!force)
1663 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001664 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001665
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001666 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1667 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1668 fs->blocksize, unix_io_manager, &jfs);
1669 if (retval) {
1670 com_err(program_name, retval,
1671 _("while trying to open journal device %s\n"),
1672 journal_device);
1673 exit(1);
1674 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001675 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001676 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001677 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001678 fflush(stdout);
1679 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001680 retval = ext2fs_add_journal_device(fs, jfs);
1681 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001682 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001683 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001684 journal_device);
1685 exit(1);
1686 }
1687 if (!quiet)
1688 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001689 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001690 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001691 } else if ((journal_size) ||
1692 (fs_param.s_feature_compat &
1693 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001694 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001695
1696 if (!journal_blocks) {
1697 fs->super->s_feature_compat &=
1698 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1699 goto no_journal;
1700 }
1701 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001702 printf(_("Creating journal (%d blocks): "),
1703 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001704 fflush(stdout);
1705 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001706 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1707 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001708 if (retval) {
1709 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001710 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001711 exit(1);
1712 }
1713 if (!quiet)
1714 printf(_("done\n"));
1715 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001716no_journal:
1717
Theodore Ts'o3839e651997-04-26 13:21:57 +00001718 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001719 printf(_("Writing superblocks and "
1720 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001721 retval = ext2fs_flush(fs);
1722 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001723 fprintf(stderr,
1724 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001725 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001726 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001727 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001728 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1729 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001730 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001731 val = ext2fs_close(fs);
1732 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001733}