blob: 2e72dcab6e29e4202ca5d738c617e568cd800f76 [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'oa6d83022006-12-26 03:38:07 -050055#include "prof_err.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000057#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000058
59#define STRIDE_LENGTH 8
60
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000061#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000062#define ZAP_BOOTBLOCK
63#endif
64
Theodore Ts'o3839e651997-04-26 13:21:57 +000065extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000066extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000067
68const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000069const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000070
71/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000072int cflag;
73int verbose;
74int quiet;
75int super_only;
76int force;
77int noaction;
78int journal_size;
79int journal_flags;
80char *bad_blocks_filename;
81__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050083struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000084char *creator_os;
85char *volume_label;
86char *mount_dir;
87char *journal_device;
88int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000089
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050090profile_t profile;
91
Theodore Ts'o31e29a12002-05-17 10:53:07 -040092int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040093int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040094
Theodore Ts'o63985322001-01-03 17:02:13 +000095static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000096{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000097 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Andreas Dilger067911a2006-07-15 22:08:20 -040098 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
99 "[-j] [-J journal-options]\n"
100 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
101 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
102 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
103 "[-r fs-revision] [-R options] [-qvSV]\n\tdevice [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104 program_name);
105 exit(1);
106}
107
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000108static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109{
110 int l = 0;
111
112 arg >>= 1;
113 while (arg) {
114 l++;
115 arg >>= 1;
116 }
117 return l;
118}
119
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000120static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000121{
122 int l;
123
124 for (l=0; arg ; l++)
125 arg = arg / 10;
126 return l;
127}
128
Theodore Ts'od99225e2004-09-25 07:40:12 -0400129static int parse_version_number(const char *s)
130{
131 int major, minor, rev;
132 char *endptr;
133 const char *cp = s;
134
135 if (!s)
136 return 0;
137 major = strtol(cp, &endptr, 10);
138 if (cp == endptr || *endptr != '.')
139 return 0;
140 cp = endptr + 1;
141 minor = strtol(cp, &endptr, 10);
142 if (cp == endptr || *endptr != '.')
143 return 0;
144 cp = endptr + 1;
145 rev = strtol(cp, &endptr, 10);
146 if (cp == endptr)
147 return 0;
148 return ((((major * 256) + minor) * 256) + rev);
149}
150
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000151/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152 * Helper function for read_bb_file and test_disk
153 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500154static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155{
Theodore Ts'o66938372002-03-08 00:14:46 -0500156 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 return;
158}
159
160/*
161 * Reads the bad blocks list from a file
162 */
163static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
164 const char *bad_blocks_file)
165{
166 FILE *f;
167 errcode_t retval;
168
169 f = fopen(bad_blocks_file, "r");
170 if (!f) {
171 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000172 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173 exit(1);
174 }
175 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
176 fclose (f);
177 if (retval) {
178 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000179 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 exit(1);
181 }
182}
183
184/*
185 * Runs the badblocks program to test the disk
186 */
187static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
188{
189 FILE *f;
190 errcode_t retval;
191 char buf[1024];
192
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400193 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500194 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Theodore Ts'o8ade4792006-11-08 00:41:50 -0500195 fs->device_name, fs->super->s_blocks_count-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000197 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198 f = popen(buf, "r");
199 if (!f) {
200 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400201 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 exit(1);
203 }
204 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000205 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206 if (retval) {
207 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000208 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 exit(1);
210 }
211}
212
213static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
214{
Theodore Ts'o54434922003-12-07 01:28:50 -0500215 dgrp_t i;
216 blk_t j;
217 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 blk_t blk;
219 badblocks_iterate bb_iter;
220 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000221 blk_t group_block;
222 int group;
223 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224
225 if (!bb_list)
226 return;
227
228 /*
229 * The primary superblock and group descriptors *must* be
230 * good; if not, abort.
231 */
232 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
233 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000234 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000235 fprintf(stderr, _("Block %d in primary "
236 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400237 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000238 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500240 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 exit(1);
242 }
243 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000244
245 /*
246 * See if any of the bad blocks are showing up in the backup
247 * superblocks and/or group descriptors. If so, issue a
248 * warning and adjust the block counts appropriately.
249 */
250 group_block = fs->super->s_first_data_block +
251 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000252
253 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000254 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000255 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000256 if (ext2fs_badblocks_list_test(bb_list,
257 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000258 if (!group_bad)
259 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500260_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000261" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000262 group_block);
263 group_bad++;
264 group = ext2fs_group_of_blk(fs, group_block+j);
265 fs->group_desc[group].bg_free_blocks_count++;
266 fs->super->s_free_blocks_count++;
267 }
268 }
269 group_block += fs->super->s_blocks_per_group;
270 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000271
272 /*
273 * Mark all the bad blocks as used...
274 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000275 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000277 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000278 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 exit(1);
280 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000281 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000282 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000283 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284}
285
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000286/*
287 * These functions implement a generalized progress meter.
288 */
289struct progress_struct {
290 char format[20];
291 char backup[80];
292 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400293 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000294};
295
296static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500297 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000299 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000300
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000301 memset(progress, 0, sizeof(struct progress_struct));
302 if (quiet)
303 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000304
305 /*
306 * Figure out how many digits we need
307 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000308 i = int_log10(max);
309 sprintf(progress->format, "%%%dd/%%%dld", i, i);
310 memset(progress->backup, '\b', sizeof(progress->backup)-1);
311 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500312 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000313 progress->backup[(2*i)+1] = 0;
314 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000315
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400316 progress->skip_progress = 0;
317 if (getenv("MKE2FS_SKIP_PROGRESS"))
318 progress->skip_progress++;
319
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000320 fputs(label, stdout);
321 fflush(stdout);
322}
323
324static void progress_update(struct progress_struct *progress, __u32 val)
325{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400326 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000327 return;
328 printf(progress->format, val, progress->max);
329 fputs(progress->backup, stdout);
330}
331
332static void progress_close(struct progress_struct *progress)
333{
334 if (progress->format[0] == 0)
335 return;
336 fputs(_("done \n"), stdout);
337}
338
339
340/*
341 * Helper function which zeros out _num_ blocks starting at _blk_. In
342 * case of an error, the details of the error is returned via _ret_blk_
343 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
344 * success, and an error code on an error.
345 *
346 * As a special case, if the first argument is NULL, then it will
347 * attempt to free the static zeroizing buffer. (This is to keep
348 * programs that check for memory leaks happy.)
349 */
350static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
351 struct progress_struct *progress,
352 blk_t *ret_blk, int *ret_count)
353{
354 int j, count, next_update, next_update_incr;
355 static char *buf;
356 errcode_t retval;
357
358 /* If fs is null, clean up the static buffer and return */
359 if (!fs) {
360 if (buf) {
361 free(buf);
362 buf = 0;
363 }
364 return 0;
365 }
366 /* Allocate the zeroizing buffer if necessary */
367 if (!buf) {
368 buf = malloc(fs->blocksize * STRIDE_LENGTH);
369 if (!buf) {
370 com_err("malloc", ENOMEM,
371 _("while allocating zeroizing buffer"));
372 exit(1);
373 }
374 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
375 }
376 /* OK, do the write loop */
377 next_update = 0;
378 next_update_incr = num / 100;
379 if (next_update_incr < 1)
380 next_update_incr = 1;
381 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400382 count = num - j;
383 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000384 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000385 retval = io_channel_write_blk(fs->io, blk, count, buf);
386 if (retval) {
387 if (ret_count)
388 *ret_count = count;
389 if (ret_blk)
390 *ret_blk = blk;
391 return retval;
392 }
393 if (progress && j > next_update) {
394 next_update += num / 100;
395 progress_update(progress, blk);
396 }
397 }
398 return 0;
399}
400
401static void write_inode_tables(ext2_filsys fs)
402{
403 errcode_t retval;
404 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500405 dgrp_t i;
406 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000407 struct progress_struct progress;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400408 int lazy_flag = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000409
410 if (quiet)
411 memset(&progress, 0, sizeof(progress));
412 else
413 progress_init(&progress, _("Writing inode tables: "),
414 fs->group_desc_count);
415
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400416 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
417 EXT2_FEATURE_COMPAT_LAZY_BG))
418 lazy_flag = 1;
419
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000421 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000423 blk = fs->group_desc[i].bg_inode_table;
424 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000425
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400426 if (!(lazy_flag &&
427 (fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT))) {
428 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
429 if (retval) {
430 fprintf(stderr, _("\nCould not write %d "
431 "blocks in inode table starting at %u: %s\n"),
432 num, blk, error_message(retval));
433 exit(1);
434 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000435 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000436 if (sync_kludge) {
437 if (sync_kludge == 1)
438 sync();
439 else if ((i % sync_kludge) == 0)
440 sync();
441 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000443 zero_blocks(0, 0, 0, 0, 0, 0);
444 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445}
446
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400447static void setup_lazy_bg(ext2_filsys fs)
448{
449 dgrp_t i;
450 int blks;
451 struct ext2_super_block *sb = fs->super;
452 struct ext2_group_desc *bg = fs->group_desc;
453
454 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
455 EXT2_FEATURE_COMPAT_LAZY_BG)) {
456 for (i = 0; i < fs->group_desc_count; i++, bg++) {
457 if ((i == 0) ||
458 (i == fs->group_desc_count-1))
459 continue;
460 if (bg->bg_free_inodes_count ==
461 sb->s_inodes_per_group) {
462 bg->bg_free_inodes_count = 0;
463 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
464 sb->s_free_inodes_count -=
465 sb->s_inodes_per_group;
466 }
467 blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
468 if (bg->bg_free_blocks_count == blks) {
469 bg->bg_free_blocks_count = 0;
470 bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
471 sb->s_free_blocks_count -= blks;
472 }
473 }
474 }
475}
476
477
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478static void create_root_dir(ext2_filsys fs)
479{
480 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000481 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482
483 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
484 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000485 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000486 exit(1);
487 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000488 if (geteuid()) {
489 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
490 if (retval) {
491 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000492 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000493 exit(1);
494 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000495 inode.i_uid = getuid();
496 if (inode.i_uid)
497 inode.i_gid = getgid();
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500498 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000499 if (retval) {
500 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000501 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000502 exit(1);
503 }
504 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505}
506
507static void create_lost_and_found(ext2_filsys fs)
508{
509 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000510 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 const char *name = "lost+found";
512 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000513 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514
Theodore Ts'o6a525062001-12-24 09:40:00 -0500515 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
517 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000518 com_err("ext2fs_mkdir", retval,
519 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000520 exit(1);
521 }
522
523 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
524 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000525 com_err("ext2_lookup", retval,
526 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 exit(1);
528 }
529
530 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000531 if ((lpf_size += fs->blocksize) >= 16*1024)
532 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533 retval = ext2fs_expand_dir(fs, ino);
534 if (retval) {
535 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000536 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537 exit(1);
538 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500539 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540}
541
542static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
543{
544 errcode_t retval;
545
Theodore Ts'of3db3561997-04-26 13:34:30 +0000546 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547 fs->group_desc[0].bg_free_inodes_count--;
548 fs->super->s_free_inodes_count--;
549 retval = ext2fs_update_bb_inode(fs, bb_list);
550 if (retval) {
551 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000552 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 exit(1);
554 }
555
556}
557
558static void reserve_inodes(ext2_filsys fs)
559{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000560 ext2_ino_t i;
561 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000563 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000564 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000565 group = ext2fs_group_of_ino(fs, i);
566 fs->group_desc[group].bg_free_inodes_count--;
567 fs->super->s_free_inodes_count--;
568 }
569 ext2fs_mark_ib_dirty(fs);
570}
571
Theodore Ts'o756df352002-03-07 20:52:12 -0500572#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500573#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500574#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500575
Theodore Ts'o04a96852001-08-30 21:55:26 -0400576static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000577{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400578 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000579 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500580 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000581
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400582 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600583 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500584 printf(_("Out of memory erasing sectors %d-%d\n"),
585 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600586 exit(1);
587 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500588
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500589 if (sect == 0) {
590 /* Check for a BSD disklabel, and don't erase it if so */
591 retval = io_channel_read_blk(fs->io, 0, -512, buf);
592 if (retval)
593 fprintf(stderr,
594 _("Warning: could not read block 0: %s\n"),
595 error_message(retval));
596 else {
597 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
598 if ((*magic == BSD_DISKMAGIC) ||
599 (*magic == BSD_MAGICDISK))
600 return;
601 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500602 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500603
Theodore Ts'o70988102002-07-14 08:00:00 -0400604 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000605 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400606 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000607 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400608 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000609 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500610 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
611 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000612}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000613
614static void create_journal_dev(ext2_filsys fs)
615{
616 struct progress_struct progress;
617 errcode_t retval;
618 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000619 blk_t blk;
620 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000621
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000622 retval = ext2fs_create_journal_superblock(fs,
623 fs->super->s_blocks_count, 0, &buf);
624 if (retval) {
625 com_err("create_journal_dev", retval,
626 _("while initializing journal superblock"));
627 exit(1);
628 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000629 if (quiet)
630 memset(&progress, 0, sizeof(progress));
631 else
632 progress_init(&progress, _("Zeroing journal device: "),
633 fs->super->s_blocks_count);
634
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000635 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
636 &progress, &blk, &count);
637 if (retval) {
638 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000639 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000640 blk, count);
641 exit(1);
642 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000643 zero_blocks(0, 0, 0, 0, 0, 0);
644
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000645 retval = io_channel_write_blk(fs->io,
646 fs->super->s_first_data_block+1,
647 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000648 if (retval) {
649 com_err("create_journal_dev", retval,
650 _("while writing journal superblock"));
651 exit(1);
652 }
653 progress_close(&progress);
654}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000655
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656static void show_stats(ext2_filsys fs)
657{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000658 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000659 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500660 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500662 dgrp_t i;
663 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500665 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500666 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500667 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000668
669 memset(buf, 0, sizeof(buf));
670 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000671 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500672 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500673 os = e2p_os2string(fs->super->s_creator_os);
674 fputs(os, stdout);
675 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000676 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000677 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000678 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000679 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000680 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000681 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000682 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000683 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684 s->s_r_blocks_count,
685 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000686 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500687 if (s->s_reserved_gdt_blocks)
688 printf(_("Maximum filesystem blocks=%lu\n"),
689 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
690 (fs->blocksize / sizeof(struct ext2_group_desc)) *
691 s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000692 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000693 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000694 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000695 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000696 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000697 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000698 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000699
700 if (fs->group_desc_count == 1) {
701 printf("\n");
702 return;
703 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500704
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000705 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000706 group_block = s->s_first_data_block;
707 col_left = 0;
708 for (i = 1; i < fs->group_desc_count; i++) {
709 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000710 if (!ext2fs_bg_has_super(fs, i))
711 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000712 if (i != 1)
713 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000714 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000715 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000716 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000717 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000718 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000719 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000720 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000721 }
722 printf("\n\n");
723}
724
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000725/*
726 * Set the S_CREATOR_OS field. Return true if OS is known,
727 * otherwise, 0.
728 */
729static int set_os(struct ext2_super_block *sb, char *os)
730{
731 if (isdigit (*os))
732 sb->s_creator_os = atoi (os);
733 else if (strcasecmp(os, "linux") == 0)
734 sb->s_creator_os = EXT2_OS_LINUX;
735 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
736 sb->s_creator_os = EXT2_OS_HURD;
737 else if (strcasecmp(os, "masix") == 0)
738 sb->s_creator_os = EXT2_OS_MASIX;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500739 else if (strcasecmp(os, "freebsd") == 0)
740 sb->s_creator_os = EXT2_OS_FREEBSD;
741 else if (strcasecmp(os, "lites") == 0)
742 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000743 else
744 return 0;
745 return 1;
746}
747
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000748#define PATH_SET "PATH=/sbin"
749
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500750static void parse_extended_opts(struct ext2_super_block *param,
751 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000752{
753 char *buf, *token, *next, *p, *arg;
754 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500755 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000756
757 len = strlen(opts);
758 buf = malloc(len+1);
759 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500760 fprintf(stderr,
761 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000762 exit(1);
763 }
764 strcpy(buf, opts);
765 for (token = buf; token && *token; token = next) {
766 p = strchr(token, ',');
767 next = 0;
768 if (p) {
769 *p = 0;
770 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500771 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000772 arg = strchr(token, '=');
773 if (arg) {
774 *arg = 0;
775 arg++;
776 }
777 if (strcmp(token, "stride") == 0) {
778 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500779 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000780 continue;
781 }
782 fs_stride = strtoul(arg, &p, 0);
783 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000784 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400785 _("Invalid stride parameter: %s\n"),
786 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500787 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000788 continue;
789 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500790 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500791 unsigned long resize, bpg, rsv_groups;
792 unsigned long group_desc_count, desc_blocks;
793 unsigned int gdpb, blocksize;
794 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500795
796 if (!arg) {
797 r_usage++;
798 continue;
799 }
800
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500801 resize = parse_num_blocks(arg,
802 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500803
804 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500805 fprintf(stderr,
806 _("Invalid resize parameter: %s\n"),
807 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500808 r_usage++;
809 continue;
810 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500811 if (resize <= param->s_blocks_count) {
812 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400813 _("The resize maximum must be greater "
814 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500815 r_usage++;
816 continue;
817 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500818
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500819 blocksize = EXT2_BLOCK_SIZE(param);
820 bpg = param->s_blocks_per_group;
821 if (!bpg)
822 bpg = blocksize * 8;
823 gdpb = blocksize / sizeof(struct ext2_group_desc);
Theodore Ts'o69022e02006-08-30 01:57:00 -0400824 group_desc_count =
825 ext2fs_div_ceil(param->s_blocks_count, bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500826 desc_blocks = (group_desc_count +
827 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400828 rsv_groups = ext2fs_div_ceil(resize, bpg);
829 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500830 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500831 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500832 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
833
834 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400835 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
836 fprintf(stderr,
837 _("On-line resizing not supported with revision 0 filesystems\n"));
838 exit(1);
839 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500840 param->s_feature_compat |=
841 EXT2_FEATURE_COMPAT_RESIZE_INODE;
842
843 param->s_reserved_gdt_blocks = rsv_gdb;
844 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000845 } else
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500846 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000847 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500848 if (r_usage) {
849 fprintf(stderr, _("\nBad options specified.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400850 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000851 "and may take an argument which\n"
852 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400853 "Valid extended options are:\n"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500854 "\tstride=<stride length in blocks>\n"
855 "\tresize=<resize maximum size in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000856 exit(1);
857 }
858}
859
Theodore Ts'o896938d1999-10-23 01:04:50 +0000860static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400861 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500862 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400863 EXT2_FEATURE_COMPAT_DIR_INDEX |
864 EXT2_FEATURE_COMPAT_LAZY_BG, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000865 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400866 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
867 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000868 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
869};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000870
871
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500872static void syntax_err_report(const char *filename, long err, int line_num)
873{
874 fprintf(stderr,
875 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
876 filename, line_num, error_message(err));
877 exit(1);
878}
879
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200880static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500881
882static void edit_feature(const char *str, __u32 *compat_array)
883{
884 if (!str)
885 return;
886
887 if (e2p_edit_feature(str, compat_array, ok_features)) {
888 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
889 str);
890 exit(1);
891 }
892}
893
Theodore Ts'o3839e651997-04-26 13:21:57 +0000894static void PRS(int argc, char *argv[])
895{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400896 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000897 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500898 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000899 int blocksize = 0;
900 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600901 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -0500902 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400903 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500904 int show_version_only = 0;
Eric Sandeenf3358642006-09-12 14:56:17 -0400905 __u64 num_inodes = 0; /* u64 to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000906 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000907 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500908 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500909 const char * fs_type = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000910 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500911#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000912 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000913#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400914 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500915 int s_opt = -1, r_opt = -1;
916 char *fs_features = 0;
917 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500918 char *newpath;
919 int pathlen = sizeof(PATH_SET) + 1;
920
921 if (oldpath)
922 pathlen += strlen(oldpath);
923 newpath = malloc(pathlen);
924 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000925
Theodore Ts'o3839e651997-04-26 13:21:57 +0000926 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000927 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000928 strcat (newpath, ":");
929 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -0500930 }
931 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000932
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000933 tmp = getenv("MKE2FS_SYNC");
934 if (tmp)
935 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400936
937 /* Determine the system page size if possible */
938#ifdef HAVE_SYSCONF
939#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
940#define _SC_PAGESIZE _SC_PAGE_SIZE
941#endif
942#ifdef _SC_PAGESIZE
943 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600944 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400945 sys_page_size = sysval;
946#endif /* _SC_PAGESIZE */
947#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500948
949 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
950 config_fn[0] = tmp;
951 profile_set_syntax_err_cb(syntax_err_report);
952 profile_init(config_fn, &profile);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000953
Theodore Ts'o3839e651997-04-26 13:21:57 +0000954 setbuf(stdout, NULL);
955 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500956 add_error_table(&et_ext2_error_table);
957 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500958 memset(&fs_param, 0, sizeof(struct ext2_super_block));
959 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400960
Theodore Ts'o756df352002-03-07 20:52:12 -0500961#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000962 if (uname(&ut)) {
963 perror("uname");
964 exit(1);
965 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400966 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500967 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500968 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000969#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700970
971 if (argc && *argv) {
972 program_name = get_progname(*argv);
973
974 /* If called as mkfs.ext3, create a journal inode */
975 if (!strcmp(program_name, "mkfs.ext3"))
976 journal_size = -1;
977 }
978
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000979 while ((c = getopt (argc, argv,
Andreas Dilger25247852005-07-06 12:58:15 -0500980 "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 +0000981 switch (c) {
982 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400983 blocksize = strtol(optarg, &tmp, 0);
984 b = (blocksize > 0) ? blocksize : -blocksize;
985 if (b < EXT2_MIN_BLOCK_SIZE ||
986 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000987 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400988 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000989 exit(1);
990 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600991 if (blocksize > 4096)
992 fprintf(stderr, _("Warning: blocksize %d not "
993 "usable on most systems.\n"),
994 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400995 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500996 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400997 int_log2(blocksize >>
998 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000999 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001000 case 'c': /* Check for bad blocks */
1001 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001002 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001003 break;
1004 case 'f':
1005 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001006 if (size < EXT2_MIN_BLOCK_SIZE ||
1007 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001008 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001009 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001010 optarg);
1011 exit(1);
1012 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001013 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001014 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001015 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001016 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001017 break;
1018 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001019 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001020 if (*tmp) {
1021 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001022 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001023 exit(1);
1024 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001025 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001026 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001027 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001028 exit(1);
1029 }
1030 break;
1031 case 'i':
1032 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001033 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1034 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001035 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001036 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001037 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001038 optarg, EXT2_MIN_BLOCK_SIZE,
1039 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001040 exit(1);
1041 }
1042 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001043 case 'J':
1044 parse_journal_opts(optarg);
1045 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001046 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001047 if (!journal_size)
1048 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001049 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001050 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001051 bad_blocks_filename = malloc(strlen(optarg)+1);
1052 if (!bad_blocks_filename) {
1053 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001054 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001055 exit(1);
1056 }
1057 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001058 break;
1059 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001060 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001061 if (reserved_ratio > 50 || *tmp) {
1062 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001063 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001064 optarg);
1065 exit(1);
1066 }
1067 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001068 case 'n':
1069 noaction++;
1070 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001071 case 'o':
1072 creator_os = optarg;
1073 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001074 case 'q':
1075 quiet = 1;
1076 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001077 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001078 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001079 if (*tmp) {
1080 com_err(program_name, 0,
1081 _("bad revision level - %s"), optarg);
1082 exit(1);
1083 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001084 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001085 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001086 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001087 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001088 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001089 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001090 inode_size = strtoul(optarg, &tmp, 0);
1091 if (*tmp) {
1092 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001093 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001094 exit(1);
1095 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001096 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001097 case 'v':
1098 verbose = 1;
1099 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001100 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001101 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001102 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001103 case 'L':
1104 volume_label = optarg;
1105 break;
1106 case 'M':
1107 mount_dir = optarg;
1108 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001109 case 'N':
1110 num_inodes = strtoul(optarg, &tmp, 0);
1111 if (*tmp) {
1112 com_err(program_name, 0,
1113 _("bad num inodes - %s"), optarg);
1114 exit(1);
1115 }
1116 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001117 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001118 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001119 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001120 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001121 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001122 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001123 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001124 case 'S':
1125 super_only = 1;
1126 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001127 case 'T':
1128 fs_type = optarg;
1129 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001130 case 'V':
1131 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001132 show_version_only++;
1133 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001134 default:
1135 usage();
1136 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001137 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001138 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001139 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001140 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001141
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001142 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001143 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1144 E2FSPROGS_DATE);
1145
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001146 if (show_version_only) {
1147 fprintf(stderr, _("\tUsing %s\n"),
1148 error_message(EXT2_ET_BASE));
1149 exit(0);
1150 }
1151
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001152 /*
1153 * If there's no blocksize specified and there is a journal
1154 * device, use it to figure out the blocksize
1155 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001156 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001157 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001158 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001159
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001160#ifdef CONFIG_TESTIO_DEBUG
1161 io_ptr = test_io_manager;
1162 test_io_backing_manager = unix_io_manager;
1163#else
1164 io_ptr = unix_io_manager;
1165#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001166 retval = ext2fs_open(journal_device,
1167 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001168 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001169 if (retval) {
1170 com_err(program_name, retval,
1171 _("while trying to open journal device %s\n"),
1172 journal_device);
1173 exit(1);
1174 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001175 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001176 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001177 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001178 "minimum blocksize %d\n"), jfs->blocksize,
1179 -blocksize);
1180 exit(1);
1181 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001182 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001183 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001184 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1185 ext2fs_close(jfs);
1186 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001187
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001188 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001189 if (!force) {
1190 com_err(program_name, 0,
1191 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001192 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001193 proceed_question();
1194 }
1195 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1196 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001197 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001198 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001199 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001200 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1201 fs_param.s_log_block_size);
1202 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001203 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001204 argv[optind - 1]);
1205 exit(1);
1206 }
1207 }
1208 if (optind < argc)
1209 usage();
1210
Theodore Ts'o74becf31997-04-26 14:37:06 +00001211 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001212 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001213 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001214
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001215 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001216
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001217 if (noaction && fs_param.s_blocks_count) {
1218 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001219 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001220 } else {
1221 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001222 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001223 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001224 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001225 if ((retval == EFBIG) &&
1226 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001227 (fs_param.s_log_block_size == 0)) {
1228 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001229 blocksize = 4096;
1230 goto retry;
1231 }
1232 }
1233
Theodore Ts'oa789d841998-03-30 01:20:55 +00001234 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1235 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001236 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001237 exit(1);
1238 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001239 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001240 if (retval == EXT2_ET_UNIMPLEMENTED) {
1241 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001242 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001243 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001244 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001245 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001246 } else {
1247 if (dev_size == 0) {
1248 com_err(program_name, 0,
1249 _("Device size reported to be zero. "
1250 "Invalid partition specified, or\n\t"
1251 "partition table wasn't reread "
1252 "after running fdisk, due to\n\t"
1253 "a modified partition being busy "
1254 "and in use. You may need to reboot\n\t"
1255 "to re-read your partition table.\n"
1256 ));
1257 exit(1);
1258 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001259 fs_param.s_blocks_count = dev_size;
1260 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1261 fs_param.s_blocks_count &= ~((sys_page_size /
1262 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001263 }
1264
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001265 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001266 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001267 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001268 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001269 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001270
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001271 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001272 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001273 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1274
1275 if (megs <= 3)
1276 fs_type = "floppy";
1277 else if (megs <= 512)
1278 fs_type = "small";
1279 else
1280 fs_type = "default";
1281 }
1282
1283 /* Figure out what features should be enabled */
1284
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001285 tmp = tmp2 = NULL;
1286 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1287 profile_get_string(profile, "defaults", "base_features", 0,
1288 "filetype,sparse_super", &tmp);
1289 profile_get_string(profile, "fs_types", fs_type,
1290 "base_features", tmp, &tmp2);
1291 edit_feature(tmp2, &fs_param.s_feature_compat);
1292 free(tmp);
1293 free(tmp2);
1294
1295 tmp = tmp2 = NULL;
1296 profile_get_string(profile, "defaults", "default_features", 0,
1297 "", &tmp);
1298 profile_get_string(profile, "fs_types", fs_type,
1299 "default_features", tmp, &tmp2);
1300 }
1301 edit_feature(fs_features ? fs_features : tmp2,
1302 &fs_param.s_feature_compat);
1303 if (tmp)
1304 free(tmp);
1305 if (tmp2)
1306 free(tmp2);
1307
1308 if (r_opt == EXT2_GOOD_OLD_REV &&
1309 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1310 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001311 fprintf(stderr, _("Filesystem features not supported "
1312 "with revision 0 filesystems\n"));
1313 exit(1);
1314 }
1315
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001316 if (s_opt > 0) {
1317 if (r_opt == EXT2_GOOD_OLD_REV) {
1318 fprintf(stderr, _("Sparse superblocks not supported "
1319 "with revision 0 filesystems\n"));
1320 exit(1);
1321 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001322 fs_param.s_feature_ro_compat |=
1323 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001324 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001325 fs_param.s_feature_ro_compat &=
1326 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1327
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001328 if (journal_size != 0) {
1329 if (r_opt == EXT2_GOOD_OLD_REV) {
1330 fprintf(stderr, _("Journals not supported "
1331 "with revision 0 filesystems\n"));
1332 exit(1);
1333 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001334 fs_param.s_feature_compat |=
1335 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001336 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001337
1338 if (fs_param.s_feature_incompat &
1339 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1340 if (!fs_type)
1341 fs_type = "journal";
1342 reserved_ratio = 0;
1343 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1344 fs_param.s_feature_compat = 0;
1345 fs_param.s_feature_ro_compat = 0;
1346 }
1347
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001348 /* Set first meta blockgroup via an environment variable */
1349 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001350 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001351 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001352 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001353
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001354 /* Get the hardware sector size, if available */
1355 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1356 if (retval) {
1357 com_err(program_name, retval,
1358 _("while trying to determine hardware sector size"));
1359 exit(1);
1360 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001361
Theodore Ts'o54434922003-12-07 01:28:50 -05001362 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001363 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001364
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001365 if (blocksize <= 0) {
1366 profile_get_integer(profile, "defaults", "blocksize", 0,
1367 1024, &use_bsize);
1368 profile_get_integer(profile, "fs_types", fs_type,
1369 "blocksize", use_bsize, &use_bsize);
1370
1371 if (use_bsize == -1) {
1372 use_bsize = sys_page_size;
1373 if ((linux_version_code < (2*65536 + 6*256)) &&
1374 (use_bsize > 4096))
1375 use_bsize = 4096;
1376 }
1377 if (sector_size && use_bsize < sector_size)
1378 use_bsize = sector_size;
1379 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1380 use_bsize = -blocksize;
1381 blocksize = use_bsize;
1382 fs_param.s_blocks_count /= blocksize / 1024;
1383 }
1384
1385 if (inode_ratio == 0) {
1386 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1387 8192, &inode_ratio);
1388 profile_get_integer(profile, "fs_types", fs_type,
1389 "inode_ratio", inode_ratio,
1390 &inode_ratio);
1391
1392 if (inode_ratio < blocksize)
1393 inode_ratio = blocksize;
1394 }
1395
1396 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1397 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1398
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001399 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001400
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001401 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001402 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001403
1404 /* Since sparse_super is the default, we would only have a problem
1405 * here if it was explicitly disabled.
1406 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001407 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1408 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001409 com_err(program_name, 0,
1410 _("reserved online resize blocks not supported "
1411 "on non-sparse filesystem"));
1412 exit(1);
1413 }
1414
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001415 if (fs_param.s_blocks_per_group) {
1416 if (fs_param.s_blocks_per_group < 256 ||
1417 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001418 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001419 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001420 exit(1);
1421 }
1422 }
1423
Theodore Ts'o642935c2006-11-14 23:38:17 -05001424 if (!force && fs_param.s_blocks_count >= ((unsigned) 1 << 31)) {
Theodore Ts'o675b79c2005-06-30 20:00:41 -04001425 com_err(program_name, 0,
1426 _("Filesystem too large. No more than 2**31-1 blocks\n"
1427 "\t (8TB using a blocksize of 4k) are currently supported."));
1428 exit(1);
1429 }
1430
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001431 if ((blocksize > 4096) &&
1432 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1433 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1434 "blocksizes greater than 4096\n\tusing ext3. "
1435 "Use -b 4096 if this is an issue for you.\n\n"));
1436
Andreas Dilger067911a2006-07-15 22:08:20 -04001437 if (inode_size == 0) {
1438 profile_get_integer(profile, "defaults", "inode_size", NULL,
1439 0, &inode_size);
1440 profile_get_integer(profile, "fs_types", fs_type,
1441 "inode_size", inode_size,
1442 &inode_size);
1443 }
1444
1445 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001446 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001447 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001448 inode_size & (inode_size - 1)) {
1449 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001450 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001451 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001452 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001453 exit(1);
1454 }
1455 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1456 fprintf(stderr, _("Warning: %d-byte inodes not usable "
Andreas Dilger067911a2006-07-15 22:08:20 -04001457 "on older systems\n"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001458 inode_size);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001459 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001460 }
1461
Eric Sandeenf3358642006-09-12 14:56:17 -04001462 /* Make sure number of inodes specified will fit in 32 bits */
1463 if (num_inodes == 0) {
1464 __u64 n;
1465 n = (__u64) fs_param.s_blocks_count * blocksize / inode_ratio;
1466 if (n > ~0U) {
1467 com_err(program_name, 0,
1468 _("too many inodes (%llu), raise inode ratio?"), n);
1469 exit(1);
1470 }
1471 } else if (num_inodes > ~0U) {
1472 com_err(program_name, 0,
1473 _("too many inodes (%llu), specify < 2^32 inodes"),
1474 (__u64)num_inodes);
1475 exit(1);
1476 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001477 /*
1478 * Calculate number of inodes based on the inode ratio
1479 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001480 fs_param.s_inodes_count = num_inodes ? num_inodes :
1481 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001482 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001483
1484 /*
1485 * Calculate number of blocks to reserve
1486 */
Theodore Ts'oa8862d92006-08-30 03:08:13 -04001487 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1488 fs_param.s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001489}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001490
Theodore Ts'o3839e651997-04-26 13:21:57 +00001491int main (int argc, char *argv[])
1492{
1493 errcode_t retval = 0;
1494 ext2_filsys fs;
1495 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001496 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001497 unsigned int i;
1498 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001499 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001500
1501#ifdef ENABLE_NLS
1502 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001503 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001504 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1505 textdomain(NLS_CAT_NAME);
1506#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001507 PRS(argc, argv);
1508
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001509#ifdef CONFIG_TESTIO_DEBUG
1510 io_ptr = test_io_manager;
1511 test_io_backing_manager = unix_io_manager;
1512#else
1513 io_ptr = unix_io_manager;
1514#endif
1515
Theodore Ts'o3839e651997-04-26 13:21:57 +00001516 /*
1517 * Initialize the superblock....
1518 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001519 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001520 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001521 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001522 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001523 exit(1);
1524 }
1525
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001526 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001527 * Wipe out the old on-disk superblock
1528 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001529 if (!noaction)
1530 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001531
1532 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001533 * Generate a UUID for it...
1534 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001535 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001536
1537 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001538 * Initialize the directory index variables
1539 */
1540 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1541 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1542
1543 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001544 * Add "jitter" to the superblock's check interval so that we
1545 * don't check all the filesystems at the same time. We use a
1546 * kludgy hack of using the UUID to derive a random jitter value.
1547 */
1548 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1549 val += fs->super->s_uuid[i];
1550 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1551
1552 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001553 * Override the creator OS, if applicable
1554 */
1555 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001556 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001557 exit(1);
1558 }
1559
1560 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001561 * For the Hurd, we will turn off filetype since it doesn't
1562 * support it.
1563 */
1564 if (fs->super->s_creator_os == EXT2_OS_HURD)
1565 fs->super->s_feature_incompat &=
1566 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1567
1568 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001569 * Set the volume label...
1570 */
1571 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001572 memset(fs->super->s_volume_name, 0,
1573 sizeof(fs->super->s_volume_name));
1574 strncpy(fs->super->s_volume_name, volume_label,
1575 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001576 }
1577
1578 /*
1579 * Set the last mount directory
1580 */
1581 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001582 memset(fs->super->s_last_mounted, 0,
1583 sizeof(fs->super->s_last_mounted));
1584 strncpy(fs->super->s_last_mounted, mount_dir,
1585 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001586 }
1587
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001588 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001589 show_stats(fs);
1590
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001591 if (noaction)
1592 exit(0);
1593
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001594 if (fs->super->s_feature_incompat &
1595 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1596 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001597 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001598 }
1599
Theodore Ts'o3839e651997-04-26 13:21:57 +00001600 if (bad_blocks_filename)
1601 read_bb_file(fs, &bb_list, bad_blocks_filename);
1602 if (cflag)
1603 test_disk(fs, &bb_list);
1604
1605 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001606 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001607 retval = ext2fs_allocate_tables(fs);
1608 if (retval) {
1609 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001610 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001611 exit(1);
1612 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001613 if (super_only) {
1614 fs->super->s_state |= EXT2_ERROR_FS;
1615 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1616 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001617 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001618 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001619 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001620 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001621 blk_t ret_blk;
1622
1623#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001624 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001625#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001626
1627 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001628 * Wipe out any old MD RAID (or other) metadata at the end
1629 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001630 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001631 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001632 start = (blocks & ~(rsv - 1));
1633 if (start > rsv)
1634 start -= rsv;
1635 if (start > 0)
1636 retval = zero_blocks(fs, start, blocks - start,
1637 NULL, &ret_blk, NULL);
1638
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001639 if (retval) {
1640 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001641 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001642 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001643 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -04001644 setup_lazy_bg(fs);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001645 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001646 create_root_dir(fs);
1647 create_lost_and_found(fs);
1648 reserve_inodes(fs);
1649 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001650 if (fs->super->s_feature_compat &
1651 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1652 retval = ext2fs_create_resize_inode(fs);
1653 if (retval) {
1654 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001655 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001656 exit(1);
1657 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001658 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001659 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001660
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001661 if (journal_device) {
1662 ext2_filsys jfs;
1663
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001664 if (!force)
1665 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001666 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001667
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001668 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1669 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1670 fs->blocksize, unix_io_manager, &jfs);
1671 if (retval) {
1672 com_err(program_name, retval,
1673 _("while trying to open journal device %s\n"),
1674 journal_device);
1675 exit(1);
1676 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001677 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001678 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001679 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001680 fflush(stdout);
1681 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001682 retval = ext2fs_add_journal_device(fs, jfs);
1683 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001684 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001685 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001686 journal_device);
1687 exit(1);
1688 }
1689 if (!quiet)
1690 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001691 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001692 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001693 } else if ((journal_size) ||
1694 (fs_param.s_feature_compat &
1695 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001696 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001697
1698 if (!journal_blocks) {
1699 fs->super->s_feature_compat &=
1700 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1701 goto no_journal;
1702 }
1703 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001704 printf(_("Creating journal (%d blocks): "),
1705 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001706 fflush(stdout);
1707 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001708 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1709 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001710 if (retval) {
1711 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001712 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001713 exit(1);
1714 }
1715 if (!quiet)
1716 printf(_("done\n"));
1717 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001718no_journal:
1719
Theodore Ts'o3839e651997-04-26 13:21:57 +00001720 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001721 printf(_("Writing superblocks and "
1722 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001723 retval = ext2fs_flush(fs);
1724 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001725 fprintf(stderr,
1726 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001727 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001728 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001729 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001730 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1731 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001732 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001733 val = ext2fs_close(fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001734 remove_error_table(&et_ext2_error_table);
1735 remove_error_table(&et_prof_error_table);
Andreas Dilger568101f2001-10-13 01:22:25 -06001736 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001737}