blob: fc086a480913c9d26ecc2c82825fd53989e5d929 [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'obdd80f22008-02-28 21:12:31 -050097 fprintf(stderr, _("Usage: %s [-c|-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] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -050099 "[-J journal-options]\n"
Andreas Dilger067911a2006-07-15 22:08:20 -0400100 "\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[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500103 "[-r fs-revision] [-E extended-option[,...]]\n"
104 "\t[-T fs-type] [-jnqvFSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105 program_name);
106 exit(1);
107}
108
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000109static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110{
111 int l = 0;
112
113 arg >>= 1;
114 while (arg) {
115 l++;
116 arg >>= 1;
117 }
118 return l;
119}
120
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000121static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000122{
123 int l;
124
125 for (l=0; arg ; l++)
126 arg = arg / 10;
127 return l;
128}
129
Theodore Ts'od99225e2004-09-25 07:40:12 -0400130static int parse_version_number(const char *s)
131{
132 int major, minor, rev;
133 char *endptr;
134 const char *cp = s;
135
136 if (!s)
137 return 0;
138 major = strtol(cp, &endptr, 10);
139 if (cp == endptr || *endptr != '.')
140 return 0;
141 cp = endptr + 1;
142 minor = strtol(cp, &endptr, 10);
143 if (cp == endptr || *endptr != '.')
144 return 0;
145 cp = endptr + 1;
146 rev = strtol(cp, &endptr, 10);
147 if (cp == endptr)
148 return 0;
149 return ((((major * 256) + minor) * 256) + rev);
150}
151
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000152/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000153 * Helper function for read_bb_file and test_disk
154 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500155static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156{
Theodore Ts'o66938372002-03-08 00:14:46 -0500157 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158 return;
159}
160
161/*
162 * Reads the bad blocks list from a file
163 */
164static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
165 const char *bad_blocks_file)
166{
167 FILE *f;
168 errcode_t retval;
169
170 f = fopen(bad_blocks_file, "r");
171 if (!f) {
172 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000173 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 exit(1);
175 }
176 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
177 fclose (f);
178 if (retval) {
179 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000180 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000181 exit(1);
182 }
183}
184
185/*
186 * Runs the badblocks program to test the disk
187 */
188static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
189{
190 FILE *f;
191 errcode_t retval;
192 char buf[1024];
193
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400194 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500195 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Theodore Ts'o8ade4792006-11-08 00:41:50 -0500196 fs->device_name, fs->super->s_blocks_count-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000198 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199 f = popen(buf, "r");
200 if (!f) {
201 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400202 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203 exit(1);
204 }
205 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000206 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 if (retval) {
208 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000209 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210 exit(1);
211 }
212}
213
214static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
215{
Theodore Ts'o54434922003-12-07 01:28:50 -0500216 dgrp_t i;
217 blk_t j;
218 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000219 blk_t blk;
220 badblocks_iterate bb_iter;
221 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000222 blk_t group_block;
223 int group;
224 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225
226 if (!bb_list)
227 return;
228
229 /*
230 * The primary superblock and group descriptors *must* be
231 * good; if not, abort.
232 */
233 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
234 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000235 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000236 fprintf(stderr, _("Block %d in primary "
237 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400238 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000239 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500241 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000242 exit(1);
243 }
244 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000245
246 /*
247 * See if any of the bad blocks are showing up in the backup
248 * superblocks and/or group descriptors. If so, issue a
249 * warning and adjust the block counts appropriately.
250 */
251 group_block = fs->super->s_first_data_block +
252 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000253
254 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000255 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000256 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000257 if (ext2fs_badblocks_list_test(bb_list,
258 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000259 if (!group_bad)
260 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500261_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000262" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000263 group_block);
264 group_bad++;
265 group = ext2fs_group_of_blk(fs, group_block+j);
266 fs->group_desc[group].bg_free_blocks_count++;
267 fs->super->s_free_blocks_count++;
268 }
269 }
270 group_block += fs->super->s_blocks_per_group;
271 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272
273 /*
274 * Mark all the bad blocks as used...
275 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000276 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000278 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000279 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000280 exit(1);
281 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000282 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000283 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000284 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285}
286
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000287/*
288 * These functions implement a generalized progress meter.
289 */
290struct progress_struct {
291 char format[20];
292 char backup[80];
293 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400294 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000295};
296
297static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500298 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000299{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000300 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000301
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000302 memset(progress, 0, sizeof(struct progress_struct));
303 if (quiet)
304 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000305
306 /*
307 * Figure out how many digits we need
308 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000309 i = int_log10(max);
310 sprintf(progress->format, "%%%dd/%%%dld", i, i);
311 memset(progress->backup, '\b', sizeof(progress->backup)-1);
312 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500313 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000314 progress->backup[(2*i)+1] = 0;
315 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000316
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400317 progress->skip_progress = 0;
318 if (getenv("MKE2FS_SKIP_PROGRESS"))
319 progress->skip_progress++;
320
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000321 fputs(label, stdout);
322 fflush(stdout);
323}
324
325static void progress_update(struct progress_struct *progress, __u32 val)
326{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400327 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000328 return;
329 printf(progress->format, val, progress->max);
330 fputs(progress->backup, stdout);
331}
332
333static void progress_close(struct progress_struct *progress)
334{
335 if (progress->format[0] == 0)
336 return;
337 fputs(_("done \n"), stdout);
338}
339
340
341/*
342 * Helper function which zeros out _num_ blocks starting at _blk_. In
343 * case of an error, the details of the error is returned via _ret_blk_
344 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
345 * success, and an error code on an error.
346 *
347 * As a special case, if the first argument is NULL, then it will
348 * attempt to free the static zeroizing buffer. (This is to keep
349 * programs that check for memory leaks happy.)
350 */
351static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
352 struct progress_struct *progress,
353 blk_t *ret_blk, int *ret_count)
354{
355 int j, count, next_update, next_update_incr;
356 static char *buf;
357 errcode_t retval;
358
359 /* If fs is null, clean up the static buffer and return */
360 if (!fs) {
361 if (buf) {
362 free(buf);
363 buf = 0;
364 }
365 return 0;
366 }
367 /* Allocate the zeroizing buffer if necessary */
368 if (!buf) {
369 buf = malloc(fs->blocksize * STRIDE_LENGTH);
370 if (!buf) {
371 com_err("malloc", ENOMEM,
372 _("while allocating zeroizing buffer"));
373 exit(1);
374 }
375 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
376 }
377 /* OK, do the write loop */
378 next_update = 0;
379 next_update_incr = num / 100;
380 if (next_update_incr < 1)
381 next_update_incr = 1;
382 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400383 count = num - j;
384 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000385 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000386 retval = io_channel_write_blk(fs->io, blk, count, buf);
387 if (retval) {
388 if (ret_count)
389 *ret_count = count;
390 if (ret_blk)
391 *ret_blk = blk;
392 return retval;
393 }
394 if (progress && j > next_update) {
395 next_update += num / 100;
396 progress_update(progress, blk);
397 }
398 }
399 return 0;
400}
401
402static void write_inode_tables(ext2_filsys fs)
403{
404 errcode_t retval;
405 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500406 dgrp_t i;
407 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000408 struct progress_struct progress;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400409 int lazy_flag = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000410
411 if (quiet)
412 memset(&progress, 0, sizeof(progress));
413 else
414 progress_init(&progress, _("Writing inode tables: "),
415 fs->group_desc_count);
416
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400417 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
418 EXT2_FEATURE_COMPAT_LAZY_BG))
419 lazy_flag = 1;
420
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000422 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000424 blk = fs->group_desc[i].bg_inode_table;
425 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000426
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400427 if (!(lazy_flag &&
428 (fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT))) {
429 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
430 if (retval) {
431 fprintf(stderr, _("\nCould not write %d "
432 "blocks in inode table starting at %u: %s\n"),
433 num, blk, error_message(retval));
434 exit(1);
435 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000436 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000437 if (sync_kludge) {
438 if (sync_kludge == 1)
439 sync();
440 else if ((i % sync_kludge) == 0)
441 sync();
442 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000444 zero_blocks(0, 0, 0, 0, 0, 0);
445 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000446}
447
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400448static void setup_lazy_bg(ext2_filsys fs)
449{
450 dgrp_t i;
451 int blks;
452 struct ext2_super_block *sb = fs->super;
453 struct ext2_group_desc *bg = fs->group_desc;
454
455 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
456 EXT2_FEATURE_COMPAT_LAZY_BG)) {
457 for (i = 0; i < fs->group_desc_count; i++, bg++) {
458 if ((i == 0) ||
459 (i == fs->group_desc_count-1))
460 continue;
461 if (bg->bg_free_inodes_count ==
462 sb->s_inodes_per_group) {
463 bg->bg_free_inodes_count = 0;
464 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
465 sb->s_free_inodes_count -=
466 sb->s_inodes_per_group;
467 }
468 blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
469 if (bg->bg_free_blocks_count == blks) {
470 bg->bg_free_blocks_count = 0;
471 bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
472 sb->s_free_blocks_count -= blks;
473 }
474 }
475 }
476}
477
478
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479static void create_root_dir(ext2_filsys fs)
480{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400481 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000482 struct ext2_inode inode;
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400483 __u32 uid, gid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484
485 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
486 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000487 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488 exit(1);
489 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000490 if (geteuid()) {
491 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
492 if (retval) {
493 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000494 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000495 exit(1);
496 }
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400497 uid = getuid();
498 inode.i_uid = uid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500499 ext2fs_set_i_uid_high(inode, uid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400500 if (uid) {
501 gid = getgid();
502 inode.i_gid = gid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500503 ext2fs_set_i_gid_high(inode, gid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400504 }
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500505 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000506 if (retval) {
507 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000508 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000509 exit(1);
510 }
511 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512}
513
514static void create_lost_and_found(ext2_filsys fs)
515{
516 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000517 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 const char *name = "lost+found";
519 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000520 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521
Theodore Ts'o6a525062001-12-24 09:40:00 -0500522 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000523 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
524 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000525 com_err("ext2fs_mkdir", retval,
526 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 exit(1);
528 }
529
530 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
531 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000532 com_err("ext2_lookup", retval,
533 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 exit(1);
535 }
536
537 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000538 if ((lpf_size += fs->blocksize) >= 16*1024)
539 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540 retval = ext2fs_expand_dir(fs, ino);
541 if (retval) {
542 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000543 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 exit(1);
545 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500546 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547}
548
549static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
550{
551 errcode_t retval;
552
Theodore Ts'of3db3561997-04-26 13:34:30 +0000553 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000554 fs->group_desc[0].bg_free_inodes_count--;
555 fs->super->s_free_inodes_count--;
556 retval = ext2fs_update_bb_inode(fs, bb_list);
557 if (retval) {
558 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000559 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560 exit(1);
561 }
562
563}
564
565static void reserve_inodes(ext2_filsys fs)
566{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000567 ext2_ino_t i;
568 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000569
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000570 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000571 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000572 group = ext2fs_group_of_ino(fs, i);
573 fs->group_desc[group].bg_free_inodes_count--;
574 fs->super->s_free_inodes_count--;
575 }
576 ext2fs_mark_ib_dirty(fs);
577}
578
Theodore Ts'o756df352002-03-07 20:52:12 -0500579#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500580#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500581#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500582
Theodore Ts'o04a96852001-08-30 21:55:26 -0400583static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000584{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400585 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000586 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500587 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000588
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400589 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600590 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500591 printf(_("Out of memory erasing sectors %d-%d\n"),
592 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600593 exit(1);
594 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500595
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500596 if (sect == 0) {
597 /* Check for a BSD disklabel, and don't erase it if so */
598 retval = io_channel_read_blk(fs->io, 0, -512, buf);
599 if (retval)
600 fprintf(stderr,
601 _("Warning: could not read block 0: %s\n"),
602 error_message(retval));
603 else {
604 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
605 if ((*magic == BSD_DISKMAGIC) ||
606 (*magic == BSD_MAGICDISK))
607 return;
608 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500609 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500610
Theodore Ts'o70988102002-07-14 08:00:00 -0400611 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000612 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400613 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000614 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400615 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000616 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500617 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
618 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000619}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000620
621static void create_journal_dev(ext2_filsys fs)
622{
623 struct progress_struct progress;
624 errcode_t retval;
625 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000626 blk_t blk;
627 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000628
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000629 retval = ext2fs_create_journal_superblock(fs,
630 fs->super->s_blocks_count, 0, &buf);
631 if (retval) {
632 com_err("create_journal_dev", retval,
633 _("while initializing journal superblock"));
634 exit(1);
635 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000636 if (quiet)
637 memset(&progress, 0, sizeof(progress));
638 else
639 progress_init(&progress, _("Zeroing journal device: "),
640 fs->super->s_blocks_count);
641
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000642 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
643 &progress, &blk, &count);
644 if (retval) {
645 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000646 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000647 blk, count);
648 exit(1);
649 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000650 zero_blocks(0, 0, 0, 0, 0, 0);
651
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000652 retval = io_channel_write_blk(fs->io,
653 fs->super->s_first_data_block+1,
654 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000655 if (retval) {
656 com_err("create_journal_dev", retval,
657 _("while writing journal superblock"));
658 exit(1);
659 }
660 progress_close(&progress);
661}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000662
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663static void show_stats(ext2_filsys fs)
664{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000665 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000666 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500667 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500669 dgrp_t i;
670 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500672 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500673 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500674 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000675
676 memset(buf, 0, sizeof(buf));
677 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000678 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500679 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500680 os = e2p_os2string(fs->super->s_creator_os);
681 fputs(os, stdout);
682 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000683 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000684 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000685 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000686 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000687 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000688 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000689 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000690 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691 s->s_r_blocks_count,
692 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000693 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500694 if (s->s_reserved_gdt_blocks)
695 printf(_("Maximum filesystem blocks=%lu\n"),
696 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
697 (fs->blocksize / sizeof(struct ext2_group_desc)) *
698 s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000699 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000700 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000701 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000702 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000703 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000704 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000705 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000706
707 if (fs->group_desc_count == 1) {
708 printf("\n");
709 return;
710 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500711
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000712 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000713 group_block = s->s_first_data_block;
714 col_left = 0;
715 for (i = 1; i < fs->group_desc_count; i++) {
716 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000717 if (!ext2fs_bg_has_super(fs, i))
718 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000719 if (i != 1)
720 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000721 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000722 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000724 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000726 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000727 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728 }
729 printf("\n\n");
730}
731
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000732/*
733 * Set the S_CREATOR_OS field. Return true if OS is known,
734 * otherwise, 0.
735 */
736static int set_os(struct ext2_super_block *sb, char *os)
737{
738 if (isdigit (*os))
739 sb->s_creator_os = atoi (os);
740 else if (strcasecmp(os, "linux") == 0)
741 sb->s_creator_os = EXT2_OS_LINUX;
742 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
743 sb->s_creator_os = EXT2_OS_HURD;
744 else if (strcasecmp(os, "masix") == 0)
745 sb->s_creator_os = EXT2_OS_MASIX;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500746 else if (strcasecmp(os, "freebsd") == 0)
747 sb->s_creator_os = EXT2_OS_FREEBSD;
748 else if (strcasecmp(os, "lites") == 0)
749 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000750 else
751 return 0;
752 return 1;
753}
754
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000755#define PATH_SET "PATH=/sbin"
756
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500757static void parse_extended_opts(struct ext2_super_block *param,
758 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000759{
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500760 char *buf, *token, *next, *p, *arg, *badopt = "";
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000761 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500762 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000763
764 len = strlen(opts);
765 buf = malloc(len+1);
766 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500767 fprintf(stderr,
768 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000769 exit(1);
770 }
771 strcpy(buf, opts);
772 for (token = buf; token && *token; token = next) {
773 p = strchr(token, ',');
774 next = 0;
775 if (p) {
776 *p = 0;
777 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500778 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000779 arg = strchr(token, '=');
780 if (arg) {
781 *arg = 0;
782 arg++;
783 }
784 if (strcmp(token, "stride") == 0) {
785 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500786 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500787 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000788 continue;
789 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500790 param->s_raid_stride = strtoul(arg, &p, 0);
791 if (*p || (param->s_raid_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000792 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400793 _("Invalid stride parameter: %s\n"),
794 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500795 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000796 continue;
797 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500798 } else if (strcmp(token, "stripe-width") == 0 ||
799 strcmp(token, "stripe_width") == 0) {
800 if (!arg) {
801 r_usage++;
802 badopt = token;
803 continue;
804 }
805 param->s_raid_stripe_width = strtoul(arg, &p, 0);
806 if (*p || (param->s_raid_stripe_width == 0)) {
807 fprintf(stderr,
808 _("Invalid stripe-width parameter: %s\n"),
809 arg);
810 r_usage++;
811 continue;
812 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500813 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500814 unsigned long resize, bpg, rsv_groups;
815 unsigned long group_desc_count, desc_blocks;
816 unsigned int gdpb, blocksize;
817 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500818
819 if (!arg) {
820 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500821 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500822 continue;
823 }
824
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500825 resize = parse_num_blocks(arg,
826 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500827
828 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500829 fprintf(stderr,
830 _("Invalid resize parameter: %s\n"),
831 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500832 r_usage++;
833 continue;
834 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500835 if (resize <= param->s_blocks_count) {
836 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400837 _("The resize maximum must be greater "
838 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500839 r_usage++;
840 continue;
841 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500842
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500843 blocksize = EXT2_BLOCK_SIZE(param);
844 bpg = param->s_blocks_per_group;
845 if (!bpg)
846 bpg = blocksize * 8;
847 gdpb = blocksize / sizeof(struct ext2_group_desc);
Theodore Ts'o69022e02006-08-30 01:57:00 -0400848 group_desc_count =
849 ext2fs_div_ceil(param->s_blocks_count, bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500850 desc_blocks = (group_desc_count +
851 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400852 rsv_groups = ext2fs_div_ceil(resize, bpg);
853 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500854 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500855 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500856 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
857
858 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400859 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
860 fprintf(stderr,
861 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400862 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400863 exit(1);
864 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500865 param->s_feature_compat |=
866 EXT2_FEATURE_COMPAT_RESIZE_INODE;
867
868 param->s_reserved_gdt_blocks = rsv_gdb;
869 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500870 } else if (!strcmp(token, "test_fs")) {
871 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500872 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500873 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500874 badopt = token;
875 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000876 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500877 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500878 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400879 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000880 "and may take an argument which\n"
881 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400882 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500883 "\tstride=<RAID per-disk data chunk in blocks>\n"
884 "\tstripe-width=<RAID stride * data disks in blocks>\n"
885 "\tresize=<resize maximum size in blocks>\n\n"
886 "\ttest_fs\n"),
887 badopt);
Brian Behlendorf21400382007-05-31 11:30:47 -0400888 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000889 exit(1);
890 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500891 if (param->s_raid_stride &&
892 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
893 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
894 "multiple of stride %u.\n\n"),
895 param->s_raid_stripe_width, param->s_raid_stride);
896
Brian Behlendorf21400382007-05-31 11:30:47 -0400897 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000898}
899
Theodore Ts'o896938d1999-10-23 01:04:50 +0000900static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500901 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400902 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500903 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400904 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o0d1de232008-01-27 19:38:46 -0500905 EXT2_FEATURE_COMPAT_LAZY_BG |
Theodore Ts'o558df542008-02-27 15:01:19 -0500906 EXT2_FEATURE_COMPAT_EXT_ATTR,
907 /* Incompat */
908 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400909 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
910 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o558df542008-02-27 15:01:19 -0500911 /* R/O compat */
912 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
913 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o896938d1999-10-23 01:04:50 +0000914};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000915
916
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500917static void syntax_err_report(const char *filename, long err, int line_num)
918{
919 fprintf(stderr,
920 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
921 filename, line_num, error_message(err));
922 exit(1);
923}
924
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200925static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500926
927static void edit_feature(const char *str, __u32 *compat_array)
928{
929 if (!str)
930 return;
931
932 if (e2p_edit_feature(str, compat_array, ok_features)) {
933 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
934 str);
935 exit(1);
936 }
937}
938
Theodore Ts'od48bc602007-07-04 14:10:46 -0400939extern const char *mke2fs_default_profile;
940static const char *default_files[] = { "<default>", 0 };
941
Theodore Ts'o3839e651997-04-26 13:21:57 +0000942static void PRS(int argc, char *argv[])
943{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400944 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000945 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500946 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000947 int blocksize = 0;
948 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600949 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -0500950 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400951 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500952 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400953 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000954 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000955 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500956 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500957 const char * fs_type = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000958 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500959#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000960 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000961#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400962 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500963 int s_opt = -1, r_opt = -1;
964 char *fs_features = 0;
965 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500966 char *newpath;
967 int pathlen = sizeof(PATH_SET) + 1;
968
969 if (oldpath)
970 pathlen += strlen(oldpath);
971 newpath = malloc(pathlen);
972 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000973
Theodore Ts'o3839e651997-04-26 13:21:57 +0000974 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000975 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000976 strcat (newpath, ":");
977 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -0500978 }
979 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000980
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000981 tmp = getenv("MKE2FS_SYNC");
982 if (tmp)
983 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400984
985 /* Determine the system page size if possible */
986#ifdef HAVE_SYSCONF
987#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
988#define _SC_PAGESIZE _SC_PAGE_SIZE
989#endif
990#ifdef _SC_PAGESIZE
991 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600992 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400993 sys_page_size = sysval;
994#endif /* _SC_PAGESIZE */
995#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500996
997 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
998 config_fn[0] = tmp;
999 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001000 retval = profile_init(config_fn, &profile);
1001 if (retval == ENOENT) {
1002 profile_init(default_files, &profile);
1003 profile_set_default(profile, mke2fs_default_profile);
1004 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001005
Theodore Ts'o3839e651997-04-26 13:21:57 +00001006 setbuf(stdout, NULL);
1007 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001008 add_error_table(&et_ext2_error_table);
1009 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001010 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1011 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001012
Theodore Ts'o756df352002-03-07 20:52:12 -05001013#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001014 if (uname(&ut)) {
1015 perror("uname");
1016 exit(1);
1017 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001018 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001019 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001020 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001021#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001022
1023 if (argc && *argv) {
1024 program_name = get_progname(*argv);
1025
1026 /* If called as mkfs.ext3, create a journal inode */
1027 if (!strcmp(program_name, "mkfs.ext3"))
1028 journal_size = -1;
1029 }
1030
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001031 while ((c = getopt (argc, argv,
Andreas Dilger25247852005-07-06 12:58:15 -05001032 "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 +00001033 switch (c) {
1034 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001035 blocksize = strtol(optarg, &tmp, 0);
1036 b = (blocksize > 0) ? blocksize : -blocksize;
1037 if (b < EXT2_MIN_BLOCK_SIZE ||
1038 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001039 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001040 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041 exit(1);
1042 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001043 if (blocksize > 4096)
1044 fprintf(stderr, _("Warning: blocksize %d not "
1045 "usable on most systems.\n"),
1046 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001047 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001048 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001049 int_log2(blocksize >>
1050 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001051 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001052 case 'c': /* Check for bad blocks */
1053 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001054 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001055 break;
1056 case 'f':
1057 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001058 if (size < EXT2_MIN_BLOCK_SIZE ||
1059 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001060 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001061 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001062 optarg);
1063 exit(1);
1064 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001065 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001066 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001067 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001068 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001069 break;
1070 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001071 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001072 if (*tmp) {
1073 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001074 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001075 exit(1);
1076 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001077 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001078 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001079 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001080 exit(1);
1081 }
1082 break;
1083 case 'i':
1084 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001085 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1086 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001087 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001088 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001089 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001090 optarg, EXT2_MIN_BLOCK_SIZE,
1091 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001092 exit(1);
1093 }
1094 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001095 case 'J':
1096 parse_journal_opts(optarg);
1097 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001098 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001099 if (!journal_size)
1100 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001101 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001102 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001103 bad_blocks_filename = malloc(strlen(optarg)+1);
1104 if (!bad_blocks_filename) {
1105 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001106 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001107 exit(1);
1108 }
1109 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001110 break;
1111 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001112 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001113 if (reserved_ratio > 50 || *tmp) {
1114 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001115 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001116 optarg);
1117 exit(1);
1118 }
1119 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001120 case 'n':
1121 noaction++;
1122 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001123 case 'o':
1124 creator_os = optarg;
1125 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001126 case 'q':
1127 quiet = 1;
1128 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001129 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001130 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001131 if (*tmp) {
1132 com_err(program_name, 0,
1133 _("bad revision level - %s"), optarg);
1134 exit(1);
1135 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001136 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001137 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001138 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001139 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001140 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001141 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001142 inode_size = strtoul(optarg, &tmp, 0);
1143 if (*tmp) {
1144 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001145 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001146 exit(1);
1147 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001148 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001149 case 'v':
1150 verbose = 1;
1151 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001152 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001153 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001154 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001155 case 'L':
1156 volume_label = optarg;
1157 break;
1158 case 'M':
1159 mount_dir = optarg;
1160 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001161 case 'N':
1162 num_inodes = strtoul(optarg, &tmp, 0);
1163 if (*tmp) {
1164 com_err(program_name, 0,
1165 _("bad num inodes - %s"), optarg);
1166 exit(1);
1167 }
1168 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001169 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001170 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001171 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001172 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001173 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001174 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001175 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001176 case 'S':
1177 super_only = 1;
1178 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001179 case 'T':
1180 fs_type = optarg;
1181 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001182 case 'V':
1183 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001184 show_version_only++;
1185 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001186 default:
1187 usage();
1188 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001189 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001190 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001191 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001192 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001193
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001194 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001195 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1196 E2FSPROGS_DATE);
1197
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001198 if (show_version_only) {
1199 fprintf(stderr, _("\tUsing %s\n"),
1200 error_message(EXT2_ET_BASE));
1201 exit(0);
1202 }
1203
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001204 /*
1205 * If there's no blocksize specified and there is a journal
1206 * device, use it to figure out the blocksize
1207 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001208 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001209 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001210 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001211
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001212#ifdef CONFIG_TESTIO_DEBUG
1213 io_ptr = test_io_manager;
1214 test_io_backing_manager = unix_io_manager;
1215#else
1216 io_ptr = unix_io_manager;
1217#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001218 retval = ext2fs_open(journal_device,
1219 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001220 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001221 if (retval) {
1222 com_err(program_name, retval,
1223 _("while trying to open journal device %s\n"),
1224 journal_device);
1225 exit(1);
1226 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001227 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001228 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001229 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001230 "minimum blocksize %d\n"), jfs->blocksize,
1231 -blocksize);
1232 exit(1);
1233 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001234 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001235 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001236 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1237 ext2fs_close(jfs);
1238 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001239
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001240 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001241 if (!force) {
1242 com_err(program_name, 0,
1243 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001244 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001245 proceed_question();
1246 }
1247 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1248 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001249 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001250 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001251 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001252 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1253 fs_param.s_log_block_size);
1254 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001255 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001256 argv[optind - 1]);
1257 exit(1);
1258 }
1259 }
1260 if (optind < argc)
1261 usage();
1262
Theodore Ts'o74becf31997-04-26 14:37:06 +00001263 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001264 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001265 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001266
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001267 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001268
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001269 if (noaction && fs_param.s_blocks_count) {
1270 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001271 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001272 } else {
1273 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001274 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001275 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001276 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001277 if ((retval == EFBIG) &&
1278 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001279 (fs_param.s_log_block_size == 0)) {
1280 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001281 blocksize = 4096;
1282 goto retry;
1283 }
1284 }
1285
Theodore Ts'oa789d841998-03-30 01:20:55 +00001286 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1287 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001288 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001289 exit(1);
1290 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001291 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001292 if (retval == EXT2_ET_UNIMPLEMENTED) {
1293 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001294 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001295 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001296 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001297 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001298 } else {
1299 if (dev_size == 0) {
1300 com_err(program_name, 0,
1301 _("Device size reported to be zero. "
1302 "Invalid partition specified, or\n\t"
1303 "partition table wasn't reread "
1304 "after running fdisk, due to\n\t"
1305 "a modified partition being busy "
1306 "and in use. You may need to reboot\n\t"
1307 "to re-read your partition table.\n"
1308 ));
1309 exit(1);
1310 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001311 fs_param.s_blocks_count = dev_size;
1312 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1313 fs_param.s_blocks_count &= ~((sys_page_size /
1314 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001315 }
1316
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001317 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001318 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001319 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001320 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001321 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001322
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001323 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001324 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001325 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1326
Brian Behlendorfe0661502007-03-19 08:25:38 -04001327 if (fs_param.s_feature_incompat &
1328 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1329 fs_type = "journal";
1330 else if (megs <= 3)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001331 fs_type = "floppy";
1332 else if (megs <= 512)
1333 fs_type = "small";
1334 else
1335 fs_type = "default";
1336 }
1337
1338 /* Figure out what features should be enabled */
1339
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001340 tmp = tmp2 = NULL;
1341 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1342 profile_get_string(profile, "defaults", "base_features", 0,
Eric Sandeena25d2312007-05-07 23:38:14 -04001343 "sparse_super,filetype,resize_inode,dir_index",
1344 &tmp);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001345 profile_get_string(profile, "fs_types", fs_type,
1346 "base_features", tmp, &tmp2);
1347 edit_feature(tmp2, &fs_param.s_feature_compat);
1348 free(tmp);
1349 free(tmp2);
1350
1351 tmp = tmp2 = NULL;
1352 profile_get_string(profile, "defaults", "default_features", 0,
1353 "", &tmp);
1354 profile_get_string(profile, "fs_types", fs_type,
1355 "default_features", tmp, &tmp2);
1356 }
1357 edit_feature(fs_features ? fs_features : tmp2,
1358 &fs_param.s_feature_compat);
1359 if (tmp)
1360 free(tmp);
1361 if (tmp2)
1362 free(tmp2);
1363
1364 if (r_opt == EXT2_GOOD_OLD_REV &&
1365 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1366 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001367 fprintf(stderr, _("Filesystem features not supported "
1368 "with revision 0 filesystems\n"));
1369 exit(1);
1370 }
1371
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001372 if (s_opt > 0) {
1373 if (r_opt == EXT2_GOOD_OLD_REV) {
1374 fprintf(stderr, _("Sparse superblocks not supported "
1375 "with revision 0 filesystems\n"));
1376 exit(1);
1377 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001378 fs_param.s_feature_ro_compat |=
1379 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001380 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001381 fs_param.s_feature_ro_compat &=
1382 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1383
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001384 if (journal_size != 0) {
1385 if (r_opt == EXT2_GOOD_OLD_REV) {
1386 fprintf(stderr, _("Journals not supported "
1387 "with revision 0 filesystems\n"));
1388 exit(1);
1389 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001390 fs_param.s_feature_compat |=
1391 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001392 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001393
1394 if (fs_param.s_feature_incompat &
1395 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001396 reserved_ratio = 0;
1397 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1398 fs_param.s_feature_compat = 0;
1399 fs_param.s_feature_ro_compat = 0;
1400 }
1401
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001402 /* Set first meta blockgroup via an environment variable */
1403 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001404 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001405 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001406 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001407
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001408 /* Get the hardware sector size, if available */
1409 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1410 if (retval) {
1411 com_err(program_name, retval,
1412 _("while trying to determine hardware sector size"));
1413 exit(1);
1414 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001415
Theodore Ts'o54434922003-12-07 01:28:50 -05001416 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001417 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001418
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001419 if (blocksize <= 0) {
1420 profile_get_integer(profile, "defaults", "blocksize", 0,
Eric Sandeena25d2312007-05-07 23:38:14 -04001421 4096, &use_bsize);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001422 profile_get_integer(profile, "fs_types", fs_type,
1423 "blocksize", use_bsize, &use_bsize);
1424
1425 if (use_bsize == -1) {
1426 use_bsize = sys_page_size;
1427 if ((linux_version_code < (2*65536 + 6*256)) &&
1428 (use_bsize > 4096))
1429 use_bsize = 4096;
1430 }
1431 if (sector_size && use_bsize < sector_size)
1432 use_bsize = sector_size;
1433 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1434 use_bsize = -blocksize;
1435 blocksize = use_bsize;
1436 fs_param.s_blocks_count /= blocksize / 1024;
1437 }
1438
1439 if (inode_ratio == 0) {
1440 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1441 8192, &inode_ratio);
1442 profile_get_integer(profile, "fs_types", fs_type,
1443 "inode_ratio", inode_ratio,
1444 &inode_ratio);
1445
1446 if (inode_ratio < blocksize)
1447 inode_ratio = blocksize;
1448 }
1449
1450 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1451 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1452
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001453 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001454
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001455 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001456 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001457
1458 /* Since sparse_super is the default, we would only have a problem
1459 * here if it was explicitly disabled.
1460 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001461 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1462 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001463 com_err(program_name, 0,
1464 _("reserved online resize blocks not supported "
1465 "on non-sparse filesystem"));
1466 exit(1);
1467 }
1468
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001469 if (fs_param.s_blocks_per_group) {
1470 if (fs_param.s_blocks_per_group < 256 ||
1471 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001472 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001473 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001474 exit(1);
1475 }
1476 }
1477
Theodore Ts'o642935c2006-11-14 23:38:17 -05001478 if (!force && fs_param.s_blocks_count >= ((unsigned) 1 << 31)) {
Theodore Ts'o675b79c2005-06-30 20:00:41 -04001479 com_err(program_name, 0,
1480 _("Filesystem too large. No more than 2**31-1 blocks\n"
1481 "\t (8TB using a blocksize of 4k) are currently supported."));
1482 exit(1);
1483 }
1484
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001485 if ((blocksize > 4096) &&
1486 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1487 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1488 "blocksizes greater than 4096\n\tusing ext3. "
1489 "Use -b 4096 if this is an issue for you.\n\n"));
1490
Andreas Dilger067911a2006-07-15 22:08:20 -04001491 if (inode_size == 0) {
1492 profile_get_integer(profile, "defaults", "inode_size", NULL,
1493 0, &inode_size);
1494 profile_get_integer(profile, "fs_types", fs_type,
1495 "inode_size", inode_size,
1496 &inode_size);
1497 }
1498
1499 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001500 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001501 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001502 inode_size & (inode_size - 1)) {
1503 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001504 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001505 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001506 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001507 exit(1);
1508 }
1509 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1510 fprintf(stderr, _("Warning: %d-byte inodes not usable "
Andreas Dilger067911a2006-07-15 22:08:20 -04001511 "on older systems\n"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001512 inode_size);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001513 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001514 }
1515
Eric Sandeenf3358642006-09-12 14:56:17 -04001516 /* Make sure number of inodes specified will fit in 32 bits */
1517 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001518 unsigned long long n;
1519 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
Eric Sandeenf3358642006-09-12 14:56:17 -04001520 if (n > ~0U) {
1521 com_err(program_name, 0,
1522 _("too many inodes (%llu), raise inode ratio?"), n);
1523 exit(1);
1524 }
1525 } else if (num_inodes > ~0U) {
1526 com_err(program_name, 0,
1527 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001528 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04001529 exit(1);
1530 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001531 /*
1532 * Calculate number of inodes based on the inode ratio
1533 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001534 fs_param.s_inodes_count = num_inodes ? num_inodes :
1535 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001536 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001537
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001538 if ((((long long)fs_param.s_inodes_count) *
1539 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1540 (((long long)fs_param.s_blocks_count) *
1541 EXT2_BLOCK_SIZE(&fs_param))) {
1542 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1543 "(%u) too big for a\n\t"
1544 "filesystem with %lu blocks, "
1545 "specify higher inode_ratio (-i)\n\t"
1546 "or lower inode count (-N).\n"),
1547 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001548 fs_param.s_inodes_count,
1549 (unsigned long) fs_param.s_blocks_count);
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001550 exit(1);
1551 }
1552
Theodore Ts'o3839e651997-04-26 13:21:57 +00001553 /*
1554 * Calculate number of blocks to reserve
1555 */
Theodore Ts'oa8862d92006-08-30 03:08:13 -04001556 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1557 fs_param.s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001558}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001559
Theodore Ts'o3839e651997-04-26 13:21:57 +00001560int main (int argc, char *argv[])
1561{
1562 errcode_t retval = 0;
1563 ext2_filsys fs;
1564 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001565 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001566 unsigned int i;
1567 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001568 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001569
1570#ifdef ENABLE_NLS
1571 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001572 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001573 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1574 textdomain(NLS_CAT_NAME);
1575#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001576 PRS(argc, argv);
1577
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001578#ifdef CONFIG_TESTIO_DEBUG
1579 io_ptr = test_io_manager;
1580 test_io_backing_manager = unix_io_manager;
1581#else
1582 io_ptr = unix_io_manager;
1583#endif
1584
Theodore Ts'o3839e651997-04-26 13:21:57 +00001585 /*
1586 * Initialize the superblock....
1587 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001588 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001589 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001590 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001591 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001592 exit(1);
1593 }
1594
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001595 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1596 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1597
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001598 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001599 * Wipe out the old on-disk superblock
1600 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001601 if (!noaction)
1602 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001603
1604 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001605 * Generate a UUID for it...
1606 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001607 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001608
1609 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001610 * Initialize the directory index variables
1611 */
1612 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1613 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1614
1615 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001616 * Add "jitter" to the superblock's check interval so that we
1617 * don't check all the filesystems at the same time. We use a
1618 * kludgy hack of using the UUID to derive a random jitter value.
1619 */
1620 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1621 val += fs->super->s_uuid[i];
1622 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1623
1624 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001625 * Override the creator OS, if applicable
1626 */
1627 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001628 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001629 exit(1);
1630 }
1631
1632 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001633 * For the Hurd, we will turn off filetype since it doesn't
1634 * support it.
1635 */
1636 if (fs->super->s_creator_os == EXT2_OS_HURD)
1637 fs->super->s_feature_incompat &=
1638 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1639
1640 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001641 * Set the volume label...
1642 */
1643 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001644 memset(fs->super->s_volume_name, 0,
1645 sizeof(fs->super->s_volume_name));
1646 strncpy(fs->super->s_volume_name, volume_label,
1647 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001648 }
1649
1650 /*
1651 * Set the last mount directory
1652 */
1653 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001654 memset(fs->super->s_last_mounted, 0,
1655 sizeof(fs->super->s_last_mounted));
1656 strncpy(fs->super->s_last_mounted, mount_dir,
1657 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001658 }
1659
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001660 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001661 show_stats(fs);
1662
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001663 if (noaction)
1664 exit(0);
1665
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001666 if (fs->super->s_feature_incompat &
1667 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1668 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001669 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001670 }
1671
Theodore Ts'o3839e651997-04-26 13:21:57 +00001672 if (bad_blocks_filename)
1673 read_bb_file(fs, &bb_list, bad_blocks_filename);
1674 if (cflag)
1675 test_disk(fs, &bb_list);
1676
1677 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001678 fs->stride = fs_stride = fs->super->s_raid_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001679 retval = ext2fs_allocate_tables(fs);
1680 if (retval) {
1681 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001682 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001683 exit(1);
1684 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001685 if (super_only) {
1686 fs->super->s_state |= EXT2_ERROR_FS;
1687 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1688 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001689 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001690 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001691 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001692 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001693 blk_t ret_blk;
1694
1695#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001696 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001697#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001698
1699 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001700 * Wipe out any old MD RAID (or other) metadata at the end
1701 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001702 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001703 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001704 start = (blocks & ~(rsv - 1));
1705 if (start > rsv)
1706 start -= rsv;
1707 if (start > 0)
1708 retval = zero_blocks(fs, start, blocks - start,
1709 NULL, &ret_blk, NULL);
1710
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001711 if (retval) {
1712 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001713 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001714 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001715 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -04001716 setup_lazy_bg(fs);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001717 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001718 create_root_dir(fs);
1719 create_lost_and_found(fs);
1720 reserve_inodes(fs);
1721 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001722 if (fs->super->s_feature_compat &
1723 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1724 retval = ext2fs_create_resize_inode(fs);
1725 if (retval) {
1726 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001727 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001728 exit(1);
1729 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001730 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001731 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001732
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001733 if (journal_device) {
1734 ext2_filsys jfs;
1735
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001736 if (!force)
1737 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001738 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001739
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001740 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1741 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1742 fs->blocksize, unix_io_manager, &jfs);
1743 if (retval) {
1744 com_err(program_name, retval,
1745 _("while trying to open journal device %s\n"),
1746 journal_device);
1747 exit(1);
1748 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001749 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001750 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001751 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001752 fflush(stdout);
1753 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001754 retval = ext2fs_add_journal_device(fs, jfs);
1755 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001756 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001757 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001758 journal_device);
1759 exit(1);
1760 }
1761 if (!quiet)
1762 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001763 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001764 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001765 } else if ((journal_size) ||
1766 (fs_param.s_feature_compat &
1767 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001768 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001769
1770 if (!journal_blocks) {
1771 fs->super->s_feature_compat &=
1772 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1773 goto no_journal;
1774 }
1775 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001776 printf(_("Creating journal (%d blocks): "),
1777 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001778 fflush(stdout);
1779 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001780 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1781 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001782 if (retval) {
1783 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001784 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001785 exit(1);
1786 }
1787 if (!quiet)
1788 printf(_("done\n"));
1789 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001790no_journal:
1791
Theodore Ts'o3839e651997-04-26 13:21:57 +00001792 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001793 printf(_("Writing superblocks and "
1794 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001795 retval = ext2fs_flush(fs);
1796 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001797 fprintf(stderr,
1798 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001799 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001800 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001801 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001802 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1803 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001804 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001805 val = ext2fs_close(fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001806 remove_error_table(&et_ext2_error_table);
1807 remove_error_table(&et_prof_error_table);
Andreas Dilger568101f2001-10-13 01:22:25 -06001808 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001809}