blob: a955af1264f21dc24298f77a0ddbc958a4c1e52a [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>
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +053047#include <libgen.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000048
Theodore Ts'o54c637d2001-05-14 11:45:38 +000049#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000050#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000051#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000052#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000053#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000054#include "util.h"
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050055#include "profile.h"
Theodore Ts'oa6d83022006-12-26 03:38:07 -050056#include "prof_err.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000057#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000058#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000059
60#define STRIDE_LENGTH 8
61
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000062#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000063#define ZAP_BOOTBLOCK
64#endif
65
Theodore Ts'o3839e651997-04-26 13:21:57 +000066extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000067extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000068
69const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000070const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000071
72/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000073int cflag;
74int verbose;
75int quiet;
76int super_only;
77int force;
78int noaction;
79int journal_size;
80int journal_flags;
Theodore Ts'oa4396e92008-04-18 10:19:27 -040081int lazy_itable_init;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000082char *bad_blocks_filename;
83__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050085struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000086char *creator_os;
87char *volume_label;
88char *mount_dir;
89char *journal_device;
90int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +053091char **fs_types;
Theodore Ts'o3839e651997-04-26 13:21:57 +000092
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050093profile_t profile;
94
Theodore Ts'o31e29a12002-05-17 10:53:07 -040095int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040096int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040097
Theodore Ts'o63985322001-01-03 17:02:13 +000098static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000099{
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500100 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400101 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500102 "[-J journal-options]\n"
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400103 "\t[-G meta group size] [-N number-of-inodes]\n"
104 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
105 "\t[-g blocks-per-group] [-L volume-label] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400106 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500107 "[-r fs-revision] [-E extended-option[,...]]\n"
108 "\t[-T fs-type] [-jnqvFSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 program_name);
110 exit(1);
111}
112
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000113static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114{
115 int l = 0;
116
117 arg >>= 1;
118 while (arg) {
119 l++;
120 arg >>= 1;
121 }
122 return l;
123}
124
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000125static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000126{
127 int l;
128
129 for (l=0; arg ; l++)
130 arg = arg / 10;
131 return l;
132}
133
Theodore Ts'od99225e2004-09-25 07:40:12 -0400134static int parse_version_number(const char *s)
135{
136 int major, minor, rev;
137 char *endptr;
138 const char *cp = s;
139
140 if (!s)
141 return 0;
142 major = strtol(cp, &endptr, 10);
143 if (cp == endptr || *endptr != '.')
144 return 0;
145 cp = endptr + 1;
146 minor = strtol(cp, &endptr, 10);
147 if (cp == endptr || *endptr != '.')
148 return 0;
149 cp = endptr + 1;
150 rev = strtol(cp, &endptr, 10);
151 if (cp == endptr)
152 return 0;
153 return ((((major * 256) + minor) * 256) + rev);
154}
155
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000156/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 * Helper function for read_bb_file and test_disk
158 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500159static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160{
Theodore Ts'o66938372002-03-08 00:14:46 -0500161 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162 return;
163}
164
165/*
166 * Reads the bad blocks list from a file
167 */
168static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
169 const char *bad_blocks_file)
170{
171 FILE *f;
172 errcode_t retval;
173
174 f = fopen(bad_blocks_file, "r");
175 if (!f) {
176 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000177 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 exit(1);
179 }
180 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
181 fclose (f);
182 if (retval) {
183 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000184 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185 exit(1);
186 }
187}
188
189/*
190 * Runs the badblocks program to test the disk
191 */
192static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
193{
194 FILE *f;
195 errcode_t retval;
196 char buf[1024];
197
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400198 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500199 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Theodore Ts'o8ade4792006-11-08 00:41:50 -0500200 fs->device_name, fs->super->s_blocks_count-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000202 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203 f = popen(buf, "r");
204 if (!f) {
205 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400206 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 exit(1);
208 }
209 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000210 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 if (retval) {
212 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000213 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000214 exit(1);
215 }
216}
217
218static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
219{
Theodore Ts'o54434922003-12-07 01:28:50 -0500220 dgrp_t i;
221 blk_t j;
222 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223 blk_t blk;
224 badblocks_iterate bb_iter;
225 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000226 blk_t group_block;
227 int group;
228 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229
230 if (!bb_list)
231 return;
232
233 /*
234 * The primary superblock and group descriptors *must* be
235 * good; if not, abort.
236 */
237 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
238 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000239 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000240 fprintf(stderr, _("Block %d in primary "
241 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400242 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000243 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500245 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000246 exit(1);
247 }
248 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000249
250 /*
251 * See if any of the bad blocks are showing up in the backup
252 * superblocks and/or group descriptors. If so, issue a
253 * warning and adjust the block counts appropriately.
254 */
255 group_block = fs->super->s_first_data_block +
256 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000257
258 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000259 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000260 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000261 if (ext2fs_badblocks_list_test(bb_list,
262 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000263 if (!group_bad)
264 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500265_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000266" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000267 group_block);
268 group_bad++;
269 group = ext2fs_group_of_blk(fs, group_block+j);
270 fs->group_desc[group].bg_free_blocks_count++;
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400271 ext2fs_group_desc_csum_set(fs, group);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000272 fs->super->s_free_blocks_count++;
273 }
274 }
275 group_block += fs->super->s_blocks_per_group;
276 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277
278 /*
279 * Mark all the bad blocks as used...
280 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000281 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000283 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000284 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285 exit(1);
286 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000287 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000288 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000289 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000290}
291
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000292/*
293 * These functions implement a generalized progress meter.
294 */
295struct progress_struct {
296 char format[20];
297 char backup[80];
298 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400299 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000300};
301
302static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500303 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000304{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000305 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000306
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000307 memset(progress, 0, sizeof(struct progress_struct));
308 if (quiet)
309 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000310
311 /*
312 * Figure out how many digits we need
313 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000314 i = int_log10(max);
315 sprintf(progress->format, "%%%dd/%%%dld", i, i);
316 memset(progress->backup, '\b', sizeof(progress->backup)-1);
317 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500318 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000319 progress->backup[(2*i)+1] = 0;
320 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000321
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400322 progress->skip_progress = 0;
323 if (getenv("MKE2FS_SKIP_PROGRESS"))
324 progress->skip_progress++;
325
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000326 fputs(label, stdout);
327 fflush(stdout);
328}
329
330static void progress_update(struct progress_struct *progress, __u32 val)
331{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400332 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000333 return;
334 printf(progress->format, val, progress->max);
335 fputs(progress->backup, stdout);
336}
337
338static void progress_close(struct progress_struct *progress)
339{
340 if (progress->format[0] == 0)
341 return;
342 fputs(_("done \n"), stdout);
343}
344
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400345static void write_inode_tables(ext2_filsys fs, int lazy_flag)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000346{
347 errcode_t retval;
348 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500349 dgrp_t i;
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400350 int num, ipb;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000351 struct progress_struct progress;
352
353 if (quiet)
354 memset(&progress, 0, sizeof(progress));
355 else
356 progress_init(&progress, _("Writing inode tables: "),
357 fs->group_desc_count);
358
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000360 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000361
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000362 blk = fs->group_desc[i].bg_inode_table;
363 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000364
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400365 if (lazy_flag) {
366 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400367 num = ((((fs->super->s_inodes_per_group -
368 fs->group_desc[i].bg_itable_unused) *
369 EXT2_INODE_SIZE(fs->super)) +
370 EXT2_BLOCK_SIZE(fs->super) - 1) /
371 EXT2_BLOCK_SIZE(fs->super));
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400372 } else {
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500373 /* The kernel doesn't need to zero the itable blocks */
374 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400375 ext2fs_group_desc_csum_set(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000376 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530377 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400378 if (retval) {
379 fprintf(stderr, _("\nCould not write %d "
380 "blocks in inode table starting at %u: %s\n"),
381 num, blk, error_message(retval));
382 exit(1);
383 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000384 if (sync_kludge) {
385 if (sync_kludge == 1)
386 sync();
387 else if ((i % sync_kludge) == 0)
388 sync();
389 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530391 ext2fs_zero_blocks(0, 0, 0, 0, 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000392 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000393}
394
395static void create_root_dir(ext2_filsys fs)
396{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400397 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000398 struct ext2_inode inode;
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400399 __u32 uid, gid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000400
401 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
402 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000403 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000404 exit(1);
405 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000406 if (geteuid()) {
407 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
408 if (retval) {
409 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000410 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000411 exit(1);
412 }
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400413 uid = getuid();
414 inode.i_uid = uid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500415 ext2fs_set_i_uid_high(inode, uid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400416 if (uid) {
417 gid = getgid();
418 inode.i_gid = gid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500419 ext2fs_set_i_gid_high(inode, gid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400420 }
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500421 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000422 if (retval) {
423 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000424 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000425 exit(1);
426 }
427 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428}
429
430static void create_lost_and_found(ext2_filsys fs)
431{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400432 unsigned int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000433 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000434 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435 const char *name = "lost+found";
436 int i;
437
Theodore Ts'o6a525062001-12-24 09:40:00 -0500438 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000439 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
440 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000441 com_err("ext2fs_mkdir", retval,
442 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443 exit(1);
444 }
445
446 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
447 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000448 com_err("ext2_lookup", retval,
449 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 exit(1);
451 }
452
453 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100454 /* Ensure that lost+found is at least 2 blocks, so we always
455 * test large empty blocks for big-block filesystems. */
456 if ((lpf_size += fs->blocksize) >= 16*1024 &&
457 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000458 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 retval = ext2fs_expand_dir(fs, ino);
460 if (retval) {
461 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000462 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 exit(1);
464 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500465 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466}
467
468static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
469{
470 errcode_t retval;
471
Theodore Ts'of3db3561997-04-26 13:34:30 +0000472 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400473 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000474 retval = ext2fs_update_bb_inode(fs, bb_list);
475 if (retval) {
476 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000477 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 exit(1);
479 }
480
481}
482
483static void reserve_inodes(ext2_filsys fs)
484{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000485 ext2_ino_t i;
486 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400488 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
489 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 ext2fs_mark_ib_dirty(fs);
491}
492
Theodore Ts'o756df352002-03-07 20:52:12 -0500493#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500494#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500495#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500496
Theodore Ts'o04a96852001-08-30 21:55:26 -0400497static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000498{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400499 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000500 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500501 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000502
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400503 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600504 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500505 printf(_("Out of memory erasing sectors %d-%d\n"),
506 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600507 exit(1);
508 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500509
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500510 if (sect == 0) {
511 /* Check for a BSD disklabel, and don't erase it if so */
512 retval = io_channel_read_blk(fs->io, 0, -512, buf);
513 if (retval)
514 fprintf(stderr,
515 _("Warning: could not read block 0: %s\n"),
516 error_message(retval));
517 else {
518 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
519 if ((*magic == BSD_DISKMAGIC) ||
520 (*magic == BSD_MAGICDISK))
521 return;
522 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500523 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500524
Theodore Ts'o70988102002-07-14 08:00:00 -0400525 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000526 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400527 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000528 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400529 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000530 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500531 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
532 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000533}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000534
535static void create_journal_dev(ext2_filsys fs)
536{
537 struct progress_struct progress;
538 errcode_t retval;
539 char *buf;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530540 blk_t blk, err_blk;
541 int c, count, err_count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000542
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000543 retval = ext2fs_create_journal_superblock(fs,
544 fs->super->s_blocks_count, 0, &buf);
545 if (retval) {
546 com_err("create_journal_dev", retval,
547 _("while initializing journal superblock"));
548 exit(1);
549 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000550 if (quiet)
551 memset(&progress, 0, sizeof(progress));
552 else
553 progress_init(&progress, _("Zeroing journal device: "),
554 fs->super->s_blocks_count);
555
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530556 blk = 0;
557 count = fs->super->s_blocks_count;
558 while (count > 0) {
559 if (count > 1024)
560 c = 1024;
561 else
562 c = count;
563 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
564 if (retval) {
565 com_err("create_journal_dev", retval,
566 _("while zeroing journal device "
567 "(block %u, count %d)"),
568 err_blk, err_count);
569 exit(1);
570 }
571 blk += c;
572 count -= c;
573 progress_update(&progress, blk);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000574 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530575 ext2fs_zero_blocks(0, 0, 0, 0, 0);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000576
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000577 retval = io_channel_write_blk(fs->io,
578 fs->super->s_first_data_block+1,
579 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000580 if (retval) {
581 com_err("create_journal_dev", retval,
582 _("while writing journal superblock"));
583 exit(1);
584 }
585 progress_close(&progress);
586}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000587
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588static void show_stats(ext2_filsys fs)
589{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000590 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000591 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500592 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000593 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500594 dgrp_t i;
595 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000596
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500597 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500598 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500599 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000600
601 memset(buf, 0, sizeof(buf));
602 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000603 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500604 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500605 os = e2p_os2string(fs->super->s_creator_os);
606 fputs(os, stdout);
607 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000608 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000609 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000610 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000611 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000612 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000613 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000615 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 s->s_r_blocks_count,
617 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000618 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500619 if (s->s_reserved_gdt_blocks)
620 printf(_("Maximum filesystem blocks=%lu\n"),
621 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200622 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000623 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000624 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000625 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000626 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000627 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000629 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000630
631 if (fs->group_desc_count == 1) {
632 printf("\n");
633 return;
634 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500635
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000636 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637 group_block = s->s_first_data_block;
638 col_left = 0;
639 for (i = 1; i < fs->group_desc_count; i++) {
640 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000641 if (!ext2fs_bg_has_super(fs, i))
642 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000643 if (i != 1)
644 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000645 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000646 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000648 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000650 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000651 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000652 }
653 printf("\n\n");
654}
655
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000656/*
657 * Set the S_CREATOR_OS field. Return true if OS is known,
658 * otherwise, 0.
659 */
660static int set_os(struct ext2_super_block *sb, char *os)
661{
662 if (isdigit (*os))
663 sb->s_creator_os = atoi (os);
664 else if (strcasecmp(os, "linux") == 0)
665 sb->s_creator_os = EXT2_OS_LINUX;
666 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
667 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500668 else if (strcasecmp(os, "freebsd") == 0)
669 sb->s_creator_os = EXT2_OS_FREEBSD;
670 else if (strcasecmp(os, "lites") == 0)
671 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000672 else
673 return 0;
674 return 1;
675}
676
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000677#define PATH_SET "PATH=/sbin"
678
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500679static void parse_extended_opts(struct ext2_super_block *param,
680 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000681{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400682 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000683 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500684 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000685
686 len = strlen(opts);
687 buf = malloc(len+1);
688 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500689 fprintf(stderr,
690 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000691 exit(1);
692 }
693 strcpy(buf, opts);
694 for (token = buf; token && *token; token = next) {
695 p = strchr(token, ',');
696 next = 0;
697 if (p) {
698 *p = 0;
699 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500700 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000701 arg = strchr(token, '=');
702 if (arg) {
703 *arg = 0;
704 arg++;
705 }
706 if (strcmp(token, "stride") == 0) {
707 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500708 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500709 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000710 continue;
711 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500712 param->s_raid_stride = strtoul(arg, &p, 0);
713 if (*p || (param->s_raid_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000714 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400715 _("Invalid stride parameter: %s\n"),
716 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500717 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000718 continue;
719 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500720 } else if (strcmp(token, "stripe-width") == 0 ||
721 strcmp(token, "stripe_width") == 0) {
722 if (!arg) {
723 r_usage++;
724 badopt = token;
725 continue;
726 }
727 param->s_raid_stripe_width = strtoul(arg, &p, 0);
728 if (*p || (param->s_raid_stripe_width == 0)) {
729 fprintf(stderr,
730 _("Invalid stripe-width parameter: %s\n"),
731 arg);
732 r_usage++;
733 continue;
734 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500735 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500736 unsigned long resize, bpg, rsv_groups;
737 unsigned long group_desc_count, desc_blocks;
738 unsigned int gdpb, blocksize;
739 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500740
741 if (!arg) {
742 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500743 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500744 continue;
745 }
746
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500747 resize = parse_num_blocks(arg,
748 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500749
750 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500751 fprintf(stderr,
752 _("Invalid resize parameter: %s\n"),
753 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500754 r_usage++;
755 continue;
756 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500757 if (resize <= param->s_blocks_count) {
758 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400759 _("The resize maximum must be greater "
760 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500761 r_usage++;
762 continue;
763 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500764
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500765 blocksize = EXT2_BLOCK_SIZE(param);
766 bpg = param->s_blocks_per_group;
767 if (!bpg)
768 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200769 gdpb = EXT2_DESC_PER_BLOCK(param);
Theodore Ts'o69022e02006-08-30 01:57:00 -0400770 group_desc_count =
771 ext2fs_div_ceil(param->s_blocks_count, bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500772 desc_blocks = (group_desc_count +
773 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400774 rsv_groups = ext2fs_div_ceil(resize, bpg);
775 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500776 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500777 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500778 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
779
780 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400781 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
782 fprintf(stderr,
783 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400784 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400785 exit(1);
786 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500787 param->s_feature_compat |=
788 EXT2_FEATURE_COMPAT_RESIZE_INODE;
789
790 param->s_reserved_gdt_blocks = rsv_gdb;
791 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500792 } else if (!strcmp(token, "test_fs")) {
793 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400794 } else if (!strcmp(token, "lazy_itable_init")) {
Theodore Ts'o43781b92008-04-27 19:38:02 -0400795 if (arg)
796 lazy_itable_init = strtoul(arg, &p, 0);
797 else
798 lazy_itable_init = 1;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500799 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500800 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500801 badopt = token;
802 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000803 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500804 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500805 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400806 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000807 "and may take an argument which\n"
808 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400809 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500810 "\tstride=<RAID per-disk data chunk in blocks>\n"
811 "\tstripe-width=<RAID stride * data disks in blocks>\n"
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400812 "\tresize=<resize maximum size in blocks>\n"
813 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
814 "\ttest_fs\n\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400815 badopt ? badopt : "");
Brian Behlendorf21400382007-05-31 11:30:47 -0400816 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000817 exit(1);
818 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500819 if (param->s_raid_stride &&
820 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
821 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
822 "multiple of stride %u.\n\n"),
823 param->s_raid_stripe_width, param->s_raid_stride);
824
Brian Behlendorf21400382007-05-31 11:30:47 -0400825 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000826}
827
Theodore Ts'o896938d1999-10-23 01:04:50 +0000828static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500829 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400830 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500831 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400832 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o558df542008-02-27 15:01:19 -0500833 EXT2_FEATURE_COMPAT_EXT_ATTR,
834 /* Incompat */
835 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'obf6b8482008-02-20 08:13:19 -0500836 EXT3_FEATURE_INCOMPAT_EXTENTS|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400837 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500838 EXT2_FEATURE_INCOMPAT_META_BG|
839 EXT4_FEATURE_INCOMPAT_FLEX_BG,
Theodore Ts'o558df542008-02-27 15:01:19 -0500840 /* R/O compat */
841 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500842 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
843 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
Theodore Ts'o896938d1999-10-23 01:04:50 +0000844};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000845
846
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500847static void syntax_err_report(const char *filename, long err, int line_num)
848{
849 fprintf(stderr,
850 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
851 filename, line_num, error_message(err));
852 exit(1);
853}
854
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200855static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500856
857static void edit_feature(const char *str, __u32 *compat_array)
858{
859 if (!str)
860 return;
861
862 if (e2p_edit_feature(str, compat_array, ok_features)) {
863 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
864 str);
865 exit(1);
866 }
867}
868
Theodore Ts'o3d438362008-02-19 08:32:58 -0500869struct str_list {
870 char **list;
871 int num;
872 int max;
873};
874
875static errcode_t init_list(struct str_list *sl)
876{
877 sl->num = 0;
878 sl->max = 0;
879 sl->list = malloc((sl->max+1) * sizeof(char *));
880 if (!sl->list)
881 return ENOMEM;
882 sl->list[0] = 0;
883 return 0;
884}
885
886static errcode_t push_string(struct str_list *sl, const char *str)
887{
888 char **new_list;
889
890 if (sl->num >= sl->max) {
891 sl->max += 2;
892 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
893 if (!new_list)
894 return ENOMEM;
895 sl->list = new_list;
896 }
897 sl->list[sl->num] = malloc(strlen(str)+1);
898 if (sl->list[sl->num] == 0)
899 return ENOMEM;
900 strcpy(sl->list[sl->num], str);
901 sl->num++;
902 sl->list[sl->num] = 0;
903 return 0;
904}
905
906static void print_str_list(char **list)
907{
908 char **cpp;
909
910 for (cpp = list; *cpp; cpp++) {
911 printf("'%s'", *cpp);
912 if (cpp[1])
913 fputs(", ", stdout);
914 }
915 fputc('\n', stdout);
916}
917
918static char **parse_fs_type(const char *fs_type,
919 const char *usage_types,
920 struct ext2_super_block *fs_param,
921 char *progname)
922{
923 const char *ext_type = 0;
924 char *parse_str;
925 char *profile_type = 0;
926 char *cp, *t;
927 const char *size_type;
928 struct str_list list;
929 int state = 0;
930 unsigned long meg;
931
932 if (init_list(&list))
933 return 0;
934
935 if (fs_type)
936 ext_type = fs_type;
937 else if (progname) {
938 ext_type = strrchr(progname, '/');
939 if (ext_type)
940 ext_type++;
941 else
942 ext_type = progname;
943
944 if (!strncmp(ext_type, "mkfs.", 5)) {
945 ext_type += 5;
946 if (ext_type[0] == 0)
947 ext_type = 0;
948 } else
949 ext_type = 0;
950 }
951
952 if (!ext_type) {
953 profile_get_string(profile, "defaults", "fs_type", 0,
954 "ext2", &profile_type);
955 ext_type = profile_type;
956 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
957 ext_type = "ext3";
958 }
959
960 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
961 if (fs_param->s_blocks_count < 3 * meg)
962 size_type = "floppy";
963 else if (fs_param->s_blocks_count < 512 * meg)
964 size_type = "small";
965 else
966 size_type = "default";
967
968 if (!usage_types)
969 usage_types = size_type;
970
971 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
972 if (!parse_str) {
973 free(list.list);
974 return 0;
975 }
976 if (usage_types)
977 strcpy(parse_str, usage_types);
978 else
979 *parse_str = '\0';
980
981 if (ext_type)
982 push_string(&list, ext_type);
983 cp = parse_str;
984 while (1) {
985 t = strchr(cp, ',');
986 if (t)
987 *t = '\0';
988
989 if (*cp)
990 push_string(&list, cp);
991 if (t)
992 cp = t+1;
993 else {
994 cp = "";
995 break;
996 }
997 }
998 free(parse_str);
999 if (profile_type)
1000 free(profile_type);
1001 return (list.list);
1002}
1003
1004static char *get_string_from_profile(char **fs_types, const char *opt,
1005 const char *def_val)
1006{
1007 char *ret = 0;
1008 char **cpp;
1009 int i;
1010
1011 for (i=0; fs_types[i]; i++);
1012 for (i-=1; i >=0 ; i--) {
1013 profile_get_string(profile, "fs_types", fs_types[i],
1014 opt, 0, &ret);
1015 if (ret)
1016 return ret;
1017 }
1018 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1019 return (ret);
1020}
1021
1022static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1023{
1024 int ret;
1025 char **cpp;
1026
1027 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1028 for (cpp = fs_types; *cpp; cpp++)
1029 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1030 return ret;
1031}
1032
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001033static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1034{
1035 int ret;
1036 char **cpp;
1037
1038 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1039 for (cpp = fs_types; *cpp; cpp++)
1040 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1041 return ret;
1042}
Theodore Ts'o3d438362008-02-19 08:32:58 -05001043
Theodore Ts'od48bc602007-07-04 14:10:46 -04001044extern const char *mke2fs_default_profile;
1045static const char *default_files[] = { "<default>", 0 };
1046
Theodore Ts'o3839e651997-04-26 13:21:57 +00001047static void PRS(int argc, char *argv[])
1048{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001049 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001050 int size;
Theodore Ts'o2d363582008-05-14 18:09:37 -04001051 char *tmp, *tmp2, **cpp;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001052 int blocksize = 0;
1053 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -06001054 int inode_size = 0;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001055 unsigned long flex_bg_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -05001056 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001057 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001058 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001059 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001060 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001061 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001062 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -05001063 const char * fs_type = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001064 const char * usage_types = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001065 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -05001066#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +00001067 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +00001068#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001069 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001070 int s_opt = -1, r_opt = -1;
1071 char *fs_features = 0;
1072 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -05001073 char *newpath;
1074 int pathlen = sizeof(PATH_SET) + 1;
1075
1076 if (oldpath)
1077 pathlen += strlen(oldpath);
1078 newpath = malloc(pathlen);
1079 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001080
Theodore Ts'o3839e651997-04-26 13:21:57 +00001081 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001082 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001083 strcat (newpath, ":");
1084 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -05001085 }
1086 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001087
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001088 tmp = getenv("MKE2FS_SYNC");
1089 if (tmp)
1090 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001091
1092 /* Determine the system page size if possible */
1093#ifdef HAVE_SYSCONF
1094#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1095#define _SC_PAGESIZE _SC_PAGE_SIZE
1096#endif
1097#ifdef _SC_PAGESIZE
1098 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -06001099 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001100 sys_page_size = sysval;
1101#endif /* _SC_PAGESIZE */
1102#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001103
1104 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1105 config_fn[0] = tmp;
1106 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001107 retval = profile_init(config_fn, &profile);
1108 if (retval == ENOENT) {
1109 profile_init(default_files, &profile);
1110 profile_set_default(profile, mke2fs_default_profile);
1111 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001112
Theodore Ts'o3839e651997-04-26 13:21:57 +00001113 setbuf(stdout, NULL);
1114 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001115 add_error_table(&et_ext2_error_table);
1116 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001117 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1118 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001119
Theodore Ts'o756df352002-03-07 20:52:12 -05001120#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001121 if (uname(&ut)) {
1122 perror("uname");
1123 exit(1);
1124 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001125 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001126 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001127 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001128#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001129
1130 if (argc && *argv) {
1131 program_name = get_progname(*argv);
1132
1133 /* If called as mkfs.ext3, create a journal inode */
1134 if (!strcmp(program_name, "mkfs.ext3"))
1135 journal_size = -1;
1136 }
1137
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001138 while ((c = getopt (argc, argv,
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001139 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001140 switch (c) {
1141 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001142 blocksize = strtol(optarg, &tmp, 0);
1143 b = (blocksize > 0) ? blocksize : -blocksize;
1144 if (b < EXT2_MIN_BLOCK_SIZE ||
1145 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001146 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001147 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001148 exit(1);
1149 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001150 if (blocksize > 4096)
1151 fprintf(stderr, _("Warning: blocksize %d not "
1152 "usable on most systems.\n"),
1153 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001154 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001155 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001156 int_log2(blocksize >>
1157 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001158 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001159 case 'c': /* Check for bad blocks */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001160 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001161 break;
1162 case 'f':
1163 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001164 if (size < EXT2_MIN_BLOCK_SIZE ||
1165 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001166 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001167 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001168 optarg);
1169 exit(1);
1170 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001171 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001172 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001173 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001174 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001175 break;
1176 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001177 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001178 if (*tmp) {
1179 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001180 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001181 exit(1);
1182 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001183 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001184 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001185 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001186 exit(1);
1187 }
1188 break;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001189 case 'G':
1190 flex_bg_size = strtoul(optarg, &tmp, 0);
1191 if (*tmp) {
1192 com_err(program_name, 0,
1193 _("Illegal number for flex_bg size"));
1194 exit(1);
1195 }
1196 if (flex_bg_size < 2 ||
1197 (flex_bg_size & (flex_bg_size-1)) != 0) {
1198 com_err(program_name, 0,
1199 _("flex_bg size must be a power of 2"));
1200 exit(1);
1201 }
1202 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001203 case 'i':
1204 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001205 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1206 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001207 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001208 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001209 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001210 optarg, EXT2_MIN_BLOCK_SIZE,
1211 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001212 exit(1);
1213 }
1214 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001215 case 'J':
1216 parse_journal_opts(optarg);
1217 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001218 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001219 if (!journal_size)
1220 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001221 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001222 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001223 bad_blocks_filename = malloc(strlen(optarg)+1);
1224 if (!bad_blocks_filename) {
1225 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001226 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001227 exit(1);
1228 }
1229 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001230 break;
1231 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001232 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001233 if (reserved_ratio > 50 || *tmp) {
1234 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001235 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001236 optarg);
1237 exit(1);
1238 }
1239 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001240 case 'n':
1241 noaction++;
1242 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001243 case 'o':
1244 creator_os = optarg;
1245 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001246 case 'q':
1247 quiet = 1;
1248 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001249 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001250 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001251 if (*tmp) {
1252 com_err(program_name, 0,
1253 _("bad revision level - %s"), optarg);
1254 exit(1);
1255 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001256 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001257 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001258 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001259 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001260 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001261 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001262 inode_size = strtoul(optarg, &tmp, 0);
1263 if (*tmp) {
1264 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001265 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001266 exit(1);
1267 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001268 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001269 case 'v':
1270 verbose = 1;
1271 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001272 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001273 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001274 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001275 case 'L':
1276 volume_label = optarg;
1277 break;
1278 case 'M':
1279 mount_dir = optarg;
1280 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001281 case 'N':
1282 num_inodes = strtoul(optarg, &tmp, 0);
1283 if (*tmp) {
1284 com_err(program_name, 0,
1285 _("bad num inodes - %s"), optarg);
1286 exit(1);
1287 }
1288 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001289 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001290 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001291 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001292 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001293 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001294 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001295 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001296 case 'S':
1297 super_only = 1;
1298 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001299 case 't':
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001300 fs_type = optarg;
1301 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001302 case 'T':
1303 usage_types = optarg;
1304 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001305 case 'V':
1306 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001307 show_version_only++;
1308 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001309 default:
1310 usage();
1311 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001312 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001313 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001314 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001315 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001316
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001317 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001318 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1319 E2FSPROGS_DATE);
1320
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001321 if (show_version_only) {
1322 fprintf(stderr, _("\tUsing %s\n"),
1323 error_message(EXT2_ET_BASE));
1324 exit(0);
1325 }
1326
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001327 /*
1328 * If there's no blocksize specified and there is a journal
1329 * device, use it to figure out the blocksize
1330 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001331 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001332 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001333 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001334
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001335#ifdef CONFIG_TESTIO_DEBUG
1336 io_ptr = test_io_manager;
1337 test_io_backing_manager = unix_io_manager;
1338#else
1339 io_ptr = unix_io_manager;
1340#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001341 retval = ext2fs_open(journal_device,
1342 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001343 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001344 if (retval) {
1345 com_err(program_name, retval,
1346 _("while trying to open journal device %s\n"),
1347 journal_device);
1348 exit(1);
1349 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001350 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001351 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001352 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001353 "minimum blocksize %d\n"), jfs->blocksize,
1354 -blocksize);
1355 exit(1);
1356 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001357 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001358 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001359 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1360 ext2fs_close(jfs);
1361 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001362
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001363 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001364 if (!force) {
1365 com_err(program_name, 0,
1366 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001367 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001368 proceed_question();
1369 }
1370 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1371 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001372 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001373 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001374 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001375 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1376 fs_param.s_log_block_size);
1377 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001378 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001379 argv[optind - 1]);
1380 exit(1);
1381 }
1382 }
1383 if (optind < argc)
1384 usage();
1385
Theodore Ts'o74becf31997-04-26 14:37:06 +00001386 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001387 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001388 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001389
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001390 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001391
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001392 if (noaction && fs_param.s_blocks_count) {
1393 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001394 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001395 } else {
1396 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001397 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001398 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001399 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001400 if ((retval == EFBIG) &&
1401 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001402 (fs_param.s_log_block_size == 0)) {
1403 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001404 blocksize = 4096;
1405 goto retry;
1406 }
1407 }
1408
Theodore Ts'oa789d841998-03-30 01:20:55 +00001409 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1410 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001411 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001412 exit(1);
1413 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001414 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001415 if (retval == EXT2_ET_UNIMPLEMENTED) {
1416 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001417 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001418 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001419 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001420 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001421 } else {
1422 if (dev_size == 0) {
1423 com_err(program_name, 0,
1424 _("Device size reported to be zero. "
1425 "Invalid partition specified, or\n\t"
1426 "partition table wasn't reread "
1427 "after running fdisk, due to\n\t"
1428 "a modified partition being busy "
1429 "and in use. You may need to reboot\n\t"
1430 "to re-read your partition table.\n"
1431 ));
1432 exit(1);
1433 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001434 fs_param.s_blocks_count = dev_size;
1435 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1436 fs_param.s_blocks_count &= ~((sys_page_size /
1437 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001438 }
1439
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001440 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001441 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001442 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001443 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001444 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001445
Theodore Ts'o3d438362008-02-19 08:32:58 -05001446 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1447 if (!fs_types) {
1448 fprintf(stderr, _("Failed to parse fs types list\n"));
1449 exit(1);
1450 }
1451 if (verbose) {
1452 fputs("Fs_types for mke2fs.conf resolution: ", stdout);
1453 print_str_list(fs_types);
1454 }
1455
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001456 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001457 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001458 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1459
Brian Behlendorfe0661502007-03-19 08:25:38 -04001460 if (fs_param.s_feature_incompat &
1461 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1462 fs_type = "journal";
1463 else if (megs <= 3)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001464 fs_type = "floppy";
1465 else if (megs <= 512)
1466 fs_type = "small";
1467 else
1468 fs_type = "default";
1469 }
1470
1471 /* Figure out what features should be enabled */
1472
Theodore Ts'o3d438362008-02-19 08:32:58 -05001473 tmp = NULL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001474 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001475 tmp = get_string_from_profile(fs_types, "base_features",
1476 "sparse_super,filetype,resize_inode,dir_index");
1477 edit_feature(tmp, &fs_param.s_feature_compat);
1478 free(tmp);
1479
1480 for (cpp = fs_types; *cpp; cpp++) {
1481 tmp = NULL;
1482 profile_get_string(profile, "fs_types", *cpp,
1483 "features", "", &tmp);
1484 if (tmp && *tmp)
1485 edit_feature(tmp, &fs_param.s_feature_compat);
1486 if (tmp)
1487 free(tmp);
1488 }
1489 tmp = get_string_from_profile(fs_types, "default_features",
1490 "");
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001491 }
Theodore Ts'o3d438362008-02-19 08:32:58 -05001492 edit_feature(fs_features ? fs_features : tmp,
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001493 &fs_param.s_feature_compat);
1494 if (tmp)
1495 free(tmp);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001496
1497 if (r_opt == EXT2_GOOD_OLD_REV &&
1498 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1499 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001500 fprintf(stderr, _("Filesystem features not supported "
1501 "with revision 0 filesystems\n"));
1502 exit(1);
1503 }
1504
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001505 if (s_opt > 0) {
1506 if (r_opt == EXT2_GOOD_OLD_REV) {
1507 fprintf(stderr, _("Sparse superblocks not supported "
1508 "with revision 0 filesystems\n"));
1509 exit(1);
1510 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001511 fs_param.s_feature_ro_compat |=
1512 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001513 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001514 fs_param.s_feature_ro_compat &=
1515 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1516
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001517 if (journal_size != 0) {
1518 if (r_opt == EXT2_GOOD_OLD_REV) {
1519 fprintf(stderr, _("Journals not supported "
1520 "with revision 0 filesystems\n"));
1521 exit(1);
1522 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001523 fs_param.s_feature_compat |=
1524 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001525 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001526
1527 if (fs_param.s_feature_incompat &
1528 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001529 reserved_ratio = 0;
1530 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1531 fs_param.s_feature_compat = 0;
1532 fs_param.s_feature_ro_compat = 0;
1533 }
Theodore Ts'od94cc2e2008-04-27 00:08:14 -04001534
1535 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1536 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1537 fprintf(stderr, _("The resize_inode and meta_bg features "
1538 "are not compatible.\n"
1539 "They can not be both enabled "
1540 "simultaneously.\n"));
1541 exit(1);
1542 }
1543
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001544 /* Set first meta blockgroup via an environment variable */
1545 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001546 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001547 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001548 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001549
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001550 /* Get the hardware sector size, if available */
1551 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1552 if (retval) {
1553 com_err(program_name, retval,
1554 _("while trying to determine hardware sector size"));
1555 exit(1);
1556 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001557
Theodore Ts'o54434922003-12-07 01:28:50 -05001558 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001559 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001560
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001561 if (blocksize <= 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001562 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001563
1564 if (use_bsize == -1) {
1565 use_bsize = sys_page_size;
1566 if ((linux_version_code < (2*65536 + 6*256)) &&
1567 (use_bsize > 4096))
1568 use_bsize = 4096;
1569 }
1570 if (sector_size && use_bsize < sector_size)
1571 use_bsize = sector_size;
1572 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1573 use_bsize = -blocksize;
1574 blocksize = use_bsize;
1575 fs_param.s_blocks_count /= blocksize / 1024;
1576 }
1577
1578 if (inode_ratio == 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001579 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1580 8192);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001581 if (inode_ratio < blocksize)
1582 inode_ratio = blocksize;
1583 }
1584
1585 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1586 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1587
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001588 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001589
1590 lazy_itable_init = get_bool_from_profile(fs_types,
1591 "lazy_itable_init", 0);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001592
Theodore Ts'o2d363582008-05-14 18:09:37 -04001593 /* Get options from profile */
1594 for (cpp = fs_types; *cpp; cpp++) {
1595 tmp = NULL;
1596 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1597 if (tmp && *tmp)
1598 parse_extended_opts(&fs_param, tmp);
1599 if (tmp)
1600 free(tmp);
1601 }
1602
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001603 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001604 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001605
1606 /* Since sparse_super is the default, we would only have a problem
1607 * here if it was explicitly disabled.
1608 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001609 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1610 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001611 com_err(program_name, 0,
1612 _("reserved online resize blocks not supported "
1613 "on non-sparse filesystem"));
1614 exit(1);
1615 }
1616
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001617 if (fs_param.s_blocks_per_group) {
1618 if (fs_param.s_blocks_per_group < 256 ||
1619 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001620 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001621 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001622 exit(1);
1623 }
1624 }
1625
Theodore Ts'o3d438362008-02-19 08:32:58 -05001626 if (inode_size == 0)
1627 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001628 if (!flex_bg_size && (fs_param.s_feature_incompat &
1629 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1630 flex_bg_size = get_int_from_profile(fs_types,
1631 "flex_bg_size", 16);
1632 if (flex_bg_size) {
1633 if (!(fs_param.s_feature_incompat &
1634 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1635 com_err(program_name, 0,
1636 _("Flex_bg feature not enabled, so "
1637 "flex_bg size may not be specified"));
1638 exit(1);
1639 }
1640 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1641 }
Andreas Dilger067911a2006-07-15 22:08:20 -04001642
1643 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001644 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001645 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001646 inode_size & (inode_size - 1)) {
1647 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001648 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001649 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001650 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001651 exit(1);
1652 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001653 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001654 }
1655
Eric Sandeenf3358642006-09-12 14:56:17 -04001656 /* Make sure number of inodes specified will fit in 32 bits */
1657 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001658 unsigned long long n;
1659 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
Eric Sandeenf3358642006-09-12 14:56:17 -04001660 if (n > ~0U) {
1661 com_err(program_name, 0,
1662 _("too many inodes (%llu), raise inode ratio?"), n);
1663 exit(1);
1664 }
1665 } else if (num_inodes > ~0U) {
1666 com_err(program_name, 0,
1667 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001668 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04001669 exit(1);
1670 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001671 /*
1672 * Calculate number of inodes based on the inode ratio
1673 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001674 fs_param.s_inodes_count = num_inodes ? num_inodes :
1675 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001676 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001677
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001678 if ((((long long)fs_param.s_inodes_count) *
1679 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1680 (((long long)fs_param.s_blocks_count) *
1681 EXT2_BLOCK_SIZE(&fs_param))) {
1682 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1683 "(%u) too big for a\n\t"
1684 "filesystem with %lu blocks, "
1685 "specify higher inode_ratio (-i)\n\t"
1686 "or lower inode count (-N).\n"),
1687 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001688 fs_param.s_inodes_count,
1689 (unsigned long) fs_param.s_blocks_count);
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001690 exit(1);
1691 }
1692
Theodore Ts'o3839e651997-04-26 13:21:57 +00001693 /*
1694 * Calculate number of blocks to reserve
1695 */
Theodore Ts'oa8862d92006-08-30 03:08:13 -04001696 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1697 fs_param.s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001698}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001699
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301700static int should_do_undo(const char *name)
1701{
1702 errcode_t retval;
1703 io_channel channel;
1704 __u16 s_magic;
1705 struct ext2_super_block super;
1706 io_manager manager = unix_io_manager;
1707 int csum_flag, force_undo;
1708
1709 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1710 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1711 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1712 if (!force_undo && (!csum_flag || !lazy_itable_init))
1713 return 0;
1714
1715 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1716 if (retval) {
1717 /*
1718 * We don't handle error cases instead we
1719 * declare that the file system doesn't exist
1720 * and let the rest of mke2fs take care of
1721 * error
1722 */
1723 retval = 0;
1724 goto open_err_out;
1725 }
1726
1727 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1728 retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
1729 if (retval) {
1730 retval = 0;
1731 goto err_out;
1732 }
1733
1734#if defined(WORDS_BIGENDIAN)
1735 s_magic = ext2fs_swab16(super.s_magic);
1736#else
1737 s_magic = super.s_magic;
1738#endif
1739
1740 if (s_magic == EXT2_SUPER_MAGIC)
1741 retval = 1;
1742
1743err_out:
1744 io_channel_close(channel);
1745
1746open_err_out:
1747
1748 return retval;
1749}
1750
1751static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1752{
1753 errcode_t retval = 0;
1754 char *tdb_dir, tdb_file[PATH_MAX];
1755 char *device_name, *tmp_name;
1756
1757 /*
1758 * Configuration via a conf file would be
1759 * nice
1760 */
1761 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1762 if (!tdb_dir)
1763 profile_get_string(profile, "defaults",
1764 "undo_dir", 0, "/var/lib/e2fsprogs",
1765 &tdb_dir);
1766
1767 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1768 access(tdb_dir, W_OK))
1769 return 0;
1770
1771 tmp_name = strdup(name);
1772 device_name = basename(tmp_name);
1773 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1774
1775 if (!access(tdb_file, F_OK)) {
1776 if (unlink(tdb_file) < 0) {
1777 retval = errno;
1778 com_err(program_name, retval,
1779 _("while trying to delete %s"),
1780 tdb_file);
1781 return retval;
1782 }
1783 }
1784
1785 set_undo_io_backing_manager(*io_ptr);
1786 *io_ptr = undo_io_manager;
1787 set_undo_io_backup_file(tdb_file);
1788 printf(_("Overwriting existing filesystem; this can be undone "
1789 "using the command:\n"
1790 " e2undo %s %s\n\n"), tdb_file, name);
1791err_out:
1792 free(tmp_name);
1793 return retval;
1794}
1795
Theodore Ts'o3839e651997-04-26 13:21:57 +00001796int main (int argc, char *argv[])
1797{
1798 errcode_t retval = 0;
1799 ext2_filsys fs;
1800 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001801 unsigned int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001802 unsigned int i;
1803 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001804 io_manager io_ptr;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301805 char tdb_string[40];
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001806
1807#ifdef ENABLE_NLS
1808 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001809 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001810 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1811 textdomain(NLS_CAT_NAME);
1812#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001813 PRS(argc, argv);
1814
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001815#ifdef CONFIG_TESTIO_DEBUG
1816 io_ptr = test_io_manager;
1817 test_io_backing_manager = unix_io_manager;
1818#else
1819 io_ptr = unix_io_manager;
1820#endif
1821
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301822 if (should_do_undo(device_name)) {
1823 retval = mke2fs_setup_tdb(device_name, &io_ptr);
1824 if (retval)
1825 exit(1);
1826 }
1827
Theodore Ts'o3839e651997-04-26 13:21:57 +00001828 /*
1829 * Initialize the superblock....
1830 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001831 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001832 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001833 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001834 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001835 exit(1);
1836 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301837 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1838 32768 : fs->blocksize * 8);
1839 io_channel_set_options(fs->io, tdb_string);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001840
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001841 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1842 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1843
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001844 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001845 * Wipe out the old on-disk superblock
1846 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001847 if (!noaction)
1848 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001849
1850 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001851 * Generate a UUID for it...
1852 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001853 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001854
1855 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001856 * Initialize the directory index variables
1857 */
1858 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1859 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1860
1861 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001862 * Add "jitter" to the superblock's check interval so that we
1863 * don't check all the filesystems at the same time. We use a
1864 * kludgy hack of using the UUID to derive a random jitter value.
1865 */
1866 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1867 val += fs->super->s_uuid[i];
1868 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1869
1870 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001871 * Override the creator OS, if applicable
1872 */
1873 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001874 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001875 exit(1);
1876 }
1877
1878 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001879 * For the Hurd, we will turn off filetype since it doesn't
1880 * support it.
1881 */
1882 if (fs->super->s_creator_os == EXT2_OS_HURD)
1883 fs->super->s_feature_incompat &=
1884 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1885
1886 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001887 * Set the volume label...
1888 */
1889 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001890 memset(fs->super->s_volume_name, 0,
1891 sizeof(fs->super->s_volume_name));
1892 strncpy(fs->super->s_volume_name, volume_label,
1893 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001894 }
1895
1896 /*
1897 * Set the last mount directory
1898 */
1899 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001900 memset(fs->super->s_last_mounted, 0,
1901 sizeof(fs->super->s_last_mounted));
1902 strncpy(fs->super->s_last_mounted, mount_dir,
1903 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001904 }
1905
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001906 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001907 show_stats(fs);
1908
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001909 if (noaction)
1910 exit(0);
1911
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001912 if (fs->super->s_feature_incompat &
1913 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1914 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001915 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001916 }
1917
Theodore Ts'o3839e651997-04-26 13:21:57 +00001918 if (bad_blocks_filename)
1919 read_bb_file(fs, &bb_list, bad_blocks_filename);
1920 if (cflag)
1921 test_disk(fs, &bb_list);
1922
1923 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001924 fs->stride = fs_stride = fs->super->s_raid_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001925 retval = ext2fs_allocate_tables(fs);
1926 if (retval) {
1927 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001928 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001929 exit(1);
1930 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001931 if (super_only) {
1932 fs->super->s_state |= EXT2_ERROR_FS;
1933 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1934 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001935 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001936 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001937 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001938 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001939 blk_t ret_blk;
1940
1941#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001942 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001943#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001944
1945 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001946 * Wipe out any old MD RAID (or other) metadata at the end
1947 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001948 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001949 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001950 start = (blocks & ~(rsv - 1));
1951 if (start > rsv)
1952 start -= rsv;
1953 if (start > 0)
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301954 retval = ext2fs_zero_blocks(fs, start, blocks - start,
1955 &ret_blk, NULL);
Andreas Dilger59f27242001-08-30 15:39:04 -06001956
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001957 if (retval) {
1958 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001959 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001960 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001961 }
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001962 write_inode_tables(fs, lazy_itable_init);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001963 create_root_dir(fs);
1964 create_lost_and_found(fs);
1965 reserve_inodes(fs);
1966 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001967 if (fs->super->s_feature_compat &
1968 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1969 retval = ext2fs_create_resize_inode(fs);
1970 if (retval) {
1971 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001972 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001973 exit(1);
1974 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001975 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001976 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001977
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001978 if (journal_device) {
1979 ext2_filsys jfs;
1980
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001981 if (!force)
1982 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001983 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001984
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001985 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1986 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1987 fs->blocksize, unix_io_manager, &jfs);
1988 if (retval) {
1989 com_err(program_name, retval,
1990 _("while trying to open journal device %s\n"),
1991 journal_device);
1992 exit(1);
1993 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001994 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001995 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001996 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001997 fflush(stdout);
1998 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001999 retval = ext2fs_add_journal_device(fs, jfs);
2000 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002001 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00002002 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002003 journal_device);
2004 exit(1);
2005 }
2006 if (!quiet)
2007 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002008 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06002009 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002010 } else if ((journal_size) ||
2011 (fs_param.s_feature_compat &
2012 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002013 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002014
2015 if (!journal_blocks) {
2016 fs->super->s_feature_compat &=
2017 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2018 goto no_journal;
2019 }
2020 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04002021 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00002022 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002023 fflush(stdout);
2024 }
Theodore Ts'o63985322001-01-03 17:02:13 +00002025 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2026 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002027 if (retval) {
2028 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00002029 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002030 exit(1);
2031 }
2032 if (!quiet)
2033 printf(_("done\n"));
2034 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002035no_journal:
2036
Theodore Ts'o3839e651997-04-26 13:21:57 +00002037 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002038 printf(_("Writing superblocks and "
2039 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002040 retval = ext2fs_flush(fs);
2041 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05002042 fprintf(stderr,
2043 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002044 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002045 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002046 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04002047 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2048 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002049 }
Andreas Dilger568101f2001-10-13 01:22:25 -06002050 val = ext2fs_close(fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002051 remove_error_table(&et_ext2_error_table);
2052 remove_error_table(&et_prof_error_table);
Theodore Ts'o3d438362008-02-19 08:32:58 -05002053 profile_release(profile);
Andreas Dilger568101f2001-10-13 01:22:25 -06002054 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002055}